ROSE  0.9.9.109
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. More...
 
template<typename T >
genMask (size_t n)
 Bitmask with bits 0 through N-1 set. More...
 
template<typename T >
genMask (size_t lobit, size_t hibit)
 Generate a bitmask. More...
 
template<typename T >
bool isPowerOfTwo (T value)
 Returns true if the value is a power of two. More...
 
template<typename T >
log2max (T value)
 Returns the base-2 logorithm of value. More...
 
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. More...
 
template<typename T >
size_t countSet (T val)
 Counts how many bits are set (one). More...
 
template<typename T >
size_t countClear (T val)
 Counts how many bits are clear (zero). More...
 
template<typename T >
boost::optional< size_t > msb_set (T val)
 Optionally returns the zero-origin position of the most significant set bit. More...
 
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. More...
 
template<typename T >
signExtend2 (T value, size_t from_width, size_t to_width)
 Sign extend value. More...
 
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. More...
 
template<typename T >
shift_to2 (size_t lobit, size_t hibit, T value)
 Create a shifted value. More...
 
template<size_t lobit, size_t hibit, typename T >
extract (T bits)
 Extract bits from a value. More...
 
template<typename T >
extract2 (size_t lobit, size_t hibit, T bits)
 Extract bits from a value. More...
 

Function Documentation

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.

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.

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.

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.

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().

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.

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.

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.

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.

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.

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.

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.

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 BitPattern< uint16_t >::nsignificant().

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

Counts how many bits are clear (zero).

Definition at line 291 of file integerOps.h.

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 BitPattern< uint16_t >::width().