From 9b9a20e226f80328ca6c7aa1dd270bac15fbcc45 Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 7 Jul 2025 11:47:13 +0200 Subject: [PATCH] we are using GNU extensions, using `__builtin_bswap*` is fine. --- src/util/compat/bswap.h | 44 ---------------------------------------- src/util/compat/endian.h | 25 +++++++++++------------ test/dat.h | 6 ------ test/t_util.h | 32 ----------------------------- 4 files changed, 12 insertions(+), 95 deletions(-) delete mode 100644 src/util/compat/bswap.h diff --git a/src/util/compat/bswap.h b/src/util/compat/bswap.h deleted file mode 100644 index 39455bc..0000000 --- a/src/util/compat/bswap.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "../types.h" - -#if defined(__GNUC__) || defined(__clang__) -#define bswap16 __builtin_bswap16 -#define bswap32 __builtin_bswap32 -#define bswap64 __builtin_bswap64 -#elif __has_include() -#include -#define bswap16 bswap_16 -#define bswap32 bswap_32 -#define bswap64 bswap_64 -#else -#define bswap16 bswap16_impl -#define bswap32 bswap32_impl -#define bswap64 bswap64_impl -#define IMPL_BSWAP -#endif - -#if defined(IMPL_BSWAP) || !defined(NDEBUG) -static inline u16 bswap16_impl(u16 x) { - return (x << 8) | (x >> 8); -} - -static inline u32 bswap32_impl(u32 x) { - return (x << 24) | - ((0x0000FF00U & x) << 8) | - ((0x00FF0000U & x) >> 8) | - (x >> 24); -} - -static inline u64 bswap64_impl(u64 x) { - return (x << 56) | - ((0x000000000000FF00ULL & x) << 40) | - ((0x0000000000FF0000ULL & x) << 24) | - ((0x00000000FF000000ULL & x) << 8) | - ((0x000000FF00000000ULL & x) >> 8) | - ((0x0000FF0000000000ULL & x) >> 24) | - ((0x00FF000000000000ULL & x) >> 40) | - (x >> 56); -} -#endif -#undef IMPL_BSWAP diff --git a/src/util/compat/endian.h b/src/util/compat/endian.h index e1fcb95..809fbd8 100644 --- a/src/util/compat/endian.h +++ b/src/util/compat/endian.h @@ -6,7 +6,6 @@ #include "../atrb.h" #include "../types.h" -#include "bswap.h" /* little endian */ atrb_const static inline u16 le16ton(u16); // converts little-endian (LE) encoding to native for a 16 bit integer. (NOOP if native is LE) @@ -31,19 +30,19 @@ u64 le64ton(u64 x) { return x; } u16 ntole16(u16 x) { return x; } u32 ntole32(u32 x) { return x; } u64 ntole64(u64 x) { return x; } -u16 be16ton(u16 x) { return bswap16(x); } -u32 be32ton(u32 x) { return bswap32(x); } -u64 be64ton(u64 x) { return bswap64(x); } -u16 ntobe16(u16 x) { return bswap16(x); } -u32 ntobe32(u32 x) { return bswap32(x); } -u64 ntobe64(u64 x) { return bswap64(x); } +u16 be16ton(u16 x) { return __builtin_bswap16(x); } +u32 be32ton(u32 x) { return __builtin_bswap32(x); } +u64 be64ton(u64 x) { return __builtin_bswap64(x); } +u16 ntobe16(u16 x) { return __builtin_bswap16(x); } +u32 ntobe32(u32 x) { return __builtin_bswap32(x); } +u64 ntobe64(u64 x) { return __builtin_bswap64(x); } #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -u16 le16ton(u16 x) { bswap_16(x); } -u32 le32ton(u32 x) { bswap_32(x); } -u64 le64ton(u64 x) { bswap_64(x); } -u16 ntole16(u16 x) { bswap_16(x); } -u32 ntole32(u32 x) { bswap_32(x); } -u64 ntole64(u64 x) { bswap_64(x); } +u16 le16ton(u16 x) { __builtin_bswap16(x); } +u32 le32ton(u32 x) { __builtin_bswap32(x); } +u64 le64ton(u64 x) { __builtin_bswap64(x); } +u16 ntole16(u16 x) { __builtin_bswap16(x); } +u32 ntole32(u32 x) { __builtin_bswap32(x); } +u64 ntole64(u64 x) { __builtin_bswap64(x); } u16 be16ton(u16 x) { return x; } u32 be32ton(u32 x) { return x; } u64 be64ton(u64 x) { return x; } diff --git a/test/dat.h b/test/dat.h index df186b7..a41185c 100644 --- a/test/dat.h +++ b/test/dat.h @@ -7,12 +7,6 @@ #include "test.h" testdat tests[] = { - {"", test_bswap16, (u16[]){0x11EE, 0xEE11} }, - {"", test_bswap32, (u32[]){0x1142ADEE, 0xEEAD4211} }, - {"", test_bswap64, (u64[]){0x114266897799ADEE, 0xEEAD997789664211} }, - {"", test_bswap16_impl, (u16[]){0x11EE, 0xEE11} }, - {"", test_bswap32_impl, (u32[]){0x1142ADEE, 0xEEAD4211} }, - {"", test_bswap64_impl, (u64[]){0x114266897799ADEE, 0xEEAD997789664211} }, {"k=v", test_procbuf, &(struct test_procbuf){"key=val", "key", "val", 0} }, {"sometxt", test_procbuf, &(struct test_procbuf){"sometxt", "sometxt", "", CONF_ESYNTAX} }, {"comment", test_procbuf, &(struct test_procbuf){"# comment", "", "", CONF_ENODAT} }, diff --git a/test/t_util.h b/test/t_util.h index 1dedf8b..d175c27 100644 --- a/test/t_util.h +++ b/test/t_util.h @@ -2,8 +2,6 @@ #include #include "../src/util/colour32.h" -#include "../src/util/compat/bswap.h" -#include "../src/util/types.h" #include "test.h" int test_colour32_endianess(void *d) { @@ -11,33 +9,3 @@ int test_colour32_endianess(void *d) { colour32 c = {0xFF000000}; // setting just the red channel return assert_true(c.ch.r == 0xFF); } - -int test_bswap16(void *d) { - u16 *arg = d; - return assert_true(bswap16(arg[0]) == arg[1]); -} - -int test_bswap32(void *d) { - u32 *arg = d; - return assert_true(bswap32(arg[0]) == arg[1]); -} - -int test_bswap64(void *d) { - u64 *arg = d; - return assert_true(bswap64(arg[0]) == arg[1]); -} - -int test_bswap16_impl(void *d) { - u16 *arg = d; - return assert_true(bswap16_impl(arg[0]) == arg[1]); -} - -int test_bswap32_impl(void *d) { - u32 *arg = d; - return assert_true(bswap32_impl(arg[0]) == arg[1]); -} - -int test_bswap64_impl(void *d) { - u64 *arg = d; - return assert_true(bswap64_impl(arg[0]) == arg[1]); -}