summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/FixedPointSpec.scala
blob: a928f08e6232324fcfe65ada4f9d81a492ed9fb4 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// See LICENSE for license details.

package chiselTests

import chisel3._
import chisel3.experimental.FixedPoint
import chisel3.internal.firrtl.{BinaryPoint, Width}
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)
  }
}

//noinspection TypeAnnotation,EmptyParenMethodAccessedAsParameterless
class FixedPointFromBitsTester extends BasicTester {
  val uint = 3.U(4.W)
  val sint = (-3).S

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

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

  val uintToFp = uint.asFixedPoint(1.BP)
  val sintToFp = sint.asFixedPoint(1.BP)
  val fpToFp   = fp.asFixedPoint(1.BP)

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

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

  assert(uintToFp === uint_result)
  assert(sintToFp === sint_result)
  assert(fpToFp   === fp_result)

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

  val f1bp5 = 1.5.F(1.BP)
  val f6bp0 = 6.0.F(0.BP)
  val f6bp2 = 6.0.F(2.BP)

  val f1bp5shiftleft2 = Wire(FixedPoint(Width(), BinaryPoint()))
  val f6bp0shiftright2 = Wire(FixedPoint(Width(), BinaryPoint()))
  val f6bp2shiftright2 = Wire(FixedPoint(Width(), BinaryPoint()))

  f1bp5shiftleft2 := f1bp5 << 2
  f6bp0shiftright2 := f6bp0 >> 2
  f6bp2shiftright2 := f6bp2 >> 2

  assert(f1bp5shiftleft2 === f6bp0)
  assert(f1bp5shiftleft2 === 6.0.F(8.BP))

  // shifting does not move binary point, so in first case below one bit is lost in shift
  assert(f6bp0shiftright2 === 1.0.F(0.BP))
  assert(f6bp2shiftright2 === 1.5.F(2.BP))


  stop()
}

class FixedPointMuxTester extends BasicTester {
  val largeWidthLowPrecision = 6.0.F(4.W, 0.BP)
  val smallWidthHighPrecision = 0.25.F(2.W, 2.BP)
  val unknownWidthLowPrecision = 6.0.F(0.BP)
  val unknownFixed = Wire(FixedPoint())
  unknownFixed := smallWidthHighPrecision

  assert(Mux(true.B, largeWidthLowPrecision, smallWidthHighPrecision) === 6.0.F(0.BP))
  assert(Mux(false.B, largeWidthLowPrecision, smallWidthHighPrecision) === 0.25.F(2.BP))
  assert(Mux(false.B, largeWidthLowPrecision, unknownFixed) === 0.25.F(2.BP))
  assert(Mux(true.B, unknownWidthLowPrecision, smallWidthHighPrecision) === 6.0.F(0.BP))

  stop()
}

class SBP extends Module {
  val io = IO(new Bundle {
    val in =  Input(FixedPoint(6.W, 2.BP))
    val out = Output(FixedPoint(4.W, 0.BP))
  })
  io.out := io.in.setBinaryPoint(0)
}

class SBPTester extends BasicTester {
  val dut = Module(new SBP)
  dut.io.in := 3.75.F(2.BP)

  assert(dut.io.out === 3.0.F(0.BP))

  val test = Wire(FixedPoint(10.W, 5.BP))
  // Initialize test, avoiding a "Reference test is not fully initialized" error from firrtl.
  test := 0.0.F(5.BP)
  val q = test.setBinaryPoint(18)
  assert(q.getWidth.U === 23.U)

  stop()
}

class FixedPointLitExtractTester extends BasicTester {
  assert(-4.75.F(2.BP)(1) === false.B)
  assert(-4.75.F(2.BP)(2) === true.B)
  assert(-4.75.F(2.BP)(100) === true.B)
  assert(-4.75.F(2.BP)(3, 0) === "b1101".U)
  assert(-4.75.F(2.BP)(9, 0) === "b1111101101".U)

  assert(-4.75.F(6.W, 2.BP)(1) === false.B)
  assert(-4.75.F(6.W, 2.BP)(2) === true.B)
  assert(-4.75.F(6.W, 2.BP)(100) === true.B)
  assert(-4.75.F(6.W, 2.BP)(3, 0) === "b1101".U)
  assert(-4.75.F(6.W, 2.BP)(9, 0) === "b1111101101".U)
  stop()
}

class FixedPointSpec extends ChiselPropSpec {
  property("should allow set binary point") {
    assertTesterPasses { new SBPTester }
  }
  property("should allow fromBits") {
    assertTesterPasses { new FixedPointFromBitsTester }
  }
  property("should mux different widths and binary points") {
    assertTesterPasses { new FixedPointMuxTester }
  }
  property("Negative shift amounts are invalid") {
    a [ChiselException] should be thrownBy { elaborate(new NegativeShift(FixedPoint(1.W, 0.BP))) }
  }
  property("Bit extraction on literals should work for all non-negative indices") {
    assertTesterPasses(new FixedPointLitExtractTester)
  }
}