Merge branch 'num-bigint-0.1.x' into master

This commit is contained in:
Josh Stone 2018-05-14 13:01:51 -07:00
commit b374f9ac92
4 changed files with 53 additions and 1 deletions

@ -15,6 +15,9 @@ readme = "README.md"
[[bench]]
name = "bigint"
[[bench]]
name = "factorial"
[[bench]]
name = "gcd"

@ -9,6 +9,16 @@
[8]: https://github.com/rust-num/num-bigint/pull/8
[23]: https://github.com/rust-num/num-bigint/pull/23
# Release 0.1.44
- [Division with single-digit divisors is now much faster.][42]
- The README now compares [`ramp`, `rug`, `rust-gmp`][20], and [`apint`][21].
**Contributors**: @cuviper, @Robbepop
[20]: https://github.com/rust-num/num-bigint/pull/20
[21]: https://github.com/rust-num/num-bigint/pull/21
[42]: https://github.com/rust-num/num-bigint/pull/42
# Release 0.1.43

35
benches/factorial.rs Executable file

@ -0,0 +1,35 @@
#![feature(test)]
extern crate num_bigint;
extern crate num_traits;
extern crate test;
use num_bigint::BigUint;
use num_traits::One;
use std::ops::{Div, Mul};
use test::Bencher;
#[bench]
fn factorial_mul_biguint(b: &mut Bencher) {
b.iter(|| (1u32..1000).map(BigUint::from).fold(BigUint::one(), Mul::mul));
}
#[bench]
fn factorial_mul_u32(b: &mut Bencher) {
b.iter(|| (1u32..1000).fold(BigUint::one(), Mul::mul));
}
// The division test is inspired by this blog comparison:
// <https://tiehuis.github.io/big-integers-in-zig#division-test-single-limb>
#[bench]
fn factorial_div_biguint(b: &mut Bencher) {
let n: BigUint = (1u32..1000).fold(BigUint::one(), Mul::mul);
b.iter(|| (1u32..1000).rev().map(BigUint::from).fold(n.clone(), Div::div));
}
#[bench]
fn factorial_div_u32(b: &mut Bencher) {
let n: BigUint = (1u32..1000).fold(BigUint::one(), Mul::mul);
b.iter(|| (1u32..1000).rev().fold(n.clone(), Div::div));
}

@ -424,9 +424,13 @@ pub fn div_rem(u: &BigUint, d: &BigUint) -> (BigUint, BigUint) {
if u.is_zero() {
return (Zero::zero(), Zero::zero());
}
if *d == One::one() {
if d.data == [1] {
return (u.clone(), Zero::zero());
}
if d.data.len() == 1 {
let (div, rem) = div_rem_digit(u.clone(), d.data[0]);
return (div, rem.into());
}
// Required or the q_len calculation below can underflow:
match u.cmp(d) {