|
1 |
| -#![feature(core_intrinsics)] |
2 |
| - |
3 |
| -fn i128_to_u64(u: i128) -> Option<u64> { |
4 |
| - let min = u64::MIN as i128; |
5 |
| - //let max = u64::MAX as i128; |
6 |
| - let max = 18446744073709551612_i128; |
7 |
| - //println!("{} < {} => {}", u, min, u < min); |
8 |
| - //println!("max: {:b}", u64::MAX); |
9 |
| - println!("max: {:b}", max); |
10 |
| - println!("max: {}", max); |
11 |
| - //println!("{}", u < min); |
12 |
| - //println!("{}", u > max); |
13 |
| - if u < min || u > max { |
14 |
| - None |
15 |
| - } else { |
16 |
| - Some(u as u64) |
17 |
| - } |
18 |
| -} |
| 1 | +#![feature(const_option)] |
| 2 | + |
| 3 | +use std::num::{NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize}; |
19 | 4 |
|
20 | 5 | fn main() {
|
21 | 6 | /*test_float!(f64, f64, f64::INFINITY, f64::NEG_INFINITY, f64::NAN);
|
@@ -58,21 +43,6 @@ fn main() {
|
58 | 43 | }
|
59 | 44 | println!("{}", 9.4f64);*/
|
60 | 45 |
|
61 |
| - let mut value = 0; |
62 |
| - let res = unsafe { std::intrinsics::atomic_cxchg(&mut value, 0, 1) }; |
63 |
| - println!("{:?}", res); |
64 |
| - let res = unsafe { std::intrinsics::atomic_cxchg(&mut value, 0, 1) }; |
65 |
| - println!("{:?}", res); |
66 |
| - |
67 |
| - use std::sync::atomic::{AtomicBool, Ordering}; |
68 |
| - |
69 |
| - let a = AtomicBool::new(false); |
70 |
| - assert_eq!(a.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst), Ok(false)); |
71 |
| - assert_eq!(a.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst), Err(true)); |
72 |
| - |
73 |
| - a.store(false, Ordering::SeqCst); |
74 |
| - assert_eq!(a.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst), Ok(false)); |
75 |
| - |
76 | 46 | // FIXME: the code seems to be the same when using an integer, but somehow, it doesn't work for
|
77 | 47 | // a float. Could it be related to the fact that floating-points use different registers?
|
78 | 48 |
|
@@ -160,4 +130,71 @@ fn main() {
|
160 | 130 | t_min as u64
|
161 | 131 | );*/
|
162 | 132 | */
|
| 133 | + |
| 134 | + assert_eq!(NonZeroU8::new(1).unwrap().leading_zeros(), 7); |
| 135 | + assert_eq!(NonZeroI8::new(1).unwrap().leading_zeros(), 7); |
| 136 | + assert_eq!(NonZeroU16::new(1).unwrap().leading_zeros(), 15); |
| 137 | + assert_eq!(NonZeroI16::new(1).unwrap().leading_zeros(), 15); |
| 138 | + assert_eq!(NonZeroU32::new(1).unwrap().leading_zeros(), 31); |
| 139 | + assert_eq!(NonZeroI32::new(1).unwrap().leading_zeros(), 31); |
| 140 | + assert_eq!(NonZeroU64::new(1).unwrap().leading_zeros(), 63); |
| 141 | + assert_eq!(NonZeroI64::new(1).unwrap().leading_zeros(), 63); |
| 142 | + assert_eq!(NonZeroU128::new(1).unwrap().leading_zeros(), 127); |
| 143 | + assert_eq!(NonZeroI128::new(1).unwrap().leading_zeros(), 127); |
| 144 | + assert_eq!(NonZeroUsize::new(1).unwrap().leading_zeros(), usize::BITS - 1); |
| 145 | + assert_eq!(NonZeroIsize::new(1).unwrap().leading_zeros(), usize::BITS - 1); |
| 146 | + |
| 147 | + assert_eq!(NonZeroU8::new(u8::MAX >> 2).unwrap().leading_zeros(), 2); |
| 148 | + assert_eq!(NonZeroI8::new((u8::MAX >> 2) as i8).unwrap().leading_zeros(), 2); |
| 149 | + assert_eq!(NonZeroU16::new(u16::MAX >> 2).unwrap().leading_zeros(), 2); |
| 150 | + assert_eq!(NonZeroI16::new((u16::MAX >> 2) as i16).unwrap().leading_zeros(), 2); |
| 151 | + assert_eq!(NonZeroU32::new(u32::MAX >> 2).unwrap().leading_zeros(), 2); |
| 152 | + assert_eq!(NonZeroI32::new((u32::MAX >> 2) as i32).unwrap().leading_zeros(), 2); |
| 153 | + assert_eq!(NonZeroU64::new(u64::MAX >> 2).unwrap().leading_zeros(), 2); |
| 154 | + assert_eq!(NonZeroI64::new((u64::MAX >> 2) as i64).unwrap().leading_zeros(), 2); |
| 155 | + |
| 156 | + /* |
| 157 | + //let mut num = u128::MAX >> 20; |
| 158 | + //let mut num = u128::MAX; |
| 159 | + #[inline(never)] |
| 160 | + fn two() -> u128 { |
| 161 | + 2 |
| 162 | + } |
| 163 | +
|
| 164 | + //let mut num = 340282366920938463463374607431768211455_u128 >> two(); |
| 165 | + let mut num = 340282366920938463463374607431768211455_u128 >> 2; |
| 166 | + //let mut num = 340282366920938463463374607431768211455_u128; |
| 167 | + //let mut num = 10_u128 >> 2; |
| 168 | + const MASK: u128 = 0x80000000000000000000000000000000; |
| 169 | + for _ in 0..128 { |
| 170 | + if num & MASK == MASK { |
| 171 | + print!("1"); |
| 172 | + } |
| 173 | + else { |
| 174 | + print!("0"); |
| 175 | + } |
| 176 | + num <<= 1; |
| 177 | + } |
| 178 | + println!(); |
| 179 | +*/ |
| 180 | + assert_eq!(NonZeroU128::new(u128::MAX >> 2).unwrap().leading_zeros(), 2); |
| 181 | + assert_eq!(NonZeroI128::new((u128::MAX >> 2) as i128).unwrap().leading_zeros(), 2); |
| 182 | + assert_eq!(NonZeroUsize::new(usize::MAX >> 2).unwrap().leading_zeros(), 2); |
| 183 | + assert_eq!(NonZeroIsize::new((usize::MAX >> 2) as isize).unwrap().leading_zeros(), 2); |
| 184 | + |
| 185 | + assert_eq!(NonZeroU8::new(u8::MAX).unwrap().leading_zeros(), 0); |
| 186 | + assert_eq!(NonZeroI8::new(-1i8).unwrap().leading_zeros(), 0); |
| 187 | + assert_eq!(NonZeroU16::new(u16::MAX).unwrap().leading_zeros(), 0); |
| 188 | + assert_eq!(NonZeroI16::new(-1i16).unwrap().leading_zeros(), 0); |
| 189 | + assert_eq!(NonZeroU32::new(u32::MAX).unwrap().leading_zeros(), 0); |
| 190 | + assert_eq!(NonZeroI32::new(-1i32).unwrap().leading_zeros(), 0); |
| 191 | + assert_eq!(NonZeroU64::new(u64::MAX).unwrap().leading_zeros(), 0); |
| 192 | + assert_eq!(NonZeroI64::new(-1i64).unwrap().leading_zeros(), 0); |
| 193 | + assert_eq!(NonZeroU128::new(u128::MAX).unwrap().leading_zeros(), 0); |
| 194 | + assert_eq!(NonZeroI128::new(-1i128).unwrap().leading_zeros(), 0); |
| 195 | + assert_eq!(NonZeroUsize::new(usize::MAX).unwrap().leading_zeros(), 0); |
| 196 | + assert_eq!(NonZeroIsize::new(-1isize).unwrap().leading_zeros(), 0); |
| 197 | + |
| 198 | + const LEADING_ZEROS: u32 = NonZeroU16::new(1).unwrap().leading_zeros(); |
| 199 | + assert_eq!(LEADING_ZEROS, 15); |
163 | 200 | }
|
0 commit comments