bitwise
Modules
ToOneHot #(int SIZE)
module ToOneHot #(int SIZE) : int#(0..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#(0..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) : bool[SIZE] bits'0 -> int#(0..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) : int#(FROM: 0, TO) v'0 -> int#(0..(TO-1) / pow2#(E: LOWER_BITS) + 1) upper'0, int#(0..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) : int#(0..UPPER_TO) upper'0, int#(0..pow2#(E: LOWER_BITS)) lower'0 -> int#(0..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) : int#(FROM, TO) i'0 -> int#(FROM - (FROM mod clog2#(V: LOWER_BITS))..TO - (TO + 1 mod clog2#(V: LOWER_BITS))) o'0 {
}Sets the lower [..LOWER_BITS] bits to false, thus aligning the value
Works for unsigned and signed integers alike.