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

Subversion Repositories ft816float

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

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

powered by: WebSVN 2.1.0

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