blob: 0047e4e9824dacb71a8da9802eb1440357607e9d [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Lame bswap replacements as we can't assume the host is sane and provides
3 * working versions of these.
4 */
5
6#ifndef _BSWAP_H
7#define _BSWAP_H 1
8
9#ifdef __linux__
10# include <byteswap.h>
11#else
12
13static __inline__ uint16_t bswap_16(uint16_t x)
14{
15 return ((((x) & 0xff00) >> 8) | \
16 (((x) & 0x00ff) << 8));
17}
18static __inline__ uint32_t bswap_32(uint32_t x)
19{
20 return ((((x) & 0xff000000) >> 24) | \
21 (((x) & 0x00ff0000) >> 8) | \
22 (((x) & 0x0000ff00) << 8) | \
23 (((x) & 0x000000ff) << 24));
24}
25static __inline__ uint64_t bswap_64(uint64_t x)
26{
27#define _uswap_64(x, sfx) \
28 return ((((x) & 0xff00000000000000##sfx) >> 56) | \
29 (((x) & 0x00ff000000000000##sfx) >> 40) | \
30 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
31 (((x) & 0x000000ff00000000##sfx) >> 8) | \
32 (((x) & 0x00000000ff000000##sfx) << 8) | \
33 (((x) & 0x0000000000ff0000##sfx) << 24) | \
34 (((x) & 0x000000000000ff00##sfx) << 40) | \
35 (((x) & 0x00000000000000ff##sfx) << 56));
36#if defined(__GNUC__)
37 _uswap_64(x, ull)
38#else
39 _uswap_64(x, )
40#endif
41#undef _uswap_64
42}
43#endif
44
45#endif