Skip to content

Optimize divisions and remainders by constants. #5167

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 1 commit into
base: main
Choose a base branch
from

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented May 10, 2025

Following the techniques described in Hacker's Delight, Chapter 10.

Benchmarking suggested that is this generally worth it, except for non-powers of 2 for a) int operations on JS and b) long operations on Wasm.

For the long divisions on JS, this yields a 6x speedup, despite the fact that the resulting code needs 16 (!) primitive int multiplications.

@sjrd sjrd force-pushed the opt-divide-and-remainder branch 2 times, most recently from b0ca504 to 0ef8849 Compare June 2, 2025 15:34
@sjrd sjrd force-pushed the opt-divide-and-remainder branch from 0ef8849 to 560a48c Compare June 13, 2025 16:38
@sjrd sjrd force-pushed the opt-divide-and-remainder branch 3 times, most recently from eee96e7 to 864ebae Compare June 23, 2025 15:23
@sjrd sjrd force-pushed the opt-divide-and-remainder branch 4 times, most recently from 3eac5c3 to 615ec6a Compare July 15, 2025 15:24
@sjrd sjrd marked this pull request as ready for review July 15, 2025 15:25
@sjrd sjrd requested a review from gzm0 July 15, 2025 15:25
@sjrd
Copy link
Member Author

sjrd commented Jul 15, 2025

@gzm0 This PR is now ready for review. It sent me through many detours before I could finish it. :p

@sjrd sjrd force-pushed the opt-divide-and-remainder branch 4 times, most recently from 95a590f to be78b6a Compare July 15, 2025 16:43
Following the techniques described in Hacker's Delight, Chapter 10.

Benchmarking suggested that is this generally worth it, except for
non-powers of 2 for a) int operations on JS and b) long operations
on Wasm.

For the long divisions on JS, this yields a 6x speedup, despite the
fact that the resulting code needs 16 (!) primitive int
multiplications.
@sjrd sjrd force-pushed the opt-divide-and-remainder branch from be78b6a to 04771e4 Compare July 25, 2025 08:49
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