2009-10-04 15:57:12 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file tuklib_integer.h
|
2009-11-22 04:52:30 -05:00
|
|
|
/// \brief Various integer and bit operations
|
2009-10-04 15:57:12 -04:00
|
|
|
///
|
2009-11-22 04:52:30 -05:00
|
|
|
/// This file provides macros or functions to do some basic integer and bit
|
|
|
|
/// operations.
|
|
|
|
///
|
2019-12-30 17:18:24 -05:00
|
|
|
/// Native endian inline functions (XX = 16, 32, or 64):
|
2019-12-30 17:41:28 -05:00
|
|
|
/// - Unaligned native endian reads: readXXne(ptr)
|
|
|
|
/// - Unaligned native endian writes: writeXXne(ptr, num)
|
2019-12-30 17:29:48 -05:00
|
|
|
/// - Aligned native endian reads: aligned_readXXne(ptr)
|
|
|
|
/// - Aligned native endian writes: aligned_writeXXne(ptr, num)
|
2019-12-30 17:18:24 -05:00
|
|
|
///
|
|
|
|
/// Endianness-converting integer operations (these can be macros!)
|
|
|
|
/// (XX = 16, 32, or 64; Y = b or l):
|
2009-10-04 15:57:12 -04:00
|
|
|
/// - Byte swapping: bswapXX(num)
|
2019-12-30 17:18:24 -05:00
|
|
|
/// - Byte order conversions to/from native (byteswaps if Y isn't
|
|
|
|
/// the native endianness): convXXYe(num)
|
2019-12-30 17:41:28 -05:00
|
|
|
/// - Unaligned reads (16/32-bit only): readXXYe(ptr)
|
|
|
|
/// - Unaligned writes (16/32-bit only): writeXXYe(ptr, num)
|
2019-12-30 17:29:48 -05:00
|
|
|
/// - Aligned reads: aligned_readXXYe(ptr)
|
|
|
|
/// - Aligned writes: aligned_writeXXYe(ptr, num)
|
2009-10-04 15:57:12 -04:00
|
|
|
///
|
2019-12-30 17:18:24 -05:00
|
|
|
/// Since the above can macros, the arguments should have no side effects
|
|
|
|
/// because they may be evaluated more than once.
|
2009-11-22 04:52:30 -05:00
|
|
|
///
|
2019-12-30 17:18:24 -05:00
|
|
|
/// Bit scan operations for non-zero 32-bit integers (inline functions):
|
2009-11-22 04:52:30 -05:00
|
|
|
/// - Bit scan reverse (find highest non-zero bit): bsr32(num)
|
|
|
|
/// - Count leading zeros: clz32(num)
|
|
|
|
/// - Count trailing zeros: ctz32(num)
|
|
|
|
/// - Bit scan forward (simply an alias for ctz32()): bsf32(num)
|
|
|
|
///
|
|
|
|
/// The above bit scan operations return 0-31. If num is zero,
|
|
|
|
/// the result is undefined.
|
2009-10-04 15:57:12 -04:00
|
|
|
//
|
2009-11-22 04:52:30 -05:00
|
|
|
// Authors: Lasse Collin
|
|
|
|
// Joachim Henke
|
2009-10-04 15:57:12 -04:00
|
|
|
//
|
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef TUKLIB_INTEGER_H
|
|
|
|
#define TUKLIB_INTEGER_H
|
|
|
|
|
|
|
|
#include "tuklib_common.h"
|
2019-06-01 11:41:16 -04:00
|
|
|
#include <string.h>
|
2009-10-04 15:57:12 -04:00
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
// Newer Intel C compilers require immintrin.h for _bit_scan_reverse()
|
|
|
|
// and such functions.
|
|
|
|
#if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1500)
|
|
|
|
# include <immintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2009-10-04 15:57:12 -04:00
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
///////////////////
|
|
|
|
// Byte swapping //
|
|
|
|
///////////////////
|
|
|
|
|
|
|
|
#if defined(HAVE___BUILTIN_BSWAPXX)
|
|
|
|
// GCC >= 4.8 and Clang
|
|
|
|
# define bswap16(n) __builtin_bswap16(n)
|
|
|
|
# define bswap32(n) __builtin_bswap32(n)
|
|
|
|
# define bswap64(n) __builtin_bswap64(n)
|
2009-10-04 15:57:12 -04:00
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
#elif defined(HAVE_BYTESWAP_H)
|
2009-10-04 15:57:12 -04:00
|
|
|
// glibc, uClibc, dietlibc
|
|
|
|
# include <byteswap.h>
|
|
|
|
# ifdef HAVE_BSWAP_16
|
|
|
|
# define bswap16(num) bswap_16(num)
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_BSWAP_32
|
|
|
|
# define bswap32(num) bswap_32(num)
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_BSWAP_64
|
|
|
|
# define bswap64(num) bswap_64(num)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#elif defined(HAVE_SYS_ENDIAN_H)
|
|
|
|
// *BSDs and Darwin
|
|
|
|
# include <sys/endian.h>
|
|
|
|
|
|
|
|
#elif defined(HAVE_SYS_BYTEORDER_H)
|
|
|
|
// Solaris
|
|
|
|
# include <sys/byteorder.h>
|
|
|
|
# ifdef BSWAP_16
|
|
|
|
# define bswap16(num) BSWAP_16(num)
|
|
|
|
# endif
|
|
|
|
# ifdef BSWAP_32
|
|
|
|
# define bswap32(num) BSWAP_32(num)
|
|
|
|
# endif
|
|
|
|
# ifdef BSWAP_64
|
|
|
|
# define bswap64(num) BSWAP_64(num)
|
|
|
|
# endif
|
|
|
|
# ifdef BE_16
|
|
|
|
# define conv16be(num) BE_16(num)
|
|
|
|
# endif
|
|
|
|
# ifdef BE_32
|
|
|
|
# define conv32be(num) BE_32(num)
|
|
|
|
# endif
|
|
|
|
# ifdef BE_64
|
|
|
|
# define conv64be(num) BE_64(num)
|
|
|
|
# endif
|
|
|
|
# ifdef LE_16
|
|
|
|
# define conv16le(num) LE_16(num)
|
|
|
|
# endif
|
|
|
|
# ifdef LE_32
|
|
|
|
# define conv32le(num) LE_32(num)
|
|
|
|
# endif
|
|
|
|
# ifdef LE_64
|
|
|
|
# define conv64le(num) LE_64(num)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef bswap16
|
2019-12-30 17:18:24 -05:00
|
|
|
# define bswap16(n) (uint16_t)( \
|
|
|
|
(((n) & 0x00FFU) << 8) \
|
|
|
|
| (((n) & 0xFF00U) >> 8) \
|
|
|
|
)
|
2009-10-04 15:57:12 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef bswap32
|
2019-12-30 17:18:24 -05:00
|
|
|
# define bswap32(n) (uint32_t)( \
|
|
|
|
(((n) & UINT32_C(0x000000FF)) << 24) \
|
|
|
|
| (((n) & UINT32_C(0x0000FF00)) << 8) \
|
|
|
|
| (((n) & UINT32_C(0x00FF0000)) >> 8) \
|
|
|
|
| (((n) & UINT32_C(0xFF000000)) >> 24) \
|
|
|
|
)
|
2009-10-04 15:57:12 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef bswap64
|
2019-12-30 17:18:24 -05:00
|
|
|
# define bswap64(n) (uint64_t)( \
|
|
|
|
(((n) & UINT64_C(0x00000000000000FF)) << 56) \
|
|
|
|
| (((n) & UINT64_C(0x000000000000FF00)) << 40) \
|
|
|
|
| (((n) & UINT64_C(0x0000000000FF0000)) << 24) \
|
|
|
|
| (((n) & UINT64_C(0x00000000FF000000)) << 8) \
|
|
|
|
| (((n) & UINT64_C(0x000000FF00000000)) >> 8) \
|
|
|
|
| (((n) & UINT64_C(0x0000FF0000000000)) >> 24) \
|
|
|
|
| (((n) & UINT64_C(0x00FF000000000000)) >> 40) \
|
|
|
|
| (((n) & UINT64_C(0xFF00000000000000)) >> 56) \
|
|
|
|
)
|
2009-10-04 15:57:12 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Define conversion macros using the basic byte swapping macros.
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
# ifndef conv16be
|
|
|
|
# define conv16be(num) ((uint16_t)(num))
|
|
|
|
# endif
|
|
|
|
# ifndef conv32be
|
|
|
|
# define conv32be(num) ((uint32_t)(num))
|
|
|
|
# endif
|
|
|
|
# ifndef conv64be
|
|
|
|
# define conv64be(num) ((uint64_t)(num))
|
|
|
|
# endif
|
|
|
|
# ifndef conv16le
|
|
|
|
# define conv16le(num) bswap16(num)
|
|
|
|
# endif
|
|
|
|
# ifndef conv32le
|
|
|
|
# define conv32le(num) bswap32(num)
|
|
|
|
# endif
|
|
|
|
# ifndef conv64le
|
|
|
|
# define conv64le(num) bswap64(num)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# ifndef conv16be
|
|
|
|
# define conv16be(num) bswap16(num)
|
|
|
|
# endif
|
|
|
|
# ifndef conv32be
|
|
|
|
# define conv32be(num) bswap32(num)
|
|
|
|
# endif
|
|
|
|
# ifndef conv64be
|
|
|
|
# define conv64be(num) bswap64(num)
|
|
|
|
# endif
|
|
|
|
# ifndef conv16le
|
|
|
|
# define conv16le(num) ((uint16_t)(num))
|
|
|
|
# endif
|
|
|
|
# ifndef conv32le
|
|
|
|
# define conv32le(num) ((uint32_t)(num))
|
|
|
|
# endif
|
|
|
|
# ifndef conv64le
|
|
|
|
# define conv64le(num) ((uint64_t)(num))
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
// Unaligned reads and writes //
|
|
|
|
////////////////////////////////
|
|
|
|
|
2019-06-01 11:41:16 -04:00
|
|
|
// The traditional way of casting e.g. *(const uint16_t *)uint8_pointer
|
2019-12-30 17:18:24 -05:00
|
|
|
// is bad even if the uint8_pointer is properly aligned because this kind
|
|
|
|
// of casts break strict aliasing rules and result in undefined behavior.
|
|
|
|
// With unaligned pointers it's even worse: compilers may emit vector
|
|
|
|
// instructions that require aligned pointers even if non-vector
|
|
|
|
// instructions work with unaligned pointers.
|
2019-06-01 11:41:16 -04:00
|
|
|
//
|
|
|
|
// Using memcpy() is the standard compliant way to do unaligned access.
|
|
|
|
// Many modern compilers inline it so there is no function call overhead.
|
2019-12-30 17:18:24 -05:00
|
|
|
// For those compilers that don't handle the memcpy() method well, the
|
|
|
|
// old casting method (that violates strict aliasing) can be requested at
|
|
|
|
// build time. A third method, casting to a packed struct, would also be
|
|
|
|
// an option but isn't provided to keep things simpler (it's already a mess).
|
|
|
|
// Hopefully this is flexible enough in practice.
|
2019-06-01 11:41:16 -04:00
|
|
|
|
|
|
|
static inline uint16_t
|
2019-12-30 17:41:28 -05:00
|
|
|
read16ne(const uint8_t *buf)
|
2019-06-01 11:41:16 -04:00
|
|
|
{
|
2019-12-30 17:18:24 -05:00
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
|
|
|
&& defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
|
|
|
|
return *(const uint16_t *)buf;
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
|
|
|
uint16_t num;
|
|
|
|
memcpy(&num, buf, sizeof(num));
|
|
|
|
return num;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
2019-12-30 17:41:28 -05:00
|
|
|
read32ne(const uint8_t *buf)
|
2019-06-01 11:41:16 -04:00
|
|
|
{
|
2019-12-30 17:18:24 -05:00
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
|
|
|
&& defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
|
|
|
|
return *(const uint32_t *)buf;
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
|
|
|
uint32_t num;
|
|
|
|
memcpy(&num, buf, sizeof(num));
|
|
|
|
return num;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint64_t
|
2019-12-30 17:41:28 -05:00
|
|
|
read64ne(const uint8_t *buf)
|
2019-06-01 11:41:16 -04:00
|
|
|
{
|
2019-12-30 17:18:24 -05:00
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
|
|
|
&& defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
|
|
|
|
return *(const uint64_t *)buf;
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
|
|
|
uint64_t num;
|
|
|
|
memcpy(&num, buf, sizeof(num));
|
|
|
|
return num;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2019-12-30 17:41:28 -05:00
|
|
|
write16ne(uint8_t *buf, uint16_t num)
|
2019-06-01 11:41:16 -04:00
|
|
|
{
|
2019-12-30 17:18:24 -05:00
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
|
|
|
&& defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
|
|
|
|
*(uint16_t *)buf = num;
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
|
|
|
memcpy(buf, &num, sizeof(num));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2019-12-30 17:41:28 -05:00
|
|
|
write32ne(uint8_t *buf, uint32_t num)
|
2019-06-01 11:41:16 -04:00
|
|
|
{
|
2019-12-30 17:18:24 -05:00
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
|
|
|
&& defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
|
|
|
|
*(uint32_t *)buf = num;
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
|
|
|
memcpy(buf, &num, sizeof(num));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2019-12-30 17:41:28 -05:00
|
|
|
write64ne(uint8_t *buf, uint64_t num)
|
2019-06-01 11:41:16 -04:00
|
|
|
{
|
2019-12-30 17:18:24 -05:00
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
|
|
|
&& defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING)
|
|
|
|
*(uint64_t *)buf = num;
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
|
|
|
memcpy(buf, &num, sizeof(num));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-04 15:57:12 -04:00
|
|
|
static inline uint16_t
|
2019-12-30 17:41:28 -05:00
|
|
|
read16be(const uint8_t *buf)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2019-06-01 11:41:16 -04:00
|
|
|
#if defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
|
2019-12-30 17:41:28 -05:00
|
|
|
uint16_t num = read16ne(buf);
|
2019-06-20 12:40:30 -04:00
|
|
|
return conv16be(num);
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
2009-11-22 04:52:30 -05:00
|
|
|
uint16_t num = ((uint16_t)buf[0] << 8) | (uint16_t)buf[1];
|
2009-10-04 15:57:12 -04:00
|
|
|
return num;
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
2009-10-04 15:57:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint16_t
|
2019-12-30 17:41:28 -05:00
|
|
|
read16le(const uint8_t *buf)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2019-06-01 11:41:16 -04:00
|
|
|
#if !defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
|
2019-12-30 17:41:28 -05:00
|
|
|
uint16_t num = read16ne(buf);
|
2019-06-20 12:40:30 -04:00
|
|
|
return conv16le(num);
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
2009-11-22 04:52:30 -05:00
|
|
|
uint16_t num = ((uint16_t)buf[0]) | ((uint16_t)buf[1] << 8);
|
2009-10-04 15:57:12 -04:00
|
|
|
return num;
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
2009-10-04 15:57:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
2019-12-30 17:41:28 -05:00
|
|
|
read32be(const uint8_t *buf)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2019-06-01 11:41:16 -04:00
|
|
|
#if defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
|
2019-12-30 17:41:28 -05:00
|
|
|
uint32_t num = read32ne(buf);
|
2019-06-20 12:40:30 -04:00
|
|
|
return conv32be(num);
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
2009-10-04 15:57:12 -04:00
|
|
|
uint32_t num = (uint32_t)buf[0] << 24;
|
|
|
|
num |= (uint32_t)buf[1] << 16;
|
|
|
|
num |= (uint32_t)buf[2] << 8;
|
|
|
|
num |= (uint32_t)buf[3];
|
|
|
|
return num;
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
2009-10-04 15:57:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
2019-12-30 17:41:28 -05:00
|
|
|
read32le(const uint8_t *buf)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2019-06-01 11:41:16 -04:00
|
|
|
#if !defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
|
2019-12-30 17:41:28 -05:00
|
|
|
uint32_t num = read32ne(buf);
|
2019-06-20 12:40:30 -04:00
|
|
|
return conv32le(num);
|
2019-06-01 11:41:16 -04:00
|
|
|
#else
|
2009-10-04 15:57:12 -04:00
|
|
|
uint32_t num = (uint32_t)buf[0];
|
|
|
|
num |= (uint32_t)buf[1] << 8;
|
|
|
|
num |= (uint32_t)buf[2] << 16;
|
|
|
|
num |= (uint32_t)buf[3] << 24;
|
|
|
|
return num;
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
2009-10-04 15:57:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
// NOTE: Possible byte swapping must be done in a macro to allow the compiler
|
|
|
|
// to optimize byte swapping of constants when using glibc's or *BSD's
|
|
|
|
// byte swapping macros. The actual write is done in an inline function
|
|
|
|
// to make type checking of the buf pointer possible.
|
2019-06-01 11:41:16 -04:00
|
|
|
#if defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
|
2019-12-30 17:41:28 -05:00
|
|
|
# define write16be(buf, num) write16ne(buf, conv16be(num))
|
|
|
|
# define write32be(buf, num) write32ne(buf, conv32be(num))
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(WORDS_BIGENDIAN) || defined(TUKLIB_FAST_UNALIGNED_ACCESS)
|
2019-12-30 17:41:28 -05:00
|
|
|
# define write16le(buf, num) write16ne(buf, conv16le(num))
|
|
|
|
# define write32le(buf, num) write32ne(buf, conv32le(num))
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-12-30 17:41:28 -05:00
|
|
|
#ifndef write16be
|
2009-10-04 15:57:12 -04:00
|
|
|
static inline void
|
2019-12-30 17:41:28 -05:00
|
|
|
write16be(uint8_t *buf, uint16_t num)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2015-02-03 14:45:53 -05:00
|
|
|
buf[0] = (uint8_t)(num >> 8);
|
|
|
|
buf[1] = (uint8_t)num;
|
2009-10-04 15:57:12 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
2009-10-04 15:57:12 -04:00
|
|
|
|
|
|
|
|
2019-12-30 17:41:28 -05:00
|
|
|
#ifndef write16le
|
2009-10-04 15:57:12 -04:00
|
|
|
static inline void
|
2019-12-30 17:41:28 -05:00
|
|
|
write16le(uint8_t *buf, uint16_t num)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2015-02-03 14:45:53 -05:00
|
|
|
buf[0] = (uint8_t)num;
|
|
|
|
buf[1] = (uint8_t)(num >> 8);
|
2009-10-04 15:57:12 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
2009-10-04 15:57:12 -04:00
|
|
|
|
|
|
|
|
2019-12-30 17:41:28 -05:00
|
|
|
#ifndef write32be
|
2009-10-04 15:57:12 -04:00
|
|
|
static inline void
|
2019-12-30 17:41:28 -05:00
|
|
|
write32be(uint8_t *buf, uint32_t num)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2015-02-03 14:45:53 -05:00
|
|
|
buf[0] = (uint8_t)(num >> 24);
|
|
|
|
buf[1] = (uint8_t)(num >> 16);
|
|
|
|
buf[2] = (uint8_t)(num >> 8);
|
|
|
|
buf[3] = (uint8_t)num;
|
2009-10-04 15:57:12 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-06-01 11:41:16 -04:00
|
|
|
#endif
|
2009-10-04 15:57:12 -04:00
|
|
|
|
|
|
|
|
2019-12-30 17:41:28 -05:00
|
|
|
#ifndef write32le
|
2009-10-04 15:57:12 -04:00
|
|
|
static inline void
|
2019-12-30 17:41:28 -05:00
|
|
|
write32le(uint8_t *buf, uint32_t num)
|
2009-10-04 15:57:12 -04:00
|
|
|
{
|
2015-02-03 14:45:53 -05:00
|
|
|
buf[0] = (uint8_t)num;
|
|
|
|
buf[1] = (uint8_t)(num >> 8);
|
|
|
|
buf[2] = (uint8_t)(num >> 16);
|
|
|
|
buf[3] = (uint8_t)(num >> 24);
|
2009-10-04 15:57:12 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2009-11-22 04:52:30 -05:00
|
|
|
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
//////////////////////////////
|
|
|
|
// Aligned reads and writes //
|
|
|
|
//////////////////////////////
|
|
|
|
|
|
|
|
// Separate functions for aligned reads and writes are provided since on
|
|
|
|
// strict-align archs aligned access is much faster than unaligned access.
|
|
|
|
//
|
|
|
|
// Just like in the unaligned case, memcpy() is needed to avoid
|
|
|
|
// strict aliasing violations. However, on archs that don't support
|
|
|
|
// unaligned access the compiler cannot know that the pointers given
|
|
|
|
// to memcpy() are aligned which results in slow code. As of C11 there is
|
|
|
|
// no standard way to tell the compiler that we know that the address is
|
|
|
|
// aligned but some compilers have language extensions to do that. With
|
|
|
|
// such language extensions the memcpy() method gives excellent results.
|
|
|
|
//
|
|
|
|
// What to do on a strict-align system when no known language extentensions
|
|
|
|
// are available? Falling back to byte-by-byte access would be safe but ruin
|
|
|
|
// optimizations that have been made specifically with aligned access in mind.
|
|
|
|
// As a compromise, aligned reads will fall back to non-compliant type punning
|
|
|
|
// but aligned writes will be byte-by-byte, that is, fast reads are preferred
|
|
|
|
// over fast writes. This obviously isn't great but hopefully it's a working
|
|
|
|
// compromise for now.
|
|
|
|
//
|
|
|
|
// __builtin_assume_aligned is support by GCC >= 4.7 and clang >= 3.6.
|
|
|
|
#ifdef HAVE___BUILTIN_ASSUME_ALIGNED
|
|
|
|
# define tuklib_memcpy_aligned(dest, src, size) \
|
|
|
|
memcpy(dest, __builtin_assume_aligned(src, size), size)
|
|
|
|
#else
|
|
|
|
# define tuklib_memcpy_aligned(dest, src, size) \
|
|
|
|
memcpy(dest, src, size)
|
|
|
|
# ifndef TUKLIB_FAST_UNALIGNED_ACCESS
|
|
|
|
# define TUKLIB_USE_UNSAFE_ALIGNED_READS 1
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint16_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read16ne(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
|
|
|
#if defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING) \
|
|
|
|
|| defined(TUKLIB_USE_UNSAFE_ALIGNED_READS)
|
|
|
|
return *(const uint16_t *)buf;
|
|
|
|
#else
|
|
|
|
uint16_t num;
|
|
|
|
tuklib_memcpy_aligned(&num, buf, sizeof(num));
|
|
|
|
return num;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read32ne(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
|
|
|
#if defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING) \
|
|
|
|
|| defined(TUKLIB_USE_UNSAFE_ALIGNED_READS)
|
|
|
|
return *(const uint32_t *)buf;
|
|
|
|
#else
|
|
|
|
uint32_t num;
|
|
|
|
tuklib_memcpy_aligned(&num, buf, sizeof(num));
|
|
|
|
return num;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint64_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read64ne(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
|
|
|
#if defined(TUKLIB_USE_UNSAFE_TYPE_PUNNING) \
|
|
|
|
|| defined(TUKLIB_USE_UNSAFE_ALIGNED_READS)
|
|
|
|
return *(const uint64_t *)buf;
|
|
|
|
#else
|
|
|
|
uint64_t num;
|
|
|
|
tuklib_memcpy_aligned(&num, buf, sizeof(num));
|
|
|
|
return num;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_write16ne(uint8_t *buf, uint16_t num)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
|
|
|
#ifdef TUKLIB_USE_UNSAFE_TYPE_PUNNING
|
|
|
|
*(uint16_t *)buf = num;
|
|
|
|
#else
|
|
|
|
tuklib_memcpy_aligned(buf, &num, sizeof(num));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_write32ne(uint8_t *buf, uint32_t num)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
|
|
|
#ifdef TUKLIB_USE_UNSAFE_TYPE_PUNNING
|
|
|
|
*(uint32_t *)buf = num;
|
|
|
|
#else
|
|
|
|
tuklib_memcpy_aligned(buf, &num, sizeof(num));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_write64ne(uint8_t *buf, uint64_t num)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
|
|
|
#ifdef TUKLIB_USE_UNSAFE_TYPE_PUNNING
|
|
|
|
*(uint64_t *)buf = num;
|
|
|
|
#else
|
|
|
|
tuklib_memcpy_aligned(buf, &num, sizeof(num));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint16_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read16be(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
2019-12-30 17:29:48 -05:00
|
|
|
uint16_t num = aligned_read16ne(buf);
|
2019-12-30 17:18:24 -05:00
|
|
|
return conv16be(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint16_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read16le(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
2019-12-30 17:29:48 -05:00
|
|
|
uint16_t num = aligned_read16ne(buf);
|
2019-12-30 17:18:24 -05:00
|
|
|
return conv16le(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read32be(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
2019-12-30 17:29:48 -05:00
|
|
|
uint32_t num = aligned_read32ne(buf);
|
2019-12-30 17:18:24 -05:00
|
|
|
return conv32be(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read32le(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
2019-12-30 17:29:48 -05:00
|
|
|
uint32_t num = aligned_read32ne(buf);
|
2019-12-30 17:18:24 -05:00
|
|
|
return conv32le(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint64_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read64be(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
2019-12-30 17:29:48 -05:00
|
|
|
uint64_t num = aligned_read64ne(buf);
|
2019-12-30 17:18:24 -05:00
|
|
|
return conv64be(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint64_t
|
2019-12-30 17:29:48 -05:00
|
|
|
aligned_read64le(const uint8_t *buf)
|
2019-12-30 17:18:24 -05:00
|
|
|
{
|
2019-12-30 17:29:48 -05:00
|
|
|
uint64_t num = aligned_read64ne(buf);
|
2019-12-30 17:18:24 -05:00
|
|
|
return conv64le(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// These need to be macros like in the unaligned case.
|
2019-12-30 17:29:48 -05:00
|
|
|
#define aligned_write16be(buf, num) aligned_write16ne((buf), conv16be(num))
|
|
|
|
#define aligned_write16le(buf, num) aligned_write16ne((buf), conv16le(num))
|
|
|
|
#define aligned_write32be(buf, num) aligned_write32ne((buf), conv32be(num))
|
|
|
|
#define aligned_write32le(buf, num) aligned_write32ne((buf), conv32le(num))
|
|
|
|
#define aligned_write64be(buf, num) aligned_write64ne((buf), conv64be(num))
|
|
|
|
#define aligned_write64le(buf, num) aligned_write64ne((buf), conv64le(num))
|
2019-12-30 17:18:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
// Bit operations //
|
|
|
|
////////////////////
|
|
|
|
|
2009-11-22 04:52:30 -05:00
|
|
|
static inline uint32_t
|
|
|
|
bsr32(uint32_t n)
|
|
|
|
{
|
|
|
|
// Check for ICC first, since it tends to define __GNUC__ too.
|
|
|
|
#if defined(__INTEL_COMPILER)
|
|
|
|
return _bit_scan_reverse(n);
|
|
|
|
|
|
|
|
#elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
|
|
|
|
// GCC >= 3.4 has __builtin_clz(), which gives good results on
|
|
|
|
// multiple architectures. On x86, __builtin_clz() ^ 31U becomes
|
|
|
|
// either plain BSR (so the XOR gets optimized away) or LZCNT and
|
|
|
|
// XOR (if -march indicates that SSE4a instructions are supported).
|
2019-06-23 12:33:55 -04:00
|
|
|
return (uint32_t)__builtin_clz(n) ^ 31U;
|
2009-11-22 04:52:30 -05:00
|
|
|
|
|
|
|
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
|
|
|
uint32_t i;
|
|
|
|
__asm__("bsrl %1, %0" : "=r" (i) : "rm" (n));
|
|
|
|
return i;
|
|
|
|
|
2019-06-01 14:30:03 -04:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
unsigned long i;
|
|
|
|
_BitScanReverse(&i, n);
|
2009-11-22 04:52:30 -05:00
|
|
|
return i;
|
|
|
|
|
|
|
|
#else
|
|
|
|
uint32_t i = 31;
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xFFFF0000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 16;
|
|
|
|
i = 15;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xFF000000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 8;
|
|
|
|
i -= 8;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xF0000000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 4;
|
|
|
|
i -= 4;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xC0000000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 2;
|
|
|
|
i -= 2;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0x80000000) == 0)
|
2009-11-22 04:52:30 -05:00
|
|
|
--i;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
|
|
|
clz32(uint32_t n)
|
|
|
|
{
|
|
|
|
#if defined(__INTEL_COMPILER)
|
|
|
|
return _bit_scan_reverse(n) ^ 31U;
|
|
|
|
|
|
|
|
#elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX == UINT32_MAX
|
2019-06-23 12:33:55 -04:00
|
|
|
return (uint32_t)__builtin_clz(n);
|
2009-11-22 04:52:30 -05:00
|
|
|
|
|
|
|
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
|
|
|
uint32_t i;
|
|
|
|
__asm__("bsrl %1, %0\n\t"
|
|
|
|
"xorl $31, %0"
|
|
|
|
: "=r" (i) : "rm" (n));
|
|
|
|
return i;
|
|
|
|
|
2019-06-01 14:30:03 -04:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
unsigned long i;
|
|
|
|
_BitScanReverse(&i, n);
|
2009-11-22 04:52:30 -05:00
|
|
|
return i ^ 31U;
|
|
|
|
|
|
|
|
#else
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xFFFF0000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 16;
|
|
|
|
i = 16;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xFF000000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 8;
|
|
|
|
i += 8;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xF0000000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 4;
|
|
|
|
i += 4;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0xC0000000) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n <<= 2;
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0x80000000) == 0)
|
2009-11-22 04:52:30 -05:00
|
|
|
++i;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t
|
|
|
|
ctz32(uint32_t n)
|
|
|
|
{
|
|
|
|
#if defined(__INTEL_COMPILER)
|
|
|
|
return _bit_scan_forward(n);
|
|
|
|
|
|
|
|
#elif TUKLIB_GNUC_REQ(3, 4) && UINT_MAX >= UINT32_MAX
|
2019-06-23 12:33:55 -04:00
|
|
|
return (uint32_t)__builtin_ctz(n);
|
2009-11-22 04:52:30 -05:00
|
|
|
|
|
|
|
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
|
|
|
uint32_t i;
|
|
|
|
__asm__("bsfl %1, %0" : "=r" (i) : "rm" (n));
|
|
|
|
return i;
|
|
|
|
|
2019-06-01 14:30:03 -04:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
unsigned long i;
|
|
|
|
_BitScanForward(&i, n);
|
2009-11-22 04:52:30 -05:00
|
|
|
return i;
|
|
|
|
|
|
|
|
#else
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0x0000FFFF) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n >>= 16;
|
|
|
|
i = 16;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0x000000FF) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n >>= 8;
|
|
|
|
i += 8;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0x0000000F) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n >>= 4;
|
|
|
|
i += 4;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0x00000003) == 0) {
|
2009-11-22 04:52:30 -05:00
|
|
|
n >>= 2;
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:18:24 -05:00
|
|
|
if ((n & 0x00000001) == 0)
|
2009-11-22 04:52:30 -05:00
|
|
|
++i;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#define bsf32 ctz32
|
|
|
|
|
2009-10-04 15:57:12 -04:00
|
|
|
#endif
|