-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
Use Log2_64_Ceil to compute PowerOf2Ceil #67580
Conversation
@llvm/pr-subscribers-llvm-support ChangesInstead of calling NextPowerOf2, we can use compiler intrinsics to do this faster. Full diff: https://github.com/llvm/llvm-project/pull/67580.diff 1 Files Affected:
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index dc095941fdc8a9f..f592beee5ccffc2 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -361,7 +361,11 @@ constexpr inline uint64_t NextPowerOf2(uint64_t A) {
inline uint64_t PowerOf2Ceil(uint64_t A) {
if (!A)
return 0;
+#if __has_builtin(__builtin_clzl)
+ return 1ULL << (64 - __builtin_clzl(A - 1));
+#else
return NextPowerOf2(A - 1);
+#endif
}
/// Returns the next integer (mod 2**64) that is greater than or equal to
|
b813c0b
to
0632bea
Compare
491619e
to
ae33fef
Compare
@AtariDreams I am not really familiar with the reasoning behind the current solution. I would probably ping the original author of the function? |
@dcci Thoughts on this? |
ddec74f
to
537f8af
Compare
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.
This is manual strength reduction. NextPowerOf2
is a complex operation whose original purpose isn't clear to me.
The only thing NextPowerOf2 has doing for it is that it is constexpr but also like it's used in non constexpr places too but that is something for another PR to address |
@MaskRay Can we please merge this? |
@MaskRay Any updates? |
a1685c7
to
681ad07
Compare
The current implemention of |
Thanks for catching this. Minor correction, when the argument is larger than 2**63, |
Fixed! |
✅ With the latest revision this PR passed the C/C++ code formatter. |
@MaskRay All done and taken care of! |
5866e0b
to
56e0973
Compare
525448b
to
d6ca186
Compare
Instead of calling NextPowerOf2, which is only useful for constants, we should call Log2_64_Ceil, which is faster because it uses compiler intrinsics where supported.
Instead of calling NextPowerOf2, which is only useful for constants, we should call Log2_64_Ceil, which is faster because it uses compiler intrinsics where supported.
Instead of calling NextPowerOf2, which is only useful for constants,
we should call Log2_64_Ceil, which is faster because it uses compiler
intrinsics where supported.