45 #ifndef TUKLIB_INTEGER_H
46 #define TUKLIB_INTEGER_H
53 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1500)
54 # include <immintrin.h>
62 #if defined(HAVE___BUILTIN_BSWAPXX)
64 # define bswap16(n) __builtin_bswap16(n)
65 # define bswap32(n) __builtin_bswap32(n)
66 # define bswap64(n) __builtin_bswap64(n)
68 #elif defined(HAVE_BYTESWAP_H)
70 # include <byteswap.h>
72 # define bswap16(num) bswap_16(num)
75 # define bswap32(num) bswap_32(num)
78 # define bswap64(num) bswap_64(num)
81 #elif defined(HAVE_SYS_ENDIAN_H)
83 # include <sys/endian.h>
85 #elif defined(HAVE_SYS_BYTEORDER_H)
87 # include <sys/byteorder.h>
89 # define bswap16(num) BSWAP_16(num)
92 # define bswap32(num) BSWAP_32(num)
95 # define bswap64(num) BSWAP_64(num)
98 # define conv16be(num) BE_16(num)
101 # define conv32be(num) BE_32(num)
104 # define conv64be(num) BE_64(num)
107 # define conv16le(num) LE_16(num)
110 # define conv32le(num) LE_32(num)
113 # define conv64le(num) LE_64(num)
118 # define bswap16(n) (uint16_t)( \
119 (((n) & 0x00FFU) << 8) \
120 | (((n) & 0xFF00U) >> 8) \
125 # define bswap32(n) (uint32_t)( \
126 (((n) & UINT32_C(0x000000FF)) << 24) \
127 | (((n) & UINT32_C(0x0000FF00)) << 8) \
128 | (((n) & UINT32_C(0x00FF0000)) >> 8) \
129 | (((n) & UINT32_C(0xFF000000)) >> 24) \
134 # define bswap64(n) (uint64_t)( \
135 (((n) & UINT64_C(0x00000000000000FF)) << 56) \
136 | (((n) & UINT64_C(0x000000000000FF00)) << 40) \
137 | (((n) & UINT64_C(0x0000000000FF0000)) << 24) \
138 | (((n) & UINT64_C(0x00000000FF000000)) << 8) \
139 | (((n) & UINT64_C(0x000000FF00000000)) >> 8) \
140 | (((n) & UINT64_C(0x0000FF0000000000)) >> 24) \
141 | (((n) & UINT64_C(0x00FF000000000000)) >> 40) \
142 | (((n) & UINT64_C(0xFF00000000000000)) >> 56) \
147 #ifdef WORDS_BIGENDIAN
149 # define conv16be(num) ((uint16_t)(num))
152 # define conv32be(num) ((uint32_t)(num))
155 # define conv64be(num) ((uint64_t)(num))
158 # define conv16le(num) bswap16(num)
161 # define conv32le(num) bswap32(num)
164 # define conv64le(num) bswap64(num)
168 # define conv16be(num) bswap16(num)
171 # define conv32be(num) bswap32(num)
174 # define conv64be(num) bswap64(num)
177 # define conv16le(num) ((uint16_t)(num))
180 # define conv32le(num) ((uint32_t)(num))
183 # define conv64le(num) ((uint64_t)(num))
210 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
211 && defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
224 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
225 && defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
238 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
239 && defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
252 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
253 && defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
265 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
266 && defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
278 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
279 && defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
291 #if defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
304 #if !defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
317 #if defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
333 #if !defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
350 #if defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
351 # define write16be(buf, num) write16ne(buf, conv16be(num))
352 # define write32be(buf, num) write32ne(buf, conv32be(num))
355 #if !defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
356 # define write16le(buf, num) write16ne(buf, conv16le(num))
357 # define write32le(buf, num) write32ne(buf, conv32le(num))
433 #ifdef HAVE___BUILTIN_ASSUME_ALIGNED
434 # define tuklib_memcpy_aligned(dest, src, size) \
435 memcpy(dest, __builtin_assume_aligned(src, size), size)
437 # define tuklib_memcpy_aligned(dest, src, size) \
438 memcpy(dest, src, size)
439 # ifndef TUKLIB_FAST_UNALIGNED_ACCESS
440 # define TUKLIB_USE_UNSAFE_ALIGNED_READS 1
448 #if defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING) \
449 || defined(TUKLIB_USE_UNSAFE_ALIGNED_READS)
462 #if defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING) \
463 || defined(TUKLIB_USE_UNSAFE_ALIGNED_READS)
476 #if defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING) \
477 || defined(TUKLIB_USE_UNSAFE_ALIGNED_READS)
490 #ifdef TUKLIB_USE_UNSAFE_TYPE_PUNNING
502 #ifdef TUKLIB_USE_UNSAFE_TYPE_PUNNING
514 #ifdef TUKLIB_USE_UNSAFE_TYPE_PUNNING
572 #define aligned_write16be(buf, num) aligned_write16ne((buf), conv16be(num))
573 #define aligned_write16le(buf, num) aligned_write16ne((buf), conv16le(num))
574 #define aligned_write32be(buf, num) aligned_write32ne((buf), conv32be(num))
575 #define aligned_write32le(buf, num) aligned_write32ne((buf), conv32le(num))
576 #define aligned_write64be(buf, num) aligned_write64ne((buf), conv64be(num))
577 #define aligned_write64le(buf, num) aligned_write64ne((buf), conv64le(num))
588 #if defined(__INTEL_COMPILER)
589 return _bit_scan_reverse(
n);
591 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
598 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
600 __asm__(
"bsrl %1, %0" :
"=r" (
i) :
"rm" (
n));
603 #elif defined(_MSC_VER)
605 _BitScanReverse(&
i,
n);
611 if ((
n & 0xFFFF0000) == 0) {
616 if ((
n & 0xFF000000) == 0) {
621 if ((
n & 0xF0000000) == 0) {
626 if ((
n & 0xC0000000) == 0) {
631 if ((
n & 0x80000000) == 0)
642 #if defined(__INTEL_COMPILER)
643 return _bit_scan_reverse(
n) ^ 31U;
645 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
648 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
650 __asm__(
"bsrl %1, %0\n\t"
652 :
"=r" (
i) :
"rm" (
n));
655 #elif defined(_MSC_VER)
657 _BitScanReverse(&
i,
n);
663 if ((
n & 0xFFFF0000) == 0) {
668 if ((
n & 0xFF000000) == 0) {
673 if ((
n & 0xF0000000) == 0) {
678 if ((
n & 0xC0000000) == 0) {
683 if ((
n & 0x80000000) == 0)
694 #if defined(__INTEL_COMPILER)
695 return _bit_scan_forward(
n);
697 #elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX >= UINT32_MAX
700 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
702 __asm__(
"bsfl %1, %0" :
"=r" (
i) :
"rm" (
n));
705 #elif defined(_MSC_VER)
707 _BitScanForward(&
i,
n);
713 if ((
n & 0x0000FFFF) == 0) {
718 if ((
n & 0x000000FF) == 0) {
723 if ((
n & 0x0000000F) == 0) {
728 if ((
n & 0x00000003) == 0) {
733 if ((
n & 0x00000001) == 0)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Common definitions for tuklib modules.
static void aligned_write16ne(uint8_t *buf, uint16_t num)
static void write64ne(uint8_t *buf, uint64_t num)
static uint32_t aligned_read32le(const uint8_t *buf)
static uint32_t read32le(const uint8_t *buf)
static uint32_t clz32(uint32_t n)
static uint16_t read16ne(const uint8_t *buf)
static void write32be(uint8_t *buf, uint32_t num)
static uint16_t aligned_read16ne(const uint8_t *buf)
static uint64_t aligned_read64le(const uint8_t *buf)
static void aligned_write32ne(uint8_t *buf, uint32_t num)
static uint64_t aligned_read64be(const uint8_t *buf)
static uint32_t ctz32(uint32_t n)
static void write16ne(uint8_t *buf, uint16_t num)
static void aligned_write64ne(uint8_t *buf, uint64_t num)
static uint32_t aligned_read32be(const uint8_t *buf)
static uint16_t aligned_read16le(const uint8_t *buf)
static uint32_t aligned_read32ne(const uint8_t *buf)
static uint16_t read16le(const uint8_t *buf)
#define tuklib_memcpy_aligned(dest, src, size)
static uint32_t bsr32(uint32_t n)
static void write16be(uint8_t *buf, uint16_t num)
static uint32_t read32be(const uint8_t *buf)
static uint64_t aligned_read64ne(const uint8_t *buf)
static uint16_t read16be(const uint8_t *buf)
static void write32ne(uint8_t *buf, uint32_t num)
static uint32_t read32ne(const uint8_t *buf)
static uint64_t read64ne(const uint8_t *buf)
static uint16_t aligned_read16be(const uint8_t *buf)
#define write16le(buf, num)
#define write32le(buf, num)