Skip to content
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

Multiply and divide by shift #1

Closed
wants to merge 1 commit into from

Conversation

SiwenFeng
Copy link

In general, multiply or divide by an integer can get the result by shifting it. The multiply and divide method - generate left-shifted code if multiply by 2n, and multiply by any other integer or divide by any number. The shift method is more efficient than the multiplication and division method.

In general, multiply or divide by an integer can get the result by shifting it. The multiply and divide method - generate left-shifted code if multiply by 2n, and multiply by any other integer or divide by any number. The shift method is more efficient than the multiplication and division method.
@gurushida
Copy link
Owner

One of the goal of this project is to be as easy to understand as possible. Even in the case of a compiler not smart enough to generate the fastest code anyway, replacing a "/4" by ">>2" makes the code less readable, so unless this happens in a performance critical code path (like the animation loop of a 3D rendering engine), which is not the case here, I prefer readability.

Besides, if for some reason you really need to optimize this code as much as you can, adding -O3 to the Makefile and looking at ways to do some operations like polynomial calculations without reallocating intermediate objects is likely to make a much bigger difference than a division.

@gurushida gurushida closed this Dec 16, 2019
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.

None yet

2 participants