Skip to content

Commit 86d1f4c

Browse files
[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.
1 parent 1cf7b8f commit 86d1f4c

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

llvm/include/llvm/ADT/bit.h

+35
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@
2727
#include <cstdlib> // for _byteswap_{ushort,ulong,uint64}
2828
#endif
2929

30+
#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \
31+
defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
32+
#include <endian.h>
33+
#elif defined(_AIX)
34+
#include <sys/machine.h>
35+
#elif defined(__sun)
36+
/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
37+
#include <sys/types.h>
38+
#define BIG_ENDIAN 4321
39+
#define LITTLE_ENDIAN 1234
40+
#if defined(_BIG_ENDIAN)
41+
#define BYTE_ORDER BIG_ENDIAN
42+
#else
43+
#define BYTE_ORDER LITTLE_ENDIAN
44+
#endif
45+
#elif defined(__MVS__)
46+
#define BIG_ENDIAN 4321
47+
#define LITTLE_ENDIAN 1234
48+
#define BYTE_ORDER BIG_ENDIAN
49+
#else
50+
#if !defined(BYTE_ORDER) && !defined(_WIN32)
51+
#include <machine/endian.h>
52+
#endif
53+
#endif
54+
3055
#ifdef _MSC_VER
3156
// Declare these intrinsics manually rather including intrin.h. It's very
3257
// expensive, and bit.h is popular via MathExtras.h.
@@ -41,6 +66,16 @@ unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
4166

4267
namespace llvm {
4368

69+
enum class endianness {
70+
big,
71+
little,
72+
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
73+
native = big
74+
#else
75+
native = little
76+
#endif
77+
};
78+
4479
// This implementation of bit_cast is different from the C++20 one in two ways:
4580
// - It isn't constexpr because that requires compiler support.
4681
// - It requires trivially-constructible To, to avoid UB in the implementation.

llvm/include/llvm/Support/Endian.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_SUPPORT_ENDIAN_H
1414
#define LLVM_SUPPORT_ENDIAN_H
1515

16+
#include "llvm/ADT/bit.h"
1617
#include "llvm/Support/Compiler.h"
1718
#include "llvm/Support/SwapByteOrder.h"
1819
#include <cassert>
@@ -22,13 +23,6 @@
2223
#include <type_traits>
2324

2425
namespace llvm {
25-
26-
enum class endianness {
27-
big,
28-
little,
29-
native = llvm::sys::IsBigEndianHost ? big : little
30-
};
31-
3226
namespace support {
3327

3428
// TODO: Remove the following once we are done migrating to llvm::endianness,

llvm/include/llvm/Support/SwapByteOrder.h

+2-30
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,12 @@
1919
#include <cstdint>
2020
#include <type_traits>
2121

22-
#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \
23-
defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
24-
#include <endian.h>
25-
#elif defined(_AIX)
26-
#include <sys/machine.h>
27-
#elif defined(__sun)
28-
/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
29-
#include <sys/types.h>
30-
#define BIG_ENDIAN 4321
31-
#define LITTLE_ENDIAN 1234
32-
#if defined(_BIG_ENDIAN)
33-
#define BYTE_ORDER BIG_ENDIAN
34-
#else
35-
#define BYTE_ORDER LITTLE_ENDIAN
36-
#endif
37-
#elif defined(__MVS__)
38-
#define BIG_ENDIAN 4321
39-
#define LITTLE_ENDIAN 1234
40-
#define BYTE_ORDER BIG_ENDIAN
41-
#else
42-
#if !defined(BYTE_ORDER) && !defined(_WIN32)
43-
#include <machine/endian.h>
44-
#endif
45-
#endif
46-
4722
namespace llvm {
4823

4924
namespace sys {
5025

51-
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
52-
constexpr bool IsBigEndianHost = true;
53-
#else
54-
constexpr bool IsBigEndianHost = false;
55-
#endif
26+
constexpr bool IsBigEndianHost =
27+
llvm::endianness::native == llvm::endianness::big;
5628

5729
static const bool IsLittleEndianHost = !IsBigEndianHost;
5830

0 commit comments

Comments
 (0)