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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpAddsub.v] - Blame information for rev 6

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

Line No. Rev Author Line
1 6 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2016  Robert Finch, Stratford
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// This source file is free software: you can redistribute it and/or modify 
9
// it under the terms of the GNU Lesser General Public License as published 
10
// by the Free Software Foundation, either version 3 of the License, or     
11
// (at your option) any later version.                                      
12
//                                                                          
13
// This source file is distributed in the hope that it will be useful,      
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
16
// GNU General Public License for more details.                             
17
//                                                                          
18
// You should have received a copy of the GNU General Public License        
19
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
20
//
21
//      fpAddsub.v
22
//  - floating point adder/subtracter
23
//  - two cycle latency
24
//  - can issue every clock cycle
25
//  - parameterized width
26
//  - IEEE 754 representation
27
//
28
//      This adder/subtractor handles denormalized numbers.
29
// It has a two cycle latency.
30
// The output format is of an internal expanded representation
31
// in preparation to be fed into a normalization unit, then
32
// rounding. Basically, it's the same as the regular format
33
// except the mantissa is doubled in size, the leading two
34
// bits of which are assumed to be whole bits.
35
// ============================================================================
36
//
37
module fpAddsub(clk, ce, rm, op, a, b, o);
38
parameter WID = 32;
39
localparam MSB = WID-1;
40
localparam EMSB = WID==80 ? 14 :
41
                  WID==64 ? 10 :
42
                                  WID==52 ? 10 :
43
                                  WID==48 ? 10 :
44
                                  WID==44 ? 10 :
45
                                  WID==42 ? 10 :
46
                                  WID==40 ?  9 :
47
                                  WID==32 ?  7 :
48
                                  WID==24 ?  6 : 4;
49
localparam FMSB = WID==80 ? 63 :
50
                  WID==64 ? 51 :
51
                                  WID==52 ? 39 :
52
                                  WID==48 ? 35 :
53
                                  WID==44 ? 31 :
54
                                  WID==42 ? 29 :
55
                                  WID==40 ? 28 :
56
                                  WID==32 ? 22 :
57
                                  WID==24 ? 15 : 9;
58
 
59
localparam WX = 3;
60
localparam FX = (FMSB+1)*2-1;   // the MSB of the expanded fraction
61
localparam EX = FX + WX + EMSB + 1;
62
 
63
input clk;              // system clock
64
input ce;               // core clock enable
65
input [2:0] rm;  // rounding mode
66
input op;               // operation 0 = add, 1 = subtract
67
input [WID-1:0] a;       // operand a
68
input [WID-1:0] b;       // operand b
69
output [EX+1:0] o;       // output
70
 
71
 
72
// variables
73
wire so;                        // sign output
74
wire [EMSB:0] xo;        // de normalized exponent output
75
reg [EMSB:0] xo1;        // de normalized exponent output
76
wire [FX+WX:0] mo;       // mantissa output
77
reg [FX+WX:0] mo1;       // mantissa output
78
 
79
// There's an extra bit output in the mantissa to allow for three whole
80
// digits which the normalizer uses.
81
assign o = {so,xo,mo};
82
 
83
// operands sign,exponent,mantissa
84
wire sa, sb;
85
wire [EMSB:0] xa, xb;
86
wire [FMSB:0] ma, mb;
87
wire [FMSB+1:0] fracta, fractb;
88
wire [FMSB+1:0] fracta1, fractb1;
89
 
90
// which has greater magnitude ? Used for sign calc
91
wire xa_gt_xb = xa > xb;
92
wire xa_gt_xb1;
93
wire a_gt_b = xa_gt_xb || (xa==xb && ma > mb);
94
wire a_gt_b1;
95
wire az, bz;    // operand a,b is zero
96
 
97
wire adn, bdn;          // a,b denormalized ?
98
wire xaInf, xbInf;
99
wire aInf, bInf, aInf1, bInf1;
100
wire aNan, bNan, aNan1, bNan1;
101
 
102
wire [EMSB:0] xad = xa|adn;      // operand a exponent, compensated for denormalized numbers
103
wire [EMSB:0] xbd = xb|bdn; // operand b exponent, compensated for denormalized numbers
104
 
105
fpDecompose #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .man(ma), .fract(fracta), .xz(adn), .vz(az), .xinf(xaInf), .inf(aInf), .nan(aNan) );
106
fpDecompose #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .man(mb), .fract(fractb), .xz(bdn), .vz(bz), .xinf(xbInf), .inf(bInf), .nan(bNan) );
107
 
108
// Figure out which operation is really needed an add or
109
// subtract ?
110
// If the signs are the same, use the orignal op,
111
// otherwise flip the operation
112
//  a +  b = add,+
113
//  a + -b = sub, so of larger
114
// -a +  b = sub, so of larger
115
// -a + -b = add,-
116
//  a -  b = sub, so of larger
117
//  a - -b = add,+
118
// -a -  b = add,-
119
// -a - -b = sub, so of larger
120
wire realOp = op ^ sa ^ sb;
121
wire realOp1;
122
wire op1;
123
 
124
// Find out if the result will be zero.
125
wire resZero = (realOp && xa==xb && ma==mb) ||  // subtract, same magnitude
126
                           (az & bz);           // both a,b zero
127
 
128
// Compute output exponent
129
//
130
// The output exponent is the larger of the two exponents,
131
// unless a subtract operation is in progress and the two
132
// numbers are equal, in which case the exponent should be
133
// zero.
134
 
135
always @(xaInf,xbInf,resZero,xa,xb,xa_gt_xb)
136
        xo1 = (xaInf&xbInf) ? xa : resZero ? 0 : xa_gt_xb ? xa : xb;
137
 
138
// Compute output sign
139
reg so1;
140
always @*
141
        case ({resZero,sa,op,sb})       // synopsys full_case parallel_case
142
        4'b0000: so1 <= 0;                       // + + + = +
143
        4'b0001: so1 <= !a_gt_b;        // + + - = sign of larger
144
        4'b0010: so1 <= !a_gt_b;        // + - + = sign of larger
145
        4'b0011: so1 <= 0;                       // + - - = +
146
        4'b0100: so1 <= a_gt_b;         // - + + = sign of larger
147
        4'b0101: so1 <= 1;                      // - + - = -
148
        4'b0110: so1 <= 1;                      // - - + = -
149
        4'b0111: so1 <= a_gt_b;         // - - - = sign of larger
150
        4'b1000: so1 <= 0;                       //  A +  B, sign = +
151
        4'b1001: so1 <= rm==3;          //  A + -B, sign = + unless rounding down
152
        4'b1010: so1 <= rm==3;          //  A -  B, sign = + unless rounding down
153
        4'b1011: so1 <= 0;                       // +A - -B, sign = +
154
        4'b1100: so1 <= rm==3;          // -A +  B, sign = + unless rounding down
155
        4'b1101: so1 <= 1;                      // -A + -B, sign = -
156
        4'b1110: so1 <= 1;                      // -A - +B, sign = -
157
        4'b1111: so1 <= rm==3;          // -A - -B, sign = + unless rounding down
158
        endcase
159
 
160
delay2 #(EMSB+1) d1(.clk(clk), .ce(ce), .i(xo1), .o(xo) );
161
delay2 #(1)      d2(.clk(clk), .ce(ce), .i(so1), .o(so) );
162
 
163
// Compute the difference in exponents, provides shift amount
164
wire [EMSB:0] xdiff = xa_gt_xb ? xad - xbd : xbd - xad;
165
wire [6:0] xdif = xdiff > FMSB+3 ? FMSB+3 : xdiff;
166
wire [6:0] xdif1;
167
 
168
// determine which fraction to denormalize
169
wire [FMSB+1:0] mfs = xa_gt_xb ? fractb : fracta;
170
wire [FMSB+1:0] mfs1;
171
 
172
// Determine the sticky bit
173
wire sticky, sticky1;
174
redor64 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
175
 
176
// register inputs to shifter and shift
177
delay1 #(1)      d16(.clk(clk), .ce(ce), .i(sticky), .o(sticky1) );
178
delay1 #(7)      d15(.clk(clk), .ce(ce), .i(xdif),   .o(xdif1) );
179
delay1 #(FMSB+2) d14(.clk(clk), .ce(ce), .i(mfs),    .o(mfs1) );
180
 
181
wire [FMSB+3:0] md1 = ({mfs1,2'b0} >> xdif1)|sticky1;
182
 
183
// sync control signals
184
delay1 #(1) d4 (.clk(clk), .ce(ce), .i(xa_gt_xb), .o(xa_gt_xb1) );
185
delay1 #(1) d17(.clk(clk), .ce(ce), .i(a_gt_b), .o(a_gt_b1) );
186
delay1 #(1) d5 (.clk(clk), .ce(ce), .i(realOp), .o(realOp1) );
187
delay1 #(FMSB+2) d5a(.clk(clk), .ce(ce), .i(fracta), .o(fracta1) );
188
delay1 #(FMSB+2) d6a(.clk(clk), .ce(ce), .i(fractb), .o(fractb1) );
189
delay1 #(1) d7 (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
190
delay1 #(1) d8 (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
191
delay1 #(1) d9 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
192
delay1 #(1) d10(.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
193
delay1 #(1) d11(.clk(clk), .ce(ce), .i(op), .o(op1) );
194
 
195
// Sort operands and perform add/subtract
196
// addition can generate an extra bit, subtract can't go negative
197
wire [FMSB+3:0] oa = xa_gt_xb1 ? {fracta1,2'b0} : md1;
198
wire [FMSB+3:0] ob = xa_gt_xb1 ? md1 : {fractb1,2'b0};
199
wire [FMSB+3:0] oaa = a_gt_b1 ? oa : ob;
200
wire [FMSB+3:0] obb = a_gt_b1 ? ob : oa;
201
wire [FMSB+4:0] mab = realOp1 ? oaa - obb : oaa + obb;
202
 
203
always @*
204
        casex({aInf1&bInf1,aNan1,bNan1})
205
        3'b1xx:         mo1 = {1'b0,op1,{FMSB-1{1'b0}},op1,{FMSB{1'b0}}};       // inf +/- inf - generate QNaN on subtract, inf on add
206
        3'bx1x:         mo1 = {1'b0,fracta1[FMSB+1:0],{FMSB{1'b0}}};
207
        3'bxx1:         mo1 = {1'b0,fractb1[FMSB+1:0],{FMSB{1'b0}}};
208
        default:        mo1 = {mab,{FMSB-1{1'b0}}};     // mab has an extra lead bit
209
        endcase
210
 
211
delay1 #(FX+WX+1) d3(.clk(clk), .ce(ce), .i(mo1), .o(mo) );
212
 
213
endmodule
214
 
215
module fpAddsub_tb();
216
reg clk;
217
wire ce = 1'b1;
218
wire [2:0] rm = 3'b0;
219
wire [57:0] o1,o2,o3,o4,o5,o6;
220
wire [35:0] o11,o12,o13;
221
wire [31:0] o21,o22,o23;
222
 
223
initial begin
224
  clk = 1'b0;
225
end
226
always #10 clk = ~clk;
227
 
228
fpAddsub u1 (clk, ce, rm, 1'b0, 32'h0, 32'h0, o1);  // zero plus zero
229
fpAddsub u2 (clk, ce, rm, 1'b1, 32'h0, 32'h0, o2);  // zero minus zero
230
fpAddsub u3 (clk, ce, rm, 1'b0, 32'h3F000000, 32'h3F000000, o3);  // .5 + .5
231
fpAddsub u4 (clk, ce, rm, 1'b0, 32'h43520000, 32'h41700000, o4);  // 210+15
232
fpAddsub u5 (clk, ce, rm, 1'b1, 32'hC3520000, 32'hC1700000, o5);  // -210- -15
233
 
234
fpNormalize u11 (clk, ce, 1'b0, o3, o11);
235
fpNormalize u12 (clk, ce, 1'b0, o4, o12);
236
fpNormalize u13 (clk, ce, 1'b0, o5, o13);
237
 
238
fpRound u21 (3'd1, o11, o21);         // zero for zero
239
fpRound u22 (3'd1, o12, o22); // 
240
fpRound u23 (3'd1, o13, o23); // 
241
 
242
endmodule
243
 

powered by: WebSVN 2.1.0

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