[llvm] eca2529 - Use Log2_64_Ceil to compute PowerOf2Ceil (#67580)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 15 18:52:34 PST 2024


Author: AtariDreams
Date: 2024-01-15T18:52:29-08:00
New Revision: eca2529592b59fe2c4b2e06adf15900c7a2ca95f

URL: https://github.com/llvm/llvm-project/commit/eca2529592b59fe2c4b2e06adf15900c7a2ca95f
DIFF: https://github.com/llvm/llvm-project/commit/eca2529592b59fe2c4b2e06adf15900c7a2ca95f.diff

LOG: Use Log2_64_Ceil to compute PowerOf2Ceil (#67580)

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.

Added: 
    

Modified: 
    llvm/include/llvm/Support/MathExtras.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 7278c4eb7695887..aa4f4d2ed42e262 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -359,9 +359,9 @@ constexpr inline uint64_t NextPowerOf2(uint64_t A) {
 /// Returns the power of two which is greater than or equal to the given value.
 /// Essentially, it is a ceil operation across the domain of powers of two.
 inline uint64_t PowerOf2Ceil(uint64_t A) {
-  if (!A)
+  if (!A || A > UINT64_MAX / 2)
     return 0;
-  return NextPowerOf2(A - 1);
+  return UINT64_C(1) << Log2_64_Ceil(A);
 }
 
 /// Returns the next integer (mod 2**64) that is greater than or equal to


        


More information about the llvm-commits mailing list