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