blob: 5a8a64a743687005daeba4c382fc9ae15f432512 [file] [log] [blame]
Kevin O'Connorb3064592012-08-14 21:20:10 -04001#ifndef __BYTEORDER_H
2#define __BYTEORDER_H
3
4static inline u16 __swab16_constant(u16 val) {
5 return (val<<8) | (val>>8);
6}
7static inline u32 __swab32_constant(u32 val) {
8 return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
9}
10static inline u64 __swab64_constant(u64 val) {
11 return ((u64)__swab32_constant(val) << 32) | __swab32_constant(val>>32);
12}
13static inline u32 __swab32(u32 val) {
14 asm("bswapl %0" : "+r"(val));
15 return val;
16}
17static inline u64 __swab64(u64 val) {
18 union u64_u32_u i, o;
19 i.val = val;
Kevin O'Connorb3064592012-08-14 21:20:10 -040020 o.lo = __swab32(i.hi);
Kevin O'Connor30e6af02012-09-15 12:17:37 -040021 o.hi = __swab32(i.lo);
Kevin O'Connorb3064592012-08-14 21:20:10 -040022 return o.val;
23}
24
25#define swab16(x) __swab16_constant(x)
26#define swab32(x) (__builtin_constant_p((u32)(x)) \
27 ? __swab32_constant(x) : __swab32(x))
28#define swab64(x) (__builtin_constant_p((u64)(x)) \
29 ? __swab64_constant(x) : __swab64(x))
30
31static inline u16 cpu_to_le16(u16 x) {
32 return x;
33}
34static inline u32 cpu_to_le32(u32 x) {
35 return x;
36}
37static inline u64 cpu_to_le64(u64 x) {
38 return x;
39}
40static inline u16 le16_to_cpu(u16 x) {
41 return x;
42}
43static inline u32 le32_to_cpu(u32 x) {
44 return x;
45}
46static inline u32 le64_to_cpu(u64 x) {
47 return x;
48}
49
50static inline u16 cpu_to_be16(u16 x) {
51 return swab16(x);
52}
53static inline u32 cpu_to_be32(u32 x) {
54 return swab32(x);
55}
56static inline u64 cpu_to_be64(u64 x) {
57 return swab64(x);
58}
59static inline u16 be16_to_cpu(u16 x) {
60 return swab16(x);
61}
62static inline u32 be32_to_cpu(u32 x) {
63 return swab32(x);
64}
65static inline u32 be64_to_cpu(u64 x) {
66 return swab64(x);
67}
68
69#endif // byteorder.h