summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/Math.scala
diff options
context:
space:
mode:
authorRichard Lin2019-03-11 05:09:12 -0700
committeredwardcwang2019-03-11 05:09:12 -0700
commit275515dc8900d2ed87e344318d66232a647e7ff2 (patch)
treeaed9ea0dee0b1204fc52347a44c9cb29ff4071e8 /src/main/scala/chisel3/util/Math.scala
parenta22b3f264c2386af74612fe0c8b83e9910a17b90 (diff)
ScalaDocs improvement for utils Math, MixedVec (#1019)
Diffstat (limited to 'src/main/scala/chisel3/util/Math.scala')
-rw-r--r--src/main/scala/chisel3/util/Math.scala63
1 files changed, 58 insertions, 5 deletions
diff --git a/src/main/scala/chisel3/util/Math.scala b/src/main/scala/chisel3/util/Math.scala
index 0e1b36d5..f9278c7d 100644
--- a/src/main/scala/chisel3/util/Math.scala
+++ b/src/main/scala/chisel3/util/Math.scala
@@ -8,7 +8,21 @@ package chisel3.util
import chisel3._
import chisel3.internal.chiselRuntimeDeprecated
-/** Compute the log2 rounded up with min value of 1 */
+/** Compute the log2 of a Scala integer, rounded up, with min value of 1.
+ * Useful for getting the number of bits needed to represent some number of states (in - 1),
+ * To get the number of bits needed to represent some number n, use log2Up(n + 1).
+ * with the minimum value preventing the creation of currently-unsupported zero-width wires.
+ *
+ * Note: prefer to use log2Ceil when in is known to be > 1 (where log2Ceil(in) > 0).
+ * This will be deprecated when zero-width wires is supported.
+ *
+ * @example {{{
+ * log2Up(1) // returns 1
+ * log2Up(2) // returns 1
+ * log2Up(3) // returns 2
+ * log2Up(4) // returns 2
+ * }}}
+ */
object log2Up {
// Do not deprecate until zero-width wires fully work:
// https://github.com/freechipsproject/chisel3/issues/847
@@ -17,7 +31,20 @@ object log2Up {
def apply(in: BigInt): Int = Chisel.log2Up(in)
}
-/** Compute the log2 rounded up */
+/** Compute the log2 of a Scala integer, rounded up.
+ * Useful for getting the number of bits needed to represent some number of states (in - 1).
+ * To get the number of bits needed to represent some number n, use log2Ceil(n + 1).
+ *
+ * Note: can return zero, and should not be used in cases where it may generate unsupported
+ * zero-width wires.
+ *
+ * @example {{{
+ * log2Ceil(1) // returns 0
+ * log2Ceil(2) // returns 1
+ * log2Ceil(3) // returns 2
+ * log2Ceil(4) // returns 2
+ * }}}
+ */
object log2Ceil {
def apply(in: BigInt): Int = {
require(in > 0)
@@ -26,7 +53,15 @@ object log2Ceil {
def apply(in: Int): Int = apply(BigInt(in))
}
-/** Compute the log2 rounded down with min value of 1 */
+/** Compute the log2 of a Scala integer, rounded down, with min value of 1.
+ *
+ * @example {{{
+ * log2Down(1) // returns 1
+ * log2Down(2) // returns 1
+ * log2Down(3) // returns 1
+ * log2Down(4) // returns 2
+ * }}}
+ */
object log2Down {
// Do not deprecate until zero-width wires fully work:
// https://github.com/freechipsproject/chisel3/issues/847
@@ -35,13 +70,31 @@ object log2Down {
def apply(in: BigInt): Int = Chisel.log2Down(in)
}
-/** Compute the log2 rounded down */
+/** Compute the log2 of a Scala integer, rounded down.
+ *
+ * Can be useful in computing the next-smallest power of two.
+ *
+ * @example {{{
+ * log2Floor(1) // returns 0
+ * log2Floor(2) // returns 1
+ * log2Floor(3) // returns 1
+ * log2Floor(4) // returns 2
+ * }}}
+ */
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 */
+/** Returns whether a Scala integer is a power of two.
+ *
+ * @example {{{
+ * isPow2(1) // returns true
+ * isPow2(2) // returns true
+ * isPow2(3) // returns false
+ * isPow2(4) // returns true
+ * }}}
+ */
object isPow2 {
def apply(in: BigInt): Boolean = in > 0 && ((in & (in-1)) == 0)
def apply(in: Int): Boolean = apply(BigInt(in))