[llvm] fcd82f9 - MathExtras: Make some templates constexpr NFC (#145522)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 24 09:32:07 PDT 2025
Author: Haojian Wu
Date: 2025-06-24T18:32:03+02:00
New Revision: fcd82f9cf3b64d8959afed4569259163da3a5afd
URL: https://github.com/llvm/llvm-project/commit/fcd82f9cf3b64d8959afed4569259163da3a5afd
DIFF: https://github.com/llvm/llvm-project/commit/fcd82f9cf3b64d8959afed4569259163da3a5afd.diff
LOG: MathExtras: Make some templates constexpr NFC (#145522)
so that they can be evaluated at compiler time (we can see these values
in hover via clangd).
Added:
Modified:
llvm/include/llvm/Support/MathExtras.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index ae3150e5602ee..7bbf1b3aab761 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -79,7 +79,7 @@ constexpr float ef = 0x1.5bf0a8P+1F, // (2.71828183) https://oeis.org/A
/// Create a bitmask with the N right-most bits set to 1, and all other
/// bits set to 0. Only unsigned types are allowed.
-template <typename T> T maskTrailingOnes(unsigned N) {
+template <typename T> constexpr T maskTrailingOnes(unsigned N) {
static_assert(std::is_unsigned_v<T>, "Invalid type!");
const unsigned Bits = CHAR_BIT * sizeof(T);
assert(N <= Bits && "Invalid bit index");
@@ -90,19 +90,19 @@ template <typename T> T maskTrailingOnes(unsigned N) {
/// Create a bitmask with the N left-most bits set to 1, and all other
/// bits set to 0. Only unsigned types are allowed.
-template <typename T> T maskLeadingOnes(unsigned N) {
+template <typename T> constexpr T maskLeadingOnes(unsigned N) {
return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
}
/// Create a bitmask with the N right-most bits set to 0, and all other
/// bits set to 1. Only unsigned types are allowed.
-template <typename T> T maskTrailingZeros(unsigned N) {
+template <typename T> constexpr T maskTrailingZeros(unsigned N) {
return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
}
/// Create a bitmask with the N left-most bits set to 0, and all other
/// bits set to 1. Only unsigned types are allowed.
-template <typename T> T maskLeadingZeros(unsigned N) {
+template <typename T> constexpr T maskLeadingZeros(unsigned N) {
return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
}
@@ -120,7 +120,7 @@ static const unsigned char BitReverseTable256[256] = {
};
/// Reverse the bits in \p Val.
-template <typename T> T reverseBits(T Val) {
+template <typename T> constexpr T reverseBits(T Val) {
#if __has_builtin(__builtin_bitreverse8)
if constexpr (std::is_same_v<T, uint8_t>)
return __builtin_bitreverse8(Val);
@@ -217,7 +217,7 @@ constexpr bool isShiftedUInt(uint64_t x) {
}
/// Gets the maximum value for a N-bit unsigned integer.
-inline uint64_t maxUIntN(uint64_t N) {
+inline constexpr uint64_t maxUIntN(uint64_t N) {
assert(N <= 64 && "integer width out of range");
// uint64_t(1) << 64 is undefined behavior, so we can't do
@@ -233,7 +233,7 @@ inline uint64_t maxUIntN(uint64_t N) {
}
/// Gets the minimum value for a N-bit signed integer.
-inline int64_t minIntN(int64_t N) {
+inline constexpr int64_t minIntN(int64_t N) {
assert(N <= 64 && "integer width out of range");
if (N == 0)
@@ -242,7 +242,7 @@ inline int64_t minIntN(int64_t N) {
}
/// Gets the maximum value for a N-bit signed integer.
-inline int64_t maxIntN(int64_t N) {
+inline constexpr int64_t maxIntN(int64_t N) {
assert(N <= 64 && "integer width out of range");
// This relies on two's complement wraparound when N == 64, so we convert to
@@ -253,12 +253,12 @@ inline int64_t maxIntN(int64_t N) {
}
/// Checks if an unsigned integer fits into the given (dynamic) bit width.
-inline bool isUIntN(unsigned N, uint64_t x) {
+inline constexpr bool isUIntN(unsigned N, uint64_t x) {
return N >= 64 || x <= maxUIntN(N);
}
/// Checks if an signed integer fits into the given (dynamic) bit width.
-inline bool isIntN(unsigned N, int64_t x) {
+inline constexpr bool isIntN(unsigned N, int64_t x) {
return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
}
More information about the llvm-commits
mailing list