[英]How to specify squaring the difference of two unsigned numbers in Chisel3?
Here is one way do it that seems ugly. 这是一种看似难看的方式。
class DiffSquared extends Module {
val inputWidth = 8
val width = 16
val io = IO(new Bundle {
val X = Input(UInt(inputWidth.W))
val M = Input(UInt(inputWidth.W))
val Out = Output(UInt(width.W))
})
val x = Wire(UInt((inputWidth+1).W))
val m = Wire(UInt((inputWidth+1).W))
x := io.X
m := io.M
val diff = Wire(SInt((inputWidth+1).W))
diff := x.asSInt - m.asSInt
io.Out := (diff*diff).asUInt
}
What is a better way to zero-extend the UInt to a 9 bit SInt, do the difference, square it, and represent the result as a 16 bit UInt? 什么是将UInt归零到9位SInt的更好方法,做差异,将其平方,并将结果表示为16位UInt?
Since no one bit, here is a slight improvement. 既然没有一点,这里有一点点改进。 Should we add a zero extension method to UInt to make this better? 我们应该向UInt添加零扩展方法以使其更好吗? Is there already one there? 那里已经有一个吗?
class DiffSquared extends Module {
val inputWidth = 8
val width = 16
val io = IO( new Bundle{
val X = Input(UInt(inputWidth.W))
val M = Input(UInt(inputWidth.W))
val Out = Output(UInt(width.W))
})
def zX(w:UInt) = Wire(UInt((w.getWidth+1).W),init=w).asSInt
val diff = Wire(init=zX(io.X)-zX(io.M))
io.Out := (diff*diff).asUInt
}
There's a useful function here: zext that zero extends a UInt to an SInt of the UInt's width + 1. Thus you could write the code as: 这里有一个很有用的功能: zext将UI扩展为UInt宽度+ 1的SInt。因此,您可以将代码编写为:
class DiffSquared extends Module {
val inputWidth = 8
val width = 16
val io = IO(new Bundle {
val X = Input(UInt(inputWidth.W))
val M = Input(UInt(inputWidth.W))
val Out = Output(UInt(width.W))
})
val diff = io.X.zext() - io.M.zext()
io.Out := (diff*diff).asUInt
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.