1 |
67 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2006-2022 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// DFPDivide128.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 DFPPkg::*;
|
48 |
|
|
|
49 |
|
|
`define QINFDIV 4'd2
|
50 |
|
|
`define QZEROZERO 4'd3
|
51 |
|
|
|
52 |
|
|
module DFPDivide128(rst, clk, ce, ld, op, a, b, o, done, sign_exe, overflow, underflow);
|
53 |
|
|
parameter N=34;
|
54 |
|
|
// FADD is a constant that makes the divider width a multiple of four and includes eight extra bits.
|
55 |
|
|
input rst;
|
56 |
|
|
input clk;
|
57 |
|
|
input ce;
|
58 |
|
|
input ld;
|
59 |
|
|
input op;
|
60 |
|
|
input DFP128 a, b;
|
61 |
|
|
output DFP128UD o;
|
62 |
|
|
output reg done;
|
63 |
|
|
output sign_exe;
|
64 |
|
|
output overflow;
|
65 |
|
|
output underflow;
|
66 |
|
|
|
67 |
|
|
// registered outputs
|
68 |
|
|
reg sign_exe=0;
|
69 |
|
|
reg inf=0;
|
70 |
|
|
reg overflow=0;
|
71 |
|
|
reg underflow=0;
|
72 |
|
|
|
73 |
|
|
reg so, sxo;
|
74 |
|
|
reg [13:0] xo;
|
75 |
|
|
reg [(N+1)*4*2-1:0] mo;
|
76 |
|
|
|
77 |
|
|
DFP128U au, bu;
|
78 |
|
|
DFPUnpack128 u01 (a, au);
|
79 |
|
|
DFPUnpack128 u02 (b, bu);
|
80 |
|
|
|
81 |
|
|
// constants
|
82 |
|
|
wire [13:0] infXp = 13'h2FFF; // infinite / NaN - all ones
|
83 |
|
|
wire [13:0] bias = 14'h17FF;
|
84 |
|
|
// The following is the value for an exponent of zero, with the offset
|
85 |
|
|
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
|
86 |
|
|
// The following is a template for a quiet nan. (MSB=1)
|
87 |
|
|
wire [N*4-1:0] qNaN = {4'h1,{(N-1)*4{1'b0}}};
|
88 |
|
|
|
89 |
|
|
// variables
|
90 |
|
|
wire [(N+2)*4*2-1:0] divo;
|
91 |
|
|
|
92 |
|
|
// Operands
|
93 |
|
|
reg sa, sb; // sign bit
|
94 |
|
|
reg [N*4-1:0] siga, sigb;
|
95 |
|
|
reg az, bz;
|
96 |
|
|
reg aInf, bInf;
|
97 |
|
|
reg aNan,bNan;
|
98 |
|
|
wire done1;
|
99 |
|
|
wire signed [7:0] lzcnt;
|
100 |
|
|
|
101 |
|
|
// -----------------------------------------------------------
|
102 |
|
|
// Clock #1
|
103 |
|
|
// - decode the input operands
|
104 |
|
|
// - derive basic information
|
105 |
|
|
// - calculate fraction
|
106 |
|
|
// -----------------------------------------------------------
|
107 |
|
|
reg ld1;
|
108 |
|
|
always @(posedge clk)
|
109 |
|
|
if (ce) sa <= au.sign;
|
110 |
|
|
always @(posedge clk)
|
111 |
|
|
if (ce) sb <= bu.sign;
|
112 |
|
|
always @(posedge clk)
|
113 |
|
|
if (ce) siga <= au.sig;
|
114 |
|
|
always @(posedge clk)
|
115 |
|
|
if (ce) sigb <= bu.sig;
|
116 |
|
|
always @(posedge clk)
|
117 |
|
|
if (ce) az <= au.exp==14'd0 && au.sig==136'd0;
|
118 |
|
|
always @(posedge clk)
|
119 |
|
|
if (ce) bz <= bu.exp==14'd0 && bu.sig==136'd0;
|
120 |
|
|
always @(posedge clk)
|
121 |
|
|
if (ce) aInf <= au.infinity;
|
122 |
|
|
always @(posedge clk)
|
123 |
|
|
if (ce) bInf <= bu.infinity;
|
124 |
|
|
always @(posedge clk)
|
125 |
|
|
if (ce) aNan <= au.nan;
|
126 |
|
|
always @(posedge clk)
|
127 |
|
|
if (ce) bNan <= bu.nan;
|
128 |
|
|
ft_delay #(.WID(1), .DEP(1)) udly1 (.clk(clk), .ce(ce), .i(ld), .o(ld1));
|
129 |
|
|
|
130 |
|
|
// -----------------------------------------------------------
|
131 |
|
|
// Clock #2 to N
|
132 |
|
|
// - calculate fraction
|
133 |
|
|
// -----------------------------------------------------------
|
134 |
|
|
wire done3a,done3;
|
135 |
|
|
// Perform divide
|
136 |
|
|
dfdiv #(N+2) u2 (.clk(clk), .ld(ld1), .a({siga,8'b0}), .b({sigb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
|
137 |
|
|
wire [7:0] lzcnt_bin = lzcnt[3:0] + (lzcnt[7:4] * 10);
|
138 |
|
|
wire [(N+2)*4*2-1:0] divo1 = divo[(N+2)*4*2-1:0] << ({lzcnt_bin,2'b0}+N*4);//WAS FPWID=128?+44
|
139 |
|
|
ft_delay #(.WID(1), .DEP(3)) u3 (.clk(clk), .ce(ce), .i(done1), .o(done3a));
|
140 |
|
|
assign done3 = done1&done3a;
|
141 |
|
|
|
142 |
|
|
// -----------------------------------------------------------
|
143 |
|
|
// Clock #N+1
|
144 |
|
|
// - calculate exponent
|
145 |
|
|
// - calculate fraction
|
146 |
|
|
// - determine when a NaN is output
|
147 |
|
|
// -----------------------------------------------------------
|
148 |
|
|
// Compute the exponent.
|
149 |
|
|
// - correct the exponent for denormalized operands
|
150 |
|
|
// - adjust the difference by the bias (add 127)
|
151 |
|
|
// - also factor in the different decimal position for division
|
152 |
|
|
reg [15:0] ex1; // sum of exponents
|
153 |
|
|
reg qNaNOut;
|
154 |
|
|
|
155 |
|
|
always @(posedge clk)
|
156 |
|
|
if (ce) ex1 <= au.exp - bu.exp + bias - lzcnt_bin;
|
157 |
|
|
|
158 |
|
|
always @(posedge clk)
|
159 |
|
|
if (ce) qNaNOut <= (az&bz)|(aInf&bInf);
|
160 |
|
|
|
161 |
|
|
wire over = 1'b0;
|
162 |
|
|
wire under = &ex1[15:14];
|
163 |
|
|
reg [3:0] st;
|
164 |
|
|
|
165 |
|
|
// -----------------------------------------------------------
|
166 |
|
|
// Clock #N+3
|
167 |
|
|
// -----------------------------------------------------------
|
168 |
|
|
always @(posedge clk)
|
169 |
|
|
// Simulation likes to see these values reset to zero on reset. Otherwise the
|
170 |
|
|
// values propagate in sim as X's.
|
171 |
|
|
if (rst) begin
|
172 |
|
|
xo <= 1'd0;
|
173 |
|
|
mo <= 1'd0;
|
174 |
|
|
so <= 1'd0;
|
175 |
|
|
sign_exe <= 1'd0;
|
176 |
|
|
overflow <= 1'd0;
|
177 |
|
|
underflow <= 1'd0;
|
178 |
|
|
done <= 1'b1;
|
179 |
|
|
end
|
180 |
|
|
else if (ce) begin
|
181 |
|
|
done <= 1'b0;
|
182 |
|
|
if (done3&done1) begin
|
183 |
|
|
done <= 1'b1;
|
184 |
|
|
|
185 |
|
|
casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
|
186 |
|
|
5'b1????: xo <= infXp; // NaN exponent value
|
187 |
|
|
5'b01???: xo <= 1'd0; // divide by inf
|
188 |
|
|
5'b001??: xo <= infXp; // divide by zero
|
189 |
|
|
5'b0001?: xo <= infXp; // overflow
|
190 |
|
|
5'b00001: xo <= 1'd0; // underflow
|
191 |
|
|
default: xo <= ex1; // normal or underflow: passthru neg. exp. for normalization
|
192 |
|
|
endcase
|
193 |
|
|
|
194 |
|
|
casez({aNan,bNan,qNaNOut,bInf,bz,over,aInf&bInf,az&bz})
|
195 |
|
|
8'b1???????: begin mo <= {4'h1,au[N*4-1:0],{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end
|
196 |
|
|
8'b01??????: begin mo <= {4'h1,bu[N*4-1:0],{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end
|
197 |
|
|
8'b001?????: begin mo <= {4'h1,qNaN[N*4-1:0]|{aInf,1'b0}|{az,bz},{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end
|
198 |
|
|
8'b0001????: begin mo <= {(N+1)*4*2-1{1'd0}}; st[3] <= 1'b0; end // div by inf
|
199 |
|
|
8'b00001???: begin mo <= {(N+1)*4*2-1{1'd0}}; st[3] <= 1'b0; end // div by zero
|
200 |
|
|
8'b000001??: begin mo <= {(N+1)*4*2-1{1'd0}}; st[3] <= 1'b0; end // Inf exponent
|
201 |
|
|
8'b0000001?: begin mo <= {4'h1,qNaN|`QINFDIV,{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end // infinity / infinity
|
202 |
|
|
8'b00000001: begin mo <= {4'h1,qNaN|`QZEROZERO,{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end // zero / zero
|
203 |
|
|
default: begin mo <= divo1[(N+2)*4*2-1:8]; st[3] <= 1'b0; end // plain div
|
204 |
|
|
endcase
|
205 |
|
|
|
206 |
|
|
sign_exe <= sa & sb;
|
207 |
|
|
overflow <= over;
|
208 |
|
|
underflow <= under;
|
209 |
|
|
|
210 |
|
|
o.nan <= aNan|bNan|qNaNOut;
|
211 |
|
|
o.snan <= aNan|bNan|qNaNOut;
|
212 |
|
|
o.qnan <= 1'b0;
|
213 |
|
|
o.infinity <= over|aInf;
|
214 |
|
|
o.sign <= sa ^ sb;
|
215 |
|
|
o.exp <= xo;
|
216 |
|
|
o.sig <= mo;
|
217 |
|
|
end
|
218 |
|
|
end
|
219 |
|
|
|
220 |
|
|
endmodule
|
221 |
|
|
|
222 |
|
|
module DFPDivide128nr(rst, clk, ce, ld, op, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
|
223 |
|
|
parameter N=34;
|
224 |
|
|
input rst;
|
225 |
|
|
input clk;
|
226 |
|
|
input ce;
|
227 |
|
|
input ld;
|
228 |
|
|
input op;
|
229 |
|
|
input DFP128 a, b;
|
230 |
|
|
output DFP128 o;
|
231 |
|
|
input [2:0] rm;
|
232 |
|
|
output sign_exe;
|
233 |
|
|
output done;
|
234 |
|
|
output inf;
|
235 |
|
|
output overflow;
|
236 |
|
|
output underflow;
|
237 |
|
|
|
238 |
|
|
DFP128UD o1;
|
239 |
|
|
wire sign_exe1, inf1, overflow1, underflow1;
|
240 |
|
|
DFP128UN fpn0;
|
241 |
|
|
wire done1, done1a;
|
242 |
|
|
|
243 |
|
|
DFPDivide128 #(.N(N)) u1 (rst, clk, ce, ld, op, a, b, o1, done1, sign_exe1, overflow1, underflow1);
|
244 |
|
|
DFPNormalize128 #(.N(N)) u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
|
245 |
|
|
DFPRound128 #(.N(N)) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
|
246 |
|
|
delay2 #(1) u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
|
247 |
|
|
delay2 #(1) u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
|
248 |
|
|
delay2 #(1) u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
|
249 |
|
|
delay2 #(1) u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
|
250 |
|
|
ft_delay #(.WID(1),.DEP(11)) u8(.clk(clk), .ce(ce), .i(done1), .o(done1a));
|
251 |
|
|
assign done = done1&done1a;
|
252 |
|
|
|
253 |
|
|
endmodule
|
254 |
|
|
|