Reimplement the generic fmod

This commit is contained in:
quaternic
2025-04-22 03:56:51 +03:00
committed by Trevor Gross
parent a8652953e4
commit e075e9fbde
3 changed files with 58 additions and 69 deletions

View File

@@ -14,6 +14,7 @@
#![allow(clippy::excessive_precision)] #![allow(clippy::excessive_precision)]
#![allow(clippy::float_cmp)] #![allow(clippy::float_cmp)]
#![allow(clippy::int_plus_one)] #![allow(clippy::int_plus_one)]
#![allow(clippy::just_underscores_and_digits)]
#![allow(clippy::many_single_char_names)] #![allow(clippy::many_single_char_names)]
#![allow(clippy::mixed_case_hex_literals)] #![allow(clippy::mixed_case_hex_literals)]
#![allow(clippy::needless_late_init)] #![allow(clippy::needless_late_init)]

View File

@@ -1,84 +1,68 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT OR Apache-2.0 */
/* origin: musl src/math/fmod.c. Ported to generic Rust algorithm in 2025, TG. */
use super::super::{CastFrom, Float, Int, MinInt}; use super::super::{CastFrom, Float, Int, MinInt};
#[inline] #[inline]
pub fn fmod<F: Float>(x: F, y: F) -> F { pub fn fmod<F: Float>(x: F, y: F) -> F {
let zero = F::Int::ZERO; let _1 = F::Int::ONE;
let one = F::Int::ONE; let sx = x.to_bits() & F::SIGN_MASK;
let mut ix = x.to_bits(); let ux = x.to_bits() & !F::SIGN_MASK;
let mut iy = y.to_bits(); let uy = y.to_bits() & !F::SIGN_MASK;
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 { // Cases that return NaN:
// NaN % _
// Inf % _
// _ % NaN
// _ % 0
let x_nan_or_inf = ux & F::EXP_MASK == F::EXP_MASK;
let y_nan_or_zero = uy.wrapping_sub(_1) & F::EXP_MASK == F::EXP_MASK;
if x_nan_or_inf | y_nan_or_zero {
return (x * y) / (x * y); return (x * y) / (x * y);
} }
if ix << 1 <= iy << 1 { if ux < uy {
if ix << 1 == iy << 1 { // |x| < |y|
return F::ZERO * x;
}
return x; return x;
} }
/* normalize x and y */ let (num, ex) = into_sig_exp::<F>(ux);
if ex == 0 { let (div, ey) = into_sig_exp::<F>(uy);
let i = ix << (F::EXP_BITS + 1);
ex -= i.leading_zeros() as i32; // To compute `(num << ex) % (div << ey)`, first
ix <<= -ex + 1; // evaluate `rem = (num << (ex - ey)) % div` ...
} else { let rem = reduction(num, ex - ey, div);
ix &= F::Int::MAX >> F::EXP_BITS; // ... so the result will be `rem << ey`
ix |= one << F::SIG_BITS;
if rem.is_zero() {
// Return zero with the sign of `x`
return F::from_bits(sx);
};
// We would shift `rem` up by `ey`, but have to stop at `F::SIG_BITS`
let shift = ey.min(F::SIG_BITS - rem.ilog2());
// Anything past that is added to the exponent field
let bits = (rem << shift) + (F::Int::cast_from(ey - shift) << F::SIG_BITS);
F::from_bits(sx + bits)
} }
if ey == 0 { /// Given the bits of a finite float, return a tuple of
let i = iy << (F::EXP_BITS + 1); /// - the mantissa with the implicit bit (0 if subnormal, 1 otherwise)
ey -= i.leading_zeros() as i32; /// - the additional exponent past 1, (0 for subnormal, 0 or more otherwise)
iy <<= -ey + 1; fn into_sig_exp<F: Float>(mut bits: F::Int) -> (F::Int, u32) {
} else { bits &= !F::SIGN_MASK;
iy &= F::Int::MAX >> F::EXP_BITS; // Subtract 1 from the exponent, clamping at 0
iy |= one << F::SIG_BITS; let sat = bits.checked_sub(F::IMPLICIT_BIT).unwrap_or(F::Int::ZERO);
(
bits - (sat & F::EXP_MASK),
u32::cast_from(sat >> F::SIG_BITS),
)
} }
/* x mod y */ /// Compute the remainder `(x * 2.pow(e)) % y` without overflow.
while ex > ey { fn reduction<I: Int>(mut x: I, e: u32, y: I) -> I {
let i = ix.wrapping_sub(iy); x %= y;
if i >> (F::BITS - 1) == zero { for _ in 0..e {
if i == zero { x <<= 1;
return F::ZERO * x; x = x.checked_sub(y).unwrap_or(x);
} }
ix = i; x
}
ix <<= 1;
ex -= 1;
}
let i = ix.wrapping_sub(iy);
if i >> (F::BITS - 1) == zero {
if i == zero {
return F::ZERO * x;
}
ix = i;
}
let shift = ix.leading_zeros().saturating_sub(F::EXP_BITS);
ix <<= shift;
ex -= shift as i32;
/* scale result */
if ex > 0 {
ix -= one << F::SIG_BITS;
ix |= F::Int::cast_from(ex) << F::SIG_BITS;
} else {
ix >>= -ex + 1;
}
ix |= sx;
F::from_bits(ix)
} }

View File

@@ -40,6 +40,9 @@ pub trait Int:
+ PartialOrd + PartialOrd
+ ops::AddAssign + ops::AddAssign
+ ops::SubAssign + ops::SubAssign
+ ops::MulAssign
+ ops::DivAssign
+ ops::RemAssign
+ ops::BitAndAssign + ops::BitAndAssign
+ ops::BitOrAssign + ops::BitOrAssign
+ ops::BitXorAssign + ops::BitXorAssign
@@ -51,6 +54,7 @@ pub trait Int:
+ ops::Sub<Output = Self> + ops::Sub<Output = Self>
+ ops::Mul<Output = Self> + ops::Mul<Output = Self>
+ ops::Div<Output = Self> + ops::Div<Output = Self>
+ ops::Rem<Output = Self>
+ ops::Shl<i32, Output = Self> + ops::Shl<i32, Output = Self>
+ ops::Shl<u32, Output = Self> + ops::Shl<u32, Output = Self>
+ ops::Shr<i32, Output = Self> + ops::Shr<i32, Output = Self>