[llvm] 2423cf4 - [Support] Simplify reverseBits with constexpr if (NFC)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 3 23:27:48 PDT 2022
Author: Kazu Hirata
Date: 2022-09-03T23:27:15-07:00
New Revision: 2423cf4f88107b00166b105561fb63b2af26a5e9
URL: https://github.com/llvm/llvm-project/commit/2423cf4f88107b00166b105561fb63b2af26a5e9
DIFF: https://github.com/llvm/llvm-project/commit/2423cf4f88107b00166b105561fb63b2af26a5e9.diff
LOG: [Support] Simplify reverseBits with constexpr if (NFC)
Differential Revision: https://reviews.llvm.org/D132814
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 2b2c47ecc749..b72d483df186 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -296,45 +296,33 @@ static const unsigned char BitReverseTable256[256] = {
};
/// Reverse the bits in \p Val.
-template <typename T>
-T reverseBits(T Val) {
- unsigned char in[sizeof(Val)];
- unsigned char out[sizeof(Val)];
- std::memcpy(in, &Val, sizeof(Val));
- for (unsigned i = 0; i < sizeof(Val); ++i)
- out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
- std::memcpy(&Val, out, sizeof(Val));
- return Val;
-}
-
+template <typename T> T reverseBits(T Val) {
#if __has_builtin(__builtin_bitreverse8)
-template<>
-inline uint8_t reverseBits<uint8_t>(uint8_t Val) {
- return __builtin_bitreverse8(Val);
-}
+ if constexpr (std::is_same_v<T, uint8_t>)
+ return __builtin_bitreverse8(Val);
#endif
-
#if __has_builtin(__builtin_bitreverse16)
-template<>
-inline uint16_t reverseBits<uint16_t>(uint16_t Val) {
- return __builtin_bitreverse16(Val);
-}
+ if constexpr (std::is_same_v<T, uint16_t>)
+ return __builtin_bitreverse16(Val);
#endif
-
#if __has_builtin(__builtin_bitreverse32)
-template<>
-inline uint32_t reverseBits<uint32_t>(uint32_t Val) {
- return __builtin_bitreverse32(Val);
-}
+ if constexpr (std::is_same_v<T, uint32_t>)
+ return __builtin_bitreverse32(Val);
#endif
-
#if __has_builtin(__builtin_bitreverse64)
-template<>
-inline uint64_t reverseBits<uint64_t>(uint64_t Val) {
- return __builtin_bitreverse64(Val);
-}
+ if constexpr (std::is_same_v<T, uint64_t>)
+ return __builtin_bitreverse64(Val);
#endif
+ unsigned char in[sizeof(Val)];
+ unsigned char out[sizeof(Val)];
+ std::memcpy(in, &Val, sizeof(Val));
+ for (unsigned i = 0; i < sizeof(Val); ++i)
+ out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
+ std::memcpy(&Val, out, sizeof(Val));
+ return Val;
+}
+
// NOTE: The following support functions use the _32/_64 extensions instead of
// type overloading so that signed and unsigned integers can be used without
// ambiguity.
More information about the llvm-commits
mailing list