|
| 1 | +//! Test the u256 implementation. the ops already get exercised reasonably well through the `f128` |
| 2 | +//! routines, so this only does a few million fuzz iterations against GMP. |
| 3 | +
|
| 4 | +#![cfg(feature = "build-mpfr")] |
| 5 | + |
| 6 | +use std::sync::LazyLock; |
| 7 | + |
| 8 | +use libm::support::{HInt, u256}; |
| 9 | +type BigInt = rug::Integer; |
| 10 | + |
| 11 | +use libm_test::bigint_fuzz_iteration_count; |
| 12 | +use libm_test::gen::random::SEED; |
| 13 | +use rand::{Rng, SeedableRng}; |
| 14 | +use rand_chacha::ChaCha8Rng; |
| 15 | +use rug::Assign; |
| 16 | +use rug::integer::Order; |
| 17 | +use rug::ops::NotAssign; |
| 18 | + |
| 19 | +static BIGINT_U256_MAX: LazyLock<BigInt> = |
| 20 | + LazyLock::new(|| BigInt::from_digits(&[u128::MAX, u128::MAX], Order::Lsf)); |
| 21 | + |
| 22 | +/// Copied from the test module. |
| 23 | +fn hexu(v: u256) -> String { |
| 24 | + format!("0x{:032x}{:032x}", v.hi, v.lo) |
| 25 | +} |
| 26 | + |
| 27 | +fn random_u256(rng: &mut ChaCha8Rng) -> u256 { |
| 28 | + let lo: u128 = rng.gen(); |
| 29 | + let hi: u128 = rng.gen(); |
| 30 | + u256 { lo, hi } |
| 31 | +} |
| 32 | + |
| 33 | +fn assign_bigint(bx: &mut BigInt, x: u256) { |
| 34 | + bx.assign_digits(&[x.lo, x.hi], Order::Lsf); |
| 35 | +} |
| 36 | + |
| 37 | +fn from_bigint(bx: &mut BigInt) -> u256 { |
| 38 | + // Truncate so the result fits into `[u128; 2]`. This makes all ops overflowing. |
| 39 | + *bx &= &*BIGINT_U256_MAX; |
| 40 | + let mut bres = [0u128, 0]; |
| 41 | + bx.write_digits(&mut bres, Order::Lsf); |
| 42 | + bx.assign(0); |
| 43 | + u256 { lo: bres[0], hi: bres[1] } |
| 44 | +} |
| 45 | + |
| 46 | +fn check_one( |
| 47 | + x: impl FnOnce() -> String, |
| 48 | + y: impl FnOnce() -> Option<String>, |
| 49 | + actual: u256, |
| 50 | + expected: &mut BigInt, |
| 51 | +) { |
| 52 | + let expected = from_bigint(expected); |
| 53 | + if actual != expected { |
| 54 | + let xmsg = x(); |
| 55 | + let ymsg = y().map(|y| format!("y: {y}\n")).unwrap_or_default(); |
| 56 | + panic!( |
| 57 | + "Results do not match\n\ |
| 58 | + input: {xmsg}\n\ |
| 59 | + {ymsg}\ |
| 60 | + actual: {}\n\ |
| 61 | + expected: {}\ |
| 62 | + ", |
| 63 | + hexu(actual), |
| 64 | + hexu(expected), |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +fn mp_u256_bitor() { |
| 71 | + let mut rng = ChaCha8Rng::from_seed(*SEED); |
| 72 | + let mut bx = BigInt::new(); |
| 73 | + let mut by = BigInt::new(); |
| 74 | + |
| 75 | + for _ in 0..bigint_fuzz_iteration_count() { |
| 76 | + let x = random_u256(&mut rng); |
| 77 | + let y = random_u256(&mut rng); |
| 78 | + assign_bigint(&mut bx, x); |
| 79 | + assign_bigint(&mut by, y); |
| 80 | + let actual = x | y; |
| 81 | + bx |= &by; |
| 82 | + check_one(|| hexu(x), || Some(hexu(y)), actual, &mut bx); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[test] |
| 87 | +fn mp_u256_not() { |
| 88 | + let mut rng = ChaCha8Rng::from_seed(*SEED); |
| 89 | + let mut bx = BigInt::new(); |
| 90 | + |
| 91 | + for _ in 0..bigint_fuzz_iteration_count() { |
| 92 | + let x = random_u256(&mut rng); |
| 93 | + assign_bigint(&mut bx, x); |
| 94 | + let actual = !x; |
| 95 | + bx.not_assign(); |
| 96 | + check_one(|| hexu(x), || None, actual, &mut bx); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +#[test] |
| 101 | +fn mp_u256_add() { |
| 102 | + let mut rng = ChaCha8Rng::from_seed(*SEED); |
| 103 | + let mut bx = BigInt::new(); |
| 104 | + let mut by = BigInt::new(); |
| 105 | + |
| 106 | + for _ in 0..bigint_fuzz_iteration_count() { |
| 107 | + let x = random_u256(&mut rng); |
| 108 | + let y = random_u256(&mut rng); |
| 109 | + assign_bigint(&mut bx, x); |
| 110 | + assign_bigint(&mut by, y); |
| 111 | + let actual = x + y; |
| 112 | + bx += &by; |
| 113 | + check_one(|| hexu(x), || Some(hexu(y)), actual, &mut bx); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#[test] |
| 118 | +fn mp_u256_shr() { |
| 119 | + let mut rng = ChaCha8Rng::from_seed(*SEED); |
| 120 | + let mut bx = BigInt::new(); |
| 121 | + |
| 122 | + for _ in 0..bigint_fuzz_iteration_count() { |
| 123 | + let x = random_u256(&mut rng); |
| 124 | + let shift: u32 = rng.gen_range(0..255); |
| 125 | + assign_bigint(&mut bx, x); |
| 126 | + let actual = x >> shift; |
| 127 | + bx >>= shift; |
| 128 | + check_one(|| hexu(x), || Some(shift.to_string()), actual, &mut bx); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[test] |
| 133 | +fn mp_u256_widen_mul() { |
| 134 | + let mut rng = ChaCha8Rng::from_seed(*SEED); |
| 135 | + let mut bx = BigInt::new(); |
| 136 | + let mut by = BigInt::new(); |
| 137 | + |
| 138 | + for _ in 0..bigint_fuzz_iteration_count() { |
| 139 | + let x: u128 = rng.gen(); |
| 140 | + let y: u128 = rng.gen(); |
| 141 | + bx.assign(x); |
| 142 | + by.assign(y); |
| 143 | + let actual = x.widen_mul(y); |
| 144 | + bx *= &by; |
| 145 | + check_one(|| format!("{x:#034x}"), || Some(format!("{y:#034x}")), actual, &mut bx); |
| 146 | + } |
| 147 | +} |
0 commit comments