From 4f81b57bce638815de6671c2652095578773e935 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Fri, 24 Feb 2017 00:19:03 -0800 Subject: 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. --- src/main/scala/chisel3/util/OneHot.scala | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src') 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, -- cgit v1.2.3