[llvm] [Support, ADT] Move llvm::endianness to bit.h (PR #68280)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 4 21:00:39 PDT 2023


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/68280

In C++20, std::endian comes from <bit>.  Following the same spirit,
this patch moves llvm::endianness and the endian detection logic to
bit.h.

Without this patch, llvm::endianness::native is defined in terms of
llvm::sys::IsBigEndianHost.  This patch reverses the dependency.
llvm::sys::IsBigEndianHost is now defined in terms of
llvm::endianness::native.


>From f04ca41e0135669772b0608029982969c9f8b8a9 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 25 Aug 2023 10:49:33 -0700
Subject: [PATCH] [Support, ADT] Move llvm::endianness to bit.h

In C++20, std::endian comes from <bit>.  Following the same spirit,
this patch moves llvm::endianness and the endian detection logic to
bit.h.

Without this patch, llvm::endianness::native is defined in terms of
llvm::sys::IsBigEndianHost.  This patch reverses the dependency.
llvm::sys::IsBigEndianHost is now defined in terms of
llvm::endianness::native.
---
 llvm/include/llvm/ADT/bit.h               | 35 +++++++++++++++++++++++
 llvm/include/llvm/Support/Endian.h        |  7 -----
 llvm/include/llvm/Support/SwapByteOrder.h | 32 ++-------------------
 3 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/llvm/include/llvm/ADT/bit.h b/llvm/include/llvm/ADT/bit.h
index 2840c5f608d3ea8..0a228b2204d6fc2 100644
--- a/llvm/include/llvm/ADT/bit.h
+++ b/llvm/include/llvm/ADT/bit.h
@@ -27,6 +27,31 @@
 #include <cstdlib>  // for _byteswap_{ushort,ulong,uint64}
 #endif
 
+#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) ||            \
+    defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
+#include <endian.h>
+#elif defined(_AIX)
+#include <sys/machine.h>
+#elif defined(__sun)
+/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
+#include <sys/types.h>
+#define BIG_ENDIAN 4321
+#define LITTLE_ENDIAN 1234
+#if defined(_BIG_ENDIAN)
+#define BYTE_ORDER BIG_ENDIAN
+#else
+#define BYTE_ORDER LITTLE_ENDIAN
+#endif
+#elif defined(__MVS__)
+#define BIG_ENDIAN 4321
+#define LITTLE_ENDIAN 1234
+#define BYTE_ORDER BIG_ENDIAN
+#else
+#if !defined(BYTE_ORDER) && !defined(_WIN32)
+#include <machine/endian.h>
+#endif
+#endif
+
 #ifdef _MSC_VER
 // Declare these intrinsics manually rather including intrin.h. It's very
 // expensive, and bit.h is popular via MathExtras.h.
@@ -41,6 +66,16 @@ unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
 
 namespace llvm {
 
+enum class endianness {
+  big,
+  little,
+#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
+  native = big
+#else
+  native = little
+#endif
+};
+
 // This implementation of bit_cast is different from the C++20 one in two ways:
 //  - It isn't constexpr because that requires compiler support.
 //  - It requires trivially-constructible To, to avoid UB in the implementation.
diff --git a/llvm/include/llvm/Support/Endian.h b/llvm/include/llvm/Support/Endian.h
index e0afeabbcf34a0e..28a1d2da9bb5b15 100644
--- a/llvm/include/llvm/Support/Endian.h
+++ b/llvm/include/llvm/Support/Endian.h
@@ -22,13 +22,6 @@
 #include <type_traits>
 
 namespace llvm {
-
-enum class endianness {
-  big,
-  little,
-  native = llvm::sys::IsBigEndianHost ? big : little
-};
-
 namespace support {
 
 // TODO: Remove the following once we are done migrating to llvm::endianness,
diff --git a/llvm/include/llvm/Support/SwapByteOrder.h b/llvm/include/llvm/Support/SwapByteOrder.h
index 43fa5347d6fe851..8f26af6f68ac65f 100644
--- a/llvm/include/llvm/Support/SwapByteOrder.h
+++ b/llvm/include/llvm/Support/SwapByteOrder.h
@@ -19,40 +19,12 @@
 #include <cstdint>
 #include <type_traits>
 
-#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) ||            \
-    defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
-#include <endian.h>
-#elif defined(_AIX)
-#include <sys/machine.h>
-#elif defined(__sun)
-/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
-#include <sys/types.h>
-#define BIG_ENDIAN 4321
-#define LITTLE_ENDIAN 1234
-#if defined(_BIG_ENDIAN)
-#define BYTE_ORDER BIG_ENDIAN
-#else
-#define BYTE_ORDER LITTLE_ENDIAN
-#endif
-#elif defined(__MVS__)
-#define BIG_ENDIAN 4321
-#define LITTLE_ENDIAN 1234
-#define BYTE_ORDER BIG_ENDIAN
-#else
-#if !defined(BYTE_ORDER) && !defined(_WIN32)
-#include <machine/endian.h>
-#endif
-#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
+constexpr bool IsBigEndianHost =
+    llvm::endianness::native == llvm::endianness::big;
 
 static const bool IsLittleEndianHost = !IsBigEndianHost;
 



More information about the llvm-commits mailing list