From e3a9b0f35955ab0fdcba3da713bb2f4cd0b29680 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 27 Feb 2020 17:06:48 -0800 Subject: [PATCH] [Support] Remove byte swapping from MathExtras.h MathExtras.h was just wrapping SwapByteOrder.h functionality, so have the callers use it directly. Use the MathExtras.h name (ByteSwap_NN) as the standard naming, since it appears to be the most popular. --- lldb/include/lldb/Core/Opcode.h | 2 +- llvm/include/llvm/Support/MathExtras.h | 16 -------- llvm/include/llvm/Support/SwapByteOrder.h | 55 ++++++++++++++-------------- llvm/lib/Support/ConvertUTFWrapper.cpp | 2 +- llvm/lib/Support/Triple.cpp | 1 + llvm/unittests/Support/MathExtrasTest.cpp | 10 ----- llvm/unittests/Support/SwapByteOrderTest.cpp | 10 +++++ 7 files changed, 41 insertions(+), 55 deletions(-) diff --git a/lldb/include/lldb/Core/Opcode.h b/lldb/include/lldb/Core/Opcode.h index f06ef38c..a812ae2 100644 --- a/lldb/include/lldb/Core/Opcode.h +++ b/lldb/include/lldb/Core/Opcode.h @@ -12,7 +12,7 @@ #include "lldb/Utility/Endian.h" #include "lldb/lldb-enumerations.h" -#include "llvm/Support/MathExtras.h" +#include "llvm/Support/SwapByteOrder.h" #include #include diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index 67a9691..f07d0b7 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -14,7 +14,6 @@ #define LLVM_SUPPORT_MATHEXTRAS_H #include "llvm/Support/Compiler.h" -#include "llvm/Support/SwapByteOrder.h" #include #include #include @@ -470,21 +469,6 @@ constexpr inline bool isPowerOf2_64(uint64_t Value) { return Value && !(Value & (Value - 1)); } -/// Return a byte-swapped representation of the 16-bit argument. -inline uint16_t ByteSwap_16(uint16_t Value) { - return sys::SwapByteOrder_16(Value); -} - -/// Return a byte-swapped representation of the 32-bit argument. -inline uint32_t ByteSwap_32(uint32_t Value) { - return sys::SwapByteOrder_32(Value); -} - -/// Return a byte-swapped representation of the 64-bit argument. -inline uint64_t ByteSwap_64(uint64_t Value) { - return sys::SwapByteOrder_64(Value); -} - /// Count the number of ones from the most significant bit to the first /// zero bit. /// diff --git a/llvm/include/llvm/Support/SwapByteOrder.h b/llvm/include/llvm/Support/SwapByteOrder.h index a8bfc39..a9f4373 100644 --- a/llvm/include/llvm/Support/SwapByteOrder.h +++ b/llvm/include/llvm/Support/SwapByteOrder.h @@ -42,19 +42,10 @@ #endif namespace llvm { -namespace sys { - -#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN -constexpr bool IsBigEndianHost = true; -#else -constexpr bool IsBigEndianHost = false; -#endif -static const bool IsLittleEndianHost = !IsBigEndianHost; - -/// SwapByteOrder_16 - This function returns a byte-swapped representation of +/// ByteSwap_16 - This function returns a byte-swapped representation of /// the 16-bit argument. -inline uint16_t SwapByteOrder_16(uint16_t value) { +inline uint16_t ByteSwap_16(uint16_t value) { #if defined(_MSC_VER) && !defined(_DEBUG) // The DLL version of the runtime lacks these functions (bug!?), but in a // release build they're replaced with BSWAP instructions anyway. @@ -67,7 +58,7 @@ inline uint16_t SwapByteOrder_16(uint16_t value) { } /// This function returns a byte-swapped representation of the 32-bit argument. -inline uint32_t SwapByteOrder_32(uint32_t value) { +inline uint32_t ByteSwap_32(uint32_t value) { #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC)) return __builtin_bswap32(value); #elif defined(_MSC_VER) && !defined(_DEBUG) @@ -82,44 +73,54 @@ inline uint32_t SwapByteOrder_32(uint32_t value) { } /// This function returns a byte-swapped representation of the 64-bit argument. -inline uint64_t SwapByteOrder_64(uint64_t value) { +inline uint64_t ByteSwap_64(uint64_t value) { #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC)) return __builtin_bswap64(value); #elif defined(_MSC_VER) && !defined(_DEBUG) return _byteswap_uint64(value); #else - uint64_t Hi = SwapByteOrder_32(uint32_t(value)); - uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32)); + uint64_t Hi = ByteSwap_32(uint32_t(value)); + uint32_t Lo = ByteSwap_32(uint32_t(value >> 32)); return (Hi << 32) | Lo; #endif } +namespace sys { + +#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN +constexpr bool IsBigEndianHost = true; +#else +constexpr bool IsBigEndianHost = false; +#endif + +static const bool IsLittleEndianHost = !IsBigEndianHost; + inline unsigned char getSwappedBytes(unsigned char C) { return C; } inline signed char getSwappedBytes(signed char C) { return C; } inline char getSwappedBytes(char C) { return C; } -inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); } -inline signed short getSwappedBytes( signed short C) { return SwapByteOrder_16(C); } +inline unsigned short getSwappedBytes(unsigned short C) { return ByteSwap_16(C); } +inline signed short getSwappedBytes( signed short C) { return ByteSwap_16(C); } -inline unsigned int getSwappedBytes(unsigned int C) { return SwapByteOrder_32(C); } -inline signed int getSwappedBytes( signed int C) { return SwapByteOrder_32(C); } +inline unsigned int getSwappedBytes(unsigned int C) { return ByteSwap_32(C); } +inline signed int getSwappedBytes( signed int C) { return ByteSwap_32(C); } inline unsigned long getSwappedBytes(unsigned long C) { // Handle LLP64 and LP64 platforms. - return sizeof(long) == sizeof(int) ? SwapByteOrder_32((uint32_t)C) - : SwapByteOrder_64((uint64_t)C); + return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C) + : ByteSwap_64((uint64_t)C); } inline signed long getSwappedBytes(signed long C) { // Handle LLP64 and LP64 platforms. - return sizeof(long) == sizeof(int) ? SwapByteOrder_32((uint32_t)C) - : SwapByteOrder_64((uint64_t)C); + return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C) + : ByteSwap_64((uint64_t)C); } inline unsigned long long getSwappedBytes(unsigned long long C) { - return SwapByteOrder_64(C); + return ByteSwap_64(C); } inline signed long long getSwappedBytes(signed long long C) { - return SwapByteOrder_64(C); + return ByteSwap_64(C); } inline float getSwappedBytes(float C) { @@ -128,7 +129,7 @@ inline float getSwappedBytes(float C) { float f; } in, out; in.f = C; - out.i = SwapByteOrder_32(in.i); + out.i = ByteSwap_32(in.i); return out.f; } @@ -138,7 +139,7 @@ inline double getSwappedBytes(double C) { double d; } in, out; in.d = C; - out.i = SwapByteOrder_64(in.i); + out.i = ByteSwap_64(in.i); return out.d; } diff --git a/llvm/lib/Support/ConvertUTFWrapper.cpp b/llvm/lib/Support/ConvertUTFWrapper.cpp index eb4ead6..6ec5678 100644 --- a/llvm/lib/Support/ConvertUTFWrapper.cpp +++ b/llvm/lib/Support/ConvertUTFWrapper.cpp @@ -102,7 +102,7 @@ bool convertUTF16ToUTF8String(ArrayRef SrcBytes, std::string &Out) { if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) { ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd); for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I) - ByteSwapped[I] = llvm::sys::SwapByteOrder_16(ByteSwapped[I]); + ByteSwapped[I] = llvm::ByteSwap_16(ByteSwapped[I]); Src = &ByteSwapped[0]; SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1; } diff --git a/llvm/lib/Support/Triple.cpp b/llvm/lib/Support/Triple.cpp index 2c480c1..e09abd2 100644 --- a/llvm/lib/Support/Triple.cpp +++ b/llvm/lib/Support/Triple.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Host.h" +#include "llvm/Support/SwapByteOrder.h" #include "llvm/Support/TargetParser.h" #include using namespace llvm; diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp index e910d83..d899af9 100644 --- a/llvm/unittests/Support/MathExtrasTest.cpp +++ b/llvm/unittests/Support/MathExtrasTest.cpp @@ -222,16 +222,6 @@ TEST(MathExtras, CTLog2) { EXPECT_EQ(CTLog2<1ULL << 15>(), 15U); } -TEST(MathExtras, ByteSwap_32) { - EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344)); - EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD)); -} - -TEST(MathExtras, ByteSwap_64) { - EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL)); - EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL)); -} - TEST(MathExtras, countLeadingOnes) { for (int i = 30; i >= 0; --i) { // Start with all ones and unset some bit. diff --git a/llvm/unittests/Support/SwapByteOrderTest.cpp b/llvm/unittests/Support/SwapByteOrderTest.cpp index 9581f07..85392fa 100644 --- a/llvm/unittests/Support/SwapByteOrderTest.cpp +++ b/llvm/unittests/Support/SwapByteOrderTest.cpp @@ -16,6 +16,16 @@ using namespace llvm; namespace { +TEST(ByteSwap, Swap_32) { + EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344)); + EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD)); +} + +TEST(ByteSwap, Swap_64) { + EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL)); + EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL)); +} + // In these first two tests all of the original_uintx values are truncated // except for 64. We could avoid this, but there's really no point. -- 2.7.4