Prefer constexpr to preprocessor conditionals when checking endianness
authorYedidya Feldblum <yfeldblum@fb.com>
Sat, 14 May 2016 19:49:52 +0000 (12:49 -0700)
committerFacebook Github Bot 8 <facebook-github-bot-8-bot@fb.com>
Sat, 14 May 2016 19:53:25 +0000 (12:53 -0700)
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

folly/Bits.h
folly/experimental/BitVectorCoding.h
folly/experimental/EliasFanoCoding.h
folly/experimental/EventCount.h
folly/experimental/symbolizer/Elf.cpp

index b474617e6f94444193119620e0ef54a7272015c7..e50355da8c2dd20117e553fb948ff948cbb377cd 100644 (file)
@@ -278,28 +278,17 @@ FB_GEN(uint16_t, our_bswap16)
 
 #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>
 struct EndianInt : public EndianIntBase<T> {
  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 <class T> static T swap(T x) {
     return folly::detail::EndianInt<T>::swap(x);
index 738e820abd7c3123357c51634013b3fea9d3cc2b..874a5d271d09338417f880ff1b7b62f6b684f69b 100644 (file)
 #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 <class Pointer>
 struct BitVectorCompressedListBase {
   BitVectorCompressedListBase() = default;
index 00a59fcba4a8b29d714a4e5404a2a8c1f7e40d7c..80dbaee346c1f8456a8e4ac1eb33dbd0c6cb20c4 100644 (file)
 #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 <class Pointer>
 struct EliasFanoCompressedListBase {
   EliasFanoCompressedListBase() = default;
index e0f5a94d6975988ce885c264f0632a69e0d8062e..7c491542013f0b4e474456384d9a558193af859c 100644 (file)
@@ -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.
index 29a614b2908218bfe2c1a9aa4597300d8f06d801..a61829a5ddfcb6232905a97a4dcfb863444ddcc3 100644 (file)
@@ -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 ||