Debian Codegraph @

src/include/endian.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #ifndef _ENDIAN_H_
4 #define _ENDIAN_H_
5
6 #include <arch/byteorder.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <swab.h>
10
11 #if defined(__LITTLE_ENDIAN)
12 #define cpu_to_le64(x) ((uint64_t)(x))
13 #define le64_to_cpu(x) ((uint64_t)(x))
14 #define cpu_to_le32(x) ((uint32_t)(x))
15 #define le32_to_cpu(x) ((uint32_t)(x))
16 #define cpu_to_le16(x) ((uint16_t)(x))
17 #define le16_to_cpu(x) ((uint16_t)(x))
18 #define cpu_to_be64(x) swab64(x)
19 #define be64_to_cpu(x) swab64(x)
20 #define cpu_to_be32(x) swab32(x)
21 #define be32_to_cpu(x) swab32(x)
22 #define cpu_to_be16(x) swab16(x)
23 #define be16_to_cpu(x) swab16(x)
24 #elif defined(__BIG_ENDIAN)
25 #define cpu_to_le64(x) swab64(x)
26 #define le64_to_cpu(x) swab64(x)
27 #define cpu_to_le32(x) swab32(x)
28 #define le32_to_cpu(x) swab32(x)
29 #define cpu_to_le16(x) swab16(x)
30 #define le16_to_cpu(x) swab16(x)
31 #define cpu_to_be64(x) ((uint64_t)(x))
32 #define be64_to_cpu(x) ((uint64_t)(x))
33 #define cpu_to_be32(x) ((uint32_t)(x))
34 #define be32_to_cpu(x) ((uint32_t)(x))
35 #define cpu_to_be16(x) ((uint16_t)(x))
36 #define be16_to_cpu(x) ((uint16_t)(x))
37 #else
38 #error "<arch/byteorder.h> must #define __LITTLE_ENDIAN or __BIG_ENDIAN"
39 #endif
40
41 #define ntohll(x) be64_to_cpu(x)
42 #define htonll(x) cpu_to_be64(x)
43 #define ntohl(x) be32_to_cpu(x)
44 #define htonl(x) cpu_to_be32(x)
45
46 #define __clrsetbits(endian, bits, addr, clear, set) \
47 write##bits(addr, cpu_to_##endian##bits((endian##bits##_to_cpu( \
48 read##bits(addr)) & ~((uint##bits##_t)(clear))) | (set)))
49
50 #define clrbits_le64(addr, clear) __clrsetbits(le, 64, addr, clear, 0)
51 #define clrbits_be64(addr, clear) __clrsetbits(be, 64, addr, clear, 0)
52 #define clrbits_le32(addr, clear) __clrsetbits(le, 32, addr, clear, 0)
53 #define clrbits_be32(addr, clear) __clrsetbits(be, 32, addr, clear, 0)
54 #define clrbits_le16(addr, clear) __clrsetbits(le, 16, addr, clear, 0)
55 #define clrbits_be16(addr, clear) __clrsetbits(be, 16, addr, clear, 0)
56
57 #define setbits_le64(addr, set) __clrsetbits(le, 64, addr, 0, set)
58 #define setbits_be64(addr, set) __clrsetbits(be, 64, addr, 0, set)
59 #define setbits_le32(addr, set) __clrsetbits(le, 32, addr, 0, set)
60 #define setbits_be32(addr, set) __clrsetbits(be, 32, addr, 0, set)
61 #define setbits_le16(addr, set) __clrsetbits(le, 16, addr, 0, set)
62 #define setbits_be16(addr, set) __clrsetbits(be, 16, addr, 0, set)
63
64 #define clrsetbits_le64(addr, clear, set) __clrsetbits(le, 64, addr, clear, set)
65 #define clrsetbits_be64(addr, clear, set) __clrsetbits(be, 64, addr, clear, set)
66 #define clrsetbits_le32(addr, clear, set) __clrsetbits(le, 32, addr, clear, set)
67 #define clrsetbits_be32(addr, clear, set) __clrsetbits(be, 32, addr, clear, set)
68 #define clrsetbits_le16(addr, clear, set) __clrsetbits(le, 16, addr, clear, set)
69 #define clrsetbits_be16(addr, clear, set) __clrsetbits(be, 16, addr, clear, set)
70
71 /*
72 * be16dec/be32dec/be64dec/le16dec/le32dec/le64dec family of functions.
73 * RISC-V doesn't support misaligned access so decode it byte by byte.
74 */
75 #define DEFINE_ENDIAN_DEC(endian, width) \
76 static inline uint##width##_t endian##width##dec(const void *p) \
77 { \
78 if (ENV_RISCV) { \
79 uint##width##_t val; \
80 memcpy(&val, p, sizeof(val)); \
81 return endian##width##_to_cpu(val); \
82 } else { \
83 return endian##width##_to_cpu(*(uint##width##_t *)p); \
84 } \
85 }
86
87 DEFINE_ENDIAN_DEC(be, 16)
88 DEFINE_ENDIAN_DEC(be, 32)
89 DEFINE_ENDIAN_DEC(be, 64)
90 DEFINE_ENDIAN_DEC(le, 16)
91 DEFINE_ENDIAN_DEC(le, 32)
92 DEFINE_ENDIAN_DEC(le, 64)
93
94 /*
95 * be16enc/be32enc/be64enc/le16enc/le32enc/le64enc family of functions.
96 * RISC-V doesn't support misaligned access so encode it byte by byte.
97 */
98 #define DEFINE_ENDIAN_ENC(endian, width) \
99 static inline void endian##width##enc(void *p, uint##width##_t u) \
100 { \
101 if (ENV_RISCV) { \
102 uint##width##_t val = cpu_to_##endian##width(u); \
103 memcpy(p, &val, sizeof(val)); \
104 } else { \
105 *(uint##width##_t *)p = cpu_to_##endian##width(u); \
106 } \
107 }
108
109 DEFINE_ENDIAN_ENC(be, 16)
110 DEFINE_ENDIAN_ENC(be, 32)
111 DEFINE_ENDIAN_ENC(be, 64)
112 DEFINE_ENDIAN_ENC(le, 16)
113 DEFINE_ENDIAN_ENC(le, 32)
114 DEFINE_ENDIAN_ENC(le, 64)
115
116 /*
117 * Portable (API) endian support that can be used in code that is shared
118 * with userspace (man 3 endian) tools.
119 */
120 static inline uint16_t htobe16(uint16_t host_16bits)
121 {
122 return cpu_to_be16(host_16bits);
123 }
124
125 static inline uint16_t htole16(uint16_t host_16bits)
126 {
127 return cpu_to_le16(host_16bits);
128 }
129
130 static inline uint16_t be16toh(uint16_t big_endian_16bits)
131 {
132 return be16_to_cpu(big_endian_16bits);
133 }
134
135 static inline uint16_t le16toh(uint16_t little_endian_16bits)
136 {
137 return le16_to_cpu(little_endian_16bits);
138 }
139
140 static inline uint32_t htobe32(uint32_t host_32bits)
141 {
142 return cpu_to_be32(host_32bits);
143 }
144
145 static inline uint32_t htole32(uint32_t host_32bits)
146 {
147 return cpu_to_le32(host_32bits);
148 }
149
150 static inline uint32_t be32toh(uint32_t big_endian_32bits)
151 {
152 return be32_to_cpu(big_endian_32bits);
153 }
154
155 static inline uint32_t le32toh(uint32_t little_endian_32bits)
156 {
157 return le32_to_cpu(little_endian_32bits);
158 }
159
160 static inline uint64_t htobe64(uint64_t host_64bits)
161 {
162 return cpu_to_be64(host_64bits);
163 }
164
165 static inline uint64_t htole64(uint64_t host_64bits)
166 {
167 return cpu_to_le64(host_64bits);
168 }
169
170 static inline uint64_t be64toh(uint64_t big_endian_64bits)
171 {
172 return be64_to_cpu(big_endian_64bits);
173 }
174
175 static inline uint64_t le64toh(uint64_t little_endian_64bits)
176 {
177 return le64_to_cpu(little_endian_64bits);
178 }
179
180 #endif