Skip to content

Optimize fmod with a method using integer multiplication #1002

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

quaternic
Copy link
Contributor

@quaternic quaternic commented Aug 1, 2025

This is kind of a retry at #898. One of the problems there was that it would have added overhead and regressed performance for typical inputs.

Unlike that PR, this doesn't aim for sub-linear scaling; the cost of evaluating fmod(x, y) is still roughly proportional to log2(|x/y|). However, the constant factor is much better. Running the random-benchmarks locally, I got walltime reductions of

fmodf16:  -56.9%
fmodf:    -85.0%
fmod:     -95.4%
fmodf128: -98.7%

New utilities in libm::support:

  • trait NarrowingDiv for dividing u2N / uN when the quotient fits in uN
  • a reasonable implementation of that for up to u256 / u128
  • fn linear_mul_reduction<U>(x: U, mut e: u32, y: U) -> U computes (x << e) % y with the new method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant