Skip to content

Commit fa63dbf

Browse files
authored
Rollup merge of #146284 - Kivooeo:blazing-fast-division-bignum, r=Mark-Simulacrum
Remove `div_rem` from `core::num::bignum` This fixes very old fixme that sounds like this ``` Stupid slow base-2 long division taken from https://en.wikipedia.org/wiki/Division_algorithm FIXME use a greater base ($ty) for the long division. ``` By deleting this method since it was never used
2 parents f96bc5c + a2d66db commit fa63dbf

2 files changed

Lines changed: 0 additions & 56 deletions

File tree

‎library/core/src/num/bignum.rs‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -335,43 +335,6 @@ macro_rules! define_bignum {
335335
}
336336
(self, borrow)
337337
}
338-
339-
/// Divide self by another bignum, overwriting `q` with the quotient and `r` with the
340-
/// remainder.
341-
pubfn div_rem(&self, d:&$name, q:&mut $name, r:&mut $name){
342-
// Stupid slow base-2 long division taken from
343-
// https://en.wikipedia.org/wiki/Division_algorithm
344-
// FIXME use a greater base ($ty) for the long division.
345-
assert!(!d.is_zero());
346-
let digitbits = <$ty>::BITSasusize;
347-
for digit in &mut q.base[..]{
348-
*digit = 0;
349-
}
350-
for digit in &mut r.base[..]{
351-
*digit = 0;
352-
}
353-
r.size = d.size;
354-
q.size = 1;
355-
letmut q_is_zero = true;
356-
let end = self.bit_length();
357-
for i in (0..end).rev(){
358-
r.mul_pow2(1);
359-
r.base[0] |= self.get_bit(i)as $ty;
360-
if&*r >= d {
361-
r.sub(d);
362-
// Set bit `i` of q to 1.
363-
let digit_idx = i / digitbits;
364-
let bit_idx = i % digitbits;
365-
if q_is_zero {
366-
q.size = digit_idx + 1;
367-
q_is_zero = false;
368-
}
369-
q.base[digit_idx] |= 1 << bit_idx;
370-
}
371-
}
372-
debug_assert!(q.base[q.size..].iter().all(|&d| d == 0));
373-
debug_assert!(r.base[r.size..].iter().all(|&d| d == 0));
374-
}
375338
}
376339

377340
implcrate::cmp::PartialEqfor $name {

‎library/coretests/tests/num/bignum.rs‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,6 @@ fn test_div_rem_small() {
167167
);
168168
}
169169

170-
#[test]
171-
fntest_div_rem(){
172-
fndiv_rem(n:u64,d:u64) -> (Big,Big){
173-
letmut q = Big::from_small(42);
174-
letmut r = Big::from_small(42);
175-
Big::from_u64(n).div_rem(&Big::from_u64(d),&mut q,&mut r);
176-
(q, r)
177-
}
178-
assert_eq!(div_rem(1,1),(Big::from_small(1),Big::from_small(0)));
179-
assert_eq!(div_rem(4,3),(Big::from_small(1),Big::from_small(1)));
180-
assert_eq!(div_rem(1,7),(Big::from_small(0),Big::from_small(1)));
181-
assert_eq!(div_rem(45,9),(Big::from_small(5),Big::from_small(0)));
182-
assert_eq!(div_rem(103,9),(Big::from_small(11),Big::from_small(4)));
183-
assert_eq!(div_rem(123456,77),(Big::from_u64(1603),Big::from_small(25)));
184-
assert_eq!(div_rem(0xffff,1),(Big::from_u64(0xffff),Big::from_small(0)));
185-
assert_eq!(div_rem(0xeeee,0xffff),(Big::from_small(0),Big::from_u64(0xeeee)));
186-
assert_eq!(div_rem(2_000_000,2),(Big::from_u64(1_000_000),Big::from_u64(0)));
187-
}
188-
189170
#[test]
190171
fntest_is_zero(){
191172
assert!(Big::from_small(0).is_zero());

0 commit comments

Comments
 (0)