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