9652c30b6ef0f7965f1b6d9e5e51a93bbc3f3469
[musl] / include / endian.h
1 #ifndef _ENDIAN_H
2 #define _ENDIAN_H
3
4 #define __LITTLE_ENDIAN 1234
5 #define __BIG_ENDIAN 4321
6 #define __PDP_ENDIAN 3412
7
8 #include <bits/endian.h>
9
10 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
11
12 #define BIG_ENDIAN __BIG_ENDIAN
13 #define LITTLE_ENDIAN __LITTLE_ENDIAN
14 #define PDP_ENDIAN __PDP_ENDIAN
15 #define BYTE_ORDER __BYTE_ORDER
16
17 #include <stdint.h>
18
19 #if __STDC_VERSION__ >= 199901L
20 inline
21 #endif
22 static uint16_t __bswap16(uint16_t __x)
23 {
24         return __x<<8 | __x>>8;
25 }
26
27 #if __STDC_VERSION__ >= 199901L
28 inline
29 #endif
30 static uint32_t __bswap32(uint32_t __x)
31 {
32         return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
33 }
34
35 #if __STDC_VERSION__ >= 199901L
36 inline
37 #endif
38 static uint64_t __bswap64(uint64_t __x)
39 {
40         return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
41 }
42
43 #if __BYTE_ORDER == __LITTLE_ENDIAN
44 #define htobe16(x) __bswap16(x)
45 #define be16toh(x) __bswap16(x)
46 #define betoh16(x) __bswap16(x)
47 #define htobe32(x) __bswap32(x)
48 #define be32toh(x) __bswap32(x)
49 #define betoh32(x) __bswap32(x)
50 #define htobe64(x) __bswap64(x)
51 #define be64toh(x) __bswap64(x)
52 #define betoh64(x) __bswap64(x)
53 #define htole16(x) (uint16_t)(x)
54 #define le16toh(x) (uint16_t)(x)
55 #define letoh16(x) (uint16_t)(x)
56 #define htole32(x) (uint32_t)(x)
57 #define le32toh(x) (uint32_t)(x)
58 #define letoh32(x) (uint32_t)(x)
59 #define htole64(x) (uint64_t)(x)
60 #define le64toh(x) (uint64_t)(x)
61 #define letoh64(x) (uint64_t)(x)
62 #else
63 #define htobe16(x) (uint16_t)(x)
64 #define be16toh(x) (uint16_t)(x)
65 #define betoh16(x) (uint16_t)(x)
66 #define htobe32(x) (uint32_t)(x)
67 #define be32toh(x) (uint32_t)(x)
68 #define betoh32(x) (uint32_t)(x)
69 #define htobe64(x) (uint64_t)(x)
70 #define be64toh(x) (uint64_t)(x)
71 #define betoh64(x) (uint64_t)(x)
72 #define htole16(x) __bswap16(x)
73 #define le16toh(x) __bswap16(x)
74 #define letoh16(x) __bswap16(x)
75 #define htole32(x) __bswap32(x)
76 #define le32toh(x) __bswap32(x)
77 #define letoh32(x) __bswap32(x)
78 #define htole64(x) __bswap64(x)
79 #define le64toh(x) __bswap64(x)
80 #define letoh64(x) __bswap64(x)
81 #endif
82
83 #endif
84
85 #endif