This repository was archived by the owner on Apr 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Add fmodf16
#469
Merged
Merged
Add fmodf16
#469
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ fminf128 | |
fminf16 | ||
fmod | ||
fmodf | ||
fmodf16 | ||
frexp | ||
frexpf | ||
hypot | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,5 @@ | ||
/// Calculate the remainder of `x / y`, the precise result of `x - trunc(x / y) * y`. | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn fmod(x: f64, y: f64) -> f64 { | ||
let mut uxi = x.to_bits(); | ||
let mut uyi = y.to_bits(); | ||
let mut ex = ((uxi >> 52) & 0x7ff) as i64; | ||
let mut ey = ((uyi >> 52) & 0x7ff) as i64; | ||
let sx = uxi >> 63; | ||
let mut i; | ||
|
||
if uyi << 1 == 0 || y.is_nan() || ex == 0x7ff { | ||
return (x * y) / (x * y); | ||
} | ||
if uxi << 1 <= uyi << 1 { | ||
if uxi << 1 == uyi << 1 { | ||
return 0.0 * x; | ||
} | ||
return x; | ||
} | ||
|
||
/* normalize x and y */ | ||
if ex == 0 { | ||
i = uxi << 12; | ||
while i >> 63 == 0 { | ||
ex -= 1; | ||
i <<= 1; | ||
} | ||
uxi <<= -ex + 1; | ||
} else { | ||
uxi &= u64::MAX >> 12; | ||
uxi |= 1 << 52; | ||
} | ||
if ey == 0 { | ||
i = uyi << 12; | ||
while i >> 63 == 0 { | ||
ey -= 1; | ||
i <<= 1; | ||
} | ||
uyi <<= -ey + 1; | ||
} else { | ||
uyi &= u64::MAX >> 12; | ||
uyi |= 1 << 52; | ||
} | ||
|
||
/* x mod y */ | ||
while ex > ey { | ||
i = uxi.wrapping_sub(uyi); | ||
if i >> 63 == 0 { | ||
if i == 0 { | ||
return 0.0 * x; | ||
} | ||
uxi = i; | ||
} | ||
uxi <<= 1; | ||
ex -= 1; | ||
} | ||
i = uxi.wrapping_sub(uyi); | ||
if i >> 63 == 0 { | ||
if i == 0 { | ||
return 0.0 * x; | ||
} | ||
uxi = i; | ||
} | ||
while uxi >> 52 == 0 { | ||
uxi <<= 1; | ||
ex -= 1; | ||
} | ||
|
||
/* scale result */ | ||
if ex > 0 { | ||
uxi -= 1 << 52; | ||
uxi |= (ex as u64) << 52; | ||
} else { | ||
uxi >>= -ex + 1; | ||
} | ||
uxi |= sx << 63; | ||
|
||
f64::from_bits(uxi) | ||
super::generic::fmod(x, y) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,5 @@ | ||
use core::f32; | ||
|
||
/// Calculate the remainder of `x / y`, the precise result of `x - trunc(x / y) * y`. | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn fmodf(x: f32, y: f32) -> f32 { | ||
let mut uxi = x.to_bits(); | ||
let mut uyi = y.to_bits(); | ||
let mut ex = ((uxi >> 23) & 0xff) as i32; | ||
let mut ey = ((uyi >> 23) & 0xff) as i32; | ||
let sx = uxi & 0x80000000; | ||
let mut i; | ||
|
||
if uyi << 1 == 0 || y.is_nan() || ex == 0xff { | ||
return (x * y) / (x * y); | ||
} | ||
|
||
if uxi << 1 <= uyi << 1 { | ||
if uxi << 1 == uyi << 1 { | ||
return 0.0 * x; | ||
} | ||
|
||
return x; | ||
} | ||
|
||
/* normalize x and y */ | ||
if ex == 0 { | ||
i = uxi << 9; | ||
while i >> 31 == 0 { | ||
ex -= 1; | ||
i <<= 1; | ||
} | ||
|
||
uxi <<= -ex + 1; | ||
} else { | ||
uxi &= u32::MAX >> 9; | ||
uxi |= 1 << 23; | ||
} | ||
|
||
if ey == 0 { | ||
i = uyi << 9; | ||
while i >> 31 == 0 { | ||
ey -= 1; | ||
i <<= 1; | ||
} | ||
|
||
uyi <<= -ey + 1; | ||
} else { | ||
uyi &= u32::MAX >> 9; | ||
uyi |= 1 << 23; | ||
} | ||
|
||
/* x mod y */ | ||
while ex > ey { | ||
i = uxi.wrapping_sub(uyi); | ||
if i >> 31 == 0 { | ||
if i == 0 { | ||
return 0.0 * x; | ||
} | ||
uxi = i; | ||
} | ||
uxi <<= 1; | ||
|
||
ex -= 1; | ||
} | ||
|
||
i = uxi.wrapping_sub(uyi); | ||
if i >> 31 == 0 { | ||
if i == 0 { | ||
return 0.0 * x; | ||
} | ||
uxi = i; | ||
} | ||
|
||
while uxi >> 23 == 0 { | ||
uxi <<= 1; | ||
ex -= 1; | ||
} | ||
|
||
/* scale result up */ | ||
if ex > 0 { | ||
uxi -= 1 << 23; | ||
uxi |= (ex as u32) << 23; | ||
} else { | ||
uxi >>= -ex + 1; | ||
} | ||
uxi |= sx; | ||
|
||
f32::from_bits(uxi) | ||
super::generic::fmod(x, y) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// Calculate the remainder of `x / y`, the precise result of `x - trunc(x / y) * y`. | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn fmodf16(x: f16, y: f16) -> f16 { | ||
super::generic::fmod(x, y) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||
/* 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}; | ||||||
|
||||||
#[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 mut ix = x.to_bits(); | ||||||
let mut iy = y.to_bits(); | ||||||
let mut ex = x.exp().signed(); | ||||||
let mut ey = y.exp().signed(); | ||||||
let sx = ix & F::SIGN_MASK; | ||||||
|
||||||
if iy << 1 == zero || y.is_nan() || ex == F::EXP_MAX as i32 { | ||||||
return (x * y) / (x * y); | ||||||
} | ||||||
|
||||||
if ix << 1 <= iy << 1 { | ||||||
if ix << 1 == iy << 1 { | ||||||
return F::ZERO * x; | ||||||
} | ||||||
return x; | ||||||
} | ||||||
|
||||||
/* 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is something similar here. I believe the intent was also to have the
Suggested change
However in this branch, it doesn't make any difference. The only bit for which it can change the result is anyway or'ed back to 1 on the very next line. But it seems more clearly correct with the |
||||||
ix |= one << F::SIG_BITS; | ||||||
} | ||||||
|
||||||
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; | ||||||
} | ||||||
|
||||||
/* 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; | ||||||
} | ||||||
|
||||||
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) | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was mistranslated from the previous loop. It fails to account for the sign bit. I believe it should be
I discovered this because I translated this implementation to Wasm by hand for use in the Scala.js-to-Wasm backend:
scala-js/scala-js#5147
My test cases with subnormals failed with the formula as is, but do pass with the added
+ 1
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for finding this, I think you are correct. I'll make sure the failures get tested with the fix. I'll double check with all the cases from scala-js/scala-js#5147, but do you happen to know off the top of your head which ones failed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Among the tests I wrote, the following fail without the
+ 1
s.Tests are
test(expectedResult, x, y)
:For
Double
(binary64):For
Float
(binary32):Basically they're cases where
x
is a normal buty
is a subnormal. If they're both subnormals, the two errors "cancel out". And ifx
is a subnormal buty
is a normal, thenx < y
and so the earlier branch exits early before getting here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed info, opened #535 to look at this.