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

Subversion Repositories ft816float

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

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

powered by: WebSVN 2.1.0

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