Skip to content

Commit fcd82f9

Browse files
authored
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).
1 parent c8a9579 commit fcd82f9

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

llvm/include/llvm/Support/MathExtras.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ constexpr float ef = 0x1.5bf0a8P+1F, // (2.71828183) https://oeis.org/A
7979

8080
/// Create a bitmask with the N right-most bits set to 1, and all other
8181
/// bits set to 0. Only unsigned types are allowed.
82-
template <typename T> T maskTrailingOnes(unsigned N) {
82+
template <typename T> constexpr T maskTrailingOnes(unsigned N) {
8383
static_assert(std::is_unsigned_v<T>, "Invalid type!");
8484
const unsigned Bits = CHAR_BIT * sizeof(T);
8585
assert(N <= Bits && "Invalid bit index");
@@ -90,19 +90,19 @@ template <typename T> T maskTrailingOnes(unsigned N) {
9090

9191
/// Create a bitmask with the N left-most bits set to 1, and all other
9292
/// bits set to 0. Only unsigned types are allowed.
93-
template <typename T> T maskLeadingOnes(unsigned N) {
93+
template <typename T> constexpr T maskLeadingOnes(unsigned N) {
9494
return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
9595
}
9696

9797
/// Create a bitmask with the N right-most bits set to 0, and all other
9898
/// bits set to 1. Only unsigned types are allowed.
99-
template <typename T> T maskTrailingZeros(unsigned N) {
99+
template <typename T> constexpr T maskTrailingZeros(unsigned N) {
100100
return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
101101
}
102102

103103
/// Create a bitmask with the N left-most bits set to 0, and all other
104104
/// bits set to 1. Only unsigned types are allowed.
105-
template <typename T> T maskLeadingZeros(unsigned N) {
105+
template <typename T> constexpr T maskLeadingZeros(unsigned N) {
106106
return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
107107
}
108108

@@ -120,7 +120,7 @@ static const unsigned char BitReverseTable256[256] = {
120120
};
121121

122122
/// Reverse the bits in \p Val.
123-
template <typename T> T reverseBits(T Val) {
123+
template <typename T> constexpr T reverseBits(T Val) {
124124
#if __has_builtin(__builtin_bitreverse8)
125125
if constexpr (std::is_same_v<T, uint8_t>)
126126
return __builtin_bitreverse8(Val);
@@ -217,7 +217,7 @@ constexpr bool isShiftedUInt(uint64_t x) {
217217
}
218218

219219
/// Gets the maximum value for a N-bit unsigned integer.
220-
inline uint64_t maxUIntN(uint64_t N) {
220+
inline constexpr uint64_t maxUIntN(uint64_t N) {
221221
assert(N <= 64 && "integer width out of range");
222222

223223
// uint64_t(1) << 64 is undefined behavior, so we can't do
@@ -233,7 +233,7 @@ inline uint64_t maxUIntN(uint64_t N) {
233233
}
234234

235235
/// Gets the minimum value for a N-bit signed integer.
236-
inline int64_t minIntN(int64_t N) {
236+
inline constexpr int64_t minIntN(int64_t N) {
237237
assert(N <= 64 && "integer width out of range");
238238

239239
if (N == 0)
@@ -242,7 +242,7 @@ inline int64_t minIntN(int64_t N) {
242242
}
243243

244244
/// Gets the maximum value for a N-bit signed integer.
245-
inline int64_t maxIntN(int64_t N) {
245+
inline constexpr int64_t maxIntN(int64_t N) {
246246
assert(N <= 64 && "integer width out of range");
247247

248248
// 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) {
253253
}
254254

255255
/// Checks if an unsigned integer fits into the given (dynamic) bit width.
256-
inline bool isUIntN(unsigned N, uint64_t x) {
256+
inline constexpr bool isUIntN(unsigned N, uint64_t x) {
257257
return N >= 64 || x <= maxUIntN(N);
258258
}
259259

260260
/// Checks if an signed integer fits into the given (dynamic) bit width.
261-
inline bool isIntN(unsigned N, int64_t x) {
261+
inline constexpr bool isIntN(unsigned N, int64_t x) {
262262
return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
263263
}
264264

0 commit comments

Comments
 (0)