| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Atomic float implementation based on atomic integer. |
| 5 |
|
| 6 |
This module provides atomic float implementations using atomic integer. |
| 7 |
|
| 8 |
Note that most of `fetch_*` operations of atomic floats are implemented using |
| 9 |
CAS loops, which can be slower than equivalent operations of atomic integers. |
| 10 |
|
| 11 |
AArch64 with FEAT_LSFE and GPU targets have atomic instructions for float. |
| 12 |
See aarch64.rs for AArch64 with FEAT_LSFE. |
| 13 |
GPU targets will also use architecture-specific implementations instead of this implementation in the |
| 14 |
future: https://github.com/taiki-e/portable-atomic/issues/34 / https://github.com/taiki-e/portable-atomic/pull/45 |
| 15 |
*/ |
| 16 |
|
| 17 |
// TODO: fetch_{minimum,maximum}* https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3008r2.html / https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p0493r5.pdf |
| 18 |
|
| 19 |
#![cfg_attr( |
| 20 |
all(target_pointer_width = "16", not(feature = "fallback")), |
| 21 |
allow(unused_imports, unused_macros) |
| 22 |
)] |
| 23 |
|
| 24 |
use core::{cell::UnsafeCell, sync::atomic::Ordering}; |
| 25 |
|
| 26 |
macro_rules! atomic_float { |
| 27 |
( |
| 28 |
$atomic_type:ident, $float_type:ident, $atomic_int_type:ident, $int_type:ident, |
| 29 |
$align:literal |
| 30 |
) => { |
| 31 |
#[repr(C, align($align))] |
| 32 |
pub(crate) struct $atomic_type { |
| 33 |
v: UnsafeCell<$float_type>, |
| 34 |
} |
| 35 |
|
| 36 |
// Send is implicitly implemented. |
| 37 |
// SAFETY: any data races are prevented by atomic operations. |
| 38 |
unsafe impl Sync for $atomic_type {} |
| 39 |
|
| 40 |
impl $atomic_type { |
| 41 |
#[inline] |
| 42 |
pub(crate) const fn new(v: $float_type) -> Self { |
| 43 |
Self { v: UnsafeCell::new(v) } |
| 44 |
} |
| 45 |
|
| 46 |
#[inline] |
| 47 |
pub(crate) fn is_lock_free() -> bool { |
| 48 |
crate::$atomic_int_type::is_lock_free() |
| 49 |
} |
| 50 |
pub(crate) const IS_ALWAYS_LOCK_FREE: bool = |
| 51 |
crate::$atomic_int_type::is_always_lock_free(); |
| 52 |
|
| 53 |
#[inline] |
| 54 |
#[cfg_attr( |
| 55 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 56 |
track_caller |
| 57 |
)] |
| 58 |
pub(crate) fn load(&self, order: Ordering) -> $float_type { |
| 59 |
$float_type::from_bits(self.as_bits().load(order)) |
| 60 |
} |
| 61 |
|
| 62 |
#[inline] |
| 63 |
#[cfg_attr( |
| 64 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 65 |
track_caller |
| 66 |
)] |
| 67 |
pub(crate) fn store(&self, val: $float_type, order: Ordering) { |
| 68 |
self.as_bits().store(val.to_bits(), order) |
| 69 |
} |
| 70 |
|
| 71 |
const_fn! { |
| 72 |
const_if: #[cfg(not(portable_atomic_no_const_raw_ptr_deref))]; |
| 73 |
#[inline(always)] |
| 74 |
pub(crate) const fn as_bits(&self) -> &crate::$atomic_int_type { |
| 75 |
// SAFETY: $atomic_type and $atomic_int_type have the same layout, |
| 76 |
// and there is no concurrent access to the value that does not go through this method. |
| 77 |
unsafe { &*(self as *const Self as *const crate::$atomic_int_type) } |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#[inline] |
| 82 |
pub(crate) const fn as_ptr(&self) -> *mut $float_type { |
| 83 |
self.v.get() |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
cfg_has_atomic_cas_or_amo32! { |
| 88 |
impl $atomic_type { |
| 89 |
#[inline] |
| 90 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 91 |
pub(crate) fn swap(&self, val: $float_type, order: Ordering) -> $float_type { |
| 92 |
$float_type::from_bits(self.as_bits().swap(val.to_bits(), order)) |
| 93 |
} |
| 94 |
#[inline] |
| 95 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 96 |
pub(crate) fn fetch_neg(&self, order: Ordering) -> $float_type { |
| 97 |
const NEG_MASK: $int_type = !0 / 2 + 1; |
| 98 |
$float_type::from_bits(self.as_bits().fetch_xor(NEG_MASK, order)) |
| 99 |
} |
| 100 |
|
| 101 |
#[inline] |
| 102 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 103 |
pub(crate) fn fetch_abs(&self, order: Ordering) -> $float_type { |
| 104 |
const ABS_MASK: $int_type = !0 / 2; |
| 105 |
$float_type::from_bits(self.as_bits().fetch_and(ABS_MASK, order)) |
| 106 |
} |
| 107 |
} |
| 108 |
cfg_has_atomic_cas! { |
| 109 |
impl $atomic_type { |
| 110 |
#[inline] |
| 111 |
#[cfg_attr( |
| 112 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 113 |
track_caller |
| 114 |
)] |
| 115 |
pub(crate) fn compare_exchange( |
| 116 |
&self, |
| 117 |
current: $float_type, |
| 118 |
new: $float_type, |
| 119 |
success: Ordering, |
| 120 |
failure: Ordering, |
| 121 |
) -> Result<$float_type, $float_type> { |
| 122 |
match self.as_bits().compare_exchange( |
| 123 |
current.to_bits(), |
| 124 |
new.to_bits(), |
| 125 |
success, |
| 126 |
failure, |
| 127 |
) { |
| 128 |
Ok(v) => Ok($float_type::from_bits(v)), |
| 129 |
Err(v) => Err($float_type::from_bits(v)), |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[inline] |
| 134 |
#[cfg_attr( |
| 135 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 136 |
track_caller |
| 137 |
)] |
| 138 |
pub(crate) fn compare_exchange_weak( |
| 139 |
&self, |
| 140 |
current: $float_type, |
| 141 |
new: $float_type, |
| 142 |
success: Ordering, |
| 143 |
failure: Ordering, |
| 144 |
) -> Result<$float_type, $float_type> { |
| 145 |
match self.as_bits().compare_exchange_weak( |
| 146 |
current.to_bits(), |
| 147 |
new.to_bits(), |
| 148 |
success, |
| 149 |
failure, |
| 150 |
) { |
| 151 |
Ok(v) => Ok($float_type::from_bits(v)), |
| 152 |
Err(v) => Err($float_type::from_bits(v)), |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
#[cfg(not(all( |
| 157 |
any(target_arch = "aarch64", target_arch = "arm64ec"), |
| 158 |
any(target_feature = "lsfe", portable_atomic_target_feature = "lsfe"), |
| 159 |
target_feature = "neon", // for vreg |
| 160 |
not(any(miri, portable_atomic_sanitize_thread)), |
| 161 |
any(not(portable_atomic_no_asm), portable_atomic_unstable_asm), |
| 162 |
)))] |
| 163 |
impl $atomic_type { |
| 164 |
#[inline] |
| 165 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 166 |
pub(crate) fn fetch_add(&self, val: $float_type, order: Ordering) -> $float_type { |
| 167 |
self.fetch_update_(order, |x| x + val) |
| 168 |
} |
| 169 |
|
| 170 |
#[inline] |
| 171 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 172 |
pub(crate) fn fetch_sub(&self, val: $float_type, order: Ordering) -> $float_type { |
| 173 |
self.fetch_update_(order, |x| x - val) |
| 174 |
} |
| 175 |
|
| 176 |
#[inline] |
| 177 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 178 |
fn fetch_update_<F>(&self, order: Ordering, mut f: F) -> $float_type |
| 179 |
where |
| 180 |
F: FnMut($float_type) -> $float_type, |
| 181 |
{ |
| 182 |
// This is a private function and all instances of `f` only operate on the value |
| 183 |
// loaded, so there is no need to synchronize the first load/failed CAS. |
| 184 |
let mut prev = self.load(Ordering::Relaxed); |
| 185 |
loop { |
| 186 |
let next = f(prev); |
| 187 |
match self.compare_exchange_weak(prev, next, order, Ordering::Relaxed) { |
| 188 |
Ok(x) => return x, |
| 189 |
Err(next_prev) => prev = next_prev, |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
#[inline] |
| 195 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 196 |
pub(crate) fn fetch_max(&self, val: $float_type, order: Ordering) -> $float_type { |
| 197 |
self.fetch_update_(order, |x| x.max(val)) |
| 198 |
} |
| 199 |
|
| 200 |
#[inline] |
| 201 |
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
| 202 |
pub(crate) fn fetch_min(&self, val: $float_type, order: Ordering) -> $float_type { |
| 203 |
self.fetch_update_(order, |x| x.min(val)) |
| 204 |
} |
| 205 |
} |
| 206 |
} // cfg_has_atomic_cas! |
| 207 |
} // cfg_has_atomic_cas_or_amo32! |
| 208 |
}; |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(portable_atomic_unstable_f16)] |
| 212 |
cfg_has_atomic_16! { |
| 213 |
atomic_float!(AtomicF16, f16, AtomicU16, u16, 2); |
| 214 |
} |
| 215 |
cfg_has_atomic_32! { |
| 216 |
atomic_float!(AtomicF32, f32, AtomicU32, u32, 4); |
| 217 |
} |
| 218 |
cfg_has_atomic_64! { |
| 219 |
atomic_float!(AtomicF64, f64, AtomicU64, u64, 8); |
| 220 |
} |
| 221 |
#[cfg(portable_atomic_unstable_f128)] |
| 222 |
cfg_has_atomic_128! { |
| 223 |
atomic_float!(AtomicF128, f128, AtomicU128, u128, 16); |
| 224 |
} |