| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
128-bit atomic implementation on s390x. |
| 5 |
|
| 6 |
This architecture provides the following 128-bit atomic instructions: |
| 7 |
|
| 8 |
- LPQ/STPQ: load/store (arch1 or later, i.e., baseline) |
| 9 |
- CDSG: CAS (arch1 or later, i.e., baseline) |
| 10 |
|
| 11 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 12 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 13 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#s390x |
| 14 |
|
| 15 |
LLVM's minimal supported architecture level is arch8 (z10): |
| 16 |
https://github.com/llvm/llvm-project/blob/llvmorg-22.1.0-rc1/llvm/lib/Target/SystemZ/SystemZProcessors.td#L16-L17 |
| 17 |
This does not appear to have changed since the current s390x backend was added in LLVM 3.3: |
| 18 |
https://github.com/llvm/llvm-project/commit/5f613dfd1f7edb0ae95d521b7107b582d9df5103#diff-cbaef692b3958312e80fd5507a7e2aff071f1acb086f10e8a96bc06a7bb289db |
| 19 |
|
| 20 |
We could use asm byte_wise_atomic_load + asm atomic_compare_exchange pattern for RMW here, but using an |
| 21 |
inline assembly allows omitting the comparison of results and the storing/comparing of condition flags. |
| 22 |
|
| 23 |
Note: On Miri and ThreadSanitizer which do not support inline assembly, we don't use |
| 24 |
this module and use intrinsics.rs instead. |
| 25 |
|
| 26 |
Refs: |
| 27 |
- z/Architecture Principles of Operation, Fifteenth Edition (SA22-7832-14) |
| 28 |
https://www.ibm.com/docs/en/module_1678991624569/pdf/SA22-7832-14.pdf |
| 29 |
- atomic-maybe-uninit |
| 30 |
https://github.com/taiki-e/atomic-maybe-uninit |
| 31 |
|
| 32 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 33 |
*/ |
| 34 |
|
| 35 |
include!("macros.rs"); |
| 36 |
|
| 37 |
use core::{arch::asm, sync::atomic::Ordering}; |
| 38 |
|
| 39 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 40 |
use crate::utils::ptr::PtrExt as _; |
| 41 |
use crate::utils::{Pair, U128}; |
| 42 |
|
| 43 |
// bcr 14,0 requires fast-BCR-serialization facility added in arch9 (z196). |
| 44 |
#[cfg(any( |
| 45 |
target_feature = "fast-serialization", |
| 46 |
portable_atomic_target_feature = "fast-serialization", |
| 47 |
))] |
| 48 |
macro_rules! serialization { |
| 49 |
() => { |
| 50 |
"bcr 14, 0" |
| 51 |
}; |
| 52 |
} |
| 53 |
#[cfg(not(any( |
| 54 |
target_feature = "fast-serialization", |
| 55 |
portable_atomic_target_feature = "fast-serialization", |
| 56 |
)))] |
| 57 |
macro_rules! serialization { |
| 58 |
() => { |
| 59 |
"bcr 15, 0" |
| 60 |
}; |
| 61 |
} |
| 62 |
|
| 63 |
// Use distinct operands on z196 or later, otherwise split to lgr and $op. |
| 64 |
#[cfg(any(target_feature = "distinct-ops", portable_atomic_target_feature = "distinct-ops"))] |
| 65 |
macro_rules! distinct_op { |
| 66 |
($op:tt, $a0:tt, $a1:tt, $a2:tt) => { |
| 67 |
concat!($op, "k ", $a0, ", ", $a1, ", ", $a2) |
| 68 |
}; |
| 69 |
} |
| 70 |
#[cfg(not(any(target_feature = "distinct-ops", portable_atomic_target_feature = "distinct-ops")))] |
| 71 |
macro_rules! distinct_op { |
| 72 |
($op:tt, $a0:tt, $a1:tt, $a2:tt) => { |
| 73 |
concat!("lgr ", $a0, ", ", $a1, "\n", $op, " ", $a0, ", ", $a2) |
| 74 |
}; |
| 75 |
} |
| 76 |
|
| 77 |
// Use selgr$cond on z15 or later, otherwise split to locgr$cond and $op. |
| 78 |
#[cfg(any( |
| 79 |
target_feature = "miscellaneous-extensions-3", |
| 80 |
portable_atomic_target_feature = "miscellaneous-extensions-3", |
| 81 |
))] |
| 82 |
#[cfg(any( |
| 83 |
target_feature = "load-store-on-cond", |
| 84 |
portable_atomic_target_feature = "load-store-on-cond", |
| 85 |
))] |
| 86 |
macro_rules! select_op { |
| 87 |
($cond:tt, $a0:tt, $a1:tt, $a2:tt) => { |
| 88 |
concat!("selgr", $cond, " ", $a0, ", ", $a1, ", ", $a2) |
| 89 |
}; |
| 90 |
} |
| 91 |
#[cfg(not(any( |
| 92 |
target_feature = "miscellaneous-extensions-3", |
| 93 |
portable_atomic_target_feature = "miscellaneous-extensions-3", |
| 94 |
)))] |
| 95 |
#[cfg(any( |
| 96 |
target_feature = "load-store-on-cond", |
| 97 |
portable_atomic_target_feature = "load-store-on-cond", |
| 98 |
))] |
| 99 |
macro_rules! select_op { |
| 100 |
($cond:tt, $a0:tt, $a1:tt, $a2:tt) => { |
| 101 |
concat!("lgr ", $a0, ", ", $a2, "\n", "locgr", $cond, " ", $a0, ", ", $a1) |
| 102 |
}; |
| 103 |
} |
| 104 |
|
| 105 |
// Extracts and checks condition code. |
| 106 |
#[inline] |
| 107 |
fn extract_cc(r: i64) -> bool { |
| 108 |
r.wrapping_add(-268435456) & (1 << 31) != 0 |
| 109 |
} |
| 110 |
|
| 111 |
#[inline] |
| 112 |
unsafe fn atomic_load(src: *mut u128, _order: Ordering) -> u128 { |
| 113 |
debug_assert!(src.addr() % 16 == 0); |
| 114 |
let (out_hi, out_lo); |
| 115 |
|
| 116 |
// SAFETY: the caller must uphold the safety contract. |
| 117 |
unsafe { |
| 118 |
// atomic load is always SeqCst. |
| 119 |
asm!( |
| 120 |
"lpq %r0, 0({src})", // atomic { r0:r1 = *src } |
| 121 |
src = in(reg) ptr_reg!(src), |
| 122 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 123 |
out("r0") out_hi, |
| 124 |
out("r1") out_lo, |
| 125 |
options(nostack, preserves_flags), |
| 126 |
); |
| 127 |
U128 { pair: Pair { hi: out_hi, lo: out_lo } }.whole |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#[inline] |
| 132 |
unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) { |
| 133 |
debug_assert!(dst.addr() % 16 == 0); |
| 134 |
let val = U128 { whole: val }; |
| 135 |
|
| 136 |
// SAFETY: the caller must uphold the safety contract. |
| 137 |
unsafe { |
| 138 |
macro_rules! atomic_store { |
| 139 |
($acquire:expr) => { |
| 140 |
asm!( |
| 141 |
"stpq %r0, 0({dst})", // atomic { *dst = r0:r1 } |
| 142 |
$acquire, // fence |
| 143 |
dst = in(reg) ptr_reg!(dst), |
| 144 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 145 |
in("r0") val.pair.hi, |
| 146 |
in("r1") val.pair.lo, |
| 147 |
options(nostack, preserves_flags), |
| 148 |
) |
| 149 |
}; |
| 150 |
} |
| 151 |
match order { |
| 152 |
// Relaxed and Release stores are equivalent. |
| 153 |
Ordering::Relaxed | Ordering::Release => atomic_store!(""), |
| 154 |
Ordering::SeqCst => atomic_store!(serialization!()), |
| 155 |
_ => unreachable!(), |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#[inline] |
| 161 |
unsafe fn atomic_compare_exchange( |
| 162 |
dst: *mut u128, |
| 163 |
old: u128, |
| 164 |
new: u128, |
| 165 |
_success: Ordering, |
| 166 |
_failure: Ordering, |
| 167 |
) -> Result<u128, u128> { |
| 168 |
debug_assert!(dst.addr() % 16 == 0); |
| 169 |
let old = U128 { whole: old }; |
| 170 |
let new = U128 { whole: new }; |
| 171 |
let (prev_hi, prev_lo); |
| 172 |
let r; |
| 173 |
|
| 174 |
// SAFETY: the caller must uphold the safety contract. |
| 175 |
// CDSG has SeqCst semantics. |
| 176 |
let prev = unsafe { |
| 177 |
macro_rules! cmpxchg { |
| 178 |
($new_hi:tt, $new_lo:tt) => { |
| 179 |
asm!( |
| 180 |
concat!("cdsg %r0, %", $new_hi, ", 0({dst})"), // atomic { if *dst == r0:r1 { cc = 0; *dst = new_hi:new_lo } else { cc = 1; r0:r1 = *dst } } |
| 181 |
"ipm {r}", // r[:] = cc |
| 182 |
dst = in(reg) ptr_reg!(dst), |
| 183 |
r = lateout(reg) r, |
| 184 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 185 |
inout("r0") old.pair.hi => prev_hi, |
| 186 |
inout("r1") old.pair.lo => prev_lo, |
| 187 |
in($new_hi) new.pair.hi, |
| 188 |
in($new_lo) new.pair.lo, |
| 189 |
// Do not use `preserves_flags` because CDSG modifies the condition code. |
| 190 |
options(nostack), |
| 191 |
) |
| 192 |
}; |
| 193 |
} |
| 194 |
// r4 is stack pointer on z/OS. |
| 195 |
#[cfg(not(target_os = "zos"))] |
| 196 |
cmpxchg!("r4", "r5"); |
| 197 |
#[cfg(target_os = "zos")] |
| 198 |
cmpxchg!("r12", "r13"); |
| 199 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 200 |
}; |
| 201 |
if extract_cc(r) { Ok(prev) } else { Err(prev) } |
| 202 |
} |
| 203 |
|
| 204 |
// cdsg is always strong. |
| 205 |
use self::atomic_compare_exchange as atomic_compare_exchange_weak; |
| 206 |
|
| 207 |
#[inline] |
| 208 |
unsafe fn atomic_swap(dst: *mut u128, val: u128, _order: Ordering) -> u128 { |
| 209 |
debug_assert!(dst.addr() % 16 == 0); |
| 210 |
let val = U128 { whole: val }; |
| 211 |
let (mut prev_hi, mut prev_lo); |
| 212 |
|
| 213 |
// SAFETY: the caller must uphold the safety contract. |
| 214 |
// CDSG has SeqCst semantics. |
| 215 |
// |
| 216 |
// Do not use atomic_rmw_cas_3 because it needs extra LGR to implement swap. |
| 217 |
unsafe { |
| 218 |
macro_rules! swap { |
| 219 |
($val_hi:tt, $val_lo:tt) => { |
| 220 |
asm!( |
| 221 |
"lg %r1, 8({dst})", // atomic { r1 = *dst.byte_add(8) } |
| 222 |
"lg %r0, 0({dst})", // atomic { r0 = *dst } |
| 223 |
"2:", // 'retry: |
| 224 |
concat!("cdsg %r0, %", $val_hi, ", 0({dst})"), // atomic { if *dst == r0:r1 { cc = 0; *dst = val_hi:val_lo } else { cc = 1; r0:r1 = *dst } } |
| 225 |
"jl 2b", // if cc == 1 { jump 'retry } |
| 226 |
dst = in(reg) ptr_reg!(dst), |
| 227 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 228 |
out("r0") prev_hi, |
| 229 |
out("r1") prev_lo, |
| 230 |
in($val_hi) val.pair.hi, |
| 231 |
in($val_lo) val.pair.lo, |
| 232 |
// Do not use `preserves_flags` because CDSG modifies the condition code. |
| 233 |
options(nostack), |
| 234 |
) |
| 235 |
}; |
| 236 |
} |
| 237 |
// r4 is stack pointer on z/OS. |
| 238 |
#[cfg(not(target_os = "zos"))] |
| 239 |
swap!("r4", "r5"); |
| 240 |
#[cfg(target_os = "zos")] |
| 241 |
swap!("r12", "r13"); |
| 242 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/// Atomic RMW by CAS loop (3 arguments) |
| 247 |
/// `unsafe fn(dst: *mut u128, val: u128, order: Ordering) -> u128;` |
| 248 |
/// |
| 249 |
/// `$op` can use the following registers: |
| 250 |
/// - val_hi/val_lo pair: val argument (read-only for `$op`) |
| 251 |
/// - r0/r1 pair: previous value loaded (read-only for `$op`) |
| 252 |
/// - r12/r13 pair: new value that will be stored |
| 253 |
macro_rules! atomic_rmw_cas_3 { |
| 254 |
($name:ident, [$($reg:tt)*], $($op:tt)*) => { |
| 255 |
#[inline] |
| 256 |
unsafe fn $name(dst: *mut u128, val: u128, _order: Ordering) -> u128 { |
| 257 |
debug_assert!(dst.addr() % 16 == 0); |
| 258 |
let val = U128 { whole: val }; |
| 259 |
let (mut prev_hi, mut prev_lo); |
| 260 |
|
| 261 |
// SAFETY: the caller must uphold the safety contract. |
| 262 |
// CDSG has SeqCst semantics. |
| 263 |
unsafe { |
| 264 |
asm!( |
| 265 |
"lg %r1, 8({dst})", // atomic { r1 = *dst.byte_add(8) } |
| 266 |
"lg %r0, 0({dst})", // atomic { r0 = *dst } |
| 267 |
"2:", // 'retry: |
| 268 |
$($op)* |
| 269 |
"cdsg %r0, %r12, 0({dst})", // atomic { if *dst == r0:r1 { cc = 0; *dst = r12:r13 } else { cc = 1; r0:r1 = *dst } } |
| 270 |
"jl 2b", // if cc == 1 { jump 'retry } |
| 271 |
dst = in(reg) ptr_reg!(dst), |
| 272 |
val_hi = in(reg) val.pair.hi, |
| 273 |
val_lo = in(reg) val.pair.lo, |
| 274 |
$($reg)* |
| 275 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 276 |
out("r0") prev_hi, |
| 277 |
out("r1") prev_lo, |
| 278 |
out("r12") _, |
| 279 |
out("r13") _, |
| 280 |
// Do not use `preserves_flags` because CDSG modifies the condition code. |
| 281 |
options(nostack), |
| 282 |
); |
| 283 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 284 |
} |
| 285 |
} |
| 286 |
}; |
| 287 |
} |
| 288 |
/// Atomic RMW by CAS loop (2 arguments) |
| 289 |
/// `unsafe fn(dst: *mut u128, order: Ordering) -> u128;` |
| 290 |
/// |
| 291 |
/// `$op` can use the following registers: |
| 292 |
/// - r0/r1 pair: previous value loaded (read-only for `$op`) |
| 293 |
/// - r12/r13 pair: new value that will be stored |
| 294 |
macro_rules! atomic_rmw_cas_2 { |
| 295 |
($name:ident, [$($reg:tt)*], $($op:tt)*) => { |
| 296 |
#[inline] |
| 297 |
unsafe fn $name(dst: *mut u128, _order: Ordering) -> u128 { |
| 298 |
debug_assert!(dst.addr() % 16 == 0); |
| 299 |
let (mut prev_hi, mut prev_lo); |
| 300 |
|
| 301 |
// SAFETY: the caller must uphold the safety contract. |
| 302 |
// CDSG has SeqCst semantics. |
| 303 |
unsafe { |
| 304 |
asm!( |
| 305 |
"lg %r1, 8({dst})", // atomic { r1 = *dst.byte_add(8) } |
| 306 |
"lg %r0, 0({dst})", // atomic { r0 = *dst } |
| 307 |
"2:", // 'retry: |
| 308 |
$($op)* |
| 309 |
"cdsg %r0, %r12, 0({dst})", // atomic { if *dst == r0:r1 { cc = 0; *dst = r12:r13 } else { cc = 1; r0:r1 = *dst } } |
| 310 |
"jl 2b", // if cc == 1 { jump 'retry } |
| 311 |
dst = in(reg) ptr_reg!(dst), |
| 312 |
$($reg)* |
| 313 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 314 |
out("r0") prev_hi, |
| 315 |
out("r1") prev_lo, |
| 316 |
out("r12") _, |
| 317 |
out("r13") _, |
| 318 |
// Do not use `preserves_flags` because CDSG modifies the condition code. |
| 319 |
options(nostack), |
| 320 |
); |
| 321 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 322 |
} |
| 323 |
} |
| 324 |
}; |
| 325 |
} |
| 326 |
|
| 327 |
atomic_rmw_cas_3! { |
| 328 |
atomic_add, [], |
| 329 |
distinct_op!("algr", "%r13", "%r1", "{val_lo}"), // r13 = r1 + val_lo; cc = zero | carry |
| 330 |
"lgr %r12, %r0", // r12 = r0 |
| 331 |
"alcgr %r12, {val_hi}", // r12 += val_hi + carry |
| 332 |
} |
| 333 |
atomic_rmw_cas_3! { |
| 334 |
atomic_sub, [], |
| 335 |
distinct_op!("slgr", "%r13", "%r1", "{val_lo}"), // r13 = r1 - val_lo; cc = zero | borrow |
| 336 |
"lgr %r12, %r0", // r12 = r0 |
| 337 |
"slbgr %r12, {val_hi}", // r12 -= val_hi + borrow |
| 338 |
} |
| 339 |
atomic_rmw_cas_3! { |
| 340 |
atomic_and, [], |
| 341 |
distinct_op!("ngr", "%r13", "%r1", "{val_lo}"), // r13 = r1 & val_lo |
| 342 |
distinct_op!("ngr", "%r12", "%r0", "{val_hi}"), // r12 = r0 & val_hi |
| 343 |
} |
| 344 |
|
| 345 |
// Use nngrk on z15 or later. |
| 346 |
#[cfg(any( |
| 347 |
target_feature = "miscellaneous-extensions-3", |
| 348 |
portable_atomic_target_feature = "miscellaneous-extensions-3", |
| 349 |
))] |
| 350 |
atomic_rmw_cas_3! { |
| 351 |
atomic_nand, [], |
| 352 |
"nngrk %r13, %r1, {val_lo}", // r13 = !(r1 & val_lo) |
| 353 |
"nngrk %r12, %r0, {val_hi}", // r12 = !(r0 & val_hi) |
| 354 |
} |
| 355 |
#[cfg(not(any( |
| 356 |
target_feature = "miscellaneous-extensions-3", |
| 357 |
portable_atomic_target_feature = "miscellaneous-extensions-3", |
| 358 |
)))] |
| 359 |
atomic_rmw_cas_3! { |
| 360 |
atomic_nand, [], |
| 361 |
distinct_op!("ngr", "%r13", "%r1", "{val_lo}"), // r13 = r1 & val_lo |
| 362 |
distinct_op!("ngr", "%r12", "%r0", "{val_hi}"), // r12 = r0 & val_hi |
| 363 |
"lcgr %r13, %r13", // r13 = !r13 + 1 |
| 364 |
"aghi %r13, -1", // r13 -= 1 |
| 365 |
"lcgr %r12, %r12", // r12 = !r12 + 1 |
| 366 |
"aghi %r12, -1", // r12 -= 1 |
| 367 |
} |
| 368 |
|
| 369 |
atomic_rmw_cas_3! { |
| 370 |
atomic_or, [], |
| 371 |
distinct_op!("ogr", "%r13", "%r1", "{val_lo}"), // r13 = r1 | val_lo |
| 372 |
distinct_op!("ogr", "%r12", "%r0", "{val_hi}"), // r12 = r0 | val_hi |
| 373 |
} |
| 374 |
atomic_rmw_cas_3! { |
| 375 |
atomic_xor, [], |
| 376 |
distinct_op!("xgr", "%r13", "%r1", "{val_lo}"), // r13 = r1 ^ val_lo |
| 377 |
distinct_op!("xgr", "%r12", "%r0", "{val_hi}"), // r12 = r0 ^ val_hi |
| 378 |
} |
| 379 |
|
| 380 |
cfg_sel!({ |
| 381 |
#[cfg(any( |
| 382 |
target_feature = "load-store-on-cond", |
| 383 |
portable_atomic_target_feature = "load-store-on-cond", |
| 384 |
))] |
| 385 |
{ |
| 386 |
atomic_rmw_cas_3! { |
| 387 |
atomic_max, [], |
| 388 |
"clgr %r1, {val_lo}", // if r1(u) < val_lo(u) { cc = 1 } else if r1(u) > val_lo(u) { cc = 2 } else { cc = 0 } |
| 389 |
select_op!("h", "%r12", "%r1", "{val_lo}"), // if cc == 2 { r12 = r1 } else { r12 = val_lo } |
| 390 |
"cgr %r0, {val_hi}", // if r0(i) < val_hi(i) { cc = 1 } else if r0(i) > val_hi(i) { cc = 2 } else { cc = 0 } |
| 391 |
select_op!("h", "%r13", "%r1", "{val_lo}"), // if cc == 2 { r13 = r1 } else { r13 = val_lo } |
| 392 |
"locgre %r13, %r12", // if cc == 0 { r13 = r12 } |
| 393 |
select_op!("h", "%r12", "%r0", "{val_hi}"), // if cc == 2 { r12 = r0 } else { r12 = val_hi } |
| 394 |
} |
| 395 |
atomic_rmw_cas_3! { |
| 396 |
atomic_umax, [tmp = out(reg) _,], |
| 397 |
"clgr %r1, {val_lo}", // if r1(u) < val_lo(u) { cc = 1 } else if r1(u) > val_lo(u) { cc = 2 } else { cc = 0 } |
| 398 |
select_op!("h", "{tmp}", "%r1", "{val_lo}"), // if cc == 2 { tmp = r1 } else { tmp = val_lo } |
| 399 |
"clgr %r0, {val_hi}", // if r0(u) < val_hi(u) { cc = 1 } else if r0(u) > val_hi(u) { cc = 2 } else { cc = 0 } |
| 400 |
select_op!("h", "%r12", "%r0", "{val_hi}"), // if cc == 2 { r12 = r0 } else { r12 = val_hi } |
| 401 |
select_op!("h", "%r13", "%r1", "{val_lo}"), // if cc == 2 { r13 = r1 } else { r13 = val_lo } |
| 402 |
"cgr %r0, {val_hi}", // if r0(i) < val_hi(i) { cc = 1 } else if r0(i) > val_hi(i) { cc = 2 } else { cc = 0 } |
| 403 |
"locgre %r13, {tmp}", // if cc == 0 { r13 = tmp } |
| 404 |
} |
| 405 |
atomic_rmw_cas_3! { |
| 406 |
atomic_min, [], |
| 407 |
"clgr %r1, {val_lo}", // if r1(u) < val_lo(u) { cc = 1 } else if r1(u) > val_lo(u) { cc = 2 } else { cc = 0 } |
| 408 |
select_op!("l", "%r12", "%r1", "{val_lo}"), // if cc == 1 { r12 = r1 } else { r12 = val_lo } |
| 409 |
"cgr %r0, {val_hi}", // if r0(i) < val_hi(i) { cc = 1 } else if r0(i) > val_hi(i) { cc = 2 } else { cc = 0 } |
| 410 |
select_op!("l", "%r13", "%r1", "{val_lo}"), // if cc == 1 { r13 = r1 } else { r13 = val_lo } |
| 411 |
"locgre %r13, %r12", // if cc == 0 { r13 = r12 } |
| 412 |
select_op!("l", "%r12", "%r0", "{val_hi}"), // if cc == 1 { r12 = r0 } else { r12 = val_hi } |
| 413 |
} |
| 414 |
atomic_rmw_cas_3! { |
| 415 |
atomic_umin, [tmp = out(reg) _,], |
| 416 |
"clgr %r1, {val_lo}", // if r1(u) < val_lo(u) { cc = 1 } else if r1(u) > val_lo(u) { cc = 2 } else { cc = 0 } |
| 417 |
select_op!("l", "{tmp}", "%r1", "{val_lo}"), // if cc == 1 { tmp = r1 } else { tmp = val_lo } |
| 418 |
"clgr %r0, {val_hi}", // if r0(u) < val_hi(u) { cc = 1 } else if r0(u) > val_hi(u) { cc = 2 } else { cc = 0 } |
| 419 |
select_op!("l", "%r12", "%r0", "{val_hi}"), // if cc == 1 { r12 = r0 } else { r12 = val_hi } |
| 420 |
select_op!("l", "%r13", "%r1", "{val_lo}"), // if cc == 1 { r13 = r1 } else { r13 = val_lo } |
| 421 |
"cgr %r0, {val_hi}", // if r0(i) < val_hi(i) { cc = 1 } else if r0(i) > val_hi(i) { cc = 2 } else { cc = 0 } |
| 422 |
"locgre %r13, {tmp}", // if cc == 0 { r13 = tmp } |
| 423 |
} |
| 424 |
} |
| 425 |
#[cfg(else)] |
| 426 |
{ |
| 427 |
#[rustfmt::skip] |
| 428 |
macro_rules! atomic_cmp { |
| 429 |
($name:ident, $cmp_hi1:tt, $cmp_hi2:tt, $cmp_lo:tt,) => { |
| 430 |
#[inline] |
| 431 |
unsafe fn $name(dst: *mut u128, val: u128, _order: Ordering) -> u128 { |
| 432 |
debug_assert!(dst.addr() % 16 == 0); |
| 433 |
let val = U128 { whole: val }; |
| 434 |
let (mut prev_hi, mut prev_lo); |
| 435 |
|
| 436 |
// SAFETY: the caller must uphold the safety contract. |
| 437 |
// CDSG has SeqCst semantics. |
| 438 |
unsafe { |
| 439 |
macro_rules! cmp { |
| 440 |
($val_hi:tt, $val_lo:tt) => { |
| 441 |
asm!( |
| 442 |
"lg %r1, 8({dst})", // atomic { r1 = *dst.byte_add(8) } |
| 443 |
"lg %r0, 0({dst})", // atomic { r0 = *dst } |
| 444 |
"2:", // 'retry: |
| 445 |
concat!($cmp_hi1, " %", $val_hi, ", %r0, 4f"), // if $cmp_hi1(val_hi, r0) { jump 'val } |
| 446 |
concat!($cmp_hi2, " %", $val_hi, ", %r0, 3f"), // if $cmp_hi2(val_hi, r0) { jump 'prev } |
| 447 |
concat!($cmp_lo, " %", $val_lo, ", %r1, 4f"), // if $cmp_lo(val_lo, r1) { jump 'val } |
| 448 |
"3:", // 'prev: |
| 449 |
"cdsg %r0, %r0, 0({dst})", // atomic { if *dst == r0:r1 { cc = 0; *dst = r0:r1 } else { cc = 1; r0:r1 = *dst } } |
| 450 |
"jl 2b", // if cc == 1 { jump 'retry } |
| 451 |
"j 5f", |
| 452 |
"4:", // 'val: |
| 453 |
concat!("cdsg %r0, %", $val_hi, ", 0({dst})"), // atomic { if *dst == r0:r1 { cc = 0; *dst = val_hi:val_lo } else { cc = 1; r0:r1 = *dst } } |
| 454 |
"jl 2b", // if cc == 1 { jump 'retry } |
| 455 |
"5:", |
| 456 |
dst = in(reg) ptr_reg!(dst), |
| 457 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 458 |
out("r0") prev_hi, |
| 459 |
out("r1") prev_lo, |
| 460 |
in($val_hi) val.pair.hi, |
| 461 |
in($val_lo) val.pair.lo, |
| 462 |
// Do not use `preserves_flags` because CDSG modifies the condition code. |
| 463 |
options(nostack), |
| 464 |
) |
| 465 |
}; |
| 466 |
} |
| 467 |
// r4 is stack pointer on z/OS. |
| 468 |
#[cfg(not(target_os = "zos"))] |
| 469 |
cmp!("r4", "r5"); |
| 470 |
#[cfg(target_os = "zos")] |
| 471 |
cmp!("r12", "r13"); |
| 472 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 473 |
} |
| 474 |
} |
| 475 |
}; |
| 476 |
} |
| 477 |
// loc{,g}r requires z196 or later. |
| 478 |
atomic_cmp! { |
| 479 |
atomic_max, |
| 480 |
"cgrjh", // signed > |
| 481 |
"cgrjl", // signed < |
| 482 |
"clgrjhe", // unsigned >= |
| 483 |
} |
| 484 |
atomic_cmp! { |
| 485 |
atomic_umax, |
| 486 |
"clgrjh", // unsigned > |
| 487 |
"clgrjl", // unsigned < |
| 488 |
"clgrjhe", // unsigned >= |
| 489 |
} |
| 490 |
atomic_cmp! { |
| 491 |
atomic_min, |
| 492 |
"cgrjl", // signed < |
| 493 |
"cgrjh", // signed > |
| 494 |
"clgrjle", // unsigned <= |
| 495 |
} |
| 496 |
atomic_cmp! { |
| 497 |
atomic_umin, |
| 498 |
"clgrjl", // unsigned < |
| 499 |
"clgrjh", // unsigned > |
| 500 |
"clgrjle", // unsigned <= |
| 501 |
} |
| 502 |
} |
| 503 |
}); |
| 504 |
|
| 505 |
atomic_rmw_cas_2! { |
| 506 |
atomic_not, [], |
| 507 |
"lcgr %r13, %r1", // r13 = !r1 + 1 |
| 508 |
"aghi %r13, -1", // r13 -= 1 |
| 509 |
"lcgr %r12, %r0", // r12 = !r0 + 1 |
| 510 |
"aghi %r12, -1", // r12 -= 1 |
| 511 |
} |
| 512 |
|
| 513 |
#[cfg(any(target_feature = "distinct-ops", portable_atomic_target_feature = "distinct-ops"))] |
| 514 |
atomic_rmw_cas_2! { |
| 515 |
atomic_neg, [zero = in(reg) 0_u64,], |
| 516 |
"slgrk %r13, {zero}, %r1", // r13 = 0 - r1; cc = zero | borrow |
| 517 |
"lghi %r12, 0", // r12 = 0 |
| 518 |
"slbgr %r12, %r0", // r12 -= r0 + borrow |
| 519 |
} |
| 520 |
#[cfg(not(any(target_feature = "distinct-ops", portable_atomic_target_feature = "distinct-ops")))] |
| 521 |
atomic_rmw_cas_2! { |
| 522 |
atomic_neg, [], |
| 523 |
"lghi %r13, 0", // r13 = 0 |
| 524 |
"slgr %r13, %r1", // r13 -= r1; cc = zero | borrow |
| 525 |
"lghi %r12, 0", // r12 = 0 |
| 526 |
"slbgr %r12, %r0", // r12 -= r0 + borrow |
| 527 |
} |
| 528 |
|
| 529 |
#[inline] |
| 530 |
const fn is_lock_free() -> bool { |
| 531 |
IS_ALWAYS_LOCK_FREE |
| 532 |
} |
| 533 |
const IS_ALWAYS_LOCK_FREE: bool = true; |
| 534 |
|
| 535 |
atomic128!(AtomicI128, i128, atomic_max, atomic_min); |
| 536 |
atomic128!(AtomicU128, u128, atomic_umax, atomic_umin); |
| 537 |
|
| 538 |
#[cfg(test)] |
| 539 |
mod tests { |
| 540 |
use super::*; |
| 541 |
|
| 542 |
test_atomic_int!(i128); |
| 543 |
test_atomic_int!(u128); |
| 544 |
|
| 545 |
// load/store/swap implementation is not affected by signedness, so it is |
| 546 |
// enough to test only unsigned types. |
| 547 |
stress_test!(u128); |
| 548 |
} |