summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/FixedPointSpec.scala
blob: d0557e66f198d8f1af741b8815886c260da258c9 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// See LICENSE for license details.

package chiselTests

import chisel3._
import chisel3.experimental.FixedPoint
import chisel3.testers.BasicTester
import org.scalatest._

//scalastyle:off magic.number
class FixedPointLiteralSpec extends FlatSpec with Matchers {
  behavior of "fixed point utilities"

  they should "allow conversion between doubles and the bigints needed to represent them" in {
    val initialDouble = 0.125
    val bigInt = FixedPoint.toBigInt(initialDouble, 4)
    val finalDouble = FixedPoint.toDouble(bigInt, 4)

    initialDouble should be(finalDouble)
  }
}

class FixedPointFromBitsTester extends BasicTester {
  val uint = 3.U(4.W)
  val sint = -3.S

  val fp   = FixedPoint.fromDouble(3.0, width = 4, binaryPoint = 0)
  val fp_tpe = FixedPoint(4.W, 1.BP)
  val uint_result = FixedPoint.fromDouble(1.5, width = 4, binaryPoint = 1)
  val sint_result = FixedPoint.fromDouble(-1.5, width = 4, binaryPoint = 1)
  val fp_result   = FixedPoint.fromDouble(1.5, width = 4, binaryPoint = 1)

  val uint2fp = fp_tpe.fromBits(uint)
  val sint2fp = fp_tpe.fromBits(sint)
  val fp2fp   = fp_tpe.fromBits(fp)

  val negativefp = -3.5.F(binaryPoint = 4)
  val positivefp = 3.5.F(binaryPoint = 4)

  assert(uint2fp === uint_result)
  assert(sint2fp === sint_result)
  assert(fp2fp   === fp_result)

  assert(positivefp.abs() === positivefp)
  assert(negativefp.abs() === positivefp)
  assert(negativefp.abs() =/= negativefp)

  stop()
}

class SBP extends Module {
  val io = IO(new Bundle {
    val in =  Input(FixedPoint(6, 2))
    val out = Output(FixedPoint(4, 0))
  })
  io.out := io.in.setBinaryPoint(0)
}
class SBPTester extends BasicTester {
  val dut = Module(new SBP)
  dut.io.in := FixedPoint.fromDouble(3.75, binaryPoint = 2)

  assert(dut.io.out === FixedPoint.fromDouble(3.0, binaryPoint = 0))

  stop()
}

class FixedPointSpec extends ChiselPropSpec {
  property("should allow set binary point") {
    assertTesterPasses { new SBPTester }
  }
  property("should allow fromBits") {
    assertTesterPasses { new FixedPointFromBitsTester }
  }
}