we are using GNU extensions, using __builtin_bswap* is fine.

This commit is contained in:
2025-07-07 11:47:13 +02:00
parent c56e2399e9
commit 9b9a20e226
4 changed files with 12 additions and 95 deletions

View File

@@ -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(<byteswap.h>)
#include <byteswap.h>
#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

View File

@@ -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; }

View File

@@ -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} },

View File

@@ -2,8 +2,6 @@
#include <stdint.h>
#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]);
}