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

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 10:43:56 PDT 2023


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

>From a4f3957acad6534109ad701aeca348e07b2d0273 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 compiler intrinsics to compute PowerOf2Ceil

Instead of calling NextPowerOf2, we can use compiler intrinsics to do this faster.
---
 llvm/include/llvm/Support/MathExtras.h | 4 ++++
 1 file changed, 4 insertions(+)

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



More information about the llvm-commits mailing list