Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
endian.h
Go to the documentation of this file.
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 <_endian.h>
//machine endian header
9
10
#define BIG_ENDIAN __BIG_ENDIAN
11
#define LITTLE_ENDIAN __LITTLE_ENDIAN
12
#define PDP_ENDIAN __PDP_ENDIAN
13
#define BYTE_ORDER __BYTE_ORDER
14
15
#include <
stdint.h
>
16
17
#define __bswap16(x) ((uint16_t)((((uint16_t)(x)&0xff00) >> 8) | (((uint16_t)(x)&0x00ff) << 8)))
18
19
#define __bswap32(x) \
20
((uint32_t)((((uint32_t)(x)&0xff000000) >> 24) | (((uint32_t)(x)&0x00ff0000) >> 8) | \
21
(((uint32_t)(x)&0x0000ff00) << 8) | (((uint32_t)(x)&0x000000ff) << 24)))
22
23
#define __bswap64(x) \
24
((uint64_t)((((uint64_t)(x)&0xff00000000000000ULL) >> 56) | \
25
(((uint64_t)(x)&0x00ff000000000000ULL) >> 40) | \
26
(((uint64_t)(x)&0x0000ff0000000000ULL) >> 24) | \
27
(((uint64_t)(x)&0x000000ff00000000ULL) >> 8) | \
28
(((uint64_t)(x)&0x00000000ff000000ULL) << 8) | \
29
(((uint64_t)(x)&0x0000000000ff0000ULL) << 24) | \
30
(((uint64_t)(x)&0x000000000000ff00ULL) << 40) | \
31
(((uint64_t)(x)&0x00000000000000ffULL) << 56)))
32
33
#if __BYTE_ORDER == __LITTLE_ENDIAN
34
35
// Definitions from musl libc
36
#define htobe16(x) __bswap16(x)
37
#define be16toh(x) __bswap16(x)
38
#define betoh16(x) __bswap16(x)
39
#define htobe32(x) __bswap32(x)
40
#define be32toh(x) __bswap32(x)
41
#define betoh32(x) __bswap32(x)
42
#define htobe64(x) __bswap64(x)
43
#define be64toh(x) __bswap64(x)
44
#define betoh64(x) __bswap64(x)
45
#define htole16(x) (uint16_t)(x)
46
#define le16toh(x) (uint16_t)(x)
47
#define letoh16(x) (uint16_t)(x)
48
#define htole32(x) (uint32_t)(x)
49
#define le32toh(x) (uint32_t)(x)
50
#define letoh32(x) (uint32_t)(x)
51
#define htole64(x) (uint64_t)(x)
52
#define le64toh(x) (uint64_t)(x)
53
#define letoh64(x) (uint64_t)(x)
54
55
// From Apple Open Source Libc
56
#define ntohs(x) __bswap16(x)
57
#define htons(x) __bswap16(x)
58
#define ntohl(x) __bswap32(x)
59
#define htonl(x) __bswap32(x)
60
#define ntohll(x) __bswap64(x)
61
#define htonll(x) __bswap64(x)
62
#define NTOHL(x) (x) = ntohl((uint32_t)x)
63
#define NTOHS(x) (x) = ntohs((uint16_t)x)
64
#define NTOHLL(x) (x) = ntohll((uint64_t)x)
65
#define HTONL(x) (x) = htonl((uint32_t)x)
66
#define HTONS(x) (x) = htons((uint16_t)x)
67
#define HTONLL(x) (x) = htonll((uint64_t)x)
68
69
#else // BIG_ENDIAN
70
71
// Definitions from musl libc
72
#define htobe16(x) (uint16_t)(x)
73
#define be16toh(x) (uint16_t)(x)
74
#define betoh16(x) (uint16_t)(x)
75
#define htobe32(x) (uint32_t)(x)
76
#define be32toh(x) (uint32_t)(x)
77
#define betoh32(x) (uint32_t)(x)
78
#define htobe64(x) (uint64_t)(x)
79
#define be64toh(x) (uint64_t)(x)
80
#define betoh64(x) (uint64_t)(x)
81
#define htole16(x) __bswap16(x)
82
#define le16toh(x) __bswap16(x)
83
#define letoh16(x) __bswap16(x)
84
#define htole32(x) __bswap32(x)
85
#define le32toh(x) __bswap32(x)
86
#define letoh32(x) __bswap32(x)
87
#define htole64(x) __bswap64(x)
88
#define le64toh(x) __bswap64(x)
89
#define letoh64(x) __bswap64(x)
90
91
// From Apple Open Source libc
92
#define ntohl(x) ((uint32_t)(x))
93
#define ntohs(x) ((uint16_t)(x))
94
#define htonl(x) ((uint32_t)(x))
95
#define htons(x) ((uint16_t)(x))
96
#define ntohll(x) ((uint64_t)(x))
97
#define htonll(x) ((uint64_t)(x))
98
99
#define NTOHL(x) (x)
100
#define NTOHS(x) (x)
101
#define NTOHLL(x) (x)
102
#define HTONL(x) (x)
103
#define HTONS(x) (x)
104
#define HTONLL(x) (x)
105
106
#endif // endian check
107
108
#endif //__ENDIAN_H_
stdint.h
include
endian.h
Generated by
1.8.15