aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / zmij / src / tests.rs
| 1 | use core::f64::consts::LOG2_10; |
| 2 | use core::mem; |
| 3 | use num_bigint::BigUint as Uint; |
| 4 | |
| 5 | const _: () = { |
| 6 | let static_data = mem::size_of_val(&crate::STATIC_DATA.pow10_significands) |
| 7 | + mem::size_of_val(&crate::STATIC_DATA.exp_shifts) |
| 8 | + mem::size_of_val(&crate::STATIC_DATA.exp_strings) |
| 9 | + mem::size_of_val(&crate::DIGITS2); |
| 10 | if cfg!(opt_level = "s") { |
| 11 | assert!(static_data == 200); |
| 12 | } else { |
| 13 | assert!(static_data == 17232); // 16.8K |
| 14 | } |
| 15 | }; |
| 16 | |
| 17 | #[cfg(target_endian = "little")] |
| 18 | #[test] |
| 19 | fn utilities() { |
| 20 | let clz = u64::leading_zeros; |
| 21 | assert_eq!(clz(1), 63); |
| 22 | assert_eq!(clz(!0), 0); |
| 23 | |
| 24 | assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000000), 0); |
| 25 | assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000001), 1); |
| 26 | assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000009), 1); |
| 27 | assert_eq!(crate::count_trailing_nonzeros(0x00090000_09000000), 7); |
| 28 | assert_eq!(crate::count_trailing_nonzeros(0x01000000_00000000), 8); |
| 29 | assert_eq!(crate::count_trailing_nonzeros(0x09000000_00000000), 8); |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn pow10() { |
| 34 | const DEC_EXP_MIN: i32 = -292; |
| 35 | |
| 36 | // Range of decimal exponents [K_min, K_max] from the paper. |
| 37 | let dec_exp_min = -324_i32; |
| 38 | let dec_exp_max = 292_i32; |
| 39 | |
| 40 | let num_bits = 128_i32; |
| 41 | |
| 42 | // Negate dec_pow_min and dec_pow_max because we need negative powers 10^-k. |
| 43 | for (i, dec_exp) in (-dec_exp_max..=-dec_exp_min).enumerate() { |
| 44 | // dec_exp is -k in the paper. |
| 45 | let bin_exp = (f64::from(dec_exp) * LOG2_10).floor() as i32 - (num_bits - 1); |
| 46 | let bin_pow = Uint::from(2_u8).pow(bin_exp.unsigned_abs()); |
| 47 | let dec_pow = Uint::from(10_u8).pow(dec_exp.unsigned_abs()); |
| 48 | let result = if dec_exp < 0 { |
| 49 | bin_pow / dec_pow |
| 50 | } else if bin_exp < 0 { |
| 51 | dec_pow * bin_pow |
| 52 | } else { |
| 53 | dec_pow / bin_pow |
| 54 | }; |
| 55 | let hi = u64::try_from(&result >> 64).unwrap(); |
| 56 | let lo = u64::try_from(result & (Uint::from(2_u8).pow(64) - Uint::from(1_u8))).unwrap(); |
| 57 | if !crate::Pow10SignificandTable::COMPRESS { |
| 58 | assert_eq!( |
| 59 | crate::STATIC_DATA |
| 60 | .pow10_significands |
| 61 | .get(DEC_EXP_MIN + i as i32), |
| 62 | crate::uint128 { hi, lo }, |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | } |