1extern crate core;
23const U32_MAX: u64 = core::u32::MAX as u64;
4const U64_MAX: u128 = core::u64::MAX as u128;
56use ::StrengthReducedU64;
7use ::long_multiplication;
89// divides a 128-bit number by a 64-bit divisor, returning the quotient as a 64-bit number
10// assumes that the divisor and numerator have both already been bit-shifted so that divisor.leading_zeros() == 0
11#[inline]
12fn divide_128_by_64_preshifted(numerator_hi: u64, numerator_lo: u64, divisor: u64) -> u64 {
13let numerator_mid = (numerator_lo >> 32) as u128;
14let numerator_lo = numerator_lo as u32 as u128;
15let divisor_full_128 = divisor as u128;
16let divisor_hi = divisor >> 32;
1718// To get the upper 32 bits of the quotient, we want to divide 'full_upper_numerator' by 'divisor'
19 // but the problem is, full_upper_numerator is a 96-bit number, meaning we would need to use u128 to do the division all at once, and the whole point of this is that we don't want to do 128 bit divison because it's slow
20 // so instead, we'll shift both the numerator and divisor right by 32, giving us a 64 bit / 32 bit division. This won't give us the exact quotient -- but it will be close.
21let full_upper_numerator = ((numerator_hi as u128) << 32) | numerator_mid;
22let mut quotient_hi = core::cmp::min(numerator_hi / divisor_hi, U32_MAX);
23let mut product_hi = quotient_hi as u128 * divisor_full_128;
2425// quotient_hi contains our guess at what the quotient is! the problem is that we got this by ignoring the lower 32 bits of the divisor. when we account for that, the quotient might be slightly lower
26 // we will know our quotient is too high if quotient * divisor > numerator. if it is, decrement until it's in range
27while product_hi > full_upper_numerator {
28 quotient_hi -= 1;
29 product_hi -= divisor_full_128;
30 }
31let remainder_hi = full_upper_numerator - product_hi;
323334// repeat the process using the lower half of the numerator
35let full_lower_numerator = (remainder_hi << 32) | numerator_lo;
36let mut quotient_lo = core::cmp::min((remainder_hi as u64) / divisor_hi, U32_MAX);
37let mut product_lo = quotient_lo as u128 * divisor_full_128;
3839// again, quotient_lo is just a guess at this point, it might be slightly too large
40while product_lo > full_lower_numerator {
41 quotient_lo -= 1;
42 product_lo -= divisor_full_128;
43 }
4445// We now have our separate quotients, now we just have to add them together
46(quotient_hi << 32) | quotient_lo
47}
4849// divides a 128-bit number by a 64-bit divisor, returning the quotient as a 64-bit number
50// assumes that the divisor and numerator have both already been bit-shifted to maximize the number of bits in divisor_hi
51// divisor_hi should be the upper 32 bits, and divisor_lo should be the lower 32 bits
52#[inline]
53fn divide_128_by_64_preshifted_reduced(numerator_hi: u64, numerator_lo: u64, divisor_hi: StrengthReducedU64, divisor_full: u64) -> u64 {
54let numerator_mid = (numerator_lo >> 32) as u128;
55let numerator_lo = numerator_lo as u32 as u128;
56let divisor_full_128 = divisor_full as u128;
5758// To get the upper 32 bits of the quotient, we want to divide 'full_upper_numerator' by 'divisor'
59 // but the problem is, full_upper_numerator is a 96-bit number, meaning we would need to use u128 to do the division all at once, and the whole point of this is that we don't want to do 128 bit divison because it's slow
60 // so instead, we'll shift both the numerator and divisor right by 32, giving us a 64 bit / 32 bit division. This won't give us the exact quotient -- but it will be close.
61let full_upper_numerator = ((numerator_hi as u128) << 32) | numerator_mid;
62let mut quotient_hi = core::cmp::min(numerator_hi / divisor_hi, U32_MAX);
63let mut product_hi = quotient_hi as u128 * divisor_full_128;
6465// quotient_hi contains our guess at what the quotient is! the problem is that we got this by ignoring the lower 32 bits of the divisor. when we account for that, the quotient might be slightly lower
66 // we will know our quotient is too high if quotient * divisor > numerator. if it is, decrement until it's in range
67while product_hi > full_upper_numerator {
68 quotient_hi -= 1;
69 product_hi -= divisor_full_128;
70 }
71let full_upper_remainder = full_upper_numerator - product_hi;
727374// repeat the process using the lower half of the numerator
75let full_lower_numerator = (full_upper_remainder << 32) | numerator_lo;
76let mut quotient_lo = core::cmp::min((full_upper_remainder as u64) / divisor_hi, U32_MAX);
77let mut product_lo = quotient_lo as u128 * divisor_full_128;
7879// again, quotient_lo is just a guess at this point, it might be slightly too large
80while product_lo > full_lower_numerator {
81 quotient_lo -= 1;
82 product_lo -= divisor_full_128;
83 }
8485// We now have our separate quotients, now we just have to add them together
86(quotient_hi << 32) | quotient_lo
87}
8889// divides a 128-bit number by a 128-bit divisor
90pub fn divide_128(numerator: u128, divisor: u128) -> u128 {
91if divisor <= U64_MAX {
92let divisor64 = divisor as u64;
93let upper_numerator = (numerator >> 64) as u64;
94if divisor64 > upper_numerator {
95 divide_128_by_64_helper(numerator, divisor64) as u128
96 }
97else {
98let upper_quotient = upper_numerator / divisor64;
99let upper_remainder = upper_numerator - upper_quotient * divisor64;
100101let intermediate_numerator = ((upper_remainder as u128) << 64) | (numerator as u64 as u128);
102let lower_quotient = divide_128_by_64_helper(intermediate_numerator, divisor64);
103104 ((upper_quotient as u128) << 64) | (lower_quotient as u128)
105 }
106 }
107else {
108let shift_size = divisor.leading_zeros();
109let shifted_divisor = divisor << shift_size;
110111let shifted_numerator = numerator >> 1;
112113let upper_quotient = divide_128_by_64_helper(shifted_numerator, (shifted_divisor >> 64) as u64);
114let mut quotient = upper_quotient >> (63 - shift_size);
115if quotient > 0 {
116 quotient -= 1;
117 }
118119let remainder = numerator - quotient as u128 * divisor;
120if remainder >= divisor {
121 quotient += 1;
122 }
123 quotient as u128
124 }
125}
126127// divides a 128-bit number by a 64-bit divisor, returning the quotient as a 64-bit number. Panics if the quotient doesn't fit in a 64-bit number
128fn divide_128_by_64_helper(numerator: u128, divisor: u64) -> u64 {
129// Assert that the upper half of the numerator is less than the denominator. This will guarantee that the quotient fits inside the numerator.
130 // Sadly this will give us some false negatives! TODO: Find a quick test we can do that doesn't have false negatives
131 // false negative example: numerator = u64::MAX * u64::MAX / u64::MAX
132assert!(divisor > (numerator >> 64) as u64, "The numerator is too large for the denominator; the quotient might not fit inside a u64.");
133134if divisor <= U32_MAX {
135return divide_128_by_32_helper(numerator, divisor as u32);
136 }
137138let shift_size = divisor.leading_zeros();
139let shifted_divisor = divisor << shift_size;
140let shifted_numerator = numerator << shift_size;
141let divisor_hi = shifted_divisor >> 32;
142let divisor_lo = shifted_divisor as u32 as u64;
143144// split the numerator into 3 chunks: the top 64-bits, the next 32-bits, and the lowest 32-bits
145let numerator_hi : u64 = (shifted_numerator >> 64) as u64;
146let numerator_mid : u64 = (shifted_numerator >> 32) as u32 as u64;
147let numerator_lo : u64 = shifted_numerator as u32 as u64;
148149// we're essentially going to do a long division algorithm with 2 divisions, one on numerator_hi << 32 | numerator_mid, and the second on the remainder of the first | numerator_lo
150 // but numerator_hi << 32 | numerator_mid is a 96-bit number, and we only have 64 bits to work with. so instead we split the divisor into 2 chunks, and divde by the upper chunk, and then check against the lower chunk in a while loop
151152 // step 1: divide the top chunk of the numerator by the divisor
153 // IDEALLY, we would divide (numerator_hi << 32) | numerator_mid by shifted_divisor, but that would require a 128-bit numerator, which is the whole thing we're trying to avoid
154 // so instead we're going to split the second division into two sub-phases. in 1a, we divide numerator_hi by divisor_hi, and then in 1b we decrement the quotient to account for the fact that it'll be smaller when you take divisor_lo into account
155156 // keep in mind that for all of step 2, the full numerator we're using will be
157 // complete_first_numerator = (numerator_midbits << 32) | numerator_mid
158159 // step 1a: divide the upper part of the middle numerator by the upper part of the divisor
160let mut quotient_hi = core::cmp::min(numerator_hi / divisor_hi, U32_MAX);
161let mut partial_remainder_hi = numerator_hi - quotient_hi * divisor_hi;
162163// step 1b: we know sort of what the quotient is, but it's slightly too large because it doesn't account for divisor_lo, nor numerator_mid, so decrement the quotient until it fits
164 // note that if we do some algebra on the condition in this while loop,
165 // ie "quotient_hi * divisor_lo > (partial_remainder_hi << 32) | numerator_mid"
166 // we end up getting "quotient_hi * shifted_divisor < (numerator_midbits << 32) | numerator_mid". remember that the right side of the inequality sign is complete_first_numerator from above.
167 // which deminstrates that we're decrementing the quotient until the quotient multipled by the complete divisor is less than the complete numerator
168while partial_remainder_hi <= U32_MAX && quotient_hi * divisor_lo > (partial_remainder_hi << 32) | numerator_mid {
169 quotient_hi -= 1;
170 partial_remainder_hi += divisor_hi;
171 }
172173// step 2: Divide the bottom part of the numerator. We're going to have the same problem as step 1, where we want the numerator to be a 96-bit number, so again we're going to split it into 2 substeps
174 // the full numeratoe for step 3 will be:
175 // complete_second_numerator = (first_division_remainder << 32) | numerator_lo
176177 // step 2a: divide the upper part of the lower numerator by the upper part of the divisor
178 // To get the numerator, complate the calculation of the full remainder by subtracing the quotient times the lower bits of the divisor
179 // TODO: a warpping subtract is necessary here. why does this work, and why is it necessary?
180let full_remainder_hi = ((partial_remainder_hi << 32) | numerator_mid).wrapping_sub(quotient_hi * divisor_lo);
181182let mut quotient_lo = core::cmp::min(full_remainder_hi / divisor_hi, U32_MAX);
183let mut partial_remainder_lo = full_remainder_hi - quotient_lo * divisor_hi;
184185// step 2b: just like step 1b, decrement the final quotient until it's correctr when accounting for the full divisor
186while partial_remainder_lo <= U32_MAX && quotient_lo * divisor_lo > (partial_remainder_lo << 32) | numerator_lo {
187 quotient_lo -= 1;
188 partial_remainder_lo += divisor_hi;
189 }
190191// We now have our separate quotients, now we just have to add them together
192(quotient_hi << 32) | quotient_lo
193}
194195196// Same as divide_128_by_64_into_64, but optimized for scenarios where the divisor fits in a u32. Still panics if the quotient doesn't fit in a u64
197fn divide_128_by_32_helper(numerator: u128, divisor: u32) -> u64 {
198// Assert that the upper half of the numerator is less than the denominator. This will guarantee that the quotient fits inside the numerator.
199 // Sadly this will give us some false negatives! TODO: Find a quick test we can do that doesn't have false negatives
200 // false negative example: numerator = u64::MAX * u64::MAX / u64::MAX
201assert!(divisor as u64 > (numerator >> 64) as u64, "The numerator is too large for the denominator; the quotient might not fit inside a u64.");
202203let shift_size = divisor.leading_zeros();
204let shifted_divisor = (divisor << shift_size) as u64;
205let shifted_numerator = numerator << (shift_size + 32);
206207// split the numerator into 3 chunks: the top 64-bits, the next 32-bits, and the lowest 32-bits
208let numerator_hi : u64 = (shifted_numerator >> 64) as u64;
209let numerator_mid : u64 = (shifted_numerator >> 32) as u32 as u64;
210211// we're essentially going to do a long division algorithm with 2 divisions, one on numerator_hi << 32 | numerator_mid, and the second on the remainder of the first | numerator_lo
212 // but numerator_hi << 32 | numerator_mid is a 96-bit number, and we only have 64 bits to work with. so instead we split the divisor into 2 chunks, and divde by the upper chunk, and then check against the lower chunk in a while loop
213214 // step 1: divide the top chunk of the numerator by the divisor
215 // IDEALLY, we would divide (numerator_hi << 32) | numerator_mid by shifted_divisor, but that would require a 128-bit numerator, which is the whole thing we're trying to avoid
216 // so instead we're going to split the second division into two sub-phases. in 1a, we divide numerator_hi by divisor_hi, and then in 1b we decrement the quotient to account for the fact that it'll be smaller when you take divisor_lo into account
217218 // keep in mind that for all of step 1, the full numerator we're using will be
219 // complete_first_numerator = (numerator_hi << 32) | numerator_mid
220221 // step 1a: divide the upper part of the middle numerator by the upper part of the divisor
222let quotient_hi = numerator_hi / shifted_divisor;
223let remainder_hi = numerator_hi - quotient_hi * shifted_divisor;
224225// step 2: Divide the bottom part of the numerator. We're going to have the same problem as step 1, where we want the numerator to be a 96-bit number, so again we're going to split it into 2 substeps
226 // the full numeratoe for step 3 will be:
227 // complete_second_numerator = (first_division_remainder << 32) | numerator_lo
228229 // step 2a: divide the upper part of the lower numerator by the upper part of the divisor
230 // To get the numerator, complate the calculation of the full remainder by subtracing the quotient times the lower bits of the divisor
231 // TODO: a warpping subtract is necessary here. why does this work, and why is it necessary?
232let final_numerator = (remainder_hi) << 32 | numerator_mid;
233let quotient_lo = final_numerator / shifted_divisor;
234235// We now have our separate quotients, now we just have to add them together
236(quotient_hi << 32) | quotient_lo
237}
238239#[inline(never)]
240fn long_division(numerator_slice: &[u64], reduced_divisor: &StrengthReducedU64, quotient: &mut [u64]) {
241let mut remainder = 0;
242for (numerator_element, quotient_element) in numerator_slice.iter().zip(quotient.iter_mut()).rev() {
243if remainder > 0 {
244// Do one division that includes the running remainder and the upper half of this numerator element,
245 // then a second division for the first division's remainder combinedwith the lower half
246let upper_numerator = (remainder << 32) | (*numerator_element >> 32);
247let (upper_quotient, upper_remainder) = StrengthReducedU64::div_rem(upper_numerator, *reduced_divisor);
248249let lower_numerator = (upper_remainder << 32) | (*numerator_element as u32 as u64);
250let (lower_quotient, lower_remainder) = StrengthReducedU64::div_rem(lower_numerator, *reduced_divisor);
251252*quotient_element = (upper_quotient << 32) | lower_quotient;
253 remainder = lower_remainder;
254 } else {
255// The remainder is zero, which means we can take a shortcut and only do a single division!
256let (digit_quotient, digit_remainder) = StrengthReducedU64::div_rem(*numerator_element, *reduced_divisor);
257258*quotient_element = digit_quotient;
259 remainder = digit_remainder;
260 }
261 }
262}
263264#[inline]
265fn normalize_slice(input: &mut [u64]) -> &mut [u64] {
266let input_len = input.len();
267let trailing_zero_chunks = input.iter().rev().take_while(|e| **e == 0).count();
268&mut input[..input_len - trailing_zero_chunks]
269}
270271#[inline]
272fn is_slice_greater(a: &[u64], b: &[u64]) -> bool {
273if a.len() > b.len() {
274return true;
275 }
276if b.len() > a.len() {
277return false;
278 }
279280for (&ai, &bi) in a.iter().zip(b.iter()).rev() {
281if ai < bi {
282return false;
283 }
284if ai > bi {
285return true;
286 }
287 }
288false
289}
290// subtract b from a, and store the result in a
291#[inline]
292fn sub_assign(a: &mut [u64], b: &[u64]) {
293let mut borrow: i128 = 0;
294295// subtract b from a, keeping track of borrows as we go
296let (a_lo, a_hi) = a.split_at_mut(b.len());
297298for (a, b) in a_lo.iter_mut().zip(b) {
299 borrow += *a as i128;
300 borrow -= *b as i128;
301*a = borrow as u64;
302 borrow >>= 64;
303 }
304305// We're done subtracting, we just need to finish carrying
306let mut a_element = a_hi.iter_mut();
307while borrow != 0 {
308let a_element = a_element.next().expect("borrow underflow during sub_assign");
309 borrow += *a_element as i128;
310311*a_element = borrow as u64;
312 borrow >>= 64;
313 }
314}
315316pub(crate) fn divide_128_max_by_64(divisor: u64) -> u128 {
317let quotient_hi = core::u64::MAX / divisor;
318let remainder_hi = core::u64::MAX - quotient_hi * divisor;
319320let leading_zeros = divisor.leading_zeros();
321let quotient_lo = if leading_zeros >= 32 {
322let numerator_mid = (remainder_hi << 32) | core::u32::MAX as u64;
323let quotient_mid = numerator_mid / divisor;
324let remainder_mid = numerator_mid - quotient_mid * divisor;
325326let numerator_lo = (remainder_mid << 32) | core::u32::MAX as u64;
327let quotient_lo = numerator_lo / divisor;
328329 (quotient_mid << 32) | quotient_lo
330 }
331else {
332let numerator_hi = if leading_zeros > 0 { (remainder_hi << leading_zeros) | (core::u64::MAX >> (64 - leading_zeros)) } else { remainder_hi };
333let numerator_lo = core::u64::MAX << leading_zeros;
334335 divide_128_by_64_preshifted(numerator_hi, numerator_lo, divisor << leading_zeros)
336 };
337 ((quotient_hi as u128) << 64) | (quotient_lo as u128)
338}
339340fn divide_256_max_by_32(divisor: u32) -> (u128, u128) {
341let reduced_divisor = StrengthReducedU64::new(divisor as u64);
342let mut numerator_chunks = [core::u64::MAX; 4];
343let mut quotient_chunks = [0; 4];
344 long_division(&mut numerator_chunks, &reduced_divisor, &mut quotient_chunks);
345346// quotient_chunks now contains the quotient! all we have to do is recombine it into u128s
347let quotient_lo = (quotient_chunks[0] as u128) | ((quotient_chunks[1] as u128) << 64);
348let quotient_hi = (quotient_chunks[2] as u128) | ((quotient_chunks[3] as u128) << 64);
349350 (quotient_hi, quotient_lo)
351}
352353pub(crate) fn divide_256_max_by_128(divisor: u128) -> (u128, u128) {
354let leading_zeros = divisor.leading_zeros();
355356// if the divisor fits inside a u32, we can use a much faster algorithm
357if leading_zeros >= 96 {
358return divide_256_max_by_32(divisor as u32);
359 }
360361let empty_divisor_chunks = (leading_zeros / 64) as usize;
362let shift_amount = leading_zeros % 64;
363364// Shift the divisor and chunk it up into U32s
365let divisor_shifted = divisor << shift_amount;
366let divisor_chunks = [
367 divisor_shifted as u64,
368 (divisor_shifted >> 64) as u64,
369 ];
370let divisor_slice = &divisor_chunks[..(divisor_chunks.len() - empty_divisor_chunks)];
371372// We're gonna be doing a ton of u64/u64 divisions, so we're gonna eat our own dog food and set up a strength-reduced division instance
373 // the only actual **divisions* we'll be doing will be with the largest 32 bits of the full divisor, not the full divisor
374let reduced_divisor_hi = StrengthReducedU64::new(*divisor_slice.last().unwrap() >> 32);
375let divisor_hi = *divisor_slice.last().unwrap();
376377// Build our numerator, represented by u32 chunks. at first it will be full of u32::MAX, but we will iteratively take chunks out of it as we divide
378let mut numerator_chunks = [core::u64::MAX; 5];
379let mut numerator_max_idx = if shift_amount > 0 {
380 numerator_chunks[4] >>= 64 - shift_amount;
381 numerator_chunks[0] <<= shift_amount;
3825
383}
384else {
3854
386};
387388// allocate the biggest-possible quotient, even if it might be smaller -- we just won't fill out the biggest parts
389let num_quotient_chunks = 3 + empty_divisor_chunks;
390let mut quotient_chunks = [0; 4];
391for quotient_idx in (0..num_quotient_chunks).rev() {
392/*
393 * When calculating our next guess q0, we don't need to consider the digits below j
394 * + b.data.len() - 1: we're guessing digit j of the quotient (i.e. q0 << j) from
395 * digit bn of the divisor (i.e. bn << (b.data.len() - 1) - so the product of those
396 * two numbers will be zero in all digits up to (j + b.data.len() - 1).
397 */
398399let numerator_slice = &mut numerator_chunks[..numerator_max_idx];
400let numerator_start_idx = quotient_idx + divisor_slice.len() - 1;
401if numerator_start_idx >= numerator_slice.len() {
402continue;
403 }
404405406// scope for borrow checker
407{
408// divide the uppermost bits of the remaining numerator to get "sub_quotient" which will be our guess for this quotient element
409let numerator_hi = if numerator_slice.len() - numerator_start_idx > 1 { numerator_slice[numerator_start_idx + 1] } else { 0 };
410let numerator_lo = numerator_slice[numerator_start_idx];
411let mut sub_quotient = divide_128_by_64_preshifted_reduced(numerator_hi, numerator_lo, reduced_divisor_hi, divisor_hi);
412413let mut tmp_product = [0; 3];
414 long_multiplication::long_multiply(&divisor_slice, sub_quotient, &mut tmp_product);
415let sub_product = normalize_slice(&mut tmp_product);
416417// our sub_quotient is just a guess at the quotient -- it only accounts for the topmost bits of the divisor. when we take the bottom bits of the divisor into account, the actual quotient will be smaller
418 // we will know if our guess is too large if (quotient_guess * full_divisor) (aka sub_product) is greater than this iteration's numerator slice. ifthat's the case, decrement it until it's less than or equal.
419while is_slice_greater(sub_product, &numerator_slice[quotient_idx..]) {
420 sub_assign(sub_product, &divisor_slice);
421 sub_quotient -= 1;
422 }
423424// sub_quotient is now the correct sub-quotient for this iteration. add it to the full quotient, and subtract the product from the full numerator, so that what remains in the numerator is the remainder of this division
425quotient_chunks[quotient_idx] = sub_quotient;
426 sub_assign(&mut numerator_slice[quotient_idx..], sub_product);
427 }
428429430// slice off any zeroes at the end of the numerator. we're not calling normalize_slice here because of borrow checker obnoxiousness
431numerator_max_idx -= numerator_slice.iter().rev().take_while(|e| **e == 0).count();
432 }
433434435// quotient_chunks now contains the quotient! all we have to do is recombine it into u128s
436let quotient_lo = (quotient_chunks[0] as u128)
437 | ((quotient_chunks[1] as u128) << 64);
438let quotient_hi = (quotient_chunks[2] as u128)
439 | ((quotient_chunks[3] as u128) << 64);
440441 (quotient_hi, quotient_lo)
442}
443444445446#[cfg(test)]
447mod unit_tests {
448use num_bigint::BigUint;
449450#[test]
451fn test_divide_128_by_64() {
452for divisor in core::u64::MAX..=core::u64::MAX {
453let divisor_128 = core::u64::MAX as u128;
454455let numerator = divisor_128 * divisor_128 + (divisor_128 - 1);
456//for numerator in core::u128::MAX - 10..core::u128::MAX {
457let expected_quotient = numerator / divisor as u128;
458assert!(expected_quotient == core::u64::MAX as u128);
459460let actual_quotient = super::divide_128_by_64_helper(numerator as u128, divisor);
461462463464let expected_upper = (expected_quotient >> 32) as u64;
465let expected_lower = expected_quotient as u32 as u64;
466let actual_upper = (actual_quotient >> 32) as u64;
467let actual_lower = actual_quotient as u32 as u64;
468469assert_eq!(expected_upper, actual_upper, "wrong quotient for {}/{}", numerator, divisor);
470assert_eq!(expected_lower, actual_lower, "wrong quotient for {}/{}", numerator, divisor);
471//}
472}
473 }
474475fn test_divisor_128(divisor: u128) {
476let big_numerator = BigUint::from_slice(&[core::u32::MAX; 8]);
477let big_quotient = big_numerator / divisor;
478479//let (actual_hi, actual_lo) = super::divide_256_max_by_128_direct(divisor);
480let (actual64_hi, actual64_lo) = super::divide_256_max_by_128(divisor);
481482//let actual_big = (BigUint::from(actual_hi) << 128) | BigUint::from(actual_lo);
483let actual64_big = (BigUint::from(actual64_hi) << 128) | BigUint::from(actual64_lo);
484485//assert_eq!(big_quotient, actual_big, "Actual quotient didn't match expected quotient for max/{}", divisor);
486assert_eq!(big_quotient, actual64_big, "Actual64 quotient didn't match expected quotient for max/{}", divisor);
487 }
488489#[allow(unused_imports)]
490use rand::{rngs::StdRng, SeedableRng, distributions::Distribution, distributions::Uniform};
491492#[test]
493fn test_max_256() {
494let log2_tests_per_bit = 6;
495496for divisor in 1..(1 << log2_tests_per_bit) {
497 test_divisor_128(divisor);
498 }
499500let mut gen = StdRng::seed_from_u64(5673573);
501for bits in log2_tests_per_bit..128 {
502let lower_start = 1 << bits;
503let lower_stop = lower_start + (1 << (log2_tests_per_bit - 3));
504let upper_stop = 1u128.checked_shl(bits + 1).map_or(core::u128::MAX, |v| v - 1);
505let upper_start = upper_stop - (1 << (log2_tests_per_bit - 3)) + 1;
506507for divisor in lower_start..lower_stop {
508 test_divisor_128(divisor);
509 }
510for divisor in upper_start..=upper_stop {
511 test_divisor_128(divisor);
512 }
513514let random_count = 1 << log2_tests_per_bit;
515let dist = Uniform::new(lower_stop + 1, upper_start);
516for _ in 0..random_count {
517let divisor = dist.sample(&mut gen);
518 test_divisor_128(divisor);
519 }
520 }
521 }
522}