问题
As tile, how to cast UInt to SInt value in Chisel3 in right way? ig:
val opC = RegInit(0.U(64.W))
val result = RegInit(0.U(64.W))
result := Mux(opC.toSInt > 0.S, opC, 0.U)
回答1:
It depends on if you want to reinterpret as an SInt (same width), or actually cast (ie. casting an 8-bit UInt results in a 9-bit SInt).
You should reinterpret a UInt to an SInt by calling .asSInt
on the UInt. eg. opC.asSInt
, the result will be the same width.
You should cast a UInt to an SInt by calling .zext
on the UInt. eg. opC.zext
, the result will be 1-bit wider with a zero in the msb.
来源:https://stackoverflow.com/questions/49081218/how-to-cast-uint-to-sint-value-in-chisel3