1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file tuklib_integer.h
4 /// \brief Various integer and bit operations
6 /// This file provides macros or functions to do some basic integer and bit
9 /// Endianness related integer operations (XX = 16, 32, or 64; Y = b or l):
10 /// - Byte swapping: bswapXX(num)
11 /// - Byte order conversions to/from native: convXXYe(num)
12 /// - Aligned reads: readXXYe(ptr)
13 /// - Aligned writes: writeXXYe(ptr, num)
14 /// - Unaligned reads (16/32-bit only): unaligned_readXXYe(ptr)
15 /// - Unaligned writes (16/32-bit only): unaligned_writeXXYe(ptr, num)
17 /// Since they can macros, the arguments should have no side effects since
18 /// they may be evaluated more than once.
20 /// \todo PowerPC and possibly some other architectures support
21 /// byte swapping load and store instructions. This file
22 /// doesn't take advantage of those instructions.
24 /// Bit scan operations for non-zero 32-bit integers:
25 /// - Bit scan reverse (find highest non-zero bit): bsr32(num)
26 /// - Count leading zeros: clz32(num)
27 /// - Count trailing zeros: ctz32(num)
28 /// - Bit scan forward (simply an alias for ctz32()): bsf32(num)
30 /// The above bit scan operations return 0-31. If num is zero,
31 /// the result is undefined.
33 // Authors: Lasse Collin
36 // This file has been put into the public domain.
37 // You can do whatever you want with this file.
39 ///////////////////////////////////////////////////////////////////////////////
41 #ifndef TUKLIB_INTEGER_H
42 #define TUKLIB_INTEGER_H
44 #include "tuklib_common.h"
47 ////////////////////////////////////////
48 // Operating system specific features //
49 ////////////////////////////////////////
51 #if defined(HAVE_BYTESWAP_H)
52 // glibc, uClibc, dietlibc
53 # include <byteswap.h>
55 # define bswap16(num) bswap_16(num)
58 # define bswap32(num) bswap_32(num)
61 # define bswap64(num) bswap_64(num)
64 #elif defined(HAVE_SYS_ENDIAN_H)
66 # include <sys/endian.h>
68 #elif defined(HAVE_SYS_BYTEORDER_H)
70 # include <sys/byteorder.h>
72 # define bswap16(num) BSWAP_16(num)
75 # define bswap32(num) BSWAP_32(num)
78 # define bswap64(num) BSWAP_64(num)
81 # define conv16be(num) BE_16(num)
84 # define conv32be(num) BE_32(num)
87 # define conv64be(num) BE_64(num)
90 # define conv16le(num) LE_16(num)
93 # define conv32le(num) LE_32(num)
96 # define conv64le(num) LE_64(num)
106 # define bswap16(num) \
107 (((uint16_t)(num) << 8) | ((uint16_t)(num) >> 8))
111 # define bswap32(num) \
112 ( (((uint32_t)(num) << 24) ) \
113 | (((uint32_t)(num) << 8) & UINT32_C(0x00FF0000)) \
114 | (((uint32_t)(num) >> 8) & UINT32_C(0x0000FF00)) \
115 | (((uint32_t)(num) >> 24) ) )
119 # define bswap64(num) \
120 ( (((uint64_t)(num) << 56) ) \
121 | (((uint64_t)(num) << 40) & UINT64_C(0x00FF000000000000)) \
122 | (((uint64_t)(num) << 24) & UINT64_C(0x0000FF0000000000)) \
123 | (((uint64_t)(num) << 8) & UINT64_C(0x000000FF00000000)) \
124 | (((uint64_t)(num) >> 8) & UINT64_C(0x00000000FF000000)) \
125 | (((uint64_t)(num) >> 24) & UINT64_C(0x0000000000FF0000)) \
126 | (((uint64_t)(num) >> 40) & UINT64_C(0x000000000000FF00)) \
127 | (((uint64_t)(num) >> 56) ) )
130 // Define conversion macros using the basic byte swapping macros.
131 #ifdef WORDS_BIGENDIAN
133 # define conv16be(num) ((uint16_t)(num))
136 # define conv32be(num) ((uint32_t)(num))
139 # define conv64be(num) ((uint64_t)(num))
142 # define conv16le(num) bswap16(num)
145 # define conv32le(num) bswap32(num)
148 # define conv64le(num) bswap64(num)
152 # define conv16be(num) bswap16(num)
155 # define conv32be(num) bswap32(num)
158 # define conv64be(num) bswap64(num)
161 # define conv16le(num) ((uint16_t)(num))
164 # define conv32le(num) ((uint32_t)(num))
167 # define conv64le(num) ((uint64_t)(num))
172 //////////////////////////////
173 // Aligned reads and writes //
174 //////////////////////////////
176 static inline uint16_t
177 read16be(const uint8_t *buf)
179 uint16_t num = *(const uint16_t *)buf;
180 return conv16be(num);
184 static inline uint16_t
185 read16le(const uint8_t *buf)
187 uint16_t num = *(const uint16_t *)buf;
188 return conv16le(num);
192 static inline uint32_t
193 read32be(const uint8_t *buf)
195 uint32_t num = *(const uint32_t *)buf;
196 return conv32be(num);
200 static inline uint32_t
201 read32le(const uint8_t *buf)
203 uint32_t num = *(const uint32_t *)buf;
204 return conv32le(num);
208 static inline uint64_t
209 read64be(const uint8_t *buf)
211 uint64_t num = *(const uint64_t *)buf;
212 return conv64be(num);
216 static inline uint64_t
217 read64le(const uint8_t *buf)
219 uint64_t num = *(const uint64_t *)buf;
220 return conv64le(num);
224 // NOTE: Possible byte swapping must be done in a macro to allow GCC
225 // to optimize byte swapping of constants when using glibc's or *BSD's
226 // byte swapping macros. The actual write is done in an inline function
227 // to make type checking of the buf pointer possible similarly to readXXYe()
230 #define write16be(buf, num) write16ne((buf), conv16be(num))
231 #define write16le(buf, num) write16ne((buf), conv16le(num))
232 #define write32be(buf, num) write32ne((buf), conv32be(num))
233 #define write32le(buf, num) write32ne((buf), conv32le(num))
234 #define write64be(buf, num) write64ne((buf), conv64be(num))
235 #define write64le(buf, num) write64ne((buf), conv64le(num))
239 write16ne(uint8_t *buf, uint16_t num)
241 *(uint16_t *)buf = num;
247 write32ne(uint8_t *buf, uint32_t num)
249 *(uint32_t *)buf = num;
255 write64ne(uint8_t *buf, uint64_t num)
257 *(uint64_t *)buf = num;
262 ////////////////////////////////
263 // Unaligned reads and writes //
264 ////////////////////////////////
266 // NOTE: TUKLIB_FAST_UNALIGNED_ACCESS indicates only support for 16-bit and
267 // 32-bit unaligned integer loads and stores. It's possible that 64-bit
268 // unaligned access doesn't work or is slower than byte-by-byte access.
269 // Since unaligned 64-bit is probably not needed as often as 16-bit or
270 // 32-bit, we simply don't support 64-bit unaligned access for now.
271 #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
272 # define unaligned_read16be read16be
273 # define unaligned_read16le read16le
274 # define unaligned_read32be read32be
275 # define unaligned_read32le read32le
276 # define unaligned_write16be write16be
277 # define unaligned_write16le write16le
278 # define unaligned_write32be write32be
279 # define unaligned_write32le write32le
283 static inline uint16_t
284 unaligned_read16be(const uint8_t *buf)
286 uint16_t num = ((uint16_t)buf[0] << 8) | (uint16_t)buf[1];
291 static inline uint16_t
292 unaligned_read16le(const uint8_t *buf)
294 uint16_t num = ((uint16_t)buf[0]) | ((uint16_t)buf[1] << 8);
299 static inline uint32_t
300 unaligned_read32be(const uint8_t *buf)
302 uint32_t num = (uint32_t)buf[0] << 24;
303 num |= (uint32_t)buf[1] << 16;
304 num |= (uint32_t)buf[2] << 8;
305 num |= (uint32_t)buf[3];
310 static inline uint32_t
311 unaligned_read32le(const uint8_t *buf)
313 uint32_t num = (uint32_t)buf[0];
314 num |= (uint32_t)buf[1] << 8;
315 num |= (uint32_t)buf[2] << 16;
316 num |= (uint32_t)buf[3] << 24;
322 unaligned_write16be(uint8_t *buf, uint16_t num)
324 buf[0] = (uint8_t)(num >> 8);
325 buf[1] = (uint8_t)num;
331 unaligned_write16le(uint8_t *buf, uint16_t num)
333 buf[0] = (uint8_t)num;
334 buf[1] = (uint8_t)(num >> 8);
340 unaligned_write32be(uint8_t *buf, uint32_t num)
342 buf[0] = (uint8_t)(num >> 24);
343 buf[1] = (uint8_t)(num >> 16);
344 buf[2] = (uint8_t)(num >> 8);
345 buf[3] = (uint8_t)num;
351 unaligned_write32le(uint8_t *buf, uint32_t num)
353 buf[0] = (uint8_t)num;
354 buf[1] = (uint8_t)(num >> 8);
355 buf[2] = (uint8_t)(num >> 16);
356 buf[3] = (uint8_t)(num >> 24);
363 static inline uint32_t
366 // Check for ICC first, since it tends to define __GNUC__ too.
367 #if defined(__INTEL_COMPILER)
368 return _bit_scan_reverse(n);
370 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
371 // GCC >= 3.4 has __builtin_clz(), which gives good results on
372 // multiple architectures. On x86, __builtin_clz() ^ 31U becomes
373 // either plain BSR (so the XOR gets optimized away) or LZCNT and
374 // XOR (if -march indicates that SSE4a instructions are supported).
375 return __builtin_clz(n) ^ 31U;
377 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
379 __asm__("bsrl %1, %0" : "=r" (i) : "rm" (n));
382 #elif defined(_MSC_VER) && _MSC_VER >= 1400
383 // MSVC isn't supported by tuklib, but since this code exists,
384 // it doesn't hurt to have it here anyway.
386 _BitScanReverse((DWORD *)&i, n);
392 if ((n & UINT32_C(0xFFFF0000)) == 0) {
397 if ((n & UINT32_C(0xFF000000)) == 0) {
402 if ((n & UINT32_C(0xF0000000)) == 0) {
407 if ((n & UINT32_C(0xC0000000)) == 0) {
412 if ((n & UINT32_C(0x80000000)) == 0)
420 static inline uint32_t
423 #if defined(__INTEL_COMPILER)
424 return _bit_scan_reverse(n) ^ 31U;
426 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
427 return __builtin_clz(n);
429 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
431 __asm__("bsrl %1, %0\n\t"
433 : "=r" (i) : "rm" (n));
436 #elif defined(_MSC_VER) && _MSC_VER >= 1400
438 _BitScanReverse((DWORD *)&i, n);
444 if ((n & UINT32_C(0xFFFF0000)) == 0) {
449 if ((n & UINT32_C(0xFF000000)) == 0) {
454 if ((n & UINT32_C(0xF0000000)) == 0) {
459 if ((n & UINT32_C(0xC0000000)) == 0) {
464 if ((n & UINT32_C(0x80000000)) == 0)
472 static inline uint32_t
475 #if defined(__INTEL_COMPILER)
476 return _bit_scan_forward(n);
478 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX >= UINT32_MAX
479 return __builtin_ctz(n);
481 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
483 __asm__("bsfl %1, %0" : "=r" (i) : "rm" (n));
486 #elif defined(_MSC_VER) && _MSC_VER >= 1400
488 _BitScanForward((DWORD *)&i, n);
494 if ((n & UINT32_C(0x0000FFFF)) == 0) {
499 if ((n & UINT32_C(0x000000FF)) == 0) {
504 if ((n & UINT32_C(0x0000000F)) == 0) {
509 if ((n & UINT32_C(0x00000003)) == 0) {
514 if ((n & UINT32_C(0x00000001)) == 0)