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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [rtl/] [fpUnit/] [fpAddsub.v] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 51 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2006-2018  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch<remove>@finitron.ca
7
//       ||
8
//
9
//      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
// 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
//                                                                          
30
// ============================================================================
31
 
32
module fpAddsub(clk, ce, rm, op, a, b, o);
33
parameter WID = 128;
34
localparam MSB = WID-1;
35
localparam EMSB = WID==128 ? 14 :
36
                  WID==96 ? 14 :
37
                  WID==80 ? 14 :
38
                  WID==64 ? 10 :
39
                                  WID==52 ? 10 :
40
                                  WID==48 ? 11 :
41
                                  WID==44 ? 10 :
42
                                  WID==42 ? 10 :
43
                                  WID==40 ?  9 :
44
                                  WID==32 ?  7 :
45
                                  WID==24 ?  6 : 4;
46
localparam FMSB = WID==128 ? 111 :
47
                  WID==96 ? 79 :
48
                  WID==80 ? 63 :
49
                  WID==64 ? 51 :
50
                                  WID==52 ? 39 :
51
                                  WID==48 ? 34 :
52
                                  WID==44 ? 31 :
53
                                  WID==42 ? 29 :
54
                                  WID==40 ? 28 :
55
                                  WID==32 ? 22 :
56
                                  WID==24 ? 15 : 9;
57
 
58
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
59
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
60
 
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
output [EX:0] o; // output
68
 
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
wire [FX:0] mo;  // mantissa output
75
reg [FX:0] mo1;  // mantissa output
76
 
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
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
 
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
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
 
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
 
212
always @*
213
        casez({aInf1&bInf1,aNan1,bNan1})
214
        3'b1??:         mo1 = {1'b0,op1,{FMSB-1{1'b0}},op1,{FMSB{1'b0}}};       // inf +/- inf - generate QNaN on subtract, inf on add
215
        3'b01?:         mo1 = {1'b0,fracta1[FMSB+1:0],{FMSB{1'b0}}};
216
        3'b001:         mo1 = {1'b0,fractb1[FMSB+1:0],{FMSB{1'b0}}};
217
        default:        mo1 = {mab,{FMSB-1{1'b0}}};     // mab has an extra lead bit and two trailing bits
218
        endcase
219
 
220
delay1 #(FX+1) d3(.clk(clk), .ce(ce), .i(mo1), .o(mo) );
221
 
222
endmodule
223
 
224
module fpAddsubnr(clk, ce, rm, op, a, b, o);
225
parameter WID = 128;
226
localparam MSB = WID-1;
227
localparam EMSB = WID==128 ? 14 :
228
                  WID==96 ? 14 :
229
                  WID==80 ? 14 :
230
                  WID==64 ? 10 :
231
                                  WID==52 ? 10 :
232
                                  WID==48 ? 11 :
233
                                  WID==44 ? 10 :
234
                                  WID==42 ? 10 :
235
                                  WID==40 ?  9 :
236
                                  WID==32 ?  7 :
237
                                  WID==24 ?  6 : 4;
238
localparam FMSB = WID==128 ? 111 :
239
                  WID==96 ? 79 :
240
                  WID==80 ? 63 :
241
                  WID==64 ? 51 :
242
                                  WID==52 ? 39 :
243
                                  WID==48 ? 34 :
244
                                  WID==44 ? 31 :
245
                                  WID==42 ? 29 :
246
                                  WID==40 ? 28 :
247
                                  WID==32 ? 22 :
248
                                  WID==24 ? 15 : 9;
249
 
250
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
251
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
252
 
253
input clk;              // system clock
254
input ce;               // core clock enable
255
input [2:0] rm;  // rounding mode
256
input op;               // operation 0 = add, 1 = subtract
257
input [MSB:0] a; // operand a
258
input [MSB:0] b; // operand b
259
output [MSB:0] o;        // output
260
 
261
wire [EX:0] o1;
262
wire [MSB+3:0] fpn0;
263
 
264
fpAddsub    #(WID) u1 (clk, ce, rm, op, a, b, o1);
265
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(1'b0), .i(o1), .o(fpn0) );
266
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
267
 
268
endmodule

powered by: WebSVN 2.1.0

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