From 9cad9ec21ac7a9a8c463e2c694b6285269982a84 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Fri, 24 Feb 2017 00:08:18 -0800 Subject: Improve Reverse's exception behavior; avoid log2Up Provide a better error message when length < 0. Change log2Up in log2Ceil, which has no effect, since the argument is always at least 2. --- src/main/scala/chisel3/util/Bitwise.scala | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/scala/chisel3/util/Bitwise.scala b/src/main/scala/chisel3/util/Bitwise.scala index fec4be9a..387fd109 100644 --- a/src/main/scala/chisel3/util/Bitwise.scala +++ b/src/main/scala/chisel3/util/Bitwise.scala @@ -90,10 +90,10 @@ object Fill { * }}} */ object Reverse { - private def doit(in: UInt, length: Int): UInt = { - if (length == 1) { - in - } else if (isPow2(length) && length >= 8 && length <= 64) { + private def doit(in: UInt, length: Int): UInt = length match { + case _ if length < 0 => throw new IllegalArgumentException(s"length (=$length) must be nonnegative integer.") + case _ if length <= 1 => in + case _ if isPow2(length) && length >= 8 && length <= 64 => // This esoterica improves simulation performance var res = in var shift = length >> 1 @@ -104,10 +104,9 @@ object Reverse { shift = shift >> 1 } while (shift > 0) res - } else { - val half = (1 << log2Up(length))/2 + case _ => + val half = (1 << log2Ceil(length))/2 Cat(doit(in(half-1,0), half), doit(in(length-1,half), length-half)) - } } def apply(in: UInt): UInt = doit(in, in.getWidth) -- cgit v1.2.3