Function clog2

Source
pub const fn clog2(t: usize) -> usize
Expand description

Compute the minimum number of bits to represent a container with t items. This is basically ceil(log2(t)) as a constant (compile time computable) function. You can use it where a const generic (bit width) argument is required.

Example

Unfortunately, with stable Rust, this function is not of much use. For now, const generic arguments cannot be used in expressions yet. Suppose we want to design a simple state machine that counts from from 0 to some maximum number N-1, and then cycles again. We want to specify the maximum number, not the number of bits needed to represent it. In this case, we would like to use the compile time clog2 function to compute the bit width of the signal that holds the count.


#[derive(LogicBlock, Default)]
struct CountToN<const N: usize> {
    signal_out: Signal<Out, Bits<{clog2({N})}>>,
}