|
8 | 8 |
|
9 | 9 | use crate::{Cmov, CmovEq, Condition}; |
10 | 10 |
|
11 | | -/// Bitwise non-zero: returns `1` if `x != 0`, and otherwise returns `0`. |
12 | | -macro_rules! bitnz { |
13 | | - ($value:expr, $bits:expr) => { |
14 | | - core::hint::black_box(($value | $value.wrapping_neg()) >> ($bits - 1)) |
15 | | - }; |
16 | | -} |
17 | | - |
18 | 11 | impl Cmov for u16 { |
19 | 12 | #[inline] |
20 | 13 | fn cmovnz(&mut self, value: &Self, condition: Condition) { |
@@ -46,96 +39,112 @@ impl CmovEq for u16 { |
46 | 39 | impl Cmov for u32 { |
47 | 40 | #[inline] |
48 | 41 | fn cmovnz(&mut self, value: &Self, condition: Condition) { |
49 | | - let mask = nzmask32(condition); |
| 42 | + let mask = masknz32(condition); |
50 | 43 | *self = (*self & !mask) | (*value & mask); |
51 | 44 | } |
52 | 45 |
|
53 | 46 | #[inline] |
54 | 47 | fn cmovz(&mut self, value: &Self, condition: Condition) { |
55 | | - let mask = nzmask32(condition); |
| 48 | + let mask = masknz32(condition); |
56 | 49 | *self = (*self & mask) | (*value & !mask); |
57 | 50 | } |
58 | 51 | } |
59 | 52 |
|
60 | 53 | impl CmovEq for u32 { |
61 | 54 | #[inline] |
62 | 55 | fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) { |
63 | | - let xor = self ^ rhs; |
64 | | - let ne = bitnz!(xor, u32::BITS) as u8; |
| 56 | + let ne = testnz32(self ^ rhs) as u8; |
65 | 57 | output.cmovnz(&input, ne); |
66 | 58 | } |
67 | 59 |
|
68 | 60 | #[inline] |
69 | 61 | fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) { |
70 | | - let xor = self ^ rhs; |
71 | | - let ne = bitnz!(xor, u32::BITS) as u8; |
| 62 | + let ne = testnz32(self ^ rhs) as u8; |
72 | 63 | output.cmovnz(&input, ne ^ 1); |
73 | 64 | } |
74 | 65 | } |
75 | 66 |
|
76 | 67 | impl Cmov for u64 { |
77 | 68 | #[inline] |
78 | 69 | fn cmovnz(&mut self, value: &Self, condition: Condition) { |
79 | | - let mask = nzmask64(condition); |
| 70 | + let mask = masknz64(condition); |
80 | 71 | *self = (*self & !mask) | (*value & mask); |
81 | 72 | } |
82 | 73 |
|
83 | 74 | #[inline] |
84 | 75 | fn cmovz(&mut self, value: &Self, condition: Condition) { |
85 | | - let mask = nzmask64(condition); |
| 76 | + let mask = masknz64(condition); |
86 | 77 | *self = (*self & mask) | (*value & !mask); |
87 | 78 | } |
88 | 79 | } |
89 | 80 |
|
90 | 81 | impl CmovEq for u64 { |
91 | 82 | #[inline] |
92 | 83 | fn cmovne(&self, rhs: &Self, input: Condition, output: &mut Condition) { |
93 | | - let xor = self ^ rhs; |
94 | | - let ne = bitnz!(xor, u64::BITS) as u8; |
| 84 | + let ne = testnz64(self ^ rhs) as u8; |
95 | 85 | output.cmovnz(&input, ne); |
96 | 86 | } |
97 | 87 |
|
98 | 88 | #[inline] |
99 | 89 | fn cmoveq(&self, rhs: &Self, input: Condition, output: &mut Condition) { |
100 | | - let xor = self ^ rhs; |
101 | | - let ne = bitnz!(xor, u64::BITS) as u8; |
| 90 | + let ne = testnz64(self ^ rhs) as u8; |
102 | 91 | output.cmovnz(&input, ne ^ 1); |
103 | 92 | } |
104 | 93 | } |
105 | 94 |
|
| 95 | +/// Returns `0` if `x` is `0`, otherwise returns `1` (32-bit version) |
| 96 | +pub fn testnz32(mut x: u32) -> u32 { |
| 97 | + x |= x.wrapping_neg(); |
| 98 | + core::hint::black_box(x >> (u32::BITS - 1)) |
| 99 | +} |
| 100 | + |
| 101 | +/// Returns `0` if `x` is `0`, otherwise returns `1` (64-bit version) |
| 102 | +pub fn testnz64(mut x: u64) -> u64 { |
| 103 | + x |= x.wrapping_neg(); |
| 104 | + core::hint::black_box(x >> (u64::BITS - 1)) |
| 105 | +} |
| 106 | + |
106 | 107 | /// Return a [`u32::MAX`] mask if `condition` is non-zero, otherwise return zero for a zero input. |
107 | | -pub fn nzmask32(condition: Condition) -> u32 { |
108 | | - bitnz!(condition as u32, u32::BITS).wrapping_neg() |
| 108 | +pub fn masknz32(condition: Condition) -> u32 { |
| 109 | + testnz32(condition as u32).wrapping_neg() |
109 | 110 | } |
110 | 111 |
|
111 | 112 | /// Return a [`u64::MAX`] mask if `condition` is non-zero, otherwise return zero for a zero input. |
112 | | -pub fn nzmask64(condition: Condition) -> u64 { |
113 | | - bitnz!(condition as u64, u64::BITS).wrapping_neg() |
| 113 | +pub fn masknz64(condition: Condition) -> u64 { |
| 114 | + testnz64(condition as u64).wrapping_neg() |
114 | 115 | } |
115 | 116 |
|
116 | 117 | #[cfg(test)] |
117 | 118 | mod tests { |
118 | 119 | #[test] |
119 | | - fn bitnz() { |
120 | | - assert_eq!(bitnz!(0u8, u8::BITS), 0); |
| 120 | + fn testnz32() { |
| 121 | + assert_eq!(super::testnz32(0), 0); |
| 122 | + for i in 1..=u8::MAX { |
| 123 | + assert_eq!(super::testnz32(i as u32), 1); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn testnz64() { |
| 129 | + assert_eq!(super::testnz64(0), 0); |
121 | 130 | for i in 1..=u8::MAX { |
122 | | - assert_eq!(bitnz!(i, u8::BITS), 1); |
| 131 | + assert_eq!(super::testnz64(i as u64), 1); |
123 | 132 | } |
124 | 133 | } |
125 | 134 |
|
126 | 135 | #[test] |
127 | | - fn nzmask32() { |
128 | | - assert_eq!(super::nzmask32(0), 0); |
| 136 | + fn masknz32() { |
| 137 | + assert_eq!(super::masknz32(0), 0); |
129 | 138 | for i in 1..=u8::MAX { |
130 | | - assert_eq!(super::nzmask32(i), u32::MAX); |
| 139 | + assert_eq!(super::masknz32(i), u32::MAX); |
131 | 140 | } |
132 | 141 | } |
133 | 142 |
|
134 | 143 | #[test] |
135 | | - fn nzmask64() { |
136 | | - assert_eq!(super::nzmask64(0), 0); |
| 144 | + fn masknz64() { |
| 145 | + assert_eq!(super::masknz64(0), 0); |
137 | 146 | for i in 1..=u8::MAX { |
138 | | - assert_eq!(super::nzmask64(i), u64::MAX); |
| 147 | + assert_eq!(super::masknz64(i), u64::MAX); |
139 | 148 | } |
140 | 149 | } |
141 | 150 | } |
0 commit comments