29 #ifndef _SYS_ENDIAN_H_ 30 #define _SYS_ENDIAN_H_ 32 #include <sys/cdefs.h> 38 #define bswap16(x) CPU_swap_u16(x) 39 #define bswap32(x) CPU_swap_u32(x) 40 static __inline uint64_t
44 return __builtin_bswap64(v);
46 return ((v >> 56) | ((v >> 40) & 0xff00) | ((v >> 24) & 0xff0000) |
47 ((v >> 8) & 0xff000000) | ((v << 8) & ((uint64_t)0xff << 32)) |
48 ((v << 24) & ((uint64_t)0xff << 40)) |
49 ((v << 40) & ((uint64_t)0xff << 48)) | ((v << 56)));
57 #if BYTE_ORDER == LITTLE_ENDIAN 58 #define htobe16(x) bswap16((x)) 59 #define htobe32(x) bswap32((x)) 60 #define htobe64(x) bswap64((x)) 61 #define htole16(x) ((uint16_t)(x)) 62 #define htole32(x) ((uint32_t)(x)) 63 #define htole64(x) ((uint64_t)(x)) 65 #define be16toh(x) bswap16((x)) 66 #define be32toh(x) bswap32((x)) 67 #define be64toh(x) bswap64((x)) 68 #define le16toh(x) ((uint16_t)(x)) 69 #define le32toh(x) ((uint32_t)(x)) 70 #define le64toh(x) ((uint64_t)(x)) 72 #define htobe16(x) ((uint16_t)(x)) 73 #define htobe32(x) ((uint32_t)(x)) 74 #define htobe64(x) ((uint64_t)(x)) 75 #define htole16(x) bswap16((x)) 76 #define htole32(x) bswap32((x)) 77 #define htole64(x) bswap64((x)) 79 #define be16toh(x) ((uint16_t)(x)) 80 #define be32toh(x) ((uint32_t)(x)) 81 #define be64toh(x) ((uint64_t)(x)) 82 #define le16toh(x) bswap16((x)) 83 #define le32toh(x) bswap32((x)) 84 #define le64toh(x) bswap64((x)) 89 static __inline uint16_t
90 be16dec(
const void *pp)
92 uint8_t
const *p = (uint8_t
const *)pp;
94 return (((
unsigned)p[0] << 8) | p[1]);
97 static __inline uint32_t
98 be32dec(
const void *pp)
100 uint8_t
const *p = (uint8_t
const *)pp;
102 return (((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
103 ((uint32_t)p[2] << 8) | p[3]);
106 static __inline uint64_t
107 be64dec(
const void *pp)
109 uint8_t
const *p = (uint8_t
const *)pp;
111 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
114 static __inline uint16_t
115 le16dec(
const void *pp)
117 uint8_t
const *p = (uint8_t
const *)pp;
119 return (((
unsigned)p[1] << 8) | p[0]);
122 static __inline uint32_t
123 le32dec(
const void *pp)
125 uint8_t
const *p = (uint8_t
const *)pp;
127 return (((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) |
128 ((uint32_t)p[1] << 8) | p[0]);
131 static __inline uint64_t
132 le64dec(
const void *pp)
134 uint8_t
const *p = (uint8_t
const *)pp;
136 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
140 be16enc(
void *pp, uint16_t u)
142 uint8_t *p = (uint8_t *)pp;
144 p[0] = (u >> 8) & 0xff;
149 be32enc(
void *pp, uint32_t u)
151 uint8_t *p = (uint8_t *)pp;
153 p[0] = (u >> 24) & 0xff;
154 p[1] = (u >> 16) & 0xff;
155 p[2] = (u >> 8) & 0xff;
160 be64enc(
void *pp, uint64_t u)
162 uint8_t *p = (uint8_t *)pp;
164 be32enc(p, (uint32_t)(u >> 32));
165 be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
169 le16enc(
void *pp, uint16_t u)
171 uint8_t *p = (uint8_t *)pp;
174 p[1] = (u >> 8) & 0xff;
178 le32enc(
void *pp, uint32_t u)
180 uint8_t *p = (uint8_t *)pp;
183 p[1] = (u >> 8) & 0xff;
184 p[2] = (u >> 16) & 0xff;
185 p[3] = (u >> 24) & 0xff;
189 le64enc(
void *pp, uint64_t u)
191 uint8_t *p = (uint8_t *)pp;
193 le32enc(p, (uint32_t)(u & 0xffffffffU));
194 le32enc(p + 4, (uint32_t)(u >> 32));
This include file provides endian information about the target.