4 #include <linux/types.h>
5 #include <linux/compiler.h>
6 #include <asm/byteorder.h>
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
)
49 return __arch_swab16(val
);
50 #elif defined(__arch_swab16p)
51 return __arch_swab16p(&val
);
53 return __const_swab16(val
);
57 static inline __attribute_const__ __u32
___swab32(__u32 val
)
60 return __arch_swab32(val
);
61 #elif defined(__arch_swab32p)
62 return __arch_swab32p(&val
);
64 return __const_swab32(val
);
68 static inline __attribute_const__ __u64
___swab64(__u64 val
)
71 return __arch_swab64(val
);
72 #elif defined(__arch_swab64p)
73 return __arch_swab64p(&val
);
74 #elif defined(__SWAB_64_THRU_32__)
76 __u32 l
= val
& ((1ULL << 32) - 1);
77 return (((__u64
)___swab32(l
)) << 32) | ((__u64
)(___swab32(h
)));
79 return __const_swab64(val
);
83 static inline __attribute_const__ __u32
___swahw32(__u32 val
)
86 return __arch_swahw32(val
);
87 #elif defined(__arch_swahw32p)
88 return __arch_swahw32p(&val
);
90 return __const_swahw32(val
);
94 static inline __attribute_const__ __u32
___swahb32(__u32 val
)
97 return __arch_swahb32(val
);
98 #elif defined(__arch_swahb32p)
99 return __arch_swahb32p(&val
);
101 return __const_swahb32(val
);
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)) : \
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)) : \
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)) : \
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)) : \
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)) : \
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
);
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
);
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
);
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
);
204 return __swahw32(*p
);
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
);
219 return __swahb32(*p
);
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
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
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
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
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
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 */