num_bigint/
algorithms.rs

1use std::borrow::Cow;
2use std::cmp;
3use std::cmp::Ordering::{self, Equal, Greater, Less};
4use std::iter::repeat;
5use std::mem;
6use traits;
7use traits::{One, Zero};
8
9use biguint::BigUint;
10
11use bigint::BigInt;
12use bigint::Sign;
13use bigint::Sign::{Minus, NoSign, Plus};
14
15use big_digit::{self, BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
16
17// Generic functions for add/subtract/multiply with carry/borrow:
18
19// Add with carry:
20#[inline]
21fn adc(a: BigDigit, b: BigDigit, acc: &mut DoubleBigDigit) -> BigDigit {
22    *acc += DoubleBigDigit::from(a);
23    *acc += DoubleBigDigit::from(b);
24    let lo = *acc as BigDigit;
25    *acc >>= big_digit::BITS;
26    lo
27}
28
29// Subtract with borrow:
30#[inline]
31fn sbb(a: BigDigit, b: BigDigit, acc: &mut SignedDoubleBigDigit) -> BigDigit {
32    *acc += SignedDoubleBigDigit::from(a);
33    *acc -= SignedDoubleBigDigit::from(b);
34    let lo = *acc as BigDigit;
35    *acc >>= big_digit::BITS;
36    lo
37}
38
39#[inline]
40pub fn mac_with_carry(a: BigDigit, b: BigDigit, c: BigDigit, acc: &mut DoubleBigDigit) -> BigDigit {
41    *acc += DoubleBigDigit::from(a);
42    *acc += DoubleBigDigit::from(b) * DoubleBigDigit::from(c);
43    let lo = *acc as BigDigit;
44    *acc >>= big_digit::BITS;
45    lo
46}
47
48#[inline]
49pub fn mul_with_carry(a: BigDigit, b: BigDigit, acc: &mut DoubleBigDigit) -> BigDigit {
50    *acc += DoubleBigDigit::from(a) * DoubleBigDigit::from(b);
51    let lo = *acc as BigDigit;
52    *acc >>= big_digit::BITS;
53    lo
54}
55
56/// Divide a two digit numerator by a one digit divisor, returns quotient and remainder:
57///
58/// Note: the caller must ensure that both the quotient and remainder will fit into a single digit.
59/// This is _not_ true for an arbitrary numerator/denominator.
60///
61/// (This function also matches what the x86 divide instruction does).
62#[inline]
63fn div_wide(hi: BigDigit, lo: BigDigit, divisor: BigDigit) -> (BigDigit, BigDigit) {
64    debug_assert!(hi < divisor);
65
66    let lhs = big_digit::to_doublebigdigit(hi, lo);
67    let rhs = DoubleBigDigit::from(divisor);
68    ((lhs / rhs) as BigDigit, (lhs % rhs) as BigDigit)
69}
70
71pub fn div_rem_digit(mut a: BigUint, b: BigDigit) -> (BigUint, BigDigit) {
72    let mut rem = 0;
73
74    for d in a.data.iter_mut().rev() {
75        let (q, r) = div_wide(rem, *d, b);
76        *d = q;
77        rem = r;
78    }
79
80    (a.normalized(), rem)
81}
82
83pub fn rem_digit(a: &BigUint, b: BigDigit) -> BigDigit {
84    let mut rem: DoubleBigDigit = 0;
85    for &digit in a.data.iter().rev() {
86        rem = (rem << big_digit::BITS) + DoubleBigDigit::from(digit);
87        rem %= DoubleBigDigit::from(b);
88    }
89
90    rem as BigDigit
91}
92
93// Only for the Add impl:
94#[inline]
95pub fn __add2(a: &mut [BigDigit], b: &[BigDigit]) -> BigDigit {
96    debug_assert!(a.len() >= b.len());
97
98    let mut carry = 0;
99    let (a_lo, a_hi) = a.split_at_mut(b.len());
100
101    for (a, b) in a_lo.iter_mut().zip(b) {
102        *a = adc(*a, *b, &mut carry);
103    }
104
105    if carry != 0 {
106        for a in a_hi {
107            *a = adc(*a, 0, &mut carry);
108            if carry == 0 {
109                break;
110            }
111        }
112    }
113
114    carry as BigDigit
115}
116
117/// Two argument addition of raw slices:
118/// a += b
119///
120/// The caller _must_ ensure that a is big enough to store the result - typically this means
121/// resizing a to max(a.len(), b.len()) + 1, to fit a possible carry.
122pub fn add2(a: &mut [BigDigit], b: &[BigDigit]) {
123    let carry = __add2(a, b);
124
125    debug_assert!(carry == 0);
126}
127
128pub fn sub2(a: &mut [BigDigit], b: &[BigDigit]) {
129    let mut borrow = 0;
130
131    let len = cmp::min(a.len(), b.len());
132    let (a_lo, a_hi) = a.split_at_mut(len);
133    let (b_lo, b_hi) = b.split_at(len);
134
135    for (a, b) in a_lo.iter_mut().zip(b_lo) {
136        *a = sbb(*a, *b, &mut borrow);
137    }
138
139    if borrow != 0 {
140        for a in a_hi {
141            *a = sbb(*a, 0, &mut borrow);
142            if borrow == 0 {
143                break;
144            }
145        }
146    }
147
148    // note: we're _required_ to fail on underflow
149    assert!(
150        borrow == 0 && b_hi.iter().all(|x| *x == 0),
151        "Cannot subtract b from a because b is larger than a."
152    );
153}
154
155// Only for the Sub impl. `a` and `b` must have same length.
156#[inline]
157pub fn __sub2rev(a: &[BigDigit], b: &mut [BigDigit]) -> BigDigit {
158    debug_assert!(b.len() == a.len());
159
160    let mut borrow = 0;
161
162    for (ai, bi) in a.iter().zip(b) {
163        *bi = sbb(*ai, *bi, &mut borrow);
164    }
165
166    borrow as BigDigit
167}
168
169pub fn sub2rev(a: &[BigDigit], b: &mut [BigDigit]) {
170    debug_assert!(b.len() >= a.len());
171
172    let len = cmp::min(a.len(), b.len());
173    let (a_lo, a_hi) = a.split_at(len);
174    let (b_lo, b_hi) = b.split_at_mut(len);
175
176    let borrow = __sub2rev(a_lo, b_lo);
177
178    assert!(a_hi.is_empty());
179
180    // note: we're _required_ to fail on underflow
181    assert!(
182        borrow == 0 && b_hi.iter().all(|x| *x == 0),
183        "Cannot subtract b from a because b is larger than a."
184    );
185}
186
187pub fn sub_sign(a: &[BigDigit], b: &[BigDigit]) -> (Sign, BigUint) {
188    // Normalize:
189    let a = &a[..a.iter().rposition(|&x| x != 0).map_or(0, |i| i + 1)];
190    let b = &b[..b.iter().rposition(|&x| x != 0).map_or(0, |i| i + 1)];
191
192    match cmp_slice(a, b) {
193        Greater => {
194            let mut a = a.to_vec();
195            sub2(&mut a, b);
196            (Plus, BigUint::new(a))
197        }
198        Less => {
199            let mut b = b.to_vec();
200            sub2(&mut b, a);
201            (Minus, BigUint::new(b))
202        }
203        _ => (NoSign, Zero::zero()),
204    }
205}
206
207/// Three argument multiply accumulate:
208/// acc += b * c
209pub fn mac_digit(acc: &mut [BigDigit], b: &[BigDigit], c: BigDigit) {
210    if c == 0 {
211        return;
212    }
213
214    let mut carry = 0;
215    let (a_lo, a_hi) = acc.split_at_mut(b.len());
216
217    for (a, &b) in a_lo.iter_mut().zip(b) {
218        *a = mac_with_carry(*a, b, c, &mut carry);
219    }
220
221    let mut a = a_hi.iter_mut();
222    while carry != 0 {
223        let a = a.next().expect("carry overflow during multiplication!");
224        *a = adc(*a, 0, &mut carry);
225    }
226}
227
228/// Three argument multiply accumulate:
229/// acc += b * c
230fn mac3(acc: &mut [BigDigit], b: &[BigDigit], c: &[BigDigit]) {
231    let (x, y) = if b.len() < c.len() { (b, c) } else { (c, b) };
232
233    // We use three algorithms for different input sizes.
234    //
235    // - For small inputs, long multiplication is fastest.
236    // - Next we use Karatsuba multiplication (Toom-2), which we have optimized
237    //   to avoid unnecessary allocations for intermediate values.
238    // - For the largest inputs we use Toom-3, which better optimizes the
239    //   number of operations, but uses more temporary allocations.
240    //
241    // The thresholds are somewhat arbitrary, chosen by evaluating the results
242    // of `cargo bench --bench bigint multiply`.
243
244    if x.len() <= 32 {
245        // Long multiplication:
246        for (i, xi) in x.iter().enumerate() {
247            mac_digit(&mut acc[i..], y, *xi);
248        }
249    } else if x.len() <= 256 {
250        /*
251         * Karatsuba multiplication:
252         *
253         * The idea is that we break x and y up into two smaller numbers that each have about half
254         * as many digits, like so (note that multiplying by b is just a shift):
255         *
256         * x = x0 + x1 * b
257         * y = y0 + y1 * b
258         *
259         * With some algebra, we can compute x * y with three smaller products, where the inputs to
260         * each of the smaller products have only about half as many digits as x and y:
261         *
262         * x * y = (x0 + x1 * b) * (y0 + y1 * b)
263         *
264         * x * y = x0 * y0
265         *       + x0 * y1 * b
266         *       + x1 * y0 * b
267         *       + x1 * y1 * b^2
268         *
269         * Let p0 = x0 * y0 and p2 = x1 * y1:
270         *
271         * x * y = p0
272         *       + (x0 * y1 + x1 * y0) * b
273         *       + p2 * b^2
274         *
275         * The real trick is that middle term:
276         *
277         *         x0 * y1 + x1 * y0
278         *
279         *       = x0 * y1 + x1 * y0 - p0 + p0 - p2 + p2
280         *
281         *       = x0 * y1 + x1 * y0 - x0 * y0 - x1 * y1 + p0 + p2
282         *
283         * Now we complete the square:
284         *
285         *       = -(x0 * y0 - x0 * y1 - x1 * y0 + x1 * y1) + p0 + p2
286         *
287         *       = -((x1 - x0) * (y1 - y0)) + p0 + p2
288         *
289         * Let p1 = (x1 - x0) * (y1 - y0), and substitute back into our original formula:
290         *
291         * x * y = p0
292         *       + (p0 + p2 - p1) * b
293         *       + p2 * b^2
294         *
295         * Where the three intermediate products are:
296         *
297         * p0 = x0 * y0
298         * p1 = (x1 - x0) * (y1 - y0)
299         * p2 = x1 * y1
300         *
301         * In doing the computation, we take great care to avoid unnecessary temporary variables
302         * (since creating a BigUint requires a heap allocation): thus, we rearrange the formula a
303         * bit so we can use the same temporary variable for all the intermediate products:
304         *
305         * x * y = p2 * b^2 + p2 * b
306         *       + p0 * b + p0
307         *       - p1 * b
308         *
309         * The other trick we use is instead of doing explicit shifts, we slice acc at the
310         * appropriate offset when doing the add.
311         */
312
313        /*
314         * When x is smaller than y, it's significantly faster to pick b such that x is split in
315         * half, not y:
316         */
317        let b = x.len() / 2;
318        let (x0, x1) = x.split_at(b);
319        let (y0, y1) = y.split_at(b);
320
321        /*
322         * We reuse the same BigUint for all the intermediate multiplies and have to size p
323         * appropriately here: x1.len() >= x0.len and y1.len() >= y0.len():
324         */
325        let len = x1.len() + y1.len() + 1;
326        let mut p = BigUint { data: vec![0; len] };
327
328        // p2 = x1 * y1
329        mac3(&mut p.data[..], x1, y1);
330
331        // Not required, but the adds go faster if we drop any unneeded 0s from the end:
332        p.normalize();
333
334        add2(&mut acc[b..], &p.data[..]);
335        add2(&mut acc[b * 2..], &p.data[..]);
336
337        // Zero out p before the next multiply:
338        p.data.truncate(0);
339        p.data.extend(repeat(0).take(len));
340
341        // p0 = x0 * y0
342        mac3(&mut p.data[..], x0, y0);
343        p.normalize();
344
345        add2(&mut acc[..], &p.data[..]);
346        add2(&mut acc[b..], &p.data[..]);
347
348        // p1 = (x1 - x0) * (y1 - y0)
349        // We do this one last, since it may be negative and acc can't ever be negative:
350        let (j0_sign, j0) = sub_sign(x1, x0);
351        let (j1_sign, j1) = sub_sign(y1, y0);
352
353        match j0_sign * j1_sign {
354            Plus => {
355                p.data.truncate(0);
356                p.data.extend(repeat(0).take(len));
357
358                mac3(&mut p.data[..], &j0.data[..], &j1.data[..]);
359                p.normalize();
360
361                sub2(&mut acc[b..], &p.data[..]);
362            }
363            Minus => {
364                mac3(&mut acc[b..], &j0.data[..], &j1.data[..]);
365            }
366            NoSign => (),
367        }
368    } else {
369        // Toom-3 multiplication:
370        //
371        // Toom-3 is like Karatsuba above, but dividing the inputs into three parts.
372        // Both are instances of Toom-Cook, using `k=3` and `k=2` respectively.
373        //
374        // The general idea is to treat the large integers digits as
375        // polynomials of a certain degree and determine the coefficients/digits
376        // of the product of the two via interpolation of the polynomial product.
377        let i = y.len() / 3 + 1;
378
379        let x0_len = cmp::min(x.len(), i);
380        let x1_len = cmp::min(x.len() - x0_len, i);
381
382        let y0_len = i;
383        let y1_len = cmp::min(y.len() - y0_len, i);
384
385        // Break x and y into three parts, representating an order two polynomial.
386        // t is chosen to be the size of a digit so we can use faster shifts
387        // in place of multiplications.
388        //
389        // x(t) = x2*t^2 + x1*t + x0
390        let x0 = BigInt::from_slice(Plus, &x[..x0_len]);
391        let x1 = BigInt::from_slice(Plus, &x[x0_len..x0_len + x1_len]);
392        let x2 = BigInt::from_slice(Plus, &x[x0_len + x1_len..]);
393
394        // y(t) = y2*t^2 + y1*t + y0
395        let y0 = BigInt::from_slice(Plus, &y[..y0_len]);
396        let y1 = BigInt::from_slice(Plus, &y[y0_len..y0_len + y1_len]);
397        let y2 = BigInt::from_slice(Plus, &y[y0_len + y1_len..]);
398
399        // Let w(t) = x(t) * y(t)
400        //
401        // This gives us the following order-4 polynomial.
402        //
403        // w(t) = w4*t^4 + w3*t^3 + w2*t^2 + w1*t + w0
404        //
405        // We need to find the coefficients w4, w3, w2, w1 and w0. Instead
406        // of simply multiplying the x and y in total, we can evaluate w
407        // at 5 points. An n-degree polynomial is uniquely identified by (n + 1)
408        // points.
409        //
410        // It is arbitrary as to what points we evaluate w at but we use the
411        // following.
412        //
413        // w(t) at t = 0, 1, -1, -2 and inf
414        //
415        // The values for w(t) in terms of x(t)*y(t) at these points are:
416        //
417        // let a = w(0)   = x0 * y0
418        // let b = w(1)   = (x2 + x1 + x0) * (y2 + y1 + y0)
419        // let c = w(-1)  = (x2 - x1 + x0) * (y2 - y1 + y0)
420        // let d = w(-2)  = (4*x2 - 2*x1 + x0) * (4*y2 - 2*y1 + y0)
421        // let e = w(inf) = x2 * y2 as t -> inf
422
423        // x0 + x2, avoiding temporaries
424        let p = &x0 + &x2;
425
426        // y0 + y2, avoiding temporaries
427        let q = &y0 + &y2;
428
429        // x2 - x1 + x0, avoiding temporaries
430        let p2 = &p - &x1;
431
432        // y2 - y1 + y0, avoiding temporaries
433        let q2 = &q - &y1;
434
435        // w(0)
436        let r0 = &x0 * &y0;
437
438        // w(inf)
439        let r4 = &x2 * &y2;
440
441        // w(1)
442        let r1 = (p + x1) * (q + y1);
443
444        // w(-1)
445        let r2 = &p2 * &q2;
446
447        // w(-2)
448        let r3 = ((p2 + x2) * 2 - x0) * ((q2 + y2) * 2 - y0);
449
450        // Evaluating these points gives us the following system of linear equations.
451        //
452        //  0  0  0  0  1 | a
453        //  1  1  1  1  1 | b
454        //  1 -1  1 -1  1 | c
455        // 16 -8  4 -2  1 | d
456        //  1  0  0  0  0 | e
457        //
458        // The solved equation (after gaussian elimination or similar)
459        // in terms of its coefficients:
460        //
461        // w0 = w(0)
462        // w1 = w(0)/2 + w(1)/3 - w(-1) + w(2)/6 - 2*w(inf)
463        // w2 = -w(0) + w(1)/2 + w(-1)/2 - w(inf)
464        // w3 = -w(0)/2 + w(1)/6 + w(-1)/2 - w(1)/6
465        // w4 = w(inf)
466        //
467        // This particular sequence is given by Bodrato and is an interpolation
468        // of the above equations.
469        let mut comp3: BigInt = (r3 - &r1) / 3;
470        let mut comp1: BigInt = (r1 - &r2) / 2;
471        let mut comp2: BigInt = r2 - &r0;
472        comp3 = (&comp2 - comp3) / 2 + &r4 * 2;
473        comp2 += &comp1 - &r4;
474        comp1 -= &comp3;
475
476        // Recomposition. The coefficients of the polynomial are now known.
477        //
478        // Evaluate at w(t) where t is our given base to get the result.
479        let result = r0
480            + (comp1 << (32 * i))
481            + (comp2 << (2 * 32 * i))
482            + (comp3 << (3 * 32 * i))
483            + (r4 << (4 * 32 * i));
484        let result_pos = result.to_biguint().unwrap();
485        add2(&mut acc[..], &result_pos.data);
486    }
487}
488
489pub fn mul3(x: &[BigDigit], y: &[BigDigit]) -> BigUint {
490    let len = x.len() + y.len() + 1;
491    let mut prod = BigUint { data: vec![0; len] };
492
493    mac3(&mut prod.data[..], x, y);
494    prod.normalized()
495}
496
497pub fn scalar_mul(a: &mut [BigDigit], b: BigDigit) -> BigDigit {
498    let mut carry = 0;
499    for a in a.iter_mut() {
500        *a = mul_with_carry(*a, b, &mut carry);
501    }
502    carry as BigDigit
503}
504
505pub fn div_rem(mut u: BigUint, mut d: BigUint) -> (BigUint, BigUint) {
506    if d.is_zero() {
507        panic!()
508    }
509    if u.is_zero() {
510        return (Zero::zero(), Zero::zero());
511    }
512
513    if d.data.len() == 1 {
514        if d.data == [1] {
515            return (u, Zero::zero());
516        }
517        let (div, rem) = div_rem_digit(u, d.data[0]);
518        // reuse d
519        d.data.clear();
520        d += rem;
521        return (div, d);
522    }
523
524    // Required or the q_len calculation below can underflow:
525    match u.cmp(&d) {
526        Less => return (Zero::zero(), u),
527        Equal => {
528            u.set_one();
529            return (u, Zero::zero());
530        }
531        Greater => {} // Do nothing
532    }
533
534    // This algorithm is from Knuth, TAOCP vol 2 section 4.3, algorithm D:
535    //
536    // First, normalize the arguments so the highest bit in the highest digit of the divisor is
537    // set: the main loop uses the highest digit of the divisor for generating guesses, so we
538    // want it to be the largest number we can efficiently divide by.
539    //
540    let shift = d.data.last().unwrap().leading_zeros() as usize;
541    let (q, r) = if shift == 0 {
542        // no need to clone d
543        div_rem_core(u, &d)
544    } else {
545        div_rem_core(u << shift, &(d << shift))
546    };
547    // renormalize the remainder
548    (q, r >> shift)
549}
550
551pub fn div_rem_ref(u: &BigUint, d: &BigUint) -> (BigUint, BigUint) {
552    if d.is_zero() {
553        panic!()
554    }
555    if u.is_zero() {
556        return (Zero::zero(), Zero::zero());
557    }
558
559    if d.data.len() == 1 {
560        if d.data == [1] {
561            return (u.clone(), Zero::zero());
562        }
563
564        let (div, rem) = div_rem_digit(u.clone(), d.data[0]);
565        return (div, rem.into());
566    }
567
568    // Required or the q_len calculation below can underflow:
569    match u.cmp(d) {
570        Less => return (Zero::zero(), u.clone()),
571        Equal => return (One::one(), Zero::zero()),
572        Greater => {} // Do nothing
573    }
574
575    // This algorithm is from Knuth, TAOCP vol 2 section 4.3, algorithm D:
576    //
577    // First, normalize the arguments so the highest bit in the highest digit of the divisor is
578    // set: the main loop uses the highest digit of the divisor for generating guesses, so we
579    // want it to be the largest number we can efficiently divide by.
580    //
581    let shift = d.data.last().unwrap().leading_zeros() as usize;
582
583    let (q, r) = if shift == 0 {
584        // no need to clone d
585        div_rem_core(u.clone(), d)
586    } else {
587        div_rem_core(u << shift, &(d << shift))
588    };
589    // renormalize the remainder
590    (q, r >> shift)
591}
592
593/// an implementation of Knuth, TAOCP vol 2 section 4.3, algorithm D
594///
595/// # Correctness
596///
597/// This function requires the following conditions to run correctly and/or effectively
598///
599/// - `a > b`
600/// - `d.data.len() > 1`
601/// - `d.data.last().unwrap().leading_zeros() == 0`
602fn div_rem_core(mut a: BigUint, b: &BigUint) -> (BigUint, BigUint) {
603    // The algorithm works by incrementally calculating "guesses", q0, for part of the
604    // remainder. Once we have any number q0 such that q0 * b <= a, we can set
605    //
606    //     q += q0
607    //     a -= q0 * b
608    //
609    // and then iterate until a < b. Then, (q, a) will be our desired quotient and remainder.
610    //
611    // q0, our guess, is calculated by dividing the last few digits of a by the last digit of b
612    // - this should give us a guess that is "close" to the actual quotient, but is possibly
613    // greater than the actual quotient. If q0 * b > a, we simply use iterated subtraction
614    // until we have a guess such that q0 * b <= a.
615    //
616
617    let bn = *b.data.last().unwrap();
618    let q_len = a.data.len() - b.data.len() + 1;
619    let mut q = BigUint {
620        data: vec![0; q_len],
621    };
622
623    // We reuse the same temporary to avoid hitting the allocator in our inner loop - this is
624    // sized to hold a0 (in the common case; if a particular digit of the quotient is zero a0
625    // can be bigger).
626    //
627    let mut tmp = BigUint {
628        data: Vec::with_capacity(2),
629    };
630
631    for j in (0..q_len).rev() {
632        /*
633         * When calculating our next guess q0, we don't need to consider the digits below j
634         * + b.data.len() - 1: we're guessing digit j of the quotient (i.e. q0 << j) from
635         * digit bn of the divisor (i.e. bn << (b.data.len() - 1) - so the product of those
636         * two numbers will be zero in all digits up to (j + b.data.len() - 1).
637         */
638        let offset = j + b.data.len() - 1;
639        if offset >= a.data.len() {
640            continue;
641        }
642
643        /* just avoiding a heap allocation: */
644        let mut a0 = tmp;
645        a0.data.truncate(0);
646        a0.data.extend(a.data[offset..].iter().cloned());
647
648        /*
649         * q0 << j * big_digit::BITS is our actual quotient estimate - we do the shifts
650         * implicitly at the end, when adding and subtracting to a and q. Not only do we
651         * save the cost of the shifts, the rest of the arithmetic gets to work with
652         * smaller numbers.
653         */
654        let (mut q0, _) = div_rem_digit(a0, bn);
655        let mut prod = b * &q0;
656
657        while cmp_slice(&prod.data[..], &a.data[j..]) == Greater {
658            let one: BigUint = One::one();
659            q0 -= one;
660            prod -= b;
661        }
662
663        add2(&mut q.data[j..], &q0.data[..]);
664        sub2(&mut a.data[j..], &prod.data[..]);
665        a.normalize();
666
667        tmp = q0;
668    }
669
670    debug_assert!(a < *b);
671
672    (q.normalized(), a)
673}
674
675/// Find last set bit
676/// fls(0) == 0, fls(u32::MAX) == 32
677pub fn fls<T: traits::PrimInt>(v: T) -> usize {
678    mem::size_of::<T>() * 8 - v.leading_zeros() as usize
679}
680
681pub fn ilog2<T: traits::PrimInt>(v: T) -> usize {
682    fls(v) - 1
683}
684
685#[inline]
686pub fn biguint_shl(n: Cow<BigUint>, bits: usize) -> BigUint {
687    let n_unit = bits / big_digit::BITS;
688    let mut data = match n_unit {
689        0 => n.into_owned().data,
690        _ => {
691            let len = n_unit + n.data.len() + 1;
692            let mut data = Vec::with_capacity(len);
693            data.extend(repeat(0).take(n_unit));
694            data.extend(n.data.iter().cloned());
695            data
696        }
697    };
698
699    let n_bits = bits % big_digit::BITS;
700    if n_bits > 0 {
701        let mut carry = 0;
702        for elem in data[n_unit..].iter_mut() {
703            let new_carry = *elem >> (big_digit::BITS - n_bits);
704            *elem = (*elem << n_bits) | carry;
705            carry = new_carry;
706        }
707        if carry != 0 {
708            data.push(carry);
709        }
710    }
711
712    BigUint::new(data)
713}
714
715#[inline]
716pub fn biguint_shr(n: Cow<BigUint>, bits: usize) -> BigUint {
717    let n_unit = bits / big_digit::BITS;
718    if n_unit >= n.data.len() {
719        return Zero::zero();
720    }
721    let mut data = match n {
722        Cow::Borrowed(n) => n.data[n_unit..].to_vec(),
723        Cow::Owned(mut n) => {
724            n.data.drain(..n_unit);
725            n.data
726        }
727    };
728
729    let n_bits = bits % big_digit::BITS;
730    if n_bits > 0 {
731        let mut borrow = 0;
732        for elem in data.iter_mut().rev() {
733            let new_borrow = *elem << (big_digit::BITS - n_bits);
734            *elem = (*elem >> n_bits) | borrow;
735            borrow = new_borrow;
736        }
737    }
738
739    BigUint::new(data)
740}
741
742pub fn cmp_slice(a: &[BigDigit], b: &[BigDigit]) -> Ordering {
743    debug_assert!(a.last() != Some(&0));
744    debug_assert!(b.last() != Some(&0));
745
746    let (a_len, b_len) = (a.len(), b.len());
747    if a_len < b_len {
748        return Less;
749    }
750    if a_len > b_len {
751        return Greater;
752    }
753
754    for (&ai, &bi) in a.iter().rev().zip(b.iter().rev()) {
755        if ai < bi {
756            return Less;
757        }
758        if ai > bi {
759            return Greater;
760        }
761    }
762    Equal
763}
764
765#[cfg(test)]
766mod algorithm_tests {
767    use big_digit::BigDigit;
768    use traits::Num;
769    use Sign::Plus;
770    use {BigInt, BigUint};
771
772    #[test]
773    fn test_sub_sign() {
774        use super::sub_sign;
775
776        fn sub_sign_i(a: &[BigDigit], b: &[BigDigit]) -> BigInt {
777            let (sign, val) = sub_sign(a, b);
778            BigInt::from_biguint(sign, val)
779        }
780
781        let a = BigUint::from_str_radix("265252859812191058636308480000000", 10).unwrap();
782        let b = BigUint::from_str_radix("26525285981219105863630848000000", 10).unwrap();
783        let a_i = BigInt::from_biguint(Plus, a.clone());
784        let b_i = BigInt::from_biguint(Plus, b.clone());
785
786        assert_eq!(sub_sign_i(&a.data[..], &b.data[..]), &a_i - &b_i);
787        assert_eq!(sub_sign_i(&b.data[..], &a.data[..]), &b_i - &a_i);
788    }
789}