diff options
| author | ducky | 2015-10-26 17:30:15 -0700 |
|---|---|---|
| committer | Palmer Dabbelt | 2015-11-02 13:04:21 -0800 |
| commit | 6c34949c45cf7ddfdabafb749de2cb9e439b381e (patch) | |
| tree | b4bcdd586123b571a2f53b6726e501003dd46119 /src/main/scala/Chisel/util/Math.scala | |
| parent | 178f5c564e9ab0594656185e2e0a5bcc029d5743 (diff) | |
Break out Utils.scala into smaller portions
Diffstat (limited to 'src/main/scala/Chisel/util/Math.scala')
| -rw-r--r-- | src/main/scala/Chisel/util/Math.scala | 39 |
1 files changed, 39 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..270301f0 --- /dev/null +++ b/src/main/scala/Chisel/util/Math.scala @@ -0,0 +1,39 @@ +// See LICENSE for license details. + +/** Scala-land math helper functions, like logs. + */ + +package Chisel + +/** Compute the log2 rounded up with min value of 1 */ +object log2Up { + def apply(in: BigInt): Int = 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)) +} |
