gpt4 book ai didi

scala - 如何将凿子 dsptools 与浮子一起使用

转载 作者:行者123 更新时间:2023-12-03 20:50:23 27 4
gpt4 key购买 nike

我需要将 Float32 转换为 Chisel FixedPoint,执行一些计算并将 FixedPoint 转换回 Float32。

例如,我需要以下内容:

val a = 3.1F
val b = 2.2F
val res = a * b // REPL returns res: Float 6.82

现在,我这样做:

import chisel3.experimental.FixedPoint

val fp_tpe = FixedPoint(6.W, 2.BP)
val a_fix = a.Something (fp_tpe) // convert a to FixPoint
val b_fix = b.Something (fp_tpe) // convert b to FixPoint
val res_fix = a_fix * b_fix
val res0 = res_fix.Something (fp_tpe) // convert back to Float

因此,我希望增量在 的范围内,例如

val eps = 1e-4
assert ( abs(res - res0) < eps, "The error is too big")

谁可以为上述伪代码提供 Chisel3 FixedPoint 类的工作示例?

最佳答案

看看下面的代码:

import chisel3._
import chisel3.core.FixedPoint
import dsptools._


class FPMultiplier extends Module {
val io = IO(new Bundle {
val a = Input(FixedPoint(6.W, binaryPoint = 2.BP))
val b = Input(FixedPoint(6.W, binaryPoint = 2.BP))
val c = Output(FixedPoint(12.W, binaryPoint = 4.BP))
})

io.c := io.a * io.b
}

class FPMultiplierTester(c: FPMultiplier) extends DspTester(c) {
//
// This will PASS, there is sufficient precision to model the inputs
//
poke(c.io.a, 3.25)
poke(c.io.b, 2.5)

step(1)
expect(c.io.c, 8.125)

//
// This will FAIL, there is not sufficient precision to model the inputs
// But this is only caught on output, this is likely the right approach
// because you can't really pass in wrong precision data in hardware.
//
poke(c.io.a, 3.1)
poke(c.io.b, 2.2)

step(1)
expect(c.io.c, 6.82)
}


object FPMultiplierMain {
def main(args: Array[String]): Unit = {
iotesters.Driver.execute(Array("-fiv"), () => new FPMultiplier) { c =>
new FPMultiplierTester(c)
}
}
}

我还建议查看 ParameterizedAdderdsptools ,这让您了解如何编写传递不同类型的硬件模块。通常,您从 DspReals 开始,确认模型,然后开始使用 FixedPoint 大小进行实验/计算,以返回具有所需精度的结果。

关于scala - 如何将凿子 dsptools 与浮子一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469257/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com