[llvm] [ADT] Use a constexpr version of llvm::bit_ceil (NFC) (PR #79709)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 27 14:17:06 PST 2024
================
@@ -311,30 +312,17 @@ class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIterator
}
};
-/// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
-/// power of two (which means N itself if N is already a power of two).
-template<unsigned N>
-struct RoundUpToPowerOfTwo;
-
-/// RoundUpToPowerOfTwoH - If N is not a power of two, increase it. This is a
-/// helper template used to implement RoundUpToPowerOfTwo.
-template<unsigned N, bool isPowerTwo>
-struct RoundUpToPowerOfTwoH {
- enum { Val = N };
-};
-template<unsigned N>
-struct RoundUpToPowerOfTwoH<N, false> {
- enum {
- // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets
- // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
- Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
- };
-};
-
-template<unsigned N>
-struct RoundUpToPowerOfTwo {
- enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
-};
+namespace detail {
+// A constexpr version of llvm::bit_ceil.
+// TODO: Replace this with std::bit_ceil once C++20 is available.
----------------
kazutakahirata wrote:
> Can llvm/ADT/bit.h bit_ceil be changed to constexpr and used here?
I don't think we can until C++20. `llvm::bit_ceil` eventually gets to `__builtin_clzll` or `_BitScanReverse64`, which are not `constexpr`. We wish we could detect whether the parameter is a constant so that we could have a separate code path for the constant case, but that would require `if consteval` in C++20. The only way to use `constexpr` in C++17 is to have an identical function body for both the constant and non-constant case, which we cannot afford to do.
https://github.com/llvm/llvm-project/pull/79709
More information about the llvm-commits
mailing list