bitwise
Modules
ToOneHot #(int SIZE)
module ToOneHot #(int SIZE) {
interface ToOneHot : int#(FROM: 0, TO: SIZE - 1) selection'0 -> bool[SIZE] bits'0
}Creates a One-Hot encoding of the given integer.
Examples:
ToOneHot#(SIZE: 5)(3) == [false, false, false, true, false]
ToOneHot#(SIZE: 2)(0) == [true, false]
FirstSetBit #(int SIZE)
module FirstSetBit #(int SIZE) {
trigger is_nonzero : int#(FROM: 0, TO: SIZE) first_nonzero'0
action find : bool[SIZE] bits'0
}This module returns the index of the first true bit in the given bitstring.
If the whole bitstring is false, then is_nonzero is not fired.
If there is at least one set (‘1’) bit, is_nonzero triggers with the index of the first ‘1’ bit.
Usage:
FirstSetBit find_first_bit
find_first_bit.find([false, false, true, false, true])
when find_first_bit.is_nonzero : int first_nonzero {
// Fires with `2`
}
PopCount #(int SIZE)
module PopCount #(int SIZE) {
interface PopCount : bool[SIZE] bits'0 -> int#(FROM: 0, TO: SIZE+1) popcount
}Counts the number of set (‘1’) bits in the bitstring.
Examples:
PopCount([true, false, true, true, true]) == 4
PopCount(8'b00111011) == 5
BitwiseIntSplit #(int TO, int LOWER_BITS)
module BitwiseIntSplit #(int TO, int LOWER_BITS) {
interface BitwiseIntSplit : int#(FROM: 0, TO) v'0 -> int#(FROM: 0, TO: UPPER_TO) upper'0, int#(FROM: 0, TO: pow2#(E: LOWER_BITS)) lower'0
}Splits the given integer. The upper [LOWER_BITS:] bits are returned as upper, the lower [:LOWER_BITS] bits are returned in lower
See also BitwiseIntConcat.
BitwiseIntConcat #(int UPPER_TO, int LOWER_BITS)
module BitwiseIntConcat #(int UPPER_TO, int LOWER_BITS) {
interface BitwiseIntConcat : int#(FROM: 0, TO: UPPER_TO) upper'0, int#(FROM: 0, TO: pow2#(E: LOWER_BITS)) lower'0 -> int#(FROM: 0, TO: UPPER_TO * pow2#(E: LOWER_BITS)) v'0
}Recombines a lower and upper integer parts. See also BitwiseIntSplit.
AlignToPow2 #(int FROM, int TO, int LOWER_BITS)
module AlignToPow2 #(int FROM, int TO, int LOWER_BITS) {
interface AlignToPow2 : int#(FROM, TO) i'0 -> int#(FROM: ALIGNED_FROM, TO: ALIGNED_TO) o'0
}Sets the lower [:LOWER_BITS] bits to false, thus aligning the value
Works for unsigned and signed integers alike.