Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1408 | serge | 1 | #ifndef _LINUX_SWAB_H |
2 | #define _LINUX_SWAB_H |
||
3 | |||
4 | #include |
||
5 | #include |
||
6 | #include |
||
7 | |||
8 | /* |
||
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. |
||
11 | */ |
||
12 | #define ___constant_swab16(x) ((__u16)( \ |
||
13 | (((__u16)(x) & (__u16)0x00ffU) << 8) | \ |
||
14 | (((__u16)(x) & (__u16)0xff00U) >> 8))) |
||
15 | |||
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))) |
||
21 | |||
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))) |
||
31 | |||
32 | #define ___constant_swahw32(x) ((__u32)( \ |
||
33 | (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \ |
||
34 | (((__u32)(x) & (__u32)0xffff0000UL) >> 16))) |
||
35 | |||
36 | #define ___constant_swahb32(x) ((__u32)( \ |
||
37 | (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \ |
||
38 | (((__u32)(x) & (__u32)0xff00ff00UL) >> 8))) |
||
39 | |||
40 | /* |
||
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 |
||
44 | */ |
||
45 | |||
46 | static inline __attribute_const__ __u16 __fswab16(__u16 val) |
||
47 | { |
||
48 | #ifdef __arch_swab16 |
||
49 | return __arch_swab16(val); |
||
50 | #else |
||
51 | return ___constant_swab16(val); |
||
52 | #endif |
||
53 | } |
||
54 | |||
55 | static inline __attribute_const__ __u32 __fswab32(__u32 val) |
||
56 | { |
||
57 | #ifdef __arch_swab32 |
||
58 | return __arch_swab32(val); |
||
59 | #else |
||
60 | return ___constant_swab32(val); |
||
61 | #endif |
||
62 | } |
||
63 | |||
64 | static inline __attribute_const__ __u64 __fswab64(__u64 val) |
||
65 | { |
||
66 | #ifdef __arch_swab64 |
||
67 | return __arch_swab64(val); |
||
68 | #elif defined(__SWAB_64_THRU_32__) |
||
69 | __u32 h = val >> 32; |
||
70 | __u32 l = val & ((1ULL << 32) - 1); |
||
71 | return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h))); |
||
72 | #else |
||
73 | return ___constant_swab64(val); |
||
74 | #endif |
||
75 | } |
||
76 | |||
77 | static inline __attribute_const__ __u32 __fswahw32(__u32 val) |
||
78 | { |
||
79 | #ifdef __arch_swahw32 |
||
80 | return __arch_swahw32(val); |
||
81 | #else |
||
82 | return ___constant_swahw32(val); |
||
83 | #endif |
||
84 | } |
||
85 | |||
86 | static inline __attribute_const__ __u32 __fswahb32(__u32 val) |
||
87 | { |
||
88 | #ifdef __arch_swahb32 |
||
89 | return __arch_swahb32(val); |
||
90 | #else |
||
91 | return ___constant_swahb32(val); |
||
92 | #endif |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * __swab16 - return a byteswapped 16-bit value |
||
97 | * @x: value to byteswap |
||
98 | */ |
||
99 | #define __swab16(x) \ |
||
100 | (__builtin_constant_p((__u16)(x)) ? \ |
||
101 | ___constant_swab16(x) : \ |
||
102 | __fswab16(x)) |
||
103 | |||
104 | /** |
||
105 | * __swab32 - return a byteswapped 32-bit value |
||
106 | * @x: value to byteswap |
||
107 | */ |
||
108 | #define __swab32(x) \ |
||
109 | (__builtin_constant_p((__u32)(x)) ? \ |
||
110 | ___constant_swab32(x) : \ |
||
111 | __fswab32(x)) |
||
112 | |||
113 | /** |
||
114 | * __swab64 - return a byteswapped 64-bit value |
||
115 | * @x: value to byteswap |
||
116 | */ |
||
117 | #define __swab64(x) \ |
||
118 | (__builtin_constant_p((__u64)(x)) ? \ |
||
119 | ___constant_swab64(x) : \ |
||
120 | __fswab64(x)) |
||
121 | |||
122 | /** |
||
123 | * __swahw32 - return a word-swapped 32-bit value |
||
124 | * @x: value to wordswap |
||
125 | * |
||
126 | * __swahw32(0x12340000) is 0x00001234 |
||
127 | */ |
||
128 | #define __swahw32(x) \ |
||
129 | (__builtin_constant_p((__u32)(x)) ? \ |
||
130 | ___constant_swahw32(x) : \ |
||
131 | __fswahw32(x)) |
||
132 | |||
133 | /** |
||
134 | * __swahb32 - return a high and low byte-swapped 32-bit value |
||
135 | * @x: value to byteswap |
||
136 | * |
||
137 | * __swahb32(0x12345678) is 0x34127856 |
||
138 | */ |
||
139 | #define __swahb32(x) \ |
||
140 | (__builtin_constant_p((__u32)(x)) ? \ |
||
141 | ___constant_swahb32(x) : \ |
||
142 | __fswahb32(x)) |
||
143 | |||
144 | /** |
||
145 | * __swab16p - return a byteswapped 16-bit value from a pointer |
||
146 | * @p: pointer to a naturally-aligned 16-bit value |
||
147 | */ |
||
148 | static inline __u16 __swab16p(const __u16 *p) |
||
149 | { |
||
150 | #ifdef __arch_swab16p |
||
151 | return __arch_swab16p(p); |
||
152 | #else |
||
153 | return __swab16(*p); |
||
154 | #endif |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * __swab32p - return a byteswapped 32-bit value from a pointer |
||
159 | * @p: pointer to a naturally-aligned 32-bit value |
||
160 | */ |
||
161 | static inline __u32 __swab32p(const __u32 *p) |
||
162 | { |
||
163 | #ifdef __arch_swab32p |
||
164 | return __arch_swab32p(p); |
||
165 | #else |
||
166 | return __swab32(*p); |
||
167 | #endif |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * __swab64p - return a byteswapped 64-bit value from a pointer |
||
172 | * @p: pointer to a naturally-aligned 64-bit value |
||
173 | */ |
||
174 | static inline __u64 __swab64p(const __u64 *p) |
||
175 | { |
||
176 | #ifdef __arch_swab64p |
||
177 | return __arch_swab64p(p); |
||
178 | #else |
||
179 | return __swab64(*p); |
||
180 | #endif |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * __swahw32p - return a wordswapped 32-bit value from a pointer |
||
185 | * @p: pointer to a naturally-aligned 32-bit value |
||
186 | * |
||
187 | * See __swahw32() for details of wordswapping. |
||
188 | */ |
||
189 | static inline __u32 __swahw32p(const __u32 *p) |
||
190 | { |
||
191 | #ifdef __arch_swahw32p |
||
192 | return __arch_swahw32p(p); |
||
193 | #else |
||
194 | return __swahw32(*p); |
||
195 | #endif |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * __swahb32p - return a high and low byteswapped 32-bit value from a pointer |
||
200 | * @p: pointer to a naturally-aligned 32-bit value |
||
201 | * |
||
202 | * See __swahb32() for details of high/low byteswapping. |
||
203 | */ |
||
204 | static inline __u32 __swahb32p(const __u32 *p) |
||
205 | { |
||
206 | #ifdef __arch_swahb32p |
||
207 | return __arch_swahb32p(p); |
||
208 | #else |
||
209 | return __swahb32(*p); |
||
210 | #endif |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * __swab16s - byteswap a 16-bit value in-place |
||
215 | * @p: pointer to a naturally-aligned 16-bit value |
||
216 | */ |
||
217 | static inline void __swab16s(__u16 *p) |
||
218 | { |
||
219 | #ifdef __arch_swab16s |
||
220 | __arch_swab16s(p); |
||
221 | #else |
||
222 | *p = __swab16p(p); |
||
223 | #endif |
||
224 | } |
||
225 | /** |
||
226 | * __swab32s - byteswap a 32-bit value in-place |
||
227 | * @p: pointer to a naturally-aligned 32-bit value |
||
228 | */ |
||
229 | static inline void __swab32s(__u32 *p) |
||
230 | { |
||
231 | #ifdef __arch_swab32s |
||
232 | __arch_swab32s(p); |
||
233 | #else |
||
234 | *p = __swab32p(p); |
||
235 | #endif |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * __swab64s - byteswap a 64-bit value in-place |
||
240 | * @p: pointer to a naturally-aligned 64-bit value |
||
241 | */ |
||
242 | static inline void __swab64s(__u64 *p) |
||
243 | { |
||
244 | #ifdef __arch_swab64s |
||
245 | __arch_swab64s(p); |
||
246 | #else |
||
247 | *p = __swab64p(p); |
||
248 | #endif |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * __swahw32s - wordswap a 32-bit value in-place |
||
253 | * @p: pointer to a naturally-aligned 32-bit value |
||
254 | * |
||
255 | * See __swahw32() for details of wordswapping |
||
256 | */ |
||
257 | static inline void __swahw32s(__u32 *p) |
||
258 | { |
||
259 | #ifdef __arch_swahw32s |
||
260 | __arch_swahw32s(p); |
||
261 | #else |
||
262 | *p = __swahw32p(p); |
||
263 | #endif |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * __swahb32s - high and low byteswap a 32-bit value in-place |
||
268 | * @p: pointer to a naturally-aligned 32-bit value |
||
269 | * |
||
270 | * See __swahb32() for details of high and low byte swapping |
||
271 | */ |
||
272 | static inline void __swahb32s(__u32 *p) |
||
273 | { |
||
274 | #ifdef __arch_swahb32s |
||
275 | __arch_swahb32s(p); |
||
276 | #else |
||
277 | *p = __swahb32p(p); |
||
278 | #endif |
||
279 | } |
||
280 | |||
281 | #ifdef __KERNEL__ |
||
282 | # define swab16 __swab16 |
||
283 | # define swab32 __swab32 |
||
284 | # define swab64 __swab64 |
||
285 | # define swahw32 __swahw32 |
||
286 | # define swahb32 __swahb32 |
||
287 | # define swab16p __swab16p |
||
288 | # define swab32p __swab32p |
||
289 | # define swab64p __swab64p |
||
290 | # define swahw32p __swahw32p |
||
291 | # define swahb32p __swahb32p |
||
292 | # define swab16s __swab16s |
||
293 | # define swab32s __swab32s |
||
294 | # define swab64s __swab64s |
||
295 | # define swahw32s __swahw32s |
||
296 | # define swahb32s __swahb32s |
||
297 | #endif /* __KERNEL__ */ |
||
298 | |||
299 | #endif /* _LINUX_SWAB_H */><>><>><>><>><>><>><>><>><>><>><> |