]>
Commit | Line | Data |
---|---|---|
ab93bbe2 FB |
1 | #ifndef BSWAP_H |
2 | #define BSWAP_H | |
3 | ||
4 | #include "config-host.h" | |
5 | ||
6 | #include <inttypes.h> | |
7 | ||
8 | #ifdef HAVE_BYTESWAP_H | |
9 | #include <byteswap.h> | |
10 | #else | |
11 | ||
12 | #define bswap_16(x) \ | |
13 | ({ \ | |
14 | uint16_t __x = (x); \ | |
15 | ((uint16_t)( \ | |
16 | (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \ | |
17 | (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \ | |
18 | }) | |
19 | ||
20 | #define bswap_32(x) \ | |
21 | ({ \ | |
22 | uint32_t __x = (x); \ | |
23 | ((uint32_t)( \ | |
24 | (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ | |
25 | (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ | |
26 | (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ | |
27 | (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ | |
28 | }) | |
29 | ||
30 | #define bswap_64(x) \ | |
31 | ({ \ | |
32 | uint64_t __x = (x); \ | |
33 | ((uint64_t)( \ | |
34 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \ | |
35 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ | |
36 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ | |
37 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ | |
38 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ | |
39 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ | |
40 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ | |
41 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \ | |
42 | }) | |
43 | ||
44 | #endif /* !HAVE_BYTESWAP_H */ | |
45 | ||
ab93bbe2 FB |
46 | static inline uint16_t bswap16(uint16_t x) |
47 | { | |
48 | return bswap_16(x); | |
49 | } | |
50 | ||
51 | static inline uint32_t bswap32(uint32_t x) | |
52 | { | |
53 | return bswap_32(x); | |
54 | } | |
55 | ||
56 | static inline uint64_t bswap64(uint64_t x) | |
57 | { | |
58 | return bswap_64(x); | |
59 | } | |
60 | ||
61 | static inline void bswap16s(uint16_t *s) | |
62 | { | |
63 | *s = bswap16(*s); | |
64 | } | |
65 | ||
66 | static inline void bswap32s(uint32_t *s) | |
67 | { | |
68 | *s = bswap32(*s); | |
69 | } | |
70 | ||
71 | static inline void bswap64s(uint64_t *s) | |
72 | { | |
73 | *s = bswap64(*s); | |
74 | } | |
75 | ||
76 | #endif /* BSWAP_H */ |