rlib
Convenience library for useful things
Loading...
Searching...
No Matches
Bit operations

Width-specific bit-twiddling macros: count-leading / trailing zeros, population count, parity, and masked shift / rotate. More...

Files

file  rbitops.h
 Width-specific count-zeros / popcount / parity / shift / rotate macros.
 

Macros

#define RUINT64_CLZ(x)   RULONGLONG_CLZ (x)
 
#define RUINT64_CTZ(x)   RULONGLONG_CTZ (x)
 
#define RUINT64_POPCOUNT(x)   RULONGLONG_POPCOUNT (x)
 
#define RUINT64_PARITY(x)   RULONGLONG_PARITY (x)
 
#define RUINT64_SHL(x, n)   (((x) & RUINT64_MAX) << ((n) & 63))
 
#define RUINT64_SHR(x, n)   (((x) & RUINT64_MAX) >> ((n) & 63))
 
#define RUINT64_ROTL(x, n)   (RUINT64_SHL (x, n) | RUINT64_SHR (x, 64-(n)))
 
#define RUINT64_ROTR(x, n)   (RUINT64_SHR (x, n) | RUINT64_SHL (x, 64-(n)))
 
#define RUINT32_CLZ(x)   (RUINT64_CLZ (x & RUINT32_MAX) - 32)
 
#define RUINT32_CTZ(x)   RUINT64_CTZ (x & RUINT32_MAX)
 
#define RUINT32_POPCOUNT(x)   RUINT64_POPCOUNT (x & RUINT32_MAX)
 
#define RUINT32_PARITY(x)   RUINT64_PARITY (x & RUINT32_MAX)
 
#define RUINT32_SHL(x, n)   (((x) & RUINT32_MAX) << ((n) & 31))
 Shift a 32-bit x left by n (operand and count masked to width).
 
#define RUINT32_SHR(x, n)   (((x) & RUINT32_MAX) >> ((n) & 31))
 Shift a 32-bit x right by n (operand and count masked to width).
 
#define RUINT32_ROTL(x, n)   (RUINT32_SHL (x, n) | RUINT32_SHR (x, 32-(n)))
 Rotate a 32-bit x left by n bits.
 
#define RUINT32_ROTR(x, n)   (RUINT32_SHR (x, n) | RUINT32_SHL (x, 32-(n)))
 Rotate a 32-bit x right by n bits.
 
#define RUINT16_CLZ(x)   (RUINT32_CLZ (x & RUINT16_MAX) - 16)
 
#define RUINT16_CTZ(x)   MIN (RUINT32_CTZ (x & RUINT16_MAX), 16)
 
#define RUINT16_POPCOUNT(x)   RUINT32_POPCOUNT (x & RUINT16_MAX)
 
#define RUINT16_PARITY(x)   RUINT32_PARITY (x & RUINT16_MAX)
 
#define RUINT16_SHL(x, n)   (((x) & RUINT16_MAX) << ((n) & 15))
 
#define RUINT16_SHR(x, n)   (((x) & RUINT16_MAX) >> ((n) & 15))
 
#define RUINT16_ROTL(x, n)   (RUINT16_SHL (x, n) | RUINT16_SHR (x, 16-(n)))
 
#define RUINT16_ROTR(x, n)   (RUINT16_SHR (x, n) | RUINT16_SHL (x, 16-(n)))
 
#define RUINT8_CLZ(x)   (RUINT32_CLZ (x & RUINT8_MAX) - 24)
 
#define RUINT8_CTZ(x)   MIN (RUINT32_CTZ (x & RUINT8_MAX), 8)
 
#define RUINT8_POPCOUNT(x)   RUINT32_POPCOUNT (x & RUINT8_MAX)
 
#define RUINT8_PARITY(x)   RUINT32_PARITY (x & RUINT8_MAX)
 
#define RUINT8_SHL(x, n)   (((x) & RUINT8_MAX) << ((n) & 7))
 
#define RUINT8_SHR(x, n)   (((x) & RUINT8_MAX) >> ((n) & 7))
 
#define RUINT8_ROTL(x, n)   (RUINT8_SHL (x, n) | RUINT8_SHR (x, 8-(n)))
 
#define RUINT8_ROTR(x, n)   (RUINT8_SHR (x, n) | RUINT8_SHL (x, 8-(n)))
 

Detailed Description

Width-specific bit-twiddling macros: count-leading / trailing zeros, population count, parity, and masked shift / rotate.

Every macro follows the R<WIDTH>_<OP> naming convention, where WIDTH is one of UINT8 / UINT16 / UINT32 / UINT64 / SIZE (plus the native-width UINT / ULONG / ULONGLONG helpers the fixed-width ones are built on), and OP is one of:

CLZ / CTZ / POPCOUNT / PARITY compile down to the compiler's intrinsics (GCC/Clang __builtin_*, MSVC _BitScan* / __popcnt); the rest are plain macros. The RUINT32_CLZ family below is documented as the canonical example — the same eight operations exist for every listed width.