summaryrefslogtreecommitdiff
path: root/src/main/scala/Chisel/Log2.scala
blob: 01b081efbf817f69692bf39cc975f1491ba4a000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Chisel

// TODO: these operators should be backend nodes so their width can be
// inferred, rather than using getWidth.  also, C++ perf would improve.

object Log2 {
  def apply(x: Bits, width: Int): UInt = {
    if (width < 2) UInt(0)
    else if (width == 2) x(1)
    else Mux(x(width-1), UInt(width-1), apply(x, width-1))
  }

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

object OHToUInt {
  def apply(in: Seq[Bool]): UInt = apply(Vec(in))
  def apply(in: Vec[Bool]): UInt = apply(in.toBits, in.size)
  def apply(in: Bits): UInt = apply(in, in.getWidth)

  def apply(in: Bits, width: Int): UInt = {
    if (width <= 2) Log2(in, width)
    else {
      val mid = 1 << (log2Up(width)-1)
      val hi = in(width-1, mid)
      val lo = in(mid-1, 0)
      Cat(hi.orR, apply(hi | lo, mid))
    }
  }
}

object PriorityEncoder {
  def apply(in: Seq[Bool]): UInt = PriorityMux(in, (0 until in.size).map(UInt(_)))
  def apply(in: Bits): UInt = apply(in.toBools)
}