summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/Math.scala
blob: 9091b0b4b9c15a9772248e022a2644a9a120b3ed (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
49
50
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import org.scalacheck.Shrink

class Math extends ChiselPropSpec {
  import chisel3.util._
  // Disable shrinking on error.
  implicit val noShrinkListVal = Shrink[List[Int]](_ => Stream.empty)
  implicit val noShrinkInt = Shrink[Int](_ => Stream.empty)

  property ("unsignedBitLength is computed correctly") {
    forAll(safeUIntWidth) { case (width: Int) =>
      for ( offset <- List(-1, 0, 1)) {
        val n = (1 << width) + offset
        if (n >= 0) {
          val d = unsignedBitLength(n)
          val t = if (n == 0) 0 else if (offset < 0) width else width + 1
          d shouldEqual (t)
        }
      }
    }
  }

  property ("signedBitLength is computed correctly") {
    forAll(safeUIntWidth) { case (width: Int) =>
      for ( offset <- List(-1, 0, 1)) {
        for ( mult <- List(-1, +1)) {
          val n = ((1 << (width - 1)) + offset) * mult
          val d = signedBitLength(n)
          val t = n match {
            case -2 => 2
            case -1 => 1
            case 0 => 0
            case 1 => 2
            case 2 => 3
            case _ =>
              if (n > 0) {
                if (offset < 0) width else width + 1
              } else {
                if (offset > 0) width + 1 else width
              }
          }
          d shouldEqual (t)
        }
      }
    }
  }
}