Skip to content

Commit

Permalink
Improve QoR for Log2
Browse files Browse the repository at this point in the history
For reasonable circuit delay, need to divide & conquer.
  • Loading branch information
Andrew Waterman committed Jul 7, 2016
1 parent a49e27d commit c90be4e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/scala/chisel3/util/CircuitMath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ object Log2 {
UInt(0)
} else if (width == 2) {
x(1)
} else {
} else if (width <= divideAndConquerThreshold) {
Mux(x(width-1), UInt(width-1), apply(x, width-1))
} else {
val mid = 1 << (log2Ceil(width) - 1)
val hi = x(width-1, mid)
val lo = x(mid-1, 0)
val useHi = hi.orR
Cat(useHi, Mux(useHi, Log2(hi, width - mid), Log2(lo, mid)))
}
}

def apply(x: Bits): UInt = apply(x, x.getWidth)

private def divideAndConquerThreshold = 4
}

0 comments on commit c90be4e

Please sign in to comment.