1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_SWAB_H
3 #define _UAPI_LINUX_SWAB_H
6 * casts are necessary for constants, because we never know how for sure
7 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
9 #define ___constant_swab16(x) ((__u16)( \
10 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
11 (((__u16)(x) & (__u16)0xff00U) >> 8)))
13 #define ___constant_swab32(x) ((__u32)( \
14 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
15 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
16 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
17 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
19 #define ___constant_swab64(x) ((__u64)( \
20 (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
21 (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
22 (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
23 (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
24 (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
25 (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
26 (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
27 (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
29 #define ___constant_swahw32(x) ((__u32)( \
30 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
31 (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
33 #define ___constant_swahb32(x) ((__u32)( \
34 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
35 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
38 * Implement the following as inlines, but define the interface using
39 * macros to allow constant folding when possible:
40 * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
43 static inline __attribute_const__ __u16 __fswab16(__u16 val)
45 #if defined (__arch_swab16)
46 return __arch_swab16(val);
48 return ___constant_swab16(val);
52 static inline __attribute_const__ __u32 __fswab32(__u32 val)
54 #if defined(__arch_swab32)
55 return __arch_swab32(val);
57 return ___constant_swab32(val);
61 static inline __attribute_const__ __u64 __fswab64(__u64 val)
63 #if defined (__arch_swab64)
64 return __arch_swab64(val);
65 #elif defined(__SWAB_64_THRU_32__)
67 __u32 l = val & ((1ULL << 32) - 1);
68 return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h)));
70 return ___constant_swab64(val);
74 static inline __attribute_const__ __u32 __fswahw32(__u32 val)
77 return __arch_swahw32(val);
79 return ___constant_swahw32(val);
83 static inline __attribute_const__ __u32 __fswahb32(__u32 val)
86 return __arch_swahb32(val);
88 return ___constant_swahb32(val);
93 * __swab16 - return a byteswapped 16-bit value
94 * @x: value to byteswap
96 #ifdef __HAVE_BUILTIN_BSWAP16__
97 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
100 (__u16)(__builtin_constant_p(x) ? \
101 ___constant_swab16(x) : \
106 * __swab32 - return a byteswapped 32-bit value
107 * @x: value to byteswap
109 #ifdef __HAVE_BUILTIN_BSWAP32__
110 #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
112 #define __swab32(x) \
113 (__u32)(__builtin_constant_p(x) ? \
114 ___constant_swab32(x) : \
119 * __swab64 - return a byteswapped 64-bit value
120 * @x: value to byteswap
122 #ifdef __HAVE_BUILTIN_BSWAP64__
123 #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x))
125 #define __swab64(x) \
126 (__u64)(__builtin_constant_p(x) ? \
127 ___constant_swab64(x) : \
131 static __always_inline unsigned long __swab(const unsigned long y)
133 #if __BITS_PER_LONG == 64
135 #else /* __BITS_PER_LONG == 32 */
141 * __swahw32 - return a word-swapped 32-bit value
142 * @x: value to wordswap
144 * __swahw32(0x12340000) is 0x00001234
146 #define __swahw32(x) \
147 (__builtin_constant_p((__u32)(x)) ? \
148 ___constant_swahw32(x) : \
152 * __swahb32 - return a high and low byte-swapped 32-bit value
153 * @x: value to byteswap
155 * __swahb32(0x12345678) is 0x34127856
157 #define __swahb32(x) \
158 (__builtin_constant_p((__u32)(x)) ? \
159 ___constant_swahb32(x) : \
163 * __swab16p - return a byteswapped 16-bit value from a pointer
164 * @p: pointer to a naturally-aligned 16-bit value
166 static __always_inline __u16 __swab16p(const __u16 *p)
168 #ifdef __arch_swab16p
169 return __arch_swab16p(p);
176 * __swab32p - return a byteswapped 32-bit value from a pointer
177 * @p: pointer to a naturally-aligned 32-bit value
179 static __always_inline __u32 __swab32p(const __u32 *p)
181 #ifdef __arch_swab32p
182 return __arch_swab32p(p);
189 * __swab64p - return a byteswapped 64-bit value from a pointer
190 * @p: pointer to a naturally-aligned 64-bit value
192 static __always_inline __u64 __swab64p(const __u64 *p)
194 #ifdef __arch_swab64p
195 return __arch_swab64p(p);
202 * __swahw32p - return a wordswapped 32-bit value from a pointer
203 * @p: pointer to a naturally-aligned 32-bit value
205 * See __swahw32() for details of wordswapping.
207 static inline __u32 __swahw32p(const __u32 *p)
209 #ifdef __arch_swahw32p
210 return __arch_swahw32p(p);
212 return __swahw32(*p);
217 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
218 * @p: pointer to a naturally-aligned 32-bit value
220 * See __swahb32() for details of high/low byteswapping.
222 static inline __u32 __swahb32p(const __u32 *p)
224 #ifdef __arch_swahb32p
225 return __arch_swahb32p(p);
227 return __swahb32(*p);
232 * __swab16s - byteswap a 16-bit value in-place
233 * @p: pointer to a naturally-aligned 16-bit value
235 static inline void __swab16s(__u16 *p)
237 #ifdef __arch_swab16s
244 * __swab32s - byteswap a 32-bit value in-place
245 * @p: pointer to a naturally-aligned 32-bit value
247 static __always_inline void __swab32s(__u32 *p)
249 #ifdef __arch_swab32s
257 * __swab64s - byteswap a 64-bit value in-place
258 * @p: pointer to a naturally-aligned 64-bit value
260 static __always_inline void __swab64s(__u64 *p)
262 #ifdef __arch_swab64s
270 * __swahw32s - wordswap a 32-bit value in-place
271 * @p: pointer to a naturally-aligned 32-bit value
273 * See __swahw32() for details of wordswapping
275 static inline void __swahw32s(__u32 *p)
277 #ifdef __arch_swahw32s
285 * __swahb32s - high and low byteswap a 32-bit value in-place
286 * @p: pointer to a naturally-aligned 32-bit value
288 * See __swahb32() for details of high and low byte swapping
290 static inline void __swahb32s(__u32 *p)
292 #ifdef __arch_swahb32s
300 #endif /* _UAPI_LINUX_SWAB_H */