1 #ifndef _UAPI_LINUX_SWAB_H
2 #define _UAPI_LINUX_SWAB_H
4 #include <linux/types.h>
5 #include <linux/compiler.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 ___constant_swab16(x) ((__u16)( \
13 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
14 (((__u16)(x) & (__u16)0xff00U) >> 8)))
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)))
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)))
32 #define ___constant_swahw32(x) ((__u32)( \
33 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
34 (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
36 #define ___constant_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
__fswab16(__u16 val
)
48 #if defined (__arch_swab16)
49 return __arch_swab16(val
);
51 return ___constant_swab16(val
);
55 static inline __attribute_const__ __u32
__fswab32(__u32 val
)
57 #if defined(__arch_swab32)
58 return __arch_swab32(val
);
60 return ___constant_swab32(val
);
64 static inline __attribute_const__ __u64
__fswab64(__u64 val
)
66 #if defined (__arch_swab64)
67 return __arch_swab64(val
);
68 #elif defined(__SWAB_64_THRU_32__)
70 __u32 l
= val
& ((1ULL << 32) - 1);
71 return (((__u64
)__fswab32(l
)) << 32) | ((__u64
)(__fswab32(h
)));
73 return ___constant_swab64(val
);
77 static inline __attribute_const__ __u32
__fswahw32(__u32 val
)
80 return __arch_swahw32(val
);
82 return ___constant_swahw32(val
);
86 static inline __attribute_const__ __u32
__fswahb32(__u32 val
)
89 return __arch_swahb32(val
);
91 return ___constant_swahb32(val
);
96 * __swab16 - return a byteswapped 16-bit value
97 * @x: value to byteswap
99 #ifdef __HAVE_BUILTIN_BSWAP16__
100 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
102 #define __swab16(x) \
103 (__builtin_constant_p((__u16)(x)) ? \
104 ___constant_swab16(x) : \
109 * __swab32 - return a byteswapped 32-bit value
110 * @x: value to byteswap
112 #ifdef __HAVE_BUILTIN_BSWAP32__
113 #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
115 #define __swab32(x) \
116 (__builtin_constant_p((__u32)(x)) ? \
117 ___constant_swab32(x) : \
122 * __swab64 - return a byteswapped 64-bit value
123 * @x: value to byteswap
125 #ifdef __HAVE_BUILTIN_BSWAP64__
126 #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x))
128 #define __swab64(x) \
129 (__builtin_constant_p((__u64)(x)) ? \
130 ___constant_swab64(x) : \
135 * __swahw32 - return a word-swapped 32-bit value
136 * @x: value to wordswap
138 * __swahw32(0x12340000) is 0x00001234
140 #define __swahw32(x) \
141 (__builtin_constant_p((__u32)(x)) ? \
142 ___constant_swahw32(x) : \
146 * __swahb32 - return a high and low byte-swapped 32-bit value
147 * @x: value to byteswap
149 * __swahb32(0x12345678) is 0x34127856
151 #define __swahb32(x) \
152 (__builtin_constant_p((__u32)(x)) ? \
153 ___constant_swahb32(x) : \
157 * __swab16p - return a byteswapped 16-bit value from a pointer
158 * @p: pointer to a naturally-aligned 16-bit value
160 static __always_inline __u16
__swab16p(const __u16
*p
)
162 #ifdef __arch_swab16p
163 return __arch_swab16p(p
);
170 * __swab32p - return a byteswapped 32-bit value from a pointer
171 * @p: pointer to a naturally-aligned 32-bit value
173 static __always_inline __u32
__swab32p(const __u32
*p
)
175 #ifdef __arch_swab32p
176 return __arch_swab32p(p
);
183 * __swab64p - return a byteswapped 64-bit value from a pointer
184 * @p: pointer to a naturally-aligned 64-bit value
186 static __always_inline __u64
__swab64p(const __u64
*p
)
188 #ifdef __arch_swab64p
189 return __arch_swab64p(p
);
196 * __swahw32p - return a wordswapped 32-bit value from a pointer
197 * @p: pointer to a naturally-aligned 32-bit value
199 * See __swahw32() for details of wordswapping.
201 static inline __u32
__swahw32p(const __u32
*p
)
203 #ifdef __arch_swahw32p
204 return __arch_swahw32p(p
);
206 return __swahw32(*p
);
211 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
212 * @p: pointer to a naturally-aligned 32-bit value
214 * See __swahb32() for details of high/low byteswapping.
216 static inline __u32
__swahb32p(const __u32
*p
)
218 #ifdef __arch_swahb32p
219 return __arch_swahb32p(p
);
221 return __swahb32(*p
);
226 * __swab16s - byteswap a 16-bit value in-place
227 * @p: pointer to a naturally-aligned 16-bit value
229 static inline void __swab16s(__u16
*p
)
231 #ifdef __arch_swab16s
238 * __swab32s - byteswap a 32-bit value in-place
239 * @p: pointer to a naturally-aligned 32-bit value
241 static __always_inline
void __swab32s(__u32
*p
)
243 #ifdef __arch_swab32s
251 * __swab64s - byteswap a 64-bit value in-place
252 * @p: pointer to a naturally-aligned 64-bit value
254 static __always_inline
void __swab64s(__u64
*p
)
256 #ifdef __arch_swab64s
264 * __swahw32s - wordswap a 32-bit value in-place
265 * @p: pointer to a naturally-aligned 32-bit value
267 * See __swahw32() for details of wordswapping
269 static inline void __swahw32s(__u32
*p
)
271 #ifdef __arch_swahw32s
279 * __swahb32s - high and low byteswap a 32-bit value in-place
280 * @p: pointer to a naturally-aligned 32-bit value
282 * See __swahb32() for details of high and low byte swapping
284 static inline void __swahb32s(__u32
*p
)
286 #ifdef __arch_swahb32s
294 #endif /* _UAPI_LINUX_SWAB_H */