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

Subversion Repositories ft816float

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

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

Line No. Rev Author Line
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 26 robfinch
`include "fpSize.sv"
35 6 robfinch
 
36
input clk;              // system clock
37
input ce;               // core clock enable
38
input [2:0] rm;  // rounding mode
39
input op;               // operation 0 = add, 1 = subtract
40
input [WID-1:0] a;       // operand a
41
input [WID-1:0] b;       // operand b
42 8 robfinch
output [EX:0] o; // output
43 6 robfinch
 
44
 
45
// variables
46
wire so;                        // sign output
47
wire [EMSB:0] xo;        // de normalized exponent output
48
reg [EMSB:0] xo1;        // de normalized exponent output
49 8 robfinch
wire [FX:0] mo;  // mantissa output
50
reg [FX:0] mo1;  // mantissa output
51 6 robfinch
 
52
assign o = {so,xo,mo};
53
 
54
// operands sign,exponent,mantissa
55
wire sa, sb;
56
wire [EMSB:0] xa, xb;
57
wire [FMSB:0] ma, mb;
58
wire [FMSB+1:0] fracta, fractb;
59
wire [FMSB+1:0] fracta1, fractb1;
60
 
61
// which has greater magnitude ? Used for sign calc
62
wire xa_gt_xb = xa > xb;
63
wire xa_gt_xb1;
64
wire a_gt_b = xa_gt_xb || (xa==xb && ma > mb);
65
wire a_gt_b1;
66
wire az, bz;    // operand a,b is zero
67
 
68
wire adn, bdn;          // a,b denormalized ?
69
wire xaInf, xbInf;
70
wire aInf, bInf, aInf1, bInf1;
71
wire aNan, bNan, aNan1, bNan1;
72
 
73
wire [EMSB:0] xad = xa|adn;      // operand a exponent, compensated for denormalized numbers
74
wire [EMSB:0] xbd = xb|bdn; // operand b exponent, compensated for denormalized numbers
75
 
76 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) );
77
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .man(mb), .fract(fractb), .xz(bdn), .vz(bz), .xinf(xbInf), .inf(bInf), .nan(bNan) );
78 6 robfinch
 
79
// Figure out which operation is really needed an add or
80
// subtract ?
81
// If the signs are the same, use the orignal op,
82
// otherwise flip the operation
83
//  a +  b = add,+
84
//  a + -b = sub, so of larger
85
// -a +  b = sub, so of larger
86
// -a + -b = add,-
87
//  a -  b = sub, so of larger
88
//  a - -b = add,+
89
// -a -  b = add,-
90
// -a - -b = sub, so of larger
91
wire realOp = op ^ sa ^ sb;
92
wire realOp1;
93
wire op1;
94
 
95
// Find out if the result will be zero.
96
wire resZero = (realOp && xa==xb && ma==mb) ||  // subtract, same magnitude
97
                           (az & bz);           // both a,b zero
98
 
99
// Compute output exponent
100
//
101
// The output exponent is the larger of the two exponents,
102
// unless a subtract operation is in progress and the two
103
// numbers are equal, in which case the exponent should be
104
// zero.
105
 
106
always @(xaInf,xbInf,resZero,xa,xb,xa_gt_xb)
107
        xo1 = (xaInf&xbInf) ? xa : resZero ? 0 : xa_gt_xb ? xa : xb;
108
 
109
// Compute output sign
110
reg so1;
111
always @*
112
        case ({resZero,sa,op,sb})       // synopsys full_case parallel_case
113
        4'b0000: so1 <= 0;                       // + + + = +
114
        4'b0001: so1 <= !a_gt_b;        // + + - = sign of larger
115
        4'b0010: so1 <= !a_gt_b;        // + - + = sign of larger
116
        4'b0011: so1 <= 0;                       // + - - = +
117
        4'b0100: so1 <= a_gt_b;         // - + + = sign of larger
118
        4'b0101: so1 <= 1;                      // - + - = -
119
        4'b0110: so1 <= 1;                      // - - + = -
120
        4'b0111: so1 <= a_gt_b;         // - - - = sign of larger
121
        4'b1000: so1 <= 0;                       //  A +  B, sign = +
122
        4'b1001: so1 <= rm==3;          //  A + -B, sign = + unless rounding down
123
        4'b1010: so1 <= rm==3;          //  A -  B, sign = + unless rounding down
124
        4'b1011: so1 <= 0;                       // +A - -B, sign = +
125
        4'b1100: so1 <= rm==3;          // -A +  B, sign = + unless rounding down
126
        4'b1101: so1 <= 1;                      // -A + -B, sign = -
127
        4'b1110: so1 <= 1;                      // -A - +B, sign = -
128
        4'b1111: so1 <= rm==3;          // -A - -B, sign = + unless rounding down
129
        endcase
130
 
131
delay2 #(EMSB+1) d1(.clk(clk), .ce(ce), .i(xo1), .o(xo) );
132
delay2 #(1)      d2(.clk(clk), .ce(ce), .i(so1), .o(so) );
133
 
134
// Compute the difference in exponents, provides shift amount
135
wire [EMSB:0] xdiff = xa_gt_xb ? xad - xbd : xbd - xad;
136
wire [6:0] xdif = xdiff > FMSB+3 ? FMSB+3 : xdiff;
137
wire [6:0] xdif1;
138
 
139
// determine which fraction to denormalize
140
wire [FMSB+1:0] mfs = xa_gt_xb ? fractb : fracta;
141
wire [FMSB+1:0] mfs1;
142
 
143
// Determine the sticky bit
144
wire sticky, sticky1;
145 8 robfinch
generate
146
begin
147
if (WID==128)
148
    redor128 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
149
else if (WID==96)
150
    redor96 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
151 26 robfinch
else if (WID==84)
152
    redor84 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
153 8 robfinch
else if (WID==80)
154
    redor80 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
155
else if (WID==64)
156
    redor64 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
157
else if (WID==32)
158
    redor32 u1 (.a(xdif), .b({mfs,2'b0}), .o(sticky) );
159
end
160
endgenerate
161 6 robfinch
 
162
// register inputs to shifter and shift
163
delay1 #(1)      d16(.clk(clk), .ce(ce), .i(sticky), .o(sticky1) );
164
delay1 #(7)      d15(.clk(clk), .ce(ce), .i(xdif),   .o(xdif1) );
165
delay1 #(FMSB+2) d14(.clk(clk), .ce(ce), .i(mfs),    .o(mfs1) );
166
 
167
wire [FMSB+3:0] md1 = ({mfs1,2'b0} >> xdif1)|sticky1;
168
 
169
// sync control signals
170
delay1 #(1) d4 (.clk(clk), .ce(ce), .i(xa_gt_xb), .o(xa_gt_xb1) );
171
delay1 #(1) d17(.clk(clk), .ce(ce), .i(a_gt_b), .o(a_gt_b1) );
172
delay1 #(1) d5 (.clk(clk), .ce(ce), .i(realOp), .o(realOp1) );
173
delay1 #(FMSB+2) d5a(.clk(clk), .ce(ce), .i(fracta), .o(fracta1) );
174
delay1 #(FMSB+2) d6a(.clk(clk), .ce(ce), .i(fractb), .o(fractb1) );
175
delay1 #(1) d7 (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
176
delay1 #(1) d8 (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
177
delay1 #(1) d9 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
178
delay1 #(1) d10(.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
179
delay1 #(1) d11(.clk(clk), .ce(ce), .i(op), .o(op1) );
180
 
181
// Sort operands and perform add/subtract
182
// addition can generate an extra bit, subtract can't go negative
183
wire [FMSB+3:0] oa = xa_gt_xb1 ? {fracta1,2'b0} : md1;
184
wire [FMSB+3:0] ob = xa_gt_xb1 ? md1 : {fractb1,2'b0};
185
wire [FMSB+3:0] oaa = a_gt_b1 ? oa : ob;
186
wire [FMSB+3:0] obb = a_gt_b1 ? ob : oa;
187
wire [FMSB+4:0] mab = realOp1 ? oaa - obb : oaa + obb;
188 24 robfinch
wire xoinf = &xo;
189 6 robfinch
 
190
always @*
191 24 robfinch
        casez({aInf1&bInf1,aNan1,bNan1,xoinf})
192
        4'b1???:        mo1 = {1'b0,op1,{FMSB-1{1'b0}},op1,{FMSB{1'b0}}};       // inf +/- inf - generate QNaN on subtract, inf on add
193
        4'b01??:        mo1 = {1'b0,fracta1[FMSB+1:0],{FMSB{1'b0}}};
194
        4'b001?:        mo1 = {1'b0,fractb1[FMSB+1:0],{FMSB{1'b0}}};
195
        4'b0001:        mo1 = 1'd0;
196 8 robfinch
        default:        mo1 = {mab,{FMSB-1{1'b0}}};     // mab has an extra lead bit and two trailing bits
197 6 robfinch
        endcase
198
 
199 8 robfinch
delay1 #(FX+1) d3(.clk(clk), .ce(ce), .i(mo1), .o(mo) );
200 6 robfinch
 
201
endmodule
202
 
203 10 robfinch
module fpAddsubnr(clk, ce, rm, op, a, b, o);
204
parameter WID = 128;
205 26 robfinch
`include "fpSize.sv"
206 10 robfinch
 
207
input clk;              // system clock
208
input ce;               // core clock enable
209
input [2:0] rm;  // rounding mode
210
input op;               // operation 0 = add, 1 = subtract
211
input [MSB:0] a; // operand a
212
input [MSB:0] b; // operand b
213
output [MSB:0] o;        // output
214
 
215
wire [EX:0] o1;
216
wire [MSB+3:0] fpn0;
217
 
218
fpAddsub    #(WID) u1 (clk, ce, rm, op, a, b, o1);
219
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(1'b0), .i(o1), .o(fpn0) );
220
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
221
 
222
endmodule

powered by: WebSVN 2.1.0

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