Commit | Line | Data |
---|---|---|
607ca46e DH |
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/swab.h> | |
7 | ||
8 | /* | |
9 | * casts are necessary for constants, because we never know how for sure | |
10 | * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way. | |
11 | */ | |
12 | #define ___constant_swab16(x) ((__u16)( \ | |
13 | (((__u16)(x) & (__u16)0x00ffU) << 8) | \ | |
14 | (((__u16)(x) & (__u16)0xff00U) >> 8))) | |
15 | ||
16 | #define ___constant_swab32(x) ((__u32)( \ | |
17 | (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \ | |
18 | (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \ | |
19 | (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \ | |
20 | (((__u32)(x) & (__u32)0xff000000UL) >> 24))) | |
21 | ||
22 | #define ___constant_swab64(x) ((__u64)( \ | |
23 | (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \ | |
24 | (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \ | |
25 | (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \ | |
26 | (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \ | |
27 | (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \ | |
28 | (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \ | |
29 | (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \ | |
30 | (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56))) | |
31 | ||
32 | #define ___constant_swahw32(x) ((__u32)( \ | |
33 | (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \ | |
34 | (((__u32)(x) & (__u32)0xffff0000UL) >> 16))) | |
35 | ||
36 | #define ___constant_swahb32(x) ((__u32)( \ | |
37 | (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \ | |
38 | (((__u32)(x) & (__u32)0xff00ff00UL) >> 8))) | |
39 | ||
40 | /* | |
41 | * Implement the following as inlines, but define the interface using | |
42 | * macros to allow constant folding when possible: | |
43 | * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32 | |
44 | */ | |
45 | ||
46 | static inline __attribute_const__ __u16 __fswab16(__u16 val) | |
47 | { | |
7322dd75 | 48 | #if defined (__arch_swab16) |
607ca46e DH |
49 | return __arch_swab16(val); |
50 | #else | |
51 | return ___constant_swab16(val); | |
52 | #endif | |
53 | } | |
54 | ||
55 | static inline __attribute_const__ __u32 __fswab32(__u32 val) | |
56 | { | |
7322dd75 | 57 | #if defined(__arch_swab32) |
607ca46e DH |
58 | return __arch_swab32(val); |
59 | #else | |
60 | return ___constant_swab32(val); | |
61 | #endif | |
62 | } | |
63 | ||
64 | static inline __attribute_const__ __u64 __fswab64(__u64 val) | |
65 | { | |
7322dd75 | 66 | #if defined (__arch_swab64) |
607ca46e DH |
67 | return __arch_swab64(val); |
68 | #elif defined(__SWAB_64_THRU_32__) | |
69 | __u32 h = val >> 32; | |
70 | __u32 l = val & ((1ULL << 32) - 1); | |
71 | return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h))); | |
72 | #else | |
73 | return ___constant_swab64(val); | |
74 | #endif | |
75 | } | |
76 | ||
77 | static inline __attribute_const__ __u32 __fswahw32(__u32 val) | |
78 | { | |
79 | #ifdef __arch_swahw32 | |
80 | return __arch_swahw32(val); | |
81 | #else | |
82 | return ___constant_swahw32(val); | |
83 | #endif | |
84 | } | |
85 | ||
86 | static inline __attribute_const__ __u32 __fswahb32(__u32 val) | |
87 | { | |
88 | #ifdef __arch_swahb32 | |
89 | return __arch_swahb32(val); | |
90 | #else | |
91 | return ___constant_swahb32(val); | |
92 | #endif | |
93 | } | |
94 | ||
95 | /** | |
96 | * __swab16 - return a byteswapped 16-bit value | |
97 | * @x: value to byteswap | |
98 | */ | |
7322dd75 AB |
99 | #ifdef __HAVE_BUILTIN_BSWAP16__ |
100 | #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x)) | |
101 | #else | |
607ca46e DH |
102 | #define __swab16(x) \ |
103 | (__builtin_constant_p((__u16)(x)) ? \ | |
104 | ___constant_swab16(x) : \ | |
105 | __fswab16(x)) | |
7322dd75 | 106 | #endif |
607ca46e DH |
107 | |
108 | /** | |
109 | * __swab32 - return a byteswapped 32-bit value | |
110 | * @x: value to byteswap | |
111 | */ | |
7322dd75 AB |
112 | #ifdef __HAVE_BUILTIN_BSWAP32__ |
113 | #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x)) | |
114 | #else | |
607ca46e DH |
115 | #define __swab32(x) \ |
116 | (__builtin_constant_p((__u32)(x)) ? \ | |
117 | ___constant_swab32(x) : \ | |
118 | __fswab32(x)) | |
7322dd75 | 119 | #endif |
607ca46e DH |
120 | |
121 | /** | |
122 | * __swab64 - return a byteswapped 64-bit value | |
123 | * @x: value to byteswap | |
124 | */ | |
7322dd75 AB |
125 | #ifdef __HAVE_BUILTIN_BSWAP64__ |
126 | #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x)) | |
127 | #else | |
607ca46e DH |
128 | #define __swab64(x) \ |
129 | (__builtin_constant_p((__u64)(x)) ? \ | |
130 | ___constant_swab64(x) : \ | |
131 | __fswab64(x)) | |
7322dd75 | 132 | #endif |
607ca46e DH |
133 | |
134 | /** | |
135 | * __swahw32 - return a word-swapped 32-bit value | |
136 | * @x: value to wordswap | |
137 | * | |
138 | * __swahw32(0x12340000) is 0x00001234 | |
139 | */ | |
140 | #define __swahw32(x) \ | |
141 | (__builtin_constant_p((__u32)(x)) ? \ | |
142 | ___constant_swahw32(x) : \ | |
143 | __fswahw32(x)) | |
144 | ||
145 | /** | |
146 | * __swahb32 - return a high and low byte-swapped 32-bit value | |
147 | * @x: value to byteswap | |
148 | * | |
149 | * __swahb32(0x12345678) is 0x34127856 | |
150 | */ | |
151 | #define __swahb32(x) \ | |
152 | (__builtin_constant_p((__u32)(x)) ? \ | |
153 | ___constant_swahb32(x) : \ | |
154 | __fswahb32(x)) | |
155 | ||
156 | /** | |
157 | * __swab16p - return a byteswapped 16-bit value from a pointer | |
158 | * @p: pointer to a naturally-aligned 16-bit value | |
159 | */ | |
bc27fb68 | 160 | static __always_inline __u16 __swab16p(const __u16 *p) |
607ca46e DH |
161 | { |
162 | #ifdef __arch_swab16p | |
163 | return __arch_swab16p(p); | |
164 | #else | |
165 | return __swab16(*p); | |
166 | #endif | |
167 | } | |
168 | ||
169 | /** | |
170 | * __swab32p - return a byteswapped 32-bit value from a pointer | |
171 | * @p: pointer to a naturally-aligned 32-bit value | |
172 | */ | |
bc27fb68 | 173 | static __always_inline __u32 __swab32p(const __u32 *p) |
607ca46e DH |
174 | { |
175 | #ifdef __arch_swab32p | |
176 | return __arch_swab32p(p); | |
177 | #else | |
178 | return __swab32(*p); | |
179 | #endif | |
180 | } | |
181 | ||
182 | /** | |
183 | * __swab64p - return a byteswapped 64-bit value from a pointer | |
184 | * @p: pointer to a naturally-aligned 64-bit value | |
185 | */ | |
bc27fb68 | 186 | static __always_inline __u64 __swab64p(const __u64 *p) |
607ca46e DH |
187 | { |
188 | #ifdef __arch_swab64p | |
189 | return __arch_swab64p(p); | |
190 | #else | |
191 | return __swab64(*p); | |
192 | #endif | |
193 | } | |
194 | ||
195 | /** | |
196 | * __swahw32p - return a wordswapped 32-bit value from a pointer | |
197 | * @p: pointer to a naturally-aligned 32-bit value | |
198 | * | |
199 | * See __swahw32() for details of wordswapping. | |
200 | */ | |
201 | static inline __u32 __swahw32p(const __u32 *p) | |
202 | { | |
203 | #ifdef __arch_swahw32p | |
204 | return __arch_swahw32p(p); | |
205 | #else | |
206 | return __swahw32(*p); | |
207 | #endif | |
208 | } | |
209 | ||
210 | /** | |
211 | * __swahb32p - return a high and low byteswapped 32-bit value from a pointer | |
212 | * @p: pointer to a naturally-aligned 32-bit value | |
213 | * | |
214 | * See __swahb32() for details of high/low byteswapping. | |
215 | */ | |
216 | static inline __u32 __swahb32p(const __u32 *p) | |
217 | { | |
218 | #ifdef __arch_swahb32p | |
219 | return __arch_swahb32p(p); | |
220 | #else | |
221 | return __swahb32(*p); | |
222 | #endif | |
223 | } | |
224 | ||
225 | /** | |
226 | * __swab16s - byteswap a 16-bit value in-place | |
227 | * @p: pointer to a naturally-aligned 16-bit value | |
228 | */ | |
229 | static inline void __swab16s(__u16 *p) | |
230 | { | |
231 | #ifdef __arch_swab16s | |
232 | __arch_swab16s(p); | |
233 | #else | |
234 | *p = __swab16p(p); | |
235 | #endif | |
236 | } | |
237 | /** | |
238 | * __swab32s - byteswap a 32-bit value in-place | |
239 | * @p: pointer to a naturally-aligned 32-bit value | |
240 | */ | |
bc27fb68 | 241 | static __always_inline void __swab32s(__u32 *p) |
607ca46e DH |
242 | { |
243 | #ifdef __arch_swab32s | |
244 | __arch_swab32s(p); | |
245 | #else | |
246 | *p = __swab32p(p); | |
247 | #endif | |
248 | } | |
249 | ||
250 | /** | |
251 | * __swab64s - byteswap a 64-bit value in-place | |
252 | * @p: pointer to a naturally-aligned 64-bit value | |
253 | */ | |
bc27fb68 | 254 | static __always_inline void __swab64s(__u64 *p) |
607ca46e DH |
255 | { |
256 | #ifdef __arch_swab64s | |
257 | __arch_swab64s(p); | |
258 | #else | |
259 | *p = __swab64p(p); | |
260 | #endif | |
261 | } | |
262 | ||
263 | /** | |
264 | * __swahw32s - wordswap a 32-bit value in-place | |
265 | * @p: pointer to a naturally-aligned 32-bit value | |
266 | * | |
267 | * See __swahw32() for details of wordswapping | |
268 | */ | |
269 | static inline void __swahw32s(__u32 *p) | |
270 | { | |
271 | #ifdef __arch_swahw32s | |
272 | __arch_swahw32s(p); | |
273 | #else | |
274 | *p = __swahw32p(p); | |
275 | #endif | |
276 | } | |
277 | ||
278 | /** | |
279 | * __swahb32s - high and low byteswap a 32-bit value in-place | |
280 | * @p: pointer to a naturally-aligned 32-bit value | |
281 | * | |
282 | * See __swahb32() for details of high and low byte swapping | |
283 | */ | |
284 | static inline void __swahb32s(__u32 *p) | |
285 | { | |
286 | #ifdef __arch_swahb32s | |
287 | __arch_swahb32s(p); | |
288 | #else | |
289 | *p = __swahb32p(p); | |
290 | #endif | |
291 | } | |
292 | ||
293 | ||
294 | #endif /* _UAPI_LINUX_SWAB_H */ |