License cleanup: add SPDX license identifier to uapi header files with no license
[linux/fpc-iii.git] / include / uapi / linux / swab.h
blob23cd84868cc3bd8907994664c82407ca49a1d8d9
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>
7 #include <asm/swab.h>
9 /*
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);
51 #else
52 return ___constant_swab16(val);
53 #endif
56 static inline __attribute_const__ __u32 __fswab32(__u32 val)
58 #if defined(__arch_swab32)
59 return __arch_swab32(val);
60 #else
61 return ___constant_swab32(val);
62 #endif
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__)
70 __u32 h = val >> 32;
71 __u32 l = val & ((1ULL << 32) - 1);
72 return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h)));
73 #else
74 return ___constant_swab64(val);
75 #endif
78 static inline __attribute_const__ __u32 __fswahw32(__u32 val)
80 #ifdef __arch_swahw32
81 return __arch_swahw32(val);
82 #else
83 return ___constant_swahw32(val);
84 #endif
87 static inline __attribute_const__ __u32 __fswahb32(__u32 val)
89 #ifdef __arch_swahb32
90 return __arch_swahb32(val);
91 #else
92 return ___constant_swahb32(val);
93 #endif
96 /**
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))
102 #else
103 #define __swab16(x) \
104 (__builtin_constant_p((__u16)(x)) ? \
105 ___constant_swab16(x) : \
106 __fswab16(x))
107 #endif
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))
115 #else
116 #define __swab32(x) \
117 (__builtin_constant_p((__u32)(x)) ? \
118 ___constant_swab32(x) : \
119 __fswab32(x))
120 #endif
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))
128 #else
129 #define __swab64(x) \
130 (__builtin_constant_p((__u64)(x)) ? \
131 ___constant_swab64(x) : \
132 __fswab64(x))
133 #endif
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) : \
144 __fswahw32(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) : \
155 __fswahb32(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);
165 #else
166 return __swab16(*p);
167 #endif
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);
178 #else
179 return __swab32(*p);
180 #endif
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);
191 #else
192 return __swab64(*p);
193 #endif
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);
206 #else
207 return __swahw32(*p);
208 #endif
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);
221 #else
222 return __swahb32(*p);
223 #endif
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
233 __arch_swab16s(p);
234 #else
235 *p = __swab16p(p);
236 #endif
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
245 __arch_swab32s(p);
246 #else
247 *p = __swab32p(p);
248 #endif
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
258 __arch_swab64s(p);
259 #else
260 *p = __swab64p(p);
261 #endif
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
273 __arch_swahw32s(p);
274 #else
275 *p = __swahw32p(p);
276 #endif
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
288 __arch_swahb32s(p);
289 #else
290 *p = __swahb32p(p);
291 #endif
295 #endif /* _UAPI_LINUX_SWAB_H */