| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
128-bit atomic implementation on AArch64. |
| 5 |
|
| 6 |
This architecture provides the following 128-bit atomic instructions: |
| 7 |
|
| 8 |
- LDXP/STXP: LL/SC (Armv8.0 baseline) |
| 9 |
- CASP: CAS (added as Armv8.1 FEAT_LSE (optional from Armv8.0, mandatory from Armv8.1)) |
| 10 |
- LDP/STP: load/store (if Armv8.4 FEAT_LSE2 (optional from Armv8.2, mandatory from Armv8.4) is available) |
| 11 |
- LDIAPP/STILP: acquire-load/release-store (added as Armv8.9 FEAT_LRCPC3 (optional from Armv8.2) (if FEAT_LSE2 is also available)) |
| 12 |
- LDCLRP/LDSETP/SWPP: fetch-and-{clear,or},swap (added as Armv9.4 FEAT_LSE128 (optional from Armv9.3)) |
| 13 |
|
| 14 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 15 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 16 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#aarch64 |
| 17 |
|
| 18 |
This module supports all of these instructions and attempts to select the best |
| 19 |
one based on compile-time and run-time information about available CPU features |
| 20 |
and platforms. For example: |
| 21 |
|
| 22 |
- If outline-atomics is not enabled and FEAT_LSE is not available at |
| 23 |
compile-time, we use LDXP/STXP loop. |
| 24 |
- If outline-atomics is enabled and FEAT_LSE is not available at |
| 25 |
compile-time, we use CASP for CAS if FEAT_LSE is available |
| 26 |
at run-time, otherwise, use LDXP/STXP loop. |
| 27 |
- If FEAT_LSE is available at compile-time, we use CASP for load/store/CAS/RMW. |
| 28 |
However, when portable_atomic_ll_sc_rmw cfg is set, use LDXP/STXP loop instead of CASP |
| 29 |
loop for RMW (by default, it is set on Apple hardware where CASP is slow; |
| 30 |
see build script for details). |
| 31 |
- If outline-atomics is enabled and FEAT_LSE2 is not available at compile-time, |
| 32 |
we use LDP/STP (and also LDIAPP/STILP/SWPP if FEAT_LRCPC3/FEAT_LSE128 is |
| 33 |
available) for load/store if FEAT_LSE2 is available at run-time, otherwise, |
| 34 |
use LDXP/STXP or CASP depending on whether FEAT_LSE is available. |
| 35 |
- If FEAT_LSE2 is available at compile-time, we use LDP/STP for load/store. |
| 36 |
- If FEAT_LSE128 is available at compile-time, we use LDCLRP/LDSETP/SWPP for fetch_and/fetch_or/swap/{release,seqcst}-store. |
| 37 |
- If FEAT_LSE2 and FEAT_LRCPC3 are available at compile-time, we use LDIAPP/STILP for acquire-load/release-store. |
| 38 |
|
| 39 |
See each "Instruction selection flow for ..." comment in this file for the exact |
| 40 |
instruction selection per operation. |
| 41 |
|
| 42 |
Note: FEAT_LSE2 doesn't imply FEAT_LSE. FEAT_LSE128 implies FEAT_LSE but not FEAT_LSE2. |
| 43 |
|
| 44 |
Note that we do not separate LL and SC into separate functions, but handle |
| 45 |
them within a single asm block. This is because it is theoretically possible |
| 46 |
for the compiler to insert operations that might clear the reservation between |
| 47 |
LL and SC. Considering the type of operations we are providing and the fact |
| 48 |
that [progress64](https://github.com/ARM-software/progress64) uses such code, |
| 49 |
this is probably not a problem for AArch64, but it seems that AArch64 doesn't |
| 50 |
guarantee it and hexagon is the only architecture with hardware guarantees |
| 51 |
that such code works. See also: |
| 52 |
|
| 53 |
- https://yarchive.net/comp/linux/cmpxchg_ll_sc_portability.html |
| 54 |
- https://lists.llvm.org/pipermail/llvm-dev/2016-May/099490.html |
| 55 |
- https://lists.llvm.org/pipermail/llvm-dev/2018-June/123993.html |
| 56 |
|
| 57 |
Also, even when using a CAS loop to implement atomic RMW, include the loop itself |
| 58 |
in the asm block because it is more efficient for some codegen backends. |
| 59 |
https://github.com/rust-lang/compiler-builtins/issues/339#issuecomment-1191260474 |
| 60 |
|
| 61 |
Note: On Miri and ThreadSanitizer which do not support inline assembly, we don't use |
| 62 |
this module and use intrinsics.rs instead. |
| 63 |
|
| 64 |
Refs: |
| 65 |
- Arm A-profile A64 Instruction Set Architecture |
| 66 |
https://developer.arm.com/documentation/ddi0602/2025-06 |
| 67 |
- C/C++ Atomics Application Binary Interface Standard for the Arm® 64-bit Architecture |
| 68 |
https://github.com/ARM-software/abi-aa/blob/2025Q4/atomicsabi64/atomicsabi64.rst |
| 69 |
- Arm Compiler armasm User Guide |
| 70 |
https://developer.arm.com/documentation/dui0801/latest |
| 71 |
- Arm Architecture Reference Manual for A-profile architecture |
| 72 |
https://developer.arm.com/documentation/ddi0487/latest (PDF) |
| 73 |
- Arm Architecture Reference Manual Supplement - Armv8, for Armv8-R AArch64 architecture profile |
| 74 |
https://developer.arm.com/documentation/ddi0600/latest (PDF) |
| 75 |
- atomic-maybe-uninit https://github.com/taiki-e/atomic-maybe-uninit |
| 76 |
|
| 77 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 78 |
*/ |
| 79 |
|
| 80 |
include!("macros.rs"); |
| 81 |
|
| 82 |
// On musl with static linking, it seems that getauxval is not always available. |
| 83 |
// See detect/auxv.rs for more. |
| 84 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 85 |
#[cfg(any( |
| 86 |
test, |
| 87 |
not(all( |
| 88 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 89 |
any(target_feature = "lse2", portable_atomic_target_feature = "lse2"), |
| 90 |
)), |
| 91 |
))] |
| 92 |
#[cfg(any( |
| 93 |
all( |
| 94 |
target_os = "linux", |
| 95 |
any( |
| 96 |
target_env = "gnu", |
| 97 |
all(target_env = "musl", any(not(target_feature = "crt-static"), feature = "std")), |
| 98 |
target_env = "ohos", |
| 99 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 100 |
portable_atomic_outline_atomics, |
| 101 |
), |
| 102 |
), |
| 103 |
target_os = "android", |
| 104 |
target_os = "freebsd", |
| 105 |
))] |
| 106 |
#[path = "../detect/auxv.rs"] |
| 107 |
mod detect; |
| 108 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 109 |
#[cfg(any( |
| 110 |
test, |
| 111 |
not(all( |
| 112 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 113 |
any(target_feature = "lse2", portable_atomic_target_feature = "lse2"), |
| 114 |
)), |
| 115 |
))] |
| 116 |
#[cfg(any(target_os = "netbsd", target_os = "openbsd"))] |
| 117 |
#[path = "../detect/aarch64_aa64reg.rs"] |
| 118 |
mod detect; |
| 119 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 120 |
#[cfg(any( |
| 121 |
test, |
| 122 |
not(all( |
| 123 |
any(target_feature = "lse2", portable_atomic_target_feature = "lse2"), |
| 124 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 125 |
)), |
| 126 |
))] |
| 127 |
#[cfg(target_os = "illumos")] |
| 128 |
#[path = "../detect/aarch64_illumos.rs"] |
| 129 |
mod detect; |
| 130 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 131 |
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))] |
| 132 |
#[cfg(target_os = "fuchsia")] |
| 133 |
#[path = "../detect/aarch64_fuchsia.rs"] |
| 134 |
mod detect; |
| 135 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 136 |
#[cfg(any( |
| 137 |
test, |
| 138 |
not(all( |
| 139 |
any(target_feature = "lse2", portable_atomic_target_feature = "lse2"), |
| 140 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 141 |
)), |
| 142 |
))] |
| 143 |
#[cfg(target_vendor = "apple")] |
| 144 |
#[path = "../detect/aarch64_apple.rs"] |
| 145 |
mod detect; |
| 146 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 147 |
#[cfg(any( |
| 148 |
test, |
| 149 |
not(all( |
| 150 |
any(target_feature = "lse2", portable_atomic_target_feature = "lse2"), |
| 151 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 152 |
)), |
| 153 |
))] |
| 154 |
#[cfg(windows)] |
| 155 |
#[path = "../detect/aarch64_windows.rs"] |
| 156 |
mod detect; |
| 157 |
|
| 158 |
#[cfg(test)] // test-only (we use auxv.rs instead) |
| 159 |
#[cfg(not(valgrind))] |
| 160 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 161 |
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] |
| 162 |
#[path = "../detect/aarch64_aa64reg.rs"] |
| 163 |
mod test_detect_aa64reg; |
| 164 |
#[cfg(test)] // test-only (we use aarch64_aa64reg.rs instead) |
| 165 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 166 |
#[cfg(target_os = "openbsd")] |
| 167 |
#[path = "../detect/auxv.rs"] |
| 168 |
mod test_detect_auxv; |
| 169 |
|
| 170 |
#[cfg(not(portable_atomic_no_asm))] |
| 171 |
use core::arch::asm; |
| 172 |
use core::sync::atomic::Ordering; |
| 173 |
|
| 174 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 175 |
use crate::utils::ptr::PtrExt as _; |
| 176 |
use crate::utils::{Pair, U128}; |
| 177 |
|
| 178 |
#[cfg(any( |
| 179 |
target_feature = "lse", |
| 180 |
portable_atomic_target_feature = "lse", |
| 181 |
not(portable_atomic_no_outline_atomics), |
| 182 |
))] |
| 183 |
#[rustfmt::skip] |
| 184 |
macro_rules! debug_assert_lse { |
| 185 |
() => { |
| 186 |
#[cfg(all( |
| 187 |
not(portable_atomic_no_outline_atomics), |
| 188 |
any( |
| 189 |
all( |
| 190 |
target_os = "linux", |
| 191 |
any( |
| 192 |
target_env = "gnu", |
| 193 |
all( |
| 194 |
target_env = "musl", |
| 195 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 196 |
), |
| 197 |
target_env = "ohos", |
| 198 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 199 |
portable_atomic_outline_atomics, |
| 200 |
), |
| 201 |
), |
| 202 |
target_os = "android", |
| 203 |
target_os = "freebsd", |
| 204 |
target_os = "netbsd", |
| 205 |
target_os = "openbsd", |
| 206 |
target_os = "illumos", |
| 207 |
target_os = "fuchsia", |
| 208 |
target_vendor = "apple", |
| 209 |
windows, |
| 210 |
), |
| 211 |
))] |
| 212 |
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))] |
| 213 |
{ |
| 214 |
debug_assert!(detect::detect().lse()); |
| 215 |
} |
| 216 |
}; |
| 217 |
} |
| 218 |
#[cfg(any( |
| 219 |
target_feature = "lse2", |
| 220 |
portable_atomic_target_feature = "lse2", |
| 221 |
not(portable_atomic_no_outline_atomics), |
| 222 |
))] |
| 223 |
#[rustfmt::skip] |
| 224 |
macro_rules! debug_assert_lse2 { |
| 225 |
() => { |
| 226 |
#[cfg(all( |
| 227 |
not(portable_atomic_no_outline_atomics), |
| 228 |
any( |
| 229 |
all( |
| 230 |
target_os = "linux", |
| 231 |
any( |
| 232 |
target_env = "gnu", |
| 233 |
all( |
| 234 |
target_env = "musl", |
| 235 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 236 |
), |
| 237 |
target_env = "ohos", |
| 238 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 239 |
portable_atomic_outline_atomics, |
| 240 |
), |
| 241 |
), |
| 242 |
target_os = "android", |
| 243 |
target_os = "freebsd", |
| 244 |
target_os = "netbsd", |
| 245 |
target_os = "openbsd", |
| 246 |
target_os = "illumos", |
| 247 |
// These don't support detection of FEAT_LSE2. |
| 248 |
// target_os = "fuchsia", |
| 249 |
target_vendor = "apple", |
| 250 |
windows, |
| 251 |
), |
| 252 |
))] |
| 253 |
#[cfg(not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")))] |
| 254 |
{ |
| 255 |
debug_assert!(detect::detect().lse2()); |
| 256 |
} |
| 257 |
}; |
| 258 |
} |
| 259 |
#[cfg(any( |
| 260 |
target_feature = "lse128", |
| 261 |
portable_atomic_target_feature = "lse128", |
| 262 |
all( |
| 263 |
not(portable_atomic_no_outline_atomics), |
| 264 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 265 |
), |
| 266 |
))] |
| 267 |
#[rustfmt::skip] |
| 268 |
macro_rules! debug_assert_lse128 { |
| 269 |
() => { |
| 270 |
#[cfg(all( |
| 271 |
not(portable_atomic_no_outline_atomics), |
| 272 |
any( |
| 273 |
all( |
| 274 |
target_os = "linux", |
| 275 |
any( |
| 276 |
target_env = "gnu", |
| 277 |
all( |
| 278 |
target_env = "musl", |
| 279 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 280 |
), |
| 281 |
target_env = "ohos", |
| 282 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 283 |
portable_atomic_outline_atomics, |
| 284 |
), |
| 285 |
), |
| 286 |
target_os = "android", |
| 287 |
target_os = "freebsd", |
| 288 |
target_os = "netbsd", |
| 289 |
target_os = "openbsd", |
| 290 |
target_os = "illumos", |
| 291 |
// These don't support detection of FEAT_LSE128. |
| 292 |
// target_os = "fuchsia", |
| 293 |
target_vendor = "apple", |
| 294 |
windows, |
| 295 |
), |
| 296 |
))] |
| 297 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 298 |
{ |
| 299 |
debug_assert!(detect::detect().lse128()); |
| 300 |
} |
| 301 |
}; |
| 302 |
} |
| 303 |
#[cfg(any( |
| 304 |
target_feature = "rcpc3", |
| 305 |
portable_atomic_target_feature = "rcpc3", |
| 306 |
all( |
| 307 |
not(portable_atomic_no_outline_atomics), |
| 308 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 309 |
), |
| 310 |
))] |
| 311 |
#[rustfmt::skip] |
| 312 |
macro_rules! debug_assert_rcpc3 { |
| 313 |
() => { |
| 314 |
#[cfg(all( |
| 315 |
not(portable_atomic_no_outline_atomics), |
| 316 |
any( |
| 317 |
all( |
| 318 |
target_os = "linux", |
| 319 |
any( |
| 320 |
target_env = "gnu", |
| 321 |
all( |
| 322 |
target_env = "musl", |
| 323 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 324 |
), |
| 325 |
target_env = "ohos", |
| 326 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 327 |
portable_atomic_outline_atomics, |
| 328 |
), |
| 329 |
), |
| 330 |
target_os = "android", |
| 331 |
target_os = "freebsd", |
| 332 |
target_os = "netbsd", |
| 333 |
target_os = "openbsd", |
| 334 |
target_os = "illumos", |
| 335 |
// These don't support detection of FEAT_LRCPC3. |
| 336 |
// target_os = "fuchsia", |
| 337 |
target_vendor = "apple", |
| 338 |
windows, |
| 339 |
), |
| 340 |
))] |
| 341 |
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))] |
| 342 |
{ |
| 343 |
debug_assert!(detect::detect().rcpc3()); |
| 344 |
} |
| 345 |
}; |
| 346 |
} |
| 347 |
|
| 348 |
// Refs: https://developer.arm.com/documentation/100067/0611/armclang-Integrated-Assembler/AArch32-Target-selection-directives |
| 349 |
// |
| 350 |
// This is similar to #[target_feature(enable = "lse")], except that there are |
| 351 |
// no compiler guarantees regarding (un)inlining, and the scope is within an asm |
| 352 |
// block rather than a function. We use this directive because #[target_feature(enable = "lse")] |
| 353 |
// is unstable on pre-1.61 rustc and incompatible with rustc_codegen_cranelift: |
| 354 |
// https://github.com/rust-lang/rustc_codegen_cranelift/issues/1400#issuecomment-1774599775 |
| 355 |
// |
| 356 |
// The .arch_extension directive in asm! is effective until the end of the assembly block and |
| 357 |
// is not propagated to subsequent code, so the end_lse macro is unneeded. |
| 358 |
// https://godbolt.org/z/o6EPndP94 |
| 359 |
// https://github.com/torvalds/linux/commit/e0d5896bd356cd577f9710a02d7a474cdf58426b |
| 360 |
// https://github.com/torvalds/linux/commit/dd1f6308b28edf0452dd5dc7877992903ec61e69 |
| 361 |
// (It seems GCC effectively ignores this directive and always allow FEAT_LSE instructions: https://godbolt.org/z/W9W6rensG) |
| 362 |
// Note that the .arch_extension directive in global_asm!/naked_asm! which are |
| 363 |
// not used in this crate has different behavior: https://github.com/rust-lang/rust/pull/137720#discussion_r1973608259 |
| 364 |
// Note that this directive currently cannot be used correctly with global_asm!/naked_asm! |
| 365 |
// due to LLVM bug: https://github.com/rust-lang/rust/pull/137720#discussion_r2014505753 |
| 366 |
// |
| 367 |
// The .arch directive has a similar effect, but we don't use it due to the following issue: |
| 368 |
// https://github.com/torvalds/linux/commit/dd1f6308b28edf0452dd5dc7877992903ec61e69 |
| 369 |
// |
| 370 |
// Note: If FEAT_LSE is not available at compile-time, we must guarantee that |
| 371 |
// the function that uses it is not inlined into a function where it is not |
| 372 |
// clear whether FEAT_LSE is available. Otherwise, (even if we checked whether |
| 373 |
// FEAT_LSE is available at run-time) optimizations that reorder its |
| 374 |
// instructions across the if condition might introduce undefined behavior. |
| 375 |
// (see also https://rust-lang.github.io/rfcs/2045-target-feature.html#safely-inlining-target_feature-functions-on-more-contexts) |
| 376 |
// However, our code uses the ifunc helper macro that works with function pointers, |
| 377 |
// so we don't have to worry about this unless calling without helper macro. |
| 378 |
#[cfg(any( |
| 379 |
target_feature = "lse", |
| 380 |
portable_atomic_target_feature = "lse", |
| 381 |
not(portable_atomic_no_outline_atomics), |
| 382 |
))] |
| 383 |
macro_rules! start_lse { |
| 384 |
() => { |
| 385 |
".arch_extension lse" |
| 386 |
}; |
| 387 |
} |
| 388 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 389 |
#[cfg(any( |
| 390 |
target_feature = "lse128", |
| 391 |
portable_atomic_target_feature = "lse128", |
| 392 |
all( |
| 393 |
not(portable_atomic_no_outline_atomics), |
| 394 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 395 |
), |
| 396 |
))] |
| 397 |
macro_rules! start_lse128 { |
| 398 |
() => { |
| 399 |
".arch_extension lse128" |
| 400 |
}; |
| 401 |
} |
| 402 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 403 |
#[cfg(any( |
| 404 |
target_feature = "rcpc3", |
| 405 |
portable_atomic_target_feature = "rcpc3", |
| 406 |
all( |
| 407 |
not(portable_atomic_no_outline_atomics), |
| 408 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 409 |
), |
| 410 |
))] |
| 411 |
macro_rules! start_rcpc3 { |
| 412 |
() => { |
| 413 |
".arch_extension rcpc3" |
| 414 |
}; |
| 415 |
} |
| 416 |
|
| 417 |
#[cfg(target_endian = "little")] |
| 418 |
macro_rules! select_le_or_be { |
| 419 |
($le:expr, $be:expr) => { |
| 420 |
$le |
| 421 |
}; |
| 422 |
} |
| 423 |
#[cfg(target_endian = "big")] |
| 424 |
macro_rules! select_le_or_be { |
| 425 |
($le:expr, $be:expr) => { |
| 426 |
$be |
| 427 |
}; |
| 428 |
} |
| 429 |
|
| 430 |
macro_rules! atomic_rmw { |
| 431 |
($op:ident, $order:ident) => { |
| 432 |
atomic_rmw!($op, $order, write = $order) |
| 433 |
}; |
| 434 |
($op:ident, $order:ident, write = $write:ident) => { |
| 435 |
match $order { |
| 436 |
Ordering::Relaxed => $op!("", "", ""), |
| 437 |
Ordering::Acquire => $op!("a", "", ""), |
| 438 |
Ordering::Release => $op!("", "l", ""), |
| 439 |
Ordering::AcqRel => $op!("a", "l", ""), |
| 440 |
// In MSVC environments, SeqCst stores/writes by non-LSE* instructions needs fences after writes. |
| 441 |
// https://reviews.llvm.org/D141748 |
| 442 |
// https://github.com/llvm/llvm-project/commit/1ea201d73be2fdf03347e9c6be09ebed5f8e0e00 |
| 443 |
#[cfg(target_env = "msvc")] |
| 444 |
Ordering::SeqCst if $write == Ordering::SeqCst => $op!("a", "l", "dmb ish"), |
| 445 |
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments. |
| 446 |
Ordering::SeqCst => $op!("a", "l", ""), |
| 447 |
_ => unreachable!(), |
| 448 |
} |
| 449 |
}; |
| 450 |
} |
| 451 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 452 |
#[cfg(any( |
| 453 |
target_feature = "lse128", |
| 454 |
portable_atomic_target_feature = "lse128", |
| 455 |
all( |
| 456 |
not(portable_atomic_no_outline_atomics), |
| 457 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 458 |
), |
| 459 |
))] |
| 460 |
macro_rules! atomic_rmw_inst { |
| 461 |
($op:ident, $order:ident) => { |
| 462 |
atomic_rmw_inst!($op, $order, write = $order) |
| 463 |
}; |
| 464 |
($op:ident, $order:ident, write = $write:ident) => { |
| 465 |
match $order { |
| 466 |
Ordering::Relaxed => $op!("2", ""), // "" |
| 467 |
Ordering::Acquire => $op!("a", ""), // "a" |
| 468 |
Ordering::Release => $op!("6", ""), // "l" |
| 469 |
Ordering::AcqRel => $op!("e", ""), // "al" |
| 470 |
// In MSVC environments, SeqCst stores/writes by non-LSE* instructions needs fences after writes. |
| 471 |
// https://reviews.llvm.org/D141748 |
| 472 |
// https://github.com/llvm/llvm-project/commit/1ea201d73be2fdf03347e9c6be09ebed5f8e0e00 |
| 473 |
#[cfg(target_env = "msvc")] |
| 474 |
Ordering::SeqCst if $write == Ordering::SeqCst => $op!("e", "dmb ish"), |
| 475 |
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments. |
| 476 |
Ordering::SeqCst => $op!("e", ""), |
| 477 |
_ => unreachable!(), |
| 478 |
} |
| 479 |
}; |
| 480 |
} |
| 481 |
|
| 482 |
// ----------------------------------------------------------------------------- |
| 483 |
// load |
| 484 |
|
| 485 |
/* |
| 486 |
|
| 487 |
Instruction selection flow for load: |
| 488 |
- if compile_time(FEAT_LSE2) => ldp: |
| 489 |
- if compile_time(FEAT_LRCPC3) && order != relaxed => ldiapp |
| 490 |
- else => ldp |
| 491 |
- if platform_supports_detection_of(FEAT_LSE2): |
| 492 |
- if detect(FEAT_LSE2) && detect(FEAT_LRCPC3) && order != relaxed => lse2_rcpc3 (ldiapp) |
| 493 |
- if detect(FEAT_LSE2) => lse2 (ldp) |
| 494 |
- else => no_lse2: |
| 495 |
- if compile_time(FEAT_LSE) => casp |
| 496 |
- else => ldxp_stxp |
| 497 |
|
| 498 |
Note: |
| 499 |
- If FEAT_LSE2 is available at compile-time, we don't do run-time detection of |
| 500 |
FEAT_LRCPC3 at this time, since FEAT_LRCPC3 is not yet available for most CPUs. |
| 501 |
(macOS that doesn't have any FEAT_LRCPC3-enabled CPUs as of M4 is only a platform |
| 502 |
that currently enables FEAT_LSE2 at compile-time by default.) |
| 503 |
- If FEAT_LSE2 is not available at compile-time, we want to do run-time detection |
| 504 |
of FEAT_LSE2, so we do run-time detection of FEAT_LRCPC3 at the same time. |
| 505 |
- We don't do run-time detection of FEAT_LSE for load at this time, but since |
| 506 |
load by CAS is wait-free, it would probably make sense to do run-time detection. (TODO) |
| 507 |
|
| 508 |
*/ |
| 509 |
|
| 510 |
cfg_sel!({ |
| 511 |
// if compile_time(FEAT_LSE2) => ldp: |
| 512 |
// cfg guarantee that the CPU supports FEAT_LSE2. |
| 513 |
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))] |
| 514 |
{ |
| 515 |
use self::_atomic_load_ldp as atomic_load; |
| 516 |
} |
| 517 |
// if platform_supports_detection_of(FEAT_LSE2): |
| 518 |
#[cfg(all( |
| 519 |
not(portable_atomic_no_outline_atomics), |
| 520 |
any( |
| 521 |
all( |
| 522 |
target_os = "linux", |
| 523 |
any( |
| 524 |
target_env = "gnu", |
| 525 |
all( |
| 526 |
target_env = "musl", |
| 527 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 528 |
), |
| 529 |
target_env = "ohos", |
| 530 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 531 |
portable_atomic_outline_atomics, |
| 532 |
), |
| 533 |
), |
| 534 |
target_os = "android", |
| 535 |
target_os = "freebsd", |
| 536 |
target_os = "netbsd", |
| 537 |
target_os = "openbsd", |
| 538 |
target_os = "illumos", |
| 539 |
// These don't support detection of FEAT_LSE2. |
| 540 |
// target_os = "fuchsia", |
| 541 |
target_vendor = "apple", |
| 542 |
windows, |
| 543 |
), |
| 544 |
))] |
| 545 |
{ |
| 546 |
#[inline] |
| 547 |
unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 { |
| 548 |
fn_alias! { |
| 549 |
// inline(never) is just a hint and also not strictly necessary |
| 550 |
// because we use ifunc helper macro, but used for clarity. |
| 551 |
#[inline(never)] |
| 552 |
unsafe fn(src: *mut u128) -> u128; |
| 553 |
atomic_load_lse2_relaxed = _atomic_load_ldp(Ordering::Relaxed); |
| 554 |
atomic_load_lse2_acquire = _atomic_load_ldp(Ordering::Acquire); |
| 555 |
atomic_load_lse2_seqcst = _atomic_load_ldp(Ordering::SeqCst); |
| 556 |
atomic_load_lse2_rcpc3_acquire = _atomic_load_ldiapp(Ordering::Acquire); |
| 557 |
atomic_load_lse2_rcpc3_seqcst = _atomic_load_ldiapp(Ordering::SeqCst); |
| 558 |
} |
| 559 |
fn_alias! { |
| 560 |
unsafe fn(src: *mut u128) -> u128; |
| 561 |
atomic_load_no_lse2_relaxed = atomic_load_no_lse2(Ordering::Relaxed); |
| 562 |
atomic_load_no_lse2_acquire = atomic_load_no_lse2(Ordering::Acquire); |
| 563 |
atomic_load_no_lse2_seqcst = atomic_load_no_lse2(Ordering::SeqCst); |
| 564 |
} |
| 565 |
// SAFETY: the caller must uphold the safety contract. |
| 566 |
// and we've checked if FEAT_LSE2/FEAT_LRCPC3 is available. |
| 567 |
unsafe { |
| 568 |
match order { |
| 569 |
Ordering::Relaxed => { |
| 570 |
ifunc!(unsafe fn(src: *mut u128) -> u128 { |
| 571 |
let cpuinfo = detect::detect(); |
| 572 |
if cpuinfo.lse2() { |
| 573 |
// if detect(FEAT_LSE2) => lse2 (ldp) |
| 574 |
atomic_load_lse2_relaxed |
| 575 |
} else { |
| 576 |
// else => no_lse2: |
| 577 |
atomic_load_no_lse2_relaxed |
| 578 |
} |
| 579 |
}) |
| 580 |
} |
| 581 |
Ordering::Acquire => { |
| 582 |
ifunc!(unsafe fn(src: *mut u128) -> u128 { |
| 583 |
let cpuinfo = detect::detect(); |
| 584 |
if cpuinfo.lse2() { |
| 585 |
if cpuinfo.rcpc3() { |
| 586 |
// if detect(FEAT_LSE2) && detect(FEAT_LRCPC3) && order != relaxed => lse2_rcpc3 (ldiapp) |
| 587 |
atomic_load_lse2_rcpc3_acquire |
| 588 |
} else { |
| 589 |
// if detect(FEAT_LSE2) => lse2 (ldp) |
| 590 |
atomic_load_lse2_acquire |
| 591 |
} |
| 592 |
} else { |
| 593 |
// else => no_lse2: |
| 594 |
atomic_load_no_lse2_acquire |
| 595 |
} |
| 596 |
}) |
| 597 |
} |
| 598 |
Ordering::SeqCst => { |
| 599 |
ifunc!(unsafe fn(src: *mut u128) -> u128 { |
| 600 |
let cpuinfo = detect::detect(); |
| 601 |
if cpuinfo.lse2() { |
| 602 |
if cpuinfo.rcpc3() { |
| 603 |
// if detect(FEAT_LSE2) && detect(FEAT_LRCPC3) && order != relaxed => lse2_rcpc3 (ldiapp) |
| 604 |
atomic_load_lse2_rcpc3_seqcst |
| 605 |
} else { |
| 606 |
// if detect(FEAT_LSE2) => lse2 (ldp) |
| 607 |
atomic_load_lse2_seqcst |
| 608 |
} |
| 609 |
} else { |
| 610 |
// else => no_lse2: |
| 611 |
atomic_load_no_lse2_seqcst |
| 612 |
} |
| 613 |
}) |
| 614 |
} |
| 615 |
_ => unreachable!(), |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
// else => no_lse2: |
| 621 |
#[cfg(else)] |
| 622 |
{ |
| 623 |
use self::atomic_load_no_lse2 as atomic_load; |
| 624 |
} |
| 625 |
}); |
| 626 |
#[cfg(not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")))] |
| 627 |
cfg_sel!({ |
| 628 |
// if compile_time(FEAT_LSE) => casp |
| 629 |
// cfg guarantee that the CPU supports FEAT_LSE. |
| 630 |
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))] |
| 631 |
{ |
| 632 |
use self::_atomic_load_casp as atomic_load_no_lse2; |
| 633 |
} |
| 634 |
// else => ldxp_stxp |
| 635 |
#[cfg(else)] |
| 636 |
{ |
| 637 |
use self::_atomic_load_ldxp_stxp as atomic_load_no_lse2; |
| 638 |
} |
| 639 |
}); |
| 640 |
// If CPU supports FEAT_LSE2, LDP/LDIAPP is single-copy atomic reads, |
| 641 |
// otherwise it is two single-copy atomic reads. |
| 642 |
// Refs: B2.2.1 of the Arm Architecture Reference Manual Armv8, for Armv8-A architecture profile |
| 643 |
#[cfg(any( |
| 644 |
target_feature = "lse2", |
| 645 |
portable_atomic_target_feature = "lse2", |
| 646 |
not(portable_atomic_no_outline_atomics), |
| 647 |
))] |
| 648 |
#[inline] |
| 649 |
unsafe fn _atomic_load_ldp(src: *mut u128, order: Ordering) -> u128 { |
| 650 |
debug_assert!(src.addr() % 16 == 0); |
| 651 |
debug_assert_lse2!(); |
| 652 |
|
| 653 |
// SAFETY: the caller must guarantee that `dst` is valid for reads, |
| 654 |
// 16-byte aligned, that there are no concurrent non-atomic operations. |
| 655 |
// |
| 656 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/LDP--Load-pair-of-registers- |
| 657 |
unsafe { |
| 658 |
let (out_lo, out_hi); |
| 659 |
macro_rules! atomic_load_relaxed { |
| 660 |
($acquire:tt) => {{ |
| 661 |
asm!( |
| 662 |
"ldp {out_lo}, {out_hi}, [{src}]", // atomic { out_lo:out_hi = *src } |
| 663 |
$acquire, // fence |
| 664 |
src = in(reg) ptr_reg!(src), |
| 665 |
out_hi = lateout(reg) out_hi, |
| 666 |
out_lo = lateout(reg) out_lo, |
| 667 |
options(nostack, preserves_flags), |
| 668 |
); |
| 669 |
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole |
| 670 |
}}; |
| 671 |
} |
| 672 |
match order { |
| 673 |
// if compile_time(FEAT_LRCPC3) && order != relaxed => ldiapp |
| 674 |
// SAFETY: cfg guarantee that the CPU supports FEAT_LRCPC3. |
| 675 |
#[cfg(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3"))] |
| 676 |
Ordering::Acquire | Ordering::SeqCst => _atomic_load_ldiapp(src, order), |
| 677 |
|
| 678 |
// else => ldp |
| 679 |
Ordering::Relaxed => atomic_load_relaxed!(""), |
| 680 |
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))] |
| 681 |
Ordering::Acquire => atomic_load_relaxed!("dmb ishld"), |
| 682 |
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))] |
| 683 |
Ordering::SeqCst => { |
| 684 |
asm!( |
| 685 |
// ldar (or dmb ishld) is required to prevent reordering with preceding stlxp. |
| 686 |
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108891 for details. |
| 687 |
"ldar {tmp}, [{src}]", // atomic { tmp = *src } |
| 688 |
"ldp {out_lo}, {out_hi}, [{src}]", // atomic { out_lo:out_hi = *src } |
| 689 |
"dmb ishld", // fence |
| 690 |
src = in(reg) ptr_reg!(src), |
| 691 |
out_hi = lateout(reg) out_hi, |
| 692 |
out_lo = lateout(reg) out_lo, |
| 693 |
tmp = out(reg) _, |
| 694 |
options(nostack, preserves_flags), |
| 695 |
); |
| 696 |
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole |
| 697 |
} |
| 698 |
_ => unreachable!(), |
| 699 |
} |
| 700 |
} |
| 701 |
} |
| 702 |
#[cfg(any( |
| 703 |
target_feature = "lse2", |
| 704 |
portable_atomic_target_feature = "lse2", |
| 705 |
not(portable_atomic_no_outline_atomics), |
| 706 |
))] |
| 707 |
#[cfg(any( |
| 708 |
target_feature = "rcpc3", |
| 709 |
portable_atomic_target_feature = "rcpc3", |
| 710 |
all( |
| 711 |
not(portable_atomic_no_outline_atomics), |
| 712 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 713 |
), |
| 714 |
))] |
| 715 |
#[inline] |
| 716 |
unsafe fn _atomic_load_ldiapp(src: *mut u128, order: Ordering) -> u128 { |
| 717 |
debug_assert!(src.addr() % 16 == 0); |
| 718 |
debug_assert_lse2!(); |
| 719 |
debug_assert_rcpc3!(); |
| 720 |
|
| 721 |
// SAFETY: the caller must guarantee that `dst` is valid for reads, |
| 722 |
// 16-byte aligned, that there are no concurrent non-atomic operations. |
| 723 |
// |
| 724 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/LDIAPP--Load-Acquire-RCpc-ordered-pair-of-registers- |
| 725 |
unsafe { |
| 726 |
let (out_lo, out_hi); |
| 727 |
match order { |
| 728 |
Ordering::Acquire => { |
| 729 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 730 |
asm!( |
| 731 |
start_rcpc3!(), |
| 732 |
"ldiapp {out_lo}, {out_hi}, [{src}]", // atomic { out_lo:out_hi = *src } |
| 733 |
src = in(reg) ptr_reg!(src), |
| 734 |
out_hi = lateout(reg) out_hi, |
| 735 |
out_lo = lateout(reg) out_lo, |
| 736 |
options(nostack, preserves_flags), |
| 737 |
); |
| 738 |
// LLVM supports FEAT_LRCPC3 instructions on LLVM 16+, so use .inst directive on old LLVM. |
| 739 |
// https://github.com/llvm/llvm-project/commit/a6aaa969f7caec58a994142f8d855861cf3a1463 |
| 740 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 741 |
asm!( |
| 742 |
// ldiapp x0, x1, [x0] // atomic { out_lo:out_hi = *src } |
| 743 |
".inst 0xd9411800", |
| 744 |
in("x0") ptr_reg!(src), |
| 745 |
lateout("x1") out_hi, |
| 746 |
lateout("x0") out_lo, |
| 747 |
options(nostack, preserves_flags), |
| 748 |
); |
| 749 |
} |
| 750 |
Ordering::SeqCst => { |
| 751 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 752 |
asm!( |
| 753 |
start_rcpc3!(), |
| 754 |
// ldar (or dmb ishld) is required to prevent reordering with preceding stlxp. |
| 755 |
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108891 for details. |
| 756 |
"ldar {tmp}, [{src}]", // atomic { tmp = *src } |
| 757 |
"ldiapp {out_lo}, {out_hi}, [{src}]", // atomic { out_lo:out_hi = *src } |
| 758 |
src = in(reg) ptr_reg!(src), |
| 759 |
out_hi = lateout(reg) out_hi, |
| 760 |
out_lo = lateout(reg) out_lo, |
| 761 |
tmp = out(reg) _, |
| 762 |
options(nostack, preserves_flags), |
| 763 |
); |
| 764 |
// LLVM supports FEAT_LRCPC3 instructions on LLVM 16+, so use .inst directive on old LLVM. |
| 765 |
// https://github.com/llvm/llvm-project/commit/a6aaa969f7caec58a994142f8d855861cf3a1463 |
| 766 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 767 |
asm!( |
| 768 |
// ldar (or dmb ishld) is required to prevent reordering with preceding stlxp. |
| 769 |
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108891 for details. |
| 770 |
"ldar {tmp}, [x0]", // atomic { out_lo:out_hi = *src } |
| 771 |
// ldiapp x0, x1, [x0] // atomic { tmp = *src } |
| 772 |
".inst 0xd9411800", |
| 773 |
tmp = out(reg) _, |
| 774 |
in("x0") ptr_reg!(src), |
| 775 |
lateout("x1") out_hi, |
| 776 |
lateout("x0") out_lo, |
| 777 |
options(nostack, preserves_flags), |
| 778 |
); |
| 779 |
} |
| 780 |
_ => unreachable!(), |
| 781 |
} |
| 782 |
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole |
| 783 |
} |
| 784 |
} |
| 785 |
// Do not use _atomic_compare_exchange_casp because it needs extra MOV to implement load. |
| 786 |
#[cfg(any(test, not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))))] |
| 787 |
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))] |
| 788 |
#[inline] |
| 789 |
unsafe fn _atomic_load_casp(src: *mut u128, order: Ordering) -> u128 { |
| 790 |
debug_assert!(src.addr() % 16 == 0); |
| 791 |
debug_assert_lse!(); |
| 792 |
|
| 793 |
// SAFETY: the caller must uphold the safety contract. |
| 794 |
// cfg guarantee that the CPU supports FEAT_LSE. |
| 795 |
unsafe { |
| 796 |
let (out_lo, out_hi); |
| 797 |
macro_rules! atomic_load { |
| 798 |
($acquire:tt, $release:tt) => { |
| 799 |
asm!( |
| 800 |
start_lse!(), |
| 801 |
concat!("casp", $acquire, $release, " x2, x3, x2, x3, [{src}]"), // atomic { if *src == x2:x3 { *dst = x2:x3 } else { x2:x3 = *dst } } |
| 802 |
src = in(reg) ptr_reg!(src), |
| 803 |
// must be allocated to even/odd register pair |
| 804 |
inout("x2") 0_u64 => out_lo, |
| 805 |
inout("x3") 0_u64 => out_hi, |
| 806 |
options(nostack, preserves_flags), |
| 807 |
) |
| 808 |
}; |
| 809 |
} |
| 810 |
match order { |
| 811 |
Ordering::Relaxed => atomic_load!("", ""), |
| 812 |
Ordering::Acquire => atomic_load!("a", ""), |
| 813 |
// TODO: in atomicsabi64, seqcst load is the same as acquire load |
| 814 |
Ordering::SeqCst => atomic_load!("a", "l"), |
| 815 |
_ => unreachable!(), |
| 816 |
} |
| 817 |
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole |
| 818 |
} |
| 819 |
} |
| 820 |
#[cfg(any( |
| 821 |
test, |
| 822 |
all( |
| 823 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 824 |
not(any(target_feature = "lse", portable_atomic_target_feature = "lse")), |
| 825 |
), |
| 826 |
))] |
| 827 |
#[inline] |
| 828 |
unsafe fn _atomic_load_ldxp_stxp(src: *mut u128, order: Ordering) -> u128 { |
| 829 |
debug_assert!(src.addr() % 16 == 0); |
| 830 |
|
| 831 |
// SAFETY: the caller must uphold the safety contract. |
| 832 |
unsafe { |
| 833 |
let (mut out_lo, mut out_hi); |
| 834 |
macro_rules! atomic_load { |
| 835 |
($acquire:tt, $release:tt) => { |
| 836 |
asm!( |
| 837 |
"2:", // 'retry: |
| 838 |
concat!("ld", $acquire, "xp {out_lo}, {out_hi}, [{src}]"), // atomic { out_lo:out_hi = *src; EXCLUSIVE = src } |
| 839 |
// write back to ensure atomicity |
| 840 |
concat!("st", $release, "xp {r:w}, {out_lo}, {out_hi}, [{src}]"), // atomic { if EXCLUSIVE == src { *src = out_lo:out_hi; r = 0 } else { r = 1 }; EXCLUSIVE = None } |
| 841 |
"cbnz {r:w}, 2b", // if r != 0 { jump 'retry } |
| 842 |
src = in(reg) ptr_reg!(src), |
| 843 |
out_lo = out(reg) out_lo, |
| 844 |
out_hi = out(reg) out_hi, |
| 845 |
r = out(reg) _, |
| 846 |
options(nostack, preserves_flags), |
| 847 |
) |
| 848 |
}; |
| 849 |
} |
| 850 |
match order { |
| 851 |
Ordering::Relaxed => atomic_load!("", ""), |
| 852 |
Ordering::Acquire => atomic_load!("a", ""), |
| 853 |
// TODO: in atomicsabi64, seqcst load is the same as acquire load |
| 854 |
Ordering::SeqCst => atomic_load!("a", "l"), |
| 855 |
_ => unreachable!(), |
| 856 |
} |
| 857 |
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
// ----------------------------------------------------------------------------- |
| 862 |
// store |
| 863 |
|
| 864 |
/* |
| 865 |
|
| 866 |
Instruction selection flow for store: |
| 867 |
- if compile_time(FEAT_LSE2) => stp: |
| 868 |
- if compile_time(FEAT_LSE128) && order == seqcst => swpp |
| 869 |
- if compile_time(FEAT_LRCPC3) && order != relaxed => stilp |
| 870 |
- if compile_time(FEAT_LSE128) && order != relaxed => swpp |
| 871 |
- else => stp |
| 872 |
- if platform_supports_detection_of(FEAT_LSE2): |
| 873 |
- if detect(FEAT_LSE2) && detect(FEAT_LSE128) && order == seqcst => lse128 (swpp) |
| 874 |
- if detect(FEAT_LSE2) && detect(FEAT_LRCPC3) && order != relaxed => lse2_rcpc3 (stilp) |
| 875 |
- if detect(FEAT_LSE2) && detect(FEAT_LSE128) && order != relaxed => lse128 (swpp) |
| 876 |
- if detect(FEAT_LSE2) => lse2 (stp) |
| 877 |
- else => no_lse2: |
| 878 |
- if compile_time(FEAT_LSE) && not(ll_sc_rmw) => casp |
| 879 |
- else => ldxp_stxp |
| 880 |
|
| 881 |
Note: |
| 882 |
- If FEAT_LSE2 is available at compile-time, we don't do run-time detection of |
| 883 |
FEAT_LRCPC3/FEAT_LSE128 at this time, since FEAT_LRCPC3/FEAT_LSE128 is not yet available for most CPUs. |
| 884 |
(macOS that doesn't have any FEAT_LRCPC3/FEAT_LSE128-enabled CPUs as of M4 is only a platform |
| 885 |
that currently enables FEAT_LSE2 at compile-time by default.) |
| 886 |
- If FEAT_LSE2 is not available at compile-time, we want to do run-time detection |
| 887 |
of FEAT_LSE2, so we do run-time detection of FEAT_LRCPC3/FEAT_LSE128 at the same time. |
| 888 |
- We don't do run-time detection of FEAT_LSE for store at this time. |
| 889 |
|
| 890 |
*/ |
| 891 |
|
| 892 |
cfg_sel!({ |
| 893 |
// if compile_time(FEAT_LSE2) => stp: |
| 894 |
// cfg guarantee that the CPU supports FEAT_LSE2. |
| 895 |
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))] |
| 896 |
{ |
| 897 |
use self::_atomic_store_stp as atomic_store; |
| 898 |
} |
| 899 |
// if platform_supports_detection_of(FEAT_LSE2): |
| 900 |
#[cfg(all( |
| 901 |
not(portable_atomic_no_outline_atomics), |
| 902 |
any( |
| 903 |
all( |
| 904 |
target_os = "linux", |
| 905 |
any( |
| 906 |
target_env = "gnu", |
| 907 |
all( |
| 908 |
target_env = "musl", |
| 909 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 910 |
), |
| 911 |
target_env = "ohos", |
| 912 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 913 |
portable_atomic_outline_atomics, |
| 914 |
), |
| 915 |
), |
| 916 |
target_os = "android", |
| 917 |
target_os = "freebsd", |
| 918 |
target_os = "netbsd", |
| 919 |
target_os = "openbsd", |
| 920 |
target_os = "illumos", |
| 921 |
// These don't support detection of FEAT_LSE2. |
| 922 |
// target_os = "fuchsia", |
| 923 |
target_vendor = "apple", |
| 924 |
windows, |
| 925 |
), |
| 926 |
))] |
| 927 |
{ |
| 928 |
#[inline] |
| 929 |
unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) { |
| 930 |
#[cfg(any( |
| 931 |
target_feature = "lse128", |
| 932 |
portable_atomic_target_feature = "lse128", |
| 933 |
all( |
| 934 |
not(portable_atomic_no_outline_atomics), |
| 935 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 936 |
), |
| 937 |
))] |
| 938 |
#[inline] |
| 939 |
unsafe fn _atomic_store_swpp(dst: *mut u128, val: u128, order: Ordering) { |
| 940 |
// SAFETY: the caller must uphold the safety contract. |
| 941 |
unsafe { |
| 942 |
_atomic_swap_swpp(dst, val, order); |
| 943 |
} |
| 944 |
} |
| 945 |
fn_alias! { |
| 946 |
// inline(never) is just a hint and also not strictly necessary |
| 947 |
// because we use ifunc helper macro, but used for clarity. |
| 948 |
#[inline(never)] |
| 949 |
unsafe fn(dst: *mut u128, val: u128); |
| 950 |
atomic_store_lse2_relaxed = _atomic_store_stp(Ordering::Relaxed); |
| 951 |
atomic_store_lse2_release = _atomic_store_stp(Ordering::Release); |
| 952 |
atomic_store_lse2_seqcst = _atomic_store_stp(Ordering::SeqCst); |
| 953 |
atomic_store_lse2_rcpc3_release = _atomic_store_stilp(Ordering::Release); |
| 954 |
atomic_store_lse2_rcpc3_seqcst = _atomic_store_stilp(Ordering::SeqCst); |
| 955 |
atomic_store_lse128_release = _atomic_store_swpp(Ordering::Release); |
| 956 |
atomic_store_lse128_seqcst = _atomic_store_swpp(Ordering::SeqCst); |
| 957 |
} |
| 958 |
fn_alias! { |
| 959 |
unsafe fn(dst: *mut u128, val: u128); |
| 960 |
atomic_store_no_lse2_relaxed = atomic_store_no_lse2(Ordering::Relaxed); |
| 961 |
atomic_store_no_lse2_release = atomic_store_no_lse2(Ordering::Release); |
| 962 |
atomic_store_no_lse2_seqcst = atomic_store_no_lse2(Ordering::SeqCst); |
| 963 |
} |
| 964 |
// SAFETY: the caller must uphold the safety contract. |
| 965 |
// and we've checked if FEAT_LSE2/FEAT_LRCPC3/FEAT_LSE128 is available. |
| 966 |
unsafe { |
| 967 |
match order { |
| 968 |
Ordering::Relaxed => { |
| 969 |
ifunc!(unsafe fn(dst: *mut u128, val: u128) { |
| 970 |
let cpuinfo = detect::detect(); |
| 971 |
if cpuinfo.lse2() { |
| 972 |
// if detect(FEAT_LSE2) => lse2 (stp) |
| 973 |
atomic_store_lse2_relaxed |
| 974 |
} else { |
| 975 |
// else => no_lse2: |
| 976 |
atomic_store_no_lse2_relaxed |
| 977 |
} |
| 978 |
}); |
| 979 |
} |
| 980 |
Ordering::Release => { |
| 981 |
ifunc!(unsafe fn(dst: *mut u128, val: u128) { |
| 982 |
let cpuinfo = detect::detect(); |
| 983 |
if cpuinfo.lse2() { |
| 984 |
if cpuinfo.rcpc3() { |
| 985 |
// if detect(FEAT_LSE2) && detect(FEAT_LRCPC3) && order != relaxed => lse2_rcpc3 (stilp) |
| 986 |
atomic_store_lse2_rcpc3_release |
| 987 |
} else if cpuinfo.lse128() { |
| 988 |
// if detect(FEAT_LSE2) && detect(FEAT_LSE128) && order != relaxed => lse128 (swpp) |
| 989 |
atomic_store_lse128_release |
| 990 |
} else { |
| 991 |
// if detect(FEAT_LSE2) => lse2 (stp) |
| 992 |
atomic_store_lse2_release |
| 993 |
} |
| 994 |
} else { |
| 995 |
// else => no_lse2: |
| 996 |
atomic_store_no_lse2_release |
| 997 |
} |
| 998 |
}); |
| 999 |
} |
| 1000 |
Ordering::SeqCst => { |
| 1001 |
ifunc!(unsafe fn(dst: *mut u128, val: u128) { |
| 1002 |
let cpuinfo = detect::detect(); |
| 1003 |
if cpuinfo.lse2() { |
| 1004 |
if cpuinfo.lse128() { |
| 1005 |
// if detect(FEAT_LSE2) && detect(FEAT_LSE128) && order == seqcst => lse128 (swpp) |
| 1006 |
atomic_store_lse128_seqcst |
| 1007 |
} else if cpuinfo.rcpc3() { |
| 1008 |
// if detect(FEAT_LSE2) && detect(FEAT_LRCPC3) && order != relaxed => lse2_rcpc3 (stilp) |
| 1009 |
atomic_store_lse2_rcpc3_seqcst |
| 1010 |
} else { |
| 1011 |
// if detect(FEAT_LSE2) => lse2 (stp) |
| 1012 |
atomic_store_lse2_seqcst |
| 1013 |
} |
| 1014 |
} else { |
| 1015 |
// else => no_lse2: |
| 1016 |
atomic_store_no_lse2_seqcst |
| 1017 |
} |
| 1018 |
}); |
| 1019 |
} |
| 1020 |
_ => unreachable!(), |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
// else => no_lse2: |
| 1026 |
#[cfg(else)] |
| 1027 |
{ |
| 1028 |
use self::atomic_store_no_lse2 as atomic_store; |
| 1029 |
} |
| 1030 |
}); |
| 1031 |
#[cfg(not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")))] |
| 1032 |
cfg_sel!({ |
| 1033 |
// if compile_time(FEAT_LSE) && not(ll_sc_rmw) => casp |
| 1034 |
// If FEAT_LSE is available at compile-time and portable_atomic_ll_sc_rmw cfg is not set, |
| 1035 |
// we use CAS-based atomic RMW. |
| 1036 |
#[cfg(all( |
| 1037 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1038 |
not(portable_atomic_ll_sc_rmw), |
| 1039 |
))] |
| 1040 |
{ |
| 1041 |
#[inline] |
| 1042 |
unsafe fn atomic_store_no_lse2(dst: *mut u128, val: u128, order: Ordering) { |
| 1043 |
// SAFETY: the caller must uphold the safety contract. |
| 1044 |
// cfg guarantee that the CPU supports FEAT_LSE. |
| 1045 |
unsafe { |
| 1046 |
_atomic_swap_casp(dst, val, order); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
// else => ldxp_stxp |
| 1051 |
#[cfg(else)] |
| 1052 |
{ |
| 1053 |
use self::_atomic_store_ldxp_stxp as atomic_store_no_lse2; |
| 1054 |
} |
| 1055 |
}); |
| 1056 |
// If CPU supports FEAT_LSE2, STP/STILP is single-copy atomic writes, |
| 1057 |
// otherwise it is two single-copy atomic writes. |
| 1058 |
// Refs: B2.2.1 of the Arm Architecture Reference Manual Armv8, for Armv8-A architecture profile |
| 1059 |
#[cfg(any( |
| 1060 |
target_feature = "lse2", |
| 1061 |
portable_atomic_target_feature = "lse2", |
| 1062 |
not(portable_atomic_no_outline_atomics), |
| 1063 |
))] |
| 1064 |
#[inline] |
| 1065 |
unsafe fn _atomic_store_stp(dst: *mut u128, val: u128, order: Ordering) { |
| 1066 |
debug_assert!(dst.addr() % 16 == 0); |
| 1067 |
debug_assert_lse2!(); |
| 1068 |
|
| 1069 |
// SAFETY: the caller must guarantee that `dst` is valid for writes, |
| 1070 |
// 16-byte aligned, that there are no concurrent non-atomic operations. |
| 1071 |
// |
| 1072 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/STP--Store-pair-of-registers- |
| 1073 |
unsafe { |
| 1074 |
macro_rules! atomic_store { |
| 1075 |
($acquire:tt, $release:tt) => {{ |
| 1076 |
let val = U128 { whole: val }; |
| 1077 |
asm!( |
| 1078 |
$release, // fence |
| 1079 |
"stp {val_lo}, {val_hi}, [{dst}]", // atomic { *dst = val_lo:val_hi } |
| 1080 |
$acquire, // fence |
| 1081 |
dst = in(reg) ptr_reg!(dst), |
| 1082 |
val_lo = in(reg) val.pair.lo, |
| 1083 |
val_hi = in(reg) val.pair.hi, |
| 1084 |
options(nostack, preserves_flags), |
| 1085 |
); |
| 1086 |
}}; |
| 1087 |
} |
| 1088 |
match order { |
| 1089 |
// if compile_time(FEAT_LSE128) && order == seqcst => swpp |
| 1090 |
// Prefer swpp if stp requires fences. https://reviews.llvm.org/D143506 |
| 1091 |
// SAFETY: cfg guarantee that the CPU supports FEAT_LSE128. |
| 1092 |
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))] |
| 1093 |
Ordering::SeqCst => { |
| 1094 |
_atomic_swap_swpp(dst, val, order); |
| 1095 |
} |
| 1096 |
|
| 1097 |
// if compile_time(FEAT_LRCPC3) && order != relaxed => stilp |
| 1098 |
// SAFETY: cfg guarantee that the CPU supports FEAT_LRCPC3. |
| 1099 |
#[cfg(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3"))] |
| 1100 |
Ordering::Release => _atomic_store_stilp(dst, val, order), |
| 1101 |
#[cfg(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3"))] |
| 1102 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 1103 |
Ordering::SeqCst => _atomic_store_stilp(dst, val, order), |
| 1104 |
|
| 1105 |
// if compile_time(FEAT_LSE128) && order != relaxed => swpp |
| 1106 |
// Prefer swpp if stp requires fences. https://reviews.llvm.org/D143506 |
| 1107 |
// SAFETY: cfg guarantee that the CPU supports FEAT_LSE128. |
| 1108 |
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))] |
| 1109 |
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))] |
| 1110 |
Ordering::Release => { |
| 1111 |
_atomic_swap_swpp(dst, val, order); |
| 1112 |
} |
| 1113 |
|
| 1114 |
// else => stp |
| 1115 |
Ordering::Relaxed => atomic_store!("", ""), |
| 1116 |
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))] |
| 1117 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 1118 |
Ordering::Release => atomic_store!("", "dmb ish"), |
| 1119 |
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))] |
| 1120 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 1121 |
Ordering::SeqCst => atomic_store!("dmb ish", "dmb ish"), |
| 1122 |
_ => unreachable!(), |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
#[cfg(any( |
| 1127 |
target_feature = "lse2", |
| 1128 |
portable_atomic_target_feature = "lse2", |
| 1129 |
not(portable_atomic_no_outline_atomics), |
| 1130 |
))] |
| 1131 |
#[cfg(any( |
| 1132 |
target_feature = "rcpc3", |
| 1133 |
portable_atomic_target_feature = "rcpc3", |
| 1134 |
all( |
| 1135 |
not(portable_atomic_no_outline_atomics), |
| 1136 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 1137 |
), |
| 1138 |
))] |
| 1139 |
#[inline] |
| 1140 |
unsafe fn _atomic_store_stilp(dst: *mut u128, val: u128, order: Ordering) { |
| 1141 |
debug_assert!(dst.addr() % 16 == 0); |
| 1142 |
debug_assert_lse2!(); |
| 1143 |
debug_assert_rcpc3!(); |
| 1144 |
|
| 1145 |
// SAFETY: the caller must guarantee that `dst` is valid for writes, |
| 1146 |
// 16-byte aligned, that there are no concurrent non-atomic operations. |
| 1147 |
// |
| 1148 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/STILP--Store-release-ordered-pair-of-registers- |
| 1149 |
unsafe { |
| 1150 |
macro_rules! atomic_store { |
| 1151 |
($acquire:tt) => {{ |
| 1152 |
let val = U128 { whole: val }; |
| 1153 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 1154 |
asm!( |
| 1155 |
start_rcpc3!(), |
| 1156 |
"stilp {val_lo}, {val_hi}, [{dst}]", // atomic { *dst = val_lo:val_hi } |
| 1157 |
$acquire, // fence |
| 1158 |
dst = in(reg) ptr_reg!(dst), |
| 1159 |
val_lo = in(reg) val.pair.lo, |
| 1160 |
val_hi = in(reg) val.pair.hi, |
| 1161 |
options(nostack, preserves_flags), |
| 1162 |
); |
| 1163 |
// LLVM supports FEAT_LRCPC3 instructions on LLVM 16+, so use .inst directive on old LLVM. |
| 1164 |
// https://github.com/llvm/llvm-project/commit/a6aaa969f7caec58a994142f8d855861cf3a1463 |
| 1165 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 1166 |
asm!( |
| 1167 |
// stilp x2, x3, [x0] // atomic { *dst = x2:x3 } |
| 1168 |
".inst 0xd9031802", |
| 1169 |
$acquire, // fence |
| 1170 |
in("x0") ptr_reg!(dst), |
| 1171 |
in("x2") val.pair.lo, |
| 1172 |
in("x3") val.pair.hi, |
| 1173 |
options(nostack, preserves_flags), |
| 1174 |
); |
| 1175 |
}}; |
| 1176 |
} |
| 1177 |
match order { |
| 1178 |
Ordering::Release => atomic_store!(""), |
| 1179 |
// TODO: in atomicsabi64, seqcst store is the same as release store |
| 1180 |
// LLVM uses store-release (dmb ish; stp); dmb ish, GCC (libatomic) and Atomics ABI Standard |
| 1181 |
// uses store-release (stilp) without fence for SeqCst store |
| 1182 |
// (https://github.com/gcc-mirror/gcc/commit/7107574958e2bed11d916a1480ef1319f15e5ffe). |
| 1183 |
// Considering https://reviews.llvm.org/D141748, LLVM's lowering seems |
| 1184 |
// to be the safer option here (I'm not convinced that the libatomic's implementation is wrong). |
| 1185 |
Ordering::SeqCst => atomic_store!("dmb ish"), |
| 1186 |
_ => unreachable!(), |
| 1187 |
} |
| 1188 |
} |
| 1189 |
} |
| 1190 |
// Do not use _atomic_swap_ldxp_stxp because it needs extra registers to implement store. |
| 1191 |
#[cfg(any( |
| 1192 |
test, |
| 1193 |
not(all( |
| 1194 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1195 |
not(portable_atomic_ll_sc_rmw), |
| 1196 |
)) |
| 1197 |
))] |
| 1198 |
#[inline] |
| 1199 |
unsafe fn _atomic_store_ldxp_stxp(dst: *mut u128, val: u128, order: Ordering) { |
| 1200 |
debug_assert!(dst.addr() % 16 == 0); |
| 1201 |
|
| 1202 |
// SAFETY: the caller must uphold the safety contract. |
| 1203 |
unsafe { |
| 1204 |
let val = U128 { whole: val }; |
| 1205 |
macro_rules! store { |
| 1206 |
($acquire:tt, $release:tt, $msvc_fence:tt) => { |
| 1207 |
asm!( |
| 1208 |
"2:", // 'retry: |
| 1209 |
concat!("ld", $acquire, "xp xzr, {tmp}, [{dst}]"), // atomic { xzr:tmp = *dst; EXCLUSIVE = dst } |
| 1210 |
concat!("st", $release, "xp {tmp:w}, {val_lo}, {val_hi}, [{dst}]"), // atomic { if EXCLUSIVE == dst { *dst = val_lo:val_hi; tmp = 0 } else { tmp = 1 }; EXCLUSIVE = None } |
| 1211 |
"cbnz {tmp:w}, 2b", // if tmp != 0 { jump 'retry } |
| 1212 |
$msvc_fence, // fence |
| 1213 |
dst = in(reg) ptr_reg!(dst), |
| 1214 |
val_lo = in(reg) val.pair.lo, |
| 1215 |
val_hi = in(reg) val.pair.hi, |
| 1216 |
tmp = out(reg) _, |
| 1217 |
options(nostack, preserves_flags), |
| 1218 |
) |
| 1219 |
}; |
| 1220 |
} |
| 1221 |
atomic_rmw!(store, order); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
// ----------------------------------------------------------------------------- |
| 1226 |
// compare_exchange |
| 1227 |
|
| 1228 |
/* |
| 1229 |
|
| 1230 |
Instruction selection flow for compare_exchange: |
| 1231 |
- if compile_time(FEAT_LSE) => casp |
| 1232 |
- if platform_supports_detection_of(FEAT_LSE): |
| 1233 |
- if detect(FEAT_LSE) => casp |
| 1234 |
- else => ldxp_stxp |
| 1235 |
|
| 1236 |
*/ |
| 1237 |
|
| 1238 |
#[inline] |
| 1239 |
unsafe fn atomic_compare_exchange( |
| 1240 |
dst: *mut u128, |
| 1241 |
old: u128, |
| 1242 |
new: u128, |
| 1243 |
success: Ordering, |
| 1244 |
failure: Ordering, |
| 1245 |
) -> Result<u128, u128> { |
| 1246 |
// if compile_time(FEAT_LSE) => casp |
| 1247 |
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))] |
| 1248 |
// SAFETY: the caller must uphold the safety contract. |
| 1249 |
// cfg guarantee that the CPU supports FEAT_LSE. |
| 1250 |
let prev = unsafe { _atomic_compare_exchange_casp(dst, old, new, success, failure) }; |
| 1251 |
// if platform_supports_detection_of(FEAT_LSE): |
| 1252 |
#[cfg(all( |
| 1253 |
not(portable_atomic_no_outline_atomics), |
| 1254 |
any( |
| 1255 |
all( |
| 1256 |
target_os = "linux", |
| 1257 |
any( |
| 1258 |
target_env = "gnu", |
| 1259 |
all( |
| 1260 |
target_env = "musl", |
| 1261 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 1262 |
), |
| 1263 |
target_env = "ohos", |
| 1264 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 1265 |
portable_atomic_outline_atomics, |
| 1266 |
), |
| 1267 |
), |
| 1268 |
target_os = "android", |
| 1269 |
target_os = "freebsd", |
| 1270 |
target_os = "netbsd", |
| 1271 |
target_os = "openbsd", |
| 1272 |
target_os = "illumos", |
| 1273 |
target_os = "fuchsia", |
| 1274 |
target_vendor = "apple", |
| 1275 |
windows, |
| 1276 |
), |
| 1277 |
))] |
| 1278 |
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))] |
| 1279 |
let prev = { |
| 1280 |
fn_alias! { |
| 1281 |
// inline(never) is just a hint and also not strictly necessary |
| 1282 |
// because we use ifunc helper macro, but used for clarity. |
| 1283 |
#[inline(never)] |
| 1284 |
unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128; |
| 1285 |
atomic_compare_exchange_casp_relaxed |
| 1286 |
= _atomic_compare_exchange_casp(Ordering::Relaxed, Ordering::Relaxed); |
| 1287 |
atomic_compare_exchange_casp_acquire |
| 1288 |
= _atomic_compare_exchange_casp(Ordering::Acquire, Ordering::Acquire); |
| 1289 |
atomic_compare_exchange_casp_release |
| 1290 |
= _atomic_compare_exchange_casp(Ordering::Release, Ordering::Relaxed); |
| 1291 |
atomic_compare_exchange_casp_acqrel |
| 1292 |
= _atomic_compare_exchange_casp(Ordering::AcqRel, Ordering::Acquire); |
| 1293 |
} |
| 1294 |
fn_alias! { |
| 1295 |
unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128; |
| 1296 |
atomic_compare_exchange_ldxp_stxp_relaxed |
| 1297 |
= _atomic_compare_exchange_ldxp_stxp(Ordering::Relaxed, Ordering::Relaxed); |
| 1298 |
atomic_compare_exchange_ldxp_stxp_acquire |
| 1299 |
= _atomic_compare_exchange_ldxp_stxp(Ordering::Acquire, Ordering::Acquire); |
| 1300 |
atomic_compare_exchange_ldxp_stxp_release |
| 1301 |
= _atomic_compare_exchange_ldxp_stxp(Ordering::Release, Ordering::Relaxed); |
| 1302 |
atomic_compare_exchange_ldxp_stxp_acqrel |
| 1303 |
= _atomic_compare_exchange_ldxp_stxp(Ordering::AcqRel, Ordering::Acquire); |
| 1304 |
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments. |
| 1305 |
#[cfg(target_env = "msvc")] |
| 1306 |
atomic_compare_exchange_ldxp_stxp_seqcst |
| 1307 |
= _atomic_compare_exchange_ldxp_stxp(Ordering::SeqCst, Ordering::SeqCst); |
| 1308 |
} |
| 1309 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 1310 |
// reads, 16-byte aligned, that there are no concurrent non-atomic operations, |
| 1311 |
// and we've checked if FEAT_LSE is available. |
| 1312 |
unsafe { |
| 1313 |
let success = crate::utils::upgrade_success_ordering(success, failure); |
| 1314 |
match success { |
| 1315 |
Ordering::Relaxed => { |
| 1316 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 { |
| 1317 |
if detect::detect().lse() { |
| 1318 |
// if detect(FEAT_LSE) => casp |
| 1319 |
atomic_compare_exchange_casp_relaxed |
| 1320 |
} else { |
| 1321 |
// else => ldxp_stxp |
| 1322 |
atomic_compare_exchange_ldxp_stxp_relaxed |
| 1323 |
} |
| 1324 |
}) |
| 1325 |
} |
| 1326 |
Ordering::Acquire => { |
| 1327 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 { |
| 1328 |
if detect::detect().lse() { |
| 1329 |
// if detect(FEAT_LSE) => casp |
| 1330 |
atomic_compare_exchange_casp_acquire |
| 1331 |
} else { |
| 1332 |
// else => ldxp_stxp |
| 1333 |
atomic_compare_exchange_ldxp_stxp_acquire |
| 1334 |
} |
| 1335 |
}) |
| 1336 |
} |
| 1337 |
Ordering::Release => { |
| 1338 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 { |
| 1339 |
if detect::detect().lse() { |
| 1340 |
// if detect(FEAT_LSE) => casp |
| 1341 |
atomic_compare_exchange_casp_release |
| 1342 |
} else { |
| 1343 |
// else => ldxp_stxp |
| 1344 |
atomic_compare_exchange_ldxp_stxp_release |
| 1345 |
} |
| 1346 |
}) |
| 1347 |
} |
| 1348 |
// AcqRel and SeqCst RMWs are equivalent in both implementations in non-MSVC environments |
| 1349 |
// or LSE* instructions is used. |
| 1350 |
#[cfg(not(target_env = "msvc"))] |
| 1351 |
Ordering::AcqRel | Ordering::SeqCst => { |
| 1352 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 { |
| 1353 |
if detect::detect().lse() { |
| 1354 |
// if detect(FEAT_LSE) => casp |
| 1355 |
atomic_compare_exchange_casp_acqrel |
| 1356 |
} else { |
| 1357 |
// else => ldxp_stxp |
| 1358 |
atomic_compare_exchange_ldxp_stxp_acqrel |
| 1359 |
} |
| 1360 |
}) |
| 1361 |
} |
| 1362 |
#[cfg(target_env = "msvc")] |
| 1363 |
Ordering::AcqRel => { |
| 1364 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 { |
| 1365 |
if detect::detect().lse() { |
| 1366 |
// if detect(FEAT_LSE) => casp |
| 1367 |
atomic_compare_exchange_casp_acqrel |
| 1368 |
} else { |
| 1369 |
// else => ldxp_stxp |
| 1370 |
atomic_compare_exchange_ldxp_stxp_acqrel |
| 1371 |
} |
| 1372 |
}) |
| 1373 |
} |
| 1374 |
#[cfg(target_env = "msvc")] |
| 1375 |
Ordering::SeqCst => { |
| 1376 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 { |
| 1377 |
if detect::detect().lse() { |
| 1378 |
// if detect(FEAT_LSE) => casp |
| 1379 |
atomic_compare_exchange_casp_acqrel |
| 1380 |
} else { |
| 1381 |
// else => ldxp_stxp |
| 1382 |
atomic_compare_exchange_ldxp_stxp_seqcst |
| 1383 |
} |
| 1384 |
}) |
| 1385 |
} |
| 1386 |
_ => unreachable!(), |
| 1387 |
} |
| 1388 |
} |
| 1389 |
}; |
| 1390 |
// else => ldxp_stxp |
| 1391 |
#[cfg(not(all( |
| 1392 |
not(portable_atomic_no_outline_atomics), |
| 1393 |
any( |
| 1394 |
all( |
| 1395 |
target_os = "linux", |
| 1396 |
any( |
| 1397 |
target_env = "gnu", |
| 1398 |
all( |
| 1399 |
target_env = "musl", |
| 1400 |
any(not(target_feature = "crt-static"), feature = "std"), |
| 1401 |
), |
| 1402 |
target_env = "ohos", |
| 1403 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 1404 |
portable_atomic_outline_atomics, |
| 1405 |
), |
| 1406 |
), |
| 1407 |
target_os = "android", |
| 1408 |
target_os = "freebsd", |
| 1409 |
target_os = "netbsd", |
| 1410 |
target_os = "openbsd", |
| 1411 |
target_os = "illumos", |
| 1412 |
target_os = "fuchsia", |
| 1413 |
target_vendor = "apple", |
| 1414 |
windows, |
| 1415 |
), |
| 1416 |
)))] |
| 1417 |
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))] |
| 1418 |
// SAFETY: the caller must uphold the safety contract. |
| 1419 |
let prev = unsafe { _atomic_compare_exchange_ldxp_stxp(dst, old, new, success, failure) }; |
| 1420 |
if prev == old { Ok(prev) } else { Err(prev) } |
| 1421 |
} |
| 1422 |
#[cfg(any( |
| 1423 |
target_feature = "lse", |
| 1424 |
portable_atomic_target_feature = "lse", |
| 1425 |
not(portable_atomic_no_outline_atomics), |
| 1426 |
))] |
| 1427 |
#[inline] |
| 1428 |
unsafe fn _atomic_compare_exchange_casp( |
| 1429 |
dst: *mut u128, |
| 1430 |
old: u128, |
| 1431 |
new: u128, |
| 1432 |
success: Ordering, |
| 1433 |
failure: Ordering, |
| 1434 |
) -> u128 { |
| 1435 |
debug_assert!(dst.addr() % 16 == 0); |
| 1436 |
debug_assert_lse!(); |
| 1437 |
let order = crate::utils::upgrade_success_ordering(success, failure); |
| 1438 |
|
| 1439 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 1440 |
// reads, 16-byte aligned, that there are no concurrent non-atomic operations, |
| 1441 |
// and the CPU supports FEAT_LSE. |
| 1442 |
// |
| 1443 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/CASP--CASPA--CASPAL--CASPL--Compare-and-swap-pair-of-words-or-doublewords-in-memory- |
| 1444 |
unsafe { |
| 1445 |
let old = U128 { whole: old }; |
| 1446 |
let new = U128 { whole: new }; |
| 1447 |
let (prev_lo, prev_hi); |
| 1448 |
macro_rules! cmpxchg { |
| 1449 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 1450 |
asm!( |
| 1451 |
start_lse!(), |
| 1452 |
concat!("casp", $acquire, $release, " x6, x7, x4, x5, [{dst}]"), // atomic { if *dst == x6:x7 { *dst = x4:x5 } else { x6:x7 = *dst } } |
| 1453 |
dst = in(reg) ptr_reg!(dst), |
| 1454 |
// must be allocated to even/odd register pair |
| 1455 |
inout("x6") old.pair.lo => prev_lo, |
| 1456 |
inout("x7") old.pair.hi => prev_hi, |
| 1457 |
// must be allocated to even/odd register pair |
| 1458 |
in("x4") new.pair.lo, |
| 1459 |
in("x5") new.pair.hi, |
| 1460 |
options(nostack, preserves_flags), |
| 1461 |
) |
| 1462 |
}; |
| 1463 |
} |
| 1464 |
atomic_rmw!(cmpxchg, order, write = success); |
| 1465 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1466 |
} |
| 1467 |
} |
| 1468 |
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))] |
| 1469 |
#[inline] |
| 1470 |
unsafe fn _atomic_compare_exchange_ldxp_stxp( |
| 1471 |
dst: *mut u128, |
| 1472 |
old: u128, |
| 1473 |
new: u128, |
| 1474 |
success: Ordering, |
| 1475 |
failure: Ordering, |
| 1476 |
) -> u128 { |
| 1477 |
debug_assert!(dst.addr() % 16 == 0); |
| 1478 |
let order = crate::utils::upgrade_success_ordering(success, failure); |
| 1479 |
|
| 1480 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 1481 |
// reads, 16-byte aligned, and that there are no concurrent non-atomic operations. |
| 1482 |
// |
| 1483 |
// Refs: |
| 1484 |
// - LDXP: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/LDXP--Load-exclusive-pair-of-registers- |
| 1485 |
// - LDAXP: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/LDAXP--Load-acquire-exclusive-pair-of-registers- |
| 1486 |
// - STXP: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/STXP--Store-exclusive-pair-of-registers- |
| 1487 |
// - STLXP: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/STLXP--Store-release-exclusive-pair-of-registers- |
| 1488 |
// |
| 1489 |
// Note: Load-Exclusive pair (by itself) does not guarantee atomicity; to complete an atomic |
| 1490 |
// operation (even load/store), a corresponding Store-Exclusive pair must succeed. |
| 1491 |
// See Arm Architecture Reference Manual for A-profile architecture |
| 1492 |
// Section B2.2.1 "Requirements for single-copy atomicity", and |
| 1493 |
// Section B2.9 "Synchronization and semaphores" for more. |
| 1494 |
unsafe { |
| 1495 |
let old = U128 { whole: old }; |
| 1496 |
let new = U128 { whole: new }; |
| 1497 |
let (mut prev_lo, mut prev_hi); |
| 1498 |
macro_rules! cmpxchg { |
| 1499 |
($acquire:tt, $release:tt, $msvc_fence:tt) => { |
| 1500 |
asm!( |
| 1501 |
"2:", // 'retry: |
| 1502 |
concat!("ld", $acquire, "xp {prev_lo}, {prev_hi}, [{dst}]"), // atomic { prev_lo:prev_hi = *dst; EXCLUSIVE = dst } |
| 1503 |
"cmp {prev_lo}, {old_lo}", // if prev_lo == old_lo { Z = 1 } else { Z = 0 } |
| 1504 |
"ccmp {prev_hi}, {old_hi}, #0, eq", // if Z == 1 { if prev_hi == old_hi { Z = 1 } else { Z = 0 } } else { Z = 0 } |
| 1505 |
// write back to ensure atomicity |
| 1506 |
"csel {tmp_lo}, {new_lo}, {prev_lo}, eq", // if Z == 1 { tmp_lo = new_lo } else { tmp_lo = prev_lo } |
| 1507 |
"csel {tmp_hi}, {new_hi}, {prev_hi}, eq", // if Z == 1 { tmp_hi = new_hi } else { tmp_hi = prev_hi } |
| 1508 |
concat!("st", $release, "xp {r:w}, {tmp_lo}, {tmp_hi}, [{dst}]"), // atomic { if EXCLUSIVE == dst { *dst = tmp_lo:tmp_hi; r = 0 } else { r = 1 }; EXCLUSIVE = None } |
| 1509 |
"cbnz {r:w}, 2b", // if r != 0 { jump 'retry } |
| 1510 |
$msvc_fence, // fence |
| 1511 |
dst = in(reg) ptr_reg!(dst), |
| 1512 |
old_lo = in(reg) old.pair.lo, |
| 1513 |
old_hi = in(reg) old.pair.hi, |
| 1514 |
new_lo = in(reg) new.pair.lo, |
| 1515 |
new_hi = in(reg) new.pair.hi, |
| 1516 |
prev_lo = out(reg) prev_lo, |
| 1517 |
prev_hi = out(reg) prev_hi, |
| 1518 |
r = out(reg) _, |
| 1519 |
tmp_lo = out(reg) _, |
| 1520 |
tmp_hi = out(reg) _, |
| 1521 |
// Do not use `preserves_flags` because CMP and CCMP modify the condition flags. |
| 1522 |
options(nostack), |
| 1523 |
) |
| 1524 |
}; |
| 1525 |
} |
| 1526 |
atomic_rmw!(cmpxchg, order, write = success); |
| 1527 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1528 |
} |
| 1529 |
} |
| 1530 |
|
| 1531 |
// casp is always strong, and ldxp requires a corresponding (succeed) stxp for |
| 1532 |
// its atomicity (see code comment in _atomic_compare_exchange_ldxp_stxp). |
| 1533 |
// (i.e., AArch64 doesn't have 128-bit weak CAS) |
| 1534 |
use self::atomic_compare_exchange as atomic_compare_exchange_weak; |
| 1535 |
|
| 1536 |
// ----------------------------------------------------------------------------- |
| 1537 |
// RMW |
| 1538 |
|
| 1539 |
/* |
| 1540 |
|
| 1541 |
Instruction selection flow for swap/fetch_and/fetch_or: |
| 1542 |
- if compile_time(FEAT_LSE128) => swpp/ldclrp/ldsetp |
| 1543 |
- if compile_time(FEAT_LSE) && not(ll_sc_rmw) => casp |
| 1544 |
- else => ldxp_stxp |
| 1545 |
|
| 1546 |
Instruction selection flow for other RMWs: |
| 1547 |
- if compile_time(FEAT_LSE) && not(ll_sc_rmw) => casp |
| 1548 |
- else => ldxp_stxp |
| 1549 |
|
| 1550 |
Note: |
| 1551 |
- We don't do run-time detection of FEAT_LSE128 at this time, because |
| 1552 |
FEAT_LSE128 is not yet available for most CPUs, but since |
| 1553 |
swpp/ldclrp/ldsetp is wait-free, it would make sense to do run-time |
| 1554 |
detection in the future. (TODO) |
| 1555 |
- We don't do run-time detection of FEAT_LSE for store at this time. |
| 1556 |
|
| 1557 |
*/ |
| 1558 |
|
| 1559 |
cfg_sel!({ |
| 1560 |
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))] |
| 1561 |
{ |
| 1562 |
use self::_atomic_swap_swpp as atomic_swap; |
| 1563 |
} |
| 1564 |
#[cfg(all( |
| 1565 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1566 |
not(portable_atomic_ll_sc_rmw), |
| 1567 |
))] |
| 1568 |
{ |
| 1569 |
use self::_atomic_swap_casp as atomic_swap; |
| 1570 |
} |
| 1571 |
#[cfg(else)] |
| 1572 |
{ |
| 1573 |
use self::_atomic_swap_ldxp_stxp as atomic_swap; |
| 1574 |
} |
| 1575 |
}); |
| 1576 |
#[cfg(any( |
| 1577 |
target_feature = "lse128", |
| 1578 |
portable_atomic_target_feature = "lse128", |
| 1579 |
all( |
| 1580 |
not(portable_atomic_no_outline_atomics), |
| 1581 |
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")), |
| 1582 |
), |
| 1583 |
))] |
| 1584 |
#[inline] |
| 1585 |
unsafe fn _atomic_swap_swpp(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 1586 |
debug_assert!(dst.addr() % 16 == 0); |
| 1587 |
debug_assert_lse128!(); |
| 1588 |
|
| 1589 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 1590 |
// reads, 16-byte aligned, that there are no concurrent non-atomic operations, |
| 1591 |
// and the CPU supports FEAT_LSE128. |
| 1592 |
// |
| 1593 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/SWPP--SWPPA--SWPPAL--SWPPL--Swap-quadword-in-memory- |
| 1594 |
unsafe { |
| 1595 |
let val = U128 { whole: val }; |
| 1596 |
let (prev_lo, prev_hi); |
| 1597 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 1598 |
macro_rules! swap { |
| 1599 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 1600 |
asm!( |
| 1601 |
start_lse128!(), |
| 1602 |
concat!("swpp", $acquire, $release, " {val_lo}, {val_hi}, [{dst}]"), // atomic { _x = *dst; *dst = val_lo:val_hi; val_lo:val_hi = _x } |
| 1603 |
dst = in(reg) ptr_reg!(dst), |
| 1604 |
val_lo = inout(reg) val.pair.lo => prev_lo, |
| 1605 |
val_hi = inout(reg) val.pair.hi => prev_hi, |
| 1606 |
options(nostack, preserves_flags), |
| 1607 |
) |
| 1608 |
}; |
| 1609 |
} |
| 1610 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 1611 |
atomic_rmw!(swap, order); |
| 1612 |
// LLVM supports FEAT_LSE128 instructions on LLVM 16+, so use .inst directive on old LLVM. |
| 1613 |
// https://github.com/llvm/llvm-project/commit/7fea6f2e0e606e5339c3359568f680eaf64aa306 |
| 1614 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 1615 |
macro_rules! swap { |
| 1616 |
($order:tt, $_msvc_fence:tt) => { |
| 1617 |
asm!( |
| 1618 |
// swpp{,a,l,al} x2, x1, [x0] // atomic { _x = *x0; *x0 = x2:x1; x2:x1 = _x } |
| 1619 |
concat!(".inst 0x19", $order, "18002"), |
| 1620 |
in("x0") ptr_reg!(dst), |
| 1621 |
inout("x2") val.pair.lo => prev_lo, |
| 1622 |
inout("x1") val.pair.hi => prev_hi, |
| 1623 |
options(nostack, preserves_flags), |
| 1624 |
) |
| 1625 |
}; |
| 1626 |
} |
| 1627 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 1628 |
atomic_rmw_inst!(swap, order); |
| 1629 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1630 |
} |
| 1631 |
} |
| 1632 |
// Do not use atomic_rmw_cas_3 because it needs extra MOV to implement swap. |
| 1633 |
#[cfg(any(test, not(portable_atomic_ll_sc_rmw)))] |
| 1634 |
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))] |
| 1635 |
#[inline] |
| 1636 |
unsafe fn _atomic_swap_casp(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 1637 |
debug_assert!(dst.addr() % 16 == 0); |
| 1638 |
debug_assert_lse!(); |
| 1639 |
|
| 1640 |
// SAFETY: the caller must uphold the safety contract. |
| 1641 |
// cfg guarantee that the CPU supports FEAT_LSE. |
| 1642 |
unsafe { |
| 1643 |
let val = U128 { whole: val }; |
| 1644 |
let (mut prev_lo, mut prev_hi); |
| 1645 |
macro_rules! swap { |
| 1646 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 1647 |
asm!( |
| 1648 |
start_lse!(), |
| 1649 |
// If FEAT_LSE2 is not supported, this works like byte-wise atomic. |
| 1650 |
// This is not single-copy atomic reads, but this is ok because subsequent |
| 1651 |
// CAS will check for consistency. |
| 1652 |
"ldp x4, x5, [{dst}]", // atomic { x4:x5 = *dst } |
| 1653 |
"2:", // 'retry: |
| 1654 |
// casp writes the current value to the first register pair, |
| 1655 |
// so copy the `out`'s value for later comparison. |
| 1656 |
"mov {tmp_lo}, x4", // tmp_lo = x4 |
| 1657 |
"mov {tmp_hi}, x5", // tmp_hi = x5 |
| 1658 |
concat!("casp", $acquire, $release, " x4, x5, x2, x3, [{dst}]"), // atomic { if *dst == x4:x5 { *dst = x2:x3 } else { x4:x5 = *dst } } |
| 1659 |
"cmp {tmp_hi}, x5", // if tmp_hi == x5 { Z = 1 } else { Z = 0 } |
| 1660 |
"ccmp {tmp_lo}, x4, #0, eq", // if Z == 1 { if tmp_lo == x4 { Z = 1 } else { Z = 0 } } else { Z = 0 } |
| 1661 |
"b.ne 2b", // if Z == 0 { jump 'retry } |
| 1662 |
dst = in(reg) ptr_reg!(dst), |
| 1663 |
tmp_lo = out(reg) _, |
| 1664 |
tmp_hi = out(reg) _, |
| 1665 |
// must be allocated to even/odd register pair |
| 1666 |
out("x4") prev_lo, |
| 1667 |
out("x5") prev_hi, |
| 1668 |
// must be allocated to even/odd register pair |
| 1669 |
in("x2") val.pair.lo, |
| 1670 |
in("x3") val.pair.hi, |
| 1671 |
// Do not use `preserves_flags` because CMP and CCMP modify the condition flags. |
| 1672 |
options(nostack), |
| 1673 |
) |
| 1674 |
}; |
| 1675 |
} |
| 1676 |
atomic_rmw!(swap, order); |
| 1677 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1678 |
} |
| 1679 |
} |
| 1680 |
// Do not use atomic_rmw_ll_sc_3 because it needs extra MOV to implement swap. |
| 1681 |
#[cfg(any( |
| 1682 |
test, |
| 1683 |
not(all( |
| 1684 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1685 |
not(portable_atomic_ll_sc_rmw), |
| 1686 |
)) |
| 1687 |
))] |
| 1688 |
#[inline] |
| 1689 |
unsafe fn _atomic_swap_ldxp_stxp(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 1690 |
debug_assert!(dst.addr() % 16 == 0); |
| 1691 |
|
| 1692 |
// SAFETY: the caller must uphold the safety contract. |
| 1693 |
unsafe { |
| 1694 |
let val = U128 { whole: val }; |
| 1695 |
let (mut prev_lo, mut prev_hi); |
| 1696 |
macro_rules! swap { |
| 1697 |
($acquire:tt, $release:tt, $msvc_fence:tt) => { |
| 1698 |
asm!( |
| 1699 |
"2:", // 'retry: |
| 1700 |
concat!("ld", $acquire, "xp {prev_lo}, {prev_hi}, [{dst}]"), // atomic { prev_lo:prev_hi = *dst; EXCLUSIVE = dst } |
| 1701 |
concat!("st", $release, "xp {r:w}, {val_lo}, {val_hi}, [{dst}]"), // atomic { if EXCLUSIVE == dst { *dst = val_lo:val_hi; r = 0 } else { r = 1 }; EXCLUSIVE = None } |
| 1702 |
"cbnz {r:w}, 2b", // if r != 0 { jump 'retry } |
| 1703 |
$msvc_fence, // fence |
| 1704 |
dst = in(reg) ptr_reg!(dst), |
| 1705 |
val_lo = in(reg) val.pair.lo, |
| 1706 |
val_hi = in(reg) val.pair.hi, |
| 1707 |
prev_lo = out(reg) prev_lo, |
| 1708 |
prev_hi = out(reg) prev_hi, |
| 1709 |
r = out(reg) _, |
| 1710 |
options(nostack, preserves_flags), |
| 1711 |
) |
| 1712 |
}; |
| 1713 |
} |
| 1714 |
atomic_rmw!(swap, order); |
| 1715 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1716 |
} |
| 1717 |
} |
| 1718 |
|
| 1719 |
/// Atomic RMW by LL/SC loop (3 arguments) |
| 1720 |
/// `unsafe fn(dst: *mut u128, val: u128, order: Ordering) -> u128;` |
| 1721 |
/// |
| 1722 |
/// `$op` can use the following registers: |
| 1723 |
/// - val_lo/val_hi pair: val argument (read-only for `$op`) |
| 1724 |
/// - prev_lo/prev_hi pair: previous value loaded by ll (read-only for `$op`) |
| 1725 |
/// - new_lo/new_hi pair: new value that will be stored by sc |
| 1726 |
macro_rules! atomic_rmw_ll_sc_3 { |
| 1727 |
($name:ident as $reexport_name:ident $(($preserves_flags:tt))?, $($op:tt)*) => { |
| 1728 |
// If FEAT_LSE is available at compile-time and portable_atomic_ll_sc_rmw cfg is not set, |
| 1729 |
// we use CAS-based atomic RMW generated by atomic_rmw_cas_3! macro instead. |
| 1730 |
#[cfg(not(all( |
| 1731 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1732 |
not(portable_atomic_ll_sc_rmw), |
| 1733 |
)))] |
| 1734 |
use self::$name as $reexport_name; |
| 1735 |
#[cfg(any( |
| 1736 |
test, |
| 1737 |
not(all( |
| 1738 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1739 |
not(portable_atomic_ll_sc_rmw), |
| 1740 |
)) |
| 1741 |
))] |
| 1742 |
#[inline] |
| 1743 |
unsafe fn $name(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 1744 |
debug_assert!(dst.addr() % 16 == 0); |
| 1745 |
// SAFETY: the caller must uphold the safety contract. |
| 1746 |
unsafe { |
| 1747 |
let val = U128 { whole: val }; |
| 1748 |
let (mut prev_lo, mut prev_hi); |
| 1749 |
macro_rules! op { |
| 1750 |
($acquire:tt, $release:tt, $msvc_fence:tt) => { |
| 1751 |
asm!( |
| 1752 |
"2:", // 'retry: |
| 1753 |
concat!("ld", $acquire, "xp {prev_lo}, {prev_hi}, [{dst}]"), // atomic { prev_lo:prev_hi = *dst; EXCLUSIVE = dst } |
| 1754 |
$($op)* |
| 1755 |
concat!("st", $release, "xp {r:w}, {new_lo}, {new_hi}, [{dst}]"), // atomic { if EXCLUSIVE == dst { *dst = new_lo:new_hi; r = 0 } else { r = 1 }; EXCLUSIVE = None } |
| 1756 |
"cbnz {r:w}, 2b", // if r != 0 { jump 'retry } |
| 1757 |
$msvc_fence, // fence |
| 1758 |
dst = in(reg) ptr_reg!(dst), |
| 1759 |
val_lo = in(reg) val.pair.lo, |
| 1760 |
val_hi = in(reg) val.pair.hi, |
| 1761 |
prev_lo = out(reg) prev_lo, |
| 1762 |
prev_hi = out(reg) prev_hi, |
| 1763 |
new_lo = out(reg) _, |
| 1764 |
new_hi = out(reg) _, |
| 1765 |
r = out(reg) _, |
| 1766 |
options(nostack $(, $preserves_flags)?), |
| 1767 |
) |
| 1768 |
}; |
| 1769 |
} |
| 1770 |
atomic_rmw!(op, order); |
| 1771 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1772 |
} |
| 1773 |
} |
| 1774 |
}; |
| 1775 |
} |
| 1776 |
/// Atomic RMW by CAS loop (3 arguments) |
| 1777 |
/// `unsafe fn(dst: *mut u128, val: u128, order: Ordering) -> u128;` |
| 1778 |
/// |
| 1779 |
/// `$op` can use the following registers: |
| 1780 |
/// - val_lo/val_hi pair: val argument (read-only for `$op`) |
| 1781 |
/// - x6/x7 pair: previous value loaded (read-only for `$op`) |
| 1782 |
/// - x4/x5 pair: new value that will be stored |
| 1783 |
macro_rules! atomic_rmw_cas_3 { |
| 1784 |
($name:ident as $reexport_name:ident, $($op:tt)*) => { |
| 1785 |
// If FEAT_LSE is not available at compile-time or portable_atomic_ll_sc_rmw cfg is set, |
| 1786 |
// we use LL/SC-based atomic RMW generated by atomic_rmw_ll_sc_3! macro instead. |
| 1787 |
#[cfg(all( |
| 1788 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1789 |
not(portable_atomic_ll_sc_rmw), |
| 1790 |
))] |
| 1791 |
use self::$name as $reexport_name; |
| 1792 |
#[cfg(any(test, not(portable_atomic_ll_sc_rmw)))] |
| 1793 |
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))] |
| 1794 |
#[inline] |
| 1795 |
unsafe fn $name(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 1796 |
debug_assert!(dst.addr() % 16 == 0); |
| 1797 |
debug_assert_lse!(); |
| 1798 |
// SAFETY: the caller must uphold the safety contract. |
| 1799 |
// cfg guarantee that the CPU supports FEAT_LSE. |
| 1800 |
unsafe { |
| 1801 |
let val = U128 { whole: val }; |
| 1802 |
let (mut prev_lo, mut prev_hi); |
| 1803 |
macro_rules! op { |
| 1804 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 1805 |
asm!( |
| 1806 |
start_lse!(), |
| 1807 |
// If FEAT_LSE2 is not supported, this works like byte-wise atomic. |
| 1808 |
// This is not single-copy atomic reads, but this is ok because subsequent |
| 1809 |
// CAS will check for consistency. |
| 1810 |
"ldp x6, x7, [{dst}]", // atomic { x6:x7 = *dst } |
| 1811 |
"2:", // 'retry: |
| 1812 |
// casp writes the current value to the first register pair, |
| 1813 |
// so copy the `out`'s value for later comparison. |
| 1814 |
"mov {tmp_lo}, x6", // tmp_lo = x6 |
| 1815 |
"mov {tmp_hi}, x7", // tmp_hi = x7 |
| 1816 |
$($op)* |
| 1817 |
concat!("casp", $acquire, $release, " x6, x7, x4, x5, [{dst}]"), // atomic { if *dst == x6:x7 { *dst = x4:x5 } else { x6:x7 = *dst } } |
| 1818 |
"cmp {tmp_hi}, x7", // if tmp_hi == x7 { Z = 1 } else { Z = 0 } |
| 1819 |
"ccmp {tmp_lo}, x6, #0, eq", // if Z == 1 { if tmp_lo == x6 { Z = 1 } else { Z = 0 } } else { Z = 0 } |
| 1820 |
"b.ne 2b", // if Z == 0 { jump 'retry } |
| 1821 |
dst = in(reg) ptr_reg!(dst), |
| 1822 |
val_lo = in(reg) val.pair.lo, |
| 1823 |
val_hi = in(reg) val.pair.hi, |
| 1824 |
tmp_lo = out(reg) _, |
| 1825 |
tmp_hi = out(reg) _, |
| 1826 |
// must be allocated to even/odd register pair |
| 1827 |
out("x6") prev_lo, |
| 1828 |
out("x7") prev_hi, |
| 1829 |
// must be allocated to even/odd register pair |
| 1830 |
out("x4") _, |
| 1831 |
out("x5") _, |
| 1832 |
// Do not use `preserves_flags` because CMP and CCMP modify the condition flags. |
| 1833 |
options(nostack), |
| 1834 |
) |
| 1835 |
}; |
| 1836 |
} |
| 1837 |
atomic_rmw!(op, order); |
| 1838 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1839 |
} |
| 1840 |
} |
| 1841 |
}; |
| 1842 |
} |
| 1843 |
|
| 1844 |
/// Atomic RMW by LL/SC loop (2 arguments) |
| 1845 |
/// `unsafe fn(dst: *mut u128, order: Ordering) -> u128;` |
| 1846 |
/// |
| 1847 |
/// `$op` can use the following registers: |
| 1848 |
/// - prev_lo/prev_hi pair: previous value loaded by ll (read-only for `$op`) |
| 1849 |
/// - new_lo/new_hi pair: new value that will be stored by sc |
| 1850 |
macro_rules! atomic_rmw_ll_sc_2 { |
| 1851 |
($name:ident as $reexport_name:ident $(($preserves_flags:tt))?, $($op:tt)*) => { |
| 1852 |
// If FEAT_LSE is available at compile-time and portable_atomic_ll_sc_rmw cfg is not set, |
| 1853 |
// we use CAS-based atomic RMW generated by atomic_rmw_cas_2! macro instead. |
| 1854 |
#[cfg(not(all( |
| 1855 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1856 |
not(portable_atomic_ll_sc_rmw), |
| 1857 |
)))] |
| 1858 |
use self::$name as $reexport_name; |
| 1859 |
#[cfg(any( |
| 1860 |
test, |
| 1861 |
not(all( |
| 1862 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1863 |
not(portable_atomic_ll_sc_rmw), |
| 1864 |
)) |
| 1865 |
))] |
| 1866 |
#[inline] |
| 1867 |
unsafe fn $name(dst: *mut u128, order: Ordering) -> u128 { |
| 1868 |
debug_assert!(dst.addr() % 16 == 0); |
| 1869 |
// SAFETY: the caller must uphold the safety contract. |
| 1870 |
unsafe { |
| 1871 |
let (mut prev_lo, mut prev_hi); |
| 1872 |
macro_rules! op { |
| 1873 |
($acquire:tt, $release:tt, $msvc_fence:tt) => { |
| 1874 |
asm!( |
| 1875 |
"2:", // 'retry: |
| 1876 |
concat!("ld", $acquire, "xp {prev_lo}, {prev_hi}, [{dst}]"), // atomic { prev_lo:prev_hi = *dst; EXCLUSIVE = dst } |
| 1877 |
$($op)* |
| 1878 |
concat!("st", $release, "xp {r:w}, {new_lo}, {new_hi}, [{dst}]"), // atomic { if EXCLUSIVE == dst { *dst = new_lo:new_hi; r = 0 } else { r = 1 }; EXCLUSIVE = None } |
| 1879 |
"cbnz {r:w}, 2b", // if r != 0 { jump 'retry } |
| 1880 |
$msvc_fence, // fence |
| 1881 |
dst = in(reg) ptr_reg!(dst), |
| 1882 |
prev_lo = out(reg) prev_lo, |
| 1883 |
prev_hi = out(reg) prev_hi, |
| 1884 |
new_lo = out(reg) _, |
| 1885 |
new_hi = out(reg) _, |
| 1886 |
r = out(reg) _, |
| 1887 |
options(nostack $(, $preserves_flags)?), |
| 1888 |
) |
| 1889 |
}; |
| 1890 |
} |
| 1891 |
atomic_rmw!(op, order); |
| 1892 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1893 |
} |
| 1894 |
} |
| 1895 |
}; |
| 1896 |
} |
| 1897 |
/// Atomic RMW by CAS loop (2 arguments) |
| 1898 |
/// `unsafe fn(dst: *mut u128, order: Ordering) -> u128;` |
| 1899 |
/// |
| 1900 |
/// `$op` can use the following registers: |
| 1901 |
/// - x6/x7 pair: previous value loaded (read-only for `$op`) |
| 1902 |
/// - x4/x5 pair: new value that will be stored |
| 1903 |
macro_rules! atomic_rmw_cas_2 { |
| 1904 |
($name:ident as $reexport_name:ident, $($op:tt)*) => { |
| 1905 |
// If FEAT_LSE is not available at compile-time or portable_atomic_ll_sc_rmw cfg is set, |
| 1906 |
// we use LL/SC-based atomic RMW generated by atomic_rmw_ll_sc_3! macro instead. |
| 1907 |
#[cfg(all( |
| 1908 |
any(target_feature = "lse", portable_atomic_target_feature = "lse"), |
| 1909 |
not(portable_atomic_ll_sc_rmw), |
| 1910 |
))] |
| 1911 |
use self::$name as $reexport_name; |
| 1912 |
#[cfg(any(test, not(portable_atomic_ll_sc_rmw)))] |
| 1913 |
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))] |
| 1914 |
#[inline] |
| 1915 |
unsafe fn $name(dst: *mut u128, order: Ordering) -> u128 { |
| 1916 |
debug_assert!(dst.addr() % 16 == 0); |
| 1917 |
debug_assert_lse!(); |
| 1918 |
// SAFETY: the caller must uphold the safety contract. |
| 1919 |
// cfg guarantee that the CPU supports FEAT_LSE. |
| 1920 |
unsafe { |
| 1921 |
let (mut prev_lo, mut prev_hi); |
| 1922 |
macro_rules! op { |
| 1923 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 1924 |
asm!( |
| 1925 |
start_lse!(), |
| 1926 |
// If FEAT_LSE2 is not supported, this works like byte-wise atomic. |
| 1927 |
// This is not single-copy atomic reads, but this is ok because subsequent |
| 1928 |
// CAS will check for consistency. |
| 1929 |
"ldp x6, x7, [{dst}]", // atomic { x6:x7 = *dst } |
| 1930 |
"2:", // 'retry: |
| 1931 |
// casp writes the current value to the first register pair, |
| 1932 |
// so copy the `out`'s value for later comparison. |
| 1933 |
"mov {tmp_lo}, x6", // tmp_lo = x6 |
| 1934 |
"mov {tmp_hi}, x7", // tmp_hi = x7 |
| 1935 |
$($op)* |
| 1936 |
concat!("casp", $acquire, $release, " x6, x7, x4, x5, [{dst}]"), // atomic { if *dst == x6:x7 { *dst = x4:x5 } else { x6:x7 = *dst } } |
| 1937 |
"cmp {tmp_hi}, x7", // if tmp_hi == x7 { Z = 1 } else { Z = 0 } |
| 1938 |
"ccmp {tmp_lo}, x6, #0, eq", // if Z == 1 { if tmp_lo == x6 { Z = 1 } else { Z = 0 } } else { Z = 0 } |
| 1939 |
"b.ne 2b", // if Z == 0 { jump 'retry } |
| 1940 |
dst = in(reg) ptr_reg!(dst), |
| 1941 |
tmp_lo = out(reg) _, |
| 1942 |
tmp_hi = out(reg) _, |
| 1943 |
// must be allocated to even/odd register pair |
| 1944 |
out("x6") prev_lo, |
| 1945 |
out("x7") prev_hi, |
| 1946 |
// must be allocated to even/odd register pair |
| 1947 |
out("x4") _, |
| 1948 |
out("x5") _, |
| 1949 |
// Do not use `preserves_flags` because CMP and CCMP modify the condition flags. |
| 1950 |
options(nostack), |
| 1951 |
) |
| 1952 |
}; |
| 1953 |
} |
| 1954 |
atomic_rmw!(op, order); |
| 1955 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 1956 |
} |
| 1957 |
} |
| 1958 |
}; |
| 1959 |
} |
| 1960 |
|
| 1961 |
// Do not use `preserves_flags` because ADDS modifies the condition flags. |
| 1962 |
atomic_rmw_ll_sc_3! { |
| 1963 |
_atomic_add_ldxp_stxp as atomic_add, |
| 1964 |
select_le_or_be!("adds {new_lo}, {prev_lo}, {val_lo}", "adds {new_hi}, {prev_hi}, {val_hi}"), // if endian == little { new_lo = prev_lo + val_lo } else { new_hi = prev_hi + val_hi }; C = carry |
| 1965 |
select_le_or_be!("adc {new_hi}, {prev_hi}, {val_hi}", "adc {new_lo}, {prev_lo}, {val_lo}"), // if endian == little { new_hi = prev_hi + val_hi + C } else { new_lo = prev_lo + val_lo + C } |
| 1966 |
} |
| 1967 |
atomic_rmw_cas_3! { |
| 1968 |
_atomic_add_casp as atomic_add, |
| 1969 |
select_le_or_be!("adds x4, x6, {val_lo}", "adds x5, x7, {val_hi}"), // if endian == little { x4 = x6 + val_lo } else { x5 = x7 + val_hi }; C = carry |
| 1970 |
select_le_or_be!("adc x5, x7, {val_hi}", "adc x4, x6, {val_lo}"), // if endian == little { x5 = x7 + val_hi + C } else { x4 = x6 + val_lo + C } |
| 1971 |
} |
| 1972 |
|
| 1973 |
// Do not use `preserves_flags` because SUBS modifies the condition flags. |
| 1974 |
atomic_rmw_ll_sc_3! { |
| 1975 |
_atomic_sub_ldxp_stxp as atomic_sub, |
| 1976 |
select_le_or_be!("subs {new_lo}, {prev_lo}, {val_lo}", "subs {new_hi}, {prev_hi}, {val_hi}"), // if endian == little { new_lo = prev_lo - val_lo } else { new_hi = prev_hi - val_hi }; C = !borrow |
| 1977 |
select_le_or_be!("sbc {new_hi}, {prev_hi}, {val_hi}", "sbc {new_lo}, {prev_lo}, {val_lo}"), // if endian == little { new_hi = prev_hi - val_hi - !C } else { new_lo = prev_lo - val_lo - !C } |
| 1978 |
} |
| 1979 |
atomic_rmw_cas_3! { |
| 1980 |
_atomic_sub_casp as atomic_sub, |
| 1981 |
select_le_or_be!("subs x4, x6, {val_lo}", "subs x5, x7, {val_hi}"), // if endian == little { x4 = x6 - val_lo } else { x5 = x7 - val_hi }; C = !borrow |
| 1982 |
select_le_or_be!("sbc x5, x7, {val_hi}", "sbc x4, x6, {val_lo}"), // if endian == little { x5 = x7 - val_hi - !C } else { x4 = x6 - val_lo - !C } |
| 1983 |
} |
| 1984 |
|
| 1985 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 1986 |
atomic_rmw_ll_sc_3! { |
| 1987 |
_atomic_and_ldxp_stxp as atomic_and (preserves_flags), |
| 1988 |
"and {new_lo}, {prev_lo}, {val_lo}", // new_lo = prev_lo & val_lo |
| 1989 |
"and {new_hi}, {prev_hi}, {val_hi}", // new_hi = prev_hi & val_hi |
| 1990 |
} |
| 1991 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 1992 |
atomic_rmw_cas_3! { |
| 1993 |
_atomic_and_casp as atomic_and, |
| 1994 |
"and x4, x6, {val_lo}", // x4 = x6 & val_lo |
| 1995 |
"and x5, x7, {val_hi}", // x5 = x7 & val_hi |
| 1996 |
} |
| 1997 |
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))] |
| 1998 |
#[inline] |
| 1999 |
unsafe fn atomic_and(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 2000 |
debug_assert!(dst.addr() % 16 == 0); |
| 2001 |
|
| 2002 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 2003 |
// reads, 16-byte aligned, that there are no concurrent non-atomic operations, |
| 2004 |
// and the CPU supports FEAT_LSE128. |
| 2005 |
// |
| 2006 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/LDCLRP--LDCLRPA--LDCLRPAL--LDCLRPL--Atomic-bit-clear-on-quadword-in-memory- |
| 2007 |
unsafe { |
| 2008 |
let val = U128 { whole: !val }; |
| 2009 |
let (prev_lo, prev_hi); |
| 2010 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 2011 |
macro_rules! clear { |
| 2012 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 2013 |
asm!( |
| 2014 |
start_lse128!(), |
| 2015 |
concat!("ldclrp", $acquire, $release, " {val_lo}, {val_hi}, [{dst}]"), // atomic { _x = *dst; *dst = _x & !val; val = _x } |
| 2016 |
dst = in(reg) ptr_reg!(dst), |
| 2017 |
val_lo = inout(reg) val.pair.lo => prev_lo, |
| 2018 |
val_hi = inout(reg) val.pair.hi => prev_hi, |
| 2019 |
options(nostack, preserves_flags), |
| 2020 |
) |
| 2021 |
}; |
| 2022 |
} |
| 2023 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 2024 |
atomic_rmw!(clear, order); |
| 2025 |
// LLVM supports FEAT_LSE128 instructions on LLVM 16+, so use .inst directive on old LLVM. |
| 2026 |
// https://github.com/llvm/llvm-project/commit/7fea6f2e0e606e5339c3359568f680eaf64aa306 |
| 2027 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 2028 |
macro_rules! clear { |
| 2029 |
($order:tt, $_msvc_fence:tt) => { |
| 2030 |
asm!( |
| 2031 |
// ldclrp{,a,l,al} x8, x1, [x0] // atomic { _x = *x0; *x0 = _x & !x8:x1; x8:x1 = _x } |
| 2032 |
concat!(".inst 0x19", $order, "11008"), |
| 2033 |
in("x0") ptr_reg!(dst), |
| 2034 |
inout("x8") val.pair.lo => prev_lo, |
| 2035 |
inout("x1") val.pair.hi => prev_hi, |
| 2036 |
options(nostack, preserves_flags), |
| 2037 |
) |
| 2038 |
}; |
| 2039 |
} |
| 2040 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 2041 |
atomic_rmw_inst!(clear, order); |
| 2042 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 2043 |
} |
| 2044 |
} |
| 2045 |
|
| 2046 |
atomic_rmw_ll_sc_3! { |
| 2047 |
_atomic_nand_ldxp_stxp as atomic_nand (preserves_flags), |
| 2048 |
"and {new_lo}, {prev_lo}, {val_lo}", // new_lo = prev_lo & val_lo |
| 2049 |
"and {new_hi}, {prev_hi}, {val_hi}", // new_hi = prev_hi & val_hi |
| 2050 |
"mvn {new_lo}, {new_lo}", // new_lo = !new_lo |
| 2051 |
"mvn {new_hi}, {new_hi}", // new_hi = !new_hi |
| 2052 |
} |
| 2053 |
atomic_rmw_cas_3! { |
| 2054 |
_atomic_nand_casp as atomic_nand, |
| 2055 |
"and x4, x6, {val_lo}", // x4 = x6 & val_lo |
| 2056 |
"and x5, x7, {val_hi}", // x5 = x7 & val_hi |
| 2057 |
"mvn x4, x4", // x4 = !x4 |
| 2058 |
"mvn x5, x5", // x5 = !x5 |
| 2059 |
} |
| 2060 |
|
| 2061 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 2062 |
atomic_rmw_ll_sc_3! { |
| 2063 |
_atomic_or_ldxp_stxp as atomic_or (preserves_flags), |
| 2064 |
"orr {new_lo}, {prev_lo}, {val_lo}", // new_lo = prev_lo | val_lo |
| 2065 |
"orr {new_hi}, {prev_hi}, {val_hi}", // new_hi = prev_hi | val_hi |
| 2066 |
} |
| 2067 |
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))] |
| 2068 |
atomic_rmw_cas_3! { |
| 2069 |
_atomic_or_casp as atomic_or, |
| 2070 |
"orr x4, x6, {val_lo}", // x4 = x6 | val_lo |
| 2071 |
"orr x5, x7, {val_hi}", // x5 = x7 | val_hi |
| 2072 |
} |
| 2073 |
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))] |
| 2074 |
#[inline] |
| 2075 |
unsafe fn atomic_or(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 2076 |
debug_assert!(dst.addr() % 16 == 0); |
| 2077 |
|
| 2078 |
// SAFETY: the caller must guarantee that `dst` is valid for both writes and |
| 2079 |
// reads, 16-byte aligned, that there are no concurrent non-atomic operations, |
| 2080 |
// and the CPU supports FEAT_LSE128. |
| 2081 |
// |
| 2082 |
// Refs: https://developer.arm.com/documentation/ddi0602/2025-06/Base-Instructions/LDSETP--LDSETPA--LDSETPAL--LDSETPL--Atomic-bit-set-on-quadword-in-memory- |
| 2083 |
unsafe { |
| 2084 |
let val = U128 { whole: val }; |
| 2085 |
let (prev_lo, prev_hi); |
| 2086 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 2087 |
macro_rules! or { |
| 2088 |
($acquire:tt, $release:tt, $_msvc_fence:tt) => { |
| 2089 |
asm!( |
| 2090 |
start_lse128!(), |
| 2091 |
concat!("ldsetp", $acquire, $release, " {val_lo}, {val_hi}, [{dst}]"), // atomic { _x = *dst; *dst = _x | val; val = _x } |
| 2092 |
dst = in(reg) ptr_reg!(dst), |
| 2093 |
val_lo = inout(reg) val.pair.lo => prev_lo, |
| 2094 |
val_hi = inout(reg) val.pair.hi => prev_hi, |
| 2095 |
options(nostack, preserves_flags), |
| 2096 |
) |
| 2097 |
}; |
| 2098 |
} |
| 2099 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 2100 |
atomic_rmw!(or, order); |
| 2101 |
// LLVM supports FEAT_LSE128 instructions on LLVM 16+, so use .inst directive on old LLVM. |
| 2102 |
// https://github.com/llvm/llvm-project/commit/7fea6f2e0e606e5339c3359568f680eaf64aa306 |
| 2103 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 2104 |
macro_rules! or { |
| 2105 |
($order:tt, $_msvc_fence:tt) => { |
| 2106 |
asm!( |
| 2107 |
// ldsetp{,a,l,al} x2, x1, [x0] // atomic { _x = *x0; *x0 = _x | x2:x1; x2:x1 = _x } |
| 2108 |
concat!(".inst 0x19", $order, "13002"), |
| 2109 |
in("x0") ptr_reg!(dst), |
| 2110 |
inout("x2") val.pair.lo => prev_lo, |
| 2111 |
inout("x1") val.pair.hi => prev_hi, |
| 2112 |
options(nostack, preserves_flags), |
| 2113 |
) |
| 2114 |
}; |
| 2115 |
} |
| 2116 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 2117 |
atomic_rmw_inst!(or, order); |
| 2118 |
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole |
| 2119 |
} |
| 2120 |
} |
| 2121 |
|
| 2122 |
atomic_rmw_ll_sc_3! { |
| 2123 |
_atomic_xor_ldxp_stxp as atomic_xor (preserves_flags), |
| 2124 |
"eor {new_lo}, {prev_lo}, {val_lo}", // new_lo = prev_lo ^ val_lo |
| 2125 |
"eor {new_hi}, {prev_hi}, {val_hi}", // new_hi = prev_hi ^ val_hi |
| 2126 |
} |
| 2127 |
atomic_rmw_cas_3! { |
| 2128 |
_atomic_xor_casp as atomic_xor, |
| 2129 |
"eor x4, x6, {val_lo}", // x4 = x6 ^ val_lo |
| 2130 |
"eor x5, x7, {val_hi}", // x5 = x7 ^ val_hi |
| 2131 |
} |
| 2132 |
|
| 2133 |
atomic_rmw_ll_sc_2! { |
| 2134 |
_atomic_not_ldxp_stxp as atomic_not (preserves_flags), |
| 2135 |
"mvn {new_lo}, {prev_lo}", // new_lo = !prev_lo |
| 2136 |
"mvn {new_hi}, {prev_hi}", // new_hi = !prev_hi |
| 2137 |
} |
| 2138 |
atomic_rmw_cas_2! { |
| 2139 |
_atomic_not_casp as atomic_not, |
| 2140 |
"mvn x4, x6", // x4 = !x6 |
| 2141 |
"mvn x5, x7", // x5 = !x7 |
| 2142 |
} |
| 2143 |
|
| 2144 |
// Do not use `preserves_flags` because NEGS modifies the condition flags. |
| 2145 |
atomic_rmw_ll_sc_2! { |
| 2146 |
_atomic_neg_ldxp_stxp as atomic_neg, |
| 2147 |
select_le_or_be!("negs {new_lo}, {prev_lo}", "negs {new_hi}, {prev_hi}"), // if endian == little { new_lo = 0 - prev_lo } else { new_hi = 0 - prev_hi }; C = !borrow |
| 2148 |
select_le_or_be!("ngc {new_hi}, {prev_hi}", "ngc {new_lo}, {prev_lo}"), // if endian == little { new_hi = 0 - prev_hi - !C } else { new_lo = 0 - prev_lo - !C } |
| 2149 |
} |
| 2150 |
atomic_rmw_cas_2! { |
| 2151 |
_atomic_neg_casp as atomic_neg, |
| 2152 |
select_le_or_be!("negs x4, x6", "negs x5, x7"), // if endian == little { x4 = 0 - x6 } else { x5 = 0 - x7 }; C = !borrow |
| 2153 |
select_le_or_be!("ngc x5, x7", "ngc x4, x6"), // if endian == little { x5 = 0 - x7 - !C } else { x4 = 0 - x6 - !C } |
| 2154 |
} |
| 2155 |
|
| 2156 |
// Do not use `preserves_flags` because CMP and SBCS modify the condition flags. |
| 2157 |
atomic_rmw_ll_sc_3! { |
| 2158 |
_atomic_max_ldxp_stxp as atomic_max, |
| 2159 |
select_le_or_be!("cmp {val_lo}, {prev_lo}", "cmp {val_hi}, {prev_hi}"), // if endian == little { C = !(val_lo < prev_lo) } else { C = !(val_hi < prev_hi) } |
| 2160 |
select_le_or_be!("sbcs xzr, {val_hi}, {prev_hi}", "sbcs xzr, {val_lo}, {prev_lo}"), // if endian == little { (_, NZCV) = val_hi - prev_hi - !C } else { (_, NZCV) = val_lo - prev_lo - !C } |
| 2161 |
"csel {new_hi}, {prev_hi}, {val_hi}, lt", // if N != V { new_hi = prev_hi } else { new_hi = val_hi } |
| 2162 |
"csel {new_lo}, {prev_lo}, {val_lo}, lt", // if N != V { new_lo = prev_lo } else { new_lo = val_lo } |
| 2163 |
} |
| 2164 |
atomic_rmw_cas_3! { |
| 2165 |
_atomic_max_casp as atomic_max, |
| 2166 |
select_le_or_be!("cmp {val_lo}, x6", "cmp {val_hi}, x7"), // if endian == little { C = !(val_lo < x6) } else { C = !(val_hi < x7) } |
| 2167 |
select_le_or_be!("sbcs xzr, {val_hi}, x7", "sbcs xzr, {val_lo}, x6"), // if endian == little { (_, NZCV) = val_hi - x7 - !C } else { (_, NZCV) = val_lo - x6 - !C } |
| 2168 |
"csel x5, x7, {val_hi}, lt", // if N != V { x5 = x7 } else { x5 = val_hi } |
| 2169 |
"csel x4, x6, {val_lo}, lt", // if N != V { x4 = x6 } else { x4 = val_lo } |
| 2170 |
} |
| 2171 |
|
| 2172 |
// Do not use `preserves_flags` because CMP and SBCS modify the condition flags. |
| 2173 |
atomic_rmw_ll_sc_3! { |
| 2174 |
_atomic_umax_ldxp_stxp as atomic_umax, |
| 2175 |
select_le_or_be!("cmp {val_lo}, {prev_lo}", "cmp {val_hi}, {prev_hi}"), // if endian == little { C = !(val_lo < prev_lo) } else { C = !(val_hi < prev_hi) } |
| 2176 |
select_le_or_be!("sbcs xzr, {val_hi}, {prev_hi}", "sbcs xzr, {val_lo}, {prev_lo}"), // if endian == little { (_, NZCV) = val_hi - prev_hi - !C } else { (_, NZCV) = val_lo - prev_lo - !C } |
| 2177 |
"csel {new_hi}, {prev_hi}, {val_hi}, lo", // if C == 0 { new_hi = prev_hi } else { new_hi = val_hi } |
| 2178 |
"csel {new_lo}, {prev_lo}, {val_lo}, lo", // if C == 0 { new_hi = prev_hi } else { new_hi = val_hi } |
| 2179 |
} |
| 2180 |
atomic_rmw_cas_3! { |
| 2181 |
_atomic_umax_casp as atomic_umax, |
| 2182 |
select_le_or_be!("cmp {val_lo}, x6", "cmp {val_hi}, x7"), // if endian == little { C = !(val_lo < x6) } else { C = !(val_hi < x7) } |
| 2183 |
select_le_or_be!("sbcs xzr, {val_hi}, x7", "sbcs xzr, {val_lo}, x6"), // if endian == little { (_, NZCV) = val_hi - x7 - !C } else { (_, NZCV) = val_lo - x6 - !C } |
| 2184 |
"csel x5, x7, {val_hi}, lo", // if C == 0 { x5 = x7 } else { x5 = val_hi } |
| 2185 |
"csel x4, x6, {val_lo}, lo", // if C == 0 { x4 = x6 } else { x4 = val_lo } |
| 2186 |
} |
| 2187 |
|
| 2188 |
// Do not use `preserves_flags` because CMP and SBCS modify the condition flags. |
| 2189 |
atomic_rmw_ll_sc_3! { |
| 2190 |
_atomic_min_ldxp_stxp as atomic_min, |
| 2191 |
select_le_or_be!("cmp {val_lo}, {prev_lo}", "cmp {val_hi}, {prev_hi}"), // if endian == little { C = !(val_lo < prev_lo) } else { C = !(val_hi < prev_hi) } |
| 2192 |
select_le_or_be!("sbcs xzr, {val_hi}, {prev_hi}", "sbcs xzr, {val_lo}, {prev_lo}"), // if endian == little { (_, NZCV) = val_hi - prev_hi - !C } else { (_, NZCV) = val_lo - prev_lo - !C } |
| 2193 |
"csel {new_hi}, {prev_hi}, {val_hi}, ge", // if N == V { new_hi = prev_hi } else { new_hi = val_hi } |
| 2194 |
"csel {new_lo}, {prev_lo}, {val_lo}, ge", // if N == V { new_lo = prev_lo } else { new_lo = val_lo } |
| 2195 |
} |
| 2196 |
atomic_rmw_cas_3! { |
| 2197 |
_atomic_min_casp as atomic_min, |
| 2198 |
select_le_or_be!("cmp {val_lo}, x6", "cmp {val_hi}, x7"), // if endian == little { C = !(val_lo < x6) } else { C = !(val_hi < x7) } |
| 2199 |
select_le_or_be!("sbcs xzr, {val_hi}, x7", "sbcs xzr, {val_lo}, x6"), // if endian == little { (_, NZCV) = val_hi - x7 - !C } else { (_, NZCV) = val_lo - x6 - !C } |
| 2200 |
"csel x5, x7, {val_hi}, ge", // if N == V { x5 = x7 } else { x5 = val_hi } |
| 2201 |
"csel x4, x6, {val_lo}, ge", // if N == V { x4 = x6 } else { x4 = val_lo } |
| 2202 |
} |
| 2203 |
|
| 2204 |
// Do not use `preserves_flags` because CMP and SBCS modify the condition flags. |
| 2205 |
atomic_rmw_ll_sc_3! { |
| 2206 |
_atomic_umin_ldxp_stxp as atomic_umin, |
| 2207 |
select_le_or_be!("cmp {val_lo}, {prev_lo}", "cmp {val_hi}, {prev_hi}"), // if endian == little { C = !(val_lo < prev_lo) } else { C = !(val_hi < prev_hi) } |
| 2208 |
select_le_or_be!("sbcs xzr, {val_hi}, {prev_hi}", "sbcs xzr, {val_lo}, {prev_lo}"), // if endian == little { (_, NZCV) = val_hi - prev_hi - !C } else { (_, NZCV) = val_lo - prev_lo - !C } |
| 2209 |
"csel {new_hi}, {prev_hi}, {val_hi}, hs", // if C == 1 { new_hi = prev_hi } else { new_hi = val_hi } |
| 2210 |
"csel {new_lo}, {prev_lo}, {val_lo}, hs", // if C == 1 { new_lo = prev_lo } else { new_lo = val_lo } |
| 2211 |
} |
| 2212 |
atomic_rmw_cas_3! { |
| 2213 |
_atomic_umin_casp as atomic_umin, |
| 2214 |
select_le_or_be!("cmp {val_lo}, x6", "cmp {val_hi}, x7"), // if endian == little { C = !(val_lo < x6) } else { C = !(val_hi < x7) } |
| 2215 |
select_le_or_be!("sbcs xzr, {val_hi}, x7", "sbcs xzr, {val_lo}, x6"), // if endian == little { (_, NZCV) = val_hi - x7 - !C } else { (_, NZCV) = val_lo - x6 - !C } |
| 2216 |
"csel x5, x7, {val_hi}, hs", // if C == 1 { x5 = x7 } else { x5 = val_hi } |
| 2217 |
"csel x4, x6, {val_lo}, hs", // if C == 1 { x4 = x6 } else { x4 = val_lo } |
| 2218 |
} |
| 2219 |
|
| 2220 |
#[inline] |
| 2221 |
const fn is_lock_free() -> bool { |
| 2222 |
IS_ALWAYS_LOCK_FREE |
| 2223 |
} |
| 2224 |
const IS_ALWAYS_LOCK_FREE: bool = true; |
| 2225 |
|
| 2226 |
atomic128!(AtomicI128, i128, atomic_max, atomic_min); |
| 2227 |
atomic128!(AtomicU128, u128, atomic_umax, atomic_umin); |
| 2228 |
|
| 2229 |
#[cfg(test)] |
| 2230 |
mod tests { |
| 2231 |
use super::*; |
| 2232 |
|
| 2233 |
test_atomic_int!(i128); |
| 2234 |
test_atomic_int!(u128); |
| 2235 |
|
| 2236 |
// load/store/swap implementation is not affected by signedness, so it is |
| 2237 |
// enough to test only unsigned types. |
| 2238 |
stress_test!(u128); |
| 2239 |
} |