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

Subversion Repositories ft816float

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

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

powered by: WebSVN 2.1.0

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