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

roundup_power_of_two performance #10634

Closed
SiarheiVolkau opened this issue Dec 13, 2024 · 3 comments · Fixed by #10782
Closed

roundup_power_of_two performance #10634

SiarheiVolkau opened this issue Dec 13, 2024 · 3 comments · Fixed by #10782

Comments

@SiarheiVolkau
Copy link
Contributor

It seems that roundup_power_of_two is over-engineered.

Boiling down such thing leads to typical count leading zeros (clz) problem.
Modern CPUs will do it in a few cycles via special instruction(s),
we just need to use __builtin_clz for suitable compiler or rewrite it
in a way that allow compilers detect clz pattern in the code.

In current implementation no one compiler detects clz pattern thus it is just slow.

@shefty
Copy link
Member

shefty commented Feb 11, 2025

We need a solution that works across multiple compilers and operating systems. Is this call used anywhere in a fast path, where saving a few instructions matters?

@SiarheiVolkau
Copy link
Contributor Author

Why don't just do like
#if defined(__GNUC__)
__builtin_clzll approach
#elif defined(ANOTHER_COMPILER_WE_WANT_TO_SUPPORT_AND_WE_KNOW_HOW)
shortcut for that compiler
#else
current implementation of roundup_power_of_two
#endif

any objections?

@shefty
Copy link
Member

shefty commented Feb 11, 2025

No objections

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

Successfully merging a pull request may close this issue.

3 participants