Skip to content

Commit 257cc11

Browse files
add hardfloat
1 parent d5936ba commit 257cc11

File tree

9 files changed

+131
-28
lines changed

9 files changed

+131
-28
lines changed

.gitmodules

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
[submodule "fputil"]
2-
path = fputil
1+
[submodule "depencies/fputil"]
2+
path = depencies/fputil
33
url = git@github.com:CodingPlatelets/fp-division-pipelined.git
4+
[submodule "depencies/hardfloat"]
5+
path = depencies/hardfloat
6+
url = git@github.com:CodingPlatelets/berkeley-hardfloat.git

build.sbt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ val chiselVersion = "6.2.0"
88
lazy val commonChiselSettings = Seq(
99
libraryDependencies ++= Seq(
1010
"org.chipsalliance" %% "chisel" % chiselVersion,
11-
"edu.berkeley.cs" %% "chiseltest" % "6.0.0"
11+
"edu.berkeley.cs" %% "chiseltest" % "6.0.0",
12+
// "edu.berkeley.cs" %% "hardfloat" % "1.5.1-SNAPSHOT"
1213
),
1314
resolvers += "aliyun".at("https://maven.aliyun.com/repository/public"),
1415
scalacOptions ++= Seq(
@@ -30,7 +31,12 @@ lazy val root = (project in file("."))
3031
commonChiselSettings
3132
)
3233

33-
lazy val fputil = (project in file("fputil/src/main/scala")).settings(
34-
name := "fputil",
35-
commonChiselSettings
36-
)
34+
lazy val fputil = Project("fputil", file("depencies/fputil/src"))
35+
.settings(
36+
name := "fputil",
37+
commonChiselSettings
38+
)
39+
.settings(
40+
Compile / scalaSource := baseDirectory.value / "main" / "scala",
41+
Compile / resourceDirectory := baseDirectory.value / "main" / "resources"
42+
)

build.sc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def defaultVersions = Map(
1414
"chiseltest" -> ivy"edu.berkeley.cs::chiseltest:6.0.0"
1515
)
1616

17+
def extendDependencies = Map(
18+
"hardfloat" -> ivy"edu.berkeley.cs::hardfloat:1.5-SNAPSHOT"
19+
)
20+
1721
trait HasChisel extends SbtModule {
1822
def chiselModule: Option[ScalaModule] = None
1923

@@ -34,7 +38,7 @@ trait HasChisel extends SbtModule {
3438
}
3539

3640
object fputil extends HasChisel {
37-
override def millSourcePath = os.pwd / "fputil"
41+
override def millSourcePath = os.pwd / "depencies" / "fputil"
3842
}
3943

4044
trait transformer_MMModule extends ScalaModule {
@@ -66,7 +70,8 @@ object trans extends transformer_MMModule with HasChisel { m =>
6670
// def mainClass = Some("vitiskernel.VitisRTLKernelVerilog")
6771
def mainClass = Some("kernel.NewFeatureTest")
6872
override def ivyDeps = super.ivyDeps() ++ Agg(
69-
defaultVersions("chiseltest")
73+
defaultVersions("chiseltest"),
74+
extendDependencies("hardfloat")
7075
)
7176

7277
object test extends SbtTests with TestModule.ScalaTest {

depencies/hardfloat

Submodule hardfloat added at 6c6e3f4
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package kernel.alu
2+
3+
import chisel3._
4+
import chisel3.util._
5+
import hardfloat._
6+
object FPU {
7+
def equivRecFN(expWidth: Int, sigWidth: Int, a: UInt, b: UInt) = {
8+
val top4A = a(expWidth + sigWidth, expWidth + sigWidth - 3)
9+
val top4B = b(expWidth + sigWidth, expWidth + sigWidth - 3)
10+
Mux(
11+
(top4A(2, 0) === 0.U) || (top4A(2, 0) === 7.U),
12+
(top4A === top4B) && (a(sigWidth - 2, 0) === b(sigWidth - 2, 0)),
13+
Mux((top4A(2, 0) === 6.U), (top4A === top4B), (a === b))
14+
)
15+
}
16+
}
17+
18+
class ValExec_MulRecFN(expWidth: Int, sigWidth: Int) extends Module {
19+
val io = IO(new Bundle {
20+
val a = Input(Bits((expWidth + sigWidth).W))
21+
val b = Input(Bits((expWidth + sigWidth).W))
22+
val roundingMode = Input(UInt(3.W))
23+
val detectTininess = Input(UInt(1.W))
24+
25+
val expected = new Bundle {
26+
val out = Input(Bits((expWidth + sigWidth).W))
27+
val exceptionFlags = Input(Bits(5.W))
28+
val recOut = Output(Bits((expWidth + sigWidth + 1).W))
29+
}
30+
31+
val actual = new Bundle {
32+
val out = Output(Bits((expWidth + sigWidth + 1).W))
33+
val exceptionFlags = Output(Bits(5.W))
34+
}
35+
36+
val check = Output(Bool())
37+
val pass = Output(Bool())
38+
})
39+
40+
val mulRecFN = Module(new MulRecFN(expWidth, sigWidth))
41+
mulRecFN.io.a := recFNFromFN(expWidth, sigWidth, io.a)
42+
mulRecFN.io.b := recFNFromFN(expWidth, sigWidth, io.b)
43+
mulRecFN.io.roundingMode := io.roundingMode
44+
mulRecFN.io.detectTininess := io.detectTininess
45+
46+
io.expected.recOut := recFNFromFN(expWidth, sigWidth, io.expected.out)
47+
48+
io.actual.out := mulRecFN.io.out
49+
io.actual.exceptionFlags := mulRecFN.io.exceptionFlags
50+
51+
io.check := true.B
52+
io.pass :=
53+
FPU.equivRecFN(expWidth, sigWidth, io.actual.out, io.expected.recOut) &&
54+
(io.actual.exceptionFlags === io.expected.exceptionFlags)
55+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package kernel.alu
2+
3+
import chisel3._
4+
5+
class FXP(val IntWidth: Int, val FractionalWidth: Int) extends Bundle {
6+
def signWidth = IntWidth + FractionalWidth - 1
7+
}
8+
9+
object FXP {
10+
def hex2SignedInt(hex: String, width: Int) = {
11+
val maxUnsignedValue = BigInt(2).pow(width)
12+
val maxPositiveValue = BigInt(2).pow(width - 1)
13+
val resTmp = BigInt(hex, 16)
14+
if (resTmp >= maxPositiveValue) { resTmp - maxUnsignedValue }
15+
else { resTmp }
16+
}
17+
18+
def maxUnsignedValue(IntWidth: Int, FractionalWidth: Int) = BigInt(2).pow(IntWidth + FractionalWidth)
19+
def maxPositiveValue(IntWidth: Int, FractionalWidth: Int) = BigInt(2).pow(IntWidth + FractionalWidth - 1)
20+
21+
def bits2RealValue(value: BigInt, IntWidth: Int, FractionalWidth: Int) = {
22+
val maxPositiveValue = FXP.maxPositiveValue(IntWidth, FractionalWidth)
23+
BigDecimal(if (value >= maxPositiveValue) { value - maxUnsignedValue(IntWidth, FractionalWidth) }
24+
else { value }) / (BigDecimal(2).pow(FractionalWidth))
25+
}
26+
27+
}

src/main/scala/kernel/alu/Gemm.scala

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import chisel3.util._
55
import kernel.utils.DebugLog
66
import fputil.FPMult
77
import fputil.FPAdd
8+
import hardfloat._
9+
810
trait GEMMAccuracyConfig {
911
val I: Int = 4
1012
val F: Int = 12
@@ -34,25 +36,6 @@ class PEFxp extends Module with GEMMAccuracyConfig with DebugLog {
3436
io.out := res
3537
}
3638

37-
// a * b + c
38-
class FMA(width: Int = 32) extends Module with DebugLog {
39-
val io = IO(new Bundle {
40-
val a = Input(Valid(UInt(width.W)))
41-
val b = Input(Valid(UInt(width.W)))
42-
val c = Input(Valid(UInt(width.W)))
43-
val out = Valid(UInt(width.W))
44-
})
45-
46-
// one cycle latency
47-
val tmp = FPMult(width)(io.a.bits, io.b.bits, io.a.valid && io.b.valid)
48-
49-
// three cycle latency
50-
val tmpRes = FPAdd(width)(io.c.bits, tmp.bits, io.c.valid && tmp.valid)
51-
52-
io.out.bits := Mux(tmpRes.valid, tmpRes.bits, io.c.bits)
53-
io.out.valid := tmpRes.valid
54-
}
55-
5639
class PEFp(width: Int = 32, size: Int = 4) extends Module with DebugLog {
5740
val io = IO(new Bundle {
5841
val in_h = Input(Valid(UInt(width.W)))

src/main/scala/kernel/alu/SRT16Divider.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/***************************************************************************************
2+
* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
3+
* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
4+
* Copyright (c) 2020-2021 Peng Cheng Laboratory
5+
*
6+
* XiangShan is licensed under Mulan PSL v2.
7+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
8+
* You may obtain a copy of Mulan PSL v2 at:
9+
* http://license.coscl.org.cn/MulanPSL2
10+
*
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
*
15+
* See the Mulan PSL v2 for more details.
16+
*
17+
*
18+
* Acknowledgement
19+
*
20+
* This implementation is inspired by several key papers:
21+
* [1] Elisardo Antelo, Tomas Lang, Paolo Montuschi, and Alberto Nannarelli. "[Digit-recurrence dividers with reduced
22+
* logical depth.](https://doi.org/10.1109/TC.2005.115)" IEEE Transactions on Computers 54.7: 837-851. 2005.
23+
***************************************************************************************/
124
package kernel.alu
225

326
import chisel3._

0 commit comments

Comments
 (0)