blob: 0e1b36d519899df69d622eb8b59026d30a186ebe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
// See LICENSE for license details.
/** Scala-land math helper functions, like logs.
*/
package chisel3.util
import chisel3._
import chisel3.internal.chiselRuntimeDeprecated
/** Compute the log2 rounded up with min value of 1 */
object log2Up {
// Do not deprecate until zero-width wires fully work:
// https://github.com/freechipsproject/chisel3/issues/847
//@chiselRuntimeDeprecated
//@deprecated("Use log2Ceil instead", "chisel3")
def apply(in: BigInt): Int = Chisel.log2Up(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 {
// Do not deprecate until zero-width wires fully work:
// https://github.com/freechipsproject/chisel3/issues/847
//@chiselRuntimeDeprecated
//@deprecated("Use log2Floor instead", "chisel3")
def apply(in: BigInt): Int = Chisel.log2Down(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))
}
|