diff options
author | Chris Robinson <[email protected]> | 2021-01-21 22:17:40 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2021-01-21 23:21:18 -0800 |
commit | 5729e1004da8dd8760fe3fa217faec4d3cd80427 (patch) | |
tree | 52ab1feea30d15daba457078e510702a945c9a4b /common/albit.h | |
parent | 87dbeed8536b35a37f9a84510a8da1546b2d76c5 (diff) |
Make the endian test more C++-like
Diffstat (limited to 'common/albit.h')
-rw-r--r-- | common/albit.h | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/common/albit.h b/common/albit.h new file mode 100644 index 00000000..225c0b89 --- /dev/null +++ b/common/albit.h @@ -0,0 +1,35 @@ +#ifndef AL_BIT_H +#define AL_BIT_H + +namespace al { + +#ifdef __BYTE_ORDER__ +enum class endian { + little = __ORDER_LITTLE_ENDIAN__, + big = __ORDER_BIG_ENDIAN__, + native = __BYTE_ORDER__ +}; + +#else + +/* This doesn't support mixed-endian. */ +namespace detail_ { +constexpr inline bool EndianTest() noexcept +{ + static_assert(sizeof(char) < sizeof(int), "char is too big"); + + constexpr int test_val{1}; + return static_cast<const char&>(test_val); +} +} // namespace detail_ + +enum class endian { + little = 0, + big = 1, + native = detail_::EndianTest() ? little : big +}; +#endif + +} // namespace al + +#endif /* AL_BIT_H */ |