Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use divide and conquer in to_radix_digits #316

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ fn to_str_radix_10_2(b: &mut Bencher) {
to_str_radix_bench(b, 10, 10009);
}

#[bench]
fn to_str_radix_10_3(b: &mut Bencher) {
to_str_radix_bench(b, 10, 100009);
}

#[bench]
fn to_str_radix_10_4(b: &mut Bencher) {
to_str_radix_bench(b, 10, 1000009);
}

#[bench]
fn to_str_radix_16(b: &mut Bencher) {
to_str_radix_bench(b, 16, 1009);
Expand Down
94 changes: 67 additions & 27 deletions src/biguint/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This uses stdlib features higher than the MSRV
#![allow(clippy::manual_range_contains)] // 1.35

use super::{biguint_from_vec, BigUint, ToBigUint};
use super::{biguint_from_vec, BigUint, IntDigits, ToBigUint};

use super::addition::add2;
use super::division::{div_rem_digit, FAST_DIV_WIDE};
Expand Down Expand Up @@ -700,35 +700,49 @@ pub(super) fn to_radix_digits_le(u: &BigUint, radix: u32) -> Vec<u8> {
// performance. We can mitigate this by dividing into chunks of a larger base first.
// The threshold for this was chosen by anecdotal performance measurements to
// approximate where this starts to make a noticeable difference.
if digits.data.len() >= 64 {
let mut big_base = BigUint::from(base);
let mut big_power = 1usize;

// Choose a target base length near √n.
let target_len = digits.data.len().sqrt();
while big_base.data.len() < target_len {
big_base = &big_base * &big_base;
big_power *= 2;
}

// This outer loop will run approximately √n times.
while digits > big_base {
// This is still the dominating factor, with n digits divided by √n digits.
let (q, mut big_r) = digits.div_rem(&big_base);
digits = q;

// This inner loop now has O(√n²)=O(n) behavior altogether.
for _ in 0..big_power {
let (q, mut r) = div_rem_digit(big_r, base);
big_r = q;
for _ in 0..power {
res.push((r % radix) as u8);
r /= radix;
}
if digits.data.len() >= 32 {
let mut big_bases = Vec::with_capacity(32);
big_bases.push((BigUint::from(base), power));

loop {
let (big_base, power) = big_bases.last().unwrap();
if big_base.data.len() > digits.data.len() / 2 + 1 {
break;
}
let next_big_base = big_base * big_base;
let next_power = *power * 2;
big_bases.push((next_big_base, next_power));
}

to_radix_digits_le_divide_and_conquer(
digits,
base,
power,
&big_bases,
big_bases.len() - 1,
&mut res,
radix,
);
while res.last() == Some(&0) {
res.pop();
}
return res;
}

to_radix_digits_le_small(digits, base, power, &mut res, radix);

res
}

// Extract little-endian radix digits for small numbers
#[inline(always)] // forced inline to get const-prop for radix=10
fn to_radix_digits_le_small(
mut digits: BigUint,
base: u64,
power: usize,
res: &mut Vec<u8>,
radix: u64,
) {
while digits.data.len() > 1 {
let (q, mut r) = div_rem_digit(digits, base);
for _ in 0..power {
Expand All @@ -743,8 +757,34 @@ pub(super) fn to_radix_digits_le(u: &BigUint, radix: u32) -> Vec<u8> {
res.push((r % radix) as u8);
r /= radix;
}
}

res
fn to_radix_digits_le_divide_and_conquer(
number: BigUint,
base: u64,
power: usize,
big_bases: &[(BigUint, usize)],
k: usize,
res: &mut Vec<u8>,
radix: u64,
) {
let &(ref big_base, result_len) = &big_bases[k];
if number.data.len() < 8 {
let prev_res_len = res.len();
if !number.is_zero() {
to_radix_digits_le_small(number, base, power, res, radix);
}
while res.len() < prev_res_len + result_len * 2 {
res.push(0);
}
return;
}
// number always has two digits in the big base
let (digit_1, digit_2) = number.div_rem(big_base);
assert!(&digit_1 < big_base);
assert!(&digit_2 < big_base);
to_radix_digits_le_divide_and_conquer(digit_2, base, power, big_bases, k - 1, res, radix);
to_radix_digits_le_divide_and_conquer(digit_1, base, power, big_bases, k - 1, res, radix);
}

pub(super) fn to_radix_le(u: &BigUint, radix: u32) -> Vec<u8> {
Expand Down