xref: /XiangShan/src/main/scala/xiangshan/backend/fu/SRT4Divider.scala (revision 35ee668dfaa9d1512c3dfbcc845d4c6a4c4ec9be)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3*
4* XiangShan is licensed under Mulan PSL v2.
5* You can use this software according to the terms and conditions of the Mulan PSL v2.
6* You may obtain a copy of Mulan PSL v2 at:
7*          http://license.coscl.org.cn/MulanPSL2
8*
9* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
10* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
11* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
12*
13* See the Mulan PSL v2 for more details.
14***************************************************************************************/
15
16package xiangshan.backend.fu
17
18import chipsalliance.rocketchip.config.Parameters
19import chisel3._
20import chisel3.util._
21import utils.SignExt
22import xiangshan.backend.fu.util.CSA3_2
23
24/** A Radix-4 SRT Integer Divider
25  *
26  * 2 ~ (5 + (len+3)/2) cycles are needed for each division.
27  */
28class SRT4DividerDataModule(len: Int) extends Module {
29  val io = IO(new Bundle() {
30    val src = Vec(2, Input(UInt(len.W)))
31    val valid, sign, kill_w, kill_r, isHi, isW = Input(Bool())
32    val in_ready = Output(Bool())
33    val out_valid = Output(Bool())
34    val out_data = Output(UInt(len.W))
35    val out_ready = Input(Bool())
36  })
37
38  val (a, b, sign, valid, kill_w, kill_r, isHi, isW) =
39    (io.src(0), io.src(1), io.sign, io.valid, io.kill_w, io.kill_r, io.isHi, io.isW)
40  val in_fire = valid && io.in_ready
41  val out_fire = io.out_ready && io.out_valid
42
43  // s_pad_* is not used
44  val s_idle :: s_lzd :: s_normlize :: s_recurrence :: s_recovery_1 :: s_recovery_2 :: s_pad_1 :: s_pad_2 :: s_finish :: Nil = Enum(9)
45  require(s_finish.litValue() == 8)
46
47  val state = RegInit(s_idle)
48  val finished = state(3).asBool // state === s_finish
49
50  val cnt_next = Wire(UInt(log2Up((len + 3) / 2).W))
51  val cnt = RegEnable(cnt_next, state === s_normlize || state === s_recurrence)
52  val rec_enough = cnt_next === 0.U
53  val newReq = in_fire
54
55  def abs(a: UInt, sign: Bool): (Bool, UInt) = {
56    val s = a(len - 1) && sign
57    (s, Mux(s, -a, a))
58  }
59
60  val (aSign, aVal) = abs(a, sign)
61  val (bSign, bVal) = abs(b, sign)
62  val aSignReg = RegEnable(aSign, newReq)
63  val qSignReg = RegEnable(aSign ^ bSign, newReq)
64  val divZero = b === 0.U
65  val divZeroReg = RegEnable(divZero, newReq)
66
67  switch(state) {
68    is(s_idle) {
69      when(in_fire && !kill_w) {
70        state := Mux(divZero, s_finish, s_lzd)
71      }
72    }
73    is(s_lzd) { // leading zero detection
74      state := s_normlize
75    }
76    is(s_normlize) { // shift a/b
77      state := s_recurrence
78    }
79    is(s_recurrence) { // (ws[j+1], wc[j+1]) = 4(ws[j],wc[j]) - q(j+1)*d
80      when(rec_enough) {
81        state := s_recovery_1
82      }
83    }
84    is(s_recovery_1) { // if rem < 0, rem = rem + d
85      state := s_recovery_2
86    }
87    is(s_recovery_2) { // recovery shift
88      state := s_finish
89    }
90    is(s_finish) {
91      when(out_fire) {
92        state := s_idle
93      }
94    }
95  }
96  when(kill_r) {
97    state := s_idle
98  }
99
100  /** Calculate abs(a)/abs(b) by recurrence
101    *
102    * ws, wc: partial remainder in carry-save form,
103    * in recurrence steps, ws/wc = 4ws[j]/4wc[j];
104    * in recovery step, ws/wc = ws[j]/wc[j];
105    * in final step, ws = abs(a)/abs(b).
106    *
107    * d: normlized divisor(1/2<=d<1)
108    *
109    * wLen = 3 integer bits + (len+1) frac bits
110    */
111  def wLen = 3 + len + 1
112
113  val ws, wc = Reg(UInt(wLen.W))
114  val ws_next, wc_next = Wire(UInt(wLen.W))
115  val d = Reg(UInt(wLen.W))
116
117  val aLeadingZeros = RegEnable(
118    next = PriorityEncoder(ws(len - 1, 0).asBools().reverse),
119    enable = state === s_lzd
120  )
121  val bLeadingZeros = RegEnable(
122    next = PriorityEncoder(d(len - 1, 0).asBools().reverse),
123    enable = state === s_lzd
124  )
125  val diff = Cat(0.U(1.W), bLeadingZeros).asSInt() - Cat(0.U(1.W), aLeadingZeros).asSInt()
126  val isNegDiff = diff(diff.getWidth - 1)
127  val quotientBits = Mux(isNegDiff, 0.U, diff.asUInt())
128  val qBitsIsOdd = quotientBits(0)
129  val recoveryShift = RegEnable(len.U - bLeadingZeros, state === s_normlize)
130  val a_shifted, b_shifted = Wire(UInt(len.W))
131  a_shifted := Mux(isNegDiff,
132    ws(len - 1, 0) << bLeadingZeros,
133    ws(len - 1, 0) << aLeadingZeros
134  )
135  b_shifted := d(len - 1, 0) << bLeadingZeros
136
137  val rem_temp = ws + wc
138  val rem_fixed = RegEnable(Mux(rem_temp(wLen - 1), rem_temp + d, rem_temp), state === s_recovery_1)
139  val rem_abs = RegEnable((rem_fixed << recoveryShift) (2 * len, len + 1), state === s_recovery_2)
140
141  when(newReq) {
142    ws := Cat(0.U(4.W), Mux(divZero, a, aVal))
143    wc := 0.U
144    d := Cat(0.U(4.W), bVal)
145  }.elsewhen(state === s_normlize) {
146    d := Cat(0.U(3.W), b_shifted, 0.U(1.W))
147    ws := Mux(qBitsIsOdd, a_shifted, a_shifted << 1)
148  }.elsewhen(state === s_recurrence) {
149    ws := Mux(rec_enough, ws_next, ws_next << 2)
150    wc := Mux(rec_enough, wc_next, wc_next << 2)
151  }
152
153  cnt_next := Mux(state === s_normlize, (quotientBits + 3.U) >> 1, cnt - 1.U)
154
155  /** Quotient selection
156    *
157    * the quotient selection table use truncated 7-bit remainder
158    * and 3-bit divisor
159    */
160  val sel_0 :: sel_d :: sel_dx2 :: sel_neg_d :: sel_neg_dx2 :: Nil = Enum(5)
161  val dx2, neg_d, neg_dx2 = Wire(UInt(wLen.W))
162  dx2 := d << 1
163  neg_d := (~d).asUInt() // add '1' in carry-save adder later
164  neg_dx2 := neg_d << 1
165
166  val q_sel = Wire(UInt(3.W))
167  val wc_adj = MuxLookup(q_sel, 0.U(2.W), Seq(
168    sel_d -> 1.U(2.W),
169    sel_dx2 -> 2.U(2.W)
170  ))
171
172  val w_truncated = (ws(wLen - 1, wLen - 1 - 6) + wc(wLen - 1, wLen - 1 - 6)).asSInt()
173  val d_truncated = b_shifted.tail(1).head(3)
174
175  val qSelTable = Array(
176    Array(12, 4, -4, -13),
177    Array(14, 4, -6, -15),
178    Array(15, 4, -6, -16),
179    Array(16, 4, -6, -18),
180    Array(18, 6, -8, -20),
181    Array(20, 6, -8, -20),
182    Array(20, 8, -8, -22),
183    Array(24, 8, -8, -24)
184  )
185
186  val table = RegEnable(
187    VecInit(qSelTable.map(row =>
188      VecInit(row.map(k => k.S(7.W)))
189    ))(d_truncated),
190    state === s_normlize
191  )
192
193  q_sel := MuxCase(sel_neg_dx2,
194    table.zip(Seq(sel_dx2, sel_d, sel_0, sel_neg_d)).map {
195      case (k, s) => (w_truncated >= k) -> s
196    }
197  )
198
199  /** Calculate (ws[j+1],wc[j+1]) by a [3-2]carry-save adder
200    *
201    * (ws[j+1], wc[j+1]) = 4(ws[j],wc[j]) - q(j+1)*d
202    */
203  val csa = Module(new CSA3_2(wLen))
204  csa.io.in(0) := ws
205  csa.io.in(1) := Cat(wc(wLen - 1, 2), wc_adj)
206  csa.io.in(2) := MuxLookup(q_sel, 0.U, Seq(
207    sel_d -> neg_d,
208    sel_dx2 -> neg_dx2,
209    sel_neg_d -> d,
210    sel_neg_dx2 -> dx2
211  ))
212  ws_next := csa.io.out(0)
213  wc_next := csa.io.out(1) << 1
214
215  // On the fly quotient conversion
216  val q, qm = Reg(UInt(len.W))
217  when(newReq) {
218    q := 0.U
219    qm := 0.U
220  }.elsewhen(state === s_recurrence) {
221    val qMap = Seq(
222      sel_0 -> (q, 0),
223      sel_d -> (q, 1),
224      sel_dx2 -> (q, 2),
225      sel_neg_d -> (qm, 3),
226      sel_neg_dx2 -> (qm, 2)
227    )
228    q := MuxLookup(q_sel, 0.U,
229      qMap.map(m => m._1 -> Cat(m._2._1(len - 3, 0), m._2._2.U(2.W)))
230    )
231    val qmMap = Seq(
232      sel_0 -> (qm, 3),
233      sel_d -> (q, 0),
234      sel_dx2 -> (q, 1),
235      sel_neg_d -> (qm, 2),
236      sel_neg_dx2 -> (qm, 1)
237    )
238    qm := MuxLookup(q_sel, 0.U,
239      qmMap.map(m => m._1 -> Cat(m._2._1(len - 3, 0), m._2._2.U(2.W)))
240    )
241  }.elsewhen(state === s_recovery_1) {
242    q := Mux(rem_temp(wLen - 1), qm, q)
243  }
244
245
246  val remainder = Mux(aSignReg, -rem_abs(len - 1, 0), rem_abs(len - 1, 0))
247  val quotient = Mux(qSignReg, -q, q)
248
249  val res = Mux(isHi,
250    Mux(divZeroReg, ws(len - 1, 0), remainder),
251    Mux(divZeroReg, Fill(len, 1.U(1.W)), quotient)
252  )
253  io.out_data := Mux(isW,
254    SignExt(res(31, 0), len),
255    res
256  )
257  io.in_ready := state === s_idle
258  io.out_valid := finished // state === s_finish
259}
260
261class SRT4Divider(len: Int)(implicit p: Parameters) extends AbstractDivider(len) {
262
263  val newReq = io.in.fire()
264
265  val uop = io.in.bits.uop
266  val uopReg = RegEnable(uop, newReq)
267  val ctrlReg = RegEnable(ctrl, newReq)
268
269  val divDataModule = Module(new SRT4DividerDataModule(len))
270
271  val kill_w = uop.roqIdx.needFlush(io.redirectIn, io.flushIn)
272  val kill_r = !divDataModule.io.in_ready && uopReg.roqIdx.needFlush(io.redirectIn, io.flushIn)
273
274  divDataModule.io.src(0) := io.in.bits.src(0)
275  divDataModule.io.src(1) := io.in.bits.src(1)
276  divDataModule.io.valid := io.in.valid
277  divDataModule.io.sign := sign
278  divDataModule.io.kill_w := kill_w
279  divDataModule.io.kill_r := kill_r
280  divDataModule.io.isHi := ctrlReg.isHi
281  divDataModule.io.isW := ctrlReg.isW
282  divDataModule.io.out_ready := io.out.ready
283
284  io.in.ready := divDataModule.io.in_ready
285  io.out.valid := divDataModule.io.out_valid
286  io.out.bits.data := divDataModule.io.out_data
287  io.out.bits.uop := uopReg
288}
289