GNU Linux-libre 4.9.333-gnu1
[releases.git] / include / uapi / linux / swab.h
1 #ifndef _UAPI_LINUX_SWAB_H
2 #define _UAPI_LINUX_SWAB_H
3
4 #include <linux/types.h>
5 #include <linux/compiler.h>
6 #include <asm/bitsperlong.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 static __always_inline unsigned long __swab(const unsigned long y)
136 {
137 #if __BITS_PER_LONG == 64
138         return __swab64(y);
139 #else /* __BITS_PER_LONG == 32 */
140         return __swab32(y);
141 #endif
142 }
143
144 /**
145  * __swahw32 - return a word-swapped 32-bit value
146  * @x: value to wordswap
147  *
148  * __swahw32(0x12340000) is 0x00001234
149  */
150 #define __swahw32(x)                            \
151         (__builtin_constant_p((__u32)(x)) ?     \
152         ___constant_swahw32(x) :                \
153         __fswahw32(x))
154
155 /**
156  * __swahb32 - return a high and low byte-swapped 32-bit value
157  * @x: value to byteswap
158  *
159  * __swahb32(0x12345678) is 0x34127856
160  */
161 #define __swahb32(x)                            \
162         (__builtin_constant_p((__u32)(x)) ?     \
163         ___constant_swahb32(x) :                \
164         __fswahb32(x))
165
166 /**
167  * __swab16p - return a byteswapped 16-bit value from a pointer
168  * @p: pointer to a naturally-aligned 16-bit value
169  */
170 static __always_inline __u16 __swab16p(const __u16 *p)
171 {
172 #ifdef __arch_swab16p
173         return __arch_swab16p(p);
174 #else
175         return __swab16(*p);
176 #endif
177 }
178
179 /**
180  * __swab32p - return a byteswapped 32-bit value from a pointer
181  * @p: pointer to a naturally-aligned 32-bit value
182  */
183 static __always_inline __u32 __swab32p(const __u32 *p)
184 {
185 #ifdef __arch_swab32p
186         return __arch_swab32p(p);
187 #else
188         return __swab32(*p);
189 #endif
190 }
191
192 /**
193  * __swab64p - return a byteswapped 64-bit value from a pointer
194  * @p: pointer to a naturally-aligned 64-bit value
195  */
196 static __always_inline __u64 __swab64p(const __u64 *p)
197 {
198 #ifdef __arch_swab64p
199         return __arch_swab64p(p);
200 #else
201         return __swab64(*p);
202 #endif
203 }
204
205 /**
206  * __swahw32p - return a wordswapped 32-bit value from a pointer
207  * @p: pointer to a naturally-aligned 32-bit value
208  *
209  * See __swahw32() for details of wordswapping.
210  */
211 static inline __u32 __swahw32p(const __u32 *p)
212 {
213 #ifdef __arch_swahw32p
214         return __arch_swahw32p(p);
215 #else
216         return __swahw32(*p);
217 #endif
218 }
219
220 /**
221  * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
222  * @p: pointer to a naturally-aligned 32-bit value
223  *
224  * See __swahb32() for details of high/low byteswapping.
225  */
226 static inline __u32 __swahb32p(const __u32 *p)
227 {
228 #ifdef __arch_swahb32p
229         return __arch_swahb32p(p);
230 #else
231         return __swahb32(*p);
232 #endif
233 }
234
235 /**
236  * __swab16s - byteswap a 16-bit value in-place
237  * @p: pointer to a naturally-aligned 16-bit value
238  */
239 static inline void __swab16s(__u16 *p)
240 {
241 #ifdef __arch_swab16s
242         __arch_swab16s(p);
243 #else
244         *p = __swab16p(p);
245 #endif
246 }
247 /**
248  * __swab32s - byteswap a 32-bit value in-place
249  * @p: pointer to a naturally-aligned 32-bit value
250  */
251 static __always_inline void __swab32s(__u32 *p)
252 {
253 #ifdef __arch_swab32s
254         __arch_swab32s(p);
255 #else
256         *p = __swab32p(p);
257 #endif
258 }
259
260 /**
261  * __swab64s - byteswap a 64-bit value in-place
262  * @p: pointer to a naturally-aligned 64-bit value
263  */
264 static __always_inline void __swab64s(__u64 *p)
265 {
266 #ifdef __arch_swab64s
267         __arch_swab64s(p);
268 #else
269         *p = __swab64p(p);
270 #endif
271 }
272
273 /**
274  * __swahw32s - wordswap a 32-bit value in-place
275  * @p: pointer to a naturally-aligned 32-bit value
276  *
277  * See __swahw32() for details of wordswapping
278  */
279 static inline void __swahw32s(__u32 *p)
280 {
281 #ifdef __arch_swahw32s
282         __arch_swahw32s(p);
283 #else
284         *p = __swahw32p(p);
285 #endif
286 }
287
288 /**
289  * __swahb32s - high and low byteswap a 32-bit value in-place
290  * @p: pointer to a naturally-aligned 32-bit value
291  *
292  * See __swahb32() for details of high and low byte swapping
293  */
294 static inline void __swahb32s(__u32 *p)
295 {
296 #ifdef __arch_swahb32s
297         __arch_swahb32s(p);
298 #else
299         *p = __swahb32p(p);
300 #endif
301 }
302
303
304 #endif /* _UAPI_LINUX_SWAB_H */