Skip to content

Commit

Permalink
Add messages to require statements in Math (#3385)
Browse files Browse the repository at this point in the history
* log2 functions
* unsignedBitLength

(cherry picked from commit 88fecfc)

# Conflicts:
#	src/main/scala/chisel3/util/Math.scala
  • Loading branch information
jackkoenig authored and mergify[bot] committed Jun 27, 2023
1 parent f5ea92a commit 389f9d8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/scala/chisel3/util/Math.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ object log2Up {
// https://github.com/freechipsproject/chisel3/issues/847
//@chiselRuntimeDeprecated
//@deprecated("Use log2Ceil instead", "chisel3")
<<<<<<< HEAD
def apply(in: BigInt): Int = Chisel.log2Up(in)
=======
def apply(in: BigInt): Int = {
require(in >= 0, s"log2Up is only defined on integers >= 0, got $in")
1.max((in - 1).bitLength)
}
def apply(in: Int): Int = apply(BigInt(in))
>>>>>>> 88fecfc1c (Add messages to require statements in Math (#3385))
}

/** Compute the log2 of a Scala integer, rounded up.
Expand All @@ -46,7 +54,7 @@ object log2Up {
*/
object log2Ceil {
def apply(in: BigInt): Int = {
require(in > 0)
require(in > 0, s"log2 is only defined on integers > 0, got $in")
(in - 1).bitLength
}
def apply(in: Int): Int = apply(BigInt(in))
Expand Down Expand Up @@ -109,7 +117,7 @@ object unsignedBitLength {
* @return - an Int representing the number of bits to encode.
*/
def apply(in: BigInt): Int = {
require(in >= 0)
require(in >= 0, s"Unsigned integers must be non-negative, got $in")
in.bitLength
}
}
Expand Down

0 comments on commit 389f9d8

Please sign in to comment.