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