diff options
| author | Andrew Waterman | 2017-02-24 00:19:03 -0800 |
|---|---|---|
| committer | Jack Koenig | 2017-03-08 11:27:04 -0600 |
| commit | 4f81b57bce638815de6671c2652095578773e935 (patch) | |
| tree | df84e3efd665867c480a807625333bcdf09903dc /src | |
| parent | 17ee2b5a344c9ff479cccade709eccddd75ce3a7 (diff) | |
Improve UIntToOH behavior on incorrect inputs; avoid log2Up
The old implementation failed to check for width <= -2, and
did the wrong thing when -1 was explicitly passed. Splitting
into two methods avoids the latter issue.
log2Ceil's argument might be 1, so employ a max operator.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/chisel3/util/OneHot.scala | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/main/scala/chisel3/util/OneHot.scala b/src/main/scala/chisel3/util/OneHot.scala index 9c1232e4..9a911c41 100644 --- a/src/main/scala/chisel3/util/OneHot.scala +++ b/src/main/scala/chisel3/util/OneHot.scala @@ -42,12 +42,13 @@ object PriorityEncoder { /** Returns the one hot encoding of the input UInt. */ object UIntToOH { - def apply(in: UInt, width: Int = -1): UInt = - if (width == -1) { - 1.U << in - } else { - (1.U << in(log2Up(width)-1,0))(width-1,0) - } + def apply(in: UInt): UInt = 1.U << in + def apply(in: UInt, width: Int): UInt = width match { + case 0 => 0.U(0.W) + case _ => + val shiftAmount = in((log2Ceil(width) - 1) max 0, 0) + (1.U << shiftAmount)(width - 1, 0) + } } /** Returns a bit vector in which only the least-significant 1 bit in the input vector, if any, |
