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

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 10:53:33 PDT 2023


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

>From d7d95ef3ded9ce31c7fb1e9b848143243cf5d8c1 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..f105005fcd53313 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) << countl_zero(A);
 }
 
 /// Returns the next integer (mod 2**64) that is greater than or equal to



More information about the llvm-commits mailing list