bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 1 | #ifndef BSWAP_H |
| 2 | #define BSWAP_H |
| 3 | |
| 4 | #include "config-host.h" |
| 5 | |
| 6 | #include <inttypes.h> |
| 7 | |
Juan Quintela | 5735147 | 2009-07-27 16:13:01 +0200 | [diff] [blame] | 8 | #ifdef CONFIG_MACHINE_BSWAP_H |
blueswir1 | 1360677 | 2008-12-05 17:54:09 +0000 | [diff] [blame] | 9 | #include <sys/endian.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <machine/bswap.h> |
| 12 | #else |
| 13 | |
Juan Quintela | 936dfb8 | 2009-07-27 16:13:03 +0200 | [diff] [blame] | 14 | #ifdef CONFIG_BYTESWAP_H |
bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 15 | #include <byteswap.h> |
| 16 | #else |
| 17 | |
| 18 | #define bswap_16(x) \ |
| 19 | ({ \ |
| 20 | uint16_t __x = (x); \ |
| 21 | ((uint16_t)( \ |
| 22 | (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \ |
| 23 | (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \ |
| 24 | }) |
| 25 | |
| 26 | #define bswap_32(x) \ |
| 27 | ({ \ |
| 28 | uint32_t __x = (x); \ |
| 29 | ((uint32_t)( \ |
| 30 | (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ |
| 31 | (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ |
| 32 | (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ |
| 33 | (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ |
| 34 | }) |
| 35 | |
| 36 | #define bswap_64(x) \ |
| 37 | ({ \ |
| 38 | uint64_t __x = (x); \ |
| 39 | ((uint64_t)( \ |
| 40 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
| 41 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
| 42 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
| 43 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
| 44 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
| 45 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
| 46 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
| 47 | (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \ |
| 48 | }) |
| 49 | |
Juan Quintela | 936dfb8 | 2009-07-27 16:13:03 +0200 | [diff] [blame] | 50 | #endif /* !CONFIG_BYTESWAP_H */ |
bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 51 | |
bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 52 | static inline uint16_t bswap16(uint16_t x) |
| 53 | { |
| 54 | return bswap_16(x); |
| 55 | } |
| 56 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 57 | static inline uint32_t bswap32(uint32_t x) |
bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 58 | { |
| 59 | return bswap_32(x); |
| 60 | } |
| 61 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 62 | static inline uint64_t bswap64(uint64_t x) |
bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 63 | { |
| 64 | return bswap_64(x); |
| 65 | } |
| 66 | |
Juan Quintela | 5735147 | 2009-07-27 16:13:01 +0200 | [diff] [blame] | 67 | #endif /* ! CONFIG_MACHINE_BSWAP_H */ |
blueswir1 | 1360677 | 2008-12-05 17:54:09 +0000 | [diff] [blame] | 68 | |
bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 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 | |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 84 | #if defined(HOST_WORDS_BIGENDIAN) |
bellard | af8ffdf | 2004-08-01 21:48:12 +0000 | [diff] [blame] | 85 | #define be_bswap(v, size) (v) |
| 86 | #define le_bswap(v, size) bswap ## size(v) |
| 87 | #define be_bswaps(v, size) |
| 88 | #define le_bswaps(p, size) *p = bswap ## size(*p); |
| 89 | #else |
| 90 | #define le_bswap(v, size) (v) |
| 91 | #define be_bswap(v, size) bswap ## size(v) |
| 92 | #define le_bswaps(v, size) |
| 93 | #define be_bswaps(p, size) *p = bswap ## size(*p); |
| 94 | #endif |
| 95 | |
| 96 | #define CPU_CONVERT(endian, size, type)\ |
| 97 | static inline type endian ## size ## _to_cpu(type v)\ |
| 98 | {\ |
| 99 | return endian ## _bswap(v, size);\ |
| 100 | }\ |
| 101 | \ |
| 102 | static inline type cpu_to_ ## endian ## size(type v)\ |
| 103 | {\ |
| 104 | return endian ## _bswap(v, size);\ |
| 105 | }\ |
| 106 | \ |
| 107 | static inline void endian ## size ## _to_cpus(type *p)\ |
| 108 | {\ |
| 109 | endian ## _bswaps(p, size)\ |
| 110 | }\ |
| 111 | \ |
| 112 | static inline void cpu_to_ ## endian ## size ## s(type *p)\ |
| 113 | {\ |
| 114 | endian ## _bswaps(p, size)\ |
| 115 | }\ |
| 116 | \ |
| 117 | static inline type endian ## size ## _to_cpup(const type *p)\ |
| 118 | {\ |
| 119 | return endian ## size ## _to_cpu(*p);\ |
| 120 | }\ |
| 121 | \ |
| 122 | static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\ |
| 123 | {\ |
| 124 | *p = cpu_to_ ## endian ## size(v);\ |
| 125 | } |
| 126 | |
| 127 | CPU_CONVERT(be, 16, uint16_t) |
| 128 | CPU_CONVERT(be, 32, uint32_t) |
| 129 | CPU_CONVERT(be, 64, uint64_t) |
| 130 | |
| 131 | CPU_CONVERT(le, 16, uint16_t) |
| 132 | CPU_CONVERT(le, 32, uint32_t) |
| 133 | CPU_CONVERT(le, 64, uint64_t) |
| 134 | |
| 135 | /* unaligned versions (optimized for frequent unaligned accesses)*/ |
| 136 | |
malc | e58ffeb | 2009-01-14 18:39:49 +0000 | [diff] [blame] | 137 | #if defined(__i386__) || defined(_ARCH_PPC) |
bellard | af8ffdf | 2004-08-01 21:48:12 +0000 | [diff] [blame] | 138 | |
| 139 | #define cpu_to_le16wu(p, v) cpu_to_le16w(p, v) |
| 140 | #define cpu_to_le32wu(p, v) cpu_to_le32w(p, v) |
| 141 | #define le16_to_cpupu(p) le16_to_cpup(p) |
| 142 | #define le32_to_cpupu(p) le32_to_cpup(p) |
aurel32 | 88738c0 | 2008-03-28 22:30:48 +0000 | [diff] [blame] | 143 | #define be32_to_cpupu(p) be32_to_cpup(p) |
bellard | af8ffdf | 2004-08-01 21:48:12 +0000 | [diff] [blame] | 144 | |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 145 | #define cpu_to_be16wu(p, v) cpu_to_be16w(p, v) |
| 146 | #define cpu_to_be32wu(p, v) cpu_to_be32w(p, v) |
Aurelien Jarno | 102c297 | 2011-01-06 22:43:13 +0100 | [diff] [blame] | 147 | #define cpu_to_be64wu(p, v) cpu_to_be64w(p, v) |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 148 | |
bellard | af8ffdf | 2004-08-01 21:48:12 +0000 | [diff] [blame] | 149 | #else |
| 150 | |
| 151 | static inline void cpu_to_le16wu(uint16_t *p, uint16_t v) |
| 152 | { |
| 153 | uint8_t *p1 = (uint8_t *)p; |
| 154 | |
blueswir1 | 9e622b1 | 2009-03-07 15:46:23 +0000 | [diff] [blame] | 155 | p1[0] = v & 0xff; |
bellard | af8ffdf | 2004-08-01 21:48:12 +0000 | [diff] [blame] | 156 | p1[1] = v >> 8; |
| 157 | } |
| 158 | |
| 159 | static inline void cpu_to_le32wu(uint32_t *p, uint32_t v) |
| 160 | { |
| 161 | uint8_t *p1 = (uint8_t *)p; |
| 162 | |
blueswir1 | 9e622b1 | 2009-03-07 15:46:23 +0000 | [diff] [blame] | 163 | p1[0] = v & 0xff; |
bellard | af8ffdf | 2004-08-01 21:48:12 +0000 | [diff] [blame] | 164 | p1[1] = v >> 8; |
| 165 | p1[2] = v >> 16; |
| 166 | p1[3] = v >> 24; |
| 167 | } |
| 168 | |
| 169 | static inline uint16_t le16_to_cpupu(const uint16_t *p) |
| 170 | { |
| 171 | const uint8_t *p1 = (const uint8_t *)p; |
| 172 | return p1[0] | (p1[1] << 8); |
| 173 | } |
| 174 | |
| 175 | static inline uint32_t le32_to_cpupu(const uint32_t *p) |
| 176 | { |
| 177 | const uint8_t *p1 = (const uint8_t *)p; |
| 178 | return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24); |
| 179 | } |
| 180 | |
aurel32 | 88738c0 | 2008-03-28 22:30:48 +0000 | [diff] [blame] | 181 | static inline uint32_t be32_to_cpupu(const uint32_t *p) |
| 182 | { |
| 183 | const uint8_t *p1 = (const uint8_t *)p; |
| 184 | return p1[3] | (p1[2] << 8) | (p1[1] << 16) | (p1[0] << 24); |
| 185 | } |
| 186 | |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 187 | static inline void cpu_to_be16wu(uint16_t *p, uint16_t v) |
| 188 | { |
| 189 | uint8_t *p1 = (uint8_t *)p; |
| 190 | |
| 191 | p1[0] = v >> 8; |
blueswir1 | 9e622b1 | 2009-03-07 15:46:23 +0000 | [diff] [blame] | 192 | p1[1] = v & 0xff; |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static inline void cpu_to_be32wu(uint32_t *p, uint32_t v) |
| 196 | { |
| 197 | uint8_t *p1 = (uint8_t *)p; |
| 198 | |
| 199 | p1[0] = v >> 24; |
| 200 | p1[1] = v >> 16; |
| 201 | p1[2] = v >> 8; |
blueswir1 | 9e622b1 | 2009-03-07 15:46:23 +0000 | [diff] [blame] | 202 | p1[3] = v & 0xff; |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Aurelien Jarno | 102c297 | 2011-01-06 22:43:13 +0100 | [diff] [blame] | 205 | static inline void cpu_to_be64wu(uint64_t *p, uint64_t v) |
| 206 | { |
| 207 | uint8_t *p1 = (uint8_t *)p; |
| 208 | |
| 209 | p1[0] = v >> 56; |
| 210 | p1[1] = v >> 48; |
| 211 | p1[2] = v >> 40; |
| 212 | p1[3] = v >> 32; |
| 213 | p1[4] = v >> 24; |
| 214 | p1[5] = v >> 16; |
| 215 | p1[6] = v >> 8; |
| 216 | p1[7] = v & 0xff; |
| 217 | } |
| 218 | |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 221 | #ifdef HOST_WORDS_BIGENDIAN |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 222 | #define cpu_to_32wu cpu_to_be32wu |
Alexander Graf | 17e6a53 | 2010-05-11 17:38:17 +0200 | [diff] [blame] | 223 | #define leul_to_cpu(v) glue(glue(le,HOST_LONG_BITS),_to_cpu)(v) |
bellard | 188d857 | 2004-10-10 15:44:19 +0000 | [diff] [blame] | 224 | #else |
| 225 | #define cpu_to_32wu cpu_to_le32wu |
Marcelo Tosatti | 213acd2 | 2010-04-23 14:04:13 -0300 | [diff] [blame] | 226 | #define leul_to_cpu(v) (v) |
bellard | af8ffdf | 2004-08-01 21:48:12 +0000 | [diff] [blame] | 227 | #endif |
| 228 | |
| 229 | #undef le_bswap |
| 230 | #undef be_bswap |
| 231 | #undef le_bswaps |
| 232 | #undef be_bswaps |
| 233 | |
Michael S. Tsirkin | e73d6e3 | 2010-01-14 15:58:52 +0200 | [diff] [blame] | 234 | /* len must be one of 1, 2, 4 */ |
| 235 | static inline uint32_t qemu_bswap_len(uint32_t value, int len) |
| 236 | { |
| 237 | return bswap32(value) >> (32 - 8 * len); |
| 238 | } |
| 239 | |
bellard | ab93bbe | 2003-08-10 21:35:13 +0000 | [diff] [blame] | 240 | #endif /* BSWAP_H */ |