HID: fix lock imbalance in hiddev
[linux-2.6/mini2440.git] / include / linux / swab.h
blob270d5c208a899aaeee19148a8a39109d59e384a8
1 #ifndef _LINUX_SWAB_H
2 #define _LINUX_SWAB_H
4 #include <linux/types.h>
5 #include <linux/compiler.h>
6 #include <asm/byteorder.h>
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.
12 #define __const_swab16(x) ((__u16)( \
13 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
14 (((__u16)(x) & (__u16)0xff00U) >> 8)))
16 #define __const_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)))
22 #define __const_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)))
32 #define __const_swahw32(x) ((__u32)( \
33 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
34 (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
36 #define __const_swahb32(x) ((__u32)( \
37 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
38 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
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
46 static inline __attribute_const__ __u16 ___swab16(__u16 val)
48 #ifdef __arch_swab16
49 return __arch_swab16(val);
50 #elif defined(__arch_swab16p)
51 return __arch_swab16p(&val);
52 #else
53 return __const_swab16(val);
54 #endif
57 static inline __attribute_const__ __u32 ___swab32(__u32 val)
59 #ifdef __arch_swab32
60 return __arch_swab32(val);
61 #elif defined(__arch_swab32p)
62 return __arch_swab32p(&val);
63 #else
64 return __const_swab32(val);
65 #endif
68 static inline __attribute_const__ __u64 ___swab64(__u64 val)
70 #ifdef __arch_swab64
71 return __arch_swab64(val);
72 #elif defined(__arch_swab64p)
73 return __arch_swab64p(&val);
74 #elif defined(__SWAB_64_THRU_32__)
75 __u32 h = val >> 32;
76 __u32 l = val & ((1ULL << 32) - 1);
77 return (((__u64)___swab32(l)) << 32) | ((__u64)(___swab32(h)));
78 #else
79 return __const_swab64(val);
80 #endif
83 static inline __attribute_const__ __u32 ___swahw32(__u32 val)
85 #ifdef __arch_swahw32
86 return __arch_swahw32(val);
87 #elif defined(__arch_swahw32p)
88 return __arch_swahw32p(&val);
89 #else
90 return __const_swahw32(val);
91 #endif
94 static inline __attribute_const__ __u32 ___swahb32(__u32 val)
96 #ifdef __arch_swahb32
97 return __arch_swahb32(val);
98 #elif defined(__arch_swahb32p)
99 return __arch_swahb32p(&val);
100 #else
101 return __const_swahb32(val);
102 #endif
106 * __swab16 - return a byteswapped 16-bit value
107 * @x: value to byteswap
109 #define __swab16(x) \
110 (__builtin_constant_p((__u16)(x)) ? \
111 __const_swab16((x)) : \
112 ___swab16((x)))
115 * __swab32 - return a byteswapped 32-bit value
116 * @x: value to byteswap
118 #define __swab32(x) \
119 (__builtin_constant_p((__u32)(x)) ? \
120 __const_swab32((x)) : \
121 ___swab32((x)))
124 * __swab64 - return a byteswapped 64-bit value
125 * @x: value to byteswap
127 #define __swab64(x) \
128 (__builtin_constant_p((__u64)(x)) ? \
129 __const_swab64((x)) : \
130 ___swab64((x)))
133 * __swahw32 - return a word-swapped 32-bit value
134 * @x: value to wordswap
136 * __swahw32(0x12340000) is 0x00001234
138 #define __swahw32(x) \
139 (__builtin_constant_p((__u32)(x)) ? \
140 __const_swahw32((x)) : \
141 ___swahw32((x)))
144 * __swahb32 - return a high and low byte-swapped 32-bit value
145 * @x: value to byteswap
147 * __swahb32(0x12345678) is 0x34127856
149 #define __swahb32(x) \
150 (__builtin_constant_p((__u32)(x)) ? \
151 __const_swahb32((x)) : \
152 ___swahb32((x)))
155 * __swab16p - return a byteswapped 16-bit value from a pointer
156 * @p: pointer to a naturally-aligned 16-bit value
158 static inline __u16 __swab16p(const __u16 *p)
160 #ifdef __arch_swab16p
161 return __arch_swab16p(p);
162 #else
163 return __swab16(*p);
164 #endif
168 * __swab32p - return a byteswapped 32-bit value from a pointer
169 * @p: pointer to a naturally-aligned 32-bit value
171 static inline __u32 __swab32p(const __u32 *p)
173 #ifdef __arch_swab32p
174 return __arch_swab32p(p);
175 #else
176 return __swab32(*p);
177 #endif
181 * __swab64p - return a byteswapped 64-bit value from a pointer
182 * @p: pointer to a naturally-aligned 64-bit value
184 static inline __u64 __swab64p(const __u64 *p)
186 #ifdef __arch_swab64p
187 return __arch_swab64p(p);
188 #else
189 return __swab64(*p);
190 #endif
194 * __swahw32p - return a wordswapped 32-bit value from a pointer
195 * @p: pointer to a naturally-aligned 32-bit value
197 * See __swahw32() for details of wordswapping.
199 static inline __u32 __swahw32p(const __u32 *p)
201 #ifdef __arch_swahw32p
202 return __arch_swahw32p(p);
203 #else
204 return __swahw32(*p);
205 #endif
209 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
210 * @p: pointer to a naturally-aligned 32-bit value
212 * See __swahb32() for details of high/low byteswapping.
214 static inline __u32 __swahb32p(const __u32 *p)
216 #ifdef __arch_swahb32p
217 return __arch_swahb32p(p);
218 #else
219 return __swahb32(*p);
220 #endif
224 * __swab16s - byteswap a 16-bit value in-place
225 * @p: pointer to a naturally-aligned 16-bit value
227 static inline void __swab16s(__u16 *p)
229 #ifdef __arch_swab16s
230 __arch_swab16s(p);
231 #else
232 *p = __swab16p(p);
233 #endif
236 * __swab32s - byteswap a 32-bit value in-place
237 * @p: pointer to a naturally-aligned 32-bit value
239 static inline void __swab32s(__u32 *p)
241 #ifdef __arch_swab32s
242 __arch_swab32s(p);
243 #else
244 *p = __swab32p(p);
245 #endif
249 * __swab64s - byteswap a 64-bit value in-place
250 * @p: pointer to a naturally-aligned 64-bit value
252 static inline void __swab64s(__u64 *p)
254 #ifdef __arch_swab64s
255 __arch_swab64s(p);
256 #else
257 *p = __swab64p(p);
258 #endif
262 * __swahw32s - wordswap a 32-bit value in-place
263 * @p: pointer to a naturally-aligned 32-bit value
265 * See __swahw32() for details of wordswapping
267 static inline void __swahw32s(__u32 *p)
269 #ifdef __arch_swahw32s
270 __arch_swahw32s(p);
271 #else
272 *p = __swahw32p(p);
273 #endif
277 * __swahb32s - high and low byteswap a 32-bit value in-place
278 * @p: pointer to a naturally-aligned 32-bit value
280 * See __swahb32() for details of high and low byte swapping
282 static inline void __swahb32s(__u32 *p)
284 #ifdef __arch_swahb32s
285 __arch_swahb32s(p);
286 #else
287 *p = __swahb32p(p);
288 #endif
291 #ifdef __KERNEL__
292 # define swab16 __swab16
293 # define swab32 __swab32
294 # define swab64 __swab64
295 # define swahw32 __swahw32
296 # define swahb32 __swahb32
297 # define swab16p __swab16p
298 # define swab32p __swab32p
299 # define swab64p __swab64p
300 # define swahw32p __swahw32p
301 # define swahb32p __swahb32p
302 # define swab16s __swab16s
303 # define swab32s __swab32s
304 # define swab64s __swab64s
305 # define swahw32s __swahw32s
306 # define swahb32s __swahb32s
307 #endif /* __KERNEL__ */
309 #endif /* _LINUX_SWAB_H */