1 #ifndef _LINUX_BYTEORDER_H
2 #define _LINUX_BYTEORDER_H
4 #include <linux/types.h>
5 #include <linux/swab.h>
7 #if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
8 # error Fix asm/byteorder.h to define one endianness
11 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
12 # error Fix asm/byteorder.h to define arch endianness
15 #ifdef __LITTLE_ENDIAN
16 # undef __LITTLE_ENDIAN
17 # define __LITTLE_ENDIAN 1234
22 # define __BIG_ENDIAN 4321
25 #if defined(__LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN_BITFIELD)
26 # define __LITTLE_ENDIAN_BITFIELD
29 #if defined(__BIG_ENDIAN) && !defined(__BIG_ENDIAN_BITFIELD)
30 # define __BIG_ENDIAN_BITFIELD
33 #ifdef __LITTLE_ENDIAN
34 # define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
35 # define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
36 # define __le64_to_cpu(x) ((__force __u64)(__le64)(x))
37 # define __cpu_to_le16(x) ((__force __le16)(__u16)(x))
38 # define __cpu_to_le32(x) ((__force __le32)(__u32)(x))
39 # define __cpu_to_le64(x) ((__force __le64)(__u64)(x))
41 # define __be16_to_cpu(x) __swab16((__force __u16)(__be16)(x))
42 # define __be32_to_cpu(x) __swab32((__force __u32)(__be32)(x))
43 # define __be64_to_cpu(x) __swab64((__force __u64)(__be64)(x))
44 # define __cpu_to_be16(x) ((__force __be16)__swab16(x))
45 # define __cpu_to_be32(x) ((__force __be32)__swab32(x))
46 # define __cpu_to_be64(x) ((__force __be64)__swab64(x))
50 # define __be16_to_cpu(x) ((__force __u16)(__be16)(x))
51 # define __be32_to_cpu(x) ((__force __u32)(__be32)(x))
52 # define __be64_to_cpu(x) ((__force __u64)(__be64)(x))
53 # define __cpu_to_be16(x) ((__force __be16)(__u16)(x))
54 # define __cpu_to_be32(x) ((__force __be32)(__u32)(x))
55 # define __cpu_to_be64(x) ((__force __be64)(__u64)(x))
57 # define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
58 # define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
59 # define __le64_to_cpu(x) __swab64((__force __u64)(__le64)(x))
60 # define __cpu_to_le16(x) ((__force __le16)__swab16(x))
61 # define __cpu_to_le32(x) ((__force __le32)__swab32(x))
62 # define __cpu_to_le64(x) ((__force __le64)__swab64(x))
66 * These helpers could be phased out over time as the base version
67 * handles constant folding.
69 #define __constant_htonl(x) __cpu_to_be32(x)
70 #define __constant_ntohl(x) __be32_to_cpu(x)
71 #define __constant_htons(x) __cpu_to_be16(x)
72 #define __constant_ntohs(x) __be16_to_cpu(x)
74 #define __constant_le16_to_cpu(x) __le16_to_cpu(x)
75 #define __constant_le32_to_cpu(x) __le32_to_cpu(x)
76 #define __constant_le64_to_cpu(x) __le64_to_cpu(x)
77 #define __constant_be16_to_cpu(x) __be16_to_cpu(x)
78 #define __constant_be32_to_cpu(x) __be32_to_cpu(x)
79 #define __constant_be64_to_cpu(x) __be64_to_cpu(x)
81 #define __constant_cpu_to_le16(x) __cpu_to_le16(x)
82 #define __constant_cpu_to_le32(x) __cpu_to_le32(x)
83 #define __constant_cpu_to_le64(x) __cpu_to_le64(x)
84 #define __constant_cpu_to_be16(x) __cpu_to_be16(x)
85 #define __constant_cpu_to_be32(x) __cpu_to_be32(x)
86 #define __constant_cpu_to_be64(x) __cpu_to_be64(x)
88 static inline void __le16_to_cpus(__u16
*p
)
95 static inline void __cpu_to_le16s(__u16
*p
)
102 static inline void __le32_to_cpus(__u32
*p
)
109 static inline void __cpu_to_le32s(__u32
*p
)
116 static inline void __le64_to_cpus(__u64
*p
)
123 static inline void __cpu_to_le64s(__u64
*p
)
130 static inline void __be16_to_cpus(__u16
*p
)
132 #ifdef __LITTLE_ENDIAN
137 static inline void __cpu_to_be16s(__u16
*p
)
139 #ifdef __LITTLE_ENDIAN
144 static inline void __be32_to_cpus(__u32
*p
)
146 #ifdef __LITTLE_ENDIAN
151 static inline void __cpu_to_be32s(__u32
*p
)
153 #ifdef __LITTLE_ENDIAN
158 static inline void __be64_to_cpus(__u64
*p
)
160 #ifdef __LITTLE_ENDIAN
165 static inline void __cpu_to_be64s(__u64
*p
)
167 #ifdef __LITTLE_ENDIAN
172 static inline __u16
__le16_to_cpup(const __le16
*p
)
174 #ifdef __LITTLE_ENDIAN
175 return (__force __u16
)*p
;
177 return __swab16p((__force __u16
*)p
);
181 static inline __u32
__le32_to_cpup(const __le32
*p
)
183 #ifdef __LITTLE_ENDIAN
184 return (__force __u32
)*p
;
186 return __swab32p((__force __u32
*)p
);
190 static inline __u64
__le64_to_cpup(const __le64
*p
)
192 #ifdef __LITTLE_ENDIAN
193 return (__force __u64
)*p
;
195 return __swab64p((__force __u64
*)p
);
199 static inline __le16
__cpu_to_le16p(const __u16
*p
)
201 #ifdef __LITTLE_ENDIAN
202 return (__force __le16
)*p
;
204 return (__force __le16
)__swab16p(p
);
208 static inline __le32
__cpu_to_le32p(const __u32
*p
)
210 #ifdef __LITTLE_ENDIAN
211 return (__force __le32
)*p
;
213 return (__force __le32
)__swab32p(p
);
217 static inline __le64
__cpu_to_le64p(const __u64
*p
)
219 #ifdef __LITTLE_ENDIAN
220 return (__force __le64
)*p
;
222 return (__force __le64
)__swab64p(p
);
226 static inline __u16
__be16_to_cpup(const __be16
*p
)
229 return (__force __u16
)*p
;
231 return __swab16p((__force __u16
*)p
);
235 static inline __u32
__be32_to_cpup(const __be32
*p
)
238 return (__force __u32
)*p
;
240 return __swab32p((__force __u32
*)p
);
244 static inline __u64
__be64_to_cpup(const __be64
*p
)
247 return (__force __u64
)*p
;
249 return __swab64p((__force __u64
*)p
);
253 static inline __be16
__cpu_to_be16p(const __u16
*p
)
256 return (__force __be16
)*p
;
258 return (__force __be16
)__swab16p(p
);
262 static inline __be32
__cpu_to_be32p(const __u32
*p
)
265 return (__force __be32
)*p
;
267 return (__force __be32
)__swab32p(p
);
271 static inline __be64
__cpu_to_be64p(const __u64
*p
)
274 return (__force __be64
)*p
;
276 return (__force __be64
)__swab64p(p
);
282 # define le16_to_cpu __le16_to_cpu
283 # define le32_to_cpu __le32_to_cpu
284 # define le64_to_cpu __le64_to_cpu
285 # define be16_to_cpu __be16_to_cpu
286 # define be32_to_cpu __be32_to_cpu
287 # define be64_to_cpu __be64_to_cpu
288 # define cpu_to_le16 __cpu_to_le16
289 # define cpu_to_le32 __cpu_to_le32
290 # define cpu_to_le64 __cpu_to_le64
291 # define cpu_to_be16 __cpu_to_be16
292 # define cpu_to_be32 __cpu_to_be32
293 # define cpu_to_be64 __cpu_to_be64
295 # define le16_to_cpup __le16_to_cpup
296 # define le32_to_cpup __le32_to_cpup
297 # define le64_to_cpup __le64_to_cpup
298 # define be16_to_cpup __be16_to_cpup
299 # define be32_to_cpup __be32_to_cpup
300 # define be64_to_cpup __be64_to_cpup
301 # define cpu_to_le16p __cpu_to_le16p
302 # define cpu_to_le32p __cpu_to_le32p
303 # define cpu_to_le64p __cpu_to_le64p
304 # define cpu_to_be16p __cpu_to_be16p
305 # define cpu_to_be32p __cpu_to_be32p
306 # define cpu_to_be64p __cpu_to_be64p
308 # define le16_to_cpus __le16_to_cpus
309 # define le32_to_cpus __le32_to_cpus
310 # define le64_to_cpus __le64_to_cpus
311 # define be16_to_cpus __be16_to_cpus
312 # define be32_to_cpus __be32_to_cpus
313 # define be64_to_cpus __be64_to_cpus
314 # define cpu_to_le16s __cpu_to_le16s
315 # define cpu_to_le32s __cpu_to_le32s
316 # define cpu_to_le64s __cpu_to_le64s
317 # define cpu_to_be16s __cpu_to_be16s
318 # define cpu_to_be32s __cpu_to_be32s
319 # define cpu_to_be64s __cpu_to_be64s
322 * They have to be macros in order to do the constant folding
323 * correctly - if the argument passed into a inline function
324 * it is no longer constant according to gcc..
331 # define ___htonl(x) __cpu_to_be32(x)
332 # define ___htons(x) __cpu_to_be16(x)
333 # define ___ntohl(x) __be32_to_cpu(x)
334 # define ___ntohs(x) __be16_to_cpu(x)
336 # define htonl(x) ___htonl(x)
337 # define ntohl(x) ___ntohl(x)
338 # define htons(x) ___htons(x)
339 # define ntohs(x) ___ntohs(x)
341 static inline void le16_add_cpu(__le16
*var
, u16 val
)
343 *var
= cpu_to_le16(le16_to_cpup(var
) + val
);
346 static inline void le32_add_cpu(__le32
*var
, u32 val
)
348 *var
= cpu_to_le32(le32_to_cpup(var
) + val
);
351 static inline void le64_add_cpu(__le64
*var
, u64 val
)
353 *var
= cpu_to_le64(le64_to_cpup(var
) + val
);
356 static inline void be16_add_cpu(__be16
*var
, u16 val
)
358 *var
= cpu_to_be16(be16_to_cpup(var
) + val
);
361 static inline void be32_add_cpu(__be32
*var
, u32 val
)
363 *var
= cpu_to_be32(be32_to_cpup(var
) + val
);
366 static inline void be64_add_cpu(__be64
*var
, u64 val
)
368 *var
= cpu_to_be64(be64_to_cpup(var
) + val
);
371 #endif /* __KERNEL__ */
372 #endif /* _LINUX_BYTEORDER_H */