Skip to content

MathExtras: Make some templates constexpr NFC #145522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions llvm/include/llvm/Support/MathExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
}

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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));
}

Expand Down
Loading