Commit e788c8ca authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot 8

Prefer constexpr to preprocessor conditionals when checking endianness

Summary:
[Folly] Prefer `constexpr` to preprocessor conditionals when checking endianness.

Using `if (folly::kIsLittleEndian) {` v.s. `#if __BYTE_ORDER__ == __LITTLE_ENDIAN__`.

Reviewed By: meyering

Differential Revision: D3296770

fbshipit-source-id: b26df83fdd42a50663746fc7c9d5fbe67e6671eb
parent 0afc1272
...@@ -278,28 +278,17 @@ FB_GEN(uint16_t, our_bswap16) ...@@ -278,28 +278,17 @@ FB_GEN(uint16_t, our_bswap16)
#undef FB_GEN #undef FB_GEN
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
template <class T>
struct EndianInt : public EndianIntBase<T> {
public:
static T big(T x) { return EndianInt::swap(x); }
static T little(T x) { return x; }
};
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
template <class T> template <class T>
struct EndianInt : public EndianIntBase<T> { struct EndianInt : public EndianIntBase<T> {
public: public:
static T big(T x) { return x; } static T big(T x) {
static T little(T x) { return EndianInt::swap(x); } return kIsLittleEndian ? EndianInt::swap(x) : x;
}
static T little(T x) {
return kIsBigEndian ? EndianInt::swap(x) : x;
}
}; };
#else
# error Your machine uses a weird endianness!
#endif /* __BYTE_ORDER__ */
} // namespace detail } // namespace detail
// big* convert between native and big-endian representations // big* convert between native and big-endian representations
...@@ -327,14 +316,7 @@ class Endian { ...@@ -327,14 +316,7 @@ class Endian {
BIG BIG
}; };
static constexpr Order order = static constexpr Order order = kIsLittleEndian ? Order::LITTLE : Order::BIG;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Order::LITTLE;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
Order::BIG;
#else
# error Your machine uses a weird endianness!
#endif /* __BYTE_ORDER__ */
template <class T> static T swap(T x) { template <class T> static T swap(T x) {
return folly::detail::EndianInt<T>::swap(x); return folly::detail::EndianInt<T>::swap(x);
......
...@@ -37,12 +37,10 @@ ...@@ -37,12 +37,10 @@
#error BitVectorCoding.h requires x86_64 #error BitVectorCoding.h requires x86_64
#endif #endif
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error BitVectorCoding.h requires little endianness
#endif
namespace folly { namespace compression { namespace folly { namespace compression {
static_assert(kIsLittleEndian, "BitVectorCoding.h requires little endianness");
template <class Pointer> template <class Pointer>
struct BitVectorCompressedListBase { struct BitVectorCompressedListBase {
BitVectorCompressedListBase() = default; BitVectorCompressedListBase() = default;
......
...@@ -44,12 +44,10 @@ ...@@ -44,12 +44,10 @@
#error EliasFanoCoding.h requires x86_64 #error EliasFanoCoding.h requires x86_64
#endif #endif
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error EliasFanoCoding.h requires little endianness
#endif
namespace folly { namespace compression { namespace folly { namespace compression {
static_assert(kIsLittleEndian, "EliasFanoCoding.h requires little endianness");
template <class Pointer> template <class Pointer>
struct EliasFanoCompressedListBase { struct EliasFanoCompressedListBase {
EliasFanoCompressedListBase() = default; EliasFanoCompressedListBase() = default;
......
...@@ -126,13 +126,7 @@ class EventCount { ...@@ -126,13 +126,7 @@ class EventCount {
static_assert(sizeof(uint32_t) == 4, "bad platform"); static_assert(sizeof(uint32_t) == 4, "bad platform");
static_assert(sizeof(uint64_t) == 8, "bad platform"); static_assert(sizeof(uint64_t) == 8, "bad platform");
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ static constexpr size_t kEpochOffset = kIsLittleEndian ? 1 : 0;
static constexpr size_t kEpochOffset = 1;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
static constexpr size_t kEpochOffset = 0; // in units of sizeof(int)
#else
# error Your machine uses a weird endianness!
#endif
// val_ stores the epoch in the most significant 32 bits and the // val_ stores the epoch in the most significant 32 bits and the
// waiter count in the least significant 32 bits. // waiter count in the least significant 32 bits.
......
...@@ -164,18 +164,12 @@ bool ElfFile::init(const char** msg) { ...@@ -164,18 +164,12 @@ bool ElfFile::init(const char** msg) {
#undef EXPECTED_CLASS #undef EXPECTED_CLASS
// Validate ELF data encoding (LSB/MSB) // Validate ELF data encoding (LSB/MSB)
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ static constexpr auto kExpectedEncoding =
# define EXPECTED_ENCODING ELFDATA2LSB kIsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ if (elfHeader.e_ident[EI_DATA] != kExpectedEncoding) {
# define EXPECTED_ENCODING ELFDATA2MSB
#else
# error Unsupported byte order
#endif
if (elfHeader.e_ident[EI_DATA] != EXPECTED_ENCODING) {
if (msg) *msg = "invalid ELF encoding"; if (msg) *msg = "invalid ELF encoding";
return false; return false;
} }
#undef EXPECTED_ENCODING
// Validate ELF version (1) // Validate ELF version (1)
if (elfHeader.e_ident[EI_VERSION] != EV_CURRENT || if (elfHeader.e_ident[EI_VERSION] != EV_CURRENT ||
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment