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/stddef.h>
7 #include <asm/bitsperlong.h>
11 * casts are necessary for constants, because we never know how for sure
12 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
14 #define ___constant_swab16(x) ((__u16)( \
15 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
16 (((__u16)(x) & (__u16)0xff00U) >> 8)))
18 #define ___constant_swab32(x) ((__u32)( \
19 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
20 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
21 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
22 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
24 #define ___constant_swab64(x) ((__u64)( \
25 (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
26 (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
27 (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
28 (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
29 (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
30 (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
31 (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
32 (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
34 #define ___constant_swahw32(x) ((__u32)( \
35 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
36 (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
38 #define ___constant_swahb32(x) ((__u32)( \
39 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
40 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
43 * Implement the following as inlines, but define the interface using
44 * macros to allow constant folding when possible:
45 * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
48 static inline __attribute_const__ __u16
__fswab16(__u16 val
)
50 #if defined (__arch_swab16)
51 return __arch_swab16(val
);
53 return ___constant_swab16(val
);
57 static inline __attribute_const__ __u32
__fswab32(__u32 val
)
59 #if defined(__arch_swab32)
60 return __arch_swab32(val
);
62 return ___constant_swab32(val
);
66 static inline __attribute_const__ __u64
__fswab64(__u64 val
)
68 #if defined (__arch_swab64)
69 return __arch_swab64(val
);
70 #elif defined(__SWAB_64_THRU_32__)
72 __u32 l
= val
& ((1ULL << 32) - 1);
73 return (((__u64
)__fswab32(l
)) << 32) | ((__u64
)(__fswab32(h
)));
75 return ___constant_swab64(val
);
79 static inline __attribute_const__ __u32
__fswahw32(__u32 val
)
82 return __arch_swahw32(val
);
84 return ___constant_swahw32(val
);
88 static inline __attribute_const__ __u32
__fswahb32(__u32 val
)
91 return __arch_swahb32(val
);
93 return ___constant_swahb32(val
);
98 * __swab16 - return a byteswapped 16-bit value
99 * @x: value to byteswap
101 #ifdef __HAVE_BUILTIN_BSWAP16__
102 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
104 #define __swab16(x) \
105 (__u16)(__builtin_constant_p(x) ? \
106 ___constant_swab16(x) : \
111 * __swab32 - return a byteswapped 32-bit value
112 * @x: value to byteswap
114 #ifdef __HAVE_BUILTIN_BSWAP32__
115 #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
117 #define __swab32(x) \
118 (__u32)(__builtin_constant_p(x) ? \
119 ___constant_swab32(x) : \
124 * __swab64 - return a byteswapped 64-bit value
125 * @x: value to byteswap
127 #ifdef __HAVE_BUILTIN_BSWAP64__
128 #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x))
130 #define __swab64(x) \
131 (__u64)(__builtin_constant_p(x) ? \
132 ___constant_swab64(x) : \
136 static __always_inline
unsigned long __swab(const unsigned long y
)
138 #if __BITS_PER_LONG == 64
140 #else /* __BITS_PER_LONG == 32 */
146 * __swahw32 - return a word-swapped 32-bit value
147 * @x: value to wordswap
149 * __swahw32(0x12340000) is 0x00001234
151 #define __swahw32(x) \
152 (__builtin_constant_p((__u32)(x)) ? \
153 ___constant_swahw32(x) : \
157 * __swahb32 - return a high and low byte-swapped 32-bit value
158 * @x: value to byteswap
160 * __swahb32(0x12345678) is 0x34127856
162 #define __swahb32(x) \
163 (__builtin_constant_p((__u32)(x)) ? \
164 ___constant_swahb32(x) : \
168 * __swab16p - return a byteswapped 16-bit value from a pointer
169 * @p: pointer to a naturally-aligned 16-bit value
171 static __always_inline __u16
__swab16p(const __u16
*p
)
173 #ifdef __arch_swab16p
174 return __arch_swab16p(p
);
181 * __swab32p - return a byteswapped 32-bit value from a pointer
182 * @p: pointer to a naturally-aligned 32-bit value
184 static __always_inline __u32
__swab32p(const __u32
*p
)
186 #ifdef __arch_swab32p
187 return __arch_swab32p(p
);
194 * __swab64p - return a byteswapped 64-bit value from a pointer
195 * @p: pointer to a naturally-aligned 64-bit value
197 static __always_inline __u64
__swab64p(const __u64
*p
)
199 #ifdef __arch_swab64p
200 return __arch_swab64p(p
);
207 * __swahw32p - return a wordswapped 32-bit value from a pointer
208 * @p: pointer to a naturally-aligned 32-bit value
210 * See __swahw32() for details of wordswapping.
212 static inline __u32
__swahw32p(const __u32
*p
)
214 #ifdef __arch_swahw32p
215 return __arch_swahw32p(p
);
217 return __swahw32(*p
);
222 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
223 * @p: pointer to a naturally-aligned 32-bit value
225 * See __swahb32() for details of high/low byteswapping.
227 static inline __u32
__swahb32p(const __u32
*p
)
229 #ifdef __arch_swahb32p
230 return __arch_swahb32p(p
);
232 return __swahb32(*p
);
237 * __swab16s - byteswap a 16-bit value in-place
238 * @p: pointer to a naturally-aligned 16-bit value
240 static inline void __swab16s(__u16
*p
)
242 #ifdef __arch_swab16s
249 * __swab32s - byteswap a 32-bit value in-place
250 * @p: pointer to a naturally-aligned 32-bit value
252 static __always_inline
void __swab32s(__u32
*p
)
254 #ifdef __arch_swab32s
262 * __swab64s - byteswap a 64-bit value in-place
263 * @p: pointer to a naturally-aligned 64-bit value
265 static __always_inline
void __swab64s(__u64
*p
)
267 #ifdef __arch_swab64s
275 * __swahw32s - wordswap a 32-bit value in-place
276 * @p: pointer to a naturally-aligned 32-bit value
278 * See __swahw32() for details of wordswapping
280 static inline void __swahw32s(__u32
*p
)
282 #ifdef __arch_swahw32s
290 * __swahb32s - high and low byteswap a 32-bit value in-place
291 * @p: pointer to a naturally-aligned 32-bit value
293 * See __swahb32() for details of high and low byte swapping
295 static inline void __swahb32s(__u32
*p
)
297 #ifdef __arch_swahb32s
305 #endif /* _UAPI_LINUX_SWAB_H */