ucb-bar / dsptools

A Library of Chisel3 Tools for Digital Signal Processing
Apache License 2.0
221 stars 38 forks source link

Arithmetic between types #250

Open jurevreca12 opened 11 months ago

jurevreca12 commented 11 months ago

Is it possible to do arithmetic between types? i.e. something akin to the following:

import chisel3._
import chisel3.stage.ChiselStage
import dsptools.numbers.Real
import dsptools.numbers.implicits._

class Test[I <: Bits: Real, W <: Bits: Real, O <: Bits](genI: I, genW: W, genO: O) extends Module {
    val inI = IO(Input(genI))
    val inW = IO(Input(genW))
    val out = IO(Output(genO))
    out := inI * inW
}

object Main extends App {
    println((new ChiselStage).emitVerilog(new Test(UInt(2.W), UInt(3.W), UInt(5.W))))
}

I realize that the Real/Ring type class defines the arithmetic only on the same type:

trait Ring[A] extends Any with spire.algebra.Ring[A] {
  def plusContext(f:   A, g: A): A
  def minusContext(f:  A, g: A): A
  def timesContext(f:  A, g: A): A
  def negateContext(f: A): A
}

however, Chisel does support multiplication with both UInt and SInt, and obviously at the hardware level this should work as well.

So is it possible to use dpstools (or extend it) to support dsp operations between different types?