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)
#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);
......
......@@ -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 <class Pointer>
struct BitVectorCompressedListBase {
BitVectorCompressedListBase() = default;
......
......@@ -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 <class Pointer>
struct EliasFanoCompressedListBase {
EliasFanoCompressedListBase() = default;
......
......@@ -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.
......
......@@ -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 ||
......
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