diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2024-03-21 11:52:31 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2024-03-21 11:52:31 -0400 |
commit | ef0aef16961508075249a6206dc0e9a9ac27d81c (patch) | |
tree | cc98d97f828319dfd8979490c678750336feae47 | |
parent | 54490a29006529f7ceb4dc0514a075f9f4175621 (diff) | |
download | bfs-ef0aef16961508075249a6206dc0e9a9ac27d81c.tar.xz |
bit: Check __BYTE_ORDER__ for the native endian
__ORDER_NATIVE_ENDIAN__ is not a thing.
-rw-r--r-- | src/bit.h | 4 | ||||
-rw-r--r-- | tests/bit.c | 13 |
2 files changed, 15 insertions, 2 deletions
@@ -167,8 +167,8 @@ #ifdef __STDC_ENDIAN_NATIVE__ # define ENDIAN_NATIVE __STDC_ENDIAN_NATIVE__ -#elif defined(__ORDER_NATIVE_ENDIAN__) -# define ENDIAN_NATIVE __ORDER_NATIVE_ENDIAN__ +#elif defined(__BYTE_ORDER__) +# define ENDIAN_NATIVE __BYTE_ORDER__ #else # define ENDIAN_NATIVE 0 #endif diff --git a/tests/bit.c b/tests/bit.c index 3d66ce3..6548c30 100644 --- a/tests/bit.c +++ b/tests/bit.c @@ -7,6 +7,7 @@ #include "../src/diag.h" #include <limits.h> #include <stdint.h> +#include <string.h> bfs_static_assert(UMAX_WIDTH(0x1) == 1); bfs_static_assert(UMAX_WIDTH(0x3) == 2); @@ -58,6 +59,18 @@ bfs_static_assert(INTMAX_MAX == IWIDTH_MAX(INTMAX_WIDTH)); bool check_bit(void) { bool ret = true; + const char *str = "\x1\x2\x3\x4"; + uint32_t word; + memcpy(&word, str, sizeof(word)); + +#if ENDIAN_NATIVE == ENDIAN_LITTLE + ret &= check_eq(word, 0x04030201); +#elif ENDIAN_NATIVE == ENDIAN_BIG + ret &= check_eq(word, 0x01020304); +#else +# warning "Skipping byte order tests on mixed/unknown-endian machine" +#endif + ret &= check_eq(bswap((uint8_t)0x12), 0x12); ret &= check_eq(bswap((uint16_t)0x1234), 0x3412); ret &= check_eq(bswap((uint32_t)0x12345678), 0x78563412); |