[llvm] 86d1f4c - [Support, ADT] Move llvm::endianness to bit.h (#68280)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 5 18:03:50 PDT 2023
Author: Kazu Hirata
Date: 2023-10-05T18:03:46-07:00
New Revision: 86d1f4c538e50e8f764e7700d167ffd1d8f70102
URL: https://github.com/llvm/llvm-project/commit/86d1f4c538e50e8f764e7700d167ffd1d8f70102
DIFF: https://github.com/llvm/llvm-project/commit/86d1f4c538e50e8f764e7700d167ffd1d8f70102.diff
LOG: [Support, ADT] Move llvm::endianness to bit.h (#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.
Added:
Modified:
llvm/include/llvm/ADT/bit.h
llvm/include/llvm/Support/Endian.h
llvm/include/llvm/Support/SwapByteOrder.h
Removed:
################################################################################
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
diff erent 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 a8a8f842318baec..808446e615458f6 100644
--- a/llvm/include/llvm/Support/Endian.h
+++ b/llvm/include/llvm/Support/Endian.h
@@ -13,6 +13,7 @@
#ifndef LLVM_SUPPORT_ENDIAN_H
#define LLVM_SUPPORT_ENDIAN_H
+#include "llvm/ADT/bit.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/SwapByteOrder.h"
#include <cassert>
@@ -22,13 +23,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