summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel/util/Math.scala
diff options
context:
space:
mode:
authorRichard Lin2016-06-08 17:12:42 -0700
committerRichard Lin2016-06-08 17:12:42 -0700
commit07fa5622ccc995f925d6d967d2a386540c9064cc (patch)
treefab4d62dba2ffb78e673ee54939d9088739a79f1 /src/main/scala/chisel/util/Math.scala
parent53813f61b7dfe246d214ab966739d01c65c8ecb0 (diff)
parent68447044e8eba5c8f525639130f1a347677ff543 (diff)
Merge pull request #197 from ucb-bar/lowercaseChisel
Rename package Chisel to chisel, add Chisel package for compatibility
Diffstat (limited to 'src/main/scala/chisel/util/Math.scala')
-rw-r--r--src/main/scala/chisel/util/Math.scala44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/main/scala/chisel/util/Math.scala b/src/main/scala/chisel/util/Math.scala
new file mode 100644
index 00000000..69464d15
--- /dev/null
+++ b/src/main/scala/chisel/util/Math.scala
@@ -0,0 +1,44 @@
+// See LICENSE for license details.
+
+/** Scala-land math helper functions, like logs.
+ */
+
+package chisel.util
+
+import chisel._
+
+/** Compute the log2 rounded up with min value of 1 */
+object log2Up {
+ def apply(in: BigInt): Int = {
+ require(in >= 0)
+ 1 max (in-1).bitLength
+ }
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Compute the log2 rounded up */
+object log2Ceil {
+ def apply(in: BigInt): Int = {
+ require(in > 0)
+ (in-1).bitLength
+ }
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Compute the log2 rounded down with min value of 1 */
+object log2Down {
+ def apply(in: BigInt): Int = log2Up(in) - (if (isPow2(in)) 0 else 1)
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Compute the log2 rounded down */
+object log2Floor {
+ def apply(in: BigInt): Int = log2Ceil(in) - (if (isPow2(in)) 0 else 1)
+ def apply(in: Int): Int = apply(BigInt(in))
+}
+
+/** Check if an Integer is a power of 2 */
+object isPow2 {
+ def apply(in: BigInt): Boolean = in > 0 && ((in & (in-1)) == 0)
+ def apply(in: Int): Boolean = apply(BigInt(in))
+}