Skip to content

Commit

Permalink
Simplify creation of masks
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 31, 2023
1 parent 0bb2531 commit 9e01234
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/imp/atomic128/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ unsafe fn atomic_umin(dst: *mut u128, val: u128, order: Ordering) -> u128 {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn atomic_not(dst: *mut u128, order: Ordering) -> u128 {
// SAFETY: the caller must uphold the safety contract.
unsafe { atomic_xor(dst, u128::MAX, order) }
unsafe { atomic_xor(dst, !0, order) }
}

#[cfg(not(any(target_arch = "x86_64", target_arch = "s390x")))]
Expand Down
2 changes: 1 addition & 1 deletion src/imp/atomic128/powerpc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ use atomic_not_pwr8 as atomic_not;
#[inline]
unsafe fn atomic_not_pwr8(dst: *mut u128, order: Ordering) -> u128 {
// SAFETY: the caller must uphold the safety contract.
unsafe { atomic_xor_pwr8(dst, u128::MAX, order) }
unsafe { atomic_xor_pwr8(dst, !0, order) }
}

#[cfg(portable_atomic_llvm_16)]
Expand Down
3 changes: 1 addition & 2 deletions src/imp/core_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ macro_rules! atomic_int {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_not(&self, order: Ordering) -> $int_type {
const NOT_MASK: $int_type = (0 as $int_type).wrapping_sub(1);
self.fetch_xor(NOT_MASK, order)
self.fetch_xor(!0, order)
}
#[cfg(not(all(
any(target_arch = "x86", target_arch = "x86_64"),
Expand Down
6 changes: 2 additions & 4 deletions src/imp/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ macro_rules! atomic {

#[inline]
pub(crate) fn fetch_not(&self, order: Ordering) -> $value_type {
const NOT_MASK: $value_type = (0 as $value_type).wrapping_sub(1);
self.fetch_xor(NOT_MASK, order)
self.fetch_xor(!0, order)
}

#[inline]
Expand Down Expand Up @@ -350,8 +349,7 @@ macro_rules! atomic_sub_word {

#[inline]
pub(crate) fn fetch_not(&self, order: Ordering) -> $value_type {
const NOT_MASK: $value_type = (0 as $value_type).wrapping_sub(1);
self.fetch_xor(NOT_MASK, order)
self.fetch_xor(!0, order)
}
}
};
Expand Down
30 changes: 12 additions & 18 deletions src/tests/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,9 @@ macro_rules! __test_atomic_int {
assert_eq!(a.load(Ordering::Relaxed), 1);
assert_eq!(a.fetch_max(0, order), 1);
assert_eq!(a.load(Ordering::Relaxed), 1);
let a = <$atomic_type>::new((0 as $int_type).wrapping_sub(1));
assert_eq!(a.fetch_max(0, order), (0 as $int_type).wrapping_sub(1));
assert_eq!(
a.load(Ordering::Relaxed),
core::cmp::max((0 as $int_type).wrapping_sub(1), 0)
);
let a = <$atomic_type>::new(!0);
assert_eq!(a.fetch_max(0, order), !0);
assert_eq!(a.load(Ordering::Relaxed), core::cmp::max(!0, 0));
}
}
#[test]
Expand All @@ -460,12 +457,9 @@ macro_rules! __test_atomic_int {
assert_eq!(a.load(Ordering::Relaxed), 0);
assert_eq!(a.fetch_min(1, order), 0);
assert_eq!(a.load(Ordering::Relaxed), 0);
let a = <$atomic_type>::new((0 as $int_type).wrapping_sub(1));
assert_eq!(a.fetch_min(0, order), (0 as $int_type).wrapping_sub(1));
assert_eq!(
a.load(Ordering::Relaxed),
core::cmp::min((0 as $int_type).wrapping_sub(1), 0)
);
let a = <$atomic_type>::new(!0);
assert_eq!(a.fetch_min(0, order), !0);
assert_eq!(a.load(Ordering::Relaxed), core::cmp::min(!0, 0));
}
}
#[test]
Expand Down Expand Up @@ -495,8 +489,8 @@ macro_rules! __test_atomic_int {
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(5);
assert_eq!(a.fetch_neg(order), 5);
assert_eq!(a.load(Ordering::Relaxed), (5 as $int_type).wrapping_neg());
assert_eq!(a.fetch_neg(order), (5 as $int_type).wrapping_neg());
assert_eq!(a.load(Ordering::Relaxed), <$int_type>::wrapping_neg(5));
assert_eq!(a.fetch_neg(order), <$int_type>::wrapping_neg(5));
assert_eq!(a.load(Ordering::Relaxed), 5);
let a = <$atomic_type>::new(<$int_type>::MIN);
assert_eq!(a.fetch_neg(order), <$int_type>::MIN);
Expand All @@ -512,7 +506,7 @@ macro_rules! __test_atomic_int {
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(5);
a.neg(order);
assert_eq!(a.load(Ordering::Relaxed), (5 as $int_type).wrapping_neg());
assert_eq!(a.load(Ordering::Relaxed), <$int_type>::wrapping_neg(5));
a.neg(order);
assert_eq!(a.load(Ordering::Relaxed), 5);
let a = <$atomic_type>::new(<$int_type>::MIN);
Expand Down Expand Up @@ -782,7 +776,7 @@ macro_rules! __test_atomic_int {
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(x);
let b = a.bit_set(bit, order);
let mask = (1 as $int_type).wrapping_shl(bit);
let mask = <$int_type>::wrapping_shl(1, bit);
assert_eq!(a.load(Ordering::Relaxed), x | mask);
assert_eq!(b, x & mask != 0);
}
Expand All @@ -792,7 +786,7 @@ macro_rules! __test_atomic_int {
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(x);
let b = a.bit_clear(bit, order);
let mask = (1 as $int_type).wrapping_shl(bit);
let mask = <$int_type>::wrapping_shl(1, bit);
assert_eq!(a.load(Ordering::Relaxed), x & !mask);
assert_eq!(b, x & mask != 0);
}
Expand All @@ -802,7 +796,7 @@ macro_rules! __test_atomic_int {
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(x);
let b = a.bit_toggle(bit, order);
let mask = (1 as $int_type).wrapping_shl(bit);
let mask = <$int_type>::wrapping_shl(1, bit);
assert_eq!(a.load(Ordering::Relaxed), x ^ mask);
assert_eq!(b, x & mask != 0);
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,19 @@ macro_rules! impl_default_bit_opts {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn bit_set(&self, bit: u32, order: Ordering) -> bool {
let mask = (1 as $int_type).wrapping_shl(bit);
let mask = <$int_type>::wrapping_shl(1, bit);
self.fetch_or(mask, order) & mask != 0
}
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn bit_clear(&self, bit: u32, order: Ordering) -> bool {
let mask = (1 as $int_type).wrapping_shl(bit);
let mask = <$int_type>::wrapping_shl(1, bit);
self.fetch_and(!mask, order) & mask != 0
}
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn bit_toggle(&self, bit: u32, order: Ordering) -> bool {
let mask = (1 as $int_type).wrapping_shl(bit);
let mask = <$int_type>::wrapping_shl(1, bit);
self.fetch_xor(mask, order) & mask != 0
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/api-test/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ macro_rules! __test_atomic_int {
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(5);
assert_eq!(a.fetch_neg(order), 5);
assert_eq!(a.load(Ordering::Relaxed), (5 as $int_type).wrapping_neg());
assert_eq!(a.fetch_neg(order), (5 as $int_type).wrapping_neg());
assert_eq!(a.load(Ordering::Relaxed), <$int_type>::wrapping_neg(5));
assert_eq!(a.fetch_neg(order), <$int_type>::wrapping_neg(5));
assert_eq!(a.load(Ordering::Relaxed), 5);
}
}
Expand All @@ -253,7 +253,7 @@ macro_rules! __test_atomic_int {
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(5);
a.neg(order);
assert_eq!(a.load(Ordering::Relaxed), (5 as $int_type).wrapping_neg());
assert_eq!(a.load(Ordering::Relaxed), <$int_type>::wrapping_neg(5));
a.neg(order);
assert_eq!(a.load(Ordering::Relaxed), 5);
}
Expand Down

0 comments on commit 9e01234

Please sign in to comment.