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

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 27 18:12:13 PDT 2023


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/67580

>From bee142ee41873df6e288ae8a2ff6be481ff6fe8d Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Wed, 27 Sep 2023 13:34:40 -0400
Subject: [PATCH] Use Log2_64_Ceil to compute PowerOf2Ceil

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.
---
 llvm/include/llvm/Support/MathExtras.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 7278c4eb7695887..60c4edddd69ad9f 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