1 /* CC0 (Public domain) - see LICENSE file for details */
8 * BSWAP_16 - reverse bytes in a constant uint16_t value.
9 * @val: constant value whose bytes to swap.
11 * Designed to be usable in constant-requiring initializers.
15 * char buf[BSWAP_16(0x1234)];
18 #define BSWAP_16(val) \
19 ((((uint16_t)(val) & 0x00ff) << 8) \
20 | (((uint16_t)(val) & 0xff00) >> 8))
23 * BSWAP_32 - reverse bytes in a constant uint32_t value.
24 * @val: constant value whose bytes to swap.
26 * Designed to be usable in constant-requiring initializers.
30 * char buf[BSWAP_32(0xff000000)];
33 #define BSWAP_32(val) \
34 ((((uint32_t)(val) & 0x000000ff) << 24) \
35 | (((uint32_t)(val) & 0x0000ff00) << 8) \
36 | (((uint32_t)(val) & 0x00ff0000) >> 8) \
37 | (((uint32_t)(val) & 0xff000000) >> 24))
40 * BSWAP_64 - reverse bytes in a constant uint64_t value.
41 * @val: constantvalue whose bytes to swap.
43 * Designed to be usable in constant-requiring initializers.
47 * char buf[BSWAP_64(0xff00000000000000ULL)];
50 #define BSWAP_64(val) \
51 ((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \
52 | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \
53 | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \
54 | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \
55 | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \
56 | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \
57 | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \
58 | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56))
64 * bswap_16 - reverse bytes in a uint16_t value.
65 * @val: value whose bytes to swap.
68 * // Output contains "1024 is 4 as two bytes reversed"
69 * printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
71 static inline uint16_t bswap_16(uint16_t val)
77 * bswap_32 - reverse bytes in a uint32_t value.
78 * @val: value whose bytes to swap.
81 * // Output contains "1024 is 262144 as four bytes reversed"
82 * printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
84 static inline uint32_t bswap_32(uint32_t val)
88 #endif /* !HAVE_BYTESWAP_H */
92 * bswap_64 - reverse bytes in a uint64_t value.
93 * @val: value whose bytes to swap.
96 * // Output contains "1024 is 1125899906842624 as eight bytes reversed"
97 * printf("1024 is %llu as eight bytes reversed\n",
98 * (unsigned long long)bswap_64(1024));
100 static inline uint64_t bswap_64(uint64_t val)
102 return BSWAP_64(val);
106 /* Needed for Glibc like endiness check */
107 #define __LITTLE_ENDIAN 1234
108 #define __BIG_ENDIAN 4321
110 /* Sanity check the defines. We don't handle weird endianness. */
111 #if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
112 #error "Unknown endian"
113 #elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
114 #error "Can't compile for both big and little endian."
115 #elif HAVE_LITTLE_ENDIAN
116 #define __BYTE_ORDER __LITTLE_ENDIAN
117 #elif HAVE_BIG_ENDIAN
118 #define __BYTE_ORDER __BIG_ENDIAN
123 /* sparse needs forcing to remove bitwise attribute from ccan/short_types */
124 #define ENDIAN_CAST __attribute__((force))
125 #define ENDIAN_TYPE __attribute__((bitwise))
131 typedef uint64_t ENDIAN_TYPE leint64_t;
132 typedef uint64_t ENDIAN_TYPE beint64_t;
133 typedef uint32_t ENDIAN_TYPE leint32_t;
134 typedef uint32_t ENDIAN_TYPE beint32_t;
135 typedef uint16_t ENDIAN_TYPE leint16_t;
136 typedef uint16_t ENDIAN_TYPE beint16_t;
138 #if HAVE_LITTLE_ENDIAN
140 * CPU_TO_LE64 - convert a constant uint64_t value to little-endian
141 * @native: constant to convert
143 #define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)(native))
146 * CPU_TO_LE32 - convert a constant uint32_t value to little-endian
147 * @native: constant to convert
149 #define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)(native))
152 * CPU_TO_LE16 - convert a constant uint16_t value to little-endian
153 * @native: constant to convert
155 #define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)(native))
158 * LE64_TO_CPU - convert a little-endian uint64_t constant
159 * @le_val: little-endian constant to convert
161 #define LE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val))
164 * LE32_TO_CPU - convert a little-endian uint32_t constant
165 * @le_val: little-endian constant to convert
167 #define LE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val))
170 * LE16_TO_CPU - convert a little-endian uint16_t constant
171 * @le_val: little-endian constant to convert
173 #define LE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val))
175 #else /* ... HAVE_BIG_ENDIAN */
176 #define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)BSWAP_64(native))
177 #define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)BSWAP_32(native))
178 #define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)BSWAP_16(native))
179 #define LE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val)
180 #define LE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val)
181 #define LE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val)
182 #endif /* HAVE_BIG_ENDIAN */
186 * CPU_TO_BE64 - convert a constant uint64_t value to big-endian
187 * @native: constant to convert
189 #define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)(native))
192 * CPU_TO_BE32 - convert a constant uint32_t value to big-endian
193 * @native: constant to convert
195 #define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)(native))
198 * CPU_TO_BE16 - convert a constant uint16_t value to big-endian
199 * @native: constant to convert
201 #define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)(native))
204 * BE64_TO_CPU - convert a big-endian uint64_t constant
205 * @le_val: big-endian constant to convert
207 #define BE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val))
210 * BE32_TO_CPU - convert a big-endian uint32_t constant
211 * @le_val: big-endian constant to convert
213 #define BE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val))
216 * BE16_TO_CPU - convert a big-endian uint16_t constant
217 * @le_val: big-endian constant to convert
219 #define BE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val))
221 #else /* ... HAVE_LITTLE_ENDIAN */
222 #define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)BSWAP_64(native))
223 #define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)BSWAP_32(native))
224 #define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)BSWAP_16(native))
225 #define BE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val)
226 #define BE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val)
227 #define BE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val)
228 #endif /* HAVE_LITTE_ENDIAN */
232 * cpu_to_le64 - convert a uint64_t value to little-endian
233 * @native: value to convert
235 static inline leint64_t cpu_to_le64(uint64_t native)
237 return CPU_TO_LE64(native);
241 * cpu_to_le32 - convert a uint32_t value to little-endian
242 * @native: value to convert
244 static inline leint32_t cpu_to_le32(uint32_t native)
246 return CPU_TO_LE32(native);
250 * cpu_to_le16 - convert a uint16_t value to little-endian
251 * @native: value to convert
253 static inline leint16_t cpu_to_le16(uint16_t native)
255 return CPU_TO_LE16(native);
259 * le64_to_cpu - convert a little-endian uint64_t value
260 * @le_val: little-endian value to convert
262 static inline uint64_t le64_to_cpu(leint64_t le_val)
264 return LE64_TO_CPU(le_val);
268 * le32_to_cpu - convert a little-endian uint32_t value
269 * @le_val: little-endian value to convert
271 static inline uint32_t le32_to_cpu(leint32_t le_val)
273 return LE32_TO_CPU(le_val);
277 * le16_to_cpu - convert a little-endian uint16_t value
278 * @le_val: little-endian value to convert
280 static inline uint16_t le16_to_cpu(leint16_t le_val)
282 return LE16_TO_CPU(le_val);
286 * cpu_to_be64 - convert a uint64_t value to big endian.
287 * @native: value to convert
289 static inline beint64_t cpu_to_be64(uint64_t native)
291 return CPU_TO_BE64(native);
295 * cpu_to_be32 - convert a uint32_t value to big endian.
296 * @native: value to convert
298 static inline beint32_t cpu_to_be32(uint32_t native)
300 return CPU_TO_BE32(native);
304 * cpu_to_be16 - convert a uint16_t value to big endian.
305 * @native: value to convert
307 static inline beint16_t cpu_to_be16(uint16_t native)
309 return CPU_TO_BE16(native);
313 * be64_to_cpu - convert a big-endian uint64_t value
314 * @be_val: big-endian value to convert
316 static inline uint64_t be64_to_cpu(beint64_t be_val)
318 return BE64_TO_CPU(be_val);
322 * be32_to_cpu - convert a big-endian uint32_t value
323 * @be_val: big-endian value to convert
325 static inline uint32_t be32_to_cpu(beint32_t be_val)
327 return BE32_TO_CPU(be_val);
331 * be16_to_cpu - convert a big-endian uint16_t value
332 * @be_val: big-endian value to convert
334 static inline uint16_t be16_to_cpu(beint16_t be_val)
336 return BE16_TO_CPU(be_val);
339 /* Whichever they include first, they get these definitions. */
340 #ifdef CCAN_SHORT_TYPES_H
342 * be64/be32/be16 - 64/32/16 bit big-endian representation.
344 typedef beint64_t be64;
345 typedef beint32_t be32;
346 typedef beint16_t be16;
349 * le64/le32/le16 - 64/32/16 bit little-endian representation.
351 typedef leint64_t le64;
352 typedef leint32_t le32;
353 typedef leint16_t le16;
355 #endif /* CCAN_ENDIAN_H */