]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/uapi/linux/swab.h
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[mirror_ubuntu-bionic-kernel.git] / include / uapi / linux / swab.h
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_SWAB_H
3 #define _UAPI_LINUX_SWAB_H
4
5 #include <linux/types.h>
6 #include <linux/compiler.h>
7 #include <asm/swab.h>
8
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.
12 */
13 #define ___constant_swab16(x) ((__u16)( \
14 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
15 (((__u16)(x) & (__u16)0xff00U) >> 8)))
16
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)))
22
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)))
32
33 #define ___constant_swahw32(x) ((__u32)( \
34 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
35 (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
36
37 #define ___constant_swahb32(x) ((__u32)( \
38 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
39 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
40
41 /*
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
45 */
46
47 static inline __attribute_const__ __u16 __fswab16(__u16 val)
48 {
49 #if defined (__arch_swab16)
50 return __arch_swab16(val);
51 #else
52 return ___constant_swab16(val);
53 #endif
54 }
55
56 static inline __attribute_const__ __u32 __fswab32(__u32 val)
57 {
58 #if defined(__arch_swab32)
59 return __arch_swab32(val);
60 #else
61 return ___constant_swab32(val);
62 #endif
63 }
64
65 static inline __attribute_const__ __u64 __fswab64(__u64 val)
66 {
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
76 }
77
78 static inline __attribute_const__ __u32 __fswahw32(__u32 val)
79 {
80 #ifdef __arch_swahw32
81 return __arch_swahw32(val);
82 #else
83 return ___constant_swahw32(val);
84 #endif
85 }
86
87 static inline __attribute_const__ __u32 __fswahb32(__u32 val)
88 {
89 #ifdef __arch_swahb32
90 return __arch_swahb32(val);
91 #else
92 return ___constant_swahb32(val);
93 #endif
94 }
95
96 /**
97 * __swab16 - return a byteswapped 16-bit value
98 * @x: value to byteswap
99 */
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
108
109 /**
110 * __swab32 - return a byteswapped 32-bit value
111 * @x: value to byteswap
112 */
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
121
122 /**
123 * __swab64 - return a byteswapped 64-bit value
124 * @x: value to byteswap
125 */
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
134
135 /**
136 * __swahw32 - return a word-swapped 32-bit value
137 * @x: value to wordswap
138 *
139 * __swahw32(0x12340000) is 0x00001234
140 */
141 #define __swahw32(x) \
142 (__builtin_constant_p((__u32)(x)) ? \
143 ___constant_swahw32(x) : \
144 __fswahw32(x))
145
146 /**
147 * __swahb32 - return a high and low byte-swapped 32-bit value
148 * @x: value to byteswap
149 *
150 * __swahb32(0x12345678) is 0x34127856
151 */
152 #define __swahb32(x) \
153 (__builtin_constant_p((__u32)(x)) ? \
154 ___constant_swahb32(x) : \
155 __fswahb32(x))
156
157 /**
158 * __swab16p - return a byteswapped 16-bit value from a pointer
159 * @p: pointer to a naturally-aligned 16-bit value
160 */
161 static __always_inline __u16 __swab16p(const __u16 *p)
162 {
163 #ifdef __arch_swab16p
164 return __arch_swab16p(p);
165 #else
166 return __swab16(*p);
167 #endif
168 }
169
170 /**
171 * __swab32p - return a byteswapped 32-bit value from a pointer
172 * @p: pointer to a naturally-aligned 32-bit value
173 */
174 static __always_inline __u32 __swab32p(const __u32 *p)
175 {
176 #ifdef __arch_swab32p
177 return __arch_swab32p(p);
178 #else
179 return __swab32(*p);
180 #endif
181 }
182
183 /**
184 * __swab64p - return a byteswapped 64-bit value from a pointer
185 * @p: pointer to a naturally-aligned 64-bit value
186 */
187 static __always_inline __u64 __swab64p(const __u64 *p)
188 {
189 #ifdef __arch_swab64p
190 return __arch_swab64p(p);
191 #else
192 return __swab64(*p);
193 #endif
194 }
195
196 /**
197 * __swahw32p - return a wordswapped 32-bit value from a pointer
198 * @p: pointer to a naturally-aligned 32-bit value
199 *
200 * See __swahw32() for details of wordswapping.
201 */
202 static inline __u32 __swahw32p(const __u32 *p)
203 {
204 #ifdef __arch_swahw32p
205 return __arch_swahw32p(p);
206 #else
207 return __swahw32(*p);
208 #endif
209 }
210
211 /**
212 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
213 * @p: pointer to a naturally-aligned 32-bit value
214 *
215 * See __swahb32() for details of high/low byteswapping.
216 */
217 static inline __u32 __swahb32p(const __u32 *p)
218 {
219 #ifdef __arch_swahb32p
220 return __arch_swahb32p(p);
221 #else
222 return __swahb32(*p);
223 #endif
224 }
225
226 /**
227 * __swab16s - byteswap a 16-bit value in-place
228 * @p: pointer to a naturally-aligned 16-bit value
229 */
230 static inline void __swab16s(__u16 *p)
231 {
232 #ifdef __arch_swab16s
233 __arch_swab16s(p);
234 #else
235 *p = __swab16p(p);
236 #endif
237 }
238 /**
239 * __swab32s - byteswap a 32-bit value in-place
240 * @p: pointer to a naturally-aligned 32-bit value
241 */
242 static __always_inline void __swab32s(__u32 *p)
243 {
244 #ifdef __arch_swab32s
245 __arch_swab32s(p);
246 #else
247 *p = __swab32p(p);
248 #endif
249 }
250
251 /**
252 * __swab64s - byteswap a 64-bit value in-place
253 * @p: pointer to a naturally-aligned 64-bit value
254 */
255 static __always_inline void __swab64s(__u64 *p)
256 {
257 #ifdef __arch_swab64s
258 __arch_swab64s(p);
259 #else
260 *p = __swab64p(p);
261 #endif
262 }
263
264 /**
265 * __swahw32s - wordswap a 32-bit value in-place
266 * @p: pointer to a naturally-aligned 32-bit value
267 *
268 * See __swahw32() for details of wordswapping
269 */
270 static inline void __swahw32s(__u32 *p)
271 {
272 #ifdef __arch_swahw32s
273 __arch_swahw32s(p);
274 #else
275 *p = __swahw32p(p);
276 #endif
277 }
278
279 /**
280 * __swahb32s - high and low byteswap a 32-bit value in-place
281 * @p: pointer to a naturally-aligned 32-bit value
282 *
283 * See __swahb32() for details of high and low byte swapping
284 */
285 static inline void __swahb32s(__u32 *p)
286 {
287 #ifdef __arch_swahb32s
288 __arch_swahb32s(p);
289 #else
290 *p = __swahb32p(p);
291 #endif
292 }
293
294
295 #endif /* _UAPI_LINUX_SWAB_H */