Skip to content

Commit

Permalink
bit: Add bswap() overloads for every primitive type
Browse files Browse the repository at this point in the history
Fixes: #145
  • Loading branch information
tavianator committed Oct 27, 2024
1 parent 1f97769 commit 515cda3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
43 changes: 34 additions & 9 deletions src/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,35 @@ static inline uint8_t bswap_u8(uint8_t n) {
return n;
}

/**
* Reverse the byte order of an integer.
*/
#define bswap(n) \
_Generic((n), \
uint8_t: bswap_u8, \
uint16_t: bswap_u16, \
uint32_t: bswap_u32, \
uint64_t: bswap_u64)(n)
#if UCHAR_WIDTH == 8
# define bswap_uc bswap_u8
#endif

#if USHRT_WIDTH == 16
# define bswap_us bswap_u16
#elif USHRT_WIDTH == 32
# define bswap_us bswap_u32
#elif USHRT_WIDTH == 64
# define bswap_us bswap_u64
#endif

#if UINT_WIDTH == 16
# define bswap_ui bswap_u16
#elif UINT_WIDTH == 32
# define bswap_ui bswap_u32
#elif UINT_WIDTH == 64
# define bswap_ui bswap_u64
#endif

#if ULONG_WIDTH == 32
# define bswap_ul bswap_u32
#elif ULONG_WIDTH == 64
# define bswap_ul bswap_u64
#endif

#if ULLONG_WIDTH == 64
# define bswap_ull bswap_u64
#endif

// Define an overload for each unsigned type
#define UINT_OVERLOADS(macro) \
Expand All @@ -225,6 +245,11 @@ static inline uint8_t bswap_u8(uint8_t n) {
unsigned long: name##_ul, \
unsigned long long: name##_ull)

/**
* Reverse the byte order of an integer.
*/
#define bswap(n) UINT_SELECT(n, bswap)(n)

// C23 polyfill: bit utilities

#if __STDC_VERSION_STDBIT_H__ >= C23
Expand Down
7 changes: 7 additions & 0 deletions tests/bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ void check_bit(void) {
check_eq(bswap((uint32_t)0x12345678), 0x78563412);
check_eq(bswap((uint64_t)0x1234567812345678), 0x7856341278563412);

// Make sure we can bswap() every unsigned type
(void)bswap((unsigned char)0);
(void)bswap((unsigned short)0);
(void)bswap(0U);
(void)bswap(0UL);
(void)bswap(0ULL);

check_eq(count_ones(0x0U), 0);
check_eq(count_ones(0x1U), 1);
check_eq(count_ones(0x2U), 1);
Expand Down

0 comments on commit 515cda3

Please sign in to comment.