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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpFMA.v] - Blame information for rev 48

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

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

powered by: WebSVN 2.1.0

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