From: Yedidya Feldblum Date: Sat, 14 May 2016 19:49:52 +0000 (-0700) Subject: Prefer constexpr to preprocessor conditionals when checking endianness X-Git-Tag: 2016.07.26~232 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e788c8cab2385ecf1ef2c5a88623b89ea4f7152e;p=folly.git 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 --- diff --git a/folly/Bits.h b/folly/Bits.h index b474617e..e50355da 100644 --- a/folly/Bits.h +++ b/folly/Bits.h @@ -278,28 +278,17 @@ FB_GEN(uint16_t, our_bswap16) #undef FB_GEN -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - -template -struct EndianInt : public EndianIntBase { - public: - static T big(T x) { return EndianInt::swap(x); } - static T little(T x) { return x; } -}; - -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - template struct EndianInt : public EndianIntBase { public: - static T big(T x) { return x; } - static T little(T x) { return EndianInt::swap(x); } + static T big(T 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 // big* convert between native and big-endian representations @@ -327,14 +316,7 @@ class Endian { BIG }; - static constexpr Order order = -#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__ */ + static constexpr Order order = kIsLittleEndian ? Order::LITTLE : Order::BIG; template static T swap(T x) { return folly::detail::EndianInt::swap(x); diff --git a/folly/experimental/BitVectorCoding.h b/folly/experimental/BitVectorCoding.h index 738e820a..874a5d27 100644 --- a/folly/experimental/BitVectorCoding.h +++ b/folly/experimental/BitVectorCoding.h @@ -37,12 +37,10 @@ #error BitVectorCoding.h requires x86_64 #endif -#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ -#error BitVectorCoding.h requires little endianness -#endif - namespace folly { namespace compression { +static_assert(kIsLittleEndian, "BitVectorCoding.h requires little endianness"); + template struct BitVectorCompressedListBase { BitVectorCompressedListBase() = default; diff --git a/folly/experimental/EliasFanoCoding.h b/folly/experimental/EliasFanoCoding.h index 00a59fcb..80dbaee3 100644 --- a/folly/experimental/EliasFanoCoding.h +++ b/folly/experimental/EliasFanoCoding.h @@ -44,12 +44,10 @@ #error EliasFanoCoding.h requires x86_64 #endif -#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ -#error EliasFanoCoding.h requires little endianness -#endif - namespace folly { namespace compression { +static_assert(kIsLittleEndian, "EliasFanoCoding.h requires little endianness"); + template struct EliasFanoCompressedListBase { EliasFanoCompressedListBase() = default; diff --git a/folly/experimental/EventCount.h b/folly/experimental/EventCount.h index e0f5a94d..7c491542 100644 --- a/folly/experimental/EventCount.h +++ b/folly/experimental/EventCount.h @@ -126,13 +126,7 @@ class EventCount { static_assert(sizeof(uint32_t) == 4, "bad platform"); static_assert(sizeof(uint64_t) == 8, "bad platform"); -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - 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 + static constexpr size_t kEpochOffset = kIsLittleEndian ? 1 : 0; // val_ stores the epoch in the most significant 32 bits and the // waiter count in the least significant 32 bits. diff --git a/folly/experimental/symbolizer/Elf.cpp b/folly/experimental/symbolizer/Elf.cpp index 29a614b2..a61829a5 100644 --- a/folly/experimental/symbolizer/Elf.cpp +++ b/folly/experimental/symbolizer/Elf.cpp @@ -164,18 +164,12 @@ bool ElfFile::init(const char** msg) { #undef EXPECTED_CLASS // Validate ELF data encoding (LSB/MSB) -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -# define EXPECTED_ENCODING ELFDATA2LSB -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -# define EXPECTED_ENCODING ELFDATA2MSB -#else -# error Unsupported byte order -#endif - if (elfHeader.e_ident[EI_DATA] != EXPECTED_ENCODING) { + static constexpr auto kExpectedEncoding = + kIsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; + if (elfHeader.e_ident[EI_DATA] != kExpectedEncoding) { if (msg) *msg = "invalid ELF encoding"; return false; } -#undef EXPECTED_ENCODING // Validate ELF version (1) if (elfHeader.e_ident[EI_VERSION] != EV_CURRENT ||