| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Atomic float implementation based on AArch64 with FEAT_LSFE. |
| 5 |
|
| 6 |
This module provides atomic float implementations using FEAT_LSFE instructions. |
| 7 |
|
| 8 |
Generated asm: |
| 9 |
- aarch64 (+lsfe) https://godbolt.org/z/7vaxeofv1 |
| 10 |
*/ |
| 11 |
|
| 12 |
#[cfg(not(portable_atomic_no_asm))] |
| 13 |
use core::arch::asm; |
| 14 |
use core::sync::atomic::Ordering; |
| 15 |
|
| 16 |
#[cfg(portable_atomic_unstable_f16)] |
| 17 |
use super::int::AtomicF16; |
| 18 |
#[cfg(portable_atomic_unstable_f128)] |
| 19 |
use super::int::AtomicF128; |
| 20 |
use super::int::{AtomicF32, AtomicF64}; |
| 21 |
|
| 22 |
// TODO: optimize no return cases: |
| 23 |
// https://developer.arm.com/documentation/ddi0602/2025-06/SIMD-FP-Instructions/STFADD--STFADDL--Floating-point-atomic-add-in-memory--without-return- |
| 24 |
// https://developer.arm.com/documentation/ddi0602/2025-06/SIMD-FP-Instructions/STFMAXNM--STFMAXNML--Floating-point-atomic-maximum-number-in-memory--without-return- |
| 25 |
// https://developer.arm.com/documentation/ddi0602/2025-06/SIMD-FP-Instructions/STFMINNM--STFMINNML--Floating-point-atomic-minimum-number-in-memory--without-return- |
| 26 |
|
| 27 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 28 |
macro_rules! start_lsfe { |
| 29 |
() => { |
| 30 |
".arch_extension lsfe" |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 35 |
macro_rules! atomic_rmw { |
| 36 |
($op:ident, $order:ident) => { |
| 37 |
atomic_rmw!($op, $order, write = $order) |
| 38 |
}; |
| 39 |
($op:ident, $order:ident, write = $write:ident) => { |
| 40 |
match $order { |
| 41 |
Ordering::Relaxed => $op!("", "", ""), |
| 42 |
Ordering::Acquire => $op!("a", "", ""), |
| 43 |
Ordering::Release => $op!("", "l", ""), |
| 44 |
Ordering::AcqRel => $op!("a", "l", ""), |
| 45 |
// In MSVC environments, SeqCst stores/writes by non-LSE* instructions needs fences after writes. |
| 46 |
// https://reviews.llvm.org/D141748 |
| 47 |
// https://github.com/llvm/llvm-project/commit/1ea201d73be2fdf03347e9c6be09ebed5f8e0e00 |
| 48 |
#[cfg(target_env = "msvc")] |
| 49 |
Ordering::SeqCst if $write == Ordering::SeqCst => $op!("a", "l", "dmb ish"), |
| 50 |
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments. |
| 51 |
Ordering::SeqCst => $op!("a", "l", ""), |
| 52 |
_ => unreachable!(), |
| 53 |
} |
| 54 |
}; |
| 55 |
} |
| 56 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 57 |
macro_rules! atomic_rmw_inst { |
| 58 |
($op:ident, $order:ident) => { |
| 59 |
atomic_rmw_inst!($op, $order, write = $order) |
| 60 |
}; |
| 61 |
($op:ident, $order:ident, write = $write:ident) => { |
| 62 |
match $order { |
| 63 |
Ordering::Relaxed => $op!("2", ""), // "" |
| 64 |
Ordering::Acquire => $op!("a", ""), // "a" |
| 65 |
Ordering::Release => $op!("6", ""), // "l" |
| 66 |
Ordering::AcqRel => $op!("e", ""), // "al" |
| 67 |
// In MSVC environments, SeqCst stores/writes by non-LSE* instructions needs fences after writes. |
| 68 |
// https://reviews.llvm.org/D141748 |
| 69 |
// https://github.com/llvm/llvm-project/commit/1ea201d73be2fdf03347e9c6be09ebed5f8e0e00 |
| 70 |
#[cfg(target_env = "msvc")] |
| 71 |
Ordering::SeqCst if $write == Ordering::SeqCst => $op!("e", "dmb ish"), |
| 72 |
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments. |
| 73 |
Ordering::SeqCst => $op!("e", ""), |
| 74 |
_ => unreachable!(), |
| 75 |
} |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
#[rustfmt::skip] |
| 80 |
macro_rules! atomic_float { |
| 81 |
($atomic_type:ident, $float_type:ident, $modifier:tt, $inst_modifier:tt) => { |
| 82 |
impl $atomic_type { |
| 83 |
#[inline] |
| 84 |
pub(crate) fn fetch_add(&self, val: $float_type, order: Ordering) -> $float_type { |
| 85 |
let dst = self.as_ptr(); |
| 86 |
let out; |
| 87 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 88 |
// pointer passed in is valid because we got it from a reference. |
| 89 |
// |
| 90 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/SIMD-FP-Instructions/LDFADD--LDFADDA--LDFADDAL--LDFADDL--Floating-point-atomic-add-in-memory- |
| 91 |
unsafe { |
| 92 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 93 |
macro_rules! add { |
| 94 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 95 |
asm!( |
| 96 |
start_lsfe!(), |
| 97 |
concat!("ldfadd", $acquire, $release, " {val:", $modifier, "}, {out:", $modifier, "}, [{dst}]"), // atomic { _x = *dst; *dst = _x + val; out = _x } |
| 98 |
dst = in(reg) ptr_reg!(dst), |
| 99 |
val = in(vreg) val, |
| 100 |
out = lateout(vreg) out, |
| 101 |
options(nostack), |
| 102 |
) |
| 103 |
}; |
| 104 |
} |
| 105 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 106 |
atomic_rmw!(add, order); |
| 107 |
// LLVM supports FEAT_LSFE instructions on LLVM 20+, so use .inst directive on old LLVM. |
| 108 |
// https://github.com/llvm/llvm-project/commit/67ff5ba9af9754261abe11d762af11532a816126 |
| 109 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 110 |
macro_rules! add { |
| 111 |
($order:tt, $_msvc_fence:tt) => { |
| 112 |
asm!( |
| 113 |
// ldfadd{,a,l,al} {h,s,d}0, {h,s,d}1, [x2] // atomic { _x = *x2; *x2 = _x + {h,s,d}0; {h,s,d}1 = _x } |
| 114 |
concat!(".inst 0x", $inst_modifier, "c", $order, "00041"), |
| 115 |
in("x2") ptr_reg!(dst), |
| 116 |
in("v0") val, |
| 117 |
out("v1") out, |
| 118 |
options(nostack), |
| 119 |
) |
| 120 |
}; |
| 121 |
} |
| 122 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 123 |
atomic_rmw_inst!(add, order); |
| 124 |
} |
| 125 |
out |
| 126 |
} |
| 127 |
#[inline] |
| 128 |
pub(crate) fn fetch_sub(&self, val: $float_type, order: Ordering) -> $float_type { |
| 129 |
// There is no atomic sub instruction, so add `-val`. |
| 130 |
self.fetch_add(-val, order) |
| 131 |
} |
| 132 |
#[inline] |
| 133 |
pub(crate) fn fetch_max(&self, val: $float_type, order: Ordering) -> $float_type { |
| 134 |
let dst = self.as_ptr(); |
| 135 |
let out; |
| 136 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 137 |
// pointer passed in is valid because we got it from a reference. |
| 138 |
// |
| 139 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/SIMD-FP-Instructions/LDFMAXNM--LDFMAXNMA--LDFMAXNMAL--LDFMAXNML--Floating-point-atomic-maximum-number-in-memory- |
| 140 |
unsafe { |
| 141 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 142 |
macro_rules! max { |
| 143 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 144 |
asm!( |
| 145 |
start_lsfe!(), |
| 146 |
concat!("ldfmaxnm", $acquire, $release, " {val:", $modifier, "}, {out:", $modifier, "}, [{dst}]"), // atomic { _x = *dst; *dst = _x.max(val); out = _x } |
| 147 |
dst = in(reg) ptr_reg!(dst), |
| 148 |
val = in(vreg) val, |
| 149 |
out = lateout(vreg) out, |
| 150 |
options(nostack), |
| 151 |
) |
| 152 |
}; |
| 153 |
} |
| 154 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 155 |
atomic_rmw!(max, order); |
| 156 |
// LLVM supports FEAT_LSFE instructions on LLVM 20+, so use .inst directive on old LLVM. |
| 157 |
// https://github.com/llvm/llvm-project/commit/67ff5ba9af9754261abe11d762af11532a816126 |
| 158 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 159 |
macro_rules! max { |
| 160 |
($order:tt, $_msvc_fence:tt) => { |
| 161 |
asm!( |
| 162 |
// ldfmaxnm{,a,l,al} {h,s,d}0, {h,s,d}1, [x2] // atomic { _x = *x2; *x2 = _x.max({h,s,d}0); {h,s,d}1 = _x } |
| 163 |
concat!(".inst 0x", $inst_modifier, "c", $order, "06041"), |
| 164 |
in("x2") ptr_reg!(dst), |
| 165 |
in("v0") val, |
| 166 |
out("v1") out, |
| 167 |
options(nostack), |
| 168 |
) |
| 169 |
}; |
| 170 |
} |
| 171 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 172 |
atomic_rmw_inst!(max, order); |
| 173 |
} |
| 174 |
out |
| 175 |
} |
| 176 |
#[inline] |
| 177 |
pub(crate) fn fetch_min(&self, val: $float_type, order: Ordering) -> $float_type { |
| 178 |
let dst = self.as_ptr(); |
| 179 |
let out; |
| 180 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 181 |
// pointer passed in is valid because we got it from a reference. |
| 182 |
// |
| 183 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/SIMD-FP-Instructions/LDFMINNM--LDFMINNMA--LDFMINNMAL--LDFMINNML--Floating-point-atomic-minimum-number-in-memory- |
| 184 |
unsafe { |
| 185 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 186 |
macro_rules! min { |
| 187 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 188 |
asm!( |
| 189 |
start_lsfe!(), |
| 190 |
concat!("ldfminnm", $acquire, $release, " {val:", $modifier, "}, {out:", $modifier, "}, [{dst}]"), // atomic { _x = *dst; *dst = _x.min(val); out = _x } |
| 191 |
dst = in(reg) ptr_reg!(dst), |
| 192 |
val = in(vreg) val, |
| 193 |
out = lateout(vreg) out, |
| 194 |
options(nostack), |
| 195 |
) |
| 196 |
}; |
| 197 |
} |
| 198 |
#[cfg(not(portable_atomic_pre_llvm_20))] |
| 199 |
atomic_rmw!(min, order); |
| 200 |
// LLVM supports FEAT_LSFE instructions on LLVM 20+, so use .inst directive on old LLVM. |
| 201 |
// https://github.com/llvm/llvm-project/commit/67ff5ba9af9754261abe11d762af11532a816126 |
| 202 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 203 |
macro_rules! min { |
| 204 |
($order:tt, $_msvc_fence:tt) => { |
| 205 |
asm!( |
| 206 |
// ldfminnm{,a,l,al} {h,s,d}0, {h,s,d}1, [x2] // atomic { _x = *x2; *x2 = _x.min({h,s,d}0); {h,s,d}1 = _x } |
| 207 |
concat!(".inst 0x", $inst_modifier, "c", $order, "07041"), |
| 208 |
in("x2") ptr_reg!(dst), |
| 209 |
in("v0") val, |
| 210 |
out("v1") out, |
| 211 |
options(nostack), |
| 212 |
) |
| 213 |
}; |
| 214 |
} |
| 215 |
#[cfg(portable_atomic_pre_llvm_20)] |
| 216 |
atomic_rmw_inst!(min, order); |
| 217 |
} |
| 218 |
out |
| 219 |
} |
| 220 |
} |
| 221 |
}; |
| 222 |
} |
| 223 |
|
| 224 |
#[cfg(portable_atomic_unstable_f16)] |
| 225 |
atomic_float!(AtomicF16, f16, "h", "7"); |
| 226 |
atomic_float!(AtomicF32, f32, "s", "b"); |
| 227 |
atomic_float!(AtomicF64, f64, "d", "f"); |
| 228 |
|
| 229 |
#[cfg(portable_atomic_unstable_f128)] |
| 230 |
impl AtomicF128 { |
| 231 |
#[inline] |
| 232 |
pub(crate) fn fetch_add(&self, val: f128, order: Ordering) -> f128 { |
| 233 |
self.fetch_update_(order, |x| x + val) |
| 234 |
} |
| 235 |
#[inline] |
| 236 |
pub(crate) fn fetch_sub(&self, val: f128, order: Ordering) -> f128 { |
| 237 |
self.fetch_update_(order, |x| x - val) |
| 238 |
} |
| 239 |
#[inline] |
| 240 |
pub(super) fn fetch_update_<F>(&self, order: Ordering, mut f: F) -> f128 |
| 241 |
where |
| 242 |
F: FnMut(f128) -> f128, |
| 243 |
{ |
| 244 |
// This is a private function and all instances of `f` only operate on the value |
| 245 |
// loaded, so there is no need to synchronize the first load/failed CAS. |
| 246 |
let mut prev = self.load(Ordering::Relaxed); |
| 247 |
loop { |
| 248 |
let next = f(prev); |
| 249 |
match self.compare_exchange_weak(prev, next, order, Ordering::Relaxed) { |
| 250 |
Ok(x) => return x, |
| 251 |
Err(next_prev) => prev = next_prev, |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
#[inline] |
| 256 |
pub(crate) fn fetch_max(&self, val: f128, order: Ordering) -> f128 { |
| 257 |
self.fetch_update_(order, |x| x.max(val)) |
| 258 |
} |
| 259 |
#[inline] |
| 260 |
pub(crate) fn fetch_min(&self, val: f128, order: Ordering) -> f128 { |
| 261 |
self.fetch_update_(order, |x| x.min(val)) |
| 262 |
} |
| 263 |
} |