-
Notifications
You must be signed in to change notification settings - Fork 103
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
Conversation
Thanks, clippy, very helpful |
Awesome! The cleanup is great too. I'll take a deeper look soon. Cc @sjrd since you tracked this implementation. |
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.
/// 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 } |
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.
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 } |
if num.is_zero() { | ||
F::from_bits(sx) | ||
} else { |
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.
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.
/* SPDX-License-Identifier: MIT */ | ||
/* origin: musl src/math/fmod.c. Ported to generic Rust algorithm in 2025, TG. */ |
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.
If this is your own work then could you add /* SPDX-License-Identifier: MIT OR Apache-2.0 */
, or leave MIT if it's still pretty derivative? I'm trying to get a bit better track of origins since licensing is pretty mixed.
Also feel free to add something like /* Authored in 2025 by quaternic */
. We need to carry around copyrights from Sun in the 90s, so IMO we can afford to give some credit to anyone who authors an original implementation :)
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) |
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.
Would you mind adding a comment about the algorithm here?
if sig == bits { sig } else { sig | F::IMPLICIT_BIT } | ||
} | ||
|
||
/// Computes (x << e) % y |
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.
/// 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
#![allow(clippy::many_single_char_names)] | ||
#![allow(clippy::just_underscores_and_digits)] |
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.
Sorting nit
#![allow(clippy::many_single_char_names)] | |
#![allow(clippy::just_underscores_and_digits)] | |
#![allow(clippy::just_underscores_and_digits)] | |
#![allow(clippy::many_single_char_names)] |
This repo has been merged into https://github.com/rust-lang/compiler-builtins and is getting archived, so I am going to close this PR; please do reopen it there! I think this should apply cleanly with |
Full reimplementation for fmod, that should be somewhat cleaner. This was showing a decent perf gain locally, but the major improvement will come later by implementing the
reduction
helper with something smarter, which does involve some tradeoffs and alternatives to consider.