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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpAddsub.sv] - Blame information for rev 89

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

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

powered by: WebSVN 2.1.0

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