| 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 = |
| 7 |
mem::size_of_val(&crate::POW10_SIGNIFICANDS) + mem::size_of_val(&crate::DIGITS2); |
| 8 |
assert!(static_data == 10120); // 9.9K |
| 9 |
}; |
| 10 |
|
| 11 |
#[cfg(target_endian = "little")] |
| 12 |
#[test] |
| 13 |
fn utilities() { |
| 14 |
let clz = u64::leading_zeros; |
| 15 |
assert_eq!(clz(1), 63); |
| 16 |
assert_eq!(clz(!0), 0); |
| 17 |
|
| 18 |
assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000000), 0); |
| 19 |
assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000001), 1); |
| 20 |
assert_eq!(crate::count_trailing_nonzeros(0x00000000_00000009), 1); |
| 21 |
assert_eq!(crate::count_trailing_nonzeros(0x00090000_09000000), 7); |
| 22 |
assert_eq!(crate::count_trailing_nonzeros(0x01000000_00000000), 8); |
| 23 |
assert_eq!(crate::count_trailing_nonzeros(0x09000000_00000000), 8); |
| 24 |
} |
| 25 |
|
| 26 |
#[test] |
| 27 |
fn umulhi_inexact_to_odd() { |
| 28 |
let pow10 = crate::POW10_SIGNIFICANDS.get(-292); |
| 29 |
assert_eq!( |
| 30 |
crate::umulhi_inexact_to_odd(pow10.hi, pow10.lo, 0x1234567890abcdefu64 << 1), |
| 31 |
0x24554a3ce60a45f5, |
| 32 |
); |
| 33 |
assert_eq!( |
| 34 |
crate::umulhi_inexact_to_odd(pow10.hi, pow10.lo, 0x1234567890abce16u64 << 1), |
| 35 |
0x24554a3ce60a4643, |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
#[test] |
| 40 |
fn pow10() { |
| 41 |
const DEC_EXP_MIN: i32 = -292; |
| 42 |
|
| 43 |
// Range of decimal exponents [K_min, K_max] from the paper. |
| 44 |
let dec_exp_min = -324_i32; |
| 45 |
let dec_exp_max = 292_i32; |
| 46 |
|
| 47 |
let num_bits = 128_i32; |
| 48 |
|
| 49 |
// Negate dec_pow_min and dec_pow_max because we need negative powers 10^-k. |
| 50 |
for (i, dec_exp) in (-dec_exp_max..=-dec_exp_min).enumerate() { |
| 51 |
// dec_exp is -k in the paper. |
| 52 |
let bin_exp = (f64::from(dec_exp) * LOG2_10).floor() as i32 - (num_bits - 1); |
| 53 |
let bin_pow = Uint::from(2_u8).pow(bin_exp.unsigned_abs()); |
| 54 |
let dec_pow = Uint::from(10_u8).pow(dec_exp.unsigned_abs()); |
| 55 |
let result = if dec_exp < 0 { |
| 56 |
bin_pow / dec_pow |
| 57 |
} else if bin_exp < 0 { |
| 58 |
dec_pow * bin_pow |
| 59 |
} else { |
| 60 |
dec_pow / bin_pow |
| 61 |
}; |
| 62 |
let hi = u64::try_from(&result >> 64).unwrap(); |
| 63 |
let lo = u64::try_from(result & (Uint::from(2_u8).pow(64) - Uint::from(1_u8))).unwrap(); |
| 64 |
assert_eq!( |
| 65 |
crate::POW10_SIGNIFICANDS.get(DEC_EXP_MIN + i as i32), |
| 66 |
crate::uint128 { hi, lo }, |
| 67 |
); |
| 68 |
} |
| 69 |
} |