8 #include <machine/endian.h>
11 #define swap16(x) ((uint16_t)(((x) << 8) | ((uint16_t)(x) >> 8)))
12 #define swap32(x) ((uint32_t)(((uint32_t)(x) << 24) & 0xff000000UL | \
13 ((uint32_t)(x) << 8) & 0x00ff0000UL | \
14 ((x) >> 8) & 0x0000ff00UL | \
15 ((x) >> 24) & 0x000000ffUL))
16 #define swap64(x) ((uint64_t)(((uint64_t)(x) << 56) & 0xff00000000000000ULL | \
17 ((uint64_t)(x) << 40) & 0x00ff000000000000ULL | \
18 ((uint64_t)(x) << 24) & 0x0000ff0000000000ULL | \
19 ((uint64_t)(x) << 8) & 0x000000ff00000000ULL | \
20 ((x) >> 8) & 0x00000000ff000000ULL | \
21 ((x) >> 24) & 0x0000000000ff0000ULL | \
22 ((x) >> 40) & 0x000000000000ff00ULL | \
23 ((x) >> 56) & 0x00000000000000ffULL))
35 #define be16atoh(x) ((uint16_t)(((x)[0]<<8)|(x)[1]))
36 #define be32atoh(x) ((uint32_t)(((x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]))
37 #define be64atoh_x(x,off,shift) (((uint64_t)((x)[off]))<<shift)
38 #define be64atoh(x) ((uint64_t)(be64atoh_x(x,0,56)|be64atoh_x(x,1,48)|be64atoh_x(x,2,40)| \
39 be64atoh_x(x,3,32)|be64atoh_x(x,4,24)|be64atoh_x(x,5,16)|be64atoh_x(x,6,8)|((x)[7])))
40 #define le16atoh(x) ((uint16_t)(((x)[1]<<8)|(x)[0]))
41 #define le32atoh(x) ((uint32_t)(((x)[3]<<24)|((x)[2]<<16)|((x)[1]<<8)|(x)[0]))
42 #define le64atoh_x(x,off,shift) (((uint64_t)(x)[off])<<shift)
43 #define le64atoh(x) ((uint64_t)(le64atoh_x(x,7,56)|le64atoh_x(x,6,48)|le64atoh_x(x,5,40)| \
44 le64atoh_x(x,4,32)|le64atoh_x(x,3,24)|le64atoh_x(x,2,16)|le64atoh_x(x,1,8)|((x)[0])))
46 #define htobe16a(a,x) (a)[0]=(uint8_t)((x)>>8), (a)[1]=(uint8_t)(x)
47 #define htobe32a(a,x) (a)[0]=(uint8_t)((x)>>24), (a)[1]=(uint8_t)((x)>>16), \
48 (a)[2]=(uint8_t)((x)>>8), (a)[3]=(uint8_t)(x)
49 #define htobe64a(a,x) (a)[0]=(uint8_t)((x)>>56), (a)[1]=(uint8_t)((x)>>48), \
50 (a)[2]=(uint8_t)((x)>>40), (a)[3]=(uint8_t)((x)>>32), \
51 (a)[4]=(uint8_t)((x)>>24), (a)[5]=(uint8_t)((x)>>16), \
52 (a)[6]=(uint8_t)((x)>>8), (a)[7]=(uint8_t)(x)
53 #define htole16a(a,x) (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
54 #define htole32a(a,x) (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
55 (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
56 #define htole64a(a,x) (a)[7]=(uint8_t)((x)>>56), (a)[6]=(uint8_t)((x)>>48), \
57 (a)[5]=(uint8_t)((x)>>40), (a)[4]=(uint8_t)((x)>>32), \
58 (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
59 (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)