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

Open
wants to merge 7 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
55 changes: 51 additions & 4 deletions benches/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,19 @@ 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(
|| {
let x = U256::random(&mut rng);
let y = U128::random(&mut rng);
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(x.div_rem(&y)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a side node, you don't need to wrap the output in black_box, Criterion already does that for all iter* methods.

BatchSize::SmallInput,
)
});

group.bench_function("div/rem, U256/U128 (in U256)", |b| {
b.iter_batched(
|| {
let x = U256::random(&mut rng);
Expand All @@ -232,6 +244,18 @@ fn bench_division(c: &mut Criterion) {
)
});

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)| black_box(x.div_rem(&y)),
BatchSize::SmallInput,
)
});

group.bench_function("div/rem_vartime, U256/U128, full size", |b| {
b.iter_batched(
|| {
Expand All @@ -244,12 +268,35 @@ fn bench_division(c: &mut Criterion) {
)
});

group.bench_function("rem, U256/U128, full size", |b| {
group.bench_function("rem, U256/U128", |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 = U128::random(&mut rng);
(x, NonZero::new(y).unwrap())
},
|(x, y)| black_box(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)| black_box(x.rem(&y)),
BatchSize::SmallInput,
)
});

group.bench_function("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)| black_box(x.rem(&y)),
Expand Down
128 changes: 78 additions & 50 deletions src/int/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
///
/// Computes the quotient and remainder of `self / rhs`.
/// Furthermore, returns the signs of `self` and `rhs`.
const fn div_rem_base(
const fn div_rem_base<const RHS_LIMBS: usize>(
&self,
rhs: &NonZero<Self>,
) -> (Uint<{ LIMBS }>, Uint<{ LIMBS }>, ConstChoice, ConstChoice) {
rhs: &NonZero<Int<RHS_LIMBS>>,
) -> (Uint<LIMBS>, Uint<RHS_LIMBS>, ConstChoice, ConstChoice) {
// Step 1: split operands into signs and magnitudes.
let (lhs_mag, lhs_sgn) = self.abs_sign();
let (rhs_mag, rhs_sgn) = rhs.abs_sign();
Expand Down Expand Up @@ -52,7 +52,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
/// assert_eq!(quotient.unwrap(), I128::from(2));
/// assert_eq!(remainder, I128::from(-2));
/// ```
pub const fn checked_div_rem(&self, rhs: &NonZero<Self>) -> (ConstCtOption<Self>, Self) {
pub const fn checked_div_rem<const RHS_LIMBS: usize>(
&self,
rhs: &NonZero<Int<RHS_LIMBS>>,
) -> (ConstCtOption<Self>, Int<RHS_LIMBS>) {
let (quotient, remainder, lhs_sgn, rhs_sgn) = self.div_rem_base(rhs);
let opposing_signs = lhs_sgn.ne(rhs_sgn);
(
Expand All @@ -66,12 +69,15 @@ impl<const LIMBS: usize> Int<LIMBS> {
/// - `self != MIN` or `rhs != MINUS_ONE`.
///
/// Note: this operation rounds towards zero, truncating any fractional part of the exact result.
pub fn checked_div(&self, rhs: &Self) -> CtOption<Self> {
pub fn checked_div<const RHS_LIMBS: usize>(&self, rhs: &Int<RHS_LIMBS>) -> CtOption<Self> {
NonZero::new(*rhs).and_then(|rhs| self.checked_div_rem(&rhs).0.into())
}

/// Computes `self` % `rhs`, returns the remainder.
pub const fn rem(&self, rhs: &NonZero<Self>) -> Self {
pub const fn rem<const RHS_LIMBS: usize>(
&self,
rhs: &NonZero<Int<RHS_LIMBS>>,
) -> Int<RHS_LIMBS> {
self.checked_div_rem(rhs).1
}
}
Expand Down Expand Up @@ -223,7 +229,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
/// I128::from(2)
/// )
/// ```
pub fn checked_div_floor(&self, rhs: &Self) -> CtOption<Self> {
pub fn checked_div_floor<const RHS_LIMBS: usize>(
&self,
rhs: &Int<RHS_LIMBS>,
) -> CtOption<Self> {
NonZero::new(*rhs).and_then(|rhs| self.checked_div_rem_floor(&rhs).0.into())
}

Expand Down Expand Up @@ -257,7 +266,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
/// assert_eq!(quotient.unwrap(), I128::from(2));
/// assert_eq!(remainder, I128::from(2));
/// ```
pub const fn checked_div_rem_floor(&self, rhs: &NonZero<Self>) -> (ConstCtOption<Self>, Self) {
pub const fn checked_div_rem_floor<const RHS_LIMBS: usize>(
&self,
rhs: &NonZero<Int<RHS_LIMBS>>,
) -> (ConstCtOption<Self>, Int<RHS_LIMBS>) {
let (lhs_mag, lhs_sgn) = self.abs_sign();
let (rhs_mag, rhs_sgn) = rhs.abs_sign();
let (quotient, remainder) = lhs_mag.div_rem(&rhs_mag);
Expand All @@ -283,40 +295,40 @@ impl<const LIMBS: usize> Int<LIMBS> {
}
}

impl<const LIMBS: usize> CheckedDiv for Int<LIMBS> {
fn checked_div(&self, rhs: &Int<LIMBS>) -> CtOption<Self> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> CheckedDiv<Int<RHS_LIMBS>> for Int<LIMBS> {
fn checked_div(&self, rhs: &Int<RHS_LIMBS>) -> CtOption<Self> {
self.checked_div(rhs)
}
}

impl<const LIMBS: usize> Div<&NonZero<Int<LIMBS>>> for &Int<LIMBS> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
type Output = CtOption<Int<LIMBS>>;

fn div(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self / *rhs
}
}

impl<const LIMBS: usize> Div<&NonZero<Int<LIMBS>>> for Int<LIMBS> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
type Output = CtOption<Int<LIMBS>>;

fn div(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
self / *rhs
}
}

impl<const LIMBS: usize> Div<NonZero<Int<LIMBS>>> for &Int<LIMBS> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
type Output = CtOption<Int<LIMBS>>;

fn div(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self / rhs
}
}

impl<const LIMBS: usize> Div<NonZero<Int<LIMBS>>> for Int<LIMBS> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
type Output = CtOption<Int<LIMBS>>;

fn div(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
self.checked_div(&rhs)
}
}
Expand All @@ -333,34 +345,42 @@ impl<const LIMBS: usize> DivAssign<NonZero<Int<LIMBS>>> for Int<LIMBS> {
}
}

impl<const LIMBS: usize> Div<NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>>
for Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<LIMBS>>;

fn div(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
Wrapping((self.0 / rhs).expect("cannot represent positive equivalent of Int::MIN as int"))
}
}

impl<const LIMBS: usize> Div<NonZero<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>>
for &Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<LIMBS>>;

fn div(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self / rhs
}
}

impl<const LIMBS: usize> Div<&NonZero<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>>
for &Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<LIMBS>>;

fn div(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self / *rhs
}
}

impl<const LIMBS: usize> Div<&NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>>
for Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<LIMBS>>;

fn div(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
self / *rhs
}
}
Expand All @@ -379,34 +399,34 @@ impl<const LIMBS: usize> DivAssign<NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>>
}
}

impl<const LIMBS: usize> Rem<&NonZero<Int<LIMBS>>> for &Int<LIMBS> {
type Output = Int<LIMBS>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
type Output = Int<RHS_LIMBS>;

fn rem(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self % *rhs
}
}

impl<const LIMBS: usize> Rem<&NonZero<Int<LIMBS>>> for Int<LIMBS> {
type Output = Int<LIMBS>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
type Output = Int<RHS_LIMBS>;

fn rem(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
self % *rhs
}
}

impl<const LIMBS: usize> Rem<NonZero<Int<LIMBS>>> for &Int<LIMBS> {
type Output = Int<LIMBS>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
type Output = Int<RHS_LIMBS>;

fn rem(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self % rhs
}
}

impl<const LIMBS: usize> Rem<NonZero<Int<LIMBS>>> for Int<LIMBS> {
type Output = Int<LIMBS>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
type Output = Int<RHS_LIMBS>;

fn rem(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
Self::rem(&self, &rhs)
}
}
Expand All @@ -423,34 +443,42 @@ impl<const LIMBS: usize> RemAssign<NonZero<Int<LIMBS>>> for Int<LIMBS> {
}
}

impl<const LIMBS: usize> Rem<NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
type Output = Wrapping<Int<LIMBS>>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>>
for Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<RHS_LIMBS>>;

fn rem(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
Wrapping(self.0 % rhs)
}
}

impl<const LIMBS: usize> Rem<NonZero<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
type Output = Wrapping<Int<LIMBS>>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>>
for &Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<RHS_LIMBS>>;

fn rem(self, rhs: NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self % rhs
}
}

impl<const LIMBS: usize> Rem<&NonZero<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> {
type Output = Wrapping<Int<LIMBS>>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>>
for &Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<RHS_LIMBS>>;

fn rem(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
*self % *rhs
}
}

impl<const LIMBS: usize> Rem<&NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
type Output = Wrapping<Int<LIMBS>>;
impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>>
for Wrapping<Int<LIMBS>>
{
type Output = Wrapping<Int<RHS_LIMBS>>;

fn rem(self, rhs: &NonZero<Int<LIMBS>>) -> Self::Output {
fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
self % *rhs
}
}
Expand Down
Loading