1 |
6 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2006-2016 Robert Finch, Stratford
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
9 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
10 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
11 |
|
|
// (at your option) any later version.
|
12 |
|
|
//
|
13 |
|
|
// This source file is distributed in the hope that it will be useful,
|
14 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
// GNU General Public License for more details.
|
17 |
|
|
//
|
18 |
|
|
// You should have received a copy of the GNU General Public License
|
19 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20 |
|
|
//
|
21 |
|
|
// fpMul.v
|
22 |
|
|
// - floating point multiplier
|
23 |
|
|
// - two cycle latency
|
24 |
|
|
// - can issue every clock cycle
|
25 |
|
|
// - parameterized width
|
26 |
|
|
// - IEEE 754 representation
|
27 |
|
|
//
|
28 |
|
|
// Floating Point Multiplier
|
29 |
|
|
//
|
30 |
|
|
// Properties:
|
31 |
|
|
// +-inf * +-inf = -+inf (this is handled by exOver)
|
32 |
|
|
// +-inf * 0 = QNaN
|
33 |
|
|
//
|
34 |
|
|
// ============================================================================
|
35 |
|
|
//
|
36 |
|
|
module fpMul (clk, ce, a, b, o, sign_exe, inf, overflow, underflow);
|
37 |
|
|
parameter WID = 32;
|
38 |
|
|
localparam MSB = WID-1;
|
39 |
|
|
localparam EMSB =
|
40 |
|
|
WID==80 ? 14 :
|
41 |
|
|
WID==64 ? 10 :
|
42 |
|
|
WID==52 ? 10 :
|
43 |
|
|
WID==48 ? 10 :
|
44 |
|
|
WID==44 ? 10 :
|
45 |
|
|
WID==42 ? 10 :
|
46 |
|
|
WID==40 ? 9 :
|
47 |
|
|
WID==32 ? 7 :
|
48 |
|
|
WID==24 ? 6 : 4;
|
49 |
|
|
localparam FMSB =
|
50 |
|
|
WID==80 ? 63 :
|
51 |
|
|
WID==64 ? 51 :
|
52 |
|
|
WID==52 ? 39 :
|
53 |
|
|
WID==48 ? 35 :
|
54 |
|
|
WID==44 ? 31 :
|
55 |
|
|
WID==42 ? 29 :
|
56 |
|
|
WID==40 ? 28 :
|
57 |
|
|
WID==32 ? 22 :
|
58 |
|
|
WID==24 ? 15 : 9;
|
59 |
|
|
|
60 |
|
|
localparam WX = 3;
|
61 |
|
|
localparam FX = (FMSB+1)*2-1; // the MSB of the expanded fraction
|
62 |
|
|
localparam EX = FX + WX + EMSB + 1;
|
63 |
|
|
|
64 |
|
|
input clk;
|
65 |
|
|
input ce;
|
66 |
|
|
input [WID:1] a, b;
|
67 |
|
|
output [EX+1:0] o;
|
68 |
|
|
output sign_exe;
|
69 |
|
|
output inf;
|
70 |
|
|
output overflow;
|
71 |
|
|
output underflow;
|
72 |
|
|
|
73 |
|
|
reg [EMSB:0] xo1; // extra bit for sign
|
74 |
|
|
reg [FX+WX:0] mo1;
|
75 |
|
|
|
76 |
|
|
// constants
|
77 |
|
|
wire [EMSB:0] infXp = {EMSB+1{1'b1}}; // infinite / NaN - all ones
|
78 |
|
|
// The following is the value for an exponent of zero, with the offset
|
79 |
|
|
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
|
80 |
|
|
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}}; //2^0 exponent
|
81 |
|
|
// The following is a template for a quiet nan. (MSB=1)
|
82 |
|
|
wire [FMSB:0] qNaN = {1'b1,{FMSB{1'b0}}};
|
83 |
|
|
|
84 |
|
|
// variables
|
85 |
|
|
reg [FX+WX:0] fract1,fract1a;
|
86 |
|
|
wire [FX+WX:0] fracto;
|
87 |
|
|
wire [EMSB+2:0] ex1; // sum of exponents
|
88 |
|
|
wire [EMSB :0] ex2;
|
89 |
|
|
|
90 |
|
|
// Decompose the operands
|
91 |
|
|
wire sa, sb; // sign bit
|
92 |
|
|
wire [EMSB:0] xa, xb; // exponent bits
|
93 |
|
|
wire [FMSB+1:0] fracta, fractb;
|
94 |
|
|
wire a_dn, b_dn; // a/b is denormalized
|
95 |
|
|
wire az, bz;
|
96 |
|
|
wire aInf, bInf, aInf1, bInf1;
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
// -----------------------------------------------------------
|
100 |
|
|
// First clock
|
101 |
|
|
// - decode the input operands
|
102 |
|
|
// - derive basic information
|
103 |
|
|
// - calculate exponent
|
104 |
|
|
// - calculate fraction
|
105 |
|
|
// -----------------------------------------------------------
|
106 |
|
|
|
107 |
|
|
fpDecompose #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf) );
|
108 |
|
|
fpDecompose #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf) );
|
109 |
|
|
|
110 |
|
|
// Compute the sum of the exponents.
|
111 |
|
|
// correct the exponent for denormalized operands
|
112 |
|
|
// adjust the sum by the exponent offset (subtract 127)
|
113 |
|
|
// mul: ex1 = xa + xb, result should always be < 1ffh
|
114 |
|
|
assign ex1 = (az|bz) ? 0 : (xa|a_dn) + (xb|b_dn) - bias;
|
115 |
|
|
generate
|
116 |
|
|
if (WID==64) begin
|
117 |
|
|
reg [35:0] p00,p01,p02;
|
118 |
|
|
reg [35:0] p10,p11,p12;
|
119 |
|
|
reg [35:0] p20,p21,p22;
|
120 |
|
|
always @(posedge clk)
|
121 |
|
|
if (ce) begin
|
122 |
|
|
p00 <= fracta[17: 0] * fractb[17: 0];
|
123 |
|
|
p01 <= fracta[35:18] * fractb[17: 0];
|
124 |
|
|
p02 <= fracta[52:36] * fractb[17: 0];
|
125 |
|
|
p10 <= fracta[17: 0] * fractb[35:18];
|
126 |
|
|
p11 <= fracta[35:18] * fractb[35:18];
|
127 |
|
|
p12 <= fracta[52:36] * fractb[35:18];
|
128 |
|
|
p20 <= fracta[17: 0] * fractb[52:36];
|
129 |
|
|
p21 <= fracta[35:18] * fractb[52:36];
|
130 |
|
|
p22 <= fracta[52:36] * fractb[52:36];
|
131 |
|
|
fract1 <= {p02,36'b0} + {p01,18'b0} + p00 +
|
132 |
|
|
{p12,54'b0} + {p11,36'b0} + {p10,18'b0} +
|
133 |
|
|
{p22,72'b0} + {p21,54'b0} + {p20,36'b0}
|
134 |
|
|
;
|
135 |
|
|
end
|
136 |
|
|
end
|
137 |
|
|
else if (WID==32) begin
|
138 |
|
|
reg [35:0] p00,p01;
|
139 |
|
|
reg [35:0] p10,p11;
|
140 |
|
|
always @(posedge clk)
|
141 |
|
|
if (ce) begin
|
142 |
|
|
p00 <= fracta[17: 0] * fractb[17: 0];
|
143 |
|
|
p01 <= fracta[23:18] * fractb[17: 0];
|
144 |
|
|
p10 <= fracta[17: 0] * fractb[23:18];
|
145 |
|
|
p11 <= fracta[23:18] * fractb[23:18];
|
146 |
|
|
fract1 <= {p11,p00} + {p01,18'b0} + {p10,18'b0};
|
147 |
|
|
end
|
148 |
|
|
end
|
149 |
|
|
endgenerate
|
150 |
|
|
|
151 |
|
|
// Status
|
152 |
|
|
wire under1, over1;
|
153 |
|
|
wire under = ex1[EMSB+2]; // exponent underflow
|
154 |
|
|
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
|
155 |
|
|
|
156 |
|
|
delay2 #(EMSB+1) u3 (.clk(clk), .ce(ce), .i(ex1[EMSB:0]), .o(ex2) );
|
157 |
|
|
delay2 #(FX+WX+1) u4 (.clk(clk), .ce(ce), .i(fract1), .o(fracto) );
|
158 |
|
|
delay2 u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
|
159 |
|
|
delay2 u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
|
160 |
|
|
delay2 u6 (.clk(clk), .ce(ce), .i(under), .o(under1) );
|
161 |
|
|
delay2 u7 (.clk(clk), .ce(ce), .i(over), .o(over1) );
|
162 |
|
|
|
163 |
|
|
// determine when a NaN is output
|
164 |
|
|
wire qNaNOut;
|
165 |
|
|
delay2 u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
// -----------------------------------------------------------
|
169 |
|
|
// Second clock
|
170 |
|
|
// - correct xponent and mantissa for exceptional conditions
|
171 |
|
|
// -----------------------------------------------------------
|
172 |
|
|
|
173 |
|
|
wire so1;
|
174 |
|
|
delay3 u8 (.clk(clk), .ce(ce), .i(sa ^ sb), .o(so1) );// two clock delay!
|
175 |
|
|
|
176 |
|
|
always @(posedge clk)
|
177 |
|
|
if (ce)
|
178 |
|
|
casex({qNaNOut,aInf1,bInf1,over1,under1})
|
179 |
|
|
5'b1xxxx: xo1 = infXp; // qNaN - infinity * zero
|
180 |
|
|
5'b01xxx: xo1 = infXp; // 'a' infinite
|
181 |
|
|
5'b001xx: xo1 = infXp; // 'b' infinite
|
182 |
|
|
5'b0001x: xo1 = infXp; // result overflow
|
183 |
|
|
5'b00001: xo1 = 0; // underflow
|
184 |
|
|
default: xo1 = ex2[EMSB:0]; // situation normal
|
185 |
|
|
endcase
|
186 |
|
|
|
187 |
|
|
always @(posedge clk)
|
188 |
|
|
if (ce)
|
189 |
|
|
casex({qNaNOut,aInf1,bInf1,over1})
|
190 |
|
|
4'b1xxx: mo1 = {1'b0,qNaN|3'd4,{FMSB+1{1'b0}}}; // multiply inf * zero
|
191 |
|
|
4'b01xx: mo1 = 0; // mul inf's
|
192 |
|
|
4'b001x: mo1 = 0; // mul inf's
|
193 |
|
|
4'b0001: mo1 = 0; // mul overflow
|
194 |
|
|
default: mo1 = fracto;
|
195 |
|
|
endcase
|
196 |
|
|
|
197 |
|
|
delay3 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 |
|
|
|
202 |
|
|
assign o = {so1,xo1,mo1};
|
203 |
|
|
|
204 |
|
|
endmodule
|
205 |
|
|
|
206 |
|
|
module fpMul_tb();
|
207 |
|
|
reg clk;
|
208 |
|
|
wire ce = 1'b1;
|
209 |
|
|
wire sgnx1,sgnx2,sgnx3,sgnx4,sgnx5,sgnx6;
|
210 |
|
|
wire inf1,inf2,inf3,inf4,inf5,inf6;
|
211 |
|
|
wire of1,of2,of3,of4,of5,of6;
|
212 |
|
|
wire uf1,uf2,uf3,uf4,uf5,uf6;
|
213 |
|
|
wire [57:0] o1,o2,o3,o4,o5,o6;
|
214 |
|
|
wire [35:0] o11,o12,o13;
|
215 |
|
|
wire [31:0] o21,o22,o23;
|
216 |
|
|
|
217 |
|
|
initial begin
|
218 |
|
|
clk = 0;
|
219 |
|
|
end
|
220 |
|
|
always #10 clk <= ~clk;
|
221 |
|
|
|
222 |
|
|
fpMul u1 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o1), .sign_exe(sgnx1), .inf(inf1), .overflow(of1), .underflow(uf1));
|
223 |
|
|
fpMul u2 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o2), .sign_exe(sgnx2), .inf(inf2), .overflow(of2), .underflow(uf2));
|
224 |
|
|
// 10x10
|
225 |
|
|
fpMul u3 (.clk(clk), .ce(1'b1), .a(32'h41200000), .b(32'h41200000), .o(o3), .sign_exe(sgnx2), .inf(inf2), .overflow(of2), .underflow(uf2));
|
226 |
|
|
// 21*-17
|
227 |
|
|
fpMul u4 (.clk(clk), .ce(1'b1), .a(32'h41a80000), .b(32'hc1880000), .o(o4), .sign_exe(sgnx2), .inf(inf2), .overflow(of2), .underflow(uf2));
|
228 |
|
|
// -17*-15
|
229 |
|
|
fpMul u5 (.clk(clk), .ce(1'b1), .a(32'hc1880000), .b(32'hc1700000), .o(o5), .sign_exe(sgnx2), .inf(inf2), .overflow(of2), .underflow(uf2));
|
230 |
|
|
|
231 |
|
|
fpNormalize u11 (clk, ce, 1'b0, o3, o11);
|
232 |
|
|
fpNormalize u12 (clk, ce, 1'b0, o4, o12);
|
233 |
|
|
fpNormalize u13 (clk, ce, 1'b0, o5, o13);
|
234 |
|
|
|
235 |
|
|
fpRound u21 (3'd1, o11, o21); // zero for zero
|
236 |
|
|
fpRound u22 (3'd1, o12, o22); //
|
237 |
|
|
fpRound u23 (3'd1, o13, o23); //
|
238 |
|
|
|
239 |
|
|
endmodule
|