summaryrefslogtreecommitdiff
path: root/usual/endian.h
blob: 8291cdebe5c01cd900309b42e549b6ed1f6e2788 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * Copyright (c) 2009  Marko Kreen
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/**
 * @file
 *
 * Endianess conversion, convert integers to bytes.
 */

#ifndef _USUAL_ENDIAN_H_
#define _USUAL_ENDIAN_H_

#include <usual/base.h>
#include <string.h>

/*
 * Need to include OS headers even if unused, so our
 * definitions stay in use.
 */

#ifdef HAVE_ENDIAN_H
#include <endian.h>
#endif
#ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#endif

/*
 * Is unaligned access to integers OK?  Does not apply to floats.
 *
 * OK: x86, amd64, arm >= v6, ppc
 */
#if defined(__amd64__) || defined(__i386__) || defined(__ppc__) || defined(__ppc64__) \
	|| defined(__ARM_FEATURE_UNALIGNED) \
	|| defined(_M_IX86) || defined(_M_X64) || defined(_M_PPC) \
	|| (defined(_M_ARM) && _M_ARM >= 6)
#define WORDS_UNALIGNED_ACCESS_OK
#endif

/*
 * Ignore OS defines, as they may define only some subset of functions.
 *
 * Instead try to use compiler builtins.
 */

#undef bswap16
#undef bswap32
#undef bswap64

#undef htobe16
#undef htobe32
#undef htobe64
#undef htole16
#undef htole32
#undef htole64
#undef be16toh
#undef be32toh
#undef be64toh
#undef le16toh
#undef le32toh
#undef le64toh

#undef be16dec
#undef be32dec
#undef be64dec
#undef le16dec
#undef le32dec
#undef le64dec
#undef h16dec
#undef h32dec
#undef h64dec

#undef be16enc
#undef be32enc
#undef be64enc
#undef le16enc
#undef le32enc
#undef le64enc
#undef h16enc
#undef h32enc
#undef h64enc

/*
 * Redefine to avoid conflicts.
 */

#define bswap16(x) usual_bswap16(x)
#define bswap32(x) usual_bswap32(x)
#define bswap64(x) usual_bswap64(x)

#define be16dec(p) usual_be16dec(p)
#define be32dec(p) usual_be32dec(p)
#define be64dec(p) usual_be64dec(p)
#define le16dec(p) usual_le16dec(p)
#define le32dec(p) usual_le32dec(p)
#define le64dec(p) usual_le64dec(p)
#define h16dec(p) usual_h16dec(p)
#define h32dec(p) usual_h32dec(p)
#define h64dec(p) usual_h64dec(p)

#define be16enc(p, x) usual_be16enc(p, x)
#define be32enc(p, x) usual_be32enc(p, x)
#define be64enc(p, x) usual_be64enc(p, x)
#define le16enc(p, x) usual_le16enc(p, x)
#define le32enc(p, x) usual_le32enc(p, x)
#define le64enc(p, x) usual_le64enc(p, x)
#define h16enc(p, x) usual_h16enc(p, x)
#define h32enc(p, x) usual_h32enc(p, x)
#define h64enc(p, x) usual_h64enc(p, x)

/**
 * @name  Always swap.
 *
 * @{
 */

/** Swap 16-bit int */
static inline uint16_t bswap16(uint16_t x)
{
#if _COMPILER_GNUC(4, 8) || __has_builtin(__builtin_bswap16)
	return __builtin_bswap16(x);
#else
	return (x << 8) | (x >> 8);
#endif
}

/** Swap 32-bit int */
static inline uint32_t bswap32(uint32_t x)
{
#if _COMPILER_GNUC(4, 3) || __has_builtin(__builtin_bswap32)
	return __builtin_bswap32(x);
#else
	x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0x00FF00FF);
	return (x << 16) | (x >> 16);
#endif
}

/** Swap 64-bit int */
static inline uint64_t bswap64(uint64_t x)
{
#if _COMPILER_GNUC(4, 3) || __has_builtin(__builtin_bswap64)
	return __builtin_bswap64(x);
#else
	return ((uint64_t)bswap32(x) << 32) | bswap32(x >> 32);
#endif
}

/**
 * @}
 *
 * @name Convert host-endian int to BE/LE.
 *
 * @{
 */

#ifdef WORDS_BIGENDIAN

/** Convert native 16-bit int to big-endian */
#define	htobe16(x)	((uint16_t)(x))
/** Convert native 32-bit int to big-endian */
#define	htobe32(x)	((uint32_t)(x))
/** Convert native 64-bit int to big-endian */
#define	htobe64(x)	((uint64_t)(x))

/** Convert native 16-bit int to little-endian */
#define	htole16(x)	bswap16(x)
/** Convert native 32-bit int to little-endian */
#define	htole32(x)	bswap32(x)
/** Convert native 64-bit int to little-endian */
#define	htole64(x)	bswap64(x)

/** Convert big-endian 16-bit int to host-endian */
#define	be16toh(x)	((uint16_t)(x))
/** Convert big-endian 32-bit int to host-endian */
#define	be32toh(x)	((uint32_t)(x))
/** Convert big-endian 64-bit int to host-endian */
#define	be64toh(x)	((uint64_t)(x))

/** Convert little-endian 16-bit int to host-endian */
#define	le16toh(x)	bswap16(x)
/** Convert little-endian 32-bit int to host-endian */
#define	le32toh(x)	bswap32(x)
/** Convert little-endian 64-bit int to host-endian */
#define	le64toh(x)	bswap64(x)

#else /* !WORDS_BIGENDIAN */

/** Convert native 16-bit int to big-endian */
#define	htobe16(x)	bswap16(x)
/** Convert native 32-bit int to big-endian */
#define	htobe32(x)	bswap32(x)
/** Convert native 64-bit int to big-endian */
#define	htobe64(x)	bswap64(x)
/** Convert native 16-bit int to little-endian */
#define	htole16(x)	((uint16_t)(x))
/** Convert native 32-bit int to little-endian */
#define	htole32(x)	((uint32_t)(x))
/** Convert native 64-bit int to little-endian */
#define	htole64(x)	((uint64_t)(x))

/** Convert big-endian 16-bit int to host-endian */
#define	be16toh(x)	bswap16(x)
/** Convert big-endian 32-bit int to host-endian */
#define	be32toh(x)	bswap32(x)
/** Convert big-endian 64-bit int to host-endian */
#define	be64toh(x)	bswap64(x)

/** Convert little-endian 64-bit int to host-endian */
#define	le16toh(x)	((uint16_t)(x))
/** Convert little-endian 64-bit int to host-endian */
#define	le32toh(x)	((uint32_t)(x))
/** Convert little-endian 64-bit int to host-endian */
#define	le64toh(x)	((uint64_t)(x))

#endif

/**
 * @}
 *
 * @name Read integer values from memory and convert to host format.
 *
 * @{
 */

/** Read big-endian 16-bit int from memory */
static inline uint16_t be16dec(const void *p)
{
	uint16_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return htobe16(tmp);
}

/** Read big-endian 32-bit int from memory */
static inline uint32_t be32dec(const void *p)
{
	uint32_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return htobe32(tmp);
}

/** Read big-endian 64-bit int from memory */
static inline uint64_t be64dec(const void *p)
{
	uint64_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return htobe64(tmp);
}

/** Read little-endian 16-bit int from memory */
static inline uint16_t le16dec(const void *p)
{
	uint16_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return htole16(tmp);
}

/** Read little-endian 32-bit int from memory */
static inline uint32_t le32dec(const void *p)
{
	uint32_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return htole32(tmp);
}

/** Read little-endian 64-bit int from memory */
static inline uint64_t le64dec(const void *p)
{
	uint64_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return htole64(tmp);
}

/** Read host-endian 16-bit int from memory */
static inline uint16_t h16dec(const void *p)
{
	uint16_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return tmp;
}

/** Read host-endian 32-bit int from memory */
static inline uint32_t h32dec(const void *p)
{
	uint32_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return tmp;
}

/** Read host-endian 64-bit int from memory */
static inline uint64_t h64dec(const void *p)
{
	uint64_t tmp;
	memcpy(&tmp, p, sizeof(tmp));
	return tmp;
}

/**
 * @}
 *
 * @name Convert host value to LE/BE and write to memory
 *
 * @{
 */

/** Write big-endian 16-bit int to memory */
static inline void be16enc(void *p, uint16_t x)
{
	uint16_t tmp = htobe16(x);
	memcpy(p, &tmp, sizeof(tmp));
}

/** Write big-endian 32-bit int to memory */
static inline void be32enc(void *p, uint32_t x)
{
	uint32_t tmp = htobe32(x);
	memcpy(p, &tmp, sizeof(tmp));
}

/** Write big-endian 64-bit int to memory */
static inline void be64enc(void *p, uint64_t x)
{
	uint64_t tmp = htobe64(x);
	memcpy(p, &tmp, sizeof(tmp));
}

/** Write little-endian 16-bit int to memory */
static inline void le16enc(void *p, uint16_t x)
{
	uint16_t tmp = htole16(x);
	memcpy(p, &tmp, sizeof(tmp));
}

/** Write little-endian 32-bit int to memory */
static inline void le32enc(void *p, uint32_t x)
{
	uint32_t tmp = htole32(x);
	memcpy(p, &tmp, sizeof(tmp));
}

/** Write little-endian 64-bit int to memory */
static inline void le64enc(void *p, uint64_t x)
{
	uint64_t tmp = htole64(x);
	memcpy(p, &tmp, sizeof(tmp));
}

/** Write host-endian 16-bit int to memory */
static inline void h16enc(void *p, uint16_t x)
{
	memcpy(p, &x, sizeof(x));
}

/** Write host-endian 32-bit int to memory */
static inline void h32enc(void *p, uint32_t x)
{
	memcpy(p, &x, sizeof(x));
}

/** Write host-endian 64-bit int to memory */
static inline void h64enc(void *p, uint64_t x)
{
	memcpy(p, &x, sizeof(x));
}

/** @} */


#endif	/* _USUAL_ENDIAN_H_ */