Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

fmod: Correct the normalization of subnormals #535

Merged
merged 2 commits into from
Apr 16, 2025
Merged
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
26 changes: 24 additions & 2 deletions crates/libm-test/src/gen/case_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,33 @@ fn fminimum_numf128_cases() -> Vec<TestCase<op::fminimum_numf128::Routine>> {
}

fn fmod_cases() -> Vec<TestCase<op::fmod::Routine>> {
vec![]
let mut v = vec![];
TestCase::append_pairs(
&mut v,
&[
// Previous failure with incorrect loop iteration
// <https://github.com/rust-lang/libm/pull/469#discussion_r2022337272>
((2.1, 3.123e-320), Some(2.0696e-320)),
((2.1, 2.253547e-318), Some(1.772535e-318)),
],
);
v
}

fn fmodf_cases() -> Vec<TestCase<op::fmodf::Routine>> {
vec![]
let mut v = vec![];
TestCase::append_pairs(
&mut v,
&[
// Previous failure with incorrect loop iteration
// <https://github.com/rust-lang/libm/pull/469#discussion_r2022337272>
((2.1, 8.858e-42), Some(8.085e-42)),
((2.1, 6.39164e-40), Some(6.1636e-40)),
((5.5, 6.39164e-40), Some(4.77036e-40)),
((-151.189, 6.39164e-40), Some(-5.64734e-40)),
],
);
v
}

#[cfg(f128_enabled)]
Expand Down
4 changes: 2 additions & 2 deletions src/math/generic/fmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn fmod<F: Float>(x: F, y: F) -> F {

/* normalize x and y */
if ex == 0 {
let i = ix << F::EXP_BITS;
let i = ix << (F::EXP_BITS + 1);
ex -= i.leading_zeros() as i32;
ix <<= -ex + 1;
} else {
Expand All @@ -35,7 +35,7 @@ pub fn fmod<F: Float>(x: F, y: F) -> F {
}

if ey == 0 {
let i = iy << F::EXP_BITS;
let i = iy << (F::EXP_BITS + 1);
ey -= i.leading_zeros() as i32;
iy <<= -ey + 1;
} else {
Expand Down
Loading