Skip to content

Reimplement the generic fmod #536

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#![allow(clippy::float_cmp)]
#![allow(clippy::int_plus_one)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::just_underscores_and_digits)]
Comment on lines 14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorting nit

Suggested change
#![allow(clippy::many_single_char_names)]
#![allow(clippy::just_underscores_and_digits)]
#![allow(clippy::just_underscores_and_digits)]
#![allow(clippy::many_single_char_names)]

#![allow(clippy::mixed_case_hex_literals)]
#![allow(clippy::needless_late_init)]
#![allow(clippy::needless_return)]
Expand Down
109 changes: 44 additions & 65 deletions src/math/generic/fmod.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,63 @@
/* SPDX-License-Identifier: MIT */
/* origin: musl src/math/fmod.c. Ported to generic Rust algorithm in 2025, TG. */

use super::super::{CastFrom, Float, Int, MinInt};

/// Given the bits of a positive float, clamp the exponent field to [0,1]
fn collapse_exponent<F: Float>(bits: F::Int) -> F::Int {
let sig = bits & F::SIG_MASK;
if sig == bits { sig } else { sig | F::IMPLICIT_BIT }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if sig == bits { sig } else { sig | F::IMPLICIT_BIT }
// Set the implicit bit the input was not subnormal
if sig == bits { sig } else { sig | F::IMPLICIT_BIT }


/// Computes (x << e) % y
fn reduction<I: Int>(mut x: I, e: u32, y: I) -> I {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Computes (x << e) % y
/// Computes `(x << e) % y` exactly without using bigint math

Or something to indicate why this isn't exactly (x << e) % y

x %= y;
for _ in 0..e {
x <<= 1;
x = x.checked_sub(y).unwrap_or(x);
}
x
}

#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmod<F: Float>(x: F, y: F) -> F {
let zero = F::Int::ZERO;
let one = F::Int::ONE;
let _1 = F::Int::ONE;
let mut ix = x.to_bits();
let mut iy = y.to_bits();
let mut ex = x.ex().signed();
let mut ey = y.ex().signed();
let sx = ix & F::SIGN_MASK;

if iy << 1 == zero || y.is_nan() || ex == F::EXP_SAT as i32 {
return (x * y) / (x * y);
}

if ix << 1 <= iy << 1 {
if ix << 1 == iy << 1 {
return F::ZERO * x;
}
return x;
}
let sx = ix & F::SIGN_MASK;
ix &= !F::SIGN_MASK;
iy &= !F::SIGN_MASK;

/* normalize x and y */
if ex == 0 {
let i = ix << F::EXP_BITS;
ex -= i.leading_zeros() as i32;
ix <<= -ex + 1;
} else {
ix &= F::Int::MAX >> F::EXP_BITS;
ix |= one << F::SIG_BITS;
if ix >= F::EXP_MASK {
// x is nan or inf
return F::NAN;
}

if ey == 0 {
let i = iy << F::EXP_BITS;
ey -= i.leading_zeros() as i32;
iy <<= -ey + 1;
} else {
iy &= F::Int::MAX >> F::EXP_BITS;
iy |= one << F::SIG_BITS;
if iy.wrapping_sub(_1) >= F::EXP_MASK {
// y is nan or zero
return F::NAN;
}

/* x mod y */
while ex > ey {
let i = ix.wrapping_sub(iy);
if i >> (F::BITS - 1) == zero {
if i == zero {
return F::ZERO * x;
}
ix = i;
}

ix <<= 1;
ex -= 1;
}
if ix < iy {
// |x| < |y|
return x;
};

let i = ix.wrapping_sub(iy);
if i >> (F::BITS - 1) == zero {
if i == zero {
return F::ZERO * x;
}
let ex: u32 = x.ex().saturating_sub(1);
let ey: u32 = y.ex().saturating_sub(1);

ix = i;
}
let num = collapse_exponent::<F>(ix);
let div = collapse_exponent::<F>(iy);

let shift = ix.leading_zeros().saturating_sub(F::EXP_BITS);
ix <<= shift;
ex -= shift as i32;
let num = reduction(num, ex - ey, div);

/* scale result */
if ex > 0 {
ix -= one << F::SIG_BITS;
ix |= F::Int::cast_from(ex) << F::SIG_BITS;
if num.is_zero() {
F::from_bits(sx)
} else {
ix >>= -ex + 1;
Comment on lines +52 to 54
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if num.is_zero() {
F::from_bits(sx)
} else {
if num.is_zero() {
// Return zero with the sign of x
return F::from_bits(sx)
}

May a well early return to get rid of the else branch block.

}
let ilog = num.ilog2();
let shift = (ey + ilog).min(F::SIG_BITS) - ilog;
let scale = (ey + ilog).saturating_sub(F::SIG_BITS);

ix |= sx;

F::from_bits(ix)
let normalized = num << shift;
let scaled = normalized + (F::Int::cast_from(scale) << F::SIG_BITS);
F::from_bits(sx | scaled)
}
Comment on lines +55 to +61
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you mind adding a comment about the algorithm here?

}
4 changes: 4 additions & 0 deletions src/math/support/int_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub trait Int:
+ PartialOrd
+ ops::AddAssign
+ ops::SubAssign
+ ops::MulAssign
+ ops::DivAssign
+ ops::RemAssign
+ ops::BitAndAssign
+ ops::BitOrAssign
+ ops::BitXorAssign
Expand All @@ -51,6 +54,7 @@ pub trait Int:
+ ops::Sub<Output = Self>
+ ops::Mul<Output = Self>
+ ops::Div<Output = Self>
+ ops::Rem<Output = Self>
+ ops::Shl<i32, Output = Self>
+ ops::Shl<u32, Output = Self>
+ ops::Shr<i32, Output = Self>
Expand Down
Loading