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

Subversion Repositories ft816float

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

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

powered by: WebSVN 2.1.0

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