1 |
64 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2020-2022 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// DFPMultiply128.v
|
9 |
|
|
// - decimal floating point multiplier
|
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 |
|
|
//
|
40 |
|
|
// Floating Point Multiplier
|
41 |
|
|
//
|
42 |
|
|
// Properties:
|
43 |
|
|
// +-inf * +-inf = -+inf (this is handled by exOver)
|
44 |
|
|
// +-inf * 0 = QNaN
|
45 |
|
|
//
|
46 |
|
|
// ============================================================================
|
47 |
|
|
|
48 |
|
|
import DFPPkg::*;
|
49 |
|
|
|
50 |
|
|
//`define DFPMUL_PARALLEL 1'b1
|
51 |
|
|
|
52 |
|
|
module DFPMultiply128(clk, ce, ld, a, b, o, sign_exe, inf, overflow, underflow, done);
|
53 |
|
|
localparam N=34;
|
54 |
|
|
localparam DELAY = 2;
|
55 |
|
|
input clk;
|
56 |
|
|
input ce;
|
57 |
|
|
input ld;
|
58 |
|
|
input DFP128 a, b;
|
59 |
|
|
output DFP128UD o;
|
60 |
|
|
output sign_exe;
|
61 |
|
|
output inf;
|
62 |
|
|
output overflow;
|
63 |
|
|
output underflow;
|
64 |
|
|
output done;
|
65 |
|
|
|
66 |
|
|
reg [13:0] xo1; // extra bit for sign
|
67 |
|
|
reg [N*4*2-1:0] mo1;
|
68 |
|
|
|
69 |
|
|
// constants
|
70 |
|
|
wire [13:0] infXp = 14'h2FFF; // infinite / NaN - all ones
|
71 |
|
|
wire [13:0] bias = 14'h17FF;
|
72 |
|
|
// The following is the value for an exponent of zero, with the offset
|
73 |
|
|
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
|
74 |
|
|
// The following is a template for a quiet nan. (MSB=1)
|
75 |
|
|
wire [N*4-1:0] qNaN = {4'h1,{104{1'b0}}};
|
76 |
|
|
|
77 |
|
|
// variables
|
78 |
|
|
reg [N*4*2-1:0] sig1;
|
79 |
|
|
wire [13:0] ex2;
|
80 |
|
|
|
81 |
|
|
DFP128U au, bu;
|
82 |
|
|
DFPUnpack128 u01 (a, au);
|
83 |
|
|
DFPUnpack128 u02 (b, bu);
|
84 |
|
|
|
85 |
|
|
// Decompose the operands
|
86 |
|
|
wire sa, sb; // sign bit
|
87 |
|
|
wire [13:0] xa, xb; // exponent bits
|
88 |
|
|
wire sxa, sxb;
|
89 |
|
|
wire [N*4-1:0] siga, sigb;
|
90 |
|
|
wire a_dn, b_dn; // a/b is denormalized
|
91 |
|
|
wire aNan1, bNan1;
|
92 |
|
|
wire az, bz;
|
93 |
|
|
wire aInf1, bInf1;
|
94 |
|
|
|
95 |
|
|
assign siga = au.sig;
|
96 |
|
|
assign sigb = bu.sig;
|
97 |
|
|
assign az = au.exp==14'h0 && au.sig==136'd0;
|
98 |
|
|
assign bz = bu.exp==14'h0 && bu.sig==136'd0;
|
99 |
|
|
|
100 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
101 |
|
|
// Clock #1
|
102 |
|
|
// - decode the input operands
|
103 |
|
|
// - derive basic information
|
104 |
|
|
// - calculate exponent
|
105 |
|
|
// - calculate fraction
|
106 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
107 |
|
|
|
108 |
|
|
// -----------------------------------------------------------
|
109 |
|
|
// First clock
|
110 |
|
|
// Compute the sum of the exponents.
|
111 |
|
|
// -----------------------------------------------------------
|
112 |
|
|
|
113 |
|
|
wire under, over;
|
114 |
|
|
wire [15:0] sum_ex = au.exp + bu.exp - bias;
|
115 |
|
|
reg [15:0] sum_ex;
|
116 |
|
|
reg sx0;
|
117 |
|
|
wire done1;
|
118 |
|
|
assign under = &sum_ex[15:14];
|
119 |
|
|
assign over = sum_ex > 16'h2FFF;
|
120 |
|
|
|
121 |
|
|
wire [N*4*2-1:0] sigoo;
|
122 |
|
|
`ifdef DFPMUL_PARALLEL
|
123 |
|
|
BCDMul32 u1f (.a({20'h0,siga}),.b({20'h0,sigb}),.o(sigoo));
|
124 |
|
|
`else
|
125 |
|
|
dfmul #(.N(N)) u1g
|
126 |
|
|
(
|
127 |
|
|
.clk(clk),
|
128 |
|
|
.ld(ld),
|
129 |
|
|
.a(siga),
|
130 |
|
|
.b(sigb),
|
131 |
|
|
.p(sigoo),
|
132 |
|
|
.done(done1)
|
133 |
|
|
);
|
134 |
|
|
`endif
|
135 |
|
|
|
136 |
|
|
always @(posedge clk)
|
137 |
|
|
if (ce) sig1 <= sigoo[N*4*2-1:0];
|
138 |
|
|
|
139 |
|
|
// Status
|
140 |
|
|
wire under1, over1;
|
141 |
|
|
|
142 |
|
|
ft_delay #(.WID(14),.DEP(DELAY)) u3 (.clk(clk), .ce(ce), .i(sum_ex[13:0]), .o(ex2) );
|
143 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u2a (.clk(clk), .ce(ce), .i(au.infinity), .o(aInf1) );
|
144 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u2b (.clk(clk), .ce(ce), .i(bu.infinity), .o(bInf1) );
|
145 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u6 (.clk(clk), .ce(ce), .i(under), .o(under1) );
|
146 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u7 (.clk(clk), .ce(ce), .i(over), .o(over1) );
|
147 |
|
|
|
148 |
|
|
// determine when a NaN is output
|
149 |
|
|
wire qNaNOut;
|
150 |
|
|
wire DFP128U a1,b1;
|
151 |
|
|
wire asnan, bsnan, aqnan, bqnan;
|
152 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u5 (.clk(clk), .ce(ce), .i((au.infinity&bz)|(bu.infinity&az)), .o(qNaNOut) );
|
153 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u14 (.clk(clk), .ce(ce), .i(au.nan), .o(aNan1) );
|
154 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u15 (.clk(clk), .ce(ce), .i(bu.nan), .o(bNan1) );
|
155 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u18 (.clk(clk), .ce(ce), .i(au.snan), .o(asnan) );
|
156 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u19 (.clk(clk), .ce(ce), .i(bu.snan), .o(bsnan) );
|
157 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u18a (.clk(clk), .ce(ce), .i(au.qnan), .o(aqnan) );
|
158 |
|
|
ft_delay #(.WID(1),.DEP(DELAY)) u19a (.clk(clk), .ce(ce), .i(bu.qnan), .o(bqnan) );
|
159 |
|
|
ft_delay #(.WID($bits(a1)),.DEP(DELAY)) u16 (.clk(clk), .ce(ce), .i(a), .o(a1) );
|
160 |
|
|
ft_delay #(.WID($bits(b1)),.DEP(DELAY)) u17 (.clk(clk), .ce(ce), .i(b), .o(b1) );
|
161 |
|
|
|
162 |
|
|
// -----------------------------------------------------------
|
163 |
|
|
// Second clock
|
164 |
|
|
// - correct xponent and mantissa for exceptional conditions
|
165 |
|
|
// -----------------------------------------------------------
|
166 |
|
|
|
167 |
|
|
wire so1, sx1;
|
168 |
|
|
reg [3:0] st;
|
169 |
|
|
wire done1a;
|
170 |
|
|
|
171 |
|
|
ft_delay #(.WID(1),.DEP(1)) u8 (.clk(clk), .ce(ce), .i(au.sign ^ bu.sign), .o(so1) );// two clock delay!
|
172 |
|
|
|
173 |
|
|
always @(posedge clk)
|
174 |
|
|
if (ce)
|
175 |
|
|
casez({qNaNOut|aNan1|bNan1,aInf1,bInf1,over1,under1})
|
176 |
|
|
5'b1????: xo1 = infXp; // qNaN - infinity * zero
|
177 |
|
|
5'b01???: xo1 = infXp; // 'a' infinite
|
178 |
|
|
5'b001??: xo1 = infXp; // 'b' infinite
|
179 |
|
|
5'b0001?: xo1 = infXp; // result overflow
|
180 |
|
|
5'b00001: xo1 = ex2[13:0];//0; // underflow
|
181 |
|
|
default: xo1 = ex2[13:0]; // situation normal
|
182 |
|
|
endcase
|
183 |
|
|
|
184 |
|
|
// Force mantissa to zero when underflow or zero exponent when not supporting denormals.
|
185 |
|
|
always @(posedge clk)
|
186 |
|
|
if (ce)
|
187 |
|
|
casez({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1|under1})
|
188 |
|
|
6'b1?????: mo1 = {4'h1,a1[N*4-4-1:0],{N*4{1'b0}}};
|
189 |
|
|
6'b01????: mo1 = {4'h1,b1[N*4-4-1:0],{N*4{1'b0}}};
|
190 |
|
|
6'b001???: mo1 = {4'h1,qNaN|3'd4,{N*4{1'b0}}}; // multiply inf * zero
|
191 |
|
|
6'b0001??: mo1 = 0; // mul inf's
|
192 |
|
|
6'b00001?: mo1 = 0; // mul inf's
|
193 |
|
|
6'b000001: mo1 = 0; // mul overflow
|
194 |
|
|
default: mo1 = sig1;
|
195 |
|
|
endcase
|
196 |
|
|
|
197 |
|
|
ft_delay #(.WID(1),.DEP(DELAY+1)) u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
|
198 |
|
|
delay1 u11 (.clk(clk), .ce(ce), .i(over1), .o(overflow) );
|
199 |
|
|
delay1 u12 (.clk(clk), .ce(ce), .i(over1), .o(inf) );
|
200 |
|
|
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
|
201 |
|
|
ft_delay #(.WID(1),.DEP(3)) u18b (.clk(clk), .ce(ce), .i(done1), .o(done1a) );
|
202 |
|
|
|
203 |
|
|
assign o.nan = aNan1|bNan1|qNaNOut;
|
204 |
|
|
assign o.qnan = qNaNOut|aqnan|bqnan;
|
205 |
|
|
assign o.snan = qNaNOut ? 1'b0 : asnan|bsnan;
|
206 |
|
|
assign o.infinity = aInf1|bInf1|over;
|
207 |
|
|
assign o.sign = so1;
|
208 |
|
|
assign o.exp = xo1;
|
209 |
|
|
assign o.sig = {mo1,8'h00};
|
210 |
|
|
assign done = done1&done1a;
|
211 |
|
|
|
212 |
|
|
endmodule
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
// Multiplier with normalization and rounding.
|
216 |
|
|
|
217 |
|
|
module DFPMultiply128nr(clk, ce, ld, a, b, o, rm, sign_exe, inf, overflow, underflow, done);
|
218 |
|
|
localparam N=34;
|
219 |
|
|
input clk;
|
220 |
|
|
input ce;
|
221 |
|
|
input ld;
|
222 |
|
|
input DFP128 a, b;
|
223 |
|
|
output DFP128 o;
|
224 |
|
|
input [2:0] rm;
|
225 |
|
|
output sign_exe;
|
226 |
|
|
output inf;
|
227 |
|
|
output overflow;
|
228 |
|
|
output underflow;
|
229 |
|
|
output done;
|
230 |
|
|
|
231 |
|
|
wire done1, done1a;
|
232 |
|
|
DFP128UD o1;
|
233 |
|
|
wire sign_exe1, inf1, overflow1, underflow1;
|
234 |
|
|
DFP128UN fpn0;
|
235 |
|
|
|
236 |
|
|
DFPMultiply128 u1 (clk, ce, ld, a, b, o1, sign_exe1, inf1, overflow1, underflow1, done1);
|
237 |
|
|
DFPNormalize128 u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
|
238 |
|
|
DFPRound128 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 |
|
|
ft_delay #(.WID(1),.DEP(12)) u10 (.clk(clk), .ce(ce), .i(done1), .o(done1a) );
|
244 |
|
|
assign done = done1 & done1a;
|
245 |
|
|
|
246 |
|
|
endmodule
|