10 * bswap_16 - reverse bytes in a uint16_t value.
11 * @val: value whose bytes to swap.
14 * // Output contains "1024 is 4 as two bytes reversed"
15 * printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
17 static inline uint16_t bswap_16(uint16_t val)
19 return ((val & (uint16_t)0x00ffU) << 8)
20 | ((val & (uint16_t)0xff00U) >> 8);
24 * bswap_32 - reverse bytes in a uint32_t value.
25 * @val: value whose bytes to swap.
28 * // Output contains "1024 is 262144 as four bytes reversed"
29 * printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
31 static inline uint32_t bswap_32(uint32_t val)
33 return ((val & (uint32_t)0x000000ffUL) << 24)
34 | ((val & (uint32_t)0x0000ff00UL) << 8)
35 | ((val & (uint32_t)0x00ff0000UL) >> 8)
36 | ((val & (uint32_t)0xff000000UL) >> 24);
38 #endif /* !HAVE_BYTESWAP_H */
42 * bswap_64 - reverse bytes in a uint64_t value.
43 * @val: value whose bytes to swap.
46 * // Output contains "1024 is 1125899906842624 as eight bytes reversed"
47 * printf("1024 is %llu as eight bytes reversed\n",
48 * (unsigned long long)bswap_64(1024));
50 static inline uint64_t bswap_64(uint64_t val)
52 return ((val & (uint64_t)0x00000000000000ffULL) << 56)
53 | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
54 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
55 | ((val & (uint64_t)0x00000000ff000000ULL) << 8)
56 | ((val & (uint64_t)0x000000ff00000000ULL) >> 8)
57 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
58 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
59 | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
63 /* Sanity check the defines. We don't handle weird endianness. */
64 #if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
65 #error "Unknown endian"
66 #elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
67 #error "Can't compile for both big and little endian."
71 * cpu_to_le64 - convert a uint64_t value to little-endian
72 * @native: value to convert
74 static inline uint64_t cpu_to_le64(uint64_t native)
76 #if HAVE_LITTLE_ENDIAN
79 return bswap_64(native);
84 * cpu_to_le32 - convert a uint32_t value to little-endian
85 * @native: value to convert
87 static inline uint32_t cpu_to_le32(uint32_t native)
89 #if HAVE_LITTLE_ENDIAN
92 return bswap_32(native);
97 * cpu_to_le16 - convert a uint16_t value to little-endian
98 * @native: value to convert
100 static inline uint16_t cpu_to_le16(uint16_t native)
102 #if HAVE_LITTLE_ENDIAN
105 return bswap_16(native);
110 * le64_to_cpu - convert a little-endian uint64_t value
111 * @le_val: little-endian value to convert
113 static inline uint64_t le64_to_cpu(uint64_t le_val)
115 #if HAVE_LITTLE_ENDIAN
118 return bswap_64(le_val);
123 * le32_to_cpu - convert a little-endian uint32_t value
124 * @le_val: little-endian value to convert
126 static inline uint32_t le32_to_cpu(uint32_t le_val)
128 #if HAVE_LITTLE_ENDIAN
131 return bswap_32(le_val);
136 * le16_to_cpu - convert a little-endian uint16_t value
137 * @le_val: little-endian value to convert
139 static inline uint16_t le16_to_cpu(uint16_t le_val)
141 #if HAVE_LITTLE_ENDIAN
144 return bswap_16(le_val);
149 * cpu_to_be64 - convert a uint64_t value to big endian.
150 * @native: value to convert
152 static inline uint64_t cpu_to_be64(uint64_t native)
154 #if HAVE_LITTLE_ENDIAN
155 return bswap_64(native);
162 * cpu_to_be32 - convert a uint32_t value to big endian.
163 * @native: value to convert
165 static inline uint32_t cpu_to_be32(uint32_t native)
167 #if HAVE_LITTLE_ENDIAN
168 return bswap_32(native);
175 * cpu_to_be16 - convert a uint16_t value to big endian.
176 * @native: value to convert
178 static inline uint16_t cpu_to_be16(uint16_t native)
180 #if HAVE_LITTLE_ENDIAN
181 return bswap_16(native);
188 * be64_to_cpu - convert a big-endian uint64_t value
189 * @be_val: big-endian value to convert
191 static inline uint64_t be64_to_cpu(uint64_t be_val)
193 #if HAVE_LITTLE_ENDIAN
194 return bswap_64(be_val);
201 * be32_to_cpu - convert a big-endian uint32_t value
202 * @be_val: big-endian value to convert
204 static inline uint32_t be32_to_cpu(uint32_t be_val)
206 #if HAVE_LITTLE_ENDIAN
207 return bswap_32(be_val);
214 * be16_to_cpu - convert a big-endian uint16_t value
215 * @be_val: big-endian value to convert
217 static inline uint16_t be16_to_cpu(uint16_t be_val)
219 #if HAVE_LITTLE_ENDIAN
220 return bswap_16(be_val);
226 #endif /* CCAN_ENDIAN_H */