@@ -79,7 +79,7 @@ constexpr float ef = 0x1.5bf0a8P+1F, // (2.71828183) https://oeis.org/A
79
79
80
80
// / Create a bitmask with the N right-most bits set to 1, and all other
81
81
// / 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) {
83
83
static_assert (std::is_unsigned_v<T>, " Invalid type!" );
84
84
const unsigned Bits = CHAR_BIT * sizeof (T);
85
85
assert (N <= Bits && " Invalid bit index" );
@@ -90,19 +90,19 @@ template <typename T> T maskTrailingOnes(unsigned N) {
90
90
91
91
// / Create a bitmask with the N left-most bits set to 1, and all other
92
92
// / 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) {
94
94
return ~maskTrailingOnes<T>(CHAR_BIT * sizeof (T) - N);
95
95
}
96
96
97
97
// / Create a bitmask with the N right-most bits set to 0, and all other
98
98
// / 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) {
100
100
return maskLeadingOnes<T>(CHAR_BIT * sizeof (T) - N);
101
101
}
102
102
103
103
// / Create a bitmask with the N left-most bits set to 0, and all other
104
104
// / 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) {
106
106
return maskTrailingOnes<T>(CHAR_BIT * sizeof (T) - N);
107
107
}
108
108
@@ -120,7 +120,7 @@ static const unsigned char BitReverseTable256[256] = {
120
120
};
121
121
122
122
// / Reverse the bits in \p Val.
123
- template <typename T> T reverseBits (T Val) {
123
+ template <typename T> constexpr T reverseBits (T Val) {
124
124
#if __has_builtin(__builtin_bitreverse8)
125
125
if constexpr (std::is_same_v<T, uint8_t >)
126
126
return __builtin_bitreverse8 (Val);
@@ -217,7 +217,7 @@ constexpr bool isShiftedUInt(uint64_t x) {
217
217
}
218
218
219
219
// / 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) {
221
221
assert (N <= 64 && " integer width out of range" );
222
222
223
223
// uint64_t(1) << 64 is undefined behavior, so we can't do
@@ -233,7 +233,7 @@ inline uint64_t maxUIntN(uint64_t N) {
233
233
}
234
234
235
235
// / 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) {
237
237
assert (N <= 64 && " integer width out of range" );
238
238
239
239
if (N == 0 )
@@ -242,7 +242,7 @@ inline int64_t minIntN(int64_t N) {
242
242
}
243
243
244
244
// / 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) {
246
246
assert (N <= 64 && " integer width out of range" );
247
247
248
248
// 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) {
253
253
}
254
254
255
255
// / 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) {
257
257
return N >= 64 || x <= maxUIntN (N);
258
258
}
259
259
260
260
// / 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) {
262
262
return N >= 64 || (minIntN (N) <= x && x <= maxIntN (N));
263
263
}
264
264
0 commit comments