Speclib  0.1.2
The library for writing better CUDA libraries
Bit Twiddling

Utilities for bit-twiddling. More...

Functions

template<typename T >
constexpr bool sp::bitIsSet (T x, int bit)
 Return true iff the bitth bit of x is 1. More...
 
template<typename T >
constexpr T sp::setBit (T x, int bit)
 Set a bit in x to 1, returning the result. More...
 
template<typename T >
constexpr T sp::clearBit (T x, int bit)
 Set a bit in x to 0, returning the result. More...
 
template<typename T >
constexpr T sp::assignBit (T x, int bit, bool value)
 Set bit bit of x to value ? 1 : 0, and return the result. More...
 
template<typename T >
constexpr T sp::keepHighBits (T x, int KeepBits)
 Zero everything except the KeepBits-many high bits in x. More...
 
template<typename T >
constexpr T sp::keepLowBits (T x, int KeepBits)
 Zero everything except the KeepBits-many low bits in x. More...
 
template<typename T = uint64_t, typename ValueT >
constexpr T sp::bitfieldExtract (ValueT x, int start, int len=sizeof(T) *8)
 Extract a bitfield starting at bit start and extending for len bits. More...
 
template<typename ValueT , typename PayloadT >
constexpr ValueT sp::bitfieldInsert (ValueT x, PayloadT value, int start, int len=bits< PayloadT >)
 Copy the least significant len bits of value into x at position start. More...
 
template<typename T >
constexpr T sp::snipBit (T x, int bit)
 "cut" a bit out of x, shifting everything above it to the right. More...
 
template<typename I >
constexpr int sp::bitsNeededForValue (I n)
 Calculate the number of bits needed to represent the given integer. More...
 
template<typename I >
constexpr I sp::getLSBOnes (int count)
 Get a value with the count least significant bits set to one. More...
 
template<typename T >
constexpr T sp::popcount (const T &arg)
 Population-count the object representation of any type. More...
 

Detailed Description

Utilities for bit-twiddling.

Function Documentation

◆ assignBit()

template<typename T >
constexpr T sp::assignBit ( x,
int  bit,
bool  value 
)
constexpr

Set bit bit of x to value ? 1 : 0, and return the result.

◆ bitfieldExtract()

template<typename T = uint64_t, typename ValueT >
constexpr T sp::bitfieldExtract ( ValueT  x,
int  start,
int  len = sizeof(T) * 8 
)
constexpr

Extract a bitfield starting at bit start and extending for len bits.

The result is shifted down to the least-significant position and returned.

Note that although this function looks hideously complicated, it does reliably compile to a BFE or an AND instruction, meaning you can safely use it to do bounds-checked bit-twiddling and so on.

Template Parameters
TThe type of the bifield member being extracted.
ValueTThe type of integer containing the bitfield (deduced)

◆ bitfieldInsert()

template<typename ValueT , typename PayloadT >
constexpr ValueT sp::bitfieldInsert ( ValueT  x,
PayloadT  value,
int  start,
int  len = bits<PayloadT> 
)
constexpr

Copy the least significant len bits of value into x at position start.

◆ bitIsSet()

template<typename T >
constexpr bool sp::bitIsSet ( x,
int  bit 
)
constexpr

Return true iff the bitth bit of x is 1.

◆ bitsNeededForValue()

template<typename I >
constexpr int sp::bitsNeededForValue ( n)
constexpr

Calculate the number of bits needed to represent the given integer.

For non-negative values, this calculates log2(n + 1) rounded up to the nearest integer. For negative values, this gives the number of bits in the integer's type.

Parameters
nAn integer parameter for the calculation.
Returns
The number of bits required to represent n.

◆ clearBit()

template<typename T >
constexpr T sp::clearBit ( x,
int  bit 
)
constexpr

Set a bit in x to 0, returning the result.

The operation occurs in the type of x: T.

Template Parameters
TThe type of x, and the result.
Parameters
xThe input value.
bitThe position of the bit to clear.
Returns
A copy of x with the bit'th bit set to 1.

◆ getLSBOnes()

template<typename I >
constexpr I sp::getLSBOnes ( int  count)
constexpr

Get a value with the count least significant bits set to one.

This computation is performed without wrapping (for constexpr computation), but is not otherwise the most efficient.

◆ keepHighBits()

template<typename T >
constexpr T sp::keepHighBits ( x,
int  KeepBits 
)
constexpr

Zero everything except the KeepBits-many high bits in x.

◆ keepLowBits()

template<typename T >
constexpr T sp::keepLowBits ( x,
int  KeepBits 
)
constexpr

Zero everything except the KeepBits-many low bits in x.

◆ popcount()

template<typename T >
constexpr T sp::popcount ( const T &  arg)
constexpr

Population-count the object representation of any type.

If the input type contains any padding bytes, the function will become nondeterminstic (since any number of the padding bits might be 1). Nevertheless, the behaviour is in that case still defined and could conceivably be useful.

◆ setBit()

template<typename T >
constexpr T sp::setBit ( x,
int  bit 
)
constexpr

Set a bit in x to 1, returning the result.

The operation occurs in the type of x: T.

Template Parameters
TThe type of x, and the result.
Parameters
xThe input value.
bitThe position of the bit to set.
Returns
A copy of x with the bit'th bit set to 1.

◆ snipBit()

template<typename T >
constexpr T sp::snipBit ( x,
int  bit 
)
constexpr

"cut" a bit out of x, shifting everything above it to the right.