OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpFMA.v] - Blame information for rev 29

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      fpFMA.v
9
//              - floating point fused multiplier + adder
10
//              - can issue every clock cycle
11
//              - parameterized FPWIDth
12
//              - IEEE 754 representation
13
//
14
//
15
// This source file is free software: you can redistribute it and/or modify 
16
// it under the terms of the GNU Lesser General Public License as published 
17
// by the Free Software Foundation, either version 3 of the License, or     
18
// (at your option) any later version.                                      
19
//                                                                          
20
// This source file is distributed in the hope that it will be useful,      
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
23
// GNU General Public License for more details.                             
24
//                                                                          
25
// You should have received a copy of the GNU General Public License        
26
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
27
//                                                                          
28
// ============================================================================
29
 
30
`include "fpConfig.sv"
31
 
32
module fpFMA (clk, ce, op, rm, a, b, c, o, under, over, inf, zero);
33
parameter FPWID = 32;
34
`include "fpSize.sv"
35
 
36
input clk;
37
input ce;
38
input op;               // operation 0 = add, 1 = subtract
39
input [2:0] rm;
40
input  [MSB:0] a, b, c;
41
output [EX:0] o;
42
output under;
43
output over;
44
output inf;
45
output zero;
46
 
47
// constants
48
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
49
// The following is the value for an exponent of zero, with the offset
50
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
51
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
52
// The following is a template for a quiet nan. (MSB=1)
53
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
54
 
55
// -----------------------------------------------------------
56
// Clock #1
57
// - decode the input operands
58
// - derive basic information
59
// -----------------------------------------------------------
60
 
61
wire sa1, sb1, sc1;                     // sign bit
62
wire [EMSB:0] xa1, xb1, xc1;     // exponent bits
63
wire [FMSB+1:0] fracta1, fractb1, fractc1;       // includes unhidden bit
64
wire a_dn1, b_dn1, c_dn1;                       // a/b is denormalized
65
wire aNan1, bNan1, cNan1;
66
wire az1, bz1, cz1;
67
wire aInf1, bInf1, cInf1;
68
reg op1;
69
 
70
fpDecompReg #(FPWID) u1a (.clk(clk), .ce(ce), .i(a), .sgn(sa1), .exp(xa1), .fract(fracta1), .xz(a_dn1), .vz(az1), .inf(aInf1), .nan(aNan1) );
71
fpDecompReg #(FPWID) u1b (.clk(clk), .ce(ce), .i(b), .sgn(sb1), .exp(xb1), .fract(fractb1), .xz(b_dn1), .vz(bz1), .inf(bInf1), .nan(bNan1) );
72
fpDecompReg #(FPWID) u1c (.clk(clk), .ce(ce), .i(c), .sgn(sc1), .exp(xc1), .fract(fractc1), .xz(c_dn1), .vz(cz1), .inf(cInf1), .nan(cNan1) );
73
 
74
always @(posedge clk)
75
        if (ce) op1 <= op;
76
 
77
// -----------------------------------------------------------
78
// Clock #2
79
// Compute the sum of the exponents.
80
// correct the exponent for denormalized operands
81
// adjust the sum by the exponent offset (subtract 127)
82
// mul: ex1 = xa + xb,  result should always be < 1ffh
83
// Form partial products (clocks 2 to 5)
84
// -----------------------------------------------------------
85
 
86
reg abz2;
87
reg [EMSB+2:0] ex2;
88
reg [EMSB:0] xc2;
89
reg realOp2;
90
reg xcInf2;
91
 
92
always @(posedge clk)
93
        if (ce) abz2 <= az1|bz1;
94
always @(posedge clk)
95
        if (ce) ex2 <= (xa1|a_dn1) + (xb1|b_dn1) - bias;
96
always @(posedge clk)
97
        if (ce) xc2 <= (xc1|c_dn1);
98
always @(posedge clk)
99
        if (ce) xcInf2 = &xc1;
100
 
101
// Figure out which operation is really needed an add or
102
// subtract ?
103
// If the signs are the same, use the orignal op,
104
// otherwise flip the operation
105
//  a +  b = add,+
106
//  a + -b = sub, so of larger
107
// -a +  b = sub, so of larger
108
// -a + -b = add,-
109
//  a -  b = sub, so of larger
110
//  a - -b = add,+
111
// -a -  b = add,-
112
// -a - -b = sub, so of larger
113
always @(posedge clk)
114
        if (ce) realOp2 <= op1 ^ (sa1 ^ sb1) ^ sc1;
115
 
116
 
117
reg [FX:0] fract5;
118
generate
119
if (FPWID+`EXTRA_BITS==84) begin
120
reg [33:0] p00,p01,p02,p03;
121
reg [33:0] p10,p11,p12,p13;
122
reg [33:0] p20,p21,p22,p23;
123
reg [33:0] p30,p31,p32,p33;
124
reg [135:0] fract3a;
125
reg [135:0] fract3b;
126
reg [135:0] fract3c;
127
reg [135:0] fract3d;
128
reg [135:0] fract4a;
129
reg [135:0] fract4b;
130
 
131
        always @(posedge clk)
132
        if (ce) begin
133
                p00 <= fracta1[16: 0] * fractb1[16: 0];
134
                p01 <= fracta1[33:17] * fractb1[16: 0];
135
                p02 <= fracta1[50:34] * fractb1[16: 0];
136
                p03 <= fracta1[67:51] * fractb1[16: 0];
137
 
138
                p10 <= fracta1[16: 0] * fractb1[33:17];
139
                p11 <= fracta1[33:17] * fractb1[33:17];
140
                p12 <= fracta1[50:34] * fractb1[33:17];
141
                p13 <= fracta1[67:51] * fractb1[33:17];
142
 
143
                p20 <= fracta1[16: 0] * fractb1[50:34];
144
                p21 <= fracta1[33:17] * fractb1[50:34];
145
                p22 <= fracta1[50:34] * fractb1[50:34];
146
                p23 <= fracta1[67:51] * fractb1[50:34];
147
 
148
                p30 <= fracta1[15: 0] * fractb1[67:51];
149
                p31 <= fracta1[31:16] * fractb1[67:51];
150
                p32 <= fracta1[47:32] * fractb1[67:51];
151
                p33 <= fracta1[63:48] * fractb1[67:51];
152
        end
153
        always @(posedge clk)
154
        if (ce) begin
155
                fract3a <= {p33,p31,p20,p00};
156
                fract3b <= {p32,p12,p10,17'b0} + {p23,p03,p01,17'b0};
157
                fract3c <= {p22,p11,34'b0} + {p13,p02,34'b0};
158
                fract3d <= {p12,51'b0} + {p03,51'b0};
159
        end
160
        always @(posedge clk)
161
        if (ce) begin
162
                fract4a <= fract3a + fract3b;
163
                fract4b <= fract3c + fract3d;
164
        end
165
        always @(posedge clk)
166
        if (ce) begin
167
                fract5 <= fract4a + fract4b;
168
        end
169
end
170
else if (FPWID+`EXTRA_BITS==80) begin
171
reg [31:0] p00,p01,p02,p03;
172
reg [31:0] p10,p11,p12,p13;
173
reg [31:0] p20,p21,p22,p23;
174
reg [31:0] p30,p31,p32,p33;
175
reg [127:0] fract3a;
176
reg [127:0] fract3b;
177
reg [127:0] fract3c;
178
reg [127:0] fract3d;
179
reg [127:0] fract4a;
180
reg [127:0] fract4b;
181
 
182
        always @(posedge clk)
183
        if (ce) begin
184
                p00 <= fracta1[15: 0] * fractb1[15: 0];
185
                p01 <= fracta1[31:16] * fractb1[15: 0];
186
                p02 <= fracta1[47:32] * fractb1[15: 0];
187
                p03 <= fracta1[63:48] * fractb1[15: 0];
188
 
189
                p10 <= fracta1[15: 0] * fractb1[31:16];
190
                p11 <= fracta1[31:16] * fractb1[31:16];
191
                p12 <= fracta1[47:32] * fractb1[31:16];
192
                p13 <= fracta1[63:48] * fractb1[31:16];
193
 
194
                p20 <= fracta1[15: 0] * fractb1[47:32];
195
                p21 <= fracta1[31:16] * fractb1[47:32];
196
                p22 <= fracta1[47:32] * fractb1[47:32];
197
                p23 <= fracta1[63:48] * fractb1[47:32];
198
 
199
                p30 <= fracta1[15: 0] * fractb1[63:48];
200
                p31 <= fracta1[31:16] * fractb1[63:48];
201
                p32 <= fracta1[47:32] * fractb1[63:48];
202
                p33 <= fracta1[63:48] * fractb1[63:48];
203
        end
204
        always @(posedge clk)
205
        if (ce) begin
206
                fract3a <= {p33,p31,p20,p00};
207
                fract3b <= {p32,p12,p10,16'b0} + {p23,p03,p01,16'b0};
208
                fract3c <= {p22,p11,32'b0} + {p13,p02,32'b0};
209
                fract3d <= {p12,48'b0} + {p03,48'b0};
210
        end
211
        always @(posedge clk)
212
        if (ce) begin
213
                fract4a <= fract3a + fract3b;
214
                fract4b <= fract3c + fract3d;
215
        end
216
        always @(posedge clk)
217
        if (ce) begin
218
                fract5 <= fract4a + fract4b;
219
        end
220
end
221
else if (FPWID+`EXTRA_BITS==64) begin
222
reg [35:0] p00,p01,p02;
223
reg [35:0] p10,p11,p12;
224
reg [35:0] p20,p21,p22;
225
reg [71:0] fract3a;
226
reg [89:0] fract3b;
227
reg [107:0] fract3c;
228
reg [108:0] fract4a;
229
reg [108:0] fract4b;
230
 
231
        always @(posedge clk)
232
        if (ce) begin
233
                p00 <= fracta1[17: 0] * fractb1[17: 0];
234
                p01 <= fracta1[35:18] * fractb1[17: 0];
235
                p02 <= fracta1[52:36] * fractb1[17: 0];
236
                p10 <= fracta1[17: 0] * fractb1[35:18];
237
                p11 <= fracta1[35:18] * fractb1[35:18];
238
                p12 <= fracta1[52:36] * fractb1[35:18];
239
                p20 <= fracta1[17: 0] * fractb1[52:36];
240
                p21 <= fracta1[35:18] * fractb1[52:36];
241
                p22 <= fracta1[52:36] * fractb1[52:36];
242
        end
243
        always @(posedge clk)
244
        if (ce) begin
245
                fract3a <= {p02,p00};
246
                fract3b <= {p21,p10,18'b0} + {p12,p01,18'b0};
247
                fract3c <= {p22,p20,36'b0} + {p11,36'b0};
248
        end
249
        always @(posedge clk)
250
        if (ce) begin
251
                fract4a <= fract3a + fract3b;
252
                fract4b <= fract3c;
253
        end
254
        always @(posedge clk)
255
        if (ce) begin
256
                fract5 <= fract4a + fract4b;
257
        end
258
end
259
else if (FPWID+`EXTRA_BITS==40) begin
260
reg [27:0] p00,p01,p02;
261
reg [27:0] p10,p11,p12;
262
reg [27:0] p20,p21,p22;
263
reg [79:0] fract3a;
264
reg [79:0] fract3b;
265
reg [79:0] fract3c;
266
reg [79:0] fract4a;
267
reg [79:0] fract4b;
268
        always @(posedge clk)
269
        if (ce) begin
270
                p00 <= fracta1[13: 0] * fractb1[13: 0];
271
                p01 <= fracta1[27:14] * fractb1[13: 0];
272
                p02 <= fracta1[39:28] * fractb1[13: 0];
273
                p10 <= fracta1[13: 0] * fractb1[27:14];
274
                p11 <= fracta1[27:14] * fractb1[27:14];
275
                p12 <= fracta1[39:28] * fractb1[27:14];
276
                p20 <= fracta1[13: 0] * fractb1[39:28];
277
                p21 <= fracta1[27:14] * fractb1[39:28];
278
                p22 <= fracta1[39:28] * fractb1[39:28];
279
        end
280
        always @(posedge clk)
281
        if (ce) begin
282
                fract3a <= {p02,p00};
283
                fract3b <= {p21,p10,18'b0} + {p12,p01,18'b0};
284
                fract3c <= {p22,p20,36'b0} + {p11,36'b0};
285
        end
286
        always @(posedge clk)
287
        if (ce) begin
288
                fract4a <= fract3a + fract3b;
289
                fract4b <= fract3c;
290
        end
291
        always @(posedge clk)
292
        if (ce) begin
293
                fract5 <= fract4a + fract4b;
294
        end
295
end
296
else if (FPWID+`EXTRA_BITS==32) begin
297
reg [23:0] p00,p01,p02;
298
reg [23:0] p10,p11,p12;
299
reg [23:0] p20,p21,p22;
300
reg [63:0] fract3a;
301
reg [63:0] fract3b;
302
reg [63:0] fract4;
303
 
304
        always @(posedge clk)
305
        if (ce) begin
306
                p00 <= fracta1[11: 0] * fractb1[11: 0];
307
                p01 <= fracta1[23:12] * fractb1[11: 0];
308
                p10 <= fracta1[11: 0] * fractb1[23:12];
309
                p11 <= fracta1[23:12] * fractb1[23:12];
310
        end
311
        always @(posedge clk)
312
        if (ce) begin
313
                fract3a <= {p11,p00};
314
                fract3b <= {p01,12'b0} + {p10,12'b0};
315
        end
316
        always @(posedge clk)
317
        if (ce) begin
318
                fract4 <= fract3a + fract3b;
319
        end
320
        always @(posedge clk)
321
        if (ce) begin
322
                fract5 <= fract4;
323
        end
324
end
325
else begin
326
reg [FX:0] p00;
327
reg [FX:0] fract3;
328
reg [FX:0] fract4;
329
        always @(posedge clk)
330
    if (ce) begin
331
        p00 <= fracta1 * fractb1;
332
    end
333
        always @(posedge clk)
334
    if (ce)
335
        fract3 <= p00;
336
        always @(posedge clk)
337
    if (ce)
338
        fract4 <= fract3;
339
        always @(posedge clk)
340
    if (ce)
341
        fract5 <= fract4;
342
end
343
endgenerate
344
 
345
// -----------------------------------------------------------
346
// Clock #3
347
// Select zero exponent
348
// -----------------------------------------------------------
349
 
350
reg [EMSB+2:0] ex3;
351
reg [EMSB:0] xc3;
352
always @(posedge clk)
353
        if (ce) ex3 <= abz2 ? 1'd0 : ex2;
354
always @(posedge clk)
355
        if (ce) xc3 <= xc2;
356
 
357
// -----------------------------------------------------------
358
// Clock #4
359
// Generate partial products.
360
// -----------------------------------------------------------
361
 
362
reg [EMSB+2:0] ex4;
363
reg [EMSB:0] xc4;
364
 
365
always @(posedge clk)
366
        if (ce) ex4 <= ex3;
367
always @(posedge clk)
368
        if (ce) xc4 <= xc3;
369
 
370
// -----------------------------------------------------------
371
// Clock #5
372
// Sum partial products (above)
373
// compute multiplier overflow and underflow
374
// -----------------------------------------------------------
375
 
376
// Status
377
reg under5;
378
reg over5;
379
reg [EMSB+2:0] ex5;
380
reg [EMSB:0] xc5;
381
wire aInf5, bInf5;
382
wire aNan5, bNan5;
383
wire qNaNOut5;
384
 
385
always @(posedge clk)
386
        if (ce) under5 <= ex4[EMSB+2];
387
always @(posedge clk)
388
        if (ce) over5 <= (&ex4[EMSB:0] | ex4[EMSB+1]) & !ex4[EMSB+2];
389
always @(posedge clk)
390
        if (ce) ex5 <= ex4;
391
always @(posedge clk)
392
        if (ce) xc5 <= xc4;
393
 
394
delay4 u2a (.clk(clk), .ce(ce), .i(aInf1), .o(aInf5) );
395
delay4 u2b (.clk(clk), .ce(ce), .i(bInf1), .o(bInf5) );
396
 
397
// determine when a NaN is output
398
wire [MSB:0] a5,b5;
399
delay4 u5 (.clk(clk), .ce(ce), .i((aInf1&bz1)|(bInf1&az1)), .o(qNaNOut5) );
400
delay4 u14 (.clk(clk), .ce(ce), .i(aNan1), .o(aNan5) );
401
delay4 u15 (.clk(clk), .ce(ce), .i(bNan1), .o(bNan5) );
402
delay5 #(MSB+1) u16 (.clk(clk), .ce(ce), .i(a), .o(a5) );
403
delay5 #(MSB+1) u17 (.clk(clk), .ce(ce), .i(b), .o(b5) );
404
 
405
// -----------------------------------------------------------
406
// Clock #6
407
// - figure multiplier mantissa output
408
// - figure multiplier exponent output
409
// - correct xponent and mantissa for exceptional conditions
410
// -----------------------------------------------------------
411
 
412
reg [FX:0] mo6;
413
reg [EMSB+2:0] ex6;
414
reg [EMSB:0] xc6;
415
wire [FMSB+1:0] fractc6;
416
vtdl #(FMSB+2) u61 (.clk(clk), .ce(ce), .a(4'd4), .d(fractc1), .q(fractc6) );
417
delay1 u62 (.clk(clk), .ce(ce), .i(under5), .o(under6));
418
 
419
always @(posedge clk)
420
        if (ce) xc6 <= xc5;
421
 
422
always @(posedge clk)
423
        if (ce)
424
                casez({aNan5,bNan5,qNaNOut5,aInf5,bInf5,over5})
425
                6'b1?????:  mo6 <= {1'b1,1'b1,a5[FMSB-1:0],{FMSB+1{1'b0}}};
426
    6'b01????:  mo6 <= {1'b1,1'b1,b5[FMSB-1:0],{FMSB+1{1'b0}}};
427
                6'b001???:      mo6 <= {1'b1,qNaN|3'd4,{FMSB+1{1'b0}}}; // multiply inf * zero
428
                6'b0001??:      mo6 <= 0;        // mul inf's
429
                6'b00001?:      mo6 <= 0;        // mul inf's
430
                6'b000001:      mo6 <= 0;        // mul overflow
431
                default:        mo6 <= fract5;
432
                endcase
433
 
434
always @(posedge clk)
435
        if (ce)
436
                casez({qNaNOut5|aNan5|bNan5,aInf5,bInf5,over5,under5})
437
                5'b1????:       ex6 <= infXp;   // qNaN - infinity * zero
438
                5'b01???:       ex6 <= infXp;   // 'a' infinite
439
                5'b001??:       ex6 <= infXp;   // 'b' infinite
440
                5'b0001?:       ex6 <= infXp;   // result overflow
441
                5'b00001:       ex6 <= ex5;             //0;            // underflow
442
                default:        ex6 <= ex5;             // situation normal
443
                endcase
444
 
445
// -----------------------------------------------------------
446
// Clock #7
447
// - prep for addition, determine greater operand
448
// -----------------------------------------------------------
449
reg ex_gt_xc7;
450
reg xeq7;
451
reg ma_gt_mc7;
452
reg meq7;
453
wire az7, bz7, cz7;
454
wire realOp7;
455
 
456
// which has greater magnitude ? Used for sign calc
457
always @(posedge clk)
458
        if (ce) ex_gt_xc7 <= $signed(ex6) > $signed({2'b0,xc6});
459
always @(posedge clk)
460
        if (ce) xeq7 <= (ex6=={2'b0,xc6});
461
always @(posedge clk)
462
        if (ce) ma_gt_mc7 <= mo6 > {fractc6,{FMSB+1{1'b0}}};
463
always @(posedge clk)
464
        if (ce) meq7 <= mo6 == {fractc6,{FMSB+1{1'b0}}};
465
vtdl #(1) u71 (.clk(clk), .ce(ce), .a(4'd5), .d(az1), .q(az7));
466
vtdl #(1) u72 (.clk(clk), .ce(ce), .a(4'd5), .d(bz1), .q(bz7));
467
vtdl #(1) u73 (.clk(clk), .ce(ce), .a(4'd5), .d(cz1), .q(cz7));
468
vtdl #(1) u74 (.clk(clk), .ce(ce), .a(4'd4), .d(realOp2), .q(realOp7));
469
 
470
// -----------------------------------------------------------
471
// Clock #8
472
// - prep for addition, determine greater operand
473
// - determine if result will be zero
474
// -----------------------------------------------------------
475
 
476
reg a_gt_b8;
477
reg resZero8;
478
reg ex_gt_xc8;
479
wire [EMSB+2:0] ex8;
480
wire [EMSB:0] xc8;
481
wire xcInf8;
482
wire [2:0] rm8;
483
wire op8;
484
wire sa8, sc8;
485
 
486
delay2 #(EMSB+3) u81 (.clk(clk), .ce(ce), .i(ex6), .o(ex8));
487
delay2 #(EMSB+1) u82 (.clk(clk), .ce(ce), .i(xc6), .o(xc8));
488
vtdl #(1) u83 (.clk(clk), .ce(ce), .a(4'd5), .d(xcInf2), .q(xcInf8));
489
vtdl #(3) u84 (.clk(clk), .ce(ce), .a(4'd7), .d(rm), .q(rm8));
490
vtdl #(1) u85 (.clk(clk), .ce(ce), .a(4'd6), .d(op1), .q(op8));
491
vtdl #(1) u86 (.clk(clk), .ce(ce), .a(4'd6), .d(sa1 ^ sb1), .q(sa8));
492
vtdl #(1) u87 (.clk(clk), .ce(ce), .a(4'd6), .d(sc1), .q(sc8));
493
 
494
always @(posedge clk)
495
        if (ce) ex_gt_xc8 <= ex_gt_xc7;
496
always @(posedge clk)
497
        if (ce)
498
                a_gt_b8 <= ex_gt_xc7 || (xeq7 && ma_gt_mc7);
499
 
500
// Find out if the result will be zero.
501
always @(posedge clk)
502
        if (ce)
503
                resZero8 <= (realOp7 & xeq7 & meq7) ||  // subtract, same magnitude
504
                           ((az7 | bz7) & cz7);         // a or b zero and c zero
505
 
506
// -----------------------------------------------------------
507
// CLock #9
508
// Compute output exponent and sign
509
//
510
// The output exponent is the larger of the two exponents,
511
// unless a subtract operation is in progress and the two
512
// numbers are equal, in which case the exponent should be
513
// zero.
514
// -----------------------------------------------------------
515
 
516
reg so9;
517
reg [EMSB+2:0] ex9;
518
reg [EMSB+2:0] ex9a;
519
reg ex_gt_xc9;
520
reg [EMSB:0] xc9;
521
reg a_gt_c9;
522
wire [FX:0] mo9;
523
wire [FMSB+1:0] fractc9;
524
wire under9;
525
wire xeq9;
526
 
527
always @(posedge clk)
528
        if (ce) ex_gt_xc9 <= ex_gt_xc8;
529
always @(posedge clk)
530
        if (ce) a_gt_c9 <= a_gt_b8;
531
always @(posedge clk)
532
        if (ce) xc9 <= xc8;
533
always @(posedge clk)
534
        if (ce) ex9a <= ex8;
535
 
536
delay3 #(FX+1) u93 (.clk(clk), .ce(ce), .i(mo6), .o(mo9));
537
delay3 #(FMSB+2) u94 (.clk(clk), .ce(ce), .i(fractc6), .o(fractc9));
538
delay3 u95 (.clk(clk), .ce(ce), .i(under6), .o(under9));
539
delay2 u96 (.clk(clk), .ce(ce), .i(xeq7), .o(xeq9));
540
 
541
always @(posedge clk)
542
        if (ce) ex9 <= resZero8 ? 1'd0 : ex_gt_xc8 ? ex8 : {2'b0,xc8};
543
 
544
// Compute output sign
545
always @(posedge clk)
546
        if (ce)
547
        case ({resZero8,sa8,op8,sc8})   // synopsys full_case parallel_case
548
        4'b0000: so9 <= 0;                       // + + + = +
549
        4'b0001: so9 <= !a_gt_b8;       // + + - = sign of larger
550
        4'b0010: so9 <= !a_gt_b8;       // + - + = sign of larger
551
        4'b0011: so9 <= 0;                       // + - - = +
552
        4'b0100: so9 <= a_gt_b8;                // - + + = sign of larger
553
        4'b0101: so9 <= 1;                      // - + - = -
554
        4'b0110: so9 <= 1;                      // - - + = -
555
        4'b0111: so9 <= a_gt_b8;                // - - - = sign of larger
556
        4'b1000: so9 <= 0;                       //  A +  B, sign = +
557
        4'b1001: so9 <= rm8==3;         //  A + -B, sign = + unless rounding down
558
        4'b1010: so9 <= rm8==3;         //  A -  B, sign = + unless rounding down
559
        4'b1011: so9 <= 0;                       // +A - -B, sign = +
560
        4'b1100: so9 <= rm8==3;         // -A +  B, sign = + unless rounding down
561
        4'b1101: so9 <= 1;                      // -A + -B, sign = -
562
        4'b1110: so9 <= 1;                      // -A - +B, sign = -
563
        4'b1111: so9 <= rm8==3;         // -A - -B, sign = + unless rounding down
564
        endcase
565
 
566
// -----------------------------------------------------------
567
// Clock #10
568
// Compute the difference in exponents, provides shift amount
569
// Note that ex9a will be negative for an underflow condition
570
// so it's added rather than subtracted from xc9 as -(-num)
571
// is the same as an add. The underflow is tracked rather than
572
// using extra bits in the exponent.
573
// -----------------------------------------------------------
574
reg [EMSB+2:0] xdiff10;
575
reg [FX:0] mfs;
576
reg ops10;
577
 
578
// If the multiplier exponent was negative (underflowed) then
579
// the mantissa needs to be shifted right even more (until
580
// the exponent is zero. The total shift would be xc9-0-
581
// amount underflows which is xc9 + -ex9a.
582
 
583
always @(posedge clk)
584
        if (ce) xdiff10 <= ex_gt_xc9 ? ex9a - xc9
585
                                                                                : ex9a[EMSB+2] ? xc9 + (~ex9a+2'd1)
586
                                                                                : xc9 - ex9a;
587
 
588
// Determine which fraction to denormalize (the one with the
589
// smaller exponent is denormalized). If the exponents are equal
590
// denormalize the smaller fraction.
591
always @(posedge clk)
592
        if (ce) mfs <=
593
                xeq9 ? (a_gt_c9 ? {4'b0,fractc9,{FMSB+1{1'b0}}} : mo9)
594
                 : ex_gt_xc9 ? {4'b0,fractc9,{FMSB+1{1'b0}}} : mo9;
595
 
596
always @(posedge clk)
597
        if (ce) ops10 <= xeq9 ? (a_gt_c9 ? 1'b1 : 1'b0)
598
                                                                                                : (ex_gt_xc9 ? 1'b1 : 1'b0);
599
 
600
// -----------------------------------------------------------
601
// Clock #11
602
// Limit the size of the shifter to only bits needed.
603
// -----------------------------------------------------------
604
reg [7:0] xdif11;
605
 
606
always @(posedge clk)
607
        if (ce) xdif11 <= xdiff10 > FX+3 ? FX+3 : xdiff10;
608
 
609
// -----------------------------------------------------------
610
// Clock #12
611
// Determine the sticky bit
612
// -----------------------------------------------------------
613
 
614
wire sticky, sticky12;
615
wire [FX:0] mfs12;
616
wire [7:0] xdif12;
617
 
618
generate
619
begin
620
if (FPWID+`EXTRA_BITS==128)
621
    redor128 u121 (.a(xdif11), .b({mfs,2'b0}), .o(sticky) );
622
else if (FPWID+`EXTRA_BITS==96)
623
    redor96 u121 (.a(xdif11), .b({mfs,2'b0}), .o(sticky) );
624
else if (FPWID+`EXTRA_BITS==84)
625
    redor84 u121 (.a(xdif11), .b({mfs,2'b0}), .o(sticky) );
626
else if (FPWID+`EXTRA_BITS==80)
627
    redor80 u121 (.a(xdif11), .b({mfs,2'b0}), .o(sticky) );
628
else if (FPWID+`EXTRA_BITS==64)
629
    redor64 u121 (.a(xdif11), .b({mfs,2'b0}), .o(sticky) );
630
else if (FPWID+`EXTRA_BITS==32)
631
    redor32 u121 (.a(xdif11), .b({mfs,2'b0}), .o(sticky) );
632
end
633
endgenerate
634
 
635
// register inputs to shifter and shift
636
delay1 #(1)    u122(.clk(clk), .ce(ce), .i(sticky), .o(sticky12) );
637
delay1 #(8)    u123(.clk(clk), .ce(ce), .i(xdif11),   .o(xdif12) );
638
delay2 #(FX+1) u124(.clk(clk), .ce(ce), .i(mfs), .o(mfs12) );
639
 
640
// -----------------------------------------------------------
641
// Clock #13
642
// - denormalize operand (shift right)
643
// -----------------------------------------------------------
644
reg [FX+2:0] mfs13;
645
wire [FX:0] mo13;
646
wire ex_gt_xc13;
647
wire [FMSB+1:0] fractc13;
648
wire ops13;
649
 
650
delay4 #(FX+1) u131 (.clk(clk), .ce(ce), .i(mo9), .o(mo13));
651
delay4 u132 (.clk(clk), .ce(ce), .i(ex_gt_xc9), .o(ex_gt_xc13));
652
vtdl #(FMSB+2) u133 (.clk(clk), .ce(ce), .a(4'd3), .d(fractc9), .q(fractc13));
653
delay3 u134 (.clk(clk), .ce(ce), .i(ops10), .o(ops13));
654
 
655
always @(posedge clk)
656
        if (ce) mfs13 <= ({mfs12,2'b0} >> xdif12)|sticky12;
657
 
658
// -----------------------------------------------------------
659
// Clock #14
660
// Sort operands
661
// -----------------------------------------------------------
662
reg [FX+2:0] oa, ob;
663
wire a_gt_b14;
664
 
665
vtdl #(1) u141 (.clk(clk), .ce(ce), .a(4'd5), .d(a_gt_b8), .q(a_gt_b14));
666
 
667
always @(posedge clk)
668
        if (ce) oa <= ops13 ? {mo13,2'b00} : mfs13;
669
always @(posedge clk)
670
        if (ce) ob <= ops13 ? mfs13 : {fractc13,{FMSB+1{1'b0}},2'b00};
671
 
672
// -----------------------------------------------------------
673
// Clock #15
674
// - Sort operands
675
// -----------------------------------------------------------
676
reg [FX+2:0] oaa, obb;
677
wire realOp15;
678
wire [EMSB:0] ex15;
679
wire [EMSB:0] ex9c = ex9[EMSB+1] ? infXp : ex9[EMSB:0];
680
wire overflow15;
681
vtdl #(1) u151 (.clk(clk), .ce(ce), .a(4'd7), .d(realOp7), .q(realOp15));
682
vtdl #(EMSB+1) u152 (.clk(clk), .ce(ce), .a(4'd5), .d(ex9c), .q(ex15));
683
vtdl #(EMSB+1) u153 (.clk(clk), .ce(ce), .a(4'd5), .d(ex9[EMSB+1]| &ex9[EMSB:0]), .q(overflow15));
684
 
685
always @(posedge clk)
686
        if (ce) oaa <= a_gt_b14 ? oa : ob;
687
always @(posedge clk)
688
        if (ce) obb <= a_gt_b14 ? ob : oa;
689
 
690
// -----------------------------------------------------------
691
// Clock #16
692
// - perform add/subtract
693
// - addition can generate an extra bit, subtract can't go negative
694
// -----------------------------------------------------------
695
reg [FX+3:0] mab;
696
wire [FX:0] mo16;
697
wire [FMSB+1:0] fractc16;
698
wire Nan16;
699
wire cNan16;
700
wire aInf16, cInf16;
701
wire op16;
702
wire exinf16;
703
 
704
vtdl #(1) u161 (.clk(clk), .ce(ce), .a(4'd10), .d(qNaNOut5|aNan5|bNan5), .q(Nan16));
705
vtdl #(1) u162 (.clk(clk), .ce(ce), .a(4'd14), .d(cNan1), .q(cNan16));
706
vtdl #(1) u163 (.clk(clk), .ce(ce), .a(4'd9), .d(&ex6), .q(aInf16));
707
vtdl #(1) u164 (.clk(clk), .ce(ce), .a(4'd14), .d(cInf1), .q(cInf16));
708
vtdl #(1) u165 (.clk(clk), .ce(ce), .a(4'd14), .d(op1), .q(op16));
709
delay3 #(FX+1) u166 (.clk(clk), .ce(ce), .i(mo13), .o(mo16));
710
vtdl #(FMSB+2) u167 (.clk(clk), .ce(ce), .a(4'd6), .d(fractc9), .q(fractc16));
711
delay1 u169 (.clk(clk), .ce(ce), .i(&ex15), .o(exinf16));
712
 
713
always @(posedge clk)
714
        if (ce) mab <= realOp15 ? oaa - obb : oaa + obb;
715
 
716
// -----------------------------------------------------------
717
// Clock #17
718
// - adjust for Nans
719
// -----------------------------------------------------------
720
wire [EMSB:0] ex17;
721
reg [FX:0] mo17;
722
wire so17;
723
wire exinf17;
724
wire overflow17;
725
 
726
vtdl #(1)        u171 (.clk(clk), .ce(ce), .a(4'd7), .d(so9), .q(so17));
727
delay2 #(EMSB+1) u172 (.clk(clk), .ce(ce), .i(ex15), .o(ex17));
728
delay1 #(1) u173 (.clk(clk), .ce(ce), .i(exinf16), .o(exinf17));
729
delay2 u174 (.clk(clk), .ce(ce), .i(overflow15), .o(overflow17));
730
 
731
always @(posedge clk)
732
        casez({aInf16&cInf16,Nan16,cNan16,exinf16})
733
        4'b1???:        mo17 <= {1'b0,op16,{FMSB-1{1'b0}},op16,{FMSB{1'b0}}};   // inf +/- inf - generate QNaN on subtract, inf on add
734
        4'b01??:        mo17 <= {1'b0,mo16};
735
        4'b001?:        mo17 <= {1'b1,1'b1,fractc16[FMSB-1:0],{FMSB+1{1'b0}}};
736
        4'b0001:        mo17 <= 1'd0;
737
        default:        mo17 <= mab[FX+3:2];            // mab has two extra lead bits and two trailing bits
738
        endcase
739
 
740
assign o = {so17,ex17,mo17};
741
assign zero = {ex17,mo17}==1'd0;
742
assign inf = exinf17;
743
assign under = ex17==1'd0;
744
assign over = overflow17;
745
 
746
endmodule
747
 
748
 
749
// Multiplier with normalization and rounding.
750
 
751
module fpFMAnr(clk, ce, op, rm, a, b, c, o, inf, zero, overflow, underflow, inexact);
752
parameter FPWID=64;
753
`include "fpSize.sv"
754
 
755
input clk;
756
input ce;
757
input op;
758
input [2:0] rm;
759
input  [MSB:0] a, b, c;
760
output [MSB:0] o;
761
output zero;
762
output inf;
763
output overflow;
764
output underflow;
765
output inexact;
766
 
767
wire [EX:0] fma_o;
768
wire fma_underflow;
769
wire fma_overflow;
770
wire norm_underflow;
771
wire norm_inexact;
772
wire sign_exe1, inf1, overflow1, underflow1;
773
wire [MSB+3:0] fpn0;
774
 
775
fpFMA #(FPWID) u1
776
(
777
        .clk(clk),
778
        .ce(ce),
779
        .op(op),
780
        .rm(rm),
781
        .a(a),
782
        .b(b),
783
        .c(c),
784
        .o(fma_o),
785
        .under(fma_underflow),
786
        .over(fma_overflow),
787
        .zero(),
788
        .inf()
789
);
790
fpNormalize #(FPWID) u2
791
(
792
        .clk(clk),
793
        .ce(ce),
794
        .i(fma_o),
795
        .o(fpn0),
796
        .under_i(fma_underflow),
797
        .under_o(norm_underflow),
798
        .inexact_o(norm_inexact)
799
);
800
fpRound         #(FPWID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
801
fpDecomp        #(FPWID) u4(.i(o), .xz(), .vz(zero), .inf(inf));
802
vtdl                                            u5 (.clk(clk), .ce(ce), .a(4'd11), .d(fma_underflow), .q(underflow));
803
vtdl                                            u6 (.clk(clk), .ce(ce), .a(4'd11), .d(fma_overflow), .q(overflow));
804
delay3          #(1)    u7 (.clk(clk), .ce(ce), .i(norm_inexact), .o(inexact));
805
assign overflow = inf;
806
 
807
endmodule
808
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.