ucb-bar / dsptools

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

Set precision in div2 fixedpoint and interval may have provblems #205

Open chick opened 4 years ago

chick commented 4 years ago

Note comment in IntervalTypeClass

  // Retains significant digits while dividing
  override def div2(a: Interval, n: Int): Interval = {
    require(a.widthKnown, "Interval point width must be known for div2")
    require(a.binaryPoint.known, "Binary point must be known for div2")
    val newBP = a.binaryPoint.get + n
    // Normal shift loses significant digits; this version doesn't
    val inLong = Wire(Interval((a.getWidth + n).W, newBP.BP))
    inLong := a
    val outFull = Wire(Interval(a.getWidth.W, newBP.BP))
    // Upper n bits don't contain meaningful data following shift, so remove
    outFull := inLong >> n
    // Note: The above doesn't rely on tools to expand, shrink correctly; the version below does.
    // Assumes setBinaryPoint zero-extends. BUT Chisel doesn't seem to get widths properly and
    // some other ops rely on width correctness... (even though Firrtl is right...)
    //a.setBinaryPoint(newBP) >> n
    trimBinary(outFull, Some(a.binaryPoint.get + context.binaryPointGrowth))
  }