7 * swab_u16 - reverse bytes in a uint16_t value.
8 * @val: value whose bytes to swap.
10 static inline uint16_t swab_u16(uint16_t val)
12 return ((val & (uint16_t)0x00ffU) << 8)
13 | ((val & (uint16_t)0xff00U) >> 8);
17 * swab_u32 - reverse bytes in a uint32_t value.
18 * @val: value whose bytes to swap.
20 static inline uint32_t swab_u32(uint32_t val)
22 return ((val & (uint32_t)0x000000ffUL) << 24)
23 | ((val & (uint32_t)0x0000ff00UL) << 8)
24 | ((val & (uint32_t)0x00ff0000UL) >> 8)
25 | ((val & (uint32_t)0xff000000UL) >> 24);
29 * swab_u64 - reverse bytes in a uint64_t value.
30 * @val: value whose bytes to swap.
32 static inline uint64_t swab_u64(uint64_t val)
34 return ((val & (uint64_t)0x00000000000000ffULL) << 56)
35 | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
36 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
37 | ((val & (uint64_t)0x00000000ff000000ULL) << 8)
38 | ((val & (uint64_t)0x000000ff00000000ULL) >> 8)
39 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
40 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
41 | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
44 /* Sanity check the defines. We don't handle weird endianness. */
45 #if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
46 #error "Unknown endian"
47 #elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
48 #error "Can't compile for both big and little endian."
52 * cpu_to_le64 - convert a uint64_t value to little-endian
53 * @native: value to convert
55 static inline uint64_t cpu_to_le64(uint64_t native)
57 #if HAVE_LITTLE_ENDIAN
60 return swab_u64(native);
65 * cpu_to_le32 - convert a uint32_t value to little-endian
66 * @native: value to convert
68 static inline uint32_t cpu_to_le32(uint32_t native)
70 #if HAVE_LITTLE_ENDIAN
73 return swab_u32(native);
78 * cpu_to_le16 - convert a uint16_t value to little-endian
79 * @native: value to convert
81 static inline uint16_t cpu_to_le16(uint16_t native)
83 #if HAVE_LITTLE_ENDIAN
86 return swab_u16(native);
91 * le64_to_cpu - convert a little-endian uint64_t value
92 * @le_val: little-endian value to convert
94 static inline uint64_t le64_to_cpu(uint64_t le_val)
96 #if HAVE_LITTLE_ENDIAN
99 return swab_u64(le_val);
104 * le32_to_cpu - convert a little-endian uint32_t value
105 * @le_val: little-endian value to convert
107 static inline uint32_t le32_to_cpu(uint32_t le_val)
109 #if HAVE_LITTLE_ENDIAN
112 return swab_u32(le_val);
117 * le16_to_cpu - convert a little-endian uint16_t value
118 * @le_val: little-endian value to convert
120 static inline uint16_t le16_to_cpu(uint16_t le_val)
122 #if HAVE_LITTLE_ENDIAN
125 return swab_u16(le_val);
130 * cpu_to_be64 - convert a uint64_t value to big endian.
131 * @native: value to convert
133 static inline uint64_t cpu_to_be64(uint64_t native)
135 #if HAVE_LITTLE_ENDIAN
136 return swab_u64(native);
143 * cpu_to_be32 - convert a uint32_t value to big endian.
144 * @native: value to convert
146 static inline uint32_t cpu_to_be32(uint32_t native)
148 #if HAVE_LITTLE_ENDIAN
149 return swab_u32(native);
156 * cpu_to_be16 - convert a uint16_t value to big endian.
157 * @native: value to convert
159 static inline uint16_t cpu_to_be16(uint16_t native)
161 #if HAVE_LITTLE_ENDIAN
162 return swab_u16(native);
169 * be64_to_cpu - convert a big-endian uint64_t value
170 * @be_val: big-endian value to convert
172 static inline uint64_t be64_to_cpu(uint64_t be_val)
174 #if HAVE_LITTLE_ENDIAN
175 return swab_u64(be_val);
182 * be32_to_cpu - convert a big-endian uint32_t value
183 * @be_val: big-endian value to convert
185 static inline uint32_t be32_to_cpu(uint32_t be_val)
187 #if HAVE_LITTLE_ENDIAN
188 return swab_u32(be_val);
195 * be16_to_cpu - convert a big-endian uint16_t value
196 * @be_val: big-endian value to convert
198 static inline uint16_t be16_to_cpu(uint16_t be_val)
200 #if HAVE_LITTLE_ENDIAN
201 return swab_u16(be_val);
207 #endif /* CCAN_ENDIAN_H */