[PATCH] D132814: [Support] Simplify reverseBits with constexpr if (NFC)
Kazu Hirata via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 28 12:13:46 PDT 2022
kazu created this revision.
Herald added a project: All.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D132814
Files:
llvm/include/llvm/Support/MathExtras.h
Index: llvm/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/include/llvm/Support/MathExtras.h
+++ llvm/include/llvm/Support/MathExtras.h
@@ -298,45 +298,33 @@
};
/// 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132814.456208.patch
Type: text/x-patch
Size: 2066 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220828/f846be10/attachment.bin>
More information about the llvm-commits
mailing list