Skip to content

Commit aa2205d

Browse files
committed
Fix bigint shr impl
1 parent c53ee3c commit aa2205d

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

src/int/big.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,13 @@ impl ops::Shr<u32> for u256 {
123123
let byte_shift = rhs / 64;
124124
let bit_shift = rhs % 64;
125125

126-
dbg!(self, rhs, byte_shift, bit_shift);
127-
128126
for idx in 0..4 {
129127
let base_idx = idx + byte_shift as usize;
130-
dbg!(base_idx);
131128

132129
let Some(base) = ret.0.get(base_idx) else {
130+
ret.0[idx] = 0;
133131
continue;
134132
};
135-
dbg!(base);
136133

137134
let mut new_val = base >> bit_shift;
138135

testcrate/tests/big.rs

+50-39
Original file line numberDiff line numberDiff line change
@@ -67,57 +67,68 @@ fn not_u128() {
6767

6868
#[test]
6969
fn shr_u128() {
70-
let lo_a = [1, 2, 3, 10, u16::MAX as u128, u32::MAX as u128, u128::MAX];
71-
let b = [0, 1, 2, 4, 10, 31, 32, 33, 64, 96, 127];
70+
let only_low = [
71+
1,
72+
u16::MAX.into(),
73+
u32::MAX.into(),
74+
u64::MAX.into(),
75+
u128::MAX,
76+
];
7277

7378
let mut errors = Vec::new();
7479

75-
for a in lo_a {
76-
for b in b {
77-
let res = a.widen() >> b;
78-
let expected = (a >> b).widen();
79-
if res != expected {
80-
errors.push((a, b, res, expected));
80+
for a in only_low {
81+
for perturb in 0..10 {
82+
let a = a.saturating_add(perturb);
83+
for shift in 0..128 {
84+
let res = a.widen() >> shift;
85+
let expected = (a >> shift).widen();
86+
if res != expected {
87+
errors.push((a.widen(), shift, res, expected));
88+
}
8189
}
8290
}
8391
}
8492

93+
let check = [
94+
(
95+
u256::MAX,
96+
1,
97+
u256([u64::MAX, u64::MAX, u64::MAX, u64::MAX >> 1]),
98+
),
99+
(
100+
u256::MAX,
101+
5,
102+
u256([u64::MAX, u64::MAX, u64::MAX, u64::MAX >> 5]),
103+
),
104+
(u256::MAX, 63, u256([u64::MAX, u64::MAX, u64::MAX, 1])),
105+
(u256::MAX, 64, u256([u64::MAX, u64::MAX, u64::MAX, 0])),
106+
(u256::MAX, 65, u256([u64::MAX, u64::MAX, u64::MAX >> 1, 0])),
107+
(u256::MAX, 127, u256([u64::MAX, u64::MAX, 1, 0])),
108+
(u256::MAX, 128, u256([u64::MAX, u64::MAX, 0, 0])),
109+
(u256::MAX, 129, u256([u64::MAX, u64::MAX >> 1, 0, 0])),
110+
(u256::MAX, 191, u256([u64::MAX, 1, 0, 0])),
111+
(u256::MAX, 192, u256([u64::MAX, 0, 0, 0])),
112+
(u256::MAX, 193, u256([u64::MAX >> 1, 0, 0, 0])),
113+
(u256::MAX, 191, u256([u64::MAX, 1, 0, 0])),
114+
(u256::MAX, 254, u256([0b11, 0, 0, 0])),
115+
(u256::MAX, 255, u256([1, 0, 0, 0])),
116+
];
117+
118+
for (input, shift, expected) in check {
119+
let res = input >> shift;
120+
if res != expected {
121+
errors.push((input, shift, res, expected));
122+
}
123+
}
124+
85125
for (a, b, res, expected) in &errors {
86126
eprintln!(
87-
"FAILURE: {a:#034x} >> {b} = {} got {}",
127+
"FAILURE: {} >> {b} = {} got {}",
128+
hexu(*a),
88129
hexu(*expected),
89130
hexu(*res),
90131
);
91132
}
92133
assert!(errors.is_empty());
93134
}
94-
95-
// #[test]
96-
// fn shr_u128() {
97-
// let tests = [
98-
// (u128::MAX / 2, 2_u128, u256([u64::MAX - 1, u64::MAX, 0, 0])),
99-
// (u128::MAX, 2_u128, u256([u64::MAX - 1, u64::MAX, 1, 0])),
100-
// (u128::MAX, u128::MAX, u256([1, 0, u64::MAX - 1, u64::MAX])),
101-
// (u128::MIN, u128::MIN, u256::ZERO),
102-
// (1234, 0, u256::ZERO),
103-
// (0, 1234, u256::ZERO),
104-
// ];
105-
106-
// let mut errors = Vec::new();
107-
// for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
108-
// let res = a.widen_mul(b);
109-
// assert_eq!(res, res_z);
110-
// if res != exp {
111-
// errors.push((i, a, b, exp, res));
112-
// }
113-
// }
114-
115-
// for (i, a, b, exp, res) in &errors {
116-
// eprintln!(
117-
// "FAILURE ({i}): {a:#034x} * {b:#034x} = {} got {}",
118-
// hexu(*exp),
119-
// hexu(*res)
120-
// );
121-
// }
122-
// assert!(errors.is_empty());
123-
// }

0 commit comments

Comments
 (0)