1 : #ifndef _LINUX_BYTEORDER_SWAB_H
2 : #define _LINUX_BYTEORDER_SWAB_H
3 :
4 : /*
5 : * linux/byteorder/swab.h
6 : * Byte-swapping, independently from CPU endianness
7 : * swabXX[ps]?(foo)
8 : *
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.
12 : *
13 : * See asm-i386/byteorder.h and suches for examples of how to provide
14 : * architecture-dependent optimized versions
15 : *
16 : */
17 :
18 :
19 : /* casts are necessary for constants, because we never know how for sure
20 : * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
21 : */
22 : #define ___swab16(x) \
23 : ({ \
24 : __u16 __x = (x); \
25 : ((__u16)( \
26 : (((__u16)(__x) & (__u16)0x00ffU) << 8) | \
27 : (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
28 : })
29 :
30 : #define ___swab32(x) \
31 : ({ \
32 : __u32 __x = (x); \
33 : ((__u32)( \
34 : (((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
35 : (((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \
36 : (((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \
37 : (((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
38 : })
39 :
40 : #define ___swab64(x) \
41 : ({ \
42 : __u64 __x = (x); \
43 : ((__u64)( \
44 : (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
45 : (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
46 : (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
47 : (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) << 8) | \
48 : (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >> 8) | \
49 : (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
50 : (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
51 : (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
52 : })
53 :
54 : #define ___constant_swab16(x) \
55 : ((__u16)( \
56 : (((__u16)(x) & (__u16)0x00ffU) << 8) | \
57 : (((__u16)(x) & (__u16)0xff00U) >> 8) ))
58 : #define ___constant_swab32(x) \
59 : ((__u32)( \
60 : (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
61 : (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
62 : (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
63 : (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
64 : #define ___constant_swab64(x) \
65 : ((__u64)( \
66 : (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
67 : (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
68 : (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
69 : (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
70 : (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
71 : (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
72 : (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
73 : (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
74 :
75 : /*
76 : * provide defaults when no architecture-specific optimization is detected
77 : */
78 : #ifndef __arch__swab16
79 : # define __arch__swab16(x) ({ __u16 __tmp = (x) ; ___swab16(__tmp); })
80 : #endif
81 : #ifndef __arch__swab32
82 : # define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); })
83 : #endif
84 : #ifndef __arch__swab64
85 : # define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); })
86 : #endif
87 :
88 : #ifndef __arch__swab16p
89 : # define __arch__swab16p(x) __arch__swab16(*(x))
90 : #endif
91 : #ifndef __arch__swab32p
92 : # define __arch__swab32p(x) __arch__swab32(*(x))
93 : #endif
94 : #ifndef __arch__swab64p
95 : # define __arch__swab64p(x) __arch__swab64(*(x))
96 : #endif
97 :
98 : #ifndef __arch__swab16s
99 : # define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0)
100 : #endif
101 : #ifndef __arch__swab32s
102 : # define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0)
103 : #endif
104 : #ifndef __arch__swab64s
105 : # define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0)
106 : #endif
107 :
108 :
109 : /*
110 : * Allow constant folding
111 : */
112 : #if defined(__GNUC__) && defined(__OPTIMIZE__)
113 : # define __swab16(x) \
114 : (__builtin_constant_p((__u16)(x)) ? \
115 : ___swab16((x)) : \
116 : __fswab16((x)))
117 : # define __swab32(x) \
118 : (__builtin_constant_p((__u32)(x)) ? \
119 : ___swab32((x)) : \
120 : __fswab32((x)))
121 : # define __swab64(x) \
122 : (__builtin_constant_p((__u64)(x)) ? \
123 : ___swab64((x)) : \
124 : __fswab64((x)))
125 : #else
126 : # define __swab16(x) __fswab16(x)
127 : # define __swab32(x) __fswab32(x)
128 : # define __swab64(x) __fswab64(x)
129 : #endif /* OPTIMIZE */
130 :
131 :
132 0 : static __inline__ __u16 __fswab16(__u16 x)
133 : {
134 0 : return __arch__swab16(x);
135 : }
136 : static __inline__ __u16 __swab16p(const __u16 *x)
137 : {
138 : return __arch__swab16p(x);
139 : }
140 : static __inline__ void __swab16s(__u16 *addr)
141 : {
142 : __arch__swab16s(addr);
143 : }
144 :
145 0 : static __inline__ __u32 __fswab32(__u32 x)
146 : {
147 0 : return __arch__swab32(x);
148 : }
149 : static __inline__ __u32 __swab32p(const __u32 *x)
150 : {
151 : return __arch__swab32p(x);
152 : }
153 : static __inline__ void __swab32s(__u32 *addr)
154 : {
155 : __arch__swab32s(addr);
156 : }
157 :
158 : #ifdef __BYTEORDER_HAS_U64__
159 0 : static __inline__ __u64 __fswab64(__u64 x)
160 : {
161 : # ifdef __SWAB_64_THRU_32__
162 : __u32 h = x >> 32;
163 : __u32 l = x & ((1ULL<<32)-1);
164 : return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
165 : # else
166 0 : return __arch__swab64(x);
167 : # endif
168 : }
169 : static __inline__ __u64 __swab64p(const __u64 *x)
170 : {
171 : return __arch__swab64p(x);
172 : }
173 : static __inline__ void __swab64s(__u64 *addr)
174 : {
175 : __arch__swab64s(addr);
176 : }
177 : #endif /* __BYTEORDER_HAS_U64__ */
178 :
179 :
180 : #endif /* _LINUX_BYTEORDER_SWAB_H */
|