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 /* Sanity check the defines. We don't handle weird endianness. */
107 #if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
108 #error "Unknown endian"
109 #elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
110 #error "Can't compile for both big and little endian."
113 #if HAVE_LITTLE_ENDIAN
115 * CPU_TO_LE64 - convert a constant uint64_t value to little-endian
116 * @native: constant to convert
118 #define CPU_TO_LE64(native) (native)
121 * CPU_TO_LE32 - convert a constant uint32_t value to little-endian
122 * @native: constant to convert
124 #define CPU_TO_LE32(native) (native)
127 * CPU_TO_LE16 - convert a constant uint16_t value to little-endian
128 * @native: constant to convert
130 #define CPU_TO_LE16(native) (native)
133 * LE64_TO_CPU - convert a little-endian uint64_t constant
134 * @le_val: little-endian constant to convert
136 #define LE64_TO_CPU(le_val) (le_val)
139 * LE32_TO_CPU - convert a little-endian uint32_t constant
140 * @le_val: little-endian constant to convert
142 #define LE32_TO_CPU(le_val) (le_val)
145 * LE16_TO_CPU - convert a little-endian uint16_t constant
146 * @le_val: little-endian constant to convert
148 #define LE16_TO_CPU(le_val) (le_val)
150 #else /* ... HAVE_BIG_ENDIAN */
151 #define CPU_TO_LE64(native) BSWAP_64(native)
152 #define CPU_TO_LE32(native) BSWAP_32(native)
153 #define CPU_TO_LE16(native) BSWAP_16(native)
154 #define LE64_TO_CPU(le_val) BSWAP_64(le_val)
155 #define LE32_TO_CPU(le_val) BSWAP_32(le_val)
156 #define LE16_TO_CPU(le_val) BSWAP_16(le_val)
157 #endif /* HAVE_BIG_ENDIAN */
161 * CPU_TO_BE64 - convert a constant uint64_t value to big-endian
162 * @native: constant to convert
164 #define CPU_TO_BE64(native) (native)
167 * CPU_TO_BE32 - convert a constant uint32_t value to big-endian
168 * @native: constant to convert
170 #define CPU_TO_BE32(native) (native)
173 * CPU_TO_BE16 - convert a constant uint16_t value to big-endian
174 * @native: constant to convert
176 #define CPU_TO_BE16(native) (native)
179 * BE64_TO_CPU - convert a big-endian uint64_t constant
180 * @le_val: big-endian constant to convert
182 #define BE64_TO_CPU(le_val) (le_val)
185 * BE32_TO_CPU - convert a big-endian uint32_t constant
186 * @le_val: big-endian constant to convert
188 #define BE32_TO_CPU(le_val) (le_val)
191 * BE16_TO_CPU - convert a big-endian uint16_t constant
192 * @le_val: big-endian constant to convert
194 #define BE16_TO_CPU(le_val) (le_val)
196 #else /* ... HAVE_LITTLE_ENDIAN */
197 #define CPU_TO_BE64(native) BSWAP_64(native)
198 #define CPU_TO_BE32(native) BSWAP_32(native)
199 #define CPU_TO_BE16(native) BSWAP_16(native)
200 #define BE64_TO_CPU(le_val) BSWAP_64(le_val)
201 #define BE32_TO_CPU(le_val) BSWAP_32(le_val)
202 #define BE16_TO_CPU(le_val) BSWAP_16(le_val)
203 #endif /* HAVE_LITTE_ENDIAN */
207 * cpu_to_le64 - convert a uint64_t value to little-endian
208 * @native: value to convert
210 static inline uint64_t cpu_to_le64(uint64_t native)
212 return CPU_TO_LE64(native);
216 * cpu_to_le32 - convert a uint32_t value to little-endian
217 * @native: value to convert
219 static inline uint32_t cpu_to_le32(uint32_t native)
221 return CPU_TO_LE32(native);
225 * cpu_to_le16 - convert a uint16_t value to little-endian
226 * @native: value to convert
228 static inline uint16_t cpu_to_le16(uint16_t native)
230 return CPU_TO_LE16(native);
234 * le64_to_cpu - convert a little-endian uint64_t value
235 * @le_val: little-endian value to convert
237 static inline uint64_t le64_to_cpu(uint64_t le_val)
239 return LE64_TO_CPU(le_val);
243 * le32_to_cpu - convert a little-endian uint32_t value
244 * @le_val: little-endian value to convert
246 static inline uint32_t le32_to_cpu(uint32_t le_val)
248 return LE32_TO_CPU(le_val);
252 * le16_to_cpu - convert a little-endian uint16_t value
253 * @le_val: little-endian value to convert
255 static inline uint16_t le16_to_cpu(uint16_t le_val)
257 return LE16_TO_CPU(le_val);
261 * cpu_to_be64 - convert a uint64_t value to big endian.
262 * @native: value to convert
264 static inline uint64_t cpu_to_be64(uint64_t native)
266 return CPU_TO_BE64(native);
270 * cpu_to_be32 - convert a uint32_t value to big endian.
271 * @native: value to convert
273 static inline uint32_t cpu_to_be32(uint32_t native)
275 return CPU_TO_BE32(native);
279 * cpu_to_be16 - convert a uint16_t value to big endian.
280 * @native: value to convert
282 static inline uint16_t cpu_to_be16(uint16_t native)
284 return CPU_TO_BE16(native);
288 * be64_to_cpu - convert a big-endian uint64_t value
289 * @be_val: big-endian value to convert
291 static inline uint64_t be64_to_cpu(uint64_t be_val)
293 return BE64_TO_CPU(be_val);
297 * be32_to_cpu - convert a big-endian uint32_t value
298 * @be_val: big-endian value to convert
300 static inline uint32_t be32_to_cpu(uint32_t be_val)
302 return BE32_TO_CPU(be_val);
306 * be16_to_cpu - convert a big-endian uint16_t value
307 * @be_val: big-endian value to convert
309 static inline uint16_t be16_to_cpu(uint16_t be_val)
311 return BE16_TO_CPU(be_val);
314 #endif /* CCAN_ENDIAN_H */