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