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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPAddsub128.sv] - Blame information for rev 58

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

Line No. Rev Author Line
1 57 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2020-2021  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      DFPAddsub.sv
9
//
10
// BSD 3-Clause License
11
// Redistribution and use in source and binary forms, with or without
12
// modification, are permitted provided that the following conditions are met:
13
//
14
// 1. Redistributions of source code must retain the above copyright notice, this
15
//    list of conditions and the following disclaimer.
16
//
17
// 2. Redistributions in binary form must reproduce the above copyright notice,
18
//    this list of conditions and the following disclaimer in the documentation
19
//    and/or other materials provided with the distribution.
20
//
21
// 3. Neither the name of the copyright holder nor the names of its
22
//    contributors may be used to endorse or promote products derived from
23
//    this software without specific prior written permission.
24
//
25
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
//
36
// ============================================================================
37
 
38
import DFPPkg::*;
39
 
40
module DFPAddsub128(clk, ce, rm, op, a, b, o);
41
input clk;
42
input ce;
43
input [2:0] rm;
44
input op;
45
input DFP128 a;
46
input DFP128 b;
47
output DFP128UD o;
48
localparam N=34;                        // number of BCD digits
49
 
50
parameter TRUE = 1'b1;
51
parameter FALSE = 1'b0;
52
 
53
DFP128U au;
54
DFP128U bu;
55
wire sa, sb;
56
wire sxa, sxb;
57
wire adn, bdn;
58
wire xainf, xbinf;
59
wire ainf, binf;
60
wire aNan, bNan;
61
wire [13:0] xa, xb;
62
wire [N*4-1:0] siga, sigb;
63
 
64
DFPUnpack128 u00 (a, au);
65
DFPUnpack128 u01 (b, bu);
66
 
67
wire [(N+1)*4-1:0] oss10;
68
wire oss10c;
69
 
70
BCDAddN #(.N(N+1)) ubcdan1
71
(
72
        .ci(1'b0),
73
        .a(oaa10),
74
        .b(obb10),
75
        .o(oss10),
76
        .co(oss10c)
77
);
78
 
79
wire [(N+1)*4-1:0] odd10;
80
wire odd10c;
81
 
82
BCDSubN #(.N(N+1)) ubcdsn1
83
(
84
        .ci(1'b0),
85
        .a(oaa10),
86
        .b(obb10),
87
        .o(odd10),
88
        .co(odd10c)
89
);
90
 
91
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
92
// Clock #1
93
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94
reg op1;
95
reg az, bz;
96
always @(posedge clk)
97
        op1 <= op;
98
always @(posedge clk)
99
        az <= au.sig==136'd0 && au.exp==14'd0;
100
always @(posedge clk)
101
        bz <= bu.sig==136'd0 && bu.exp==14'd0;
102
 
103
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104
// Clock #2
105
//
106
// Figure out which operation is really needed an add or subtract ?
107
// If the signs are the same, use the orignal op,
108
// otherwise flip the operation
109
//  a +  b = add,+
110
//  a + -b = sub, so of larger
111
// -a +  b = sub, so of larger
112
// -a + -b = add,-
113
//  a -  b = sub, so of larger
114
//  a - -b = add,+
115
// -a -  b = add,-
116
// -a - -b = sub, so of larger
117
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118
reg realOp2;
119
reg op2;
120
reg [15:0] xa2, xb2;
121
reg az2, bz2;
122
reg xa_gt_xb2;
123
reg [N*4-1:0] siga2, sigb2;
124
reg sigeq, siga_gt_sigb;
125
reg xa_gt_xb2;
126
reg expeq;
127
reg sxo2;
128
 
129
always @(posedge clk)
130
  if (ce) realOp2 = op1 ^ au.sign ^ bu.sign;
131
always @(posedge clk)
132
  if (ce) op2 <= op1;
133
always @(posedge clk)
134
  if (ce) xa2 <= au.exp;
135
always @(posedge clk)
136
  if (ce) xb2 <= bu.exp;
137
always @(posedge clk)
138
  if (ce) siga2 <= au.sig;
139
always @(posedge clk)
140
  if (ce) sigb2 <= bu.sig;
141
always @(posedge clk)
142
  if (ce) az2 <= az;
143
always @(posedge clk)
144
  if (ce) bz2 <= bz;
145
always @(posedge clk)
146
  if (ce)
147
        xa_gt_xb2 <= au.exp > bu.exp;
148
 
149
always @(posedge clk)
150
  if (ce) sigeq <= au.sig==bu.sig;
151
always @(posedge clk)
152
  if (ce) siga_gt_sigb <= au.sig > bu.sig;
153
always @(posedge clk)
154
  if (ce) expeq <= au.exp==bu.exp;
155
 
156
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
157
// Clock #3
158
//
159
// Find out if the result will be zero.
160
// Determine which fraction to denormalize
161
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162
//
163
reg [13:0] xa3, xb3;
164
reg resZero3;
165
wire xaInf3, xbInf3;
166
reg xa_gt_xb3;
167
reg a_gt_b3;
168
reg op3;
169
wire sa3, sb3;
170
wire [2:0] rm3;
171
reg [N*4-1:0] mfs3;
172
 
173
always @(posedge clk)
174
  if (ce) resZero3 <= (realOp2 & expeq & sigeq) ||      // subtract, same magnitude
175
                           (az2 & bz2);               // both a,b zero
176
always @(posedge clk)
177
  if (ce) xa3 <= xa2;
178
always @(posedge clk)
179
  if (ce) xb3 <= xb2;
180
always @(posedge clk)
181
  if (ce) xa_gt_xb3 <= xa_gt_xb2;
182
always @(posedge clk)
183
  if (ce) a_gt_b3 <= xa_gt_xb2 | (expeq & siga_gt_sigb);
184
always @(posedge clk)
185
  if (ce) op3 <= op2;
186
always @(posedge clk)
187
  if (ce) mfs3 = xa_gt_xb2 ? sigb2 : siga2;
188
 
189
delay #(.WID(1), .DEP(2)) udly3c (.clk(clk), .ce(ce), .i(au.sign), .o(sa3));
190
delay #(.WID(1), .DEP(2)) udly3d (.clk(clk), .ce(ce), .i(bu.sign), .o(sb3));
191
delay #(.WID(3), .DEP(3)) udly3e (.clk(clk), .ce(ce), .i(rm), .o(rm3));
192
delay #(.WID(1), .DEP(2)) udly3f (.clk(clk), .ce(ce), .i(aInf), .o(aInf3));
193
delay #(.WID(1), .DEP(2)) udly3g (.clk(clk), .ce(ce), .i(bInf), .o(bInf3));
194
 
195
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196
// Clock #4
197
//
198
// Compute output exponent
199
//
200
// The output exponent is the larger of the two exponents,
201
// unless a subtract operation is in progress and the two
202
// numbers are equal, in which case the exponent should be
203
// zero.
204
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
205
 
206
reg [13:0] xa4, xb4;
207
reg [13:0] xo4;
208
reg xa_gt_xb4;
209
 
210
always @(posedge clk)
211
  if (ce) xa4 <= xa3;
212
always @(posedge clk)
213
  if (ce) xb4 <= xb3;
214
always @(posedge clk)
215
        if (ce) xo4 <= resZero3 ? 14'd0 : xa_gt_xb3 ? xa3 : xb3;
216
always @(posedge clk)
217
  if (ce) xa_gt_xb4 <= xa_gt_xb3;
218
 
219
// Compute output sign
220
reg so4;
221
always @*
222
        case ({resZero3,sa3,op3,sb3})   // synopsys full_case parallel_case
223
        4'b0000: so4 <= 0;                      // + + + = +
224
        4'b0001: so4 <= !a_gt_b3;       // + + - = sign of larger
225
        4'b0010: so4 <= !a_gt_b3;       // + - + = sign of larger
226
        4'b0011: so4 <= 0;                      // + - - = +
227
        4'b0100: so4 <= a_gt_b3;                // - + + = sign of larger
228
        4'b0101: so4 <= 1;                      // - + - = -
229
        4'b0110: so4 <= 1;                      // - - + = -
230
        4'b0111: so4 <= a_gt_b3;                // - - - = sign of larger
231
        4'b1000: so4 <= 0;                      //  A +  B, sign = +
232
        4'b1001: so4 <= (rm3==3'd3);            //  A + -B, sign = + unless rounding down
233
        4'b1010: so4 <= (rm3==3'd3);            //  A - B, sign = + unless rounding down
234
        4'b1011: so4 <= 0;                      // A - -B, sign = +
235
        4'b1100: so4 <= (rm3==3'd3);            // -A -  -B, sign = + unless rounding down
236
        4'b1101: so4 <= 1;                      // -A + -B, sign = -
237
        4'b1110: so4 <= 1;                      // -A - +B, sign = -
238
        4'b1111: so4 <= (rm3==3'd3);            // A - B, sign = + unless rounding down
239
        endcase
240
 
241
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
242
// Clock #5
243
//
244
// Compute the difference in exponents, provides shift amount
245
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
246
reg [13:0] xdiff5;
247
always @(posedge clk)
248
  if (ce) xdiff5 <= xa_gt_xb4 ? xa4 - xb4 : xb4 - xa4;
249
 
250
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
251
// Clock #6
252
//
253
// Compute the difference in exponents, provides shift amount
254
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
255
// If the difference in the exponent is 24 or greater (assuming 24 nybble dfp or
256
// less) then all of the bits will be shifted out to zero. There is no need to
257
// keep track of a difference more than 24.
258
reg [6:0] xdif6;
259
wire [N*4-1:0] mfs6;
260
always @(posedge clk)
261
  if (ce) xdif6 <= xdiff5 > N ? N : xdiff5[6:0];
262
delay #(.WID(N*4), .DEP(3)) udly6a (.clk(clk), .ce(ce), .i(mfs3), .o(mfs6));
263
 
264
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
265
// Clock #7
266
//
267
// Determine the sticky bit. The sticky bit is the bitwise or of all the bits
268
// being shifted out the right side. The sticky bit is computed here to
269
// reduce the number of regs required.
270
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
271
reg sticky6;
272
wire sticky7;
273
wire [7:0] xdif7;
274
wire [N*4-1:0] mfs7;
275
wire [8:0] xdif6a = {xdif6,2'b00};      // *4
276
integer n;
277
always @* begin
278
        sticky6 = 1'b0;
279
        for (n = 0; n < N*4; n = n + 4)
280
                if (n <= xdif6a)
281
                        sticky6 = sticky6| mfs6[n]|mfs6[n+1]|mfs6[n+2]|mfs6[n+3];       // non-zero nybble
282
end
283
 
284
// register inputs to shifter and shift
285
delay1 #(1)  d16(.clk(clk), .ce(ce), .i(sticky6), .o(sticky7) );
286
delay1 #(9)  d15(.clk(clk), .ce(ce), .i(xdif6a),   .o(xdif7) );
287
delay1 #(N*4) d14(.clk(clk), .ce(ce), .i(mfs6),    .o(mfs7) );
288
 
289
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
290
// Clock #8
291
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
292
reg [(N+1)*4-1:0] md8;
293
wire [N*4-1:0] siga8, sigb8;
294
wire xa_gt_xb8;
295
wire a_gt_b8;
296
always @(posedge clk)
297
  if (ce) md8 <= ({mfs7,4'b0} >> xdif7)|sticky7; // xdif7 is a multiple of four
298
 
299
// sync control signals
300
delay #(.WID(1), .DEP(4)) udly8a (.clk(clk), .ce(ce), .i(xa_gt_xb4), .o(xa_gt_xb8));
301
delay #(.WID(1), .DEP(5)) udly8b (.clk(clk), .ce(ce), .i(a_gt_b3), .o(a_gt_b8));
302
delay #(.WID(N*4), .DEP(6)) udly8d (.clk(clk), .ce(ce), .i(siga2), .o(siga8));
303
delay #(.WID(N*4), .DEP(6)) udly8e (.clk(clk), .ce(ce), .i(sigb2), .o(sigb8));
304
delay #(.WID(1), .DEP(5)) udly8j (.clk(clk), .ce(ce), .i(op3), .o(op8));
305
 
306
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
307
// Clock #9
308
// Sort operands and perform add/subtract
309
// addition can generate an extra bit, subtract can't go negative
310
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
311
reg [(N+1)*4-1:0] oa9, ob9;
312
reg a_gt_b9;
313
always @(posedge clk)
314
  if (ce) oa9 <= xa_gt_xb8 ? {siga8,4'b0} : md8;
315
always @(posedge clk)
316
  if (ce) ob9 <= xa_gt_xb8 ? md8 : {sigb8,4'b0};
317
always @(posedge clk)
318
  if (ce) a_gt_b9 <= a_gt_b8;
319
 
320
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
321
// Clock #10
322
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
323
reg [(N+1)*4-1:0] oaa10;
324
reg [(N+1)*4-1:0] obb10;
325
wire realOp10;
326
reg [13:0] xo10;
327
 
328
always @(posedge clk)
329
  if (ce) oaa10 <= a_gt_b9 ? oa9 : ob9;
330
always @(posedge clk)
331
  if (ce) obb10 <= a_gt_b9 ? ob9 : oa9;
332
delay #(.WID(1), .DEP(8)) udly10a (.clk(clk), .ce(ce), .i(realOp2), .o(realOp10));
333
delay #(.WID(14), .DEP(6)) udly10b (.clk(clk), .ce(ce), .i(xo4), .o(xo10));
334
 
335
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
336
// Clock #11
337
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
338
reg [(N+1)*4-1:0] mab11;
339
reg mab11c;
340
wire [N*4-1:0] siga11, sigb11;
341
wire abInf11;
342
wire aNan11, bNan11;
343
reg xoinf11;
344
wire op11;
345
 
346
always @(posedge clk)
347
  if (ce) mab11 <= realOp10 ? odd10 : oss10;
348
always @(posedge clk)
349
        if (ce) mab11c <= realOp10 ? odd10c : oss10c;
350
 
351
delay #(.WID(1), .DEP(8)) udly11a (.clk(clk), .ce(ce), .i(aInf3&bInf3), .o(abInf11));
352
delay #(.WID(1), .DEP(10)) udly11c (.clk(clk), .ce(ce), .i(aNan), .o(aNan11));
353
delay #(.WID(1), .DEP(10)) udly11d (.clk(clk), .ce(ce), .i(bNan), .o(bNan11));
354
delay #(.WID(1), .DEP(3)) udly11e (.clk(clk), .ce(ce), .i(op8), .o(op11));
355
delay #(.WID(N*4), .DEP(3)) udly11f (.clk(clk), .ce(ce), .i(siga8), .o(siga11));
356
delay #(.WID(N*4), .DEP(3)) udly11g (.clk(clk), .ce(ce), .i(sigb8), .o(sigb11));
357
 
358
always @(posedge clk)
359 58 robfinch
  if (ce) xoinf11 <= xo10==14'h2FFF;
360 57 robfinch
 
361
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
362
// Clock #12
363
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
364
reg [(N+1)*4*2-1:0] mo12;       // mantissa output
365
reg nan12;
366
reg qnan12;
367
reg infinity12;
368
wire sxo11;
369
wire so11;
370
delay #(.WID(1), .DEP(9)) udly12a (.clk(clk), .ce(ce), .i(sxo2), .o(sxo11));
371
delay #(.WID(1), .DEP(7)) udly12b (.clk(clk), .ce(ce), .i(so4), .o(so11));
372
 
373
always @(posedge clk)
374
if (ce)
375
        nan12 <= aNan11|bNan11;
376
 
377
always @(posedge clk)
378
if (ce) begin
379
        infinity12 <= 1'b0;
380
        qnan12 <= 1'b0;
381
        casez({abInf11,aNan11,bNan11,xoinf11})
382
        4'b1???:        // inf +/- inf - generate QNaN on subtract, inf on add
383
                if (op11) begin
384
                        mo12 <= {4'h9,{(N+1)*4*2-4{1'd0}}};
385
                        qnan12 <= 1'b1;
386
                end
387
                else begin
388
                        mo12 <= {(N+1)*2{4'h9}};
389
                        infinity12 <= 1'b1;
390
                end
391
        4'b01??:        mo12 <= {4'b0,siga11[107:0],{(N+1)*4{1'd0}}};
392
        4'b001?:        mo12 <= {4'b0,sigb11[107:0],{(N+1)*4{1'd0}}};
393
        4'b0001:        begin mo12 <= {(N+1)*4*2{1'd0}}; infinity12 <= 1'b1; end
394
        default:        mo12 <= {3'b0,mab11c,mab11,{N*4{1'd0}}};        // mab has an extra lead bit and four trailing bits
395
        endcase
396
end
397
 
398
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
399
// Clock #13
400
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
401
wire so;                        // sign output
402
wire [15:0] xo; // de normalized exponent output
403
wire [(N+1)*4*2-1:0] mo;        // mantissa output
404
 
405
delay #(.WID(1), .DEP(1)) u13c (.clk(clk), .ce(ce), .i(nan12), .o(o.nan) );
406
delay #(.WID(1), .DEP(1)) u13d (.clk(clk), .ce(ce), .i(qnan12), .o(o.qnan) );
407
delay #(.WID(1), .DEP(1)) u13e (.clk(clk), .ce(ce), .i(infinity12), .o(o.infinity) );
408
delay #(.WID(1), .DEP(9)) udly13a (.clk(clk), .ce(ce), .i(so4), .o(o.sign));
409
delay #(.WID(14), .DEP(3)) udly13b (.clk(clk), .ce(ce), .i(xo10), .o(o.exp));
410
delay #(.WID((N+1)*4*2), .DEP(1)) u13f (.clk(clk), .ce(ce), .i(mo12), .o(o.sig));
411
delay #(.WID(1), .DEP(1)) udly13g (.clk(clk), .ce(ce), .i(1'b0), .o(o.snan));
412
 
413
endmodule
414
 
415
 
416
module DFPAddsub128nr(clk, ce, rm, op, a, b, o);
417
input clk;              // system clock
418
input ce;               // core clock enable
419
input [2:0] rm; // rounding mode
420
input op;               // operation 0 = add, 1 = subtract
421
input DFP128 a; // operand a
422
input DFP128 b; // operand b
423
output DFP128 o;        // output
424
 
425
wire DFP128UD o1;
426
wire DFP128UN fpn0;
427
 
428
DFPAddsub128            u1 (clk, ce, rm, op, a, b, o1);
429
DFPNormalize128 u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
430
DFPRound128             u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
431
 
432
endmodule

powered by: WebSVN 2.1.0

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