forked from bespoke-silicon-group/basejump_stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsg_fpu_mul.sv
389 lines (348 loc) · 10.3 KB
/
bsg_fpu_mul.sv
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* bsg_fpu_mul.sv
*
* parameterized floating-point multiplier.
*
* @author Tommy Jung
*/
`include "bsg_defines.sv"
`include "bsg_fpu_defines.svh"
module bsg_fpu_mul
#(parameter `BSG_INV_PARAM(e_p) // exponent width
, parameter `BSG_INV_PARAM(m_p) // mantissa width
)
(
input clk_i
, input reset_i
, input en_i
, input v_i
, input [e_p+m_p:0] a_i
, input [e_p+m_p:0] b_i
, output logic ready_and_o
, output logic v_o
, output logic [e_p+m_p:0] z_o
, output logic unimplemented_o
, output logic invalid_o
, output logic overflow_o
, output logic underflow_o
, input yumi_i // when yumi_i is high, en_i also has to be high
);
// pipeline states / signals
logic v_1_r, v_2_r, v_3_r;
logic stall;
assign stall = v_3_r & ~yumi_i;
assign v_o = v_3_r;
assign ready_and_o = ~stall & en_i;
// preprocessors
logic a_zero, a_nan, a_sig_nan, a_infty, exp_a_zero, man_a_zero,
a_denormal, sign_a;
logic b_zero, b_nan, b_sig_nan, b_infty, exp_b_zero, man_b_zero,
b_denormal, sign_b;
logic [e_p-1:0] exp_a, exp_b;
logic [m_p-1:0] man_a, man_b;
bsg_fpu_preprocess #(
.e_p(e_p)
,.m_p(m_p)
) a_preprocess (
.a_i(a_i)
,.zero_o(a_zero)
,.nan_o(a_nan)
,.sig_nan_o(a_sig_nan)
,.infty_o(a_infty)
,.exp_zero_o(exp_a_zero)
,.man_zero_o(man_a_zero)
,.denormal_o(a_denormal)
,.sign_o(sign_a)
,.exp_o(exp_a)
,.man_o(man_a)
);
bsg_fpu_preprocess #(
.e_p(e_p)
,.m_p(m_p)
) b_preprocess (
.a_i(b_i)
,.zero_o(b_zero)
,.nan_o(b_nan)
,.sig_nan_o(b_sig_nan)
,.infty_o(b_infty)
,.exp_zero_o(exp_b_zero)
,.man_zero_o(man_b_zero)
,.denormal_o(b_denormal)
,.sign_o(sign_b)
,.exp_o(exp_b)
,.man_o(man_b)
);
// final sign
logic final_sign;
assign final_sign = sign_a ^ sign_b;
// add exponents together
logic [e_p:0] exp_sum;
assign exp_sum = {1'b0, exp_a} + {1'b0, exp_b} + 9'b1;
// sum of exp with bias removed
logic [e_p-1:0] exp_sum_unbiased;
assign exp_sum_unbiased = {~exp_sum[e_p-1], exp_sum[e_p-2:0]};
// normalized mantissa
logic [m_p:0] man_a_norm, man_b_norm;
assign man_a_norm = {1'b1, man_a};
assign man_b_norm = {1'b1, man_b};
/////////////// first pipeline stage ///////////////////////////////
//
logic final_sign_1_r;
logic [e_p-1:0] exp_sum_unbiased_1_r;
logic a_sig_nan_1_r, b_sig_nan_1_r;
logic a_nan_1_r, b_nan_1_r;
logic a_infty_1_r, b_infty_1_r;
logic a_zero_1_r, b_zero_1_r;
logic a_denormal_1_r, b_denormal_1_r;
logic [e_p:0] exp_sum_1_r;
logic [m_p:0] man_a_norm_r, man_b_norm_r;
always_ff @ (posedge clk_i) begin
if (reset_i) begin
v_1_r <= 1'b0;
end
else begin
if (~stall & en_i) begin
v_1_r <= v_i;
if (v_i) begin
final_sign_1_r <= final_sign;
exp_sum_unbiased_1_r <= exp_sum_unbiased;
a_sig_nan_1_r <= a_sig_nan;
b_sig_nan_1_r <= b_sig_nan;
a_nan_1_r <= a_nan;
b_nan_1_r <= b_nan;
a_infty_1_r <= a_infty;
b_infty_1_r <= b_infty;
a_zero_1_r <= a_zero;
b_zero_1_r <= b_zero;
a_denormal_1_r <= a_denormal;
b_denormal_1_r <= b_denormal;
exp_sum_1_r <= exp_sum;
man_a_norm_r <= man_a_norm;
man_b_norm_r <= man_b_norm;
end
end
end
end
//////////// second pipeline stage ///////////////////////////////
// for single precision: 24-bit multiplier
logic [((m_p+1)*2)-1:0] man_prod;
bsg_mul_synth #(
.width_p(m_p+1)
) mul_synth (
.a_i(man_a_norm_r)
,.b_i(man_b_norm_r)
,.o(man_prod)
);
logic [((m_p+1)*2)-1:0] man_prod_2_r;
logic [e_p-1:0] exp_sum_unbiased_2_r;
logic a_sig_nan_2_r, b_sig_nan_2_r;
logic a_nan_2_r, b_nan_2_r;
logic a_infty_2_r, b_infty_2_r;
logic a_zero_2_r, b_zero_2_r;
logic a_denormal_2_r, b_denormal_2_r;
logic [e_p:0] exp_sum_2_r;
logic final_sign_2_r;
always_ff @ (posedge clk_i) begin
if (reset_i) begin
v_2_r <= 1'b0;
end
else begin
if (~stall & en_i) begin
v_2_r <= v_1_r;
if (v_1_r) begin
man_prod_2_r <= man_prod;
exp_sum_unbiased_2_r <= exp_sum_unbiased_1_r;
a_sig_nan_2_r <= a_sig_nan_1_r;
b_sig_nan_2_r <= b_sig_nan_1_r;
a_nan_2_r <= a_nan_1_r;
b_nan_2_r <= b_nan_1_r;
a_infty_2_r <= a_infty_1_r;
b_infty_2_r <= b_infty_1_r;
a_zero_2_r <= a_zero_1_r;
b_zero_2_r <= b_zero_1_r;
a_denormal_2_r <= a_denormal_1_r;
b_denormal_2_r <= b_denormal_1_r;
exp_sum_2_r <= exp_sum_1_r;
final_sign_2_r <= final_sign_1_r;
end
end
end
end
// lowers bits
logic sticky, round, guard;
assign sticky = |man_prod_2_r[m_p-2:0];
assign round = man_prod_2_r[m_p-1];
assign guard = man_prod_2_r[m_p];
// round condition
logic round_up;
assign round_up = sticky
? (man_prod_2_r[(2*(m_p+1))-1] ? guard : round)
: (guard & (round | (man_prod_2_r[(2*(m_p+1))-1] & man_prod_2_r[m_p+1])));
// exp with additional carry bit from the product of mantissa added.
logic [e_p:0] final_exp;
assign final_exp = {1'b0, exp_sum_unbiased_2_r} + man_prod_2_r[(2*(m_p+1))-1];
// mantissa also needs to be shifted if the product is larger than 2.
logic [m_p-1:0] shifted_mantissa;
assign shifted_mantissa = man_prod_2_r[(2*(m_p+1))-1]
? man_prod_2_r[(m_p+1)+:m_p]
: man_prod_2_r[m_p+:m_p];
// pre_roundup;
logic [e_p+m_p-1:0] pre_roundup;
assign pre_roundup = {final_exp[e_p-1:0], shifted_mantissa};
//////////// third pipeline stage ///////////////////////////////
logic [e_p+m_p-1:0] pre_roundup_3_r;
logic round_up_3_r;
logic final_sign_3_r;
logic a_sig_nan_3_r, b_sig_nan_3_r;
logic a_nan_3_r, b_nan_3_r;
logic a_infty_3_r, b_infty_3_r;
logic a_zero_3_r, b_zero_3_r;
logic a_denormal_3_r, b_denormal_3_r;
logic [e_p:0] exp_sum_3_r;
logic [e_p:0] final_exp_3_r;
always_ff @ (posedge clk_i) begin
if (reset_i) begin
v_3_r <= 1'b0;
end
else begin
if (~stall & en_i) begin
v_3_r <= v_2_r;
if (v_2_r) begin
pre_roundup_3_r <= pre_roundup;
round_up_3_r <= round_up;
final_sign_3_r <= final_sign_2_r;
a_sig_nan_3_r <= a_sig_nan_2_r;
b_sig_nan_3_r <= b_sig_nan_2_r;
a_nan_3_r <= a_nan_2_r;
b_nan_3_r <= b_nan_2_r ;
a_infty_3_r <= a_infty_2_r;
b_infty_3_r <= b_infty_2_r;
a_zero_3_r <= a_zero_2_r;
b_zero_3_r <= b_zero_2_r;
a_denormal_3_r <= a_denormal_2_r;
b_denormal_3_r <= b_denormal_2_r;
exp_sum_3_r <= exp_sum_2_r;
final_exp_3_r <= final_exp;
end
end
end
end
// carry going into exp when rounding up
// (important for distinguishing between overflow and underflow)
logic carry_into_exp;
assign carry_into_exp = &{round_up_3_r, pre_roundup_3_r[m_p-1:0]};
// round up for the final result.
logic round_overflow;
logic [e_p+m_p-1:0] rounded;
assign {round_overflow, rounded} = pre_roundup_3_r + round_up_3_r;
// final output
logic sgn;
always_comb begin
sgn = final_sign_3_r;
if (a_sig_nan_3_r | b_sig_nan_3_r) begin
unimplemented_o = 1'b0;
invalid_o = 1'b1;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_SIGNAN(e_p,m_p); // sig nan
end
else if (a_nan_3_r | b_nan_3_r) begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_QUIETNAN(e_p,m_p); // quiet nan
end
else if (a_infty_3_r) begin
if (b_zero_3_r) begin
unimplemented_o = 1'b0;
invalid_o = 1'b1;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_QUIETNAN(e_p,m_p); // quiet nan
end
else begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_INFTY(sgn,e_p,m_p); // infty
end
end
else if (b_infty_3_r) begin
if (a_zero_3_r) begin
unimplemented_o = 1'b0;
invalid_o = 1'b1;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_QUIETNAN(e_p,m_p); // quiet nan
end
else begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_INFTY(sgn,e_p,m_p); // infty
end
end
else if (a_zero_3_r | b_zero_3_r) begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_ZERO(sgn,e_p,m_p); // zero
end
else if (a_denormal_3_r & b_denormal_3_r) begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b1;
z_o = `BSG_FPU_ZERO(sgn,e_p,m_p); // zero
end
else if (a_denormal_3_r | b_denormal_3_r) begin
unimplemented_o = 1'b1;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = `BSG_FPU_QUIETNAN(e_p,m_p); // quiet nan
end
else if (exp_sum_3_r[(e_p-1)+:2] == 2'b0) begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b1;
z_o = `BSG_FPU_ZERO(sgn,e_p,m_p); // zero
end
else if (exp_sum_3_r[(e_p-1)+:2] == 2'b11 | final_exp_3_r[e_p]) begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b1;
underflow_o = 1'b0;
z_o = `BSG_FPU_INFTY(sgn,e_p,m_p); // infty
end
else begin
if (pre_roundup_3_r[m_p+:e_p] == {e_p{1'b1}} & (pre_roundup_3_r[m_p] | carry_into_exp)) begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b1;
underflow_o = 1'b0;
z_o = `BSG_FPU_INFTY(sgn,e_p,m_p); // infty
end
else if (pre_roundup_3_r[m_p+:e_p] == {e_p{1'b0}} & ~carry_into_exp) begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b1;
z_o = `BSG_FPU_ZERO(sgn,e_p,m_p); // zero
end
else begin
unimplemented_o = 1'b0;
invalid_o = 1'b0;
overflow_o = 1'b0;
underflow_o = 1'b0;
z_o = {sgn, rounded}; // happy case
end
end
end
endmodule
`BSG_ABSTRACT_MODULE(bsg_fpu_mul)