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