1 #ifndef _LINUX_BYTEORDER_SWAB_H
2 #define _LINUX_BYTEORDER_SWAB_H
5 * linux/byteorder/swab.h
6 * Byte-swapping, independently from CPU endianness
9 * Francois-Rene Rideau <fare@tunes.org> 19971205
10 * separated swab functions from cpu_to_XX,
11 * to clean up support for bizarre-endian architectures.
13 * See asm-i386/byteorder.h and suches for examples of how to provide
14 * architecture-dependent optimized versions
18 #include <linux/compiler.h>
20 /* casts are necessary for constants, because we never know how for sure
21 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
23 #define ___swab16(x) \
27 (((__u16)(__x) & (__u16)0x00ffU) << 8) | \
28 (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
31 #define ___swab32(x) \
35 (((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
36 (((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \
37 (((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \
38 (((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
41 #define ___swab64(x) \
45 (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
46 (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
47 (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
48 (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) << 8) | \
49 (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >> 8) | \
50 (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
51 (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
52 (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
55 #define ___constant_swab16(x) \
57 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
58 (((__u16)(x) & (__u16)0xff00U) >> 8) ))
59 #define ___constant_swab32(x) \
61 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
62 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
63 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
64 (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
65 #define ___constant_swab64(x) \
67 (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
68 (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
69 (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
70 (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
71 (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
72 (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
73 (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
74 (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
77 * provide defaults when no architecture-specific optimization is detected
79 #ifndef __arch__swab16
80 # define __arch__swab16(x) ({ __u16 __tmp = (x) ; ___swab16(__tmp); })
82 #ifndef __arch__swab32
83 # define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); })
85 #ifndef __arch__swab64
86 # define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); })
89 #ifndef __arch__swab16p
90 # define __arch__swab16p(x) __arch__swab16(*(x))
92 #ifndef __arch__swab32p
93 # define __arch__swab32p(x) __arch__swab32(*(x))
95 #ifndef __arch__swab64p
96 # define __arch__swab64p(x) __arch__swab64(*(x))
99 #ifndef __arch__swab16s
100 # define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0)
102 #ifndef __arch__swab32s
103 # define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0)
105 #ifndef __arch__swab64s
106 # define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0)
111 * Allow constant folding
113 #if defined(__GNUC__) && defined(__OPTIMIZE__)
114 # define __swab16(x) \
115 (__builtin_constant_p((__u16)(x)) ? \
118 # define __swab32(x) \
119 (__builtin_constant_p((__u32)(x)) ? \
122 # define __swab64(x) \
123 (__builtin_constant_p((__u64)(x)) ? \
127 # define __swab16(x) __fswab16(x)
128 # define __swab32(x) __fswab32(x)
129 # define __swab64(x) __fswab64(x)
130 #endif /* OPTIMIZE */
133 static __inline__ __attribute_const__ __u16 __fswab16(__u16 x)
135 return __arch__swab16(x);
137 static __inline__ __u16 __swab16p(const __u16 *x)
139 return __arch__swab16p(x);
141 static __inline__ void __swab16s(__u16 *addr)
143 __arch__swab16s(addr);
146 static __inline__ __attribute_const__ __u32 __fswab32(__u32 x)
148 return __arch__swab32(x);
150 static __inline__ __u32 __swab32p(const __u32 *x)
152 return __arch__swab32p(x);
154 static __inline__ void __swab32s(__u32 *addr)
156 __arch__swab32s(addr);
159 #ifdef __BYTEORDER_HAS_U64__
160 static __inline__ __attribute_const__ __u64 __fswab64(__u64 x)
162 # ifdef __SWAB_64_THRU_32__
164 __u32 l = x & ((1ULL<<32)-1);
165 return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
167 return __arch__swab64(x);
170 static __inline__ __u64 __swab64p(const __u64 *x)
172 return __arch__swab64p(x);
174 static __inline__ void __swab64s(__u64 *addr)
176 __arch__swab64s(addr);
178 #endif /* __BYTEORDER_HAS_U64__ */
180 #if defined(__KERNEL__)
181 #define swab16 __swab16
182 #define swab32 __swab32
183 #define swab64 __swab64
184 #define swab16p __swab16p
185 #define swab32p __swab32p
186 #define swab64p __swab64p
187 #define swab16s __swab16s
188 #define swab32s __swab32s
189 #define swab64s __swab64s
192 #endif /* _LINUX_BYTEORDER_SWAB_H */