Livt packages
Eccelerators.BitTwiddling
Version 0.1.1
Readable 32-bit bit manipulation, counting, scanning, permutation, and arithmetic utilities for Livt.
Published files (26)
src/permutation/BitPermutation.lvt
namespace Eccelerators.BitTwiddling.Permutation
/**
* Provides rotations and fixed permutations of bits and bytes.
*
* Fixed reversals and byte swaps describe deterministic routing networks.
* Variable rotations may synthesize as barrel shifters.
*/
class BitPermutation
{
/**
* Rotates value left by distance positions with wraparound at bit 31.
*
* Valid distances are 0 through 31. Invalid distances return value
* unchanged rather than relying on target-specific shift behavior.
*/
public static fn RotateLeft32[](value: uint, distance: int) uint
{
if ((distance < 0) || (distance > 31))
{
return value
}
if (distance == 0)
{
return value
}
return (value << distance) | (value >> (32 - distance))
}
/**
* Rotates value right by distance positions with wraparound at bit 0.
*
* Valid distances are 0 through 31. Invalid distances return value
* unchanged rather than relying on target-specific shift behavior.
*/
public static fn RotateRight32[](value: uint, distance: int) uint
{
if ((distance < 0) || (distance > 31))
{
return value
}
if (distance == 0)
{
return value
}
return (value >> distance) | (value << (32 - distance))
}
/**
* Reverses all 32 bits of value so input bit n becomes output bit 31 - n.
*/
public static fn Reverse32[](value: uint) uint
{
var result: uint = value
result = ((result >> 1) & (0x55555555 as uint)) |
((result & (0x55555555 as uint)) << 1)
result = ((result >> 2) & (0x33333333 as uint)) |
((result & (0x33333333 as uint)) << 2)
result = ((result >> 4) & (0x0F0F0F0F as uint)) |
((result & (0x0F0F0F0F as uint)) << 4)
result = ((result >> 8) & (0x00FF00FF as uint)) |
((result & (0x00FF00FF as uint)) << 8)
return (result >> 16) | (result << 16)
}
/**
* Reverses all eight bits of value so input bit n becomes output bit 7 - n.
*/
public static fn ReverseByte[](value: byte) byte
{
var result: uint = value as uint
result = ((result >> 1) & (0x55 as uint)) |
((result & (0x55 as uint)) << 1)
result = ((result >> 2) & (0x33 as uint)) |
((result & (0x33 as uint)) << 2)
result = ((result >> 4) & (0x0F as uint)) |
((result & (0x0F as uint)) << 4)
return result as byte
}
/**
* Reverses the order of the four bytes in value.
*
* Bits inside each byte retain their original order.
*/
public static fn ByteSwap32[](value: uint) uint
{
return ((value & (0x000000FF as uint)) << 24) |
((value & (0x0000FF00 as uint)) << 8) |
((value & (0x00FF0000 as uint)) >> 8) |
((value & (0xFF000000 as uint)) >> 24)
}
}