Search code examples
scalachisel

Taking log2Ceil of UInt


I'm taking log2 of following calculation:

tl_out.a.bits.size := log2Ceil(s1_row * s2_column * 4.U)

where, s1_row and s2_column are UInt. I'm getting following error:

[info] Compiling 1 Scala source to ...rocket-chip/target/scala-2.12/classes ...
[error] ...scala:199:25: overloaded method value apply with alternatives:
[error]   (in: Int)Int <and>
[error]   (in: BigInt)Int
[error]  cannot be applied to (chisel3.core.UInt)
[error]         tl_out.a.bits.size := log2Ceil(s1_row * s2_column * 4.U)
[error]                               ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

It seems that log2Ceil can not be performed on UInt/SInt. Is there any way to typecast UINT/SInt or alternate method for log2Ceil which can be performed over UInt? Please assist...

Thanks & Regards,


Solution

  • log2Ceil() is a good function to find size of a word. But be aware that it's not "synthesizable" for UInt() Wire or Reg. log2Ceil() calculation is done once at synthesize time, it's not an hardware function. Then your output signal tl_out.a.bits.size will be a constant.

    If you want to find the most significant set bit, you should use the hardware functions named PriorityEncoder(Reverse(s1_row * s2_column * 4.U)).

    The PriorityEncoder() return the position of least significant '1' in signal. And the Reverse() reverse the bits order in signal.