[NET]: Add netlink connector.
[linux-2.6/verdex.git] / include / linux / byteorder / swab.h
blob2f1cb775125abe39eef09908078a287bb763ec29
1 #ifndef _LINUX_BYTEORDER_SWAB_H
2 #define _LINUX_BYTEORDER_SWAB_H
4 /*
5 * linux/byteorder/swab.h
6 * Byte-swapping, independently from CPU endianness
7 * swabXX[ps]?(foo)
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) \
24 ({ \
25 __u16 __x = (x); \
26 ((__u16)( \
27 (((__u16)(__x) & (__u16)0x00ffU) << 8) | \
28 (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
31 #define ___swab32(x) \
32 ({ \
33 __u32 __x = (x); \
34 ((__u32)( \
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) \
42 ({ \
43 __u64 __x = (x); \
44 ((__u64)( \
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) \
56 ((__u16)( \
57 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
58 (((__u16)(x) & (__u16)0xff00U) >> 8) ))
59 #define ___constant_swab32(x) \
60 ((__u32)( \
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) \
66 ((__u64)( \
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); })
81 #endif
82 #ifndef __arch__swab32
83 # define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); })
84 #endif
85 #ifndef __arch__swab64
86 # define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); })
87 #endif
89 #ifndef __arch__swab16p
90 # define __arch__swab16p(x) __arch__swab16(*(x))
91 #endif
92 #ifndef __arch__swab32p
93 # define __arch__swab32p(x) __arch__swab32(*(x))
94 #endif
95 #ifndef __arch__swab64p
96 # define __arch__swab64p(x) __arch__swab64(*(x))
97 #endif
99 #ifndef __arch__swab16s
100 # define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0)
101 #endif
102 #ifndef __arch__swab32s
103 # define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0)
104 #endif
105 #ifndef __arch__swab64s
106 # define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0)
107 #endif
111 * Allow constant folding
113 #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
114 # define __swab16(x) \
115 (__builtin_constant_p((__u16)(x)) ? \
116 ___swab16((x)) : \
117 __fswab16((x)))
118 # define __swab32(x) \
119 (__builtin_constant_p((__u32)(x)) ? \
120 ___swab32((x)) : \
121 __fswab32((x)))
122 # define __swab64(x) \
123 (__builtin_constant_p((__u64)(x)) ? \
124 ___swab64((x)) : \
125 __fswab64((x)))
126 #else
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__
163 __u32 h = x >> 32;
164 __u32 l = x & ((1ULL<<32)-1);
165 return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
166 # else
167 return __arch__swab64(x);
168 # endif
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
190 #endif
192 #endif /* _LINUX_BYTEORDER_SWAB_H */