1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_SWAB_H
3 #define _UAPI_LINUX_SWAB_H
5 #include <linux/types.h>
6 #include <linux/compiler.h>
10 * casts are necessary for constants, because we never know how for sure
11 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
13 #define ___constant_swab16(x) ((__u16)( \
14 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
15 (((__u16)(x) & (__u16)0xff00U) >> 8)))
17 #define ___constant_swab32(x) ((__u32)( \
18 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
19 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
20 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
21 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
23 #define ___constant_swab64(x) ((__u64)( \
24 (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
25 (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
26 (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
27 (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
28 (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
29 (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
30 (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
31 (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
33 #define ___constant_swahw32(x) ((__u32)( \
34 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
35 (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
37 #define ___constant_swahb32(x) ((__u32)( \
38 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
39 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
42 * Implement the following as inlines, but define the interface using
43 * macros to allow constant folding when possible:
44 * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
47 static inline __attribute_const__ __u16
__fswab16(__u16 val
)
49 #if defined (__arch_swab16)
50 return __arch_swab16(val
);
52 return ___constant_swab16(val
);
56 static inline __attribute_const__ __u32
__fswab32(__u32 val
)
58 #if defined(__arch_swab32)
59 return __arch_swab32(val
);
61 return ___constant_swab32(val
);
65 static inline __attribute_const__ __u64
__fswab64(__u64 val
)
67 #if defined (__arch_swab64)
68 return __arch_swab64(val
);
69 #elif defined(__SWAB_64_THRU_32__)
71 __u32 l
= val
& ((1ULL << 32) - 1);
72 return (((__u64
)__fswab32(l
)) << 32) | ((__u64
)(__fswab32(h
)));
74 return ___constant_swab64(val
);
78 static inline __attribute_const__ __u32
__fswahw32(__u32 val
)
81 return __arch_swahw32(val
);
83 return ___constant_swahw32(val
);
87 static inline __attribute_const__ __u32
__fswahb32(__u32 val
)
90 return __arch_swahb32(val
);
92 return ___constant_swahb32(val
);
97 * __swab16 - return a byteswapped 16-bit value
98 * @x: value to byteswap
100 #ifdef __HAVE_BUILTIN_BSWAP16__
101 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
103 #define __swab16(x) \
104 (__builtin_constant_p((__u16)(x)) ? \
105 ___constant_swab16(x) : \
110 * __swab32 - return a byteswapped 32-bit value
111 * @x: value to byteswap
113 #ifdef __HAVE_BUILTIN_BSWAP32__
114 #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
116 #define __swab32(x) \
117 (__builtin_constant_p((__u32)(x)) ? \
118 ___constant_swab32(x) : \
123 * __swab64 - return a byteswapped 64-bit value
124 * @x: value to byteswap
126 #ifdef __HAVE_BUILTIN_BSWAP64__
127 #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x))
129 #define __swab64(x) \
130 (__builtin_constant_p((__u64)(x)) ? \
131 ___constant_swab64(x) : \
136 * __swahw32 - return a word-swapped 32-bit value
137 * @x: value to wordswap
139 * __swahw32(0x12340000) is 0x00001234
141 #define __swahw32(x) \
142 (__builtin_constant_p((__u32)(x)) ? \
143 ___constant_swahw32(x) : \
147 * __swahb32 - return a high and low byte-swapped 32-bit value
148 * @x: value to byteswap
150 * __swahb32(0x12345678) is 0x34127856
152 #define __swahb32(x) \
153 (__builtin_constant_p((__u32)(x)) ? \
154 ___constant_swahb32(x) : \
158 * __swab16p - return a byteswapped 16-bit value from a pointer
159 * @p: pointer to a naturally-aligned 16-bit value
161 static __always_inline __u16
__swab16p(const __u16
*p
)
163 #ifdef __arch_swab16p
164 return __arch_swab16p(p
);
171 * __swab32p - return a byteswapped 32-bit value from a pointer
172 * @p: pointer to a naturally-aligned 32-bit value
174 static __always_inline __u32
__swab32p(const __u32
*p
)
176 #ifdef __arch_swab32p
177 return __arch_swab32p(p
);
184 * __swab64p - return a byteswapped 64-bit value from a pointer
185 * @p: pointer to a naturally-aligned 64-bit value
187 static __always_inline __u64
__swab64p(const __u64
*p
)
189 #ifdef __arch_swab64p
190 return __arch_swab64p(p
);
197 * __swahw32p - return a wordswapped 32-bit value from a pointer
198 * @p: pointer to a naturally-aligned 32-bit value
200 * See __swahw32() for details of wordswapping.
202 static inline __u32
__swahw32p(const __u32
*p
)
204 #ifdef __arch_swahw32p
205 return __arch_swahw32p(p
);
207 return __swahw32(*p
);
212 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
213 * @p: pointer to a naturally-aligned 32-bit value
215 * See __swahb32() for details of high/low byteswapping.
217 static inline __u32
__swahb32p(const __u32
*p
)
219 #ifdef __arch_swahb32p
220 return __arch_swahb32p(p
);
222 return __swahb32(*p
);
227 * __swab16s - byteswap a 16-bit value in-place
228 * @p: pointer to a naturally-aligned 16-bit value
230 static inline void __swab16s(__u16
*p
)
232 #ifdef __arch_swab16s
239 * __swab32s - byteswap a 32-bit value in-place
240 * @p: pointer to a naturally-aligned 32-bit value
242 static __always_inline
void __swab32s(__u32
*p
)
244 #ifdef __arch_swab32s
252 * __swab64s - byteswap a 64-bit value in-place
253 * @p: pointer to a naturally-aligned 64-bit value
255 static __always_inline
void __swab64s(__u64
*p
)
257 #ifdef __arch_swab64s
265 * __swahw32s - wordswap a 32-bit value in-place
266 * @p: pointer to a naturally-aligned 32-bit value
268 * See __swahw32() for details of wordswapping
270 static inline void __swahw32s(__u32
*p
)
272 #ifdef __arch_swahw32s
280 * __swahb32s - high and low byteswap a 32-bit value in-place
281 * @p: pointer to a naturally-aligned 32-bit value
283 * See __swahb32() for details of high and low byte swapping
285 static inline void __swahb32s(__u32
*p
)
287 #ifdef __arch_swahb32s
295 #endif /* _UAPI_LINUX_SWAB_H */