initial x86-64 host support (Gwenole Beauchesne)
[qemu] / bswap.h
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
46 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
47 #define HOST_LONG_BITS 64
48 #else
49 #define HOST_LONG_BITS 32
50 #endif
51
52 #define HOST_LONG_SIZE (HOST_LONG_BITS / 8)
53
54 static inline uint16_t bswap16(uint16_t x)
55 {
56     return bswap_16(x);
57 }
58
59 static inline uint32_t bswap32(uint32_t x) 
60 {
61     return bswap_32(x);
62 }
63
64 static inline uint64_t bswap64(uint64_t x) 
65 {
66     return bswap_64(x);
67 }
68
69 static inline void bswap16s(uint16_t *s)
70 {
71     *s = bswap16(*s);
72 }
73
74 static inline void bswap32s(uint32_t *s)
75 {
76     *s = bswap32(*s);
77 }
78
79 static inline void bswap64s(uint64_t *s)
80 {
81     *s = bswap64(*s);
82 }
83
84 #endif /* BSWAP_H */