[llvm] 8ab77a7 - [Support] Use llvm::byteswap in SwapByteOrder.h (NFC)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 22 19:14:39 PST 2023


Author: Kazu Hirata
Date: 2023-01-22T19:14:33-08:00
New Revision: 8ab77a78ba059c0f5f357d7b1062b25b5ab0cb61

URL: https://github.com/llvm/llvm-project/commit/8ab77a78ba059c0f5f357d7b1062b25b5ab0cb61
DIFF: https://github.com/llvm/llvm-project/commit/8ab77a78ba059c0f5f357d7b1062b25b5ab0cb61.diff

LOG: [Support] Use llvm::byteswap in SwapByteOrder.h (NFC)

This patch defines ByteSwap_{32,64} and getSwappedBytes with
llvm::byteswap.

It's tempting to define something like:

  template <typename T,
            typename = std::enable_if_t<std::is_integral_v<T>>>
  inline T getSwappedBytes(T C) { return llvm::byteswap(C); }

But this doesn't work.  The host compiler would issue:

  error: call to 'getSwappedBytes' is ambiguous

while compiling lldb/source/Utility/UUID.cpp.

Added: 
    

Modified: 
    llvm/include/llvm/Support/SwapByteOrder.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/SwapByteOrder.h b/llvm/include/llvm/Support/SwapByteOrder.h
index e8612ba6654b5..9dd08665bd23e 100644
--- a/llvm/include/llvm/Support/SwapByteOrder.h
+++ b/llvm/include/llvm/Support/SwapByteOrder.h
@@ -14,12 +14,10 @@
 #ifndef LLVM_SUPPORT_SWAPBYTEORDER_H
 #define LLVM_SUPPORT_SWAPBYTEORDER_H
 
+#include "llvm/ADT/bit.h"
 #include <cstddef>
 #include <cstdint>
 #include <type_traits>
-#if defined(_MSC_VER) && !defined(_DEBUG)
-#include <stdlib.h>
-#endif
 
 #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) ||            \
     defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
@@ -50,45 +48,13 @@ namespace llvm {
 
 /// ByteSwap_16 - This function returns a byte-swapped representation of
 /// the 16-bit argument.
-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.
-  return _byteswap_ushort(value);
-#else
-  uint16_t Hi = value << 8;
-  uint16_t Lo = value >> 8;
-  return Hi | Lo;
-#endif
-}
+inline uint16_t ByteSwap_16(uint16_t value) { return llvm::byteswap(value); }
 
 /// This function returns a byte-swapped representation of the 32-bit argument.
-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)
-  return _byteswap_ulong(value);
-#else
-  uint32_t Byte0 = value & 0x000000FF;
-  uint32_t Byte1 = value & 0x0000FF00;
-  uint32_t Byte2 = value & 0x00FF0000;
-  uint32_t Byte3 = value & 0xFF000000;
-  return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
-#endif
-}
+inline uint32_t ByteSwap_32(uint32_t value) { return llvm::byteswap(value); }
 
 /// This function returns a byte-swapped representation of the 64-bit argument.
-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 = ByteSwap_32(uint32_t(value));
-  uint32_t Lo = ByteSwap_32(uint32_t(value >> 32));
-  return (Hi << 32) | Lo;
-#endif
-}
+inline uint64_t ByteSwap_64(uint64_t value) { return llvm::byteswap(value); }
 
 namespace sys {
 
@@ -100,33 +66,21 @@ constexpr bool IsBigEndianHost = false;
 
 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 char      getSwappedBytes(unsigned char      C) { return llvm::byteswap(C); }
+inline   signed char      getSwappedBytes( signed  char      C) { return llvm::byteswap(C); }
+inline          char      getSwappedBytes(         char      C) { return llvm::byteswap(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 short     getSwappedBytes(unsigned short     C) { return llvm::byteswap(C); }
+inline   signed short     getSwappedBytes(  signed short     C) { return llvm::byteswap(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 int       getSwappedBytes(unsigned int       C) { return llvm::byteswap(C); }
+inline   signed int       getSwappedBytes(  signed int       C) { return llvm::byteswap(C); }
 
-inline unsigned long getSwappedBytes(unsigned long C) {
-  // Handle LLP64 and LP64 platforms.
-  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) ? ByteSwap_32((uint32_t)C)
-                                     : ByteSwap_64((uint64_t)C);
-}
+inline unsigned long      getSwappedBytes(unsigned long      C) { return llvm::byteswap(C); }
+inline   signed long      getSwappedBytes(  signed long      C) { return llvm::byteswap(C); }
 
-inline unsigned long long getSwappedBytes(unsigned long long C) {
-  return ByteSwap_64(C);
-}
-inline signed long long getSwappedBytes(signed long long C) {
-  return ByteSwap_64(C);
-}
+inline unsigned long long getSwappedBytes(unsigned long long C) { return llvm::byteswap(C); }
+inline   signed long long getSwappedBytes(  signed long long C) { return llvm::byteswap(C); }
 
 inline float getSwappedBytes(float C) {
   union {
@@ -134,7 +88,7 @@ inline float getSwappedBytes(float C) {
     float f;
   } in, out;
   in.f = C;
-  out.i = ByteSwap_32(in.i);
+  out.i = llvm::byteswap(in.i);
   return out.f;
 }
 
@@ -144,14 +98,14 @@ inline double getSwappedBytes(double C) {
     double d;
   } in, out;
   in.d = C;
-  out.i = ByteSwap_64(in.i);
+  out.i = llvm::byteswap(in.i);
   return out.d;
 }
 
 template <typename T>
 inline std::enable_if_t<std::is_enum<T>::value, T> getSwappedBytes(T C) {
   return static_cast<T>(
-      getSwappedBytes(static_cast<std::underlying_type_t<T>>(C)));
+      llvm::byteswap(static_cast<std::underlying_type_t<T>>(C)));
 }
 
 template<typename T>


        


More information about the llvm-commits mailing list