xref: /XiangShan/src/main/scala/xiangshan/backend/fu/fpu/FPU.scala (revision 039cdc35f5f3b68b6295ec5ace90f22a77322e02)
1package xiangshan.backend.fu.fpu
2
3import chisel3._
4import chisel3.util._
5import fudian.FloatPoint
6
7object FPU {
8
9  case class FType(expWidth: Int, precision: Int) {
10    val sigWidth = precision - 1
11    val len = expWidth + precision
12  }
13
14  val f32 = FType(8, 24)
15  val f64 = FType(11, 53)
16
17  val ftypes = List(f32, f64)
18
19  val S = ftypes.indexOf(f32).U(log2Ceil(ftypes.length).W)
20  val D = ftypes.indexOf(f64).U(log2Ceil(ftypes.length).W)
21
22  def unbox(x: UInt, typeTag: UInt): UInt = {
23    require(x.getWidth == 64)
24    val isBoxed = x.head(32).andR
25    Mux(typeTag === D,
26      x,
27      Mux(isBoxed,
28        x.tail(32),
29        FloatPoint.defaultNaNUInt(f32.expWidth, f32.precision)
30      )
31    )
32  }
33
34  def box(x: UInt, typeTag: UInt): UInt = {
35    require(x.getWidth == 64)
36    Mux(typeTag === D, x, Cat(~0.U(32.W), x(31, 0)))
37  }
38
39  def box(x: UInt, t: FType): UInt = {
40    if(t == f32){
41      Cat(~0.U(32.W), x(31, 0))
42    } else if(t == f64){
43      x(63, 0)
44    } else {
45      assert(cond = false, "Unknown ftype!")
46      0.U
47    }
48  }
49
50}
51