Skip to content

div with differently sized denominators #799

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

Merged
merged 10 commits into from
Jun 3, 2025
Merged
80 changes: 56 additions & 24 deletions benches/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,35 @@ fn bench_division(c: &mut Criterion) {
let mut rng = make_rng();
let mut group = c.benchmark_group("wrapping ops");

group.bench_function("div/rem, U256/U128, full size", |b| {
group.bench_function("div/rem, U256/U128", |b| {
b.iter_batched(
|| (U256::random(&mut rng), NonZero::<U128>::random(&mut rng)),
|(x, y)| x.div_rem(&y),
BatchSize::SmallInput,
)
});

group.bench_function("div/rem, U256/U128 (in U256)", |b| {
b.iter_batched(
|| {
let x = U256::random(&mut rng);
let y_half = U128::random(&mut rng);
let y: U256 = (y_half, U128::ZERO).into();
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(x.div_rem(&y)),
|(x, y)| x.div_rem(&y),
BatchSize::SmallInput,
)
});

group.bench_function("div/rem, U256/U128 (in U512)", |b| {
b.iter_batched(
|| {
let x = U256::random(&mut rng);
let y: U512 = U128::random(&mut rng).resize();
(x, NonZero::new(y).unwrap())
},
|(x, y)| x.div_rem(&y),
BatchSize::SmallInput,
)
});
Expand All @@ -239,20 +259,39 @@ fn bench_division(c: &mut Criterion) {
let y: U256 = (U128::MAX, U128::ZERO).into();
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(x.div_rem_vartime(&y)),
|(x, y)| x.div_rem_vartime(&y),
BatchSize::SmallInput,
)
});

group.bench_function("rem, U256/U128", |b| {
b.iter_batched(
|| (U256::random(&mut rng), NonZero::<U128>::random(&mut rng)),
|(x, y)| x.rem(&y),
BatchSize::SmallInput,
)
});

group.bench_function("rem, U256/U128 (in U256)", |b| {
b.iter_batched(
|| {
let x = U256::random(&mut rng);
let y: U256 = U128::random(&mut rng).resize();
(x, NonZero::new(y).unwrap())
},
|(x, y)| x.rem(&y),
BatchSize::SmallInput,
)
});

group.bench_function("rem, U256/U128, full size", |b| {
group.bench_function("rem, U256/U128 (in U512)", |b| {
b.iter_batched(
|| {
let x = U256::random(&mut rng);
let y_half = U128::random(&mut rng);
let y: U256 = (y_half, U128::ZERO).into();
let y: U512 = U128::random(&mut rng).resize();
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(x.rem(&y)),
|(x, y)| x.rem(&y),
BatchSize::SmallInput,
)
});
Expand All @@ -264,7 +303,7 @@ fn bench_division(c: &mut Criterion) {
let y: U256 = (U128::MAX, U128::ZERO).into();
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(x.rem_vartime(&y)),
|(x, y)| x.rem_vartime(&y),
BatchSize::SmallInput,
)
});
Expand All @@ -276,7 +315,7 @@ fn bench_division(c: &mut Criterion) {
let y: U256 = (U128::MAX, U128::ZERO).into();
(x_lo, x_hi, NonZero::new(y).unwrap())
},
|(x_lo, x_hi, y)| black_box(Uint::rem_wide_vartime((x_lo, x_hi), &y)),
|(x_lo, x_hi, y)| Uint::rem_wide_vartime((x_lo, x_hi), &y),
BatchSize::SmallInput,
)
});
Expand All @@ -289,35 +328,28 @@ fn bench_division(c: &mut Criterion) {
let y = U256::from_word(y_small.0);
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(x.div_rem(&y)),
|(x, y)| x.div_rem(&y),
BatchSize::SmallInput,
)
});

group.bench_function("div/rem, U256/Limb, single limb", |b| {
b.iter_batched(
|| {
let x = U256::random(&mut rng);
let y = Limb::random(&mut rng);
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(x.div_rem_limb(y)),
|| (U256::random(&mut rng), NonZero::<Limb>::random(&mut rng)),
|(x, y)| x.div_rem_limb(y),
BatchSize::SmallInput,
)
});

group.bench_function("div/rem, U256/Limb, single limb with reciprocal", |b| {
b.iter_batched(
|| {
let x = U256::random(&mut rng);
let mut y = Limb::random(&mut rng);
if y == Limb::ZERO {
y = Limb::ONE;
}
let r = Reciprocal::new(NonZero::new(y).unwrap());
(x, r)
(
U256::random(&mut rng),
Reciprocal::new(NonZero::<Limb>::random(&mut rng)),
)
},
|(x, r)| black_box(x.div_rem_limb_with_reciprocal(&r)),
|(x, r)| x.div_rem_limb_with_reciprocal(&r),
BatchSize::SmallInput,
)
});
Expand Down
Loading