1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file tuklib_integer.h
4 /// \brief Byte swapping and endianness related macros and functions
6 /// This file provides macros or functions to do basic endianness related
7 /// integer operations (XX = 16, 32, or 64; Y = b or l):
8 /// - Byte swapping: bswapXX(num)
9 /// - Byte order conversions to/from native: convXXYe(num)
10 /// - Aligned reads: readXXYe(ptr)
11 /// - Aligned writes: writeXXYe(ptr, num)
12 /// - Unaligned reads (16/32-bit only): unaligned_readXXYe(ptr)
13 /// - Unaligned writes (16/32-bit only): unaligned_writeXXYe(ptr, num)
15 /// Since they can macros, the arguments should have no side effects since
16 /// they may be evaluated more than once.
18 /// \todo PowerPC and possibly some other architectures support
19 /// byte swapping load and store instructions. This file
20 /// doesn't take advantage of those instructions.
22 // Author: Lasse Collin
24 // This file has been put into the public domain.
25 // You can do whatever you want with this file.
27 ///////////////////////////////////////////////////////////////////////////////
29 #ifndef TUKLIB_INTEGER_H
30 #define TUKLIB_INTEGER_H
32 #include "tuklib_common.h"
35 ////////////////////////////////////////
36 // Operating system specific features //
37 ////////////////////////////////////////
39 #if defined(HAVE_BYTESWAP_H)
40 // glibc, uClibc, dietlibc
41 # include <byteswap.h>
43 # define bswap16(num) bswap_16(num)
46 # define bswap32(num) bswap_32(num)
49 # define bswap64(num) bswap_64(num)
52 #elif defined(HAVE_SYS_ENDIAN_H)
54 # include <sys/endian.h>
56 #elif defined(HAVE_SYS_BYTEORDER_H)
58 # include <sys/byteorder.h>
60 # define bswap16(num) BSWAP_16(num)
63 # define bswap32(num) BSWAP_32(num)
66 # define bswap64(num) BSWAP_64(num)
69 # define conv16be(num) BE_16(num)
72 # define conv32be(num) BE_32(num)
75 # define conv64be(num) BE_64(num)
78 # define conv16le(num) LE_16(num)
81 # define conv32le(num) LE_32(num)
84 # define conv64le(num) LE_64(num)
94 # define bswap16(num) \
95 (((uint16_t)(num) << 8) | ((uint16_t)(num) >> 8))
99 # define bswap32(num) \
100 ( (((uint32_t)(num) << 24) ) \
101 | (((uint32_t)(num) << 8) & UINT32_C(0x00FF0000)) \
102 | (((uint32_t)(num) >> 8) & UINT32_C(0x0000FF00)) \
103 | (((uint32_t)(num) >> 24) ) )
107 # define bswap64(num) \
108 ( (((uint64_t)(num) << 56) ) \
109 | (((uint64_t)(num) << 40) & UINT64_C(0x00FF000000000000)) \
110 | (((uint64_t)(num) << 24) & UINT64_C(0x0000FF0000000000)) \
111 | (((uint64_t)(num) << 8) & UINT64_C(0x000000FF00000000)) \
112 | (((uint64_t)(num) >> 8) & UINT64_C(0x00000000FF000000)) \
113 | (((uint64_t)(num) >> 24) & UINT64_C(0x0000000000FF0000)) \
114 | (((uint64_t)(num) >> 40) & UINT64_C(0x000000000000FF00)) \
115 | (((uint64_t)(num) >> 56) ) )
118 // Define conversion macros using the basic byte swapping macros.
119 #ifdef WORDS_BIGENDIAN
121 # define conv16be(num) ((uint16_t)(num))
124 # define conv32be(num) ((uint32_t)(num))
127 # define conv64be(num) ((uint64_t)(num))
130 # define conv16le(num) bswap16(num)
133 # define conv32le(num) bswap32(num)
136 # define conv64le(num) bswap64(num)
140 # define conv16be(num) bswap16(num)
143 # define conv32be(num) bswap32(num)
146 # define conv64be(num) bswap64(num)
149 # define conv16le(num) ((uint16_t)(num))
152 # define conv32le(num) ((uint32_t)(num))
155 # define conv64le(num) ((uint64_t)(num))
160 //////////////////////////////
161 // Aligned reads and writes //
162 //////////////////////////////
164 static inline uint16_t
165 read16be(const uint8_t *buf)
167 uint16_t num = *(const uint16_t *)buf;
168 return conv16be(num);
172 static inline uint16_t
173 read16le(const uint8_t *buf)
175 uint16_t num = *(const uint16_t *)buf;
176 return conv16le(num);
180 static inline uint32_t
181 read32be(const uint8_t *buf)
183 uint32_t num = *(const uint32_t *)buf;
184 return conv32be(num);
188 static inline uint32_t
189 read32le(const uint8_t *buf)
191 uint32_t num = *(const uint32_t *)buf;
192 return conv32le(num);
196 static inline uint64_t
197 read64be(const uint8_t *buf)
199 uint64_t num = *(const uint64_t *)buf;
200 return conv64be(num);
204 static inline uint64_t
205 read64le(const uint8_t *buf)
207 uint64_t num = *(const uint64_t *)buf;
208 return conv64le(num);
212 // NOTE: Possible byte swapping must be done in a macro to allow GCC
213 // to optimize byte swapping of constants when using glibc's or *BSD's
214 // byte swapping macros. The actual write is done in an inline function
215 // to make type checking of the buf pointer possible similarly to readXXYe()
216 // functions. This also seems to silence a probably bogus GCC warning about
217 // strict aliasing when buf points to the beginning of an uint8_t array.
219 #define write16be(buf, num) write16ne((buf), conv16be(num))
220 #define write16le(buf, num) write16ne((buf), conv16le(num))
221 #define write32be(buf, num) write32ne((buf), conv32be(num))
222 #define write32le(buf, num) write32ne((buf), conv32le(num))
223 #define write64be(buf, num) write64ne((buf), conv64be(num))
224 #define write64le(buf, num) write64ne((buf), conv64le(num))
228 write16ne(uint8_t *buf, uint16_t num)
230 *(uint16_t *)buf = num;
236 write32ne(uint8_t *buf, uint32_t num)
238 *(uint32_t *)buf = num;
244 write64ne(uint8_t *buf, uint64_t num)
246 *(uint64_t *)buf = num;
251 ////////////////////////////////
252 // Unaligned reads and writes //
253 ////////////////////////////////
255 // NOTE: TUKLIB_FAST_UNALIGNED_ACCESS indicates only support for 16-bit and
256 // 32-bit unaligned integer loads and stores. It's possible that 64-bit
257 // unaligned access doesn't work or is slower than byte-by-byte access.
258 // Since unaligned 64-bit is probably not needed as often as 16-bit or
259 // 32-bit, we simply don't support 64-bit unaligned access for now.
260 #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
261 # define unaligned_read16be read16be
262 # define unaligned_read16le read16le
263 # define unaligned_read32be read32be
264 # define unaligned_read32le read32le
265 # define unaligned_write16be write16be
266 # define unaligned_write16le write16le
267 # define unaligned_write32be write32be
268 # define unaligned_write32le write32le
272 static inline uint16_t
273 unaligned_read16be(const uint8_t *buf)
275 uint16_t num = ((uint16_t)buf[0] << 8) | buf[1];
280 static inline uint16_t
281 unaligned_read16le(const uint8_t *buf)
283 uint16_t num = ((uint32_t)buf[0]) | ((uint16_t)buf[1] << 8);
288 static inline uint32_t
289 unaligned_read32be(const uint8_t *buf)
291 uint32_t num = (uint32_t)buf[0] << 24;
292 num |= (uint32_t)buf[1] << 16;
293 num |= (uint32_t)buf[2] << 8;
294 num |= (uint32_t)buf[3];
299 static inline uint32_t
300 unaligned_read32le(const uint8_t *buf)
302 uint32_t num = (uint32_t)buf[0];
303 num |= (uint32_t)buf[1] << 8;
304 num |= (uint32_t)buf[2] << 16;
305 num |= (uint32_t)buf[3] << 24;
311 unaligned_write16be(uint8_t *buf, uint16_t num)
320 unaligned_write16le(uint8_t *buf, uint16_t num)
329 unaligned_write32be(uint8_t *buf, uint32_t num)
340 unaligned_write32le(uint8_t *buf, uint32_t num)