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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPDivide.sv] - Blame information for rev 54

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

Line No. Rev Author Line
1 54 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      DFPDivide.sv
9
//    - decimal floating point divider
10
//    - parameterized width
11
//
12
//
13
// BSD 3-Clause License
14
// Redistribution and use in source and binary forms, with or without
15
// modification, are permitted provided that the following conditions are met:
16
//
17
// 1. Redistributions of source code must retain the above copyright notice, this
18
//    list of conditions and the following disclaimer.
19
//
20
// 2. Redistributions in binary form must reproduce the above copyright notice,
21
//    this list of conditions and the following disclaimer in the documentation
22
//    and/or other materials provided with the distribution.
23
//
24
// 3. Neither the name of the copyright holder nor the names of its
25
//    contributors may be used to endorse or promote products derived from
26
//    this software without specific prior written permission.
27
//
28
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
//
39
//      Floating Point Divider
40
//
41
//Properties:
42
//+-inf * +-inf = -+inf    (this is handled by exOver)
43
//+-inf * 0     = QNaN
44
//+-0 / +-0      = QNaN
45
// ============================================================================
46
 
47
import fp::*;
48
 
49
module DFPDivide(rst, clk, ce, ld, op, a, b, o, done, sign_exe, overflow, underflow);
50
// FADD is a constant that makes the divider width a multiple of four and includes eight extra bits.
51
input rst;
52
input clk;
53
input ce;
54
input ld;
55
input op;
56
input [127:0] a, b;
57
output [243:0] o;
58
output reg done;
59
output sign_exe;
60
output overflow;
61
output underflow;
62
 
63
// registered outputs
64
reg sign_exe=0;
65
reg inf=0;
66
reg     overflow=0;
67
reg     underflow=0;
68
 
69
reg so, sxo;
70
reg [3:0] st;
71
reg [15:0] xo;
72
reg [223:0] mo;
73
assign o = {st,xo,mo};
74
 
75
// constants
76
wire [15:0] infXp = 16'h9999;   // infinite / NaN - all ones
77
// The following is the value for an exponent of zero, with the offset
78
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
79
// The following is a template for a quiet nan. (MSB=1)
80
wire [107:0] qNaN  = {4'h1,{104{1'b0}}};
81
 
82
// variables
83
wire [231:0] divo;
84
 
85
// Operands
86
wire sa, sb;                    // sign bit
87
wire sxa, sxb;
88
wire [15:0] xa, xb;     // exponent bits
89
wire [107:0] siga, sigb;
90
wire a_dn, b_dn;                        // a/b is denormalized
91
wire az, bz;
92
wire aInf, bInf;
93
wire aNan,bNan;
94
wire done1;
95
wire signed [7:0] lzcnt;
96
 
97
// -----------------------------------------------------------
98
// Clock #1
99
// - decode the input operands
100
// - derive basic information
101
// - calculate fraction
102
// -----------------------------------------------------------
103
reg ld1;
104
DFPDecomposeReg u1a (.clk(clk), .ce(ce), .i(a), .sgn(sa), .sx(sxa), .exp(xa), .sig(siga), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
105
DFPDecomposeReg u1b (.clk(clk), .ce(ce), .i(b), .sgn(sb), .sx(sxb), .exp(xb), .sig(sigb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
106
delay #(.WID(1), .DEP(1)) udly1 (.clk(clk), .ce(ce), .i(ld), .o(ld1));
107
 
108
// -----------------------------------------------------------
109
// Clock #2 to N
110
// - calculate fraction
111
// -----------------------------------------------------------
112
wire done3a,done3;
113
// Perform divide
114
dfdiv #(108+8) u2 (.clk(clk), .ld(ld1), .a({siga,8'b0}), .b({sigb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
115
wire [7:0] lzcnt_bin = lzcnt[3:0] + (lzcnt[7:4] * 10);
116
wire [231:0] divo1 = divo[231:0] << ({lzcnt_bin,2'b0}+(FPWID+44));
117
delay #(.WID(1), .DEP(3)) u3 (.clk(clk), .ce(ce), .i(done1), .o(done3a));
118
assign done3 = done1&done3a;
119
 
120
// -----------------------------------------------------------
121
// Clock #N+1
122
// - calculate exponent
123
// - calculate fraction
124
// - determine when a NaN is output
125
// -----------------------------------------------------------
126
// Compute the exponent.
127
// - correct the exponent for denormalized operands
128
// - adjust the difference by the bias (add 127)
129
// - also factor in the different decimal position for division
130
reg [15:0] ex2, ex1, ex2a, ex2b;        // sum of exponents
131
reg qNaNOut;
132
reg under1, under;
133
reg over1, over;
134
wire [15:0] xapxb, xamxb, xbmxa;
135
wire xapxbc, xamxbc, xbmxac;
136
reg sxo0;
137
 
138
BCDAddN #(.N(4)) u5 (.ci(1'b0), .a(xa), .b(xb), .o(xapxb), .co(xapxbc) );
139
BCDSubN #(.N(4)) u6 (.ci(1'b0), .a(xa), .b(xb), .o(xamxb), .co(xamxbc) );
140
BCDSubN #(.N(4)) u7 (.ci(1'b0), .a(xb), .b(xa), .o(xbmxa), .co(xbmxac) );
141
BCDSubN #(.N(5)) u10 (.ci(1'b0), .a(20'h10000), .b(ex2a), .o(ex2b), .co() );
142
 
143
always @*
144
case ({sxa,sxb})
145
2'b11:  begin ex2a <= xbmxa; sxo0 <= ~xbmxac; over1 <= 1'b0; under1 <= 1'b0; end
146
2'b10:  begin ex2a <= xapxb; sxo0 <= 1'b1; over1 <= xapxbc; under1 <= 1'b0; end
147
2'b01:  begin ex2a <= xapxb; sxo0 <= 1'b0; over1 <= 1'b0; under1 <= xapxbc; end
148
2'b00:  begin ex2a <= xamxb; sxo0 <= ~xamxbc; over1 <= 1'b0; under1 <= 1'b0; end
149
endcase
150
 
151
always @*
152
if (~sxo0 && ~(sa^sb))
153
        ex2 <= ex2b;
154
else
155
        ex2 <= ex2a;
156
 
157
wire [15:0] ex1a, ex1b, ex1d;
158
reg [15:0] ex1c;
159
wire sxoa, sxob, sxoc;
160
 
161
BCDAddN #(.N(4)) u8 (.ci(1'b0), .a(ex2), .b({8'h00,lzcnt}), .o(ex1a), .co(sxoa) );
162
BCDSubN #(.N(4)) u9 (.ci(1'b0), .a(ex2), .b({8'h00,lzcnt}), .o(ex1b), .co(sxob) );
163
BCDSubN #(.N(5)) u11 (.ci(1'b0), .a(20'h10000), .b(ex1c), .o(ex1d), .co() );
164
 
165
always @(posedge clk)
166
case(sxo0)
167
2'd1:   begin ex1c <= ex1b; sxo <= ~sxob; over <= over1; under <= under1; end
168
2'd0:   begin ex1c <= ex1a; sxo <= 1'b0; over <= over1;  under <= under1|sxob; end
169
endcase
170
 
171
always @*
172
if (sxo0 & sxob)        // There was a borrow on a subtract, making the number negative
173
        ex1 <= ex1d;
174
else
175
        ex1 <= ex1c;
176
 
177
 
178
always @(posedge clk)
179
  if (ce) qNaNOut <= (az&bz)|(aInf&bInf);
180
 
181
// -----------------------------------------------------------
182
// Clock #N+3
183
// -----------------------------------------------------------
184
always @(posedge clk)
185
// Simulation likes to see these values reset to zero on reset. Otherwise the
186
// values propagate in sim as X's.
187
if (rst) begin
188
        xo <= 1'd0;
189
        mo <= 1'd0;
190
        so <= 1'd0;
191
        sign_exe <= 1'd0;
192
        overflow <= 1'd0;
193
        underflow <= 1'd0;
194
        done <= 1'b1;
195
end
196
else if (ce) begin
197
  done <= 1'b0;
198
        if (done3&done1) begin
199
          done <= 1'b1;
200
 
201
                casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
202
                5'b1????:               xo <= infXp;    // NaN exponent value
203
                5'b01???:               xo <= 1'd0;             // divide by inf
204
                5'b001??:               xo <= infXp;    // divide by zero
205
                5'b0001?:               xo <= infXp;    // overflow
206
                5'b00001:               xo <= 1'd0;             // underflow
207
                default:                xo <= ex1;      // normal or underflow: passthru neg. exp. for normalization
208
                endcase
209
 
210
                casez({aNan,bNan,qNaNOut,bInf,bz,over,aInf&bInf,az&bz})
211
                8'b1???????:  begin mo <= {4'h1,a[107:0],{111{1'b0}}}; st[3] <= 1'b1; end
212
                8'b01??????:  begin mo <= {4'h1,b[107:0],{111{1'b0}}}; st[3] <= 1'b1; end
213
                8'b001?????:    begin mo <= {4'h1,qNaN[107:0]|{aInf,1'b0}|{az,bz},{1111{1'b0}}}; st[3] <= 1'b1; end
214
                8'b0001????:    begin mo <= 224'd0;     st[3] <= 1'b0; end      // div by inf
215
                8'b00001???:    begin mo <= 224'd0;     st[3] <= 1'b0; end      // div by zero
216
                8'b000001??:    begin mo <= 224'd0;     st[3] <= 1'b0; end      // Inf exponent
217
                8'b0000001?:    begin mo <= {4'h1,qNaN|`QINFDIV,{111{1'b0}}};   st[3] <= 1'b1; end      // infinity / infinity
218
                8'b00000001:    begin mo <= {4'h1,qNaN|`QZEROZERO,{111{1'b0}}}; st[3] <= 1'b1; end      // zero / zero
219
                default:                begin mo <= divo1[231:8];       st[3] <= 1'b0; end      // plain div
220
                endcase
221
 
222
                st[0] <= sxo;
223
                st[1] <= aInf;
224
                st[2] <= ~(sa ^ sb);
225
                so              <= ~(sa ^ sb);
226
                sign_exe        <= sa & sb;
227
                overflow        <= over;
228
                underflow       <= under;
229
        end
230
end
231
 
232
endmodule
233
 
234
module DFPDividenr(rst, clk, ce, ld, op, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
235
input rst;
236
input clk;
237
input ce;
238
input ld;
239
input op;
240
input  [127:0] a, b;
241
output [127:0] o;
242
input [2:0] rm;
243
output sign_exe;
244
output done;
245
output inf;
246
output overflow;
247
output underflow;
248
 
249
wire [243:0] o1;
250
wire sign_exe1, inf1, overflow1, underflow1;
251
wire [131:0] fpn0;
252
wire done1, done1a;
253
 
254
DFPDivide    #(FPWID) u1 (rst, clk, ce, ld, op, a, b, o1, done1, sign_exe1, overflow1, underflow1);
255
DFPNormalize #(FPWID) u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
256
DFPRound     #(FPWID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
257
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
258
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
259
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
260
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
261
delay   #(.WID(1),.DEP(11))   u8(.clk(clk), .ce(ce), .i(done1), .o(done1a));
262
assign done = done1&done1a;
263
 
264
endmodule
265
 

powered by: WebSVN 2.1.0

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