[Support] Simplify isInt and isUInt with constexpr if (NFC)
Differential Revision: https://reviews.llvm.org/D132813
This commit is contained in:
parent
83ea47acd7
commit
ee40ef7aaf
|
|
@ -356,17 +356,16 @@ constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
|
||||||
|
|
||||||
/// Checks if an integer fits into the given bit width.
|
/// Checks if an integer fits into the given bit width.
|
||||||
template <unsigned N> constexpr inline bool isInt(int64_t x) {
|
template <unsigned N> constexpr inline bool isInt(int64_t x) {
|
||||||
return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
|
if constexpr (N == 8)
|
||||||
}
|
return static_cast<int8_t>(x) == x;
|
||||||
// Template specializations to get better code for common cases.
|
if constexpr (N == 16)
|
||||||
template <> constexpr inline bool isInt<8>(int64_t x) {
|
return static_cast<int16_t>(x) == x;
|
||||||
return static_cast<int8_t>(x) == x;
|
if constexpr (N == 32)
|
||||||
}
|
return static_cast<int32_t>(x) == x;
|
||||||
template <> constexpr inline bool isInt<16>(int64_t x) {
|
if constexpr (N < 64)
|
||||||
return static_cast<int16_t>(x) == x;
|
return -(INT64_C(1) << (N - 1)) <= x && x < (INT64_C(1) << (N - 1));
|
||||||
}
|
(void)x; // MSVC v19.25 warns that x is unused.
|
||||||
template <> constexpr inline bool isInt<32>(int64_t x) {
|
return true;
|
||||||
return static_cast<int32_t>(x) == x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if a signed integer is an N bit number shifted left by S.
|
/// Checks if a signed integer is an N bit number shifted left by S.
|
||||||
|
|
@ -379,34 +378,20 @@ constexpr inline bool isShiftedInt(int64_t x) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if an unsigned integer fits into the given bit width.
|
/// Checks if an unsigned integer fits into the given bit width.
|
||||||
///
|
template <unsigned N> constexpr inline bool isUInt(uint64_t x) {
|
||||||
/// This is written as two functions rather than as simply
|
|
||||||
///
|
|
||||||
/// return N >= 64 || X < (UINT64_C(1) << N);
|
|
||||||
///
|
|
||||||
/// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting
|
|
||||||
/// left too many places.
|
|
||||||
template <unsigned N>
|
|
||||||
constexpr inline std::enable_if_t<(N < 64), bool> isUInt(uint64_t X) {
|
|
||||||
static_assert(N > 0, "isUInt<0> doesn't make sense");
|
static_assert(N > 0, "isUInt<0> doesn't make sense");
|
||||||
return X < (UINT64_C(1) << (N));
|
if constexpr (N == 8)
|
||||||
}
|
return static_cast<uint8_t>(x) == x;
|
||||||
template <unsigned N>
|
if constexpr (N == 16)
|
||||||
constexpr inline std::enable_if_t<N >= 64, bool> isUInt(uint64_t) {
|
return static_cast<uint16_t>(x) == x;
|
||||||
|
if constexpr (N == 32)
|
||||||
|
return static_cast<uint32_t>(x) == x;
|
||||||
|
if constexpr (N < 64)
|
||||||
|
return x < (UINT64_C(1) << (N));
|
||||||
|
(void)x; // MSVC v19.25 warns that x is unused.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template specializations to get better code for common cases.
|
|
||||||
template <> constexpr inline bool isUInt<8>(uint64_t x) {
|
|
||||||
return static_cast<uint8_t>(x) == x;
|
|
||||||
}
|
|
||||||
template <> constexpr inline bool isUInt<16>(uint64_t x) {
|
|
||||||
return static_cast<uint16_t>(x) == x;
|
|
||||||
}
|
|
||||||
template <> constexpr inline bool isUInt<32>(uint64_t x) {
|
|
||||||
return static_cast<uint32_t>(x) == x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks if a unsigned integer is an N bit number shifted left by S.
|
/// Checks if a unsigned integer is an N bit number shifted left by S.
|
||||||
template <unsigned N, unsigned S>
|
template <unsigned N, unsigned S>
|
||||||
constexpr inline bool isShiftedUInt(uint64_t x) {
|
constexpr inline bool isShiftedUInt(uint64_t x) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue