]> git.ozlabs.org Git - ccan/blob - ccan/endian/endian.h
ccanlint: timeout, and implement -t option for quicker tests.
[ccan] / ccan / endian / endian.h
1 #ifndef CCAN_ENDIAN_H
2 #define CCAN_ENDIAN_H
3 #include <stdint.h>
4 #include "config.h"
5
6 /**
7  * swab_u16 - reverse bytes in a uint16_t value.
8  * @val: value whose bytes to swap.
9  */
10 static inline uint16_t swab_u16(uint16_t val)
11 {
12         return ((val & (uint16_t)0x00ffU) << 8)
13                 | ((val & (uint16_t)0xff00U) >> 8);
14 }
15
16 /**
17  * swab_u32 - reverse bytes in a uint32_t value.
18  * @val: value whose bytes to swap.
19  */
20 static inline uint32_t swab_u32(uint32_t val)
21 {
22         return ((val & (uint32_t)0x000000ffUL) << 24)
23                 | ((val & (uint32_t)0x0000ff00UL) <<  8)
24                 | ((val & (uint32_t)0x00ff0000UL) >>  8)
25                 | ((val & (uint32_t)0xff000000UL) >> 24);
26 }
27
28 /**
29  * swab_u64 - reverse bytes in a uint64_t value.
30  * @val: value whose bytes to swap.
31  */
32 static inline uint64_t swab_u64(uint64_t val)
33 {
34         return ((val & (uint64_t)0x00000000000000ffULL) << 56)
35                 | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
36                 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
37                 | ((val & (uint64_t)0x00000000ff000000ULL) <<  8)
38                 | ((val & (uint64_t)0x000000ff00000000ULL) >>  8)
39                 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
40                 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
41                 | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
42 }
43
44 /* Sanity check the defines.  We don't handle weird endianness. */
45 #if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
46 #error "Unknown endian"
47 #elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
48 #error "Can't compile for both big and little endian."
49 #endif
50
51 /**
52  * cpu_to_le64 - convert a uint64_t value to little-endian
53  * @native: value to convert
54  */
55 static inline uint64_t cpu_to_le64(uint64_t native)
56 {
57 #if HAVE_LITTLE_ENDIAN
58         return native;
59 #else
60         return swab_u64(native);
61 #endif
62 }
63
64 /**
65  * cpu_to_le32 - convert a uint32_t value to little-endian
66  * @native: value to convert
67  */
68 static inline uint32_t cpu_to_le32(uint32_t native)
69 {
70 #if HAVE_LITTLE_ENDIAN
71         return native;
72 #else
73         return swab_u32(native);
74 #endif
75 }
76
77 /**
78  * cpu_to_le16 - convert a uint16_t value to little-endian
79  * @native: value to convert
80  */
81 static inline uint16_t cpu_to_le16(uint16_t native)
82 {
83 #if HAVE_LITTLE_ENDIAN
84         return native;
85 #else
86         return swab_u16(native);
87 #endif
88 }
89
90 /**
91  * le64_to_cpu - convert a little-endian uint64_t value
92  * @le_val: little-endian value to convert
93  */
94 static inline uint64_t le64_to_cpu(uint64_t le_val)
95 {
96 #if HAVE_LITTLE_ENDIAN
97         return le_val;
98 #else
99         return swab_u64(le_val);
100 #endif
101 }
102
103 /**
104  * le32_to_cpu - convert a little-endian uint32_t value
105  * @le_val: little-endian value to convert
106  */
107 static inline uint32_t le32_to_cpu(uint32_t le_val)
108 {
109 #if HAVE_LITTLE_ENDIAN
110         return le_val;
111 #else
112         return swab_u32(le_val);
113 #endif
114 }
115
116 /**
117  * le16_to_cpu - convert a little-endian uint16_t value
118  * @le_val: little-endian value to convert
119  */
120 static inline uint16_t le16_to_cpu(uint16_t le_val)
121 {
122 #if HAVE_LITTLE_ENDIAN
123         return le_val;
124 #else
125         return swab_u16(le_val);
126 #endif
127 }
128
129 /**
130  * cpu_to_be64 - convert a uint64_t value to big endian.
131  * @native: value to convert
132  */
133 static inline uint64_t cpu_to_be64(uint64_t native)
134 {
135 #if HAVE_LITTLE_ENDIAN
136         return swab_u64(native);
137 #else
138         return native;
139 #endif
140 }
141
142 /**
143  * cpu_to_be32 - convert a uint32_t value to big endian.
144  * @native: value to convert
145  */
146 static inline uint32_t cpu_to_be32(uint32_t native)
147 {
148 #if HAVE_LITTLE_ENDIAN
149         return swab_u32(native);
150 #else
151         return native;
152 #endif
153 }
154
155 /**
156  * cpu_to_be16 - convert a uint16_t value to big endian.
157  * @native: value to convert
158  */
159 static inline uint16_t cpu_to_be16(uint16_t native)
160 {
161 #if HAVE_LITTLE_ENDIAN
162         return swab_u16(native);
163 #else
164         return native;
165 #endif
166 }
167
168 /**
169  * be64_to_cpu - convert a big-endian uint64_t value
170  * @be_val: big-endian value to convert
171  */
172 static inline uint64_t be64_to_cpu(uint64_t be_val)
173 {
174 #if HAVE_LITTLE_ENDIAN
175         return swab_u64(be_val);
176 #else
177         return be_val;
178 #endif
179 }
180
181 /**
182  * be32_to_cpu - convert a big-endian uint32_t value
183  * @be_val: big-endian value to convert
184  */
185 static inline uint32_t be32_to_cpu(uint32_t be_val)
186 {
187 #if HAVE_LITTLE_ENDIAN
188         return swab_u32(be_val);
189 #else
190         return be_val;
191 #endif
192 }
193
194 /**
195  * be16_to_cpu - convert a big-endian uint16_t value
196  * @be_val: big-endian value to convert
197  */
198 static inline uint16_t be16_to_cpu(uint16_t be_val)
199 {
200 #if HAVE_LITTLE_ENDIAN
201         return swab_u16(be_val);
202 #else
203         return be_val;
204 #endif
205 }
206
207 #endif /* CCAN_ENDIAN_H */