[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / include / byteswap.h
1 #ifndef ETHERBOOT_BYTESWAP_H
2 #define ETHERBOOT_BYTESWAP_H
3
4 FILE_LICENCE ( GPL2_OR_LATER );
5
6 #include "endian.h"
7 #include "bits/byteswap.h"
8
9 #define __bswap_constant_16(x) \
10 ((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
11 (((uint16_t)(x) & 0xff00) >> 8)))
12
13 #define __bswap_constant_32(x) \
14 ((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
15 (((uint32_t)(x) & 0x0000ff00U) << 8) | \
16 (((uint32_t)(x) & 0x00ff0000U) >> 8) | \
17 (((uint32_t)(x) & 0xff000000U) >> 24)))
18
19 #define __bswap_constant_64(x) \
20 ((uint64_t)((((uint64_t)(x) & 0x00000000000000ffULL) << 56) | \
21 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
22 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
23 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
24 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
25 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
26 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
27 (((uint64_t)(x) & 0xff00000000000000ULL) >> 56)))
28
29 #define __bswap_16(x) \
30 ((uint16_t)(__builtin_constant_p(x) ? \
31 __bswap_constant_16(x) : \
32 __bswap_variable_16(x)))
33
34 #define __bswap_32(x) \
35 ((uint32_t)(__builtin_constant_p(x) ? \
36 __bswap_constant_32(x) : \
37 __bswap_variable_32(x)))
38
39 #define __bswap_64(x) \
40 ((uint64_t)(__builtin_constant_p(x) ? \
41 __bswap_constant_64(x) : \
42 __bswap_variable_64(x)))
43
44 #if __BYTE_ORDER == __LITTLE_ENDIAN
45 #include "little_bswap.h"
46 #endif
47 #if __BYTE_ORDER == __BIG_ENDIAN
48 #include "big_bswap.h"
49 #endif
50
51 /* Make routines available to all */
52 #define swap64(x) __bswap_64(x)
53 #define swap32(x) __bswap_32(x)
54 #define swap16(x) __bswap_16(x)
55 #define bswap_64(x) __bswap_64(x)
56 #define bswap_32(x) __bswap_32(x)
57 #define bswap_16(x) __bswap_16(x)
58
59 #endif /* ETHERBOOT_BYTESWAP_H */