ROSE 0.11.145.192
Classes | Functions
IntegerOps Namespace Reference

Description

Bit-wise operations on integers.

Many of these are function templates for the best optimization, but we also provide non-template versions for those cases when the arguments are not known at compile time. The non-template versions typically have "2" appended to their names to indicate that they should be the second choice–used only when the template version cannot be used.

Classes

struct  GenMask
 Bit mask constant with bits 0 through n-1 set. More...
 
struct  SHL1
 Bitmask constant with bit n set. More...
 

Functions

template<typename T >
shl1 (size_t n)
 Bitmask with bit n set.
 
template<typename T >
genMask (size_t n)
 Bitmask with bits 0 through N-1 set.
 
template<typename T >
genMask (size_t lobit, size_t hibit)
 Generate a bitmask.
 
template<typename T >
bool isPowerOfTwo (T value)
 Returns true if the value is a power of two.
 
template<typename T >
log2max (T value)
 Returns the base-2 logorithm of value.
 
template<typename T >
log2 (T a)
 
template<typename T >
bool bitmask_subset (T m1, T m2)
 Determines if one bitmask is a subset of another.
 
template<typename T >
size_t countSet (T val)
 Counts how many bits are set (one).
 
template<typename T >
size_t countClear (T val)
 Counts how many bits are clear (zero).
 
template<typename T >
boost::optional< size_t > msb_set (T val)
 Optionally returns the zero-origin position of the most significant set bit.
 
template<size_t NBits, typename T >
bool signBit (T value)
 Returns true if the sign bit is set, false if clear.
 
template<typename T >
bool signBit2 (T value, size_t width=8 *sizeof(T))
 Returns true if the sign bit is set, false if clear.
 
template<size_t FromBits, size_t ToBits, typename T >
signExtend (T value)
 Sign extend value.
 
template<typename T >
signExtend2 (T value, size_t from_width, size_t to_width)
 Sign extend value.
 
template<size_t NBits, typename T >
shiftLeft (T value, size_t count)
 Shifts bits of value left by count bits.
 
template<typename T >
shiftLeft2 (T value, size_t count, size_t width=8 *sizeof(T))
 Shifts bits of value left by count bits.
 
template<size_t NBits, typename T >
shiftRightLogical (T value, size_t count)
 Shifts bits of value right by count bits without sign extension.
 
template<typename T >
shiftRightLogical2 (T value, size_t count, size_t width=8 *sizeof(T))
 Shifts bits of value right by count bits without sign extension.
 
template<size_t NBits, typename T >
shiftRightArithmetic (T value, size_t count)
 Shifts bits of value right by count bits with sign extension.
 
template<typename T >
shiftRightArithmetic2 (T value, size_t count, size_t width=8 *sizeof(T))
 Shifts bits of value right by count bits with sign extension.
 
template<size_t NBits, typename T >
rotateLeft (T value, size_t count)
 Rotate the bits of the value left by count bits.
 
template<typename T >
rotateLeft2 (T value, size_t count, size_t width=8 *sizeof(T))
 Rotate the bits of the value left by count bits.
 
template<size_t NBits, typename T >
rotateRight (T value, size_t count)
 Rotate bits of the value right by count bits.
 
template<typename T >
rotateRight2 (T value, size_t count, size_t width=8 *sizeof(T))
 Rotate bits of the value right by count bits.
 
template<size_t lobit, size_t hibit, typename T >
shift_to (T value)
 Create a shifted value.
 
template<typename T >
shift_to2 (size_t lobit, size_t hibit, T value)
 Create a shifted value.
 
template<size_t lobit, size_t hibit, typename T >
extract (T bits)
 Extract bits from a value.
 
template<typename T >
extract2 (size_t lobit, size_t hibit, T bits)
 Extract bits from a value.
 

Function Documentation

◆ shl1()

template<typename T >
T IntegerOps::shl1 ( size_t  n)
inline

Bitmask with bit n set.

Handles the case where n is greater than the width of type T.

Definition at line 43 of file integerOps.h.

◆ genMask() [1/2]

template<typename T >
T IntegerOps::genMask ( size_t  n)
inline

Bitmask with bits 0 through N-1 set.

Definition at line 55 of file integerOps.h.

◆ genMask() [2/2]

template<typename T >
T IntegerOps::genMask ( size_t  lobit,
size_t  hibit 
)
inline

Generate a bitmask.

The return value has bits lobit (inclusive) through hibit (inclusive) set, and all other bits are clear.

Definition at line 62 of file integerOps.h.

◆ signBit()

template<size_t NBits, typename T >
bool IntegerOps::signBit ( value)
inline

Returns true if the sign bit is set, false if clear.

Definition at line 72 of file integerOps.h.

◆ signBit2()

template<typename T >
bool IntegerOps::signBit2 ( value,
size_t  width = 8*sizeof(T) 
)
inline

Returns true if the sign bit is set, false if clear.

Definition at line 77 of file integerOps.h.

Referenced by shiftRightArithmetic2(), and signExtend2().

◆ signExtend()

template<size_t FromBits, size_t ToBits, typename T >
T IntegerOps::signExtend ( value)
inline

Sign extend value.

If the bit FromBits-1 is set set for value, then the result will have bits FromBits through ToBits-1 also set (other bits are unchanged). If ToBits is less than or equal to FromBits then nothing happens.

Definition at line 88 of file integerOps.h.

◆ signExtend2()

template<typename T >
T IntegerOps::signExtend2 ( value,
size_t  from_width,
size_t  to_width 
)
inline

Sign extend value.

If the bit FromBits-1 is set set for value, then the result will have bits FromBits through ToBits-1 also set (other bits are unchanged). If ToBits is less than or equal to FromBits then nothing happens.

Definition at line 93 of file integerOps.h.

References signBit2().

◆ shiftLeft()

template<size_t NBits, typename T >
T IntegerOps::shiftLeft ( value,
size_t  count 
)
inline

Shifts bits of value left by count bits.

Definition at line 103 of file integerOps.h.

◆ shiftLeft2()

template<typename T >
T IntegerOps::shiftLeft2 ( value,
size_t  count,
size_t  width = 8*sizeof(T) 
)
inline

Shifts bits of value left by count bits.

Definition at line 108 of file integerOps.h.

Referenced by Rose::BitPattern< T >::bits(), and Rose::BitPattern< T >::shift_left().

◆ shiftRightLogical()

template<size_t NBits, typename T >
T IntegerOps::shiftRightLogical ( value,
size_t  count 
)
inline

Shifts bits of value right by count bits without sign extension.

Definition at line 117 of file integerOps.h.

◆ shiftRightLogical2()

template<typename T >
T IntegerOps::shiftRightLogical2 ( value,
size_t  count,
size_t  width = 8*sizeof(T) 
)
inline

Shifts bits of value right by count bits without sign extension.

Definition at line 122 of file integerOps.h.

Referenced by Rose::BitPattern< T >::shift_left(), and shiftRightArithmetic2().

◆ shiftRightArithmetic()

template<size_t NBits, typename T >
T IntegerOps::shiftRightArithmetic ( value,
size_t  count 
)
inline

Shifts bits of value right by count bits with sign extension.

Definition at line 131 of file integerOps.h.

◆ shiftRightArithmetic2()

template<typename T >
T IntegerOps::shiftRightArithmetic2 ( value,
size_t  count,
size_t  width = 8*sizeof(T) 
)
inline

Shifts bits of value right by count bits with sign extension.

Definition at line 141 of file integerOps.h.

References shiftRightLogical2(), and signBit2().

◆ rotateLeft()

template<size_t NBits, typename T >
T IntegerOps::rotateLeft ( value,
size_t  count 
)
inline

Rotate the bits of the value left by count bits.

Definition at line 154 of file integerOps.h.

◆ rotateLeft2()

template<typename T >
T IntegerOps::rotateLeft2 ( value,
size_t  count,
size_t  width = 8*sizeof(T) 
)
inline

Rotate the bits of the value left by count bits.

Definition at line 160 of file integerOps.h.

◆ rotateRight()

template<size_t NBits, typename T >
T IntegerOps::rotateRight ( value,
size_t  count 
)
inline

Rotate bits of the value right by count bits.

Definition at line 170 of file integerOps.h.

◆ rotateRight2()

template<typename T >
T IntegerOps::rotateRight2 ( value,
size_t  count,
size_t  width = 8*sizeof(T) 
)
inline

Rotate bits of the value right by count bits.

Definition at line 176 of file integerOps.h.

◆ isPowerOfTwo()

template<typename T >
bool IntegerOps::isPowerOfTwo ( value)
inline

Returns true if the value is a power of two.

Zero is considered a power of two.

Definition at line 184 of file integerOps.h.

◆ log2max()

template<typename T >
T IntegerOps::log2max ( value)
inline

Returns the base-2 logorithm of value.

If value is not a power of two then the return value is rounded up to the next integer. The value is treated as an unsigned value. Returns zero if value is zero.

Definition at line 201 of file integerOps.h.

◆ log2()

template<typename T >
T IntegerOps::log2 ( a)
inline

Definition at line 220 of file integerOps.h.

◆ shift_to()

template<size_t lobit, size_t hibit, typename T >
T IntegerOps::shift_to ( value)
inline

Create a shifted value.

The return value is created by shifting value to the specified position in the result. Other bits of the return value are clear. The hibit is specified so that we can check at run-time that a valid value was specified (i.e., the value isn't too wide).

Definition at line 235 of file integerOps.h.

◆ shift_to2()

template<typename T >
T IntegerOps::shift_to2 ( size_t  lobit,
size_t  hibit,
value 
)
inline

Create a shifted value.

The return value is created by shifting value to the specified position in the result. Other bits of the return value are clear. The hibit is specified so that we can check at run-time that a valid value was specified (i.e., the value isn't too wide).

Definition at line 242 of file integerOps.h.

◆ extract()

template<size_t lobit, size_t hibit, typename T >
T IntegerOps::extract ( bits)
inline

Extract bits from a value.

Bits lobit through hibit, inclusive, are right shifted into the result and higher-order bits of the result are cleared.

Definition at line 255 of file integerOps.h.

◆ extract2()

template<typename T >
T IntegerOps::extract2 ( size_t  lobit,
size_t  hibit,
bits 
)
inline

Extract bits from a value.

Bits lobit through hibit, inclusive, are right shifted into the result and higher-order bits of the result are cleared.

Definition at line 261 of file integerOps.h.

◆ bitmask_subset()

template<typename T >
bool IntegerOps::bitmask_subset ( m1,
m2 
)
inline

Determines if one bitmask is a subset of another.

Returns true if the bits set in the first argument form a subset of the bits set in the second argument.

Definition at line 272 of file integerOps.h.

◆ countSet()

template<typename T >
size_t IntegerOps::countSet ( val)
inline

Counts how many bits are set (one).

Definition at line 279 of file integerOps.h.

Referenced by Rose::BitPattern< T >::nsignificant().

◆ countClear()

template<typename T >
size_t IntegerOps::countClear ( val)
inline

Counts how many bits are clear (zero).

Definition at line 291 of file integerOps.h.

◆ msb_set()

template<typename T >
boost::optional< size_t > IntegerOps::msb_set ( val)
inline

Optionally returns the zero-origin position of the most significant set bit.

Returns nothing if no bits are set.

Definition at line 303 of file integerOps.h.

Referenced by Rose::BitPattern< T >::width().