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

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 11:52:25 PDT 2023


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

>From b813c0b56ac3f400de1d0e7a42c05ad5f115d73a 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 countl_zero to compute PowerOf2Ceil

Instead of calling NextPowerOf2, which is only useful for constexpr,
we should call countl_zero, which is faster because of compiler
intrinsics.
---
 llvm/include/llvm/Support/MathExtras.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index dc095941fdc8a9f..b5b3096ba91f7d7 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -361,7 +361,7 @@ constexpr inline uint64_t NextPowerOf2(uint64_t A) {
 inline uint64_t PowerOf2Ceil(uint64_t A) {
   if (!A)
     return 0;
-  return NextPowerOf2(A - 1);
+  return uint64_t(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