summaryrefslogtreecommitdiffstats
path: root/ccan/endian/endian.h
blob: 6732e8aa8193f11a8e131a65bee1fa87fbe8495e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_ENDIAN_H
#define CCAN_ENDIAN_H
#include <stdint.h>
#include "config.h"

/**
 * BSWAP_16 - reverse bytes in a constant uint16_t value.
 * @val: constant value whose bytes to swap.
 *
 * Designed to be usable in constant-requiring initializers.
 *
 * Example:
 *	struct mystruct {
 *		char buf[BSWAP_16(0x1234)];
 *	};
 */
#define BSWAP_16(val)				\
	((((uint16_t)(val) & 0x00ff) << 8)	\
	 | (((uint16_t)(val) & 0xff00) >> 8))

/**
 * BSWAP_32 - reverse bytes in a constant uint32_t value.
 * @val: constant value whose bytes to swap.
 *
 * Designed to be usable in constant-requiring initializers.
 *
 * Example:
 *	struct mystruct {
 *		char buf[BSWAP_32(0xff000000)];
 *	};
 */
#define BSWAP_32(val)					\
	((((uint32_t)(val) & 0x000000ff) << 24)		\
	 | (((uint32_t)(val) & 0x0000ff00) << 8)		\
	 | (((uint32_t)(val) & 0x00ff0000) >> 8)		\
	 | (((uint32_t)(val) & 0xff000000) >> 24))

/**
 * BSWAP_64 - reverse bytes in a constant uint64_t value.
 * @val: constantvalue whose bytes to swap.
 *
 * Designed to be usable in constant-requiring initializers.
 *
 * Example:
 *	struct mystruct {
 *		char buf[BSWAP_64(0xff00000000000000ULL)];
 *	};
 */
#define BSWAP_64(val)						\
	((((uint64_t)(val) & 0x00000000000000ffULL) << 56)	\
	 | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40)	\
	 | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24)	\
	 | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8)	\
	 | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8)	\
	 | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24)	\
	 | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40)	\
	 | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56))

#if HAVE_BYTESWAP_H
#include <byteswap.h>
#else
/**
 * bswap_16 - reverse bytes in a uint16_t value.
 * @val: value whose bytes to swap.
 *
 * Example:
 *	// Output contains "1024 is 4 as two bytes reversed"
 *	printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
 */
static inline uint16_t bswap_16(uint16_t val)
{
	return BSWAP_16(val);
}

/**
 * bswap_32 - reverse bytes in a uint32_t value.
 * @val: value whose bytes to swap.
 *
 * Example:
 *	// Output contains "1024 is 262144 as four bytes reversed"
 *	printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
 */
static inline uint32_t bswap_32(uint32_t val)
{
	return BSWAP_32(val);
}
#endif /* !HAVE_BYTESWAP_H */

#if !HAVE_BSWAP_64
/**
 * bswap_64 - reverse bytes in a uint64_t value.
 * @val: value whose bytes to swap.
 *
 * Example:
 *	// Output contains "1024 is 1125899906842624 as eight bytes reversed"
 *	printf("1024 is %llu as eight bytes reversed\n",
 *		(unsigned long long)bswap_64(1024));
 */
static inline uint64_t bswap_64(uint64_t val)
{
	return BSWAP_64(val);
}
#endif

/* Needed for Glibc like endiness check */
#define	__LITTLE_ENDIAN	1234
#define	__BIG_ENDIAN	4321

/* Sanity check the defines.  We don't handle weird endianness. */
#if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
#error "Unknown endian"
#elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
#error "Can't compile for both big and little endian."
#elif HAVE_LITTLE_ENDIAN
#define __BYTE_ORDER	__LITTLE_ENDIAN
#elif HAVE_BIG_ENDIAN
#define __BYTE_ORDER	__BIG_ENDIAN
#endif


#ifdef __CHECKER__
/* sparse needs forcing to remove bitwise attribute from ccan/short_types */
#define ENDIAN_CAST __attribute__((force))
#define ENDIAN_TYPE __attribute__((bitwise))
#else
#define ENDIAN_CAST
#define ENDIAN_TYPE
#endif

typedef uint64_t ENDIAN_TYPE leint64_t;
typedef uint64_t ENDIAN_TYPE beint64_t;
typedef uint32_t ENDIAN_TYPE leint32_t;
typedef uint32_t ENDIAN_TYPE beint32_t;
typedef uint16_t ENDIAN_TYPE leint16_t;
typedef uint16_t ENDIAN_TYPE beint16_t;

#if HAVE_LITTLE_ENDIAN
/**
 * CPU_TO_LE64 - convert a constant uint64_t value to little-endian
 * @native: constant to convert
 */
#define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)(native))

/**
 * CPU_TO_LE32 - convert a constant uint32_t value to little-endian
 * @native: constant to convert
 */
#define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)(native))

/**
 * CPU_TO_LE16 - convert a constant uint16_t value to little-endian
 * @native: constant to convert
 */
#define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)(native))

/**
 * LE64_TO_CPU - convert a little-endian uint64_t constant
 * @le_val: little-endian constant to convert
 */
#define LE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val))

/**
 * LE32_TO_CPU - convert a little-endian uint32_t constant
 * @le_val: little-endian constant to convert
 */
#define LE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val))

/**
 * LE16_TO_CPU - convert a little-endian uint16_t constant
 * @le_val: little-endian constant to convert
 */
#define LE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val))

#else /* ... HAVE_BIG_ENDIAN */
#define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)BSWAP_64(native))
#define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)BSWAP_32(native))
#define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)BSWAP_16(native))
#define LE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val)
#define LE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val)
#define LE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val)
#endif /* HAVE_BIG_ENDIAN */

#if HAVE_BIG_ENDIAN
/**
 * CPU_TO_BE64 - convert a constant uint64_t value to big-endian
 * @native: constant to convert
 */
#define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)(native))

/**
 * CPU_TO_BE32 - convert a constant uint32_t value to big-endian
 * @native: constant to convert
 */
#define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)(native))

/**
 * CPU_TO_BE16 - convert a constant uint16_t value to big-endian
 * @native: constant to convert
 */
#define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)(native))

/**
 * BE64_TO_CPU - convert a big-endian uint64_t constant
 * @le_val: big-endian constant to convert
 */
#define BE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val))

/**
 * BE32_TO_CPU - convert a big-endian uint32_t constant
 * @le_val: big-endian constant to convert
 */
#define BE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val))

/**
 * BE16_TO_CPU - convert a big-endian uint16_t constant
 * @le_val: big-endian constant to convert
 */
#define BE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val))

#else /* ... HAVE_LITTLE_ENDIAN */
#define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)BSWAP_64(native))
#define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)BSWAP_32(native))
#define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)BSWAP_16(native))
#define BE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val)
#define BE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val)
#define BE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val)
#endif /* HAVE_LITTE_ENDIAN */


/**
 * cpu_to_le64 - convert a uint64_t value to little-endian
 * @native: value to convert
 */
static inline leint64_t cpu_to_le64(uint64_t native)
{
	return CPU_TO_LE64(native);
}

/**
 * cpu_to_le32 - convert a uint32_t value to little-endian
 * @native: value to convert
 */
static inline leint32_t cpu_to_le32(uint32_t native)
{
	return CPU_TO_LE32(native);
}

/**
 * cpu_to_le16 - convert a uint16_t value to little-endian
 * @native: value to convert
 */
static inline leint16_t cpu_to_le16(uint16_t native)
{
	return CPU_TO_LE16(native);
}

/**
 * le64_to_cpu - convert a little-endian uint64_t value
 * @le_val: little-endian value to convert
 */
static inline uint64_t le64_to_cpu(leint64_t le_val)
{
	return LE64_TO_CPU(le_val);
}

/**
 * le32_to_cpu - convert a little-endian uint32_t value
 * @le_val: little-endian value to convert
 */
static inline uint32_t le32_to_cpu(leint32_t le_val)
{
	return LE32_TO_CPU(le_val);
}

/**
 * le16_to_cpu - convert a little-endian uint16_t value
 * @le_val: little-endian value to convert
 */
static inline uint16_t le16_to_cpu(leint16_t le_val)
{
	return LE16_TO_CPU(le_val);
}

/**
 * cpu_to_be64 - convert a uint64_t value to big endian.
 * @native: value to convert
 */
static inline beint64_t cpu_to_be64(uint64_t native)
{
	return CPU_TO_BE64(native);
}

/**
 * cpu_to_be32 - convert a uint32_t value to big endian.
 * @native: value to convert
 */
static inline beint32_t cpu_to_be32(uint32_t native)
{
	return CPU_TO_BE32(native);
}

/**
 * cpu_to_be16 - convert a uint16_t value to big endian.
 * @native: value to convert
 */
static inline beint16_t cpu_to_be16(uint16_t native)
{
	return CPU_TO_BE16(native);
}

/**
 * be64_to_cpu - convert a big-endian uint64_t value
 * @be_val: big-endian value to convert
 */
static inline uint64_t be64_to_cpu(beint64_t be_val)
{
	return BE64_TO_CPU(be_val);
}

/**
 * be32_to_cpu - convert a big-endian uint32_t value
 * @be_val: big-endian value to convert
 */
static inline uint32_t be32_to_cpu(beint32_t be_val)
{
	return BE32_TO_CPU(be_val);
}

/**
 * be16_to_cpu - convert a big-endian uint16_t value
 * @be_val: big-endian value to convert
 */
static inline uint16_t be16_to_cpu(beint16_t be_val)
{
	return BE16_TO_CPU(be_val);
}

/* Whichever they include first, they get these definitions. */
#ifdef CCAN_SHORT_TYPES_H
/**
 * be64/be32/be16 - 64/32/16 bit big-endian representation.
 */
typedef beint64_t be64;
typedef beint32_t be32;
typedef beint16_t be16;

/**
 * le64/le32/le16 - 64/32/16 bit little-endian representation.
 */
typedef leint64_t le64;
typedef leint32_t le32;
typedef leint16_t le16;
#endif
#endif /* CCAN_ENDIAN_H */
OpenPOWER on IntegriCloud