| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Atomic load/store implementation on RISC-V. |
| 5 |
|
| 6 |
This is for RISC-V targets without A extension. (pre-1.76 rustc doesn't provide atomics |
| 7 |
at all on such targets. https://github.com/rust-lang/rust/pull/114499) |
| 8 |
|
| 9 |
Also, optionally provides RMW implementation when Zaamo extension or force-amo feature is enabled. |
| 10 |
|
| 11 |
If A extension is not available but Zalrsc or Zacas extension is available, LLVM can implement all |
| 12 |
atomic operations using it (https://github.com/llvm/llvm-project/commit/70f70390bb83a383fcb88ea843adbc4edf6ee34b), |
| 13 |
so this module contains no code for these extensions. |
| 14 |
|
| 15 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 16 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 17 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#risc-v |
| 18 |
|
| 19 |
Refs: |
| 20 |
- RISC-V Instruction Set Manual |
| 21 |
"Zaamo" Extension for Atomic Memory Operations |
| 22 |
https://docs.riscv.org/reference/isa/v20260120/unpriv/a-st-ext.html#sec:amo |
| 23 |
"Zabha" Extension for Byte and Halfword Atomic Memory Operations |
| 24 |
https://docs.riscv.org/reference/isa/v20260120/unpriv/zabha.html |
| 25 |
- RISC-V Atomics ABI Specification |
| 26 |
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/draft-20250812-301374e92976e298e676e7129a6212926b2299ce/riscv-atomic.adoc |
| 27 |
- atomic-maybe-uninit |
| 28 |
https://github.com/taiki-e/atomic-maybe-uninit |
| 29 |
|
| 30 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 31 |
*/ |
| 32 |
|
| 33 |
#[cfg(not(portable_atomic_no_asm))] |
| 34 |
use core::arch::asm; |
| 35 |
use core::{cell::UnsafeCell, sync::atomic::Ordering}; |
| 36 |
|
| 37 |
#[cfg(any( |
| 38 |
test, |
| 39 |
portable_atomic_force_amo, |
| 40 |
target_feature = "zaamo", |
| 41 |
portable_atomic_target_feature = "zaamo", |
| 42 |
))] |
| 43 |
items!({ |
| 44 |
macro_rules! atomic_rmw_amo_ext { |
| 45 |
// Use +a also for zaamo because `option arch +zaamo` requires LLVM 19. |
| 46 |
// https://github.com/llvm/llvm-project/commit/8be079cdddfd628d356d9ddb5ab397ea95fb1030 |
| 47 |
("w") => { |
| 48 |
"+a" |
| 49 |
}; |
| 50 |
("d") => { |
| 51 |
"+a" |
| 52 |
}; |
| 53 |
("b") => { |
| 54 |
"+a,+zabha" |
| 55 |
}; |
| 56 |
("h") => { |
| 57 |
"+a,+zabha" |
| 58 |
}; |
| 59 |
} |
| 60 |
macro_rules! atomic_rmw_amo { |
| 61 |
($op:ident, $dst:ident, $val:ident, $order:ident, $size:tt) => {{ |
| 62 |
let out; |
| 63 |
macro_rules! op { |
| 64 |
($asm_order:tt) => { |
| 65 |
// SAFETY: The user guaranteed that the AMO instruction is available in this |
| 66 |
// system by setting the portable_atomic_force_amo/target_feature and |
| 67 |
// portable_atomic_unsafe_assume_single_core. |
| 68 |
// The caller of this macro must guarantee the validity of the pointer. |
| 69 |
asm!( |
| 70 |
".option push", |
| 71 |
// https://github.com/riscv-non-isa/riscv-asm-manual/blob/v0.0.1/src/asm-manual.adoc#arch |
| 72 |
// LLVM supports `.option arch` directive on LLVM 17+. |
| 73 |
// https://github.com/llvm/llvm-project/commit/9e8ed3403c191ab9c4903e8eeb8f732ff8a43cb4 |
| 74 |
// Note that `.insn <value>` directive requires LLVM 19. |
| 75 |
// https://github.com/llvm/llvm-project/commit/2a086dce691e3cc34a2fc27f4fb255bb2cbbfac9 |
| 76 |
concat!(".option arch, ", atomic_rmw_amo_ext!($size)), |
| 77 |
concat!("amo", stringify!($op), ".", $size, $asm_order, " {out}, {val}, 0({dst})"), // atomic { _x = *dst; *dst = op(_x, val); out = _x } |
| 78 |
".option pop", |
| 79 |
dst = in(reg) ptr_reg!($dst), |
| 80 |
val = in(reg) $val, |
| 81 |
out = lateout(reg) out, |
| 82 |
options(nostack, preserves_flags), |
| 83 |
) |
| 84 |
}; |
| 85 |
} |
| 86 |
match $order { |
| 87 |
Ordering::Relaxed => op!(""), |
| 88 |
Ordering::Acquire => op!(".aq"), |
| 89 |
Ordering::Release => op!(".rl"), |
| 90 |
// AcqRel and SeqCst RMWs are equivalent. |
| 91 |
Ordering::AcqRel | Ordering::SeqCst => op!(".aqrl"), |
| 92 |
_ => unreachable!(), |
| 93 |
} |
| 94 |
out |
| 95 |
}}; |
| 96 |
} |
| 97 |
|
| 98 |
#[cfg(not(any(target_feature = "zabha", portable_atomic_target_feature = "zabha")))] |
| 99 |
items!({ |
| 100 |
#[cfg(target_arch = "riscv32")] |
| 101 |
macro_rules! w { |
| 102 |
() => { |
| 103 |
"" |
| 104 |
}; |
| 105 |
} |
| 106 |
#[cfg(target_arch = "riscv64")] |
| 107 |
macro_rules! w { |
| 108 |
() => { |
| 109 |
"w" |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
// 32-bit val.wrapping_shl(shift) but no extra `& (u32::BITS - 1)` |
| 114 |
#[inline(always)] |
| 115 |
fn sllw(val: u32, shift: u32) -> u32 { |
| 116 |
// SAFETY: Calling sll{,w} is safe. |
| 117 |
unsafe { |
| 118 |
let out; |
| 119 |
asm!( |
| 120 |
concat!("sll", w!(), " {out}, {val}, {shift}"), // out = val << shift & 31 |
| 121 |
out = lateout(reg) out, |
| 122 |
val = in(reg) val, |
| 123 |
shift = in(reg) shift, |
| 124 |
options(pure, nomem, nostack, preserves_flags), |
| 125 |
); |
| 126 |
out |
| 127 |
} |
| 128 |
} |
| 129 |
// 32-bit val.wrapping_shr(shift) but no extra `& (u32::BITS - 1)` |
| 130 |
macro_rules! srlw { |
| 131 |
($val:expr, $shift:expr) => { |
| 132 |
// SAFETY: Calling srl{,w} is safe. |
| 133 |
unsafe { |
| 134 |
let val: u32 = $val; |
| 135 |
let shift: u32 = $shift; |
| 136 |
let out; |
| 137 |
asm!( |
| 138 |
concat!("srl", w!(), " {out}, {val}, {shift}"), // out = val >> shift & 31 |
| 139 |
out = lateout(reg) out, |
| 140 |
val = in(reg) val, |
| 141 |
shift = in(reg) shift, |
| 142 |
options(pure, nomem, nostack, preserves_flags), |
| 143 |
); |
| 144 |
out |
| 145 |
} |
| 146 |
}; |
| 147 |
} |
| 148 |
|
| 149 |
trait ZeroExtend: Copy { |
| 150 |
/// Zero-extends `self` to `u32` if it is smaller than 32-bit. |
| 151 |
fn zero_extend(self) -> u32; |
| 152 |
} |
| 153 |
macro_rules! zero_extend { |
| 154 |
($int:ident, $uint:ident) => { |
| 155 |
impl ZeroExtend for $uint { |
| 156 |
#[inline(always)] |
| 157 |
fn zero_extend(self) -> u32 { |
| 158 |
self as u32 |
| 159 |
} |
| 160 |
} |
| 161 |
impl ZeroExtend for $int { |
| 162 |
#[allow(clippy::cast_sign_loss)] |
| 163 |
#[inline(always)] |
| 164 |
fn zero_extend(self) -> u32 { |
| 165 |
self as $uint as u32 |
| 166 |
} |
| 167 |
} |
| 168 |
}; |
| 169 |
} |
| 170 |
zero_extend!(i8, u8); |
| 171 |
zero_extend!(i16, u16); |
| 172 |
}); |
| 173 |
}); |
| 174 |
|
| 175 |
macro_rules! atomic_load_store { |
| 176 |
($([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $size:tt) => { |
| 177 |
#[repr(transparent)] |
| 178 |
pub(crate) struct $atomic_type $(<$($generics)*>)? { |
| 179 |
v: UnsafeCell<$value_type>, |
| 180 |
} |
| 181 |
|
| 182 |
// Send is implicitly implemented for atomic integers, but not for atomic pointers. |
| 183 |
// SAFETY: any data races are prevented by atomic operations. |
| 184 |
unsafe impl $(<$($generics)*>)? Send for $atomic_type $(<$($generics)*>)? {} |
| 185 |
// SAFETY: any data races are prevented by atomic operations. |
| 186 |
unsafe impl $(<$($generics)*>)? Sync for $atomic_type $(<$($generics)*>)? {} |
| 187 |
|
| 188 |
#[cfg(any(test, not(portable_atomic_unsafe_assume_single_core)))] |
| 189 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 190 |
#[inline] |
| 191 |
pub(crate) const fn new(v: $value_type) -> Self { |
| 192 |
Self { v: UnsafeCell::new(v) } |
| 193 |
} |
| 194 |
|
| 195 |
#[inline] |
| 196 |
pub(crate) fn is_lock_free() -> bool { |
| 197 |
Self::IS_ALWAYS_LOCK_FREE |
| 198 |
} |
| 199 |
pub(crate) const IS_ALWAYS_LOCK_FREE: bool = true; |
| 200 |
|
| 201 |
#[inline] |
| 202 |
pub(crate) const fn as_ptr(&self) -> *mut $value_type { |
| 203 |
self.v.get() |
| 204 |
} |
| 205 |
} |
| 206 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 207 |
#[inline] |
| 208 |
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] |
| 209 |
pub(crate) fn load(&self, order: Ordering) -> $value_type { |
| 210 |
crate::utils::assert_load_ordering(order); |
| 211 |
let src = self.v.get(); |
| 212 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 213 |
// pointer passed in is valid because we got it from a reference. |
| 214 |
unsafe { |
| 215 |
let out; |
| 216 |
macro_rules! atomic_load { |
| 217 |
($acquire:tt, $release:tt) => { |
| 218 |
asm!( |
| 219 |
$release, // fence |
| 220 |
concat!("l", $size, " {out}, 0({src})"), // atomic { out = *src } |
| 221 |
$acquire, // fence |
| 222 |
src = in(reg) ptr_reg!(src), |
| 223 |
out = lateout(reg) out, |
| 224 |
options(nostack, preserves_flags), |
| 225 |
) |
| 226 |
}; |
| 227 |
} |
| 228 |
match order { |
| 229 |
Ordering::Relaxed => atomic_load!("", ""), |
| 230 |
Ordering::Acquire => atomic_load!("fence r, rw", ""), |
| 231 |
Ordering::SeqCst => atomic_load!("fence r, rw", "fence rw, rw"), |
| 232 |
_ => unreachable!(), |
| 233 |
} |
| 234 |
out |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
#[inline] |
| 239 |
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] |
| 240 |
pub(crate) fn store(&self, val: $value_type, order: Ordering) { |
| 241 |
crate::utils::assert_store_ordering(order); |
| 242 |
let dst = self.v.get(); |
| 243 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 244 |
// pointer passed in is valid because we got it from a reference. |
| 245 |
unsafe { |
| 246 |
macro_rules! atomic_store { |
| 247 |
($acquire:tt, $release:tt) => { |
| 248 |
asm!( |
| 249 |
$release, // fence |
| 250 |
concat!("s", $size, " {val}, 0({dst})"), // atomic { *dst = val } |
| 251 |
$acquire, // fence |
| 252 |
dst = in(reg) ptr_reg!(dst), |
| 253 |
val = in(reg) val, |
| 254 |
options(nostack, preserves_flags), |
| 255 |
) |
| 256 |
}; |
| 257 |
} |
| 258 |
match order { |
| 259 |
Ordering::Relaxed => atomic_store!("", ""), |
| 260 |
Ordering::Release => atomic_store!("", "fence rw, w"), |
| 261 |
// https://github.com/llvm/llvm-project/commit/3ea8f2526541884e03d5bd4f4e46f4eb190990b6 |
| 262 |
Ordering::SeqCst => atomic_store!("fence rw, rw", "fence rw, w"), |
| 263 |
_ => unreachable!(), |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
}; |
| 269 |
} |
| 270 |
|
| 271 |
macro_rules! atomic_base { |
| 272 |
( |
| 273 |
$([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $int_type:ty, $size:tt, |
| 274 |
$fetch_add:ident, $fetch_sub:ident |
| 275 |
) => { |
| 276 |
atomic_load_store!($([$($generics)*])? $atomic_type, $value_type, $size); |
| 277 |
#[cfg(any( |
| 278 |
test, |
| 279 |
portable_atomic_force_amo, |
| 280 |
target_feature = "zaamo", |
| 281 |
portable_atomic_target_feature = "zaamo", |
| 282 |
))] |
| 283 |
items!({ |
| 284 |
// There is no amo{sub,nand,neg}. |
| 285 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 286 |
#[inline] |
| 287 |
pub(crate) fn swap(&self, val: $value_type, order: Ordering) -> $value_type { |
| 288 |
let dst = self.v.get(); |
| 289 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 290 |
// pointer passed in is valid because we got it from a reference. |
| 291 |
unsafe { atomic_rmw_amo!(swap, dst, val, order, $size) } |
| 292 |
} |
| 293 |
#[inline] |
| 294 |
pub(crate) fn $fetch_add(&self, val: $int_type, order: Ordering) -> $value_type { |
| 295 |
let dst = self.v.get(); |
| 296 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 297 |
// pointer passed in is valid because we got it from a reference. |
| 298 |
unsafe { atomic_rmw_amo!(add, dst, val, order, $size) } |
| 299 |
} |
| 300 |
#[inline] |
| 301 |
pub(crate) fn $fetch_sub(&self, val: $int_type, order: Ordering) -> $value_type { |
| 302 |
self.$fetch_add(val.wrapping_neg(), order) |
| 303 |
} |
| 304 |
#[inline] |
| 305 |
pub(crate) fn fetch_and(&self, val: $int_type, order: Ordering) -> $value_type { |
| 306 |
let dst = self.v.get(); |
| 307 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 308 |
// pointer passed in is valid because we got it from a reference. |
| 309 |
unsafe { atomic_rmw_amo!(and, dst, val, order, $size) } |
| 310 |
} |
| 311 |
#[inline] |
| 312 |
pub(crate) fn fetch_or(&self, val: $int_type, order: Ordering) -> $value_type { |
| 313 |
let dst = self.v.get(); |
| 314 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 315 |
// pointer passed in is valid because we got it from a reference. |
| 316 |
unsafe { atomic_rmw_amo!(or, dst, val, order, $size) } |
| 317 |
} |
| 318 |
#[inline] |
| 319 |
pub(crate) fn fetch_xor(&self, val: $int_type, order: Ordering) -> $value_type { |
| 320 |
let dst = self.v.get(); |
| 321 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 322 |
// pointer passed in is valid because we got it from a reference. |
| 323 |
unsafe { atomic_rmw_amo!(xor, dst, val, order, $size) } |
| 324 |
} |
| 325 |
} |
| 326 |
#[cfg(not(any(portable_atomic_unsafe_assume_single_core, feature = "critical-section")))] |
| 327 |
impl_default_bit_opts!($atomic_type, $int_type); |
| 328 |
}); |
| 329 |
}; |
| 330 |
} |
| 331 |
|
| 332 |
macro_rules! atomic_ptr { |
| 333 |
($size:tt) => { |
| 334 |
atomic_base!([T] AtomicPtr, *mut T, usize, $size, fetch_byte_add, fetch_byte_sub); |
| 335 |
}; |
| 336 |
} |
| 337 |
|
| 338 |
macro_rules! atomic { |
| 339 |
($atomic_type:ident, $value_type:ty, $size:tt, $max:tt, $min:tt) => { |
| 340 |
atomic_base!($atomic_type, $value_type, $value_type, $size, fetch_add, fetch_sub); |
| 341 |
#[cfg(any( |
| 342 |
test, |
| 343 |
portable_atomic_force_amo, |
| 344 |
target_feature = "zaamo", |
| 345 |
portable_atomic_target_feature = "zaamo", |
| 346 |
))] |
| 347 |
items!({ |
| 348 |
// There is no amo{sub,nand,neg}. |
| 349 |
impl $atomic_type { |
| 350 |
#[inline] |
| 351 |
pub(crate) fn fetch_not(&self, order: Ordering) -> $value_type { |
| 352 |
let dst = self.v.get(); |
| 353 |
let val: crate::utils::RegSize = !0; |
| 354 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 355 |
// pointer passed in is valid because we got it from a reference. |
| 356 |
unsafe { atomic_rmw_amo!(xor, dst, val, order, $size) } |
| 357 |
} |
| 358 |
#[inline] |
| 359 |
pub(crate) fn fetch_max(&self, val: $value_type, order: Ordering) -> $value_type { |
| 360 |
let dst = self.v.get(); |
| 361 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 362 |
// pointer passed in is valid because we got it from a reference. |
| 363 |
unsafe { atomic_rmw_amo!($max, dst, val, order, $size) } |
| 364 |
} |
| 365 |
#[inline] |
| 366 |
pub(crate) fn fetch_min(&self, val: $value_type, order: Ordering) -> $value_type { |
| 367 |
let dst = self.v.get(); |
| 368 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 369 |
// pointer passed in is valid because we got it from a reference. |
| 370 |
unsafe { atomic_rmw_amo!($min, dst, val, order, $size) } |
| 371 |
} |
| 372 |
} |
| 373 |
#[cfg(not(any( |
| 374 |
portable_atomic_unsafe_assume_single_core, |
| 375 |
feature = "critical-section", |
| 376 |
)))] |
| 377 |
items!({ |
| 378 |
impl_default_no_fetch_ops!($atomic_type, $value_type); |
| 379 |
impl $atomic_type { |
| 380 |
#[inline] |
| 381 |
pub(crate) fn not(&self, order: Ordering) { |
| 382 |
self.fetch_not(order); |
| 383 |
} |
| 384 |
} |
| 385 |
}); |
| 386 |
}); |
| 387 |
}; |
| 388 |
} |
| 389 |
|
| 390 |
macro_rules! atomic_sub_word { |
| 391 |
($atomic_type:ident, $value_type:ty, $size:tt, $max:tt, $min:tt) => { |
| 392 |
cfg_sel!({ |
| 393 |
#[cfg(any(target_feature = "zabha", portable_atomic_target_feature = "zabha"))] |
| 394 |
{ |
| 395 |
atomic!($atomic_type, $value_type, $size, $max, $min); |
| 396 |
} |
| 397 |
#[cfg(else)] |
| 398 |
{ |
| 399 |
atomic_load_store!($atomic_type, $value_type, $size); |
| 400 |
#[cfg(any( |
| 401 |
test, |
| 402 |
portable_atomic_force_amo, |
| 403 |
target_feature = "zaamo", |
| 404 |
portable_atomic_target_feature = "zaamo", |
| 405 |
))] |
| 406 |
items!({ |
| 407 |
impl $atomic_type { |
| 408 |
#[inline] |
| 409 |
pub(crate) fn fetch_and( |
| 410 |
&self, |
| 411 |
val: $value_type, |
| 412 |
order: Ordering, |
| 413 |
) -> $value_type { |
| 414 |
let dst = self.v.get(); |
| 415 |
let (dst, shift, mut mask) = |
| 416 |
crate::utils::create_sub_word_mask_values(dst); |
| 417 |
mask = !sllw(mask, shift); |
| 418 |
let mut val = sllw(ZeroExtend::zero_extend(val), shift); |
| 419 |
val |= mask; |
| 420 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 421 |
// pointer passed in is valid because we got it from a reference. |
| 422 |
let out: u32 = unsafe { atomic_rmw_amo!(and, dst, val, order, "w") }; |
| 423 |
srlw!(out, shift) |
| 424 |
} |
| 425 |
#[inline] |
| 426 |
pub(crate) fn fetch_or( |
| 427 |
&self, |
| 428 |
val: $value_type, |
| 429 |
order: Ordering, |
| 430 |
) -> $value_type { |
| 431 |
let dst = self.v.get(); |
| 432 |
let (dst, shift, _mask) = |
| 433 |
crate::utils::create_sub_word_mask_values(dst); |
| 434 |
let val = sllw(ZeroExtend::zero_extend(val), shift); |
| 435 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 436 |
// pointer passed in is valid because we got it from a reference. |
| 437 |
let out: u32 = unsafe { atomic_rmw_amo!(or, dst, val, order, "w") }; |
| 438 |
srlw!(out, shift) |
| 439 |
} |
| 440 |
#[inline] |
| 441 |
pub(crate) fn fetch_xor( |
| 442 |
&self, |
| 443 |
val: $value_type, |
| 444 |
order: Ordering, |
| 445 |
) -> $value_type { |
| 446 |
let dst = self.v.get(); |
| 447 |
let (dst, shift, _mask) = |
| 448 |
crate::utils::create_sub_word_mask_values(dst); |
| 449 |
let val = sllw(ZeroExtend::zero_extend(val), shift); |
| 450 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 451 |
// pointer passed in is valid because we got it from a reference. |
| 452 |
let out: u32 = unsafe { atomic_rmw_amo!(xor, dst, val, order, "w") }; |
| 453 |
srlw!(out, shift) |
| 454 |
} |
| 455 |
#[inline] |
| 456 |
pub(crate) fn fetch_not(&self, order: Ordering) -> $value_type { |
| 457 |
self.fetch_xor(!0, order) |
| 458 |
} |
| 459 |
} |
| 460 |
#[cfg(not(any( |
| 461 |
portable_atomic_unsafe_assume_single_core, |
| 462 |
feature = "critical-section", |
| 463 |
)))] |
| 464 |
items!({ |
| 465 |
impl_default_bit_opts!($atomic_type, $value_type); |
| 466 |
impl $atomic_type { |
| 467 |
#[inline] |
| 468 |
pub(crate) fn and(&self, val: $value_type, order: Ordering) { |
| 469 |
self.fetch_and(val, order); |
| 470 |
} |
| 471 |
#[inline] |
| 472 |
pub(crate) fn or(&self, val: $value_type, order: Ordering) { |
| 473 |
self.fetch_or(val, order); |
| 474 |
} |
| 475 |
#[inline] |
| 476 |
pub(crate) fn xor(&self, val: $value_type, order: Ordering) { |
| 477 |
self.fetch_xor(val, order); |
| 478 |
} |
| 479 |
#[inline] |
| 480 |
pub(crate) fn not(&self, order: Ordering) { |
| 481 |
self.fetch_not(order); |
| 482 |
} |
| 483 |
} |
| 484 |
}); |
| 485 |
}); |
| 486 |
} |
| 487 |
}); |
| 488 |
}; |
| 489 |
} |
| 490 |
|
| 491 |
atomic_sub_word!(AtomicI8, i8, "b", max, min); |
| 492 |
atomic_sub_word!(AtomicU8, u8, "b", maxu, minu); |
| 493 |
atomic_sub_word!(AtomicI16, i16, "h", max, min); |
| 494 |
atomic_sub_word!(AtomicU16, u16, "h", maxu, minu); |
| 495 |
atomic!(AtomicI32, i32, "w", max, min); |
| 496 |
atomic!(AtomicU32, u32, "w", maxu, minu); |
| 497 |
#[cfg(target_arch = "riscv64")] |
| 498 |
atomic!(AtomicI64, i64, "d", max, min); |
| 499 |
#[cfg(target_arch = "riscv64")] |
| 500 |
atomic!(AtomicU64, u64, "d", maxu, minu); |
| 501 |
#[cfg(target_pointer_width = "32")] |
| 502 |
atomic!(AtomicIsize, isize, "w", max, min); |
| 503 |
#[cfg(target_pointer_width = "32")] |
| 504 |
atomic!(AtomicUsize, usize, "w", maxu, minu); |
| 505 |
#[cfg(target_pointer_width = "32")] |
| 506 |
atomic_ptr!("w"); |
| 507 |
#[cfg(target_pointer_width = "64")] |
| 508 |
atomic!(AtomicIsize, isize, "d", max, min); |
| 509 |
#[cfg(target_pointer_width = "64")] |
| 510 |
atomic!(AtomicUsize, usize, "d", maxu, minu); |
| 511 |
#[cfg(target_pointer_width = "64")] |
| 512 |
atomic_ptr!("d"); |
| 513 |
|
| 514 |
#[cfg(test)] |
| 515 |
mod tests { |
| 516 |
use super::*; |
| 517 |
|
| 518 |
test_atomic_ptr_load_store!(); |
| 519 |
test_atomic_int_load_store!(i8); |
| 520 |
test_atomic_int_load_store!(u8); |
| 521 |
test_atomic_int_load_store!(i16); |
| 522 |
test_atomic_int_load_store!(u16); |
| 523 |
test_atomic_int_load_store!(i32); |
| 524 |
test_atomic_int_load_store!(u32); |
| 525 |
#[cfg(target_arch = "riscv64")] |
| 526 |
test_atomic_int_load_store!(i64); |
| 527 |
#[cfg(target_arch = "riscv64")] |
| 528 |
test_atomic_int_load_store!(u64); |
| 529 |
test_atomic_int_load_store!(isize); |
| 530 |
test_atomic_int_load_store!(usize); |
| 531 |
|
| 532 |
macro_rules! test_atomic_ptr_amo { |
| 533 |
() => { |
| 534 |
#[allow( |
| 535 |
clippy::alloc_instead_of_core, |
| 536 |
clippy::std_instead_of_alloc, |
| 537 |
clippy::std_instead_of_core, |
| 538 |
clippy::undocumented_unsafe_blocks |
| 539 |
)] |
| 540 |
mod test_atomic_ptr_amo { |
| 541 |
use super::*; |
| 542 |
test_atomic_ptr_amo!(AtomicPtr<u8>); |
| 543 |
} |
| 544 |
}; |
| 545 |
($atomic_type:ty) => { |
| 546 |
#[allow(unused_imports)] |
| 547 |
use sptr::Strict as _; |
| 548 |
|
| 549 |
use crate::tests::helper; // for old rustc |
| 550 |
|
| 551 |
::quickcheck::quickcheck! { |
| 552 |
fn quickcheck_swap(x: usize, y: usize) -> bool { |
| 553 |
let x = sptr::invalid_mut(x); |
| 554 |
let y = sptr::invalid_mut(y); |
| 555 |
for &order in &helper::SWAP_ORDERINGS { |
| 556 |
let a = <$atomic_type>::new(x); |
| 557 |
assert_eq!(a.swap(y, order), x); |
| 558 |
assert_eq!(a.swap(x, order), y); |
| 559 |
} |
| 560 |
true |
| 561 |
} |
| 562 |
fn quickcheck_fetch_byte_add(x: usize, y: usize) -> bool { |
| 563 |
let x = sptr::invalid_mut(x); |
| 564 |
let y = sptr::invalid_mut::<u8>(y); |
| 565 |
for &order in &helper::SWAP_ORDERINGS { |
| 566 |
let a = <$atomic_type>::new(x); |
| 567 |
assert_eq!(a.fetch_byte_add(y.addr(), order), x); |
| 568 |
assert_eq!( |
| 569 |
a.load(Ordering::Relaxed).addr(), x.addr().wrapping_add(y.addr()) |
| 570 |
); |
| 571 |
let a = <$atomic_type>::new(y); |
| 572 |
assert_eq!(a.fetch_byte_add(x.addr(), order), y); |
| 573 |
assert_eq!( |
| 574 |
a.load(Ordering::Relaxed).addr(), y.addr().wrapping_add(x.addr()) |
| 575 |
); |
| 576 |
} |
| 577 |
true |
| 578 |
} |
| 579 |
fn quickcheck_fetch_byte_sub(x: usize, y: usize) -> bool { |
| 580 |
let x = sptr::invalid_mut(x); |
| 581 |
let y = sptr::invalid_mut::<u8>(y); |
| 582 |
for &order in &helper::SWAP_ORDERINGS { |
| 583 |
let a = <$atomic_type>::new(x); |
| 584 |
assert_eq!(a.fetch_byte_sub(y.addr(), order), x); |
| 585 |
assert_eq!( |
| 586 |
a.load(Ordering::Relaxed).addr(), x.addr().wrapping_sub(y.addr()) |
| 587 |
); |
| 588 |
let a = <$atomic_type>::new(y); |
| 589 |
assert_eq!(a.fetch_byte_sub(x.addr(), order), y); |
| 590 |
assert_eq!( |
| 591 |
a.load(Ordering::Relaxed).addr(), y.addr().wrapping_sub(x.addr()) |
| 592 |
); |
| 593 |
} |
| 594 |
true |
| 595 |
} |
| 596 |
fn quickcheck_fetch_and(x: usize, y: usize) -> bool { |
| 597 |
let x = sptr::invalid_mut(x); |
| 598 |
let y = sptr::invalid_mut::<u8>(y); |
| 599 |
for &order in &helper::SWAP_ORDERINGS { |
| 600 |
let a = <$atomic_type>::new(x); |
| 601 |
assert_eq!(a.fetch_and(y.addr(), order), x); |
| 602 |
assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() & y.addr()); |
| 603 |
let a = <$atomic_type>::new(y); |
| 604 |
assert_eq!(a.fetch_and(x.addr(), order), y); |
| 605 |
assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr() & x.addr()); |
| 606 |
} |
| 607 |
true |
| 608 |
} |
| 609 |
fn quickcheck_fetch_or(x: usize, y: usize) -> bool { |
| 610 |
let x = sptr::invalid_mut(x); |
| 611 |
let y = sptr::invalid_mut::<u8>(y); |
| 612 |
for &order in &helper::SWAP_ORDERINGS { |
| 613 |
let a = <$atomic_type>::new(x); |
| 614 |
assert_eq!(a.fetch_or(y.addr(), order), x); |
| 615 |
assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() | y.addr()); |
| 616 |
let a = <$atomic_type>::new(y); |
| 617 |
assert_eq!(a.fetch_or(x.addr(), order), y); |
| 618 |
assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr() | x.addr()); |
| 619 |
} |
| 620 |
true |
| 621 |
} |
| 622 |
fn quickcheck_fetch_xor(x: usize, y: usize) -> bool { |
| 623 |
let x = sptr::invalid_mut(x); |
| 624 |
let y = sptr::invalid_mut::<u8>(y); |
| 625 |
for &order in &helper::SWAP_ORDERINGS { |
| 626 |
let a = <$atomic_type>::new(x); |
| 627 |
assert_eq!(a.fetch_xor(y.addr(), order), x); |
| 628 |
assert_eq!(a.load(Ordering::Relaxed).addr(), x.addr() ^ y.addr()); |
| 629 |
let a = <$atomic_type>::new(y); |
| 630 |
assert_eq!(a.fetch_xor(x.addr(), order), y); |
| 631 |
assert_eq!(a.load(Ordering::Relaxed).addr(), y.addr() ^ x.addr()); |
| 632 |
} |
| 633 |
true |
| 634 |
} |
| 635 |
} |
| 636 |
}; |
| 637 |
} |
| 638 |
macro_rules! test_atomic_int_amo { |
| 639 |
($int_type:ident) => { |
| 640 |
paste::paste! { |
| 641 |
#[allow( |
| 642 |
clippy::alloc_instead_of_core, |
| 643 |
clippy::std_instead_of_alloc, |
| 644 |
clippy::std_instead_of_core, |
| 645 |
clippy::undocumented_unsafe_blocks |
| 646 |
)] |
| 647 |
mod [<test_atomic_ $int_type _amo>] { |
| 648 |
use super::*; |
| 649 |
test_atomic_int_amo!([<Atomic $int_type:camel>], $int_type); |
| 650 |
} |
| 651 |
} |
| 652 |
}; |
| 653 |
($atomic_type:ty, $int_type:ident) => { |
| 654 |
use crate::tests::helper; |
| 655 |
::quickcheck::quickcheck! { |
| 656 |
fn quickcheck_swap(x: $int_type, y: $int_type) -> bool { |
| 657 |
for &order in &helper::SWAP_ORDERINGS { |
| 658 |
let a = <$atomic_type>::new(x); |
| 659 |
assert_eq!(a.swap(y, order), x); |
| 660 |
assert_eq!(a.swap(x, order), y); |
| 661 |
} |
| 662 |
true |
| 663 |
} |
| 664 |
fn quickcheck_fetch_add(x: $int_type, y: $int_type) -> bool { |
| 665 |
for &order in &helper::SWAP_ORDERINGS { |
| 666 |
let a = <$atomic_type>::new(x); |
| 667 |
assert_eq!(a.fetch_add(y, order), x); |
| 668 |
assert_eq!(a.load(Ordering::Relaxed), x.wrapping_add(y)); |
| 669 |
let a = <$atomic_type>::new(y); |
| 670 |
assert_eq!(a.fetch_add(x, order), y); |
| 671 |
assert_eq!(a.load(Ordering::Relaxed), y.wrapping_add(x)); |
| 672 |
} |
| 673 |
true |
| 674 |
} |
| 675 |
fn quickcheck_fetch_sub(x: $int_type, y: $int_type) -> bool { |
| 676 |
for &order in &helper::SWAP_ORDERINGS { |
| 677 |
let a = <$atomic_type>::new(x); |
| 678 |
assert_eq!(a.fetch_sub(y, order), x); |
| 679 |
assert_eq!(a.load(Ordering::Relaxed), x.wrapping_sub(y)); |
| 680 |
let a = <$atomic_type>::new(y); |
| 681 |
assert_eq!(a.fetch_sub(x, order), y); |
| 682 |
assert_eq!(a.load(Ordering::Relaxed), y.wrapping_sub(x)); |
| 683 |
} |
| 684 |
true |
| 685 |
} |
| 686 |
fn quickcheck_fetch_and(x: $int_type, y: $int_type) -> bool { |
| 687 |
for &order in &helper::SWAP_ORDERINGS { |
| 688 |
let a = <$atomic_type>::new(x); |
| 689 |
assert_eq!(a.fetch_and(y, order), x); |
| 690 |
assert_eq!(a.load(Ordering::Relaxed), x & y); |
| 691 |
let a = <$atomic_type>::new(y); |
| 692 |
assert_eq!(a.fetch_and(x, order), y); |
| 693 |
assert_eq!(a.load(Ordering::Relaxed), y & x); |
| 694 |
} |
| 695 |
true |
| 696 |
} |
| 697 |
fn quickcheck_fetch_or(x: $int_type, y: $int_type) -> bool { |
| 698 |
for &order in &helper::SWAP_ORDERINGS { |
| 699 |
let a = <$atomic_type>::new(x); |
| 700 |
assert_eq!(a.fetch_or(y, order), x); |
| 701 |
assert_eq!(a.load(Ordering::Relaxed), x | y); |
| 702 |
let a = <$atomic_type>::new(y); |
| 703 |
assert_eq!(a.fetch_or(x, order), y); |
| 704 |
assert_eq!(a.load(Ordering::Relaxed), y | x); |
| 705 |
} |
| 706 |
true |
| 707 |
} |
| 708 |
fn quickcheck_fetch_xor(x: $int_type, y: $int_type) -> bool { |
| 709 |
for &order in &helper::SWAP_ORDERINGS { |
| 710 |
let a = <$atomic_type>::new(x); |
| 711 |
assert_eq!(a.fetch_xor(y, order), x); |
| 712 |
assert_eq!(a.load(Ordering::Relaxed), x ^ y); |
| 713 |
let a = <$atomic_type>::new(y); |
| 714 |
assert_eq!(a.fetch_xor(x, order), y); |
| 715 |
assert_eq!(a.load(Ordering::Relaxed), y ^ x); |
| 716 |
} |
| 717 |
true |
| 718 |
} |
| 719 |
fn quickcheck_fetch_max(x: $int_type, y: $int_type) -> bool { |
| 720 |
for &order in &helper::SWAP_ORDERINGS { |
| 721 |
let a = <$atomic_type>::new(x); |
| 722 |
assert_eq!(a.fetch_max(y, order), x); |
| 723 |
assert_eq!(a.load(Ordering::Relaxed), core::cmp::max(x, y)); |
| 724 |
let a = <$atomic_type>::new(y); |
| 725 |
assert_eq!(a.fetch_max(x, order), y); |
| 726 |
assert_eq!(a.load(Ordering::Relaxed), core::cmp::max(y, x)); |
| 727 |
} |
| 728 |
true |
| 729 |
} |
| 730 |
fn quickcheck_fetch_min(x: $int_type, y: $int_type) -> bool { |
| 731 |
for &order in &helper::SWAP_ORDERINGS { |
| 732 |
let a = <$atomic_type>::new(x); |
| 733 |
assert_eq!(a.fetch_min(y, order), x); |
| 734 |
assert_eq!(a.load(Ordering::Relaxed), core::cmp::min(x, y)); |
| 735 |
let a = <$atomic_type>::new(y); |
| 736 |
assert_eq!(a.fetch_min(x, order), y); |
| 737 |
assert_eq!(a.load(Ordering::Relaxed), core::cmp::min(y, x)); |
| 738 |
} |
| 739 |
true |
| 740 |
} |
| 741 |
fn quickcheck_fetch_not(x: $int_type) -> bool { |
| 742 |
for &order in &helper::SWAP_ORDERINGS { |
| 743 |
let a = <$atomic_type>::new(x); |
| 744 |
assert_eq!(a.fetch_not(order), x); |
| 745 |
assert_eq!(a.load(Ordering::Relaxed), !x); |
| 746 |
assert_eq!(a.fetch_not(order), !x); |
| 747 |
assert_eq!(a.load(Ordering::Relaxed), x); |
| 748 |
} |
| 749 |
true |
| 750 |
} |
| 751 |
} |
| 752 |
}; |
| 753 |
} |
| 754 |
macro_rules! test_atomic_int_amo_sub_word { |
| 755 |
($int_type:ident) => { |
| 756 |
paste::paste! { |
| 757 |
#[allow( |
| 758 |
clippy::alloc_instead_of_core, |
| 759 |
clippy::std_instead_of_alloc, |
| 760 |
clippy::std_instead_of_core, |
| 761 |
clippy::undocumented_unsafe_blocks |
| 762 |
)] |
| 763 |
mod [<test_atomic_ $int_type _amo>] { |
| 764 |
use super::*; |
| 765 |
#[cfg(any(target_feature = "zabha", portable_atomic_target_feature = "zabha"))] |
| 766 |
test_atomic_int_amo!([<Atomic $int_type:camel>], $int_type); |
| 767 |
#[cfg(not(any(target_feature = "zabha", portable_atomic_target_feature = "zabha")))] |
| 768 |
test_atomic_int_amo_sub_word!([<Atomic $int_type:camel>], $int_type); |
| 769 |
} |
| 770 |
} |
| 771 |
}; |
| 772 |
($atomic_type:ty, $int_type:ident) => { |
| 773 |
use crate::tests::helper::{self, *}; |
| 774 |
::quickcheck::quickcheck! { |
| 775 |
fn quickcheck_fetch_and(x: $int_type, y: $int_type) -> bool { |
| 776 |
let mut rng = fastrand::Rng::new(); |
| 777 |
for &order in &helper::SWAP_ORDERINGS { |
| 778 |
for base in [0, !0] { |
| 779 |
let mut arr = Align16([ |
| 780 |
<$atomic_type>::new(base), |
| 781 |
<$atomic_type>::new(base), |
| 782 |
<$atomic_type>::new(base), |
| 783 |
<$atomic_type>::new(base), |
| 784 |
<$atomic_type>::new(base), |
| 785 |
<$atomic_type>::new(base), |
| 786 |
<$atomic_type>::new(base), |
| 787 |
<$atomic_type>::new(base), |
| 788 |
<$atomic_type>::new(base), |
| 789 |
<$atomic_type>::new(base), |
| 790 |
]); |
| 791 |
let a_idx = rng.usize(3..=6); |
| 792 |
arr.0[a_idx] = <$atomic_type>::new(x); |
| 793 |
let a = &arr.0[a_idx]; |
| 794 |
assert_eq!(a.fetch_and(y, order), x); |
| 795 |
assert_eq!(a.load(Ordering::Relaxed), x & y); |
| 796 |
for i in 0..a_idx { |
| 797 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 798 |
} |
| 799 |
for i in a_idx + 1..arr.0.len() { |
| 800 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 801 |
} |
| 802 |
arr.0[a_idx] = <$atomic_type>::new(y); |
| 803 |
let a = &arr.0[a_idx]; |
| 804 |
assert_eq!(a.fetch_and(x, order), y); |
| 805 |
assert_eq!(a.load(Ordering::Relaxed), y & x); |
| 806 |
for i in 0..a_idx { |
| 807 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 808 |
} |
| 809 |
for i in a_idx + 1..arr.0.len() { |
| 810 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
true |
| 815 |
} |
| 816 |
fn quickcheck_fetch_or(x: $int_type, y: $int_type) -> bool { |
| 817 |
let mut rng = fastrand::Rng::new(); |
| 818 |
for &order in &helper::SWAP_ORDERINGS { |
| 819 |
for base in [0, !0] { |
| 820 |
let mut arr = Align16([ |
| 821 |
<$atomic_type>::new(base), |
| 822 |
<$atomic_type>::new(base), |
| 823 |
<$atomic_type>::new(base), |
| 824 |
<$atomic_type>::new(base), |
| 825 |
<$atomic_type>::new(base), |
| 826 |
<$atomic_type>::new(base), |
| 827 |
<$atomic_type>::new(base), |
| 828 |
<$atomic_type>::new(base), |
| 829 |
<$atomic_type>::new(base), |
| 830 |
<$atomic_type>::new(base), |
| 831 |
]); |
| 832 |
let a_idx = rng.usize(3..=6); |
| 833 |
arr.0[a_idx] = <$atomic_type>::new(x); |
| 834 |
let a = &arr.0[a_idx]; |
| 835 |
assert_eq!(a.fetch_or(y, order), x); |
| 836 |
assert_eq!(a.load(Ordering::Relaxed), x | y); |
| 837 |
for i in 0..a_idx { |
| 838 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 839 |
} |
| 840 |
for i in a_idx + 1..arr.0.len() { |
| 841 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 842 |
} |
| 843 |
arr.0[a_idx] = <$atomic_type>::new(y); |
| 844 |
let a = &arr.0[a_idx]; |
| 845 |
assert_eq!(a.fetch_or(x, order), y); |
| 846 |
assert_eq!(a.load(Ordering::Relaxed), y | x); |
| 847 |
for i in 0..a_idx { |
| 848 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 849 |
} |
| 850 |
for i in a_idx + 1..arr.0.len() { |
| 851 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
true |
| 856 |
} |
| 857 |
fn quickcheck_fetch_xor(x: $int_type, y: $int_type) -> bool { |
| 858 |
let mut rng = fastrand::Rng::new(); |
| 859 |
for &order in &helper::SWAP_ORDERINGS { |
| 860 |
for base in [0, !0] { |
| 861 |
let mut arr = Align16([ |
| 862 |
<$atomic_type>::new(base), |
| 863 |
<$atomic_type>::new(base), |
| 864 |
<$atomic_type>::new(base), |
| 865 |
<$atomic_type>::new(base), |
| 866 |
<$atomic_type>::new(base), |
| 867 |
<$atomic_type>::new(base), |
| 868 |
<$atomic_type>::new(base), |
| 869 |
<$atomic_type>::new(base), |
| 870 |
<$atomic_type>::new(base), |
| 871 |
<$atomic_type>::new(base), |
| 872 |
]); |
| 873 |
let a_idx = rng.usize(3..=6); |
| 874 |
arr.0[a_idx] = <$atomic_type>::new(x); |
| 875 |
let a = &arr.0[a_idx]; |
| 876 |
assert_eq!(a.fetch_xor(y, order), x); |
| 877 |
assert_eq!(a.load(Ordering::Relaxed), x ^ y); |
| 878 |
for i in 0..a_idx { |
| 879 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 880 |
} |
| 881 |
for i in a_idx + 1..arr.0.len() { |
| 882 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 883 |
} |
| 884 |
arr.0[a_idx] = <$atomic_type>::new(y); |
| 885 |
let a = &arr.0[a_idx]; |
| 886 |
assert_eq!(a.fetch_xor(x, order), y); |
| 887 |
assert_eq!(a.load(Ordering::Relaxed), y ^ x); |
| 888 |
for i in 0..a_idx { |
| 889 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 890 |
} |
| 891 |
for i in a_idx + 1..arr.0.len() { |
| 892 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
true |
| 897 |
} |
| 898 |
fn quickcheck_fetch_not(x: $int_type) -> bool { |
| 899 |
let mut rng = fastrand::Rng::new(); |
| 900 |
for &order in &helper::SWAP_ORDERINGS { |
| 901 |
for base in [0, !0] { |
| 902 |
let mut arr = Align16([ |
| 903 |
<$atomic_type>::new(base), |
| 904 |
<$atomic_type>::new(base), |
| 905 |
<$atomic_type>::new(base), |
| 906 |
<$atomic_type>::new(base), |
| 907 |
<$atomic_type>::new(base), |
| 908 |
<$atomic_type>::new(base), |
| 909 |
<$atomic_type>::new(base), |
| 910 |
<$atomic_type>::new(base), |
| 911 |
<$atomic_type>::new(base), |
| 912 |
<$atomic_type>::new(base), |
| 913 |
]); |
| 914 |
let a_idx = rng.usize(3..=6); |
| 915 |
arr.0[a_idx] = <$atomic_type>::new(x); |
| 916 |
let a = &arr.0[a_idx]; |
| 917 |
assert_eq!(a.fetch_not(order), x); |
| 918 |
assert_eq!(a.load(Ordering::Relaxed), !x); |
| 919 |
assert_eq!(a.fetch_not(order), !x); |
| 920 |
assert_eq!(a.load(Ordering::Relaxed), x); |
| 921 |
for i in 0..a_idx { |
| 922 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 923 |
} |
| 924 |
for i in a_idx + 1..arr.0.len() { |
| 925 |
assert_eq!(arr.0[i].load(Ordering::Relaxed), base, "invalid value written"); |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
true |
| 930 |
} |
| 931 |
} |
| 932 |
}; |
| 933 |
} |
| 934 |
test_atomic_ptr_amo!(); |
| 935 |
test_atomic_int_amo_sub_word!(i8); |
| 936 |
test_atomic_int_amo_sub_word!(u8); |
| 937 |
test_atomic_int_amo_sub_word!(i16); |
| 938 |
test_atomic_int_amo_sub_word!(u16); |
| 939 |
test_atomic_int_amo!(i32); |
| 940 |
test_atomic_int_amo!(u32); |
| 941 |
#[cfg(target_arch = "riscv64")] |
| 942 |
test_atomic_int_amo!(i64); |
| 943 |
#[cfg(target_arch = "riscv64")] |
| 944 |
test_atomic_int_amo!(u64); |
| 945 |
test_atomic_int_amo!(isize); |
| 946 |
test_atomic_int_amo!(usize); |
| 947 |
} |