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)
101 ////////////////////////////////
102 // Compiler-specific features //
103 ////////////////////////////////
105 // Newer Intel C compilers require immintrin.h for _bit_scan_reverse()
106 // and such functions.
107 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1500)
108 # include <immintrin.h>
117 # define bswap16(num) \
118 (((uint16_t)(num) << 8) | ((uint16_t)(num) >> 8))
122 # define bswap32(num) \
123 ( (((uint32_t)(num) << 24) ) \
124 | (((uint32_t)(num) << 8) & UINT32_C(0x00FF0000)) \
125 | (((uint32_t)(num) >> 8) & UINT32_C(0x0000FF00)) \
126 | (((uint32_t)(num) >> 24) ) )
130 # define bswap64(num) \
131 ( (((uint64_t)(num) << 56) ) \
132 | (((uint64_t)(num) << 40) & UINT64_C(0x00FF000000000000)) \
133 | (((uint64_t)(num) << 24) & UINT64_C(0x0000FF0000000000)) \
134 | (((uint64_t)(num) << 8) & UINT64_C(0x000000FF00000000)) \
135 | (((uint64_t)(num) >> 8) & UINT64_C(0x00000000FF000000)) \
136 | (((uint64_t)(num) >> 24) & UINT64_C(0x0000000000FF0000)) \
137 | (((uint64_t)(num) >> 40) & UINT64_C(0x000000000000FF00)) \
138 | (((uint64_t)(num) >> 56) ) )
141 // Define conversion macros using the basic byte swapping macros.
142 #ifdef WORDS_BIGENDIAN
144 # define conv16be(num) ((uint16_t)(num))
147 # define conv32be(num) ((uint32_t)(num))
150 # define conv64be(num) ((uint64_t)(num))
153 # define conv16le(num) bswap16(num)
156 # define conv32le(num) bswap32(num)
159 # define conv64le(num) bswap64(num)
163 # define conv16be(num) bswap16(num)
166 # define conv32be(num) bswap32(num)
169 # define conv64be(num) bswap64(num)
172 # define conv16le(num) ((uint16_t)(num))
175 # define conv32le(num) ((uint32_t)(num))
178 # define conv64le(num) ((uint64_t)(num))
183 //////////////////////////////
184 // Aligned reads and writes //
185 //////////////////////////////
187 static inline uint16_t
188 read16be(const uint8_t *buf
)
190 uint16_t num
= *(const uint16_t *)buf
;
191 return conv16be(num
);
195 static inline uint16_t
196 read16le(const uint8_t *buf
)
198 uint16_t num
= *(const uint16_t *)buf
;
199 return conv16le(num
);
203 static inline uint32_t
204 read32be(const uint8_t *buf
)
206 uint32_t num
= *(const uint32_t *)buf
;
207 return conv32be(num
);
211 static inline uint32_t
212 read32le(const uint8_t *buf
)
214 uint32_t num
= *(const uint32_t *)buf
;
215 return conv32le(num
);
219 static inline uint64_t
220 read64be(const uint8_t *buf
)
222 uint64_t num
= *(const uint64_t *)buf
;
223 return conv64be(num
);
227 static inline uint64_t
228 read64le(const uint8_t *buf
)
230 uint64_t num
= *(const uint64_t *)buf
;
231 return conv64le(num
);
235 // NOTE: Possible byte swapping must be done in a macro to allow GCC
236 // to optimize byte swapping of constants when using glibc's or *BSD's
237 // byte swapping macros. The actual write is done in an inline function
238 // to make type checking of the buf pointer possible similarly to readXXYe()
241 #define write16be(buf, num) write16ne((buf), conv16be(num))
242 #define write16le(buf, num) write16ne((buf), conv16le(num))
243 #define write32be(buf, num) write32ne((buf), conv32be(num))
244 #define write32le(buf, num) write32ne((buf), conv32le(num))
245 #define write64be(buf, num) write64ne((buf), conv64be(num))
246 #define write64le(buf, num) write64ne((buf), conv64le(num))
250 write16ne(uint8_t *buf
, uint16_t num
)
252 *(uint16_t *)buf
= num
;
258 write32ne(uint8_t *buf
, uint32_t num
)
260 *(uint32_t *)buf
= num
;
266 write64ne(uint8_t *buf
, uint64_t num
)
268 *(uint64_t *)buf
= num
;
273 ////////////////////////////////
274 // Unaligned reads and writes //
275 ////////////////////////////////
277 // NOTE: TUKLIB_FAST_UNALIGNED_ACCESS indicates only support for 16-bit and
278 // 32-bit unaligned integer loads and stores. It's possible that 64-bit
279 // unaligned access doesn't work or is slower than byte-by-byte access.
280 // Since unaligned 64-bit is probably not needed as often as 16-bit or
281 // 32-bit, we simply don't support 64-bit unaligned access for now.
282 #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
283 # define unaligned_read16be read16be
284 # define unaligned_read16le read16le
285 # define unaligned_read32be read32be
286 # define unaligned_read32le read32le
287 # define unaligned_write16be write16be
288 # define unaligned_write16le write16le
289 # define unaligned_write32be write32be
290 # define unaligned_write32le write32le
294 static inline uint16_t
295 unaligned_read16be(const uint8_t *buf
)
297 uint16_t num
= ((uint16_t)buf
[0] << 8) | (uint16_t)buf
[1];
302 static inline uint16_t
303 unaligned_read16le(const uint8_t *buf
)
305 uint16_t num
= ((uint16_t)buf
[0]) | ((uint16_t)buf
[1] << 8);
310 static inline uint32_t
311 unaligned_read32be(const uint8_t *buf
)
313 uint32_t num
= (uint32_t)buf
[0] << 24;
314 num
|= (uint32_t)buf
[1] << 16;
315 num
|= (uint32_t)buf
[2] << 8;
316 num
|= (uint32_t)buf
[3];
321 static inline uint32_t
322 unaligned_read32le(const uint8_t *buf
)
324 uint32_t num
= (uint32_t)buf
[0];
325 num
|= (uint32_t)buf
[1] << 8;
326 num
|= (uint32_t)buf
[2] << 16;
327 num
|= (uint32_t)buf
[3] << 24;
333 unaligned_write16be(uint8_t *buf
, uint16_t num
)
335 buf
[0] = (uint8_t)(num
>> 8);
336 buf
[1] = (uint8_t)num
;
342 unaligned_write16le(uint8_t *buf
, uint16_t num
)
344 buf
[0] = (uint8_t)num
;
345 buf
[1] = (uint8_t)(num
>> 8);
351 unaligned_write32be(uint8_t *buf
, uint32_t num
)
353 buf
[0] = (uint8_t)(num
>> 24);
354 buf
[1] = (uint8_t)(num
>> 16);
355 buf
[2] = (uint8_t)(num
>> 8);
356 buf
[3] = (uint8_t)num
;
362 unaligned_write32le(uint8_t *buf
, uint32_t num
)
364 buf
[0] = (uint8_t)num
;
365 buf
[1] = (uint8_t)(num
>> 8);
366 buf
[2] = (uint8_t)(num
>> 16);
367 buf
[3] = (uint8_t)(num
>> 24);
374 static inline uint32_t
377 // Check for ICC first, since it tends to define __GNUC__ too.
378 #if defined(__INTEL_COMPILER)
379 return _bit_scan_reverse(n
);
381 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
382 // GCC >= 3.4 has __builtin_clz(), which gives good results on
383 // multiple architectures. On x86, __builtin_clz() ^ 31U becomes
384 // either plain BSR (so the XOR gets optimized away) or LZCNT and
385 // XOR (if -march indicates that SSE4a instructions are supported).
386 return __builtin_clz(n
) ^ 31U;
388 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
390 __asm__("bsrl %1, %0" : "=r" (i
) : "rm" (n
));
393 #elif defined(_MSC_VER) && _MSC_VER >= 1400
394 // MSVC isn't supported by tuklib, but since this code exists,
395 // it doesn't hurt to have it here anyway.
397 _BitScanReverse((DWORD
*)&i
, n
);
403 if ((n
& UINT32_C(0xFFFF0000)) == 0) {
408 if ((n
& UINT32_C(0xFF000000)) == 0) {
413 if ((n
& UINT32_C(0xF0000000)) == 0) {
418 if ((n
& UINT32_C(0xC0000000)) == 0) {
423 if ((n
& UINT32_C(0x80000000)) == 0)
431 static inline uint32_t
434 #if defined(__INTEL_COMPILER)
435 return _bit_scan_reverse(n
) ^ 31U;
437 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
438 return __builtin_clz(n
);
440 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
442 __asm__("bsrl %1, %0\n\t"
444 : "=r" (i
) : "rm" (n
));
447 #elif defined(_MSC_VER) && _MSC_VER >= 1400
449 _BitScanReverse((DWORD
*)&i
, n
);
455 if ((n
& UINT32_C(0xFFFF0000)) == 0) {
460 if ((n
& UINT32_C(0xFF000000)) == 0) {
465 if ((n
& UINT32_C(0xF0000000)) == 0) {
470 if ((n
& UINT32_C(0xC0000000)) == 0) {
475 if ((n
& UINT32_C(0x80000000)) == 0)
483 static inline uint32_t
486 #if defined(__INTEL_COMPILER)
487 return _bit_scan_forward(n
);
489 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX >= UINT32_MAX
490 return __builtin_ctz(n
);
492 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
494 __asm__("bsfl %1, %0" : "=r" (i
) : "rm" (n
));
497 #elif defined(_MSC_VER) && _MSC_VER >= 1400
499 _BitScanForward((DWORD
*)&i
, n
);
505 if ((n
& UINT32_C(0x0000FFFF)) == 0) {
510 if ((n
& UINT32_C(0x000000FF)) == 0) {
515 if ((n
& UINT32_C(0x0000000F)) == 0) {
520 if ((n
& UINT32_C(0x00000003)) == 0) {
525 if ((n
& UINT32_C(0x00000001)) == 0)