Debian Codegraph @

debian/rust-vendor/portable-atomic/src/tests/helper.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 #![allow(unused_macros, clippy::undocumented_unsafe_blocks)]
4
5 use std::{
6 eprintln,
7 sync::atomic::{AtomicIsize, AtomicUsize, Ordering},
8 };
9
10 use crossbeam_utils::thread;
11
12 macro_rules! __test_atomic_common {
13 ($atomic_type:ty, $value_type:ty) => {
14 use std::mem;
15 #[test]
16 fn assert_auto_traits() {
17 fn _assert<T: Send + Sync + Unpin + std::panic::UnwindSafe>() {}
18 _assert::<$atomic_type>();
19 }
20 #[test]
21 fn alignment() {
22 // https://github.com/rust-lang/rust/blob/1.84.0/library/core/tests/atomic.rs#L252
23 assert_eq!(mem::align_of::<$atomic_type>(), mem::size_of::<$atomic_type>());
24 assert_eq!(mem::size_of::<$atomic_type>(), mem::size_of::<$value_type>());
25 }
26 #[test]
27 fn is_lock_free() {
28 const IS_ALWAYS_LOCK_FREE: bool = <$atomic_type>::IS_ALWAYS_LOCK_FREE;
29 assert_eq!(IS_ALWAYS_LOCK_FREE, <$atomic_type>::IS_ALWAYS_LOCK_FREE);
30 let is_lock_free = <$atomic_type>::is_lock_free();
31 if IS_ALWAYS_LOCK_FREE {
32 // If is_always_lock_free is true, then is_lock_free must always be true.
33 assert!(is_lock_free);
34 }
35 }
36 };
37 }
38 macro_rules! __test_atomic_pub_common {
39 ($atomic_type:ty, $value_type:ty) => {
40 #[test]
41 fn is_always_lock_free() {
42 assert_eq!(<$atomic_type>::IS_ALWAYS_LOCK_FREE, <$atomic_type>::is_always_lock_free());
43 }
44 #[test]
45 fn assert_ref_unwind_safe() {
46 #[cfg(not(all(portable_atomic_no_core_unwind_safe, not(feature = "std"))))]
47 static_assertions::assert_impl_all!($atomic_type: std::panic::RefUnwindSafe);
48 #[cfg(all(portable_atomic_no_core_unwind_safe, not(feature = "std")))]
49 static_assertions::assert_not_impl_all!($atomic_type: std::panic::RefUnwindSafe);
50 }
51 };
52 }
53
54 macro_rules! __test_atomic_int_load_store {
55 ($atomic_type:ty, $int_type:ident, single_thread) => {
56 __test_atomic_common!($atomic_type, $int_type);
57 use crate::tests::helper::{self, *};
58 #[test]
59 fn accessor() {
60 let a = <$atomic_type>::new(10);
61 unsafe {
62 assert_eq!(*a.as_ptr(), 10);
63 *a.as_ptr() = 5;
64 assert_eq!(a.as_ptr() as *const (), &a as *const _ as *const ());
65 assert_eq!(*a.as_ptr(), 5);
66 }
67 }
68 // https://bugs.llvm.org/show_bug.cgi?id=37061
69 #[test]
70 fn static_load_only() {
71 static VAR: $atomic_type = <$atomic_type>::new(10);
72 for &order in &helper::LOAD_ORDERINGS {
73 assert_eq!(VAR.load(order), 10);
74 }
75 }
76 #[test]
77 fn load_store() {
78 static VAR: $atomic_type = <$atomic_type>::new(10);
79 test_load_ordering(&|order| {
80 VAR.load(order);
81 });
82 test_store_ordering(&|order| VAR.store(10, order));
83 for (&load_order, &store_order) in
84 helper::LOAD_ORDERINGS.iter().zip(&helper::STORE_ORDERINGS)
85 {
86 assert_eq!(VAR.load(load_order), 10);
87 VAR.store(5, store_order);
88 assert_eq!(VAR.load(load_order), 5);
89 VAR.store(10, store_order);
90 let a = <$atomic_type>::new(1);
91 assert_eq!(a.load(load_order), 1);
92 a.store($int_type::MIN, store_order);
93 assert_eq!(a.load(load_order), $int_type::MIN);
94 a.store($int_type::MAX, store_order);
95 assert_eq!(a.load(load_order), $int_type::MAX);
96 }
97 }
98 };
99 ($atomic_type:ty, $int_type:ident) => {
100 __test_atomic_int_load_store!($atomic_type, $int_type, single_thread);
101
102 use std::{collections::BTreeSet, eprintln, time::Instant, vec, vec::Vec};
103
104 use crossbeam_utils::thread;
105
106 #[test]
107 fn stress_load_store() {
108 let mut rng = fastrand::Rng::new();
109 let (iterations, threads) = stress_test_config(&mut rng);
110 let data1 = (0..iterations).map(|_| rng.$int_type(..)).collect::<Vec<_>>();
111 let set = data1.iter().copied().collect::<BTreeSet<_>>();
112 let a = <$atomic_type>::new(data1[rng.usize(0..iterations)]);
113 let now = &Instant::now();
114 thread::scope(|s| {
115 for _ in 0..threads {
116 s.spawn(|_| {
117 let mut rng = fastrand::Rng::new();
118 let now = *now;
119 for i in 0..iterations {
120 a.store(data1[i], rand_store_ordering(&mut rng));
121 }
122 eprintln!("store end={:?}", now.elapsed());
123 });
124 s.spawn(|_| {
125 let mut rng = fastrand::Rng::new();
126 let now = *now;
127 let mut v = vec![0; iterations];
128 for i in 0..iterations {
129 v[i] = a.load(rand_load_ordering(&mut rng));
130 }
131 eprintln!("load end={:?}", now.elapsed());
132 for v in v {
133 assert!(set.contains(&v), "load unexpected={}", v);
134 }
135 });
136 }
137 })
138 .unwrap();
139 }
140 };
141 }
142 macro_rules! __test_atomic_float_load_store {
143 ($atomic_type:ty, $float_type:ident, single_thread) => {
144 __test_atomic_common!($atomic_type, $float_type);
145 use crate::tests::helper::{self, *};
146 #[test]
147 fn accessor() {
148 let a = <$atomic_type>::new(10.);
149 unsafe {
150 assert_eq!(*a.as_ptr(), 10.);
151 *a.as_ptr() = 5.;
152 assert_eq!(a.as_ptr() as *const (), &a as *const _ as *const ());
153 assert_eq!(*a.as_ptr(), 5.);
154 }
155 }
156 // https://bugs.llvm.org/show_bug.cgi?id=37061
157 #[test]
158 fn static_load_only() {
159 static VAR: $atomic_type = <$atomic_type>::new(10.);
160 for &order in &helper::LOAD_ORDERINGS {
161 assert_eq!(VAR.load(order), 10.);
162 }
163 }
164 #[test]
165 fn load_store() {
166 static VAR: $atomic_type = <$atomic_type>::new(10.);
167 test_load_ordering(&|order| {
168 VAR.load(order);
169 });
170 test_store_ordering(&|order| VAR.store(10., order));
171 for (&load_order, &store_order) in
172 helper::LOAD_ORDERINGS.iter().zip(&helper::STORE_ORDERINGS)
173 {
174 assert_eq!(VAR.load(load_order), 10.);
175 VAR.store(5., store_order);
176 assert_eq!(VAR.load(load_order), 5.);
177 VAR.store(10., store_order);
178 let a = <$atomic_type>::new(1.);
179 assert_eq!(a.load(load_order), 1.);
180 a.store(2., store_order);
181 assert_eq!(a.load(load_order), 2.);
182 }
183 }
184 };
185 ($atomic_type:ty, $float_type:ident) => {
186 __test_atomic_float_load_store!($atomic_type, $float_type, single_thread);
187 // TODO: multi thread
188 };
189 }
190 macro_rules! __test_atomic_bool_load_store {
191 ($atomic_type:ty, single_thread) => {
192 __test_atomic_common!($atomic_type, bool);
193 use crate::tests::helper::{self, *};
194 #[test]
195 fn accessor() {
196 let a = <$atomic_type>::new(false);
197 unsafe {
198 assert_eq!(*a.as_ptr(), false);
199 *a.as_ptr() = true;
200 assert_eq!(a.as_ptr() as *const (), &a as *const _ as *const ());
201 assert_eq!(*a.as_ptr(), true);
202 }
203 }
204 // https://bugs.llvm.org/show_bug.cgi?id=37061
205 #[test]
206 fn static_load_only() {
207 static VAR: $atomic_type = <$atomic_type>::new(false);
208 for &order in &helper::LOAD_ORDERINGS {
209 assert_eq!(VAR.load(order), false);
210 }
211 }
212 #[test]
213 fn load_store() {
214 static VAR: $atomic_type = <$atomic_type>::new(false);
215 test_load_ordering(&|order| {
216 VAR.load(order);
217 });
218 test_store_ordering(&|order| VAR.store(false, order));
219 for (&load_order, &store_order) in
220 helper::LOAD_ORDERINGS.iter().zip(&helper::STORE_ORDERINGS)
221 {
222 assert_eq!(VAR.load(load_order), false);
223 VAR.store(true, store_order);
224 assert_eq!(VAR.load(load_order), true);
225 VAR.store(false, store_order);
226 let a = <$atomic_type>::new(true);
227 assert_eq!(a.load(load_order), true);
228 a.store(false, store_order);
229 assert_eq!(a.load(load_order), false);
230 }
231 }
232 };
233 ($atomic_type:ty) => {
234 __test_atomic_bool_load_store!($atomic_type, single_thread);
235 // TODO: multi thread
236 };
237 }
238 macro_rules! __test_atomic_ptr_load_store {
239 ($atomic_type:ty, single_thread) => {
240 __test_atomic_common!($atomic_type, *mut u8);
241
242 use std::ptr;
243
244 use crate::tests::helper::{self, *};
245
246 #[test]
247 fn accessor() {
248 let mut v = 1;
249 let a = <$atomic_type>::new(ptr::null_mut());
250 unsafe {
251 assert!((*a.as_ptr()).is_null());
252 *a.as_ptr() = &mut v;
253 assert_eq!(a.as_ptr() as *const (), &a as *const _ as *const ());
254 assert!(!(*a.as_ptr()).is_null());
255 }
256 }
257 // https://bugs.llvm.org/show_bug.cgi?id=37061
258 #[test]
259 fn static_load_only() {
260 static VAR: $atomic_type = <$atomic_type>::new(ptr::null_mut());
261 for &order in &helper::LOAD_ORDERINGS {
262 assert_eq!(VAR.load(order), ptr::null_mut());
263 }
264 }
265 #[test]
266 fn load_store() {
267 static VAR: $atomic_type = <$atomic_type>::new(ptr::null_mut());
268 test_load_ordering(&|order| {
269 VAR.load(order);
270 });
271 test_store_ordering(&|order| VAR.store(ptr::null_mut(), order));
272 let mut v = 1_u8;
273 let p = &mut v as *mut u8;
274 for (&load_order, &store_order) in
275 helper::LOAD_ORDERINGS.iter().zip(&helper::STORE_ORDERINGS)
276 {
277 assert_eq!(VAR.load(load_order), ptr::null_mut());
278 VAR.store(p, store_order);
279 assert_eq!(VAR.load(load_order), p);
280 VAR.store(ptr::null_mut(), store_order);
281 let a = <$atomic_type>::new(p);
282 assert_eq!(a.load(load_order), p);
283 a.store(ptr::null_mut(), store_order);
284 assert_eq!(a.load(load_order), ptr::null_mut());
285 }
286 }
287 };
288 ($atomic_type:ty) => {
289 __test_atomic_ptr_load_store!($atomic_type, single_thread);
290 // TODO: multi thread
291 };
292 }
293
294 macro_rules! __test_atomic_int {
295 ($atomic_type:ty, $int_type:ident, single_thread) => {
296 #[test]
297 fn swap() {
298 // TODO(riscv): wrong result (as of Valgrind 3.26)
299 #[cfg(valgrind)]
300 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
301 return;
302 }
303 let a = <$atomic_type>::new(5);
304 test_swap_ordering(&|order| {
305 a.swap(5, order);
306 });
307 for &order in &helper::SWAP_ORDERINGS {
308 assert_eq!(a.swap(10, order), 5);
309 assert_eq!(a.swap($int_type::MIN, order), 10);
310 assert_eq!(a.swap($int_type::MAX, order), $int_type::MIN);
311 assert_eq!(a.swap(5, order), $int_type::MAX);
312 }
313 }
314 #[test]
315 fn compare_exchange() {
316 // TODO(riscv): wrong result (as of Valgrind 3.26)
317 #[cfg(valgrind)]
318 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
319 return;
320 }
321 let a = <$atomic_type>::new(5);
322 test_compare_exchange_ordering(&|success, failure| {
323 let _ = a.compare_exchange(5, 5, success, failure);
324 });
325 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
326 let a = <$atomic_type>::new(5);
327 assert_eq!(a.compare_exchange(5, 10, success, failure), Ok(5));
328 assert_eq!(a.load(Ordering::Relaxed), 10);
329 assert_eq!(a.compare_exchange(6, 12, success, failure), Err(10));
330 assert_eq!(a.load(Ordering::Relaxed), 10);
331 }
332 }
333 #[test]
334 fn compare_exchange_weak() {
335 // TODO(riscv): wrong result (as of Valgrind 3.26)
336 #[cfg(valgrind)]
337 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
338 return;
339 }
340 let a = <$atomic_type>::new(4);
341 #[cfg(valgrind)]
342 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
343 mark_aligned_defined(&a);
344 }
345 test_compare_exchange_ordering(&|success, failure| {
346 let _ = a.compare_exchange_weak(4, 4, success, failure);
347 });
348 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
349 let a = <$atomic_type>::new(4);
350 #[cfg(valgrind)]
351 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
352 mark_aligned_defined(&a);
353 }
354 assert_eq!(a.compare_exchange_weak(6, 8, success, failure), Err(4));
355 let mut old = a.load(Ordering::Relaxed);
356 loop {
357 let new = old * 2;
358 match a.compare_exchange_weak(old, new, success, failure) {
359 Ok(_) => break,
360 Err(x) => old = x,
361 }
362 }
363 assert_eq!(a.load(Ordering::Relaxed), 8);
364 }
365 }
366 #[test]
367 fn fetch_add() {
368 // TODO(riscv): wrong result (as of Valgrind 3.26)
369 #[cfg(valgrind)]
370 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
371 return;
372 }
373 let a = <$atomic_type>::new(0);
374 #[cfg(valgrind)]
375 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
376 mark_aligned_defined(&a);
377 }
378 test_swap_ordering(&|order| {
379 a.fetch_add(0, order);
380 });
381 for &order in &helper::SWAP_ORDERINGS {
382 let a = <$atomic_type>::new(0);
383 #[cfg(valgrind)]
384 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
385 mark_aligned_defined(&a);
386 }
387 assert_eq!(a.fetch_add(10, order), 0);
388 assert_eq!(a.load(Ordering::Relaxed), 10);
389 let a = <$atomic_type>::new($int_type::MAX);
390 #[cfg(valgrind)]
391 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
392 mark_aligned_defined(&a);
393 }
394 assert_eq!(a.fetch_add(1, order), $int_type::MAX);
395 assert_eq!(a.load(Ordering::Relaxed), $int_type::MAX.wrapping_add(1));
396 }
397 }
398 #[test]
399 fn add() {
400 // TODO(riscv): wrong result (as of Valgrind 3.26)
401 #[cfg(valgrind)]
402 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
403 return;
404 }
405 let a = <$atomic_type>::new(0);
406 #[cfg(valgrind)]
407 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
408 mark_aligned_defined(&a);
409 }
410 test_swap_ordering(&|order| a.add(0, order));
411 for &order in &helper::SWAP_ORDERINGS {
412 let a = <$atomic_type>::new(0);
413 #[cfg(valgrind)]
414 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
415 mark_aligned_defined(&a);
416 }
417 a.add(10, order);
418 assert_eq!(a.load(Ordering::Relaxed), 10);
419 let a = <$atomic_type>::new($int_type::MAX);
420 #[cfg(valgrind)]
421 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
422 mark_aligned_defined(&a);
423 }
424 a.add(1, order);
425 assert_eq!(a.load(Ordering::Relaxed), $int_type::MAX.wrapping_add(1));
426 }
427 }
428 #[test]
429 fn fetch_sub() {
430 // TODO(riscv): wrong result (as of Valgrind 3.26)
431 #[cfg(valgrind)]
432 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
433 return;
434 }
435 let a = <$atomic_type>::new(20);
436 #[cfg(valgrind)]
437 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
438 mark_aligned_defined(&a);
439 }
440 test_swap_ordering(&|order| {
441 a.fetch_sub(0, order);
442 });
443 for &order in &helper::SWAP_ORDERINGS {
444 let a = <$atomic_type>::new(20);
445 #[cfg(valgrind)]
446 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
447 mark_aligned_defined(&a);
448 }
449 assert_eq!(a.fetch_sub(10, order), 20);
450 assert_eq!(a.load(Ordering::Relaxed), 10);
451 let a = <$atomic_type>::new($int_type::MIN);
452 #[cfg(valgrind)]
453 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
454 mark_aligned_defined(&a);
455 }
456 assert_eq!(a.fetch_sub(1, order), $int_type::MIN);
457 assert_eq!(a.load(Ordering::Relaxed), $int_type::MIN.wrapping_sub(1));
458 }
459 }
460 #[test]
461 fn sub() {
462 // TODO(riscv): wrong result (as of Valgrind 3.26)
463 #[cfg(valgrind)]
464 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
465 return;
466 }
467 let a = <$atomic_type>::new(20);
468 #[cfg(valgrind)]
469 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
470 mark_aligned_defined(&a);
471 }
472 test_swap_ordering(&|order| a.sub(0, order));
473 for &order in &helper::SWAP_ORDERINGS {
474 let a = <$atomic_type>::new(20);
475 #[cfg(valgrind)]
476 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
477 mark_aligned_defined(&a);
478 }
479 a.sub(10, order);
480 assert_eq!(a.load(Ordering::Relaxed), 10);
481 let a = <$atomic_type>::new($int_type::MIN);
482 #[cfg(valgrind)]
483 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
484 mark_aligned_defined(&a);
485 }
486 a.sub(1, order);
487 assert_eq!(a.load(Ordering::Relaxed), $int_type::MIN.wrapping_sub(1));
488 }
489 }
490 #[test]
491 fn fetch_and() {
492 let a = <$atomic_type>::new(0b101101);
493 test_swap_ordering(&|order| {
494 a.fetch_and(0b101101, order);
495 });
496 for &order in &helper::SWAP_ORDERINGS {
497 let a = <$atomic_type>::new(0b101101);
498 assert_eq!(a.fetch_and(0b110011, order), 0b101101);
499 assert_eq!(a.load(Ordering::Relaxed), 0b100001);
500 }
501 }
502 #[test]
503 fn and() {
504 let a = <$atomic_type>::new(0b101101);
505 test_swap_ordering(&|order| a.and(0b101101, order));
506 for &order in &helper::SWAP_ORDERINGS {
507 let a = <$atomic_type>::new(0b101101);
508 a.and(0b110011, order);
509 assert_eq!(a.load(Ordering::Relaxed), 0b100001);
510 }
511 }
512 #[test]
513 fn fetch_nand() {
514 // TODO(riscv): wrong result (as of Valgrind 3.26)
515 #[cfg(valgrind)]
516 if cfg!(target_arch = "riscv64") {
517 return;
518 }
519 let a = <$atomic_type>::new(0x13);
520 test_swap_ordering(&|order| {
521 a.fetch_nand(0x31, order);
522 });
523 for &order in &helper::SWAP_ORDERINGS {
524 let a = <$atomic_type>::new(0x13);
525 assert_eq!(a.fetch_nand(0x31, order), 0x13);
526 assert_eq!(a.load(Ordering::Relaxed), !(0x13 & 0x31));
527 }
528 }
529 #[test]
530 fn fetch_or() {
531 let a = <$atomic_type>::new(0b101101);
532 test_swap_ordering(&|order| {
533 a.fetch_or(0, order);
534 });
535 for &order in &helper::SWAP_ORDERINGS {
536 let a = <$atomic_type>::new(0b101101);
537 assert_eq!(a.fetch_or(0b110011, order), 0b101101);
538 assert_eq!(a.load(Ordering::Relaxed), 0b111111);
539 }
540 }
541 #[test]
542 fn or() {
543 let a = <$atomic_type>::new(0b101101);
544 test_swap_ordering(&|order| a.or(0, order));
545 for &order in &helper::SWAP_ORDERINGS {
546 let a = <$atomic_type>::new(0b101101);
547 a.or(0b110011, order);
548 assert_eq!(a.load(Ordering::Relaxed), 0b111111);
549 }
550 }
551 #[test]
552 fn fetch_xor() {
553 let a = <$atomic_type>::new(0b101101);
554 test_swap_ordering(&|order| {
555 a.fetch_xor(0, order);
556 });
557 for &order in &helper::SWAP_ORDERINGS {
558 let a = <$atomic_type>::new(0b101101);
559 assert_eq!(a.fetch_xor(0b110011, order), 0b101101);
560 assert_eq!(a.load(Ordering::Relaxed), 0b011110);
561 }
562 }
563 #[test]
564 fn xor() {
565 let a = <$atomic_type>::new(0b101101);
566 test_swap_ordering(&|order| a.xor(0, order));
567 for &order in &helper::SWAP_ORDERINGS {
568 let a = <$atomic_type>::new(0b101101);
569 a.xor(0b110011, order);
570 assert_eq!(a.load(Ordering::Relaxed), 0b011110);
571 }
572 }
573 #[test]
574 fn fetch_max() {
575 // TODO(gcc): failure: https://github.com/rust-lang/rustc_codegen_gcc/issues/821#issuecomment-3793567607
576 if option_env!("RUSTC_CODEGEN_GCC").unwrap_or_default() == "1"
577 && $int_type::MIN == 0
578 && mem::size_of::<$int_type>() <= 8
579 {
580 return;
581 }
582 // TODO(riscv): wrong result (as of Valgrind 3.26)
583 #[cfg(valgrind)]
584 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
585 return;
586 }
587 let a = <$atomic_type>::new(23);
588 #[cfg(valgrind)]
589 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
590 mark_aligned_defined(&a);
591 }
592 test_swap_ordering(&|order| {
593 a.fetch_max(23, order);
594 });
595 for &order in &helper::SWAP_ORDERINGS {
596 let a = <$atomic_type>::new(23);
597 #[cfg(valgrind)]
598 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
599 mark_aligned_defined(&a);
600 }
601 assert_eq!(a.fetch_max(22, order), 23);
602 assert_eq!(a.load(Ordering::Relaxed), 23);
603 assert_eq!(a.fetch_max(24, order), 23);
604 assert_eq!(a.load(Ordering::Relaxed), 24);
605 let a = <$atomic_type>::new(0);
606 #[cfg(valgrind)]
607 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
608 mark_aligned_defined(&a);
609 }
610 assert_eq!(a.fetch_max(1, order), 0);
611 assert_eq!(a.load(Ordering::Relaxed), 1);
612 assert_eq!(a.fetch_max(0, order), 1);
613 assert_eq!(a.load(Ordering::Relaxed), 1);
614 let a = <$atomic_type>::new(!0);
615 #[cfg(valgrind)]
616 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
617 mark_aligned_defined(&a);
618 }
619 assert_eq!(a.fetch_max(0, order), !0);
620 assert_eq!(a.load(Ordering::Relaxed), core::cmp::max(!0, 0));
621 }
622 }
623 #[test]
624 fn fetch_min() {
625 // TODO(gcc): failure: https://github.com/rust-lang/rustc_codegen_gcc/issues/821#issuecomment-3793567607
626 if option_env!("RUSTC_CODEGEN_GCC").unwrap_or_default() == "1"
627 && $int_type::MIN == 0
628 && mem::size_of::<$int_type>() <= 8
629 {
630 return;
631 }
632 // TODO(riscv): wrong result (as of Valgrind 3.26)
633 #[cfg(valgrind)]
634 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
635 return;
636 }
637 let a = <$atomic_type>::new(23);
638 #[cfg(valgrind)]
639 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
640 mark_aligned_defined(&a);
641 }
642 test_swap_ordering(&|order| {
643 a.fetch_min(23, order);
644 });
645 for &order in &helper::SWAP_ORDERINGS {
646 let a = <$atomic_type>::new(23);
647 #[cfg(valgrind)]
648 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
649 mark_aligned_defined(&a);
650 }
651 assert_eq!(a.fetch_min(24, order), 23);
652 assert_eq!(a.load(Ordering::Relaxed), 23);
653 assert_eq!(a.fetch_min(22, order), 23);
654 assert_eq!(a.load(Ordering::Relaxed), 22);
655 let a = <$atomic_type>::new(1);
656 #[cfg(valgrind)]
657 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
658 mark_aligned_defined(&a);
659 }
660 assert_eq!(a.fetch_min(0, order), 1);
661 assert_eq!(a.load(Ordering::Relaxed), 0);
662 assert_eq!(a.fetch_min(1, order), 0);
663 assert_eq!(a.load(Ordering::Relaxed), 0);
664 let a = <$atomic_type>::new(!0);
665 #[cfg(valgrind)]
666 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
667 mark_aligned_defined(&a);
668 }
669 assert_eq!(a.fetch_min(0, order), !0);
670 assert_eq!(a.load(Ordering::Relaxed), core::cmp::min(!0, 0));
671 }
672 }
673 #[test]
674 fn fetch_not() {
675 let a = <$atomic_type>::new(1);
676 test_swap_ordering(&|order| {
677 a.fetch_not(order);
678 });
679 for &order in &helper::SWAP_ORDERINGS {
680 let a = <$atomic_type>::new(1);
681 assert_eq!(a.fetch_not(order), 1);
682 assert_eq!(a.load(Ordering::Relaxed), !1);
683 }
684 }
685 #[test]
686 fn not() {
687 let a = <$atomic_type>::new(1);
688 test_swap_ordering(&|order| a.not(order));
689 for &order in &helper::SWAP_ORDERINGS {
690 let a = <$atomic_type>::new(1);
691 a.not(order);
692 assert_eq!(a.load(Ordering::Relaxed), !1);
693 }
694 }
695 #[test]
696 fn fetch_neg() {
697 // TODO(riscv): wrong result (as of Valgrind 3.26)
698 #[cfg(valgrind)]
699 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
700 return;
701 }
702 let a = <$atomic_type>::new(5);
703 test_swap_ordering(&|order| {
704 a.fetch_neg(order);
705 });
706 for &order in &helper::SWAP_ORDERINGS {
707 let a = <$atomic_type>::new(5);
708 assert_eq!(a.fetch_neg(order), 5);
709 assert_eq!(a.load(Ordering::Relaxed), <$int_type>::wrapping_neg(5));
710 assert_eq!(a.fetch_neg(order), <$int_type>::wrapping_neg(5));
711 assert_eq!(a.load(Ordering::Relaxed), 5);
712 let a = <$atomic_type>::new(<$int_type>::MIN);
713 assert_eq!(a.fetch_neg(order), <$int_type>::MIN);
714 assert_eq!(a.load(Ordering::Relaxed), <$int_type>::MIN.wrapping_neg());
715 assert_eq!(a.fetch_neg(order), <$int_type>::MIN.wrapping_neg());
716 assert_eq!(a.load(Ordering::Relaxed), <$int_type>::MIN);
717 }
718 }
719 #[test]
720 fn neg() {
721 // TODO(riscv): wrong result (as of Valgrind 3.26)
722 #[cfg(valgrind)]
723 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
724 return;
725 }
726 let a = <$atomic_type>::new(5);
727 test_swap_ordering(&|order| a.neg(order));
728 for &order in &helper::SWAP_ORDERINGS {
729 let a = <$atomic_type>::new(5);
730 a.neg(order);
731 assert_eq!(a.load(Ordering::Relaxed), <$int_type>::wrapping_neg(5));
732 a.neg(order);
733 assert_eq!(a.load(Ordering::Relaxed), 5);
734 let a = <$atomic_type>::new(<$int_type>::MIN);
735 a.neg(order);
736 assert_eq!(a.load(Ordering::Relaxed), <$int_type>::MIN.wrapping_neg());
737 a.neg(order);
738 assert_eq!(a.load(Ordering::Relaxed), <$int_type>::MIN);
739 }
740 }
741 #[test]
742 fn bit_set() {
743 let a = <$atomic_type>::new(0b0001);
744 test_swap_ordering(&|order| assert!(a.bit_set(0, order)));
745 for &order in &helper::SWAP_ORDERINGS {
746 let a = <$atomic_type>::new(0b0000);
747 assert!(!a.bit_set(0, order));
748 assert_eq!(a.load(Ordering::Relaxed), 0b0001);
749 assert!(a.bit_set(0, order));
750 assert_eq!(a.load(Ordering::Relaxed), 0b0001);
751 }
752 }
753 #[test]
754 fn bit_clear() {
755 let a = <$atomic_type>::new(0b0000);
756 test_swap_ordering(&|order| assert!(!a.bit_clear(0, order)));
757 for &order in &helper::SWAP_ORDERINGS {
758 let a = <$atomic_type>::new(0b0001);
759 assert!(a.bit_clear(0, order));
760 assert_eq!(a.load(Ordering::Relaxed), 0b0000);
761 assert!(!a.bit_clear(0, order));
762 assert_eq!(a.load(Ordering::Relaxed), 0b0000);
763 }
764 }
765 #[test]
766 fn bit_toggle() {
767 let a = <$atomic_type>::new(0b0000);
768 test_swap_ordering(&|order| {
769 a.bit_toggle(0, order);
770 });
771 for &order in &helper::SWAP_ORDERINGS {
772 let a = <$atomic_type>::new(0b0000);
773 assert!(!a.bit_toggle(0, order));
774 assert_eq!(a.load(Ordering::Relaxed), 0b0001);
775 assert!(a.bit_toggle(0, order));
776 assert_eq!(a.load(Ordering::Relaxed), 0b0000);
777 }
778 }
779 ::quickcheck::quickcheck! {
780 fn quickcheck_swap(x: $int_type, y: $int_type) -> bool {
781 // TODO(riscv): wrong result (as of Valgrind 3.26)
782 #[cfg(valgrind)]
783 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
784 return true;
785 }
786 for &order in &helper::SWAP_ORDERINGS {
787 let a = <$atomic_type>::new(x);
788 assert_eq!(a.swap(y, order), x);
789 assert_eq!(a.swap(x, order), y);
790 }
791 true
792 }
793 fn quickcheck_compare_exchange(x: $int_type, y: $int_type) -> bool {
794 // TODO(riscv): wrong result (as of Valgrind 3.26)
795 #[cfg(valgrind)]
796 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
797 return true;
798 }
799 #[cfg(all(
800 target_arch = "arm",
801 not(any(target_feature = "v6", portable_atomic_target_feature = "v6")),
802 ))]
803 {
804 // TODO: LLVM bug:
805 // https://github.com/llvm/llvm-project/issues/61880
806 // https://github.com/taiki-e/portable-atomic/issues/2
807 if mem::size_of::<$int_type>() <= 2 {
808 return true;
809 }
810 }
811 let mut rng = fastrand::Rng::new();
812 let z = loop {
813 let z = rng.$int_type(..);
814 if z != y {
815 break z;
816 }
817 };
818 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
819 let a = <$atomic_type>::new(x);
820 assert_eq!(a.compare_exchange(x, y, success, failure).unwrap(), x);
821 assert_eq!(a.load(Ordering::Relaxed), y);
822 assert_eq!(a.compare_exchange(z, x, success, failure).unwrap_err(), y);
823 assert_eq!(a.load(Ordering::Relaxed), y);
824 }
825 true
826 }
827 fn quickcheck_fetch_add(x: $int_type, y: $int_type) -> bool {
828 // TODO(riscv): wrong result (as of Valgrind 3.26)
829 #[cfg(valgrind)]
830 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
831 return true;
832 }
833 for &order in &helper::SWAP_ORDERINGS {
834 let a = <$atomic_type>::new(x);
835 #[cfg(valgrind)]
836 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
837 mark_aligned_defined(&a);
838 }
839 assert_eq!(a.fetch_add(y, order), x);
840 assert_eq!(a.load(Ordering::Relaxed), x.wrapping_add(y));
841 let a = <$atomic_type>::new(y);
842 #[cfg(valgrind)]
843 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
844 mark_aligned_defined(&a);
845 }
846 assert_eq!(a.fetch_add(x, order), y);
847 assert_eq!(a.load(Ordering::Relaxed), y.wrapping_add(x));
848 }
849 true
850 }
851 fn quickcheck_add(x: $int_type, y: $int_type) -> bool {
852 // TODO(riscv): wrong result (as of Valgrind 3.26)
853 #[cfg(valgrind)]
854 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
855 return true;
856 }
857 for &order in &helper::SWAP_ORDERINGS {
858 let a = <$atomic_type>::new(x);
859 #[cfg(valgrind)]
860 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
861 mark_aligned_defined(&a);
862 }
863 a.add(y, order);
864 assert_eq!(a.load(Ordering::Relaxed), x.wrapping_add(y));
865 let a = <$atomic_type>::new(y);
866 #[cfg(valgrind)]
867 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
868 mark_aligned_defined(&a);
869 }
870 a.add(x, order);
871 assert_eq!(a.load(Ordering::Relaxed), y.wrapping_add(x));
872 }
873 true
874 }
875 fn quickcheck_fetch_sub(x: $int_type, y: $int_type) -> bool {
876 // TODO(riscv): wrong result (as of Valgrind 3.26)
877 #[cfg(valgrind)]
878 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
879 return true;
880 }
881 for &order in &helper::SWAP_ORDERINGS {
882 let a = <$atomic_type>::new(x);
883 #[cfg(valgrind)]
884 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
885 mark_aligned_defined(&a);
886 }
887 assert_eq!(a.fetch_sub(y, order), x);
888 assert_eq!(a.load(Ordering::Relaxed), x.wrapping_sub(y));
889 let a = <$atomic_type>::new(y);
890 #[cfg(valgrind)]
891 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
892 mark_aligned_defined(&a);
893 }
894 assert_eq!(a.fetch_sub(x, order), y);
895 assert_eq!(a.load(Ordering::Relaxed), y.wrapping_sub(x));
896 }
897 true
898 }
899 fn quickcheck_sub(x: $int_type, y: $int_type) -> bool {
900 // TODO(riscv): wrong result (as of Valgrind 3.26)
901 #[cfg(valgrind)]
902 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
903 return true;
904 }
905 for &order in &helper::SWAP_ORDERINGS {
906 let a = <$atomic_type>::new(x);
907 #[cfg(valgrind)]
908 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
909 mark_aligned_defined(&a);
910 }
911 a.sub(y, order);
912 assert_eq!(a.load(Ordering::Relaxed), x.wrapping_sub(y));
913 let a = <$atomic_type>::new(y);
914 #[cfg(valgrind)]
915 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
916 mark_aligned_defined(&a);
917 }
918 a.sub(x, order);
919 assert_eq!(a.load(Ordering::Relaxed), y.wrapping_sub(x));
920 }
921 true
922 }
923 fn quickcheck_fetch_and(x: $int_type, y: $int_type) -> bool {
924 for &order in &helper::SWAP_ORDERINGS {
925 let a = <$atomic_type>::new(x);
926 assert_eq!(a.fetch_and(y, order), x);
927 assert_eq!(a.load(Ordering::Relaxed), x & y);
928 let a = <$atomic_type>::new(y);
929 assert_eq!(a.fetch_and(x, order), y);
930 assert_eq!(a.load(Ordering::Relaxed), y & x);
931 }
932 true
933 }
934 fn quickcheck_and(x: $int_type, y: $int_type) -> bool {
935 for &order in &helper::SWAP_ORDERINGS {
936 let a = <$atomic_type>::new(x);
937 a.and(y, order);
938 assert_eq!(a.load(Ordering::Relaxed), x & y);
939 let a = <$atomic_type>::new(y);
940 a.and(x, order);
941 assert_eq!(a.load(Ordering::Relaxed), y & x);
942 }
943 true
944 }
945 fn quickcheck_fetch_nand(x: $int_type, y: $int_type) -> bool {
946 // TODO(riscv): wrong result (as of Valgrind 3.26)
947 #[cfg(valgrind)]
948 if cfg!(target_arch = "riscv64") {
949 return true;
950 }
951 for &order in &helper::SWAP_ORDERINGS {
952 let a = <$atomic_type>::new(x);
953 assert_eq!(a.fetch_nand(y, order), x);
954 assert_eq!(a.load(Ordering::Relaxed), !(x & y));
955 let a = <$atomic_type>::new(y);
956 assert_eq!(a.fetch_nand(x, order), y);
957 assert_eq!(a.load(Ordering::Relaxed), !(y & x));
958 }
959 true
960 }
961 fn quickcheck_fetch_or(x: $int_type, y: $int_type) -> bool {
962 for &order in &helper::SWAP_ORDERINGS {
963 let a = <$atomic_type>::new(x);
964 assert_eq!(a.fetch_or(y, order), x);
965 assert_eq!(a.load(Ordering::Relaxed), x | y);
966 let a = <$atomic_type>::new(y);
967 assert_eq!(a.fetch_or(x, order), y);
968 assert_eq!(a.load(Ordering::Relaxed), y | x);
969 }
970 true
971 }
972 fn quickcheck_or(x: $int_type, y: $int_type) -> bool {
973 for &order in &helper::SWAP_ORDERINGS {
974 let a = <$atomic_type>::new(x);
975 a.or(y, order);
976 assert_eq!(a.load(Ordering::Relaxed), x | y);
977 let a = <$atomic_type>::new(y);
978 a.or(x, order);
979 assert_eq!(a.load(Ordering::Relaxed), y | x);
980 }
981 true
982 }
983 fn quickcheck_fetch_xor(x: $int_type, y: $int_type) -> bool {
984 for &order in &helper::SWAP_ORDERINGS {
985 let a = <$atomic_type>::new(x);
986 assert_eq!(a.fetch_xor(y, order), x);
987 assert_eq!(a.load(Ordering::Relaxed), x ^ y);
988 let a = <$atomic_type>::new(y);
989 assert_eq!(a.fetch_xor(x, order), y);
990 assert_eq!(a.load(Ordering::Relaxed), y ^ x);
991 }
992 true
993 }
994 fn quickcheck_xor(x: $int_type, y: $int_type) -> bool {
995 for &order in &helper::SWAP_ORDERINGS {
996 let a = <$atomic_type>::new(x);
997 a.xor(y, order);
998 assert_eq!(a.load(Ordering::Relaxed), x ^ y);
999 let a = <$atomic_type>::new(y);
1000 a.xor(x, order);
1001 assert_eq!(a.load(Ordering::Relaxed), y ^ x);
1002 }
1003 true
1004 }
1005 fn quickcheck_fetch_max(x: $int_type, y: $int_type) -> bool {
1006 // TODO(gcc): failure: https://github.com/rust-lang/rustc_codegen_gcc/issues/821#issuecomment-3793567607
1007 if option_env!("RUSTC_CODEGEN_GCC").unwrap_or_default() == "1"
1008 && $int_type::MIN == 0
1009 && mem::size_of::<$int_type>() <= 8
1010 {
1011 return true;
1012 }
1013 // TODO(riscv): wrong result (as of Valgrind 3.26)
1014 #[cfg(valgrind)]
1015 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
1016 return true;
1017 }
1018 for &order in &helper::SWAP_ORDERINGS {
1019 let a = <$atomic_type>::new(x);
1020 #[cfg(valgrind)]
1021 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
1022 mark_aligned_defined(&a);
1023 }
1024 assert_eq!(a.fetch_max(y, order), x);
1025 assert_eq!(a.load(Ordering::Relaxed), core::cmp::max(x, y));
1026 let a = <$atomic_type>::new(y);
1027 #[cfg(valgrind)]
1028 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
1029 mark_aligned_defined(&a);
1030 }
1031 assert_eq!(a.fetch_max(x, order), y);
1032 assert_eq!(a.load(Ordering::Relaxed), core::cmp::max(y, x));
1033 }
1034 true
1035 }
1036 fn quickcheck_fetch_min(x: $int_type, y: $int_type) -> bool {
1037 // TODO(gcc): failure: https://github.com/rust-lang/rustc_codegen_gcc/issues/821#issuecomment-3793567607
1038 if option_env!("RUSTC_CODEGEN_GCC").unwrap_or_default() == "1"
1039 && $int_type::MIN == 0
1040 && mem::size_of::<$int_type>() <= 8
1041 {
1042 return true;
1043 }
1044 // TODO(riscv): wrong result (as of Valgrind 3.26)
1045 #[cfg(valgrind)]
1046 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
1047 return true;
1048 }
1049 for &order in &helper::SWAP_ORDERINGS {
1050 let a = <$atomic_type>::new(x);
1051 #[cfg(valgrind)]
1052 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
1053 mark_aligned_defined(&a);
1054 }
1055 assert_eq!(a.fetch_min(y, order), x);
1056 assert_eq!(a.load(Ordering::Relaxed), core::cmp::min(x, y));
1057 let a = <$atomic_type>::new(y);
1058 #[cfg(valgrind)]
1059 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
1060 mark_aligned_defined(&a);
1061 }
1062 assert_eq!(a.fetch_min(x, order), y);
1063 assert_eq!(a.load(Ordering::Relaxed), core::cmp::min(y, x));
1064 }
1065 true
1066 }
1067 fn quickcheck_fetch_not(x: $int_type) -> bool {
1068 for &order in &helper::SWAP_ORDERINGS {
1069 let a = <$atomic_type>::new(x);
1070 assert_eq!(a.fetch_not(order), x);
1071 assert_eq!(a.load(Ordering::Relaxed), !x);
1072 assert_eq!(a.fetch_not(order), !x);
1073 assert_eq!(a.load(Ordering::Relaxed), x);
1074 }
1075 true
1076 }
1077 fn quickcheck_not(x: $int_type) -> bool {
1078 for &order in &helper::SWAP_ORDERINGS {
1079 let a = <$atomic_type>::new(x);
1080 a.not(order);
1081 assert_eq!(a.load(Ordering::Relaxed), !x);
1082 a.not(order);
1083 assert_eq!(a.load(Ordering::Relaxed), x);
1084 }
1085 true
1086 }
1087 fn quickcheck_fetch_neg(x: $int_type) -> bool {
1088 // TODO(riscv): wrong result (as of Valgrind 3.26)
1089 #[cfg(valgrind)]
1090 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
1091 return true;
1092 }
1093 #[cfg(all(
1094 target_arch = "arm",
1095 not(any(target_feature = "v6", portable_atomic_target_feature = "v6")),
1096 ))]
1097 {
1098 // TODO: LLVM bug:
1099 // https://github.com/llvm/llvm-project/issues/61880
1100 // https://github.com/taiki-e/portable-atomic/issues/2
1101 if mem::size_of::<$int_type>() <= 2 {
1102 return true;
1103 }
1104 }
1105 for &order in &helper::SWAP_ORDERINGS {
1106 let a = <$atomic_type>::new(x);
1107 assert_eq!(a.fetch_neg(order), x);
1108 assert_eq!(a.load(Ordering::Relaxed), x.wrapping_neg());
1109 assert_eq!(a.fetch_neg(order), x.wrapping_neg());
1110 assert_eq!(a.load(Ordering::Relaxed), x);
1111 }
1112 true
1113 }
1114 fn quickcheck_neg(x: $int_type) -> bool {
1115 // TODO(riscv): wrong result (as of Valgrind 3.26)
1116 #[cfg(valgrind)]
1117 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
1118 return true;
1119 }
1120 for &order in &helper::SWAP_ORDERINGS {
1121 let a = <$atomic_type>::new(x);
1122 a.neg(order);
1123 assert_eq!(a.load(Ordering::Relaxed), x.wrapping_neg());
1124 a.neg(order);
1125 assert_eq!(a.load(Ordering::Relaxed), x);
1126 }
1127 true
1128 }
1129 fn quickcheck_bit_set(x: $int_type, bit: u32) -> bool {
1130 for &order in &helper::SWAP_ORDERINGS {
1131 let a = <$atomic_type>::new(x);
1132 let b = a.bit_set(bit, order);
1133 let mask = <$int_type>::wrapping_shl(1, bit);
1134 assert_eq!(a.load(Ordering::Relaxed), x | mask);
1135 assert_eq!(b, x & mask != 0);
1136 }
1137 true
1138 }
1139 fn quickcheck_bit_clear(x: $int_type, bit: u32) -> bool {
1140 for &order in &helper::SWAP_ORDERINGS {
1141 let a = <$atomic_type>::new(x);
1142 let b = a.bit_clear(bit, order);
1143 let mask = <$int_type>::wrapping_shl(1, bit);
1144 assert_eq!(a.load(Ordering::Relaxed), x & !mask);
1145 assert_eq!(b, x & mask != 0);
1146 }
1147 true
1148 }
1149 fn quickcheck_bit_toggle(x: $int_type, bit: u32) -> bool {
1150 for &order in &helper::SWAP_ORDERINGS {
1151 let a = <$atomic_type>::new(x);
1152 let b = a.bit_toggle(bit, order);
1153 let mask = <$int_type>::wrapping_shl(1, bit);
1154 assert_eq!(a.load(Ordering::Relaxed), x ^ mask);
1155 assert_eq!(b, x & mask != 0);
1156 }
1157 true
1158 }
1159 }
1160 };
1161 ($atomic_type:ty, $int_type:ident) => {
1162 __test_atomic_int!($atomic_type, $int_type, single_thread);
1163
1164 #[test]
1165 fn stress_swap() {
1166 // TODO(riscv): wrong result (as of Valgrind 3.26)
1167 #[cfg(valgrind)]
1168 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
1169 return;
1170 }
1171 let mut rng = fastrand::Rng::new();
1172 let (iterations, threads) = stress_test_config(&mut rng);
1173 let data1 = &(0..threads)
1174 .map(|_| (0..iterations).map(|_| rng.$int_type(..)).collect::<Vec<_>>())
1175 .collect::<Vec<_>>();
1176 let data2 = &(0..threads)
1177 .map(|_| (0..iterations).map(|_| rng.$int_type(..)).collect::<Vec<_>>())
1178 .collect::<Vec<_>>();
1179 let set = &data1
1180 .iter()
1181 .flat_map(|v| v.iter().copied())
1182 .chain(data2.iter().flat_map(|v| v.iter().copied()))
1183 .collect::<BTreeSet<_>>();
1184 let a = &<$atomic_type>::new(data2[0][rng.usize(0..iterations)]);
1185 let now = &Instant::now();
1186 thread::scope(|s| {
1187 for thread in 0..threads {
1188 if thread % 2 == 0 {
1189 s.spawn(move |_| {
1190 let mut rng = fastrand::Rng::new();
1191 let now = *now;
1192 for i in 0..iterations {
1193 a.store(data1[thread][i], rand_store_ordering(&mut rng));
1194 }
1195 eprintln!("store end={:?}", now.elapsed());
1196 });
1197 } else {
1198 s.spawn(|_| {
1199 let mut rng = fastrand::Rng::new();
1200 let now = *now;
1201 let mut v = vec![0; iterations];
1202 for i in 0..iterations {
1203 v[i] = a.load(rand_load_ordering(&mut rng));
1204 }
1205 eprintln!("load end={:?}", now.elapsed());
1206 for v in v {
1207 assert!(set.contains(&v), "load unexpected={}", v);
1208 }
1209 });
1210 }
1211 s.spawn(move |_| {
1212 let mut rng = fastrand::Rng::new();
1213 let now = *now;
1214 let mut v = vec![0; iterations];
1215 for i in 0..iterations {
1216 v[i] = a.swap(data2[thread][i], rand_swap_ordering(&mut rng));
1217 }
1218 eprintln!("swap end={:?}", now.elapsed());
1219 for v in v {
1220 assert!(set.contains(&v), "swap unexpected={}", v);
1221 }
1222 });
1223 }
1224 })
1225 .unwrap();
1226 }
1227 #[test]
1228 fn stress_compare_exchange() {
1229 // TODO(riscv): wrong result (as of Valgrind 3.26)
1230 #[cfg(valgrind)]
1231 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
1232 return;
1233 }
1234 let mut rng = fastrand::Rng::new();
1235 let (iterations, threads) = stress_test_config(&mut rng);
1236 let data1 = &(0..threads)
1237 .map(|_| (0..iterations).map(|_| rng.$int_type(..)).collect::<Vec<_>>())
1238 .collect::<Vec<_>>();
1239 let data2 = &(0..threads)
1240 .map(|_| (0..iterations).map(|_| rng.$int_type(..)).collect::<Vec<_>>())
1241 .collect::<Vec<_>>();
1242 let set = &data1
1243 .iter()
1244 .flat_map(|v| v.iter().copied())
1245 .chain(data2.iter().flat_map(|v| v.iter().copied()))
1246 .collect::<BTreeSet<_>>();
1247 let a = &<$atomic_type>::new(data2[0][rng.usize(0..iterations)]);
1248 #[cfg(valgrind)]
1249 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
1250 mark_aligned_defined(a);
1251 }
1252 let now = &Instant::now();
1253 thread::scope(|s| {
1254 for thread in 0..threads {
1255 s.spawn(move |_| {
1256 let mut rng = fastrand::Rng::new();
1257 let now = *now;
1258 for i in 0..iterations {
1259 a.store(data1[thread][i], rand_store_ordering(&mut rng));
1260 }
1261 eprintln!("store end={:?}", now.elapsed());
1262 });
1263 s.spawn(|_| {
1264 let mut rng = fastrand::Rng::new();
1265 let now = *now;
1266 let mut v = vec![data2[0][0]; iterations];
1267 for i in 0..iterations {
1268 v[i] = a.load(rand_load_ordering(&mut rng));
1269 }
1270 eprintln!("load end={:?}", now.elapsed());
1271 for v in v {
1272 assert!(set.contains(&v), "load unexpected={}", v);
1273 }
1274 });
1275 s.spawn(move |_| {
1276 let mut rng = fastrand::Rng::new();
1277 let now = *now;
1278 let mut v = vec![data2[0][0]; iterations];
1279 for i in 0..iterations {
1280 let old = if i % 2 == 0 {
1281 rng.$int_type(..)
1282 } else {
1283 a.load(Ordering::Relaxed)
1284 };
1285 let new = data2[thread][i];
1286 let o = rand_compare_exchange_ordering(&mut rng);
1287 match a.compare_exchange(old, new, o.0, o.1) {
1288 Ok(r) => assert_eq!(old, r),
1289 Err(r) => v[i] = r,
1290 }
1291 }
1292 eprintln!("compare_exchange end={:?}", now.elapsed());
1293 for v in v {
1294 assert!(set.contains(&v), "cas unexpected={}", v);
1295 }
1296 });
1297 }
1298 })
1299 .unwrap();
1300 }
1301 };
1302 }
1303 macro_rules! __test_atomic_float {
1304 ($atomic_type:ty, $float_type:ident, single_thread) => {
1305 #[test]
1306 fn swap() {
1307 let a = <$atomic_type>::new(5.);
1308 test_swap_ordering(&|order| { a.swap(5., order); });
1309 for &order in &helper::SWAP_ORDERINGS {
1310 assert_eq!(a.swap(10., order), 5.);
1311 assert_eq!(a.swap(5., order), 10.);
1312 }
1313 }
1314 #[test]
1315 fn compare_exchange() {
1316 let a = <$atomic_type>::new(5.);
1317 test_compare_exchange_ordering(&|success, failure| {
1318 let _ = a.compare_exchange(5., 5., success, failure);
1319 });
1320 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1321 let a = <$atomic_type>::new(5.);
1322 assert_eq!(a.compare_exchange(5., 10., success, failure), Ok(5.));
1323 assert_eq!(a.load(Ordering::Relaxed), 10.);
1324 assert_eq!(a.compare_exchange(6., 12., success, failure), Err(10.));
1325 assert_eq!(a.load(Ordering::Relaxed), 10.);
1326 }
1327 }
1328 #[test]
1329 fn compare_exchange_weak() {
1330 let a = <$atomic_type>::new(4.);
1331 test_compare_exchange_ordering(&|success, failure| {
1332 let _ = a.compare_exchange_weak(4., 4., success, failure);
1333 });
1334 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1335 let a = <$atomic_type>::new(4.);
1336 assert_eq!(a.compare_exchange_weak(6., 8., success, failure), Err(4.));
1337 let mut old = a.load(Ordering::Relaxed);
1338 loop {
1339 let new = old * 2.;
1340 match a.compare_exchange_weak(old, new, success, failure) {
1341 Ok(_) => break,
1342 Err(x) => old = x,
1343 }
1344 }
1345 assert_eq!(a.load(Ordering::Relaxed), 8.);
1346 }
1347 }
1348 #[test]
1349 fn fetch_add() {
1350 let a = <$atomic_type>::new(0.);
1351 test_swap_ordering(&|order| { a.fetch_add(0., order); });
1352 for &order in &helper::SWAP_ORDERINGS {
1353 let a = <$atomic_type>::new(0.);
1354 assert_eq!(a.fetch_add(10., order), 0.);
1355 assert_eq!(a.load(Ordering::Relaxed), 10.);
1356 let a = <$atomic_type>::new($float_type::MAX);
1357 assert_eq!(a.fetch_add(1., order), $float_type::MAX);
1358 assert_eq!(a.load(Ordering::Relaxed), $float_type::MAX + 1.);
1359 }
1360 }
1361 #[test]
1362 fn fetch_sub() {
1363 let a = <$atomic_type>::new(20.);
1364 test_swap_ordering(&|order| { a.fetch_sub(0., order); });
1365 for &order in &helper::SWAP_ORDERINGS {
1366 let a = <$atomic_type>::new(20.);
1367 assert_eq!(a.fetch_sub(10., order), 20.);
1368 assert_eq!(a.load(Ordering::Relaxed), 10.);
1369 let a = <$atomic_type>::new($float_type::MIN);
1370 assert_eq!(a.fetch_sub(1., order), $float_type::MIN);
1371 assert_eq!(a.load(Ordering::Relaxed), $float_type::MIN - 1.);
1372 }
1373 }
1374 #[test]
1375 fn fetch_max() {
1376 if mem::size_of::<$float_type>() == 16
1377 && cfg!(any(
1378 target_arch = "arm",
1379 target_arch = "mips",
1380 target_arch = "mips32r6",
1381 target_arch = "mips64",
1382 target_arch = "mips64r6",
1383 target_vendor = "apple",
1384 windows,
1385 ))
1386 {
1387 // TODO(f128):
1388 return;
1389 }
1390 let a = <$atomic_type>::new(23.);
1391 test_swap_ordering(&|order| { a.fetch_max(23., order); });
1392 for &order in &helper::SWAP_ORDERINGS {
1393 let a = <$atomic_type>::new(23.);
1394 assert_eq!(a.fetch_max(22., order), 23.);
1395 assert_eq!(a.load(Ordering::Relaxed), 23.);
1396 assert_eq!(a.fetch_max(24., order), 23.);
1397 assert_eq!(a.load(Ordering::Relaxed), 24.);
1398 }
1399 }
1400 #[test]
1401 fn fetch_min() {
1402 if mem::size_of::<$float_type>() == 16
1403 && cfg!(any(
1404 target_arch = "arm",
1405 target_arch = "mips",
1406 target_arch = "mips32r6",
1407 target_arch = "mips64",
1408 target_arch = "mips64r6",
1409 target_vendor = "apple",
1410 windows,
1411 ))
1412 {
1413 // TODO(f128):
1414 return;
1415 }
1416 let a = <$atomic_type>::new(23.);
1417 test_swap_ordering(&|order| { a.fetch_min(23., order); });
1418 for &order in &helper::SWAP_ORDERINGS {
1419 let a = <$atomic_type>::new(23.);
1420 assert_eq!(a.fetch_min(24., order), 23.);
1421 assert_eq!(a.load(Ordering::Relaxed), 23.);
1422 assert_eq!(a.fetch_min(22., order), 23.);
1423 assert_eq!(a.load(Ordering::Relaxed), 22.);
1424 }
1425 }
1426 #[test]
1427 fn fetch_neg() {
1428 let a = <$atomic_type>::new(5.);
1429 test_swap_ordering(&|order| { a.fetch_neg(order); });
1430 for &order in &helper::SWAP_ORDERINGS {
1431 let a = <$atomic_type>::new(5.);
1432 assert_eq!(a.fetch_neg(order), 5.);
1433 assert_eq!(a.load(Ordering::Relaxed), -5.);
1434 assert_eq!(a.fetch_neg(order), -5.);
1435 assert_eq!(a.load(Ordering::Relaxed), 5.);
1436 }
1437 }
1438 #[test]
1439 fn fetch_abs() {
1440 let a = <$atomic_type>::new(23.);
1441 test_swap_ordering(&|order| { a.fetch_abs(order); });
1442 for &order in &helper::SWAP_ORDERINGS {
1443 let a = <$atomic_type>::new(-23.);
1444 assert_eq!(a.fetch_abs(order), -23.);
1445 assert_eq!(a.load(Ordering::Relaxed), 23.);
1446 assert_eq!(a.fetch_abs(order), 23.);
1447 assert_eq!(a.load(Ordering::Relaxed), 23.);
1448 }
1449 }
1450 ::quickcheck::quickcheck! {
1451 fn quickcheck_swap(x: $float_type, y: $float_type) -> bool {
1452 for &order in &helper::SWAP_ORDERINGS {
1453 let a = <$atomic_type>::new(x);
1454 assert_float_op_eq!(a.swap(y, order), x);
1455 assert_float_op_eq!(a.swap(x, order), y);
1456 }
1457 true
1458 }
1459 fn quickcheck_compare_exchange(x: $float_type, y: $float_type) -> bool {
1460 #[cfg(all(
1461 target_arch = "arm",
1462 not(any(target_feature = "v6", portable_atomic_target_feature = "v6")),
1463 ))]
1464 {
1465 // TODO: LLVM bug:
1466 // https://github.com/llvm/llvm-project/issues/61880
1467 // https://github.com/taiki-e/portable-atomic/issues/2
1468 if mem::size_of::<$float_type>() <= 2 {
1469 return true;
1470 }
1471 }
1472 let mut rng = fastrand::Rng::new();
1473 let z = loop {
1474 let z = float_rand::$float_type(&mut rng);
1475 if z != y {
1476 break z;
1477 }
1478 };
1479 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1480 let a = <$atomic_type>::new(x);
1481 assert_float_op_eq!(a.compare_exchange(x, y, success, failure).unwrap(), x);
1482 assert_float_op_eq!(a.load(Ordering::Relaxed), y);
1483 assert_float_op_eq!(
1484 a.compare_exchange(z, x, success, failure).unwrap_err(),
1485 y,
1486 );
1487 assert_float_op_eq!(a.load(Ordering::Relaxed), y);
1488 }
1489 true
1490 }
1491 fn quickcheck_fetch_add(x: $float_type, y: $float_type) -> bool {
1492 if cfg!(all(not(debug_assertions), target_arch = "x86", not(target_feature = "sse2"))) {
1493 // TODO: rustc bug: https://github.com/rust-lang/rust/issues/114479
1494 return true;
1495 }
1496 for &order in &helper::SWAP_ORDERINGS {
1497 let a = <$atomic_type>::new(x);
1498 assert_float_op_eq!(a.fetch_add(y, order), x);
1499 assert_float_op_eq!(a.load(Ordering::Relaxed), x + y);
1500 let a = <$atomic_type>::new(y);
1501 assert_float_op_eq!(a.fetch_add(x, order), y);
1502 assert_float_op_eq!(a.load(Ordering::Relaxed), y + x);
1503 }
1504 true
1505 }
1506 fn quickcheck_fetch_sub(x: $float_type, y: $float_type) -> bool {
1507 if cfg!(all(not(debug_assertions), target_arch = "x86", not(target_feature = "sse2"))) {
1508 // TODO: rustc bug: https://github.com/rust-lang/rust/issues/114479
1509 return true;
1510 }
1511 for &order in &helper::SWAP_ORDERINGS {
1512 let a = <$atomic_type>::new(x);
1513 assert_float_op_eq!(a.fetch_sub(y, order), x);
1514 assert_float_op_eq!(a.load(Ordering::Relaxed), x - y);
1515 let a = <$atomic_type>::new(y);
1516 assert_float_op_eq!(a.fetch_sub(x, order), y);
1517 assert_float_op_eq!(a.load(Ordering::Relaxed), y - x);
1518 }
1519 true
1520 }
1521 fn quickcheck_fetch_max(x: $float_type, y: $float_type) -> bool {
1522 if mem::size_of::<$float_type>() == 16
1523 && cfg!(any(
1524 target_arch = "arm",
1525 target_arch = "mips",
1526 target_arch = "mips32r6",
1527 target_arch = "mips64",
1528 target_arch = "mips64r6",
1529 target_vendor = "apple",
1530 windows,
1531 ))
1532 {
1533 // TODO(f128):
1534 return true;
1535 }
1536 for &order in &helper::SWAP_ORDERINGS {
1537 let a = <$atomic_type>::new(x);
1538 assert_float_op_eq!(a.fetch_max(y, order), x);
1539 assert_float_op_eq!(a.load(Ordering::Relaxed), x.max(y));
1540 let a = <$atomic_type>::new(y);
1541 assert_float_op_eq!(a.fetch_max(x, order), y);
1542 assert_float_op_eq!(a.load(Ordering::Relaxed), y.max(x));
1543 }
1544 true
1545 }
1546 fn quickcheck_fetch_min(x: $float_type, y: $float_type) -> bool {
1547 if mem::size_of::<$float_type>() == 16
1548 && cfg!(any(
1549 target_arch = "arm",
1550 target_arch = "mips",
1551 target_arch = "mips32r6",
1552 target_arch = "mips64",
1553 target_arch = "mips64r6",
1554 target_vendor = "apple",
1555 windows,
1556 ))
1557 {
1558 // TODO(f128):
1559 return true;
1560 }
1561 for &order in &helper::SWAP_ORDERINGS {
1562 let a = <$atomic_type>::new(x);
1563 assert_float_op_eq!(a.fetch_min(y, order), x);
1564 assert_float_op_eq!(a.load(Ordering::Relaxed), x.min(y));
1565 let a = <$atomic_type>::new(y);
1566 assert_float_op_eq!(a.fetch_min(x, order), y);
1567 assert_float_op_eq!(a.load(Ordering::Relaxed), y.min(x));
1568 }
1569 true
1570 }
1571 fn quickcheck_fetch_neg(x: $float_type) -> bool {
1572 for &order in &helper::SWAP_ORDERINGS {
1573 let a = <$atomic_type>::new(x);
1574 assert_float_op_eq!(a.fetch_neg(order), x);
1575 assert_float_op_eq!(a.load(Ordering::Relaxed), -x);
1576 assert_float_op_eq!(a.fetch_neg(order), -x);
1577 assert_float_op_eq!(a.load(Ordering::Relaxed), x);
1578 }
1579 true
1580 }
1581 fn quickcheck_fetch_abs(x: $float_type) -> bool {
1582 for &order in &helper::SWAP_ORDERINGS {
1583 let a = <$atomic_type>::new(x);
1584 assert_float_op_eq!(a.fetch_abs(order), x);
1585 assert_float_op_eq!(a.fetch_abs(order), x.abs());
1586 assert_float_op_eq!(a.load(Ordering::Relaxed), x.abs());
1587 }
1588 true
1589 }
1590 }
1591 };
1592 ($atomic_type:ty, $float_type:ident) => {
1593 __test_atomic_float!($atomic_type, $float_type, single_thread);
1594 // TODO: multi thread
1595 };
1596 }
1597 macro_rules! __test_atomic_bool {
1598 ($atomic_type:ty, single_thread) => {
1599 #[test]
1600 fn swap() {
1601 let a = <$atomic_type>::new(true);
1602 test_swap_ordering(&|order| {
1603 a.swap(true, order);
1604 });
1605 for &order in &helper::SWAP_ORDERINGS {
1606 assert_eq!(a.swap(true, order), true);
1607 assert_eq!(a.swap(false, order), true);
1608 assert_eq!(a.swap(false, order), false);
1609 assert_eq!(a.swap(true, order), false);
1610 }
1611 }
1612 #[test]
1613 fn compare_exchange() {
1614 let a = <$atomic_type>::new(true);
1615 test_compare_exchange_ordering(&|success, failure| {
1616 let _ = a.compare_exchange(true, true, success, failure);
1617 });
1618 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1619 let a = <$atomic_type>::new(true);
1620 assert_eq!(a.compare_exchange(true, false, success, failure), Ok(true));
1621 assert_eq!(a.load(Ordering::Relaxed), false);
1622 assert_eq!(a.compare_exchange(true, true, success, failure), Err(false));
1623 assert_eq!(a.load(Ordering::Relaxed), false);
1624 }
1625 }
1626 #[test]
1627 fn compare_exchange_weak() {
1628 let a = <$atomic_type>::new(false);
1629 #[cfg(valgrind)]
1630 if IMP_EMU_SUB_WORD_CAS {
1631 mark_aligned_defined(&a);
1632 }
1633 test_compare_exchange_ordering(&|success, failure| {
1634 let _ = a.compare_exchange_weak(false, false, success, failure);
1635 });
1636 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1637 let a = <$atomic_type>::new(false);
1638 #[cfg(valgrind)]
1639 if IMP_EMU_SUB_WORD_CAS {
1640 mark_aligned_defined(&a);
1641 }
1642 assert_eq!(a.compare_exchange_weak(true, true, success, failure), Err(false));
1643 let mut old = a.load(Ordering::Relaxed);
1644 let new = true;
1645 loop {
1646 match a.compare_exchange_weak(old, new, success, failure) {
1647 Ok(_) => break,
1648 Err(x) => old = x,
1649 }
1650 }
1651 assert_eq!(a.load(Ordering::Relaxed), true);
1652 }
1653 }
1654 #[test]
1655 fn fetch_and() {
1656 let a = <$atomic_type>::new(true);
1657 test_swap_ordering(&|order| assert_eq!(a.fetch_and(true, order), true));
1658 for &order in &helper::SWAP_ORDERINGS {
1659 let a = <$atomic_type>::new(true);
1660 assert_eq!(a.fetch_and(false, order), true);
1661 assert_eq!(a.load(Ordering::Relaxed), false);
1662 let a = <$atomic_type>::new(true);
1663 assert_eq!(a.fetch_and(true, order), true);
1664 assert_eq!(a.load(Ordering::Relaxed), true);
1665 let a = <$atomic_type>::new(false);
1666 assert_eq!(a.fetch_and(false, order), false);
1667 assert_eq!(a.load(Ordering::Relaxed), false);
1668 let a = <$atomic_type>::new(false);
1669 assert_eq!(a.fetch_and(true, order), false);
1670 assert_eq!(a.load(Ordering::Relaxed), false);
1671 }
1672 }
1673 #[test]
1674 fn and() {
1675 let a = <$atomic_type>::new(true);
1676 test_swap_ordering(&|order| a.and(true, order));
1677 for &order in &helper::SWAP_ORDERINGS {
1678 let a = <$atomic_type>::new(true);
1679 a.and(false, order);
1680 assert_eq!(a.load(Ordering::Relaxed), false);
1681 let a = <$atomic_type>::new(true);
1682 a.and(true, order);
1683 assert_eq!(a.load(Ordering::Relaxed), true);
1684 let a = <$atomic_type>::new(false);
1685 a.and(false, order);
1686 assert_eq!(a.load(Ordering::Relaxed), false);
1687 let a = <$atomic_type>::new(false);
1688 a.and(true, order);
1689 assert_eq!(a.load(Ordering::Relaxed), false);
1690 }
1691 }
1692 #[test]
1693 fn fetch_or() {
1694 let a = <$atomic_type>::new(true);
1695 test_swap_ordering(&|order| assert_eq!(a.fetch_or(false, order), true));
1696 for &order in &helper::SWAP_ORDERINGS {
1697 let a = <$atomic_type>::new(true);
1698 assert_eq!(a.fetch_or(false, order), true);
1699 assert_eq!(a.load(Ordering::Relaxed), true);
1700 let a = <$atomic_type>::new(true);
1701 assert_eq!(a.fetch_or(true, order), true);
1702 assert_eq!(a.load(Ordering::Relaxed), true);
1703 let a = <$atomic_type>::new(false);
1704 assert_eq!(a.fetch_or(false, order), false);
1705 assert_eq!(a.load(Ordering::Relaxed), false);
1706 let a = <$atomic_type>::new(false);
1707 assert_eq!(a.fetch_or(true, order), false);
1708 assert_eq!(a.load(Ordering::Relaxed), true);
1709 }
1710 }
1711 #[test]
1712 fn or() {
1713 let a = <$atomic_type>::new(true);
1714 test_swap_ordering(&|order| a.or(false, order));
1715 for &order in &helper::SWAP_ORDERINGS {
1716 let a = <$atomic_type>::new(true);
1717 a.or(false, order);
1718 assert_eq!(a.load(Ordering::Relaxed), true);
1719 let a = <$atomic_type>::new(true);
1720 a.or(true, order);
1721 assert_eq!(a.load(Ordering::Relaxed), true);
1722 let a = <$atomic_type>::new(false);
1723 a.or(false, order);
1724 assert_eq!(a.load(Ordering::Relaxed), false);
1725 let a = <$atomic_type>::new(false);
1726 a.or(true, order);
1727 assert_eq!(a.load(Ordering::Relaxed), true);
1728 }
1729 }
1730 #[test]
1731 fn fetch_xor() {
1732 let a = <$atomic_type>::new(true);
1733 test_swap_ordering(&|order| assert_eq!(a.fetch_xor(false, order), true));
1734 for &order in &helper::SWAP_ORDERINGS {
1735 let a = <$atomic_type>::new(true);
1736 assert_eq!(a.fetch_xor(false, order), true);
1737 assert_eq!(a.load(Ordering::Relaxed), true);
1738 let a = <$atomic_type>::new(true);
1739 assert_eq!(a.fetch_xor(true, order), true);
1740 assert_eq!(a.load(Ordering::Relaxed), false);
1741 let a = <$atomic_type>::new(false);
1742 assert_eq!(a.fetch_xor(false, order), false);
1743 assert_eq!(a.load(Ordering::Relaxed), false);
1744 let a = <$atomic_type>::new(false);
1745 assert_eq!(a.fetch_xor(true, order), false);
1746 assert_eq!(a.load(Ordering::Relaxed), true);
1747 }
1748 }
1749 #[test]
1750 fn xor() {
1751 let a = <$atomic_type>::new(true);
1752 test_swap_ordering(&|order| a.xor(false, order));
1753 for &order in &helper::SWAP_ORDERINGS {
1754 let a = <$atomic_type>::new(true);
1755 a.xor(false, order);
1756 assert_eq!(a.load(Ordering::Relaxed), true);
1757 let a = <$atomic_type>::new(true);
1758 a.xor(true, order);
1759 assert_eq!(a.load(Ordering::Relaxed), false);
1760 let a = <$atomic_type>::new(false);
1761 a.xor(false, order);
1762 assert_eq!(a.load(Ordering::Relaxed), false);
1763 let a = <$atomic_type>::new(false);
1764 a.xor(true, order);
1765 assert_eq!(a.load(Ordering::Relaxed), true);
1766 }
1767 }
1768 ::quickcheck::quickcheck! {
1769 fn quickcheck_compare_exchange(x: bool, y: bool) -> bool {
1770 let z = !y;
1771 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1772 let a = <$atomic_type>::new(x);
1773 #[cfg(valgrind)]
1774 if IMP_EMU_SUB_WORD_CAS {
1775 mark_aligned_defined(&a);
1776 }
1777 assert_eq!(a.compare_exchange(x, y, success, failure).unwrap(), x);
1778 assert_eq!(a.load(Ordering::Relaxed), y);
1779 assert_eq!(a.compare_exchange(z, x, success, failure).unwrap_err(), y);
1780 assert_eq!(a.load(Ordering::Relaxed), y);
1781 }
1782 true
1783 }
1784 }
1785 };
1786 ($atomic_type:ty) => {
1787 __test_atomic_bool!($atomic_type, single_thread);
1788 // TODO: multi thread
1789 };
1790 }
1791 macro_rules! __test_atomic_ptr {
1792 ($atomic_type:ty, single_thread) => {
1793 #[allow(unused_imports)]
1794 use sptr::Strict as _; // for old rustc
1795 #[test]
1796 fn swap() {
1797 let a = <$atomic_type>::new(ptr::null_mut());
1798 test_swap_ordering(&|order| {
1799 a.swap(ptr::null_mut(), order);
1800 });
1801 let x = &mut 1;
1802 for &order in &helper::SWAP_ORDERINGS {
1803 assert_eq!(a.swap(x, order), ptr::null_mut());
1804 assert_eq!(a.swap(ptr::null_mut(), order), x as *mut _);
1805 }
1806 }
1807 #[test]
1808 fn compare_exchange() {
1809 let a = <$atomic_type>::new(ptr::null_mut());
1810 test_compare_exchange_ordering(&|success, failure| {
1811 let _ = a.compare_exchange(ptr::null_mut(), ptr::null_mut(), success, failure);
1812 });
1813 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1814 let a = <$atomic_type>::new(ptr::null_mut());
1815 let x = &mut 1;
1816 assert_eq!(
1817 a.compare_exchange(ptr::null_mut(), x, success, failure),
1818 Ok(ptr::null_mut()),
1819 );
1820 assert_eq!(a.load(Ordering::Relaxed), x as *mut _);
1821 assert_eq!(
1822 a.compare_exchange(ptr::null_mut(), ptr::null_mut(), success, failure),
1823 Err(x as *mut _),
1824 );
1825 assert_eq!(a.load(Ordering::Relaxed), x as *mut _);
1826 }
1827 }
1828 #[test]
1829 fn compare_exchange_weak() {
1830 let a = <$atomic_type>::new(ptr::null_mut());
1831 test_compare_exchange_ordering(&|success, failure| {
1832 let _ = a.compare_exchange_weak(ptr::null_mut(), ptr::null_mut(), success, failure);
1833 });
1834 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1835 let a = <$atomic_type>::new(ptr::null_mut());
1836 let x = &mut 1;
1837 assert_eq!(a.compare_exchange_weak(x, x, success, failure), Err(ptr::null_mut()));
1838 let mut old = a.load(Ordering::Relaxed);
1839 loop {
1840 match a.compare_exchange_weak(old, x, success, failure) {
1841 Ok(_) => break,
1842 Err(x) => old = x,
1843 }
1844 }
1845 assert_eq!(a.load(Ordering::Relaxed), x as *mut _);
1846 }
1847 }
1848 // https://github.com/rust-lang/rust/blob/1.84.0/library/core/tests/atomic.rs#L130-L213
1849 #[test]
1850 fn ptr_add_null() {
1851 let atom = AtomicPtr::<i64>::new(core::ptr::null_mut());
1852 assert_eq!(atom.fetch_ptr_add(1, Ordering::SeqCst).addr(), 0);
1853 assert_eq!(atom.load(Ordering::SeqCst).addr(), 8);
1854
1855 assert_eq!(atom.fetch_byte_add(1, Ordering::SeqCst).addr(), 8);
1856 assert_eq!(atom.load(Ordering::SeqCst).addr(), 9);
1857
1858 assert_eq!(atom.fetch_ptr_sub(1, Ordering::SeqCst).addr(), 9);
1859 assert_eq!(atom.load(Ordering::SeqCst).addr(), 1);
1860
1861 assert_eq!(atom.fetch_byte_sub(1, Ordering::SeqCst).addr(), 1);
1862 assert_eq!(atom.load(Ordering::SeqCst).addr(), 0);
1863 }
1864 #[test]
1865 fn ptr_add_data() {
1866 let num = 0i64;
1867 let n = &num as *const i64 as *mut _;
1868 let atom = AtomicPtr::<i64>::new(n);
1869 assert_eq!(atom.fetch_ptr_add(1, Ordering::SeqCst), n);
1870 assert_eq!(atom.load(Ordering::SeqCst), n.wrapping_add(1));
1871
1872 assert_eq!(atom.fetch_ptr_sub(1, Ordering::SeqCst), n.wrapping_add(1));
1873 assert_eq!(atom.load(Ordering::SeqCst), n);
1874 #[allow(clippy::cast_ptr_alignment)]
1875 let bytes_from_n = |b| n.cast::<u8>().wrapping_add(b).cast::<i64>();
1876
1877 assert_eq!(atom.fetch_byte_add(1, Ordering::SeqCst), n);
1878 assert_eq!(atom.load(Ordering::SeqCst), bytes_from_n(1));
1879
1880 assert_eq!(atom.fetch_byte_add(5, Ordering::SeqCst), bytes_from_n(1));
1881 assert_eq!(atom.load(Ordering::SeqCst), bytes_from_n(6));
1882
1883 assert_eq!(atom.fetch_byte_sub(1, Ordering::SeqCst), bytes_from_n(6));
1884 assert_eq!(atom.load(Ordering::SeqCst), bytes_from_n(5));
1885
1886 assert_eq!(atom.fetch_byte_sub(5, Ordering::SeqCst), bytes_from_n(5));
1887 assert_eq!(atom.load(Ordering::SeqCst), n);
1888 }
1889 #[test]
1890 fn ptr_bitops() {
1891 let atom = AtomicPtr::<i64>::new(core::ptr::null_mut());
1892 assert_eq!(atom.fetch_or(0b0111, Ordering::SeqCst).addr(), 0);
1893 assert_eq!(atom.load(Ordering::SeqCst).addr(), 0b0111);
1894
1895 assert_eq!(atom.fetch_and(0b1101, Ordering::SeqCst).addr(), 0b0111);
1896 assert_eq!(atom.load(Ordering::SeqCst).addr(), 0b0101);
1897
1898 assert_eq!(atom.fetch_xor(0b1111, Ordering::SeqCst).addr(), 0b0101);
1899 assert_eq!(atom.load(Ordering::SeqCst).addr(), 0b1010);
1900 }
1901 #[test]
1902 fn ptr_bitops_tagging() {
1903 const MASK_TAG: usize = 0b1111;
1904 const MASK_PTR: usize = !MASK_TAG;
1905
1906 #[repr(align(16))]
1907 struct Tagme(#[allow(dead_code)] u128);
1908
1909 let tagme = Tagme(1000);
1910 let ptr = &tagme as *const Tagme as *mut Tagme;
1911 let atom: AtomicPtr<Tagme> = AtomicPtr::new(ptr);
1912
1913 assert_eq!(ptr.addr() & MASK_TAG, 0);
1914
1915 assert_eq!(atom.fetch_or(0b0111, Ordering::SeqCst), ptr);
1916 assert_eq!(atom.load(Ordering::SeqCst), ptr.map_addr(|a| a | 0b111));
1917
1918 assert_eq!(
1919 atom.fetch_and(MASK_PTR | 0b0010, Ordering::SeqCst),
1920 ptr.map_addr(|a| a | 0b111)
1921 );
1922 assert_eq!(atom.load(Ordering::SeqCst), ptr.map_addr(|a| a | 0b0010));
1923
1924 assert_eq!(atom.fetch_xor(0b1011, Ordering::SeqCst), ptr.map_addr(|a| a | 0b0010));
1925 assert_eq!(atom.load(Ordering::SeqCst), ptr.map_addr(|a| a | 0b1001));
1926
1927 assert_eq!(atom.fetch_and(MASK_PTR, Ordering::SeqCst), ptr.map_addr(|a| a | 0b1001));
1928 assert_eq!(atom.load(Ordering::SeqCst), ptr);
1929 }
1930 #[test]
1931 fn bit_set() {
1932 let a = <$atomic_type>::new(ptr::null_mut::<u64>().cast::<u8>().map_addr(|a| a | 1));
1933 test_swap_ordering(&|order| assert!(a.bit_set(0, order)));
1934 for &order in &helper::SWAP_ORDERINGS {
1935 let pointer = &mut 1u64 as *mut u64 as *mut u8;
1936 let atom = <$atomic_type>::new(pointer);
1937 // Tag the bottom bit of the pointer.
1938 assert!(!atom.bit_set(0, order));
1939 // Extract and untag.
1940 let tagged = atom.load(Ordering::Relaxed);
1941 assert_eq!(tagged.addr() & 1, 1);
1942 assert_eq!(tagged.map_addr(|p| p & !1), pointer);
1943 }
1944 }
1945 #[test]
1946 fn bit_clear() {
1947 let a = <$atomic_type>::new(ptr::null_mut::<u64>().cast::<u8>());
1948 test_swap_ordering(&|order| assert!(!a.bit_clear(0, order)));
1949 for &order in &helper::SWAP_ORDERINGS {
1950 let pointer = &mut 1u64 as *mut u64 as *mut u8;
1951 // A tagged pointer
1952 let atom = <$atomic_type>::new(pointer.map_addr(|a| a | 1));
1953 assert!(atom.bit_set(0, order));
1954 // Untag
1955 assert!(atom.bit_clear(0, order));
1956 }
1957 }
1958 #[test]
1959 fn bit_toggle() {
1960 let a = <$atomic_type>::new(ptr::null_mut::<u64>().cast::<u8>());
1961 test_swap_ordering(&|order| {
1962 a.bit_toggle(0, order);
1963 });
1964 for &order in &helper::SWAP_ORDERINGS {
1965 let pointer = &mut 1u64 as *mut u64 as *mut u8;
1966 let atom = <$atomic_type>::new(pointer);
1967 // Toggle a tag bit on the pointer.
1968 atom.bit_toggle(0, order);
1969 assert_eq!(atom.load(Ordering::Relaxed).addr() & 1, 1);
1970 }
1971 }
1972 ::quickcheck::quickcheck! {
1973 fn quickcheck_swap(x: usize, y: usize) -> bool {
1974 let x = sptr::invalid_mut(x);
1975 let y = sptr::invalid_mut(y);
1976 for &order in &helper::SWAP_ORDERINGS {
1977 let a = <$atomic_type>::new(x);
1978 assert_eq!(a.swap(y, order), x);
1979 assert_eq!(a.swap(x, order), y);
1980 }
1981 true
1982 }
1983 fn quickcheck_compare_exchange(x: usize, y: usize) -> bool {
1984 let mut rng = fastrand::Rng::new();
1985 let z = loop {
1986 let z = rng.usize(..);
1987 if z != y {
1988 break z;
1989 }
1990 };
1991 let x = sptr::invalid_mut(x);
1992 let y = sptr::invalid_mut(y);
1993 let z = sptr::invalid_mut(z);
1994 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
1995 let a = <$atomic_type>::new(x);
1996 assert_eq!(a.compare_exchange(x, y, success, failure).unwrap(), x);
1997 assert_eq!(a.load(Ordering::Relaxed), y);
1998 assert_eq!(a.compare_exchange(z, x, success, failure).unwrap_err(), y);
1999 assert_eq!(a.load(Ordering::Relaxed), y);
2000 }
2001 true
2002 }
2003 fn quickcheck_fetch_byte_add(x: usize, y: usize) -> bool {
2004 let x = sptr::invalid_mut(x);
2005 let y = sptr::invalid_mut::<u8>(y);
2006 for &order in &helper::SWAP_ORDERINGS {
2007 let a = <$atomic_type>::new(x);
2008 assert_eq!(a.fetch_byte_add(y.addr(), order), x);
2009 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr().wrapping_add(y.addr()));
2010 let a = <$atomic_type>::new(y);
2011 assert_eq!(a.fetch_byte_add(x.addr(), order), y);
2012 assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr().wrapping_add(x.addr()));
2013 }
2014 true
2015 }
2016 fn quickcheck_fetch_byte_sub(x: usize, y: usize) -> bool {
2017 let x = sptr::invalid_mut(x);
2018 let y = sptr::invalid_mut::<u8>(y);
2019 for &order in &helper::SWAP_ORDERINGS {
2020 let a = <$atomic_type>::new(x);
2021 assert_eq!(a.fetch_byte_sub(y.addr(), order), x);
2022 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr().wrapping_sub(y.addr()));
2023 let a = <$atomic_type>::new(y);
2024 assert_eq!(a.fetch_byte_sub(x.addr(), order), y);
2025 assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr().wrapping_sub(x.addr()));
2026 }
2027 true
2028 }
2029 fn quickcheck_fetch_and(x: usize, y: usize) -> bool {
2030 let x = sptr::invalid_mut(x);
2031 let y = sptr::invalid_mut::<u8>(y);
2032 for &order in &helper::SWAP_ORDERINGS {
2033 let a = <$atomic_type>::new(x);
2034 assert_eq!(a.fetch_and(y.addr(), order), x);
2035 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() & y.addr());
2036 let a = <$atomic_type>::new(y);
2037 assert_eq!(a.fetch_and(x.addr(), order), y);
2038 assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr() & x.addr());
2039 }
2040 true
2041 }
2042 fn quickcheck_fetch_or(x: usize, y: usize) -> bool {
2043 let x = sptr::invalid_mut(x);
2044 let y = sptr::invalid_mut::<u8>(y);
2045 for &order in &helper::SWAP_ORDERINGS {
2046 let a = <$atomic_type>::new(x);
2047 assert_eq!(a.fetch_or(y.addr(), order), x);
2048 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() | y.addr());
2049 let a = <$atomic_type>::new(y);
2050 assert_eq!(a.fetch_or(x.addr(), order), y);
2051 assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr() | x.addr());
2052 }
2053 true
2054 }
2055 fn quickcheck_fetch_xor(x: usize, y: usize) -> bool {
2056 let x = sptr::invalid_mut(x);
2057 let y = sptr::invalid_mut::<u8>(y);
2058 for &order in &helper::SWAP_ORDERINGS {
2059 let a = <$atomic_type>::new(x);
2060 assert_eq!(a.fetch_xor(y.addr(), order), x);
2061 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() ^ y.addr());
2062 let a = <$atomic_type>::new(y);
2063 assert_eq!(a.fetch_xor(x.addr(), order), y);
2064 assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr() ^ x.addr());
2065 }
2066 true
2067 }
2068 fn quickcheck_bit_set(x: usize, bit: u32) -> bool {
2069 let x = sptr::invalid_mut(x);
2070 for &order in &helper::SWAP_ORDERINGS {
2071 let a = <$atomic_type>::new(x);
2072 let b = a.bit_set(bit, order);
2073 let mask = <usize>::wrapping_shl(1, bit);
2074 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() | mask);
2075 assert_eq!(b, x.addr() & mask != 0);
2076 }
2077 true
2078 }
2079 fn quickcheck_bit_clear(x: usize, bit: u32) -> bool {
2080 let x = sptr::invalid_mut(x);
2081 for &order in &helper::SWAP_ORDERINGS {
2082 let a = <$atomic_type>::new(x);
2083 let b = a.bit_clear(bit, order);
2084 let mask = <usize>::wrapping_shl(1, bit);
2085 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() & !mask);
2086 assert_eq!(b, x.addr() & mask != 0);
2087 }
2088 true
2089 }
2090 fn quickcheck_bit_toggle(x: usize, bit: u32) -> bool {
2091 let x = sptr::invalid_mut(x);
2092 for &order in &helper::SWAP_ORDERINGS {
2093 let a = <$atomic_type>::new(x);
2094 let b = a.bit_toggle(bit, order);
2095 let mask = <usize>::wrapping_shl(1, bit);
2096 assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() ^ mask);
2097 assert_eq!(b, x.addr() & mask != 0);
2098 }
2099 true
2100 }
2101 }
2102 };
2103 ($atomic_type:ty) => {
2104 __test_atomic_ptr!($atomic_type, single_thread);
2105 // TODO: multi thread
2106 };
2107 }
2108
2109 macro_rules! __test_atomic_int_pub {
2110 ($atomic_type:ty, $int_type:ident) => {
2111 __test_atomic_pub_common!($atomic_type, $int_type);
2112 use std::boxed::Box;
2113
2114 #[allow(unused_imports)]
2115 use sptr::Strict as _; // for old rustc
2116 #[test]
2117 fn fetch_update() {
2118 // TODO(gcc): failure
2119 if option_env!("RUSTC_CODEGEN_GCC").unwrap_or_default() == "1"
2120 && !cfg!(debug_assertions)
2121 {
2122 return;
2123 }
2124 // TODO(riscv): wrong result (as of Valgrind 3.26)
2125 #[cfg(valgrind)]
2126 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
2127 return;
2128 }
2129 let a = <$atomic_type>::new(7);
2130 #[cfg(valgrind)]
2131 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
2132 mark_aligned_defined(&a);
2133 }
2134 test_compare_exchange_ordering(&|set, fetch| {
2135 let _ = a.fetch_update(set, fetch, |x| Some(x));
2136 });
2137 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
2138 let a = <$atomic_type>::new(7);
2139 #[cfg(valgrind)]
2140 if IMP_EMU_SUB_WORD_CAS && mem::size_of::<$int_type>() <= 2 {
2141 mark_aligned_defined(&a);
2142 }
2143 assert_eq!(a.fetch_update(success, failure, |_| None), Err(7));
2144 assert_eq!(a.fetch_update(success, failure, |x| Some(x + 1)), Ok(7));
2145 assert_eq!(a.fetch_update(success, failure, |x| Some(x + 1)), Ok(8));
2146 assert_eq!(a.load(Ordering::SeqCst), 9);
2147 }
2148 }
2149 #[test]
2150 fn impls() {
2151 #[cfg(not(portable_atomic_no_const_transmute))]
2152 const INTO_INNER: $int_type = {
2153 let a = <$atomic_type>::new(10);
2154 a.into_inner()
2155 };
2156 #[cfg(not(portable_atomic_no_const_mut_refs))]
2157 const GET_MUT: $atomic_type = {
2158 let mut a = <$atomic_type>::new(10);
2159 let _ = unsafe { <$atomic_type>::from_ptr(a.as_ptr()) };
2160 *a.get_mut() = 5;
2161 a
2162 };
2163 #[cfg(not(portable_atomic_no_const_transmute))]
2164 {
2165 assert_eq!(INTO_INNER, 10);
2166 }
2167 #[cfg(not(portable_atomic_no_const_mut_refs))]
2168 {
2169 assert_eq!(GET_MUT.into_inner(), 5);
2170 }
2171 let a = <$atomic_type>::default();
2172 let b = <$atomic_type>::from(0);
2173 assert_eq!(a.load(Ordering::SeqCst), b.load(Ordering::SeqCst));
2174 assert_eq!(std::format!("{:?}", a), std::format!("{:?}", a.load(Ordering::SeqCst)));
2175 assert_eq!(a.into_inner(), 0);
2176 assert_eq!(b.into_inner(), 0);
2177
2178 unsafe {
2179 let ptr: *mut Align16<$int_type> = Box::into_raw(Box::new(Align16(0)));
2180 assert!(ptr.addr() % mem::align_of::<$atomic_type>() == 0);
2181 {
2182 let a = <$atomic_type>::from_ptr(ptr.cast::<$int_type>());
2183 *a.as_ptr() = 1;
2184 }
2185 assert_eq!((*ptr).0, 1);
2186 drop(Box::from_raw(ptr));
2187 }
2188 }
2189 ::quickcheck::quickcheck! {
2190 fn quickcheck_fetch_update(x: $int_type, y: $int_type) -> bool {
2191 // TODO(riscv): wrong result (as of Valgrind 3.26)
2192 #[cfg(valgrind)]
2193 if cfg!(target_arch = "riscv64") && mem::size_of::<$int_type>() <= 2 {
2194 return true;
2195 }
2196 let mut rng = fastrand::Rng::new();
2197 let z = loop {
2198 let z = rng.$int_type(..);
2199 if z != y {
2200 break z;
2201 }
2202 };
2203 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
2204 let a = <$atomic_type>::new(x);
2205 assert_eq!(
2206 a.fetch_update(success, failure, |_| Some(y))
2207 .unwrap(),
2208 x
2209 );
2210 assert_eq!(
2211 a.fetch_update(success, failure, |_| Some(z))
2212 .unwrap(),
2213 y
2214 );
2215 assert_eq!(a.load(Ordering::Relaxed), z);
2216 assert_eq!(
2217 a.fetch_update(success, failure, |z| if z == y { Some(z) } else { None })
2218 .unwrap_err(),
2219 z
2220 );
2221 assert_eq!(a.load(Ordering::Relaxed), z);
2222 }
2223 true
2224 }
2225 }
2226 };
2227 }
2228 macro_rules! __test_atomic_float_pub {
2229 ($atomic_type:ty, $float_type:ident) => {
2230 __test_atomic_pub_common!($atomic_type, $float_type);
2231 use std::boxed::Box;
2232
2233 #[allow(unused_imports)]
2234 use sptr::Strict as _; // for old rustc
2235 #[test]
2236 fn fetch_update() {
2237 let a = <$atomic_type>::new(7.);
2238 test_compare_exchange_ordering(&|set, fetch| {
2239 let _ = a.fetch_update(set, fetch, |x| Some(x));
2240 });
2241 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
2242 let a = <$atomic_type>::new(7.);
2243 assert_eq!(a.fetch_update(success, failure, |_| None), Err(7.));
2244 assert_eq!(a.fetch_update(success, failure, |x| Some(x + 1.)), Ok(7.));
2245 assert_eq!(a.fetch_update(success, failure, |x| Some(x + 1.)), Ok(8.));
2246 assert_eq!(a.load(Ordering::SeqCst), 9.);
2247 }
2248 }
2249 #[test]
2250 fn impls() {
2251 #[cfg(not(portable_atomic_no_const_transmute))]
2252 const INTO_INNER: $float_type = {
2253 let a = <$atomic_type>::new(10.);
2254 a.into_inner()
2255 };
2256 #[cfg(not(portable_atomic_no_const_mut_refs))]
2257 const GET_MUT: $atomic_type = {
2258 let mut a = <$atomic_type>::new(10.);
2259 let _ = unsafe { <$atomic_type>::from_ptr(a.as_ptr()) };
2260 *a.get_mut() = 5.;
2261 a
2262 };
2263 #[cfg(not(portable_atomic_no_const_transmute))]
2264 {
2265 assert_eq!(INTO_INNER, 10.);
2266 }
2267 #[cfg(not(portable_atomic_no_const_mut_refs))]
2268 {
2269 assert_eq!(GET_MUT.into_inner(), 5.);
2270 }
2271 let a = <$atomic_type>::default();
2272 let b = <$atomic_type>::from(0.);
2273 assert_eq!(a.load(Ordering::SeqCst), b.load(Ordering::SeqCst));
2274 assert_eq!(std::format!("{:?}", a), std::format!("{:?}", a.load(Ordering::SeqCst)));
2275 assert_eq!(a.into_inner(), 0.);
2276 assert_eq!(b.into_inner(), 0.);
2277
2278 unsafe {
2279 let ptr: *mut Align16<$float_type> = Box::into_raw(Box::new(Align16(0.)));
2280 assert!(ptr.addr() % mem::align_of::<$atomic_type>() == 0);
2281 {
2282 let a = <$atomic_type>::from_ptr(ptr.cast::<$float_type>());
2283 *a.as_ptr() = 1.;
2284 }
2285 assert_eq!((*ptr).0, 1.);
2286 drop(Box::from_raw(ptr));
2287 }
2288 }
2289 };
2290 }
2291 macro_rules! __test_atomic_bool_pub {
2292 ($atomic_type:ty) => {
2293 __test_atomic_pub_common!($atomic_type, bool);
2294 use std::boxed::Box;
2295
2296 #[allow(unused_imports)]
2297 use sptr::Strict as _; // for old rustc
2298 #[test]
2299 fn fetch_nand() {
2300 let a = <$atomic_type>::new(true);
2301 test_swap_ordering(&|order| assert_eq!(a.fetch_nand(false, order), true));
2302 for &order in &helper::SWAP_ORDERINGS {
2303 let a = <$atomic_type>::new(true);
2304 assert_eq!(a.fetch_nand(false, order), true);
2305 assert_eq!(a.load(Ordering::Relaxed), true);
2306 let a = <$atomic_type>::new(true);
2307 assert_eq!(a.fetch_nand(true, order), true);
2308 assert_eq!(a.load(Ordering::Relaxed) as usize, 0);
2309 assert_eq!(a.load(Ordering::Relaxed), false);
2310 let a = <$atomic_type>::new(false);
2311 assert_eq!(a.fetch_nand(false, order), false);
2312 assert_eq!(a.load(Ordering::Relaxed), true);
2313 let a = <$atomic_type>::new(false);
2314 assert_eq!(a.fetch_nand(true, order), false);
2315 assert_eq!(a.load(Ordering::Relaxed), true);
2316 }
2317 }
2318 #[test]
2319 fn fetch_not() {
2320 let a = <$atomic_type>::new(true);
2321 test_swap_ordering(&|order| {
2322 a.fetch_not(order);
2323 });
2324 for &order in &helper::SWAP_ORDERINGS {
2325 let a = <$atomic_type>::new(true);
2326 assert_eq!(a.fetch_not(order), true);
2327 assert_eq!(a.load(Ordering::Relaxed), false);
2328 let a = <$atomic_type>::new(false);
2329 assert_eq!(a.fetch_not(order), false);
2330 assert_eq!(a.load(Ordering::Relaxed), true);
2331 }
2332 }
2333 #[test]
2334 fn not() {
2335 let a = <$atomic_type>::new(true);
2336 test_swap_ordering(&|order| a.not(order));
2337 for &order in &helper::SWAP_ORDERINGS {
2338 let a = <$atomic_type>::new(true);
2339 a.not(order);
2340 assert_eq!(a.load(Ordering::Relaxed), false);
2341 let a = <$atomic_type>::new(false);
2342 a.not(order);
2343 assert_eq!(a.load(Ordering::Relaxed), true);
2344 }
2345 }
2346 #[test]
2347 fn fetch_update() {
2348 // TODO(gcc): failure
2349 if option_env!("RUSTC_CODEGEN_GCC").unwrap_or_default() == "1"
2350 && !cfg!(debug_assertions)
2351 {
2352 return;
2353 }
2354 let a = <$atomic_type>::new(false);
2355 #[cfg(valgrind)]
2356 if IMP_EMU_SUB_WORD_CAS {
2357 mark_aligned_defined(&a);
2358 }
2359 test_compare_exchange_ordering(&|set, fetch| {
2360 let _ = a.fetch_update(set, fetch, |x| Some(x));
2361 });
2362 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
2363 let a = <$atomic_type>::new(false);
2364 #[cfg(valgrind)]
2365 if IMP_EMU_SUB_WORD_CAS {
2366 mark_aligned_defined(&a);
2367 }
2368 assert_eq!(a.fetch_update(success, failure, |_| None), Err(false));
2369 assert_eq!(a.fetch_update(success, failure, |x| Some(!x)), Ok(false));
2370 assert_eq!(a.fetch_update(success, failure, |x| Some(!x)), Ok(true));
2371 assert_eq!(a.load(Ordering::SeqCst), false);
2372 }
2373 }
2374 #[test]
2375 fn impls() {
2376 #[cfg(not(portable_atomic_no_const_transmute))]
2377 const INTO_INNER: bool = {
2378 let a = <$atomic_type>::new(true);
2379 a.into_inner()
2380 };
2381 #[cfg(not(portable_atomic_no_const_mut_refs))]
2382 const GET_MUT: $atomic_type = {
2383 let mut a = <$atomic_type>::new(true);
2384 let _ = unsafe { <$atomic_type>::from_ptr(a.as_ptr()) };
2385 *a.get_mut() = false;
2386 a
2387 };
2388 #[cfg(not(portable_atomic_no_const_transmute))]
2389 {
2390 assert_eq!(INTO_INNER, true);
2391 }
2392 #[cfg(not(portable_atomic_no_const_mut_refs))]
2393 {
2394 assert_eq!(GET_MUT.into_inner(), false);
2395 }
2396 let a = <$atomic_type>::default();
2397 let b = <$atomic_type>::from(false);
2398 assert_eq!(a.load(Ordering::SeqCst), b.load(Ordering::SeqCst));
2399 assert_eq!(std::format!("{:?}", a), std::format!("{:?}", a.load(Ordering::SeqCst)));
2400 assert_eq!(a.into_inner(), false);
2401 assert_eq!(b.into_inner(), false);
2402
2403 unsafe {
2404 let ptr: *mut bool = Box::into_raw(Box::new(false));
2405 assert!(ptr.addr() % mem::align_of::<$atomic_type>() == 0);
2406 {
2407 let a = <$atomic_type>::from_ptr(ptr);
2408 *a.as_ptr() = true;
2409 }
2410 assert_eq!((*ptr), true);
2411 drop(Box::from_raw(ptr));
2412 }
2413 }
2414 };
2415 }
2416 macro_rules! __test_atomic_ptr_pub {
2417 ($atomic_type:ty) => {
2418 __test_atomic_pub_common!($atomic_type, *mut u8);
2419 use std::boxed::Box;
2420 #[test]
2421 fn fetch_update() {
2422 // TODO(gcc): failure
2423 if option_env!("RUSTC_CODEGEN_GCC").unwrap_or_default() == "1"
2424 && !cfg!(debug_assertions)
2425 {
2426 return;
2427 }
2428 let a = <$atomic_type>::new(ptr::null_mut());
2429 test_compare_exchange_ordering(&|set, fetch| {
2430 let _ = a.fetch_update(set, fetch, |x| Some(x));
2431 });
2432 for &(success, failure) in &helper::COMPARE_EXCHANGE_ORDERINGS {
2433 let a = <$atomic_type>::new(ptr::null_mut());
2434 assert_eq!(a.fetch_update(success, failure, |_| None), Err(ptr::null_mut()));
2435 assert_eq!(
2436 a.fetch_update(success, failure, |_| Some(&a as *const _ as *mut _)),
2437 Ok(ptr::null_mut())
2438 );
2439 assert_eq!(a.load(Ordering::SeqCst), &a as *const _ as *mut _);
2440 }
2441 }
2442 #[test]
2443 fn impls() {
2444 #[cfg(not(portable_atomic_no_const_transmute))]
2445 const INTO_INNER: *mut u8 = {
2446 let a = <$atomic_type>::new(ptr::null_mut());
2447 a.into_inner()
2448 };
2449 #[cfg(not(portable_atomic_no_const_mut_refs))]
2450 const GET_MUT: $atomic_type = {
2451 let mut a = <$atomic_type>::new(ptr::null_mut());
2452 let _ = unsafe { <$atomic_type>::from_ptr(a.as_ptr()) };
2453 *a.get_mut() = ptr::null_mut::<u8>().wrapping_add(1);
2454 a
2455 };
2456 #[cfg(not(portable_atomic_no_const_transmute))]
2457 {
2458 assert!(INTO_INNER.is_null());
2459 }
2460 #[cfg(not(portable_atomic_no_const_mut_refs))]
2461 {
2462 assert_eq!(GET_MUT.into_inner(), ptr::null_mut::<u8>().wrapping_add(1));
2463 }
2464 let a = <$atomic_type>::default();
2465 let b = <$atomic_type>::from(ptr::null_mut());
2466 assert_eq!(a.load(Ordering::SeqCst), b.load(Ordering::SeqCst));
2467 assert_eq!(std::format!("{:?}", a), std::format!("{:?}", a.load(Ordering::SeqCst)));
2468 assert_eq!(std::format!("{:p}", a), std::format!("{:p}", a.load(Ordering::SeqCst)));
2469 assert_eq!(a.into_inner(), ptr::null_mut());
2470 assert_eq!(b.into_inner(), ptr::null_mut());
2471
2472 unsafe {
2473 let ptr: *mut Align16<*mut u8> = Box::into_raw(Box::new(Align16(ptr::null_mut())));
2474 assert!(ptr.addr() % mem::align_of::<$atomic_type>() == 0);
2475 {
2476 let a = <$atomic_type>::from_ptr(ptr.cast::<*mut u8>());
2477 *a.as_ptr() = ptr::null_mut::<u8>().wrapping_add(1);
2478 }
2479 assert_eq!((*ptr).0, ptr::null_mut::<u8>().wrapping_add(1));
2480 drop(Box::from_raw(ptr));
2481 }
2482 }
2483 };
2484 }
2485
2486 macro_rules! test_atomic_int_load_store {
2487 ($int_type:ident) => {
2488 paste::paste! {
2489 #[allow(
2490 clippy::alloc_instead_of_core,
2491 clippy::std_instead_of_alloc,
2492 clippy::std_instead_of_core,
2493 clippy::undocumented_unsafe_blocks
2494 )]
2495 mod [<test_atomic_ $int_type>] {
2496 use super::*;
2497 __test_atomic_int_load_store!([<Atomic $int_type:camel>], $int_type);
2498 }
2499 }
2500 };
2501 }
2502 macro_rules! test_atomic_ptr_load_store {
2503 () => {
2504 #[allow(
2505 clippy::alloc_instead_of_core,
2506 clippy::std_instead_of_alloc,
2507 clippy::std_instead_of_core,
2508 clippy::undocumented_unsafe_blocks
2509 )]
2510 mod test_atomic_ptr {
2511 use super::*;
2512 __test_atomic_ptr_load_store!(AtomicPtr<u8>);
2513 }
2514 };
2515 }
2516
2517 macro_rules! test_atomic_int_single_thread {
2518 ($int_type:ident) => {
2519 paste::paste! {
2520 #[allow(
2521 clippy::alloc_instead_of_core,
2522 clippy::std_instead_of_alloc,
2523 clippy::std_instead_of_core,
2524 clippy::undocumented_unsafe_blocks
2525 )]
2526 mod [<test_atomic_ $int_type>] {
2527 use super::*;
2528 __test_atomic_int_load_store!([<Atomic $int_type:camel>], $int_type, single_thread);
2529 __test_atomic_int!([<Atomic $int_type:camel>], $int_type, single_thread);
2530 }
2531 }
2532 };
2533 }
2534 macro_rules! test_atomic_ptr_single_thread {
2535 () => {
2536 #[allow(
2537 clippy::alloc_instead_of_core,
2538 clippy::std_instead_of_alloc,
2539 clippy::std_instead_of_core,
2540 clippy::undocumented_unsafe_blocks
2541 )]
2542 mod test_atomic_ptr {
2543 use super::*;
2544 __test_atomic_ptr_load_store!(AtomicPtr<u8>, single_thread);
2545 __test_atomic_ptr!(AtomicPtr<u8>, single_thread);
2546 }
2547 };
2548 }
2549
2550 macro_rules! test_atomic_int {
2551 ($int_type:ident) => {
2552 paste::paste! {
2553 #[allow(
2554 clippy::alloc_instead_of_core,
2555 clippy::std_instead_of_alloc,
2556 clippy::std_instead_of_core,
2557 clippy::undocumented_unsafe_blocks
2558 )]
2559 mod [<test_atomic_ $int_type>] {
2560 use super::*;
2561 __test_atomic_int_load_store!([<Atomic $int_type:camel>], $int_type);
2562 __test_atomic_int!([<Atomic $int_type:camel>], $int_type);
2563 }
2564 }
2565 };
2566 }
2567 macro_rules! test_atomic_ptr {
2568 () => {
2569 #[allow(
2570 clippy::alloc_instead_of_core,
2571 clippy::std_instead_of_alloc,
2572 clippy::std_instead_of_core,
2573 clippy::undocumented_unsafe_blocks
2574 )]
2575 #[allow(unstable_name_collisions)] // for sptr crate
2576 mod test_atomic_ptr {
2577 use super::*;
2578 __test_atomic_ptr_load_store!(AtomicPtr<u8>);
2579 __test_atomic_ptr!(AtomicPtr<u8>);
2580 }
2581 };
2582 }
2583
2584 macro_rules! test_atomic_int_pub {
2585 ($int_type:ident) => {
2586 paste::paste! {
2587 #[allow(
2588 clippy::alloc_instead_of_core,
2589 clippy::std_instead_of_alloc,
2590 clippy::std_instead_of_core,
2591 clippy::undocumented_unsafe_blocks
2592 )]
2593 mod [<test_atomic_ $int_type>] {
2594 use super::*;
2595 __test_atomic_int_load_store!([<Atomic $int_type:camel>], $int_type);
2596 __test_atomic_int!([<Atomic $int_type:camel>], $int_type);
2597 __test_atomic_int_pub!([<Atomic $int_type:camel>], $int_type);
2598 }
2599 }
2600 };
2601 }
2602 #[cfg(feature = "float")]
2603 macro_rules! test_atomic_float_pub {
2604 ($float_type:ident) => {
2605 paste::paste! {
2606 #[allow(
2607 clippy::alloc_instead_of_core,
2608 clippy::float_arithmetic,
2609 clippy::std_instead_of_alloc,
2610 clippy::std_instead_of_core,
2611 clippy::undocumented_unsafe_blocks
2612 )]
2613 mod [<test_atomic_ $float_type>] {
2614 use super::*;
2615 __test_atomic_float_load_store!([<Atomic $float_type:camel>], $float_type);
2616 __test_atomic_float!([<Atomic $float_type:camel>], $float_type);
2617 __test_atomic_float_pub!([<Atomic $float_type:camel>], $float_type);
2618 }
2619 }
2620 };
2621 }
2622 macro_rules! test_atomic_bool_pub {
2623 () => {
2624 #[allow(
2625 clippy::alloc_instead_of_core,
2626 clippy::std_instead_of_alloc,
2627 clippy::std_instead_of_core,
2628 clippy::undocumented_unsafe_blocks
2629 )]
2630 mod test_atomic_bool {
2631 use super::*;
2632 __test_atomic_bool_load_store!(AtomicBool);
2633 __test_atomic_bool!(AtomicBool);
2634 __test_atomic_bool_pub!(AtomicBool);
2635 }
2636 };
2637 }
2638 macro_rules! test_atomic_ptr_pub {
2639 () => {
2640 #[allow(
2641 clippy::alloc_instead_of_core,
2642 clippy::std_instead_of_alloc,
2643 clippy::std_instead_of_core,
2644 clippy::undocumented_unsafe_blocks
2645 )]
2646 #[allow(unstable_name_collisions)] // for sptr crate
2647 mod test_atomic_ptr {
2648 use super::*;
2649 __test_atomic_ptr_load_store!(AtomicPtr<u8>);
2650 __test_atomic_ptr!(AtomicPtr<u8>);
2651 __test_atomic_ptr_pub!(AtomicPtr<u8>);
2652 }
2653 };
2654 }
2655
2656 // Asserts that `$a` and `$b` have performed equivalent operations.
2657 #[cfg(feature = "float")]
2658 macro_rules! assert_float_op_eq {
2659 ($a:expr, $b:expr $(,)?) => {{
2660 // See also:
2661 // - https://github.com/rust-lang/unsafe-code-guidelines/issues/237.
2662 // - https://github.com/rust-lang/portable-simd/issues/39.
2663 let a = $a;
2664 let b = $b;
2665 if a.is_nan() && b.is_nan() // don't check sign of NaN: https://github.com/rust-lang/rust/issues/55131
2666 || a.is_infinite()
2667 && b.is_infinite()
2668 && a.is_sign_positive() == b.is_sign_positive()
2669 && a.is_sign_negative() == b.is_sign_negative()
2670 {
2671 // ok
2672 } else {
2673 assert_eq!(a, b);
2674 }
2675 }};
2676 }
2677
2678 #[allow(unused_unsafe)] // for old rustc
2679 fn assert_panic(f: &dyn Fn()) -> std::string::String {
2680 let backtrace = std::env::var_os("RUST_BACKTRACE");
2681 let hook = std::panic::take_hook();
2682 // set_var/remove_var is fine as we run tests with RUST_TEST_THREADS=1
2683 // std::panic::set_backtrace_style is better way here, but is unstable.
2684 unsafe { std::env::set_var("RUST_BACKTRACE", "0") } // Suppress backtrace
2685 std::panic::set_hook(std::boxed::Box::new(|_| {})); // Suppress panic msg
2686 let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
2687 std::panic::set_hook(hook);
2688 match backtrace {
2689 Some(v) => unsafe { std::env::set_var("RUST_BACKTRACE", v) },
2690 None => unsafe { std::env::remove_var("RUST_BACKTRACE") },
2691 }
2692 let msg = res.unwrap_err();
2693 msg.downcast_ref::<std::string::String>()
2694 .cloned()
2695 .unwrap_or_else(|| msg.downcast_ref::<&'static str>().copied().unwrap().into())
2696 }
2697 pub(crate) const LOAD_ORDERINGS: [Ordering; 3] =
2698 [Ordering::Relaxed, Ordering::Acquire, Ordering::SeqCst];
2699 pub(crate) fn rand_load_ordering(rng: &mut fastrand::Rng) -> Ordering {
2700 LOAD_ORDERINGS[rng.usize(0..LOAD_ORDERINGS.len())]
2701 }
2702 pub(crate) fn test_load_ordering(f: &dyn Fn(Ordering)) {
2703 for &order in &LOAD_ORDERINGS {
2704 f(order);
2705 }
2706 if !skip_should_panic_test() {
2707 assert_eq!(
2708 assert_panic(&|| f(Ordering::Release)),
2709 "there is no such thing as a release load"
2710 );
2711 assert_eq!(
2712 assert_panic(&|| f(Ordering::AcqRel)),
2713 "there is no such thing as an acquire-release load"
2714 );
2715 }
2716 }
2717 pub(crate) const STORE_ORDERINGS: [Ordering; 3] =
2718 [Ordering::Relaxed, Ordering::Release, Ordering::SeqCst];
2719 pub(crate) fn rand_store_ordering(rng: &mut fastrand::Rng) -> Ordering {
2720 STORE_ORDERINGS[rng.usize(0..STORE_ORDERINGS.len())]
2721 }
2722 pub(crate) fn test_store_ordering(f: &dyn Fn(Ordering)) {
2723 for &order in &STORE_ORDERINGS {
2724 f(order);
2725 }
2726 if !skip_should_panic_test() {
2727 assert_eq!(
2728 assert_panic(&|| f(Ordering::Acquire)),
2729 "there is no such thing as an acquire store"
2730 );
2731 assert_eq!(
2732 assert_panic(&|| f(Ordering::AcqRel)),
2733 "there is no such thing as an acquire-release store"
2734 );
2735 }
2736 }
2737 pub(crate) const SWAP_ORDERINGS: [Ordering; 5] =
2738 [Ordering::Relaxed, Ordering::Release, Ordering::Acquire, Ordering::AcqRel, Ordering::SeqCst];
2739 pub(crate) fn rand_swap_ordering(rng: &mut fastrand::Rng) -> Ordering {
2740 SWAP_ORDERINGS[rng.usize(0..SWAP_ORDERINGS.len())]
2741 }
2742 pub(crate) fn test_swap_ordering(f: &dyn Fn(Ordering)) {
2743 for &order in &SWAP_ORDERINGS {
2744 f(order);
2745 }
2746 }
2747 pub(crate) const COMPARE_EXCHANGE_ORDERINGS: [(Ordering, Ordering); 15] = [
2748 (Ordering::Relaxed, Ordering::Relaxed),
2749 (Ordering::Relaxed, Ordering::Acquire),
2750 (Ordering::Relaxed, Ordering::SeqCst),
2751 (Ordering::Acquire, Ordering::Relaxed),
2752 (Ordering::Acquire, Ordering::Acquire),
2753 (Ordering::Acquire, Ordering::SeqCst),
2754 (Ordering::Release, Ordering::Relaxed),
2755 (Ordering::Release, Ordering::Acquire),
2756 (Ordering::Release, Ordering::SeqCst),
2757 (Ordering::AcqRel, Ordering::Relaxed),
2758 (Ordering::AcqRel, Ordering::Acquire),
2759 (Ordering::AcqRel, Ordering::SeqCst),
2760 (Ordering::SeqCst, Ordering::Relaxed),
2761 (Ordering::SeqCst, Ordering::Acquire),
2762 (Ordering::SeqCst, Ordering::SeqCst),
2763 ];
2764 pub(crate) fn rand_compare_exchange_ordering(rng: &mut fastrand::Rng) -> (Ordering, Ordering) {
2765 COMPARE_EXCHANGE_ORDERINGS[rng.usize(0..COMPARE_EXCHANGE_ORDERINGS.len())]
2766 }
2767 pub(crate) fn test_compare_exchange_ordering(f: &dyn Fn(Ordering, Ordering)) {
2768 for &(success, failure) in &COMPARE_EXCHANGE_ORDERINGS {
2769 f(success, failure);
2770 }
2771 if !skip_should_panic_test() {
2772 for &order in &SWAP_ORDERINGS {
2773 let msg = assert_panic(&|| f(order, Ordering::AcqRel));
2774 assert!(
2775 msg == "there is no such thing as an acquire-release failure ordering"
2776 || msg == "there is no such thing as an acquire-release load",
2777 "{}",
2778 msg
2779 );
2780 let msg = assert_panic(&|| f(order, Ordering::Release));
2781 assert!(
2782 msg == "there is no such thing as a release failure ordering"
2783 || msg == "there is no such thing as a release load",
2784 "{}",
2785 msg
2786 );
2787 }
2788 }
2789 }
2790
2791 // Used in stress tests to find atomicity bug generated by __test_atomic_* macro.
2792 pub(crate) fn stress_test_config(rng: &mut fastrand::Rng) -> (usize, usize) {
2793 let iterations = if cfg!(miri) {
2794 50
2795 } else if cfg!(any(debug_assertions, qemu, valgrind)) {
2796 5_000
2797 } else {
2798 25_000
2799 };
2800 let threads = if cfg!(any(debug_assertions, valgrind)) { 2 } else { rng.usize(2..=8) };
2801 eprintln!("threads={}", threads);
2802 (iterations, threads)
2803 }
2804
2805 // true if the current implementation implements sub-word atomics using word-size CAS.
2806 #[cfg(valgrind)]
2807 pub(crate) const IMP_EMU_SUB_WORD_CAS: bool = cfg!(target_arch = "s390x");
2808
2809 #[cfg(valgrind)]
2810 #[inline(always)]
2811 pub(crate) fn mark_aligned_defined<T: ?Sized>(a: &T) {
2812 test_helper::valgrind::make_aligned_mem_defined::<T, 4>(a);
2813 }
2814
2815 fn skip_should_panic_test() -> bool {
2816 // Miri's panic handling is slow
2817 // MSAN false positive: https://gist.github.com/taiki-e/dd6269a8ffec46284fdc764a4849f884
2818 // Valgrind false positive on s390x
2819 is_panic_abort()
2820 || cfg!(miri)
2821 || option_env!("CARGO_PROFILE_RELEASE_LTO").map_or(false, |v| v == "fat")
2822 && build_context::SANITIZE.contains("memory")
2823 || cfg!(all(valgrind, target_arch = "s390x"))
2824 }
2825
2826 // For -C panic=abort -Z panic_abort_tests: https://github.com/rust-lang/rust/issues/67650
2827 fn is_panic_abort() -> bool {
2828 !matches!(build_context::PANIC, "unwind" | "") // cfg(panic) requires Rust 1.60
2829 }
2830
2831 #[repr(C, align(16))]
2832 pub(crate) struct Align16<T>(pub(crate) T);
2833
2834 // Stress tests to find memory ordering bug.
2835 // Note that this is not exhaustive and only tests a few cases.
2836 macro_rules! __stress_acquire_release {
2837 (should_pass,
2838 $int_type:ident, $read:ident, $write:ident, $load_order:ident, $store_order:ident
2839 ) => {
2840 paste::paste! {
2841 #[test]
2842 #[cfg_attr(all(any(debug_assertions, valgrind_cross), not(miri)), ignore = "slow in some environments")] // debug mode is slow.
2843 #[allow(
2844 clippy::cast_possible_wrap,
2845 clippy::cast_sign_loss,
2846 clippy::cast_possible_truncation,
2847 )]
2848 fn [<acquire_release1 _ $read _ $load_order:lower _ $write _ $store_order:lower>]() {
2849 __call_stress_test_fn!(acquire_release1, [<Atomic $int_type:camel>],
2850 $int_type, $read, $write, $load_order, $store_order);
2851 }
2852 }
2853 };
2854 (can_panic,
2855 $int_type:ident, $read:ident, $write:ident, $load_order:ident, $store_order:ident
2856 ) => {
2857 paste::paste! {
2858 // Currently, to make this test work well enough outside of Miri, tens of thousands
2859 // of iterations are needed, but this test is slow in some environments.
2860 // So, ignore on non-Miri environments by default. See also catch_unwind_on_weak_memory_arch.
2861 #[test]
2862 #[cfg_attr(not(miri), ignore = "slow in some environments")]
2863 #[allow(
2864 clippy::cast_possible_wrap,
2865 clippy::cast_sign_loss,
2866 clippy::cast_possible_truncation,
2867 )]
2868 fn [<acquire_release1 _ $read _ $load_order:lower _ $write _ $store_order:lower>]() {
2869 use crate::tests::helper::catch_unwind_on_weak_memory_arch as can_panic;
2870 can_panic("a=", test_helper::function_name!(), || {
2871 __call_stress_test_fn!(acquire_release1, [<Atomic $int_type:camel>],
2872 $int_type, $read, $write, $load_order, $store_order);
2873 });
2874 }
2875 }
2876 };
2877 }
2878 macro_rules! __stress_seqcst {
2879 (should_pass,
2880 $int_type:ident, $read:ident, $write:ident, $load_order:ident, $store_order:ident
2881 ) => {
2882 paste::paste! {
2883 // Currently, to make this test work well enough outside of Miri, tens of thousands
2884 // of iterations are needed, but this test is slow in some environments.
2885 // So, ignore on QEMU by default.
2886 #[test]
2887 #[cfg_attr(any(all(any(debug_assertions, valgrind_cross), not(miri)), qemu), ignore = "slow in some environments")] // debug mode is slow.
2888 #[allow(
2889 clippy::cast_possible_wrap,
2890 clippy::cast_sign_loss,
2891 clippy::cast_possible_truncation,
2892 )]
2893 fn [<seqcst1 _ $read _ $load_order:lower _ $write _ $store_order:lower>]() {
2894 __call_stress_test_fn!(seqcst1, [<Atomic $int_type:camel>],
2895 $int_type, $read, $write, $load_order, $store_order);
2896 }
2897 }
2898 };
2899 (can_panic,
2900 $int_type:ident, $read:ident, $write:ident, $load_order:ident, $store_order:ident
2901 ) => {
2902 paste::paste! {
2903 // Currently, to make this test work well enough outside of Miri, tens of thousands
2904 // of iterations are needed, but this test is slow in some environments.
2905 // So, ignore on non-Miri environments by default. See also catch_unwind_on_non_seqcst_arch.
2906 #[test]
2907 #[cfg_attr(not(miri), ignore = "slow in some environments")]
2908 #[allow(
2909 clippy::cast_possible_wrap,
2910 clippy::cast_sign_loss,
2911 clippy::cast_possible_truncation,
2912 )]
2913 fn [<seqcst1 _ $read _ $load_order:lower _ $write _ $store_order:lower>]() {
2914 use crate::tests::helper::catch_unwind_on_non_seqcst_arch as can_panic;
2915 can_panic("c=2", test_helper::function_name!(), || {
2916 __call_stress_test_fn!(seqcst1, [<Atomic $int_type:camel>],
2917 $int_type, $read, $write, $load_order, $store_order);
2918 });
2919 }
2920 }
2921 };
2922 }
2923 // To test this working correctly:
2924 // - Uncomment can_panic case in __stress_seqcst,
2925 // and run test with `--release -- acquire_release1 --ignored`.
2926 // At least on AArch64 machine, some "panicked as expected" will be shown.
2927 pub(crate) fn acquire_release1<T: Send + Sync>(
2928 min: isize,
2929 max: isize,
2930 new: impl Fn(isize) -> T,
2931 load: impl Fn(&T) -> isize + Send + Sync,
2932 store: impl Fn(&T, isize) + Send + Sync,
2933 ) {
2934 let n: usize = if cfg!(miri) {
2935 10
2936 } else if cfg!(any(qemu, valgrind)) {
2937 5_000
2938 } else {
2939 50_000
2940 };
2941
2942 let mut start: isize = 0;
2943 #[allow(clippy::cast_possible_wrap)]
2944 let mut end: isize = n as isize;
2945 // This test is relatively fast because it spawns only one thread, but
2946 // the iterations are limited to a maximum value of integers.
2947 if max < end {
2948 start = min;
2949 end = max;
2950 }
2951 assert!((start..end).len() <= n, "{}", (start..end).len());
2952 let a = &new(start);
2953 let b = &AtomicIsize::new(start);
2954 let a_ready = &AtomicUsize::new(0);
2955 let b_ready = &AtomicUsize::new(0);
2956 thread::scope(|s| {
2957 s.spawn(|_| {
2958 while a_ready.load(Ordering::Relaxed) == 0 {}
2959 b_ready.store(1, Ordering::Relaxed);
2960 for i in start..end {
2961 b.store(i, Ordering::Relaxed);
2962 store(a, i);
2963 }
2964 });
2965 let end = end - 1;
2966 a_ready.store(1, Ordering::Relaxed);
2967 while b_ready.load(Ordering::Relaxed) == 0 {}
2968 loop {
2969 let a = load(a);
2970 let b = b.load(Ordering::Relaxed);
2971 assert!(a <= b, "a={},b={}", a, b);
2972 if a == end && b == end {
2973 break;
2974 }
2975 }
2976 })
2977 .unwrap();
2978 }
2979 // To test this working correctly:
2980 // - Uncomment can_panic case in __stress_seqcst,
2981 // and run test with `--release -- seqcst1 --ignored`.
2982 // At least on AArch64 machine, some "panicked as expected" will be shown.
2983 // - Change aarch64.rs's 128-bit seqcst load from `ldar; ldp; dmb ishld` to `ldp; dmb ish`
2984 // and run test with `--release -- seqcst1` on AArch64 macOS or other AArch64 which supports FEAT_LSE2
2985 // with `RUSTFLAGS=-C target-feature=+lse2`.
2986 // tests::stress_swap_u128::seqcst1_load_seqcst_swap_seqcst should fail within a few repetitions.
2987 pub(crate) fn seqcst1<T: Send + Sync>(
2988 _min: isize,
2989 #[allow(unused_variables)] max: isize,
2990 new: impl Fn(isize) -> T,
2991 load: impl Fn(&T) -> isize + Send + Sync,
2992 store: impl Fn(&T, isize) + Send + Sync,
2993 ) {
2994 let n: usize = if cfg!(miri) {
2995 8
2996 } else if cfg!(valgrind)
2997 || build_context::SANITIZE.contains("address")
2998 || build_context::SANITIZE.contains("memory")
2999 {
3000 50
3001 } else if cfg!(qemu)
3002 || option_env!("GITHUB_ACTIONS").is_some() && cfg!(not(target_os = "linux"))
3003 {
3004 // GitHub Actions' macOS and Windows runners are slow.
3005 5_000
3006 } else {
3007 50_000
3008 };
3009
3010 let a = &new(0);
3011 let b = &new(0);
3012 let c = &AtomicUsize::new(0);
3013 let a_done = &AtomicUsize::new(0);
3014 let a_ready = &AtomicUsize::new(0);
3015 let b_ready = &AtomicUsize::new(0);
3016 let panicked = &AtomicUsize::new(0);
3017 thread::scope(|s| {
3018 s.spawn(|_| {
3019 for n in 1..n {
3020 // Acquire to make initialization of a/b/c visible.
3021 while a_ready.load(Ordering::Acquire) != n {
3022 if panicked.load(Ordering::Relaxed) != 0 {
3023 return;
3024 }
3025 }
3026 b_ready.store(n, Ordering::Relaxed);
3027 store(a, 1);
3028 if load(b) == 0 {
3029 c.fetch_add(1, Ordering::Relaxed);
3030 }
3031 // Release to make update of c visible.
3032 a_done.store(n, Ordering::Release);
3033 }
3034 });
3035 for n in 1..n {
3036 store(a, 0);
3037 store(b, 0);
3038 c.store(0, Ordering::Relaxed);
3039 // Release to make initialization of a/b/c visible.
3040 a_ready.store(n, Ordering::Release);
3041 while b_ready.load(Ordering::Relaxed) != n {}
3042 store(b, 1);
3043 if load(a) == 0 {
3044 c.fetch_add(1, Ordering::Relaxed);
3045 }
3046 // Acquire to make update of c visible.
3047 while a_done.load(Ordering::Acquire) != n {}
3048 let c = c.load(Ordering::Relaxed);
3049 let ok = matches!(c, 0 | 1);
3050 if !ok {
3051 panicked.store(1, Ordering::Relaxed);
3052 panic!("c={},n={}", c, n);
3053 }
3054 }
3055 })
3056 .unwrap();
3057 }
3058 // Catches unwinding panic on architectures with weak memory models.
3059 #[allow(dead_code)]
3060 pub(crate) fn catch_unwind_on_weak_memory_arch(pat: &str, function_name: &str, f: impl Fn()) {
3061 // With x86 TSO, RISC-V TSO (optional), SPARC TSO (optional, default?),
3062 // and IBM-370 memory models should never be a panic here.
3063 // Miri emulates weak memory models regardless of target architectures.
3064 if cfg!(all(
3065 any(
3066 target_arch = "x86",
3067 target_arch = "x86_64",
3068 target_arch = "s390x",
3069 target_arch = "sparc",
3070 target_arch = "sparc64",
3071 all(any(target_arch = "riscv32", target_arch = "riscv64"), target_feature = "ztso"),
3072 ),
3073 not(any(miri)),
3074 )) {
3075 f();
3076 } else if !is_panic_abort() {
3077 // This could be Err on architectures with weak memory models.
3078 // However, this does not necessarily mean that it will always be panic,
3079 // and implementing it with stronger orderings is also okay.
3080 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
3081 Ok(()) => {
3082 // panic!();
3083 }
3084 Err(msg) => {
3085 let msg = msg
3086 .downcast_ref::<std::string::String>()
3087 .cloned()
3088 .unwrap_or_else(|| msg.downcast_ref::<&'static str>().copied().unwrap().into());
3089 assert!(msg.contains(pat), "{}", msg);
3090 test_helper::eprintln_nocapture!(" {} panicked as expected", function_name);
3091 }
3092 }
3093 }
3094 }
3095 // Catches unwinding panic on architectures with non-sequentially consistent memory models.
3096 #[allow(dead_code)]
3097 pub(crate) fn catch_unwind_on_non_seqcst_arch(pat: &str, function_name: &str, f: impl Fn()) {
3098 if !is_panic_abort() {
3099 // This could be Err on architectures with non-sequentially consistent memory models.
3100 // However, this does not necessarily mean that it will always be panic,
3101 // and implementing it with stronger orderings is also okay.
3102 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
3103 Ok(()) => {
3104 // panic!();
3105 }
3106 Err(msg) => {
3107 let msg = msg
3108 .downcast_ref::<std::string::String>()
3109 .cloned()
3110 .unwrap_or_else(|| msg.downcast_ref::<&'static str>().copied().unwrap().into());
3111 assert!(msg.contains(pat), "{}", msg);
3112 test_helper::eprintln_nocapture!(" {} panicked as expected", function_name);
3113 }
3114 }
3115 }
3116 }
3117 macro_rules! __call_stress_test_fn {
3118 ($name:ident, $atomic_type:ident,
3119 $int_type:ident, load, cas, $load_order:ident, $store_order:ident
3120 ) => {
3121 $name(
3122 $int_type::MIN.try_into().unwrap_or(isize::MIN),
3123 $int_type::MAX.try_into().unwrap_or(isize::MAX),
3124 |v| <$atomic_type>::new(v as $int_type),
3125 |a| a.load(Ordering::$load_order) as isize,
3126 |a, v| {
3127 a.fetch_update(Ordering::$store_order, Ordering::Relaxed, |_| Some(v as $int_type))
3128 .unwrap();
3129 },
3130 )
3131 };
3132 ($name:ident, $atomic_type:ident,
3133 $int_type:ident, load, $write:ident, $load_order:ident, $store_order:ident
3134 ) => {
3135 $name(
3136 $int_type::MIN.try_into().unwrap_or(isize::MIN),
3137 $int_type::MAX.try_into().unwrap_or(isize::MAX),
3138 |v| <$atomic_type>::new(v as $int_type),
3139 |a| a.load(Ordering::$load_order) as isize,
3140 |a, v| {
3141 a.$write(v as $int_type, Ordering::$store_order);
3142 },
3143 )
3144 };
3145 ($name:ident, $atomic_type:ident,
3146 $int_type:ident, cas, $write:ident, $load_order:ident, $store_order:ident
3147 ) => {
3148 $name(
3149 $int_type::MIN.try_into().unwrap_or(isize::MIN),
3150 $int_type::MAX.try_into().unwrap_or(isize::MAX),
3151 |v| <$atomic_type>::new(v as $int_type),
3152 |a| {
3153 a.compare_exchange(0, 0, Ordering::$load_order, match Ordering::$load_order {
3154 Ordering::Release => Ordering::Relaxed,
3155 Ordering::AcqRel => Ordering::Acquire,
3156 _ => Ordering::$load_order,
3157 })
3158 .unwrap_or_else(|x| x) as isize
3159 },
3160 |a, v| {
3161 a.$write(v as $int_type, Ordering::$store_order);
3162 },
3163 )
3164 };
3165 ($name:ident, $atomic_type:ident,
3166 $int_type:ident, cas_weak, $write:ident, $load_order:ident, $store_order:ident
3167 ) => {
3168 $name(
3169 $int_type::MIN.try_into().unwrap_or(isize::MIN),
3170 $int_type::MAX.try_into().unwrap_or(isize::MAX),
3171 |v| <$atomic_type>::new(v as $int_type),
3172 |a| {
3173 a.compare_exchange_weak(0, 0, Ordering::$load_order, match Ordering::$load_order {
3174 Ordering::Release => Ordering::Relaxed,
3175 Ordering::AcqRel => Ordering::Acquire,
3176 _ => Ordering::$load_order,
3177 })
3178 .unwrap_or_else(|x| x) as isize
3179 },
3180 |a, v| {
3181 a.$write(v as $int_type, Ordering::$store_order);
3182 },
3183 )
3184 };
3185 }
3186 macro_rules! stress_test_load_store {
3187 ($int_type:ident) => {
3188 paste::paste! {
3189 #[allow(
3190 clippy::alloc_instead_of_core,
3191 clippy::std_instead_of_alloc,
3192 clippy::std_instead_of_core,
3193 clippy::undocumented_unsafe_blocks
3194 )]
3195 mod [<stress_load_store_ $int_type>] {
3196 use std::{convert::TryInto as _, sync::atomic::Ordering};
3197
3198 use crate::tests::helper::*;
3199 use super::*;
3200
3201 __stress_acquire_release!(can_panic, $int_type, load, store, Relaxed, Relaxed);
3202 __stress_acquire_release!(can_panic, $int_type, load, store, Relaxed, Release);
3203 __stress_acquire_release!(can_panic, $int_type, load, store, Relaxed, SeqCst);
3204 __stress_acquire_release!(can_panic, $int_type, load, store, Acquire, Relaxed);
3205 __stress_acquire_release!(should_pass, $int_type, load, store, Acquire, Release);
3206 __stress_acquire_release!(should_pass, $int_type, load, store, Acquire, SeqCst);
3207 __stress_acquire_release!(can_panic, $int_type, load, store, SeqCst, Relaxed);
3208 __stress_acquire_release!(should_pass, $int_type, load, store, SeqCst, Release);
3209 __stress_acquire_release!(should_pass, $int_type, load, store, SeqCst, SeqCst);
3210
3211 __stress_seqcst!(can_panic, $int_type, load, store, Relaxed, Relaxed);
3212 __stress_seqcst!(can_panic, $int_type, load, store, Relaxed, Release);
3213 __stress_seqcst!(can_panic, $int_type, load, store, Relaxed, SeqCst);
3214 __stress_seqcst!(can_panic, $int_type, load, store, Acquire, Relaxed);
3215 __stress_seqcst!(can_panic, $int_type, load, store, Acquire, Release);
3216 __stress_seqcst!(can_panic, $int_type, load, store, Acquire, SeqCst);
3217 __stress_seqcst!(can_panic, $int_type, load, store, SeqCst, Relaxed);
3218 __stress_seqcst!(can_panic, $int_type, load, store, SeqCst, Release);
3219 __stress_seqcst!(should_pass, $int_type, load, store, SeqCst, SeqCst);
3220 }
3221 }
3222 };
3223 }
3224 macro_rules! stress_test {
3225 ($int_type:ident) => {
3226 stress_test_load_store!($int_type);
3227 paste::paste! {
3228 #[allow(
3229 clippy::alloc_instead_of_core,
3230 clippy::std_instead_of_alloc,
3231 clippy::std_instead_of_core,
3232 clippy::undocumented_unsafe_blocks
3233 )]
3234 mod [<stress_swap_ $int_type>] {
3235 use std::{convert::TryInto as _, sync::atomic::Ordering};
3236
3237 use crate::tests::helper::*;
3238 use super::*;
3239
3240 __stress_acquire_release!(can_panic, $int_type, load, swap, Relaxed, Relaxed);
3241 __stress_acquire_release!(can_panic, $int_type, load, swap, Relaxed, Acquire);
3242 __stress_acquire_release!(can_panic, $int_type, load, swap, Relaxed, Release);
3243 __stress_acquire_release!(can_panic, $int_type, load, swap, Relaxed, AcqRel);
3244 __stress_acquire_release!(can_panic, $int_type, load, swap, Relaxed, SeqCst);
3245 __stress_acquire_release!(can_panic, $int_type, load, swap, Acquire, Relaxed);
3246 __stress_acquire_release!(can_panic, $int_type, load, swap, Acquire, Acquire);
3247 __stress_acquire_release!(should_pass, $int_type, load, swap, Acquire, Release);
3248 __stress_acquire_release!(should_pass, $int_type, load, swap, Acquire, AcqRel);
3249 __stress_acquire_release!(should_pass, $int_type, load, swap, Acquire, SeqCst);
3250 __stress_acquire_release!(can_panic, $int_type, load, swap, SeqCst, Relaxed);
3251 __stress_acquire_release!(can_panic, $int_type, load, swap, SeqCst, Acquire);
3252 __stress_acquire_release!(should_pass, $int_type, load, swap, SeqCst, Release);
3253 __stress_acquire_release!(should_pass, $int_type, load, swap, SeqCst, AcqRel);
3254 __stress_acquire_release!(should_pass, $int_type, load, swap, SeqCst, SeqCst);
3255
3256 __stress_seqcst!(can_panic, $int_type, load, swap, Relaxed, Relaxed);
3257 __stress_seqcst!(can_panic, $int_type, load, swap, Relaxed, Acquire);
3258 __stress_seqcst!(can_panic, $int_type, load, swap, Relaxed, Release);
3259 __stress_seqcst!(can_panic, $int_type, load, swap, Relaxed, AcqRel);
3260 __stress_seqcst!(can_panic, $int_type, load, swap, Relaxed, SeqCst);
3261 __stress_seqcst!(can_panic, $int_type, load, swap, Acquire, Relaxed);
3262 __stress_seqcst!(can_panic, $int_type, load, swap, Acquire, Acquire);
3263 __stress_seqcst!(can_panic, $int_type, load, swap, Acquire, Release);
3264 __stress_seqcst!(can_panic, $int_type, load, swap, Acquire, AcqRel);
3265 __stress_seqcst!(can_panic, $int_type, load, swap, Acquire, SeqCst);
3266 __stress_seqcst!(can_panic, $int_type, load, swap, SeqCst, Relaxed);
3267 __stress_seqcst!(can_panic, $int_type, load, swap, SeqCst, Acquire);
3268 __stress_seqcst!(can_panic, $int_type, load, swap, SeqCst, Release);
3269 __stress_seqcst!(can_panic, $int_type, load, swap, SeqCst, AcqRel);
3270 __stress_seqcst!(should_pass, $int_type, load, swap, SeqCst, SeqCst);
3271 }
3272 }
3273 // TODO: cas
3274 };
3275 }
3276
3277 #[cfg(feature = "float")]
3278 pub(crate) mod float_rand {
3279 #[cfg(portable_atomic_unstable_f16)]
3280 pub(crate) fn f16(rng: &mut fastrand::Rng) -> f16 {
3281 f16::from_bits(rng.u16(..))
3282 }
3283 pub(crate) fn f32(rng: &mut fastrand::Rng) -> f32 {
3284 f32::from_bits(rng.u32(..))
3285 }
3286 #[cfg(not(all(not(debug_assertions), target_arch = "x86", not(target_feature = "sse2"))))]
3287 pub(crate) fn f64(rng: &mut fastrand::Rng) -> f64 {
3288 f64::from_bits(rng.u64(..))
3289 }
3290 #[cfg(portable_atomic_unstable_f128)]
3291 pub(crate) fn f128(rng: &mut fastrand::Rng) -> f128 {
3292 f128::from_bits(rng.u128(..))
3293 }
3294 }