1 |
43 |
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 |
|
|
// fpDiv.v
|
10 |
|
|
// - floating point divider
|
11 |
|
|
// - parameterized width
|
12 |
|
|
// - IEEE 754 representation
|
13 |
|
|
//
|
14 |
|
|
//
|
15 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
16 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
17 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
18 |
|
|
// (at your option) any later version.
|
19 |
|
|
//
|
20 |
|
|
// This source file is distributed in the hope that it will be useful,
|
21 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23 |
|
|
// GNU General Public License for more details.
|
24 |
|
|
//
|
25 |
|
|
// You should have received a copy of the GNU General Public License
|
26 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
27 |
|
|
//
|
28 |
|
|
// Floating Point Multiplier / Divider
|
29 |
|
|
//
|
30 |
|
|
//Properties:
|
31 |
|
|
//+-inf * +-inf = -+inf (this is handled by exOver)
|
32 |
|
|
//+-inf * 0 = QNaN
|
33 |
|
|
//+-0 / +-0 = QNaN
|
34 |
|
|
// ============================================================================
|
35 |
|
|
|
36 |
|
|
`include "fp_defines.v"
|
37 |
|
|
|
38 |
|
|
module fpDiv(clk, clk4x, ce, ld, op, a, b, o, done, sign_exe, overflow, underflow);
|
39 |
|
|
|
40 |
|
|
parameter WID = 128;
|
41 |
|
|
localparam MSB = WID-1;
|
42 |
|
|
localparam EMSB = WID==128 ? 14 :
|
43 |
|
|
WID==96 ? 14 :
|
44 |
|
|
WID==80 ? 14 :
|
45 |
|
|
WID==64 ? 10 :
|
46 |
|
|
WID==52 ? 10 :
|
47 |
|
|
WID==48 ? 11 :
|
48 |
|
|
WID==44 ? 10 :
|
49 |
|
|
WID==42 ? 10 :
|
50 |
|
|
WID==40 ? 9 :
|
51 |
|
|
WID==32 ? 7 :
|
52 |
|
|
WID==24 ? 6 : 4;
|
53 |
|
|
localparam FMSB = WID==128 ? 111 :
|
54 |
|
|
WID==96 ? 79 :
|
55 |
|
|
WID==80 ? 63 :
|
56 |
|
|
WID==64 ? 51 :
|
57 |
|
|
WID==52 ? 39 :
|
58 |
|
|
WID==48 ? 34 :
|
59 |
|
|
WID==44 ? 31 :
|
60 |
|
|
WID==42 ? 29 :
|
61 |
|
|
WID==40 ? 28 :
|
62 |
|
|
WID==32 ? 22 :
|
63 |
|
|
WID==24 ? 15 : 9;
|
64 |
|
|
// FADD is a constant that makes the divider width a multiple of four and includes eight extra bits.
|
65 |
|
|
localparam FADD = WID==128 ? 9 :
|
66 |
|
|
WID==96 ? 9 :
|
67 |
|
|
WID==80 ? 9 :
|
68 |
|
|
WID==64 ? 13 :
|
69 |
|
|
WID==52 ? 9 :
|
70 |
|
|
WID==48 ? 10 :
|
71 |
|
|
WID==44 ? 9 :
|
72 |
|
|
WID==42 ? 11 :
|
73 |
|
|
WID==40 ? 8 :
|
74 |
|
|
WID==32 ? 10 :
|
75 |
|
|
WID==24 ? 9 : 11;
|
76 |
|
|
|
77 |
|
|
localparam FX = (FMSB+2)*2-1; // the MSB of the expanded fraction
|
78 |
|
|
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
|
79 |
|
|
|
80 |
|
|
input clk;
|
81 |
|
|
input clk4x;
|
82 |
|
|
input ce;
|
83 |
|
|
input ld;
|
84 |
|
|
input op;
|
85 |
|
|
input [MSB:0] a, b;
|
86 |
|
|
output [EX:0] o;
|
87 |
|
|
output done;
|
88 |
|
|
output sign_exe;
|
89 |
|
|
output overflow;
|
90 |
|
|
output underflow;
|
91 |
|
|
|
92 |
|
|
// registered outputs
|
93 |
|
|
reg sign_exe;
|
94 |
|
|
reg inf;
|
95 |
|
|
reg overflow;
|
96 |
|
|
reg underflow;
|
97 |
|
|
|
98 |
|
|
reg so;
|
99 |
|
|
reg [EMSB:0] xo;
|
100 |
|
|
reg [FX:0] mo;
|
101 |
|
|
assign o = {so,xo,mo};
|
102 |
|
|
|
103 |
|
|
// constants
|
104 |
|
|
wire [EMSB:0] infXp = {EMSB+1{1'b1}}; // infinite / NaN - all ones
|
105 |
|
|
// The following is the value for an exponent of zero, with the offset
|
106 |
|
|
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
|
107 |
|
|
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}}; //2^0 exponent
|
108 |
|
|
// The following is a template for a quiet nan. (MSB=1)
|
109 |
|
|
wire [FMSB:0] qNaN = {1'b1,{FMSB{1'b0}}};
|
110 |
|
|
|
111 |
|
|
// variables
|
112 |
|
|
wire [EMSB+2:0] ex1; // sum of exponents
|
113 |
|
|
wire [(FMSB+FADD)*2-1:0] divo;
|
114 |
|
|
|
115 |
|
|
// Operands
|
116 |
|
|
wire sa, sb; // sign bit
|
117 |
|
|
wire [EMSB:0] xa, xb; // exponent bits
|
118 |
|
|
wire [FMSB+1:0] fracta, fractb;
|
119 |
|
|
wire a_dn, b_dn; // a/b is denormalized
|
120 |
|
|
wire az, bz;
|
121 |
|
|
wire aInf, bInf;
|
122 |
|
|
wire aNan,bNan;
|
123 |
|
|
wire done1;
|
124 |
|
|
wire [7:0] lzcnt;
|
125 |
|
|
|
126 |
|
|
// -----------------------------------------------------------
|
127 |
|
|
// - decode the input operands
|
128 |
|
|
// - derive basic information
|
129 |
|
|
// - calculate exponent
|
130 |
|
|
// - calculate fraction
|
131 |
|
|
// -----------------------------------------------------------
|
132 |
|
|
|
133 |
|
|
fpDecomp #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
|
134 |
|
|
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
|
135 |
|
|
|
136 |
|
|
// Compute the exponent.
|
137 |
|
|
// - correct the exponent for denormalized operands
|
138 |
|
|
// - adjust the difference by the bias (add 127)
|
139 |
|
|
// - also factor in the different decimal position for division
|
140 |
|
|
assign ex1 = (xa|a_dn) - (xb|b_dn) + bias + FMSB + (FADD-1) - lzcnt;
|
141 |
|
|
|
142 |
|
|
// check for exponent underflow/overflow
|
143 |
|
|
wire under = ex1[EMSB+2]; // MSB set = negative exponent
|
144 |
|
|
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
|
145 |
|
|
|
146 |
|
|
// Perform divide
|
147 |
|
|
// Divider width must be a multiple of four
|
148 |
|
|
fpdivr16 #(FMSB+FADD) u2 (.clk(clk), .ld(ld), .a({3'b0,fracta,8'b0}), .b({3'b0,fractb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
|
149 |
|
|
wire [(FMSB+FADD)*2-1:0] divo1 = divo[(FMSB+FADD)*2-1:0] << (lzcnt-2);
|
150 |
|
|
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(done1), .o(done));
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
// determine when a NaN is output
|
154 |
|
|
wire qNaNOut = (az&bz)|(aInf&bInf);
|
155 |
|
|
|
156 |
|
|
always @(posedge clk)
|
157 |
|
|
if (ce) begin
|
158 |
|
|
if (done1) begin
|
159 |
|
|
casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
|
160 |
|
|
5'b1????: xo = infXp; // NaN exponent value
|
161 |
|
|
5'b01???: xo = 0; // divide by inf
|
162 |
|
|
5'b001??: xo = infXp; // divide by zero
|
163 |
|
|
5'b0001?: xo = infXp; // overflow
|
164 |
|
|
5'b00001: xo = 0; // underflow
|
165 |
|
|
default: xo = ex1; // normal or underflow: passthru neg. exp. for normalization
|
166 |
|
|
endcase
|
167 |
|
|
|
168 |
|
|
casez({aNan,bNan,qNaNOut,bInf,bz,over,aInf&bInf,az&bz})
|
169 |
|
|
8'b1???????: mo = {1'b1,a[FMSB:0],{FMSB+1{1'b0}}};
|
170 |
|
|
8'b01??????: mo = {1'b1,b[FMSB:0],{FMSB+1{1'b0}}};
|
171 |
|
|
8'b001?????: mo = {1'b1,qNaN[FMSB:0]|{aInf,1'b0}|{az,bz},{FMSB+1{1'b0}}};
|
172 |
|
|
8'b0001????: mo = 0; // div by inf
|
173 |
|
|
8'b00001???: mo = 0; // div by zero
|
174 |
|
|
8'b000001??: mo = 0; // Inf exponent
|
175 |
|
|
8'b0000001?: mo = {1'b1,qNaN|`QINFDIV,{FMSB+1{1'b0}}}; // infinity / infinity
|
176 |
|
|
8'b00000001: mo = {1'b1,qNaN|`QZEROZERO,{FMSB+1{1'b0}}}; // zero / zero
|
177 |
|
|
default: mo = divo1[(FMSB+FADD)*2-1:(FADD-2)*2-2]; // plain div
|
178 |
|
|
endcase
|
179 |
|
|
|
180 |
|
|
so = sa ^ sb;
|
181 |
|
|
sign_exe = sa & sb;
|
182 |
|
|
overflow = over;
|
183 |
|
|
underflow = under;
|
184 |
|
|
end
|
185 |
|
|
end
|
186 |
|
|
|
187 |
|
|
endmodule
|
188 |
|
|
|
189 |
|
|
module fpDivnr(clk, clk4x, ce, ld, op, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
|
190 |
|
|
parameter WID=32;
|
191 |
|
|
localparam MSB = WID-1;
|
192 |
|
|
localparam EMSB = WID==128 ? 14 :
|
193 |
|
|
WID==96 ? 14 :
|
194 |
|
|
WID==80 ? 14 :
|
195 |
|
|
WID==64 ? 10 :
|
196 |
|
|
WID==52 ? 10 :
|
197 |
|
|
WID==48 ? 11 :
|
198 |
|
|
WID==44 ? 10 :
|
199 |
|
|
WID==42 ? 10 :
|
200 |
|
|
WID==40 ? 9 :
|
201 |
|
|
WID==32 ? 7 :
|
202 |
|
|
WID==24 ? 6 : 4;
|
203 |
|
|
localparam FMSB = WID==128 ? 111 :
|
204 |
|
|
WID==96 ? 79 :
|
205 |
|
|
WID==80 ? 63 :
|
206 |
|
|
WID==64 ? 51 :
|
207 |
|
|
WID==52 ? 39 :
|
208 |
|
|
WID==48 ? 34 :
|
209 |
|
|
WID==44 ? 31 :
|
210 |
|
|
WID==42 ? 29 :
|
211 |
|
|
WID==40 ? 28 :
|
212 |
|
|
WID==32 ? 22 :
|
213 |
|
|
WID==24 ? 15 : 9;
|
214 |
|
|
|
215 |
|
|
localparam FX = (FMSB+2)*2-1; // the MSB of the expanded fraction
|
216 |
|
|
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
|
217 |
|
|
input clk;
|
218 |
|
|
input clk4x;
|
219 |
|
|
input ce;
|
220 |
|
|
input ld;
|
221 |
|
|
input op;
|
222 |
|
|
input [MSB:0] a, b;
|
223 |
|
|
output [MSB:0] o;
|
224 |
|
|
input [2:0] rm;
|
225 |
|
|
output sign_exe;
|
226 |
|
|
output done;
|
227 |
|
|
output inf;
|
228 |
|
|
output overflow;
|
229 |
|
|
output underflow;
|
230 |
|
|
|
231 |
|
|
wire [EX:0] o1;
|
232 |
|
|
wire sign_exe1, inf1, overflow1, underflow1;
|
233 |
|
|
wire [MSB+3:0] fpn0;
|
234 |
|
|
wire done1;
|
235 |
|
|
|
236 |
|
|
fpDiv #(WID) u1 (clk, clk4x, ce, ld, op, a, b, o1, done1, sign_exe1, overflow1, underflow1);
|
237 |
|
|
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(underflow1), .i(o1), .o(fpn0) );
|
238 |
|
|
fpRoundReg #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
|
239 |
|
|
delay2 #(1) u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
|
240 |
|
|
delay2 #(1) u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
|
241 |
|
|
delay2 #(1) u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
|
242 |
|
|
delay2 #(1) u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
|
243 |
|
|
delay2 #(1) u8(.clk(clk), .ce(ce), .i(done1), .o(done));
|
244 |
|
|
endmodule
|
245 |
|
|
|