1 |
6 |
robfinch |
// ===============================================================
|
2 |
|
|
// (C) 2006 Robert Finch
|
3 |
|
|
// All rights reserved.
|
4 |
|
|
// rob@birdcomputer.ca
|
5 |
|
|
//
|
6 |
|
|
// fpMul.v
|
7 |
|
|
// - floating point multiplier
|
8 |
|
|
// - two cycle latency
|
9 |
|
|
// - can issue every clock cycle
|
10 |
|
|
// - parameterized width
|
11 |
|
|
// - IEEE 754 representation
|
12 |
|
|
//
|
13 |
|
|
// This source code is free for use and modification for
|
14 |
|
|
// non-commercial or evaluation purposes, provided this
|
15 |
|
|
// copyright statement and disclaimer remains present in
|
16 |
|
|
// the file.
|
17 |
|
|
//
|
18 |
|
|
// If the code is modified, please state the origin and
|
19 |
|
|
// note that the code has been modified.
|
20 |
|
|
//
|
21 |
|
|
// NO WARRANTY.
|
22 |
|
|
// THIS Work, IS PROVIDEDED "AS IS" WITH NO WARRANTIES OF
|
23 |
|
|
// ANY KIND, WHETHER EXPRESS OR IMPLIED. The user must assume
|
24 |
|
|
// the entire risk of using the Work.
|
25 |
|
|
//
|
26 |
|
|
// IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
27 |
|
|
// ANY INCIDENTAL, CONSEQUENTIAL, OR PUNITIVE DAMAGES
|
28 |
|
|
// WHATSOEVER RELATING TO THE USE OF THIS WORK, OR YOUR
|
29 |
|
|
// RELATIONSHIP WITH THE AUTHOR.
|
30 |
|
|
//
|
31 |
|
|
// IN ADDITION, IN NO EVENT DOES THE AUTHOR AUTHORIZE YOU
|
32 |
|
|
// TO USE THE WORK IN APPLICATIONS OR SYSTEMS WHERE THE
|
33 |
|
|
// WORK'S FAILURE TO PERFORM CAN REASONABLY BE EXPECTED
|
34 |
|
|
// TO RESULT IN A SIGNIFICANT PHYSICAL INJURY, OR IN LOSS
|
35 |
|
|
// OF LIFE. ANY SUCH USE BY YOU IS ENTIRELY AT YOUR OWN RISK,
|
36 |
|
|
// AND YOU AGREE TO HOLD THE AUTHOR AND CONTRIBUTORS HARMLESS
|
37 |
|
|
// FROM ANY CLAIMS OR LOSSES RELATING TO SUCH UNAUTHORIZED
|
38 |
|
|
// USE.
|
39 |
|
|
//
|
40 |
|
|
// This multiplier/divider handles denormalized numbers.
|
41 |
|
|
// The output format is of an internal expanded representation
|
42 |
|
|
// in preparation to be fed into a normalization unit, then
|
43 |
|
|
// rounding. Basically, it's the same as the regular format
|
44 |
|
|
// except the mantissa is doubled in size, the leading two
|
45 |
|
|
// bits of which are assumed to be whole bits.
|
46 |
|
|
//
|
47 |
|
|
//
|
48 |
|
|
// Floating Point Multiplier
|
49 |
|
|
//
|
50 |
|
|
// Properties:
|
51 |
|
|
// +-inf * +-inf = -+inf (this is handled by exOver)
|
52 |
|
|
// +-inf * 0 = QNaN
|
53 |
|
|
//
|
54 |
|
|
// 1 sign number
|
55 |
|
|
// 8 exponent
|
56 |
|
|
// 48 mantissa
|
57 |
|
|
//
|
58 |
|
|
// Ref: Webpack8.1i Spartan3-4 xc3s1000-4ft256
|
59 |
|
|
// 174 LUTS / 113 slices / 24.7 ns
|
60 |
|
|
// 4 Mults
|
61 |
|
|
//=============================================================== */
|
62 |
|
|
|
63 |
|
|
module fpMul (clk, ce, a, b, o, sign_exe, inf, overflow, underflow);
|
64 |
|
|
parameter WID = 32;
|
65 |
|
|
localparam MSB = WID-1;
|
66 |
|
|
localparam EMSB = WID==80 ? 14 :
|
67 |
|
|
WID==64 ? 10 :
|
68 |
|
|
WID==52 ? 10 :
|
69 |
|
|
WID==48 ? 10 :
|
70 |
|
|
WID==44 ? 10 :
|
71 |
|
|
WID==42 ? 10 :
|
72 |
|
|
WID==40 ? 9 :
|
73 |
|
|
WID==32 ? 7 :
|
74 |
|
|
WID==24 ? 6 : 4;
|
75 |
|
|
localparam FMSB = WID==80 ? 63 :
|
76 |
|
|
WID==64 ? 51 :
|
77 |
|
|
WID==52 ? 39 :
|
78 |
|
|
WID==48 ? 35 :
|
79 |
|
|
WID==44 ? 31 :
|
80 |
|
|
WID==42 ? 29 :
|
81 |
|
|
WID==40 ? 28 :
|
82 |
|
|
WID==32 ? 22 :
|
83 |
|
|
WID==24 ? 15 : 9;
|
84 |
|
|
|
85 |
|
|
localparam FX = (FMSB+2)*2-1; // the MSB of the expanded fraction
|
86 |
|
|
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
|
87 |
|
|
|
88 |
|
|
input clk;
|
89 |
|
|
input ce;
|
90 |
|
|
input [WID:1] a, b;
|
91 |
|
|
output [EX:0] o;
|
92 |
|
|
output sign_exe;
|
93 |
|
|
output inf;
|
94 |
|
|
output overflow;
|
95 |
|
|
output underflow;
|
96 |
|
|
|
97 |
|
|
reg [EMSB:0] xo1; // extra bit for sign
|
98 |
|
|
reg [FX:0] mo1;
|
99 |
|
|
|
100 |
|
|
// constants
|
101 |
|
|
wire [EMSB:0] infXp = {EMSB+1{1'b1}}; // infinite / NaN - all ones
|
102 |
|
|
// The following is the value for an exponent of zero, with the offset
|
103 |
|
|
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
|
104 |
|
|
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}}; //2^0 exponent
|
105 |
|
|
// The following is a template for a quiet nan. (MSB=1)
|
106 |
|
|
wire [FMSB:0] qNaN = {1'b1,{FMSB{1'b0}}};
|
107 |
|
|
|
108 |
|
|
// variables
|
109 |
|
|
reg [FX:0] fract1,fract1a;
|
110 |
|
|
wire [FX:0] fracto;
|
111 |
|
|
wire [EMSB+2:0] ex1; // sum of exponents
|
112 |
|
|
wire [EMSB :0] ex2;
|
113 |
|
|
|
114 |
|
|
// Decompose the operands
|
115 |
|
|
wire sa, sb; // sign bit
|
116 |
|
|
wire [EMSB:0] xa, xb; // exponent bits
|
117 |
|
|
wire [FMSB+1:0] fracta, fractb;
|
118 |
|
|
wire a_dn, b_dn; // a/b is denormalized
|
119 |
|
|
wire az, bz;
|
120 |
|
|
wire aInf, bInf, aInf1, bInf1;
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
// -----------------------------------------------------------
|
124 |
|
|
// First clock
|
125 |
|
|
// - decode the input operands
|
126 |
|
|
// - derive basic information
|
127 |
|
|
// - calculate exponent
|
128 |
|
|
// - calculate fraction
|
129 |
|
|
// -----------------------------------------------------------
|
130 |
|
|
|
131 |
|
|
fpDecomp #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf) );
|
132 |
|
|
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf) );
|
133 |
|
|
|
134 |
|
|
// Compute the sum of the exponents.
|
135 |
|
|
// correct the exponent for denormalized operands
|
136 |
|
|
// adjust the sum by the exponent offset (subtract 127)
|
137 |
|
|
// mul: ex1 = xa + xb, result should always be < 1ffh
|
138 |
|
|
assign ex1 = (az|bz) ? 0 : (xa|a_dn) + (xb|b_dn) - bias;
|
139 |
|
|
generate
|
140 |
|
|
if (WID==64) begin
|
141 |
|
|
reg [35:0] p00,p01,p02;
|
142 |
|
|
reg [35:0] p10,p11,p12;
|
143 |
|
|
reg [35:0] p20,p21,p22;
|
144 |
|
|
always @(posedge clk)
|
145 |
|
|
if (ce) begin
|
146 |
|
|
p00 <= fracta[17: 0] * fractb[17: 0];
|
147 |
|
|
p01 <= fracta[35:18] * fractb[17: 0];
|
148 |
|
|
p02 <= fracta[52:36] * fractb[17: 0];
|
149 |
|
|
p10 <= fracta[17: 0] * fractb[35:18];
|
150 |
|
|
p11 <= fracta[35:18] * fractb[35:18];
|
151 |
|
|
p12 <= fracta[52:36] * fractb[35:18];
|
152 |
|
|
p20 <= fracta[17: 0] * fractb[52:36];
|
153 |
|
|
p21 <= fracta[35:18] * fractb[52:36];
|
154 |
|
|
p22 <= fracta[52:36] * fractb[52:36];
|
155 |
|
|
fract1 <= {p02,36'b0} + {p01,18'b0} + p00 +
|
156 |
|
|
{p12,54'b0} + {p11,36'b0} + {p10,18'b0} +
|
157 |
|
|
{p22,72'b0} + {p21,54'b0} + {p20,36'b0}
|
158 |
|
|
;
|
159 |
|
|
end
|
160 |
|
|
end
|
161 |
|
|
else if (WID==32) begin
|
162 |
|
|
reg [35:0] p00,p01;
|
163 |
|
|
reg [35:0] p10,p11;
|
164 |
|
|
always @(posedge clk)
|
165 |
|
|
if (ce) begin
|
166 |
|
|
p00 <= fracta[17: 0] * fractb[17: 0];
|
167 |
|
|
p01 <= fracta[23:18] * fractb[17: 0];
|
168 |
|
|
p10 <= fracta[17: 0] * fractb[23:18];
|
169 |
|
|
p11 <= fracta[23:18] * fractb[23:18];
|
170 |
|
|
fract1 <= {p11,p00} + {p01,18'b0} + {p10,18'b0};
|
171 |
|
|
end
|
172 |
|
|
end
|
173 |
|
|
endgenerate
|
174 |
|
|
|
175 |
|
|
// Status
|
176 |
|
|
wire under1, over1;
|
177 |
|
|
wire under = ex1[EMSB+2]; // exponent underflow
|
178 |
|
|
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
|
179 |
|
|
|
180 |
|
|
delay2 #(EMSB) u3 (.clk(clk), .ce(ce), .i(ex1[EMSB:0]), .o(ex2) );
|
181 |
|
|
delay2 #(FX+1) u4 (.clk(clk), .ce(ce), .i(fract1), .o(fracto) );
|
182 |
|
|
delay2 u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
|
183 |
|
|
delay2 u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
|
184 |
|
|
delay2 u6 (.clk(clk), .ce(ce), .i(under), .o(under1) );
|
185 |
|
|
delay2 u7 (.clk(clk), .ce(ce), .i(over), .o(over1) );
|
186 |
|
|
|
187 |
|
|
// determine when a NaN is output
|
188 |
|
|
wire qNaNOut;
|
189 |
|
|
delay2 u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
// -----------------------------------------------------------
|
193 |
|
|
// Second clock
|
194 |
|
|
// - correct xponent and mantissa for exceptional conditions
|
195 |
|
|
// -----------------------------------------------------------
|
196 |
|
|
|
197 |
|
|
wire so1;
|
198 |
|
|
delay3 u8 (.clk(clk), .ce(ce), .i(sa ^ sb), .o(so1) );// two clock delay!
|
199 |
|
|
|
200 |
|
|
always @(posedge clk)
|
201 |
|
|
if (ce)
|
202 |
|
|
casex({qNaNOut,aInf1,bInf1,over1,under1})
|
203 |
|
|
5'b1xxxx: xo1 = infXp; // qNaN - infinity * zero
|
204 |
|
|
5'b01xxx: xo1 = infXp; // 'a' infinite
|
205 |
|
|
5'b001xx: xo1 = infXp; // 'b' infinite
|
206 |
|
|
5'b0001x: xo1 = infXp; // result overflow
|
207 |
|
|
5'b00001: xo1 = 0; // underflow
|
208 |
|
|
default: xo1 = ex2[EMSB:0]; // situation normal
|
209 |
|
|
endcase
|
210 |
|
|
|
211 |
|
|
always @(posedge clk)
|
212 |
|
|
if (ce)
|
213 |
|
|
casex({qNaNOut,aInf1,bInf1,over1})
|
214 |
|
|
4'b1xxx: mo1 = {1'b0,qNaN|3'd4,{FMSB+1{1'b0}}}; // multiply inf * zero
|
215 |
|
|
4'b01xx: mo1 = 0; // mul inf's
|
216 |
|
|
4'b001x: mo1 = 0; // mul inf's
|
217 |
|
|
4'b0001: mo1 = 0; // mul overflow
|
218 |
|
|
default: mo1 = fracto;
|
219 |
|
|
endcase
|
220 |
|
|
|
221 |
|
|
delay3 u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
|
222 |
|
|
delay1 u11 (.clk(clk), .ce(ce), .i(over1), .o(overflow) );
|
223 |
|
|
delay1 u12 (.clk(clk), .ce(ce), .i(over1), .o(inf) );
|
224 |
|
|
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
|
225 |
|
|
|
226 |
|
|
assign o = {so1,xo1,mo1};
|
227 |
|
|
|
228 |
|
|
endmodule
|
229 |
|
|
|
230 |
|
|
module fpMul_tb();
|
231 |
|
|
reg clk;
|
232 |
|
|
|
233 |
|
|
initial begin
|
234 |
|
|
clk = 0;
|
235 |
|
|
end
|
236 |
|
|
always #10 clk <= ~clk;
|
237 |
|
|
|
238 |
|
|
fpMul u1 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o1), .sign_exe(sgnx1), .inf(inf1), .overflow(of1), .underflow(uf1));
|
239 |
|
|
fpMul u2 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o1), .sign_exe(sgnx1), .inf(inf1), .overflow(of1), .underflow(uf1));
|
240 |
|
|
|
241 |
|
|
endmodule
|