Debian Codegraph @

debian/rust-vendor/portable-atomic/src/imp/atomic128/riscv64.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 /*
4 128-bit atomic implementation on riscv64.
5
6 This architecture provides the following 128-bit atomic instructions:
7
8 - amocas.q: CAS (Zacas extension)
9
10 See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and
11 detailed description of the atomic and synchronize instructions in this architecture:
12 https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#risc-v
13
14 Note: On Miri and ThreadSanitizer which do not support inline assembly, we don't use
15 this module and use fallback implementation instead.
16
17 Refs:
18 - RISC-V Instruction Set Manual
19 "Zacas" Extension for Atomic Compare-and-Swap (CAS) Instructions
20 https://docs.riscv.org/reference/isa/v20260120/unpriv/zacas.html
21 - RISC-V Atomics ABI Specification
22 https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/draft-20250812-301374e92976e298e676e7129a6212926b2299ce/riscv-atomic.adoc
23
24 See tests/asm-test/asm/portable-atomic for generated assembly.
25 */
26
27 include!("macros.rs");
28
29 #[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))]
30 #[path = "../fallback/outline_atomics.rs"]
31 mod fallback;
32
33 #[cfg(not(portable_atomic_no_outline_atomics))]
34 #[cfg(any(test, not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))))]
35 #[cfg(any(target_os = "linux", target_os = "android"))]
36 #[path = "../detect/riscv_linux.rs"]
37 mod detect;
38
39 #[cfg(not(portable_atomic_no_asm))]
40 use core::arch::asm;
41 use core::sync::atomic::Ordering;
42
43 #[cfg(portable_atomic_no_strict_provenance)]
44 use crate::utils::ptr::PtrExt as _;
45 use crate::utils::{Pair, U128};
46
47 macro_rules! debug_assert_zacas {
48 () => {
49 #[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))]
50 {
51 debug_assert!(detect::detect().zacas());
52 }
53 };
54 }
55
56 // `.option arch, +zacas` directive requires LLVM 20, so we use .4byte directive for old LLVM.
57 // Note that `.insn <value>` directive requires LLVM 19.
58 // https://github.com/llvm/llvm-project/commit/2a086dce691e3cc34a2fc27f4fb255bb2cbbfac9
59 // https://github.com/riscv-non-isa/riscv-asm-manual/blob/v0.0.1/src/asm-manual.adoc#arch
60 #[cfg(not(portable_atomic_pre_llvm_20))]
61 macro_rules! start_zacas {
62 () => {
63 ".option push\n.option arch, +zacas"
64 };
65 }
66 #[cfg(not(portable_atomic_pre_llvm_20))]
67 macro_rules! end_zacas {
68 () => {
69 ".option pop"
70 };
71 }
72
73 #[cfg(not(portable_atomic_pre_llvm_20))]
74 macro_rules! atomic_rmw_amocas_order {
75 ($op:ident, $order:ident) => {
76 atomic_rmw_amocas_order!($op, $order, failure = $order)
77 };
78 ($op:ident, $order:ident, failure = $failure:ident) => {
79 match $order {
80 Ordering::Relaxed => $op!("", ""),
81 Ordering::Acquire => $op!("", ".aq"),
82 Ordering::Release => $op!("", ".rl"),
83 Ordering::AcqRel => $op!("", ".aqrl"),
84 Ordering::SeqCst if $failure == Ordering::SeqCst => $op!("fence rw,rw", ".aqrl"),
85 Ordering::SeqCst => $op!("", ".aqrl"),
86 _ => unreachable!(),
87 }
88 };
89 }
90 #[cfg(portable_atomic_pre_llvm_20)]
91 macro_rules! atomic_rmw_amocas_order_insn {
92 ($op:ident, $order:ident) => {
93 atomic_rmw_amocas_order_insn!($op, $order, failure = $order)
94 };
95 ($op:ident, $order:ident, failure = $failure:ident) => {
96 match $order {
97 Ordering::Relaxed => $op!("", "8"),
98 Ordering::Acquire => $op!("", "c"),
99 Ordering::Release => $op!("", "a"),
100 Ordering::AcqRel => $op!("", "e"),
101 Ordering::SeqCst if $failure == Ordering::SeqCst => $op!("fence rw,rw", "e"),
102 Ordering::SeqCst => $op!("", "e"),
103 _ => unreachable!(),
104 }
105 };
106 }
107
108 // If zacas is available at compile-time, we can always use zacas_fn.
109 #[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))]
110 use self::atomic_load_zacas as atomic_load;
111 // Otherwise, we need to do run-time detection and can use zacas_fn only if zacas is available.
112 #[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))]
113 #[inline]
114 unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {
115 fn_alias! {
116 // inline(never) is just a hint and also not strictly necessary
117 // because we use ifunc helper macro, but used for clarity.
118 #[inline(never)]
119 unsafe fn(src: *mut u128) -> u128;
120 atomic_load_zacas_relaxed = atomic_load_zacas(Ordering::Relaxed);
121 atomic_load_zacas_acquire = atomic_load_zacas(Ordering::Acquire);
122 atomic_load_zacas_seqcst = atomic_load_zacas(Ordering::SeqCst);
123 }
124 // SAFETY: the caller must uphold the safety contract.
125 // we only calls atomic_load_zacas if zacas is available.
126 unsafe {
127 match order {
128 Ordering::Relaxed => {
129 ifunc!(unsafe fn(src: *mut u128) -> u128 {
130 if detect::detect().zacas() {
131 atomic_load_zacas_relaxed
132 } else {
133 fallback::atomic_load_non_seqcst
134 }
135 })
136 }
137 Ordering::Acquire => {
138 ifunc!(unsafe fn(src: *mut u128) -> u128 {
139 if detect::detect().zacas() {
140 atomic_load_zacas_acquire
141 } else {
142 fallback::atomic_load_non_seqcst
143 }
144 })
145 }
146 Ordering::SeqCst => {
147 ifunc!(unsafe fn(src: *mut u128) -> u128 {
148 if detect::detect().zacas() {
149 atomic_load_zacas_seqcst
150 } else {
151 fallback::atomic_load_seqcst
152 }
153 })
154 }
155 _ => unreachable!(),
156 }
157 }
158 }
159 #[inline]
160 unsafe fn atomic_load_zacas(src: *mut u128, order: Ordering) -> u128 {
161 debug_assert!(src.addr() % 16 == 0);
162 debug_assert_zacas!();
163 let (out_lo, out_hi);
164
165 // SAFETY: the caller must uphold the safety contract.
166 unsafe {
167 #[cfg(not(portable_atomic_pre_llvm_20))]
168 macro_rules! load {
169 ($fence:tt, $asm_order:tt) => {
170 asm!(
171 start_zacas!(),
172 $fence, // fence
173 concat!("amocas.q", $asm_order, " a2, a2, 0({src})"), // atomic { if *dst == a2:a3 { *dst = a2:a3 } else { a2:a3 = *dst } }
174 end_zacas!(),
175 src = in(reg) ptr_reg!(src),
176 inout("a2") 0_u64 => out_lo,
177 inout("a3") 0_u64 => out_hi,
178 options(nostack, preserves_flags),
179 )
180 };
181 }
182 #[cfg(not(portable_atomic_pre_llvm_20))]
183 atomic_rmw_amocas_order!(load, order);
184 #[cfg(portable_atomic_pre_llvm_20)]
185 macro_rules! load {
186 ($fence:tt, $insn_order:tt) => {
187 asm!(
188 $fence, // fence
189 // amocas.q{,.aq,.rl,.aqrl} a2, a2, (a0) // atomic { if *a0 == a2:a3 { *a0 = a2:a3 } else { a2:a3 = *a0 } }
190 concat!(".4byte 0x2", $insn_order, "c5462f"),
191 in("a0") ptr_reg!(src),
192 inout("a2") 0_u64 => out_lo,
193 inout("a3") 0_u64 => out_hi,
194 options(nostack, preserves_flags),
195 )
196 };
197 }
198 #[cfg(portable_atomic_pre_llvm_20)]
199 atomic_rmw_amocas_order_insn!(load, order);
200 U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole
201 }
202 }
203
204 #[inline]
205 unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
206 // SAFETY: the caller must uphold the safety contract.
207 unsafe {
208 atomic_swap(dst, val, order);
209 }
210 }
211
212 #[inline]
213 unsafe fn atomic_compare_exchange(
214 dst: *mut u128,
215 old: u128,
216 new: u128,
217 success: Ordering,
218 failure: Ordering,
219 ) -> Result<u128, u128> {
220 #[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))]
221 // SAFETY: the caller must uphold the safety contract.
222 // cfg guarantees that zacas instructions are available at compile-time.
223 let (prev, ok) = unsafe { atomic_compare_exchange_zacas(dst, old, new, success, failure) };
224 #[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))]
225 let (prev, ok) = {
226 fn_alias! {
227 // inline(never) is just a hint and also not strictly necessary
228 // because we use ifunc helper macro, but used for clarity.
229 #[inline(never)]
230 unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool);
231 zacas_relaxed_fn = atomic_compare_exchange_zacas(Ordering::Relaxed, Ordering::Relaxed);
232 zacas_acquire_fn = atomic_compare_exchange_zacas(Ordering::Acquire, Ordering::Acquire);
233 zacas_release_fn = atomic_compare_exchange_zacas(Ordering::Release, Ordering::Relaxed);
234 zacas_acqrel_fn = atomic_compare_exchange_zacas(Ordering::AcqRel, Ordering::Acquire);
235 zacas_seqcst_fn = atomic_compare_exchange_zacas(Ordering::SeqCst, Ordering::SeqCst);
236 }
237 let order = crate::utils::upgrade_success_ordering(success, failure);
238 // SAFETY: the caller must uphold the safety contract.
239 // we only calls atomic_compare_exchange_zacas if zacas is available.
240 unsafe {
241 match order {
242 Ordering::Relaxed => {
243 ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) {
244 if detect::detect().zacas() {
245 zacas_relaxed_fn
246 } else {
247 fallback::atomic_compare_exchange_non_seqcst
248 }
249 })
250 }
251 Ordering::Acquire => {
252 ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) {
253 if detect::detect().zacas() {
254 zacas_acquire_fn
255 } else {
256 fallback::atomic_compare_exchange_non_seqcst
257 }
258 })
259 }
260 Ordering::Release => {
261 ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) {
262 if detect::detect().zacas() {
263 zacas_release_fn
264 } else {
265 fallback::atomic_compare_exchange_non_seqcst
266 }
267 })
268 }
269 Ordering::AcqRel => {
270 ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) {
271 if detect::detect().zacas() {
272 zacas_acqrel_fn
273 } else {
274 fallback::atomic_compare_exchange_non_seqcst
275 }
276 })
277 }
278 Ordering::SeqCst => {
279 ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) {
280 if detect::detect().zacas() {
281 zacas_seqcst_fn
282 } else {
283 fallback::atomic_compare_exchange_seqcst
284 }
285 })
286 }
287 _ => unreachable!(),
288 }
289 }
290 };
291 if ok { Ok(prev) } else { Err(prev) }
292 }
293 #[inline]
294 unsafe fn atomic_compare_exchange_zacas(
295 dst: *mut u128,
296 old: u128,
297 new: u128,
298 success: Ordering,
299 failure: Ordering,
300 ) -> (u128, bool) {
301 debug_assert!(dst.addr() % 16 == 0);
302 debug_assert_zacas!();
303 let order = crate::utils::upgrade_success_ordering(success, failure);
304 let old = U128 { whole: old };
305 let new = U128 { whole: new };
306 let (prev_lo, prev_hi);
307
308 // SAFETY: the caller must uphold the safety contract.
309 unsafe {
310 #[cfg(not(portable_atomic_pre_llvm_20))]
311 macro_rules! cmpxchg {
312 ($fence:tt, $asm_order:tt) => {
313 asm!(
314 start_zacas!(),
315 $fence, // fence
316 concat!("amocas.q", $asm_order, " a4, a2, 0({dst})"), // atomic { if *dst == a4:a5 { *dst = a2:a3 } else { a4:a5 = *dst } }
317 end_zacas!(),
318 dst = in(reg) ptr_reg!(dst),
319 // must be allocated to even/odd register pair
320 inout("a4") old.pair.lo => prev_lo,
321 inout("a5") old.pair.hi => prev_hi,
322 // must be allocated to even/odd register pair
323 in("a2") new.pair.lo,
324 in("a3") new.pair.hi,
325 options(nostack, preserves_flags),
326 )
327 };
328 }
329 #[cfg(not(portable_atomic_pre_llvm_20))]
330 atomic_rmw_amocas_order!(cmpxchg, order, failure = failure);
331 #[cfg(portable_atomic_pre_llvm_20)]
332 macro_rules! cmpxchg {
333 ($fence:tt, $insn_order:tt) => {
334 asm!(
335 $fence, // fence
336 // amocas.q{,.aq,.rl,.aqrl} a4, a2, (a0) // atomic { if *a0 == a4:a5 { *a0 = a2:a3 } else { a4:a5 = *a0 } }
337 concat!(".4byte 0x2", $insn_order, "c5472f"),
338 in("a0") ptr_reg!(dst),
339 // must be allocated to even/odd register pair
340 inout("a4") old.pair.lo => prev_lo,
341 inout("a5") old.pair.hi => prev_hi,
342 // must be allocated to even/odd register pair
343 in("a2") new.pair.lo,
344 in("a3") new.pair.hi,
345 options(nostack, preserves_flags),
346 )
347 };
348 }
349 #[cfg(portable_atomic_pre_llvm_20)]
350 atomic_rmw_amocas_order_insn!(cmpxchg, order, failure = failure);
351 let prev = U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole;
352 (prev, prev == old.whole)
353 }
354 }
355
356 // amocas is always strong.
357 use self::atomic_compare_exchange as atomic_compare_exchange_weak;
358
359 // 128-bit atomic load by two 64-bit atomic loads.
360 #[inline]
361 unsafe fn byte_wise_atomic_load(src: *const u128) -> u128 {
362 let (out_lo, out_hi);
363
364 // SAFETY: the caller must uphold the safety contract.
365 unsafe {
366 asm!(
367 "ld {out_lo}, ({src})", // atomic { out_lo = *src }
368 "ld {out_hi}, 8({src})", // atomic { out_hi = *src.byte_add(8) }
369 src = in(reg) ptr_reg!(src),
370 out_lo = out(reg) out_lo,
371 out_hi = out(reg) out_hi,
372 options(pure, nostack, preserves_flags, readonly),
373 );
374 U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole
375 }
376 }
377
378 macro_rules! select_atomic_rmw {
379 (
380 unsafe fn $name:ident(dst: *mut u128 $(, $($arg:tt)*)?) $(-> $ret_ty:ty)? {
381 |$zacas_fn_binding:ident| $($zacas_fn_body:tt)*
382 }
383 zacas = $zacas_fn:ident;
384 non_seqcst_fallback = $non_seqcst_fallback_fn:ident;
385 seqcst_fallback = $seqcst_fallback_fn:ident;
386 ) => {
387 #[inline]
388 unsafe fn $zacas_fn(dst: *mut u128 $(, $($arg)*)?, order: Ordering) $(-> $ret_ty)? {
389 // SAFETY: the caller must uphold the safety contract.
390 unsafe {
391 // This is not single-copy atomic reads, but this is ok because subsequent
392 // CAS will check for consistency.
393 //
394 // Note that the C++20 memory model does not allow mixed-sized atomic access,
395 // so we must use inline assembly to implement byte_wise_atomic_load.
396 // (i.e., byte-wise atomic based on the standard library's atomic types
397 // cannot be used here).
398 let mut prev = byte_wise_atomic_load(dst);
399 loop {
400 let next = {
401 let $zacas_fn_binding = prev;
402 $($zacas_fn_body)*
403 };
404 match atomic_compare_exchange_weak(dst, prev, next, order, Ordering::Relaxed) {
405 Ok(x) => return x,
406 Err(x) => prev = x,
407 }
408 }
409 }
410 }
411 // If zacas is available at compile-time, we can always use zacas_fn.
412 #[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))]
413 use self::$zacas_fn as $name;
414 // Otherwise, we need to do run-time detection and can use zacas_fn only if zacas is available.
415 #[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))]
416 #[inline]
417 unsafe fn $name(dst: *mut u128 $(, $($arg)*)?, order: Ordering) $(-> $ret_ty)? {
418 fn_alias! {
419 // inline(never) is just a hint and also not strictly necessary
420 // because we use ifunc helper macro, but used for clarity.
421 #[inline(never)]
422 unsafe fn(dst: *mut u128 $(, $($arg)*)?) $(-> $ret_ty)?;
423 zacas_relaxed_fn = $zacas_fn(Ordering::Relaxed);
424 zacas_acquire_fn = $zacas_fn(Ordering::Acquire);
425 zacas_release_fn = $zacas_fn(Ordering::Release);
426 zacas_acqrel_fn = $zacas_fn(Ordering::AcqRel);
427 zacas_seqcst_fn = $zacas_fn(Ordering::SeqCst);
428 }
429 // SAFETY: the caller must uphold the safety contract.
430 // we only calls zacas_fn if zacas is available.
431 unsafe {
432 match order {
433 Ordering::Relaxed => {
434 ifunc!(unsafe fn(dst: *mut u128 $(, $($arg)*)?) $(-> $ret_ty)? {
435 if detect::detect().zacas() {
436 zacas_relaxed_fn
437 } else {
438 fallback::$non_seqcst_fallback_fn
439 }
440 })
441 }
442 Ordering::Acquire => {
443 ifunc!(unsafe fn(dst: *mut u128 $(, $($arg)*)?) $(-> $ret_ty)? {
444 if detect::detect().zacas() {
445 zacas_acquire_fn
446 } else {
447 fallback::$non_seqcst_fallback_fn
448 }
449 })
450 }
451 Ordering::Release => {
452 ifunc!(unsafe fn(dst: *mut u128 $(, $($arg)*)?) $(-> $ret_ty)? {
453 if detect::detect().zacas() {
454 zacas_release_fn
455 } else {
456 fallback::$non_seqcst_fallback_fn
457 }
458 })
459 }
460 Ordering::AcqRel => {
461 ifunc!(unsafe fn(dst: *mut u128 $(, $($arg)*)?) $(-> $ret_ty)? {
462 if detect::detect().zacas() {
463 zacas_acqrel_fn
464 } else {
465 fallback::$non_seqcst_fallback_fn
466 }
467 })
468 }
469 Ordering::SeqCst => {
470 ifunc!(unsafe fn(dst: *mut u128 $(, $($arg)*)?) $(-> $ret_ty)? {
471 if detect::detect().zacas() {
472 zacas_seqcst_fn
473 } else {
474 fallback::$seqcst_fallback_fn
475 }
476 })
477 }
478 _ => unreachable!(),
479 }
480 }
481 }
482 };
483 }
484
485 select_atomic_rmw! {
486 unsafe fn atomic_swap(dst: *mut u128, val: u128) -> u128 {
487 |_x| val
488 }
489 zacas = atomic_swap_zacas;
490 non_seqcst_fallback = atomic_swap_non_seqcst;
491 seqcst_fallback = atomic_swap_seqcst;
492 }
493 select_atomic_rmw! {
494 unsafe fn atomic_add(dst: *mut u128, val: u128) -> u128 {
495 |x| x.wrapping_add(val)
496 }
497 zacas = atomic_add_zacas;
498 non_seqcst_fallback = atomic_add_non_seqcst;
499 seqcst_fallback = atomic_add_seqcst;
500 }
501 select_atomic_rmw! {
502 unsafe fn atomic_sub(dst: *mut u128, val: u128) -> u128 {
503 |x| x.wrapping_sub(val)
504 }
505 zacas = atomic_sub_zacas;
506 non_seqcst_fallback = atomic_sub_non_seqcst;
507 seqcst_fallback = atomic_sub_seqcst;
508 }
509 select_atomic_rmw! {
510 unsafe fn atomic_and(dst: *mut u128, val: u128) -> u128 {
511 |x| x & val
512 }
513 zacas = atomic_and_zacas;
514 non_seqcst_fallback = atomic_and_non_seqcst;
515 seqcst_fallback = atomic_and_seqcst;
516 }
517 select_atomic_rmw! {
518 unsafe fn atomic_nand(dst: *mut u128, val: u128) -> u128 {
519 |x| !(x & val)
520 }
521 zacas = atomic_nand_zacas;
522 non_seqcst_fallback = atomic_nand_non_seqcst;
523 seqcst_fallback = atomic_nand_seqcst;
524 }
525 select_atomic_rmw! {
526 unsafe fn atomic_or(dst: *mut u128, val: u128) -> u128 {
527 |x| x | val
528 }
529 zacas = atomic_or_zacas;
530 non_seqcst_fallback = atomic_or_non_seqcst;
531 seqcst_fallback = atomic_or_seqcst;
532 }
533 select_atomic_rmw! {
534 unsafe fn atomic_xor(dst: *mut u128, val: u128) -> u128 {
535 |x| x ^ val
536 }
537 zacas = atomic_xor_zacas;
538 non_seqcst_fallback = atomic_xor_non_seqcst;
539 seqcst_fallback = atomic_xor_seqcst;
540 }
541 select_atomic_rmw! {
542 unsafe fn atomic_max(dst: *mut u128, val: u128) -> u128 {
543 |x| {
544 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
545 { core::cmp::max(x as i128, val as i128) as u128 }
546 }
547 }
548 zacas = atomic_max_zacas;
549 non_seqcst_fallback = atomic_max_non_seqcst;
550 seqcst_fallback = atomic_max_seqcst;
551 }
552 select_atomic_rmw! {
553 unsafe fn atomic_umax(dst: *mut u128, val: u128) -> u128 {
554 |x| core::cmp::max(x, val)
555 }
556 zacas = atomic_umax_zacas;
557 non_seqcst_fallback = atomic_umax_non_seqcst;
558 seqcst_fallback = atomic_umax_seqcst;
559 }
560 select_atomic_rmw! {
561 unsafe fn atomic_min(dst: *mut u128, val: u128) -> u128 {
562 |x| {
563 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
564 { core::cmp::min(x as i128, val as i128) as u128 }
565 }
566 }
567 zacas = atomic_min_zacas;
568 non_seqcst_fallback = atomic_min_non_seqcst;
569 seqcst_fallback = atomic_min_seqcst;
570 }
571 select_atomic_rmw! {
572 unsafe fn atomic_umin(dst: *mut u128, val: u128) -> u128 {
573 |x| core::cmp::min(x, val)
574 }
575 zacas = atomic_umin_zacas;
576 non_seqcst_fallback = atomic_umin_non_seqcst;
577 seqcst_fallback = atomic_umin_seqcst;
578 }
579 select_atomic_rmw! {
580 unsafe fn atomic_not(dst: *mut u128) -> u128 {
581 |x| !x
582 }
583 zacas = atomic_not_zacas;
584 non_seqcst_fallback = atomic_not_non_seqcst;
585 seqcst_fallback = atomic_not_seqcst;
586 }
587 select_atomic_rmw! {
588 unsafe fn atomic_neg(dst: *mut u128) -> u128 {
589 |x| x.wrapping_neg()
590 }
591 zacas = atomic_neg_zacas;
592 non_seqcst_fallback = atomic_neg_non_seqcst;
593 seqcst_fallback = atomic_neg_seqcst;
594 }
595
596 #[inline]
597 fn is_lock_free() -> bool {
598 #[cfg(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"))]
599 {
600 // zacas is available at compile-time.
601 true
602 }
603 #[cfg(not(any(target_feature = "zacas", portable_atomic_target_feature = "zacas")))]
604 {
605 detect::detect().zacas()
606 }
607 }
608 const IS_ALWAYS_LOCK_FREE: bool =
609 cfg!(any(target_feature = "zacas", portable_atomic_target_feature = "zacas"));
610
611 atomic128!(AtomicI128, i128, atomic_max, atomic_min);
612 atomic128!(AtomicU128, u128, atomic_umax, atomic_umin);
613
614 #[cfg(test)]
615 mod tests {
616 use super::*;
617
618 test_atomic_int!(i128);
619 test_atomic_int!(u128);
620
621 // load/store/swap implementation is not affected by signedness, so it is
622 // enough to test only unsigned types.
623 stress_test!(u128);
624 }