[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.
This commit is contained in:
Reid Kleckner 2020-02-27 17:06:48 -08:00
parent 470db54cbd
commit e3a9b0f359
7 changed files with 41 additions and 55 deletions

View File

@ -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 <assert.h>
#include <stdint.h>

View File

@ -14,7 +14,6 @@
#define LLVM_SUPPORT_MATHEXTRAS_H
#include "llvm/Support/Compiler.h"
#include "llvm/Support/SwapByteOrder.h"
#include <algorithm>
#include <cassert>
#include <climits>
@ -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.
///

View File

@ -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;
}

View File

@ -102,7 +102,7 @@ bool convertUTF16ToUTF8String(ArrayRef<char> 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;
}

View File

@ -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 <cstring>
using namespace llvm;

View File

@ -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.

View File

@ -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.