OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpMul.v] - Blame information for rev 11

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 robfinch
`timescale 1ns / 1ps
2 6 robfinch
// ============================================================================
3
//        __
4 10 robfinch
//   \\__/ o\    (C) 2006-2018  Robert Finch, Waterloo
5 6 robfinch
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch<remove>@finitron.ca
7
//       ||
8
//
9 8 robfinch
//      fpMul.v
10
//              - floating point multiplier
11
//              - two cycle latency
12
//              - can issue every clock cycle
13
//              - parameterized width
14
//              - IEEE 754 representation
15
//
16
//
17 6 robfinch
// This source file is free software: you can redistribute it and/or modify 
18
// it under the terms of the GNU Lesser General Public License as published 
19
// by the Free Software Foundation, either version 3 of the License, or     
20
// (at your option) any later version.                                      
21
//                                                                          
22
// This source file is distributed in the hope that it will be useful,      
23
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
24
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
25
// GNU General Public License for more details.                             
26
//                                                                          
27
// You should have received a copy of the GNU General Public License        
28
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
29 8 robfinch
//                                                                          
30
//      Floating Point Multiplier / Divider
31 6 robfinch
//
32 8 robfinch
//      This multiplier/divider handles denormalized numbers.
33
//      The output format is of an internal expanded representation
34
//      in preparation to be fed into a normalization unit, then
35
//      rounding. Basically, it's the same as the regular format
36
//      except the mantissa is doubled in size, the leading two
37
//      bits of which are assumed to be whole bits.
38 6 robfinch
//
39 8 robfinch
//
40 6 robfinch
//      Floating Point Multiplier
41
//
42
//      Properties:
43
//      +-inf * +-inf = -+inf   (this is handled by exOver)
44
//      +-inf * 0     = QNaN
45
//      
46 8 robfinch
//      1 sign number
47
//      8 exponent
48
//      48 mantissa
49
//
50 6 robfinch
// ============================================================================
51 8 robfinch
 
52 6 robfinch
module fpMul (clk, ce, a, b, o, sign_exe, inf, overflow, underflow);
53 8 robfinch
parameter WID = 128;
54 6 robfinch
localparam MSB = WID-1;
55 8 robfinch
localparam EMSB = WID==128 ? 14 :
56
                  WID==96 ? 14 :
57
                  WID==80 ? 14 :
58
                  WID==64 ? 10 :
59 6 robfinch
                                  WID==52 ? 10 :
60 10 robfinch
                                  WID==48 ? 11 :
61 6 robfinch
                                  WID==44 ? 10 :
62
                                  WID==42 ? 10 :
63
                                  WID==40 ?  9 :
64
                                  WID==32 ?  7 :
65
                                  WID==24 ?  6 : 4;
66 8 robfinch
localparam FMSB = WID==128 ? 111 :
67
                  WID==96 ? 79 :
68
                  WID==80 ? 63 :
69
                  WID==64 ? 51 :
70 6 robfinch
                                  WID==52 ? 39 :
71 10 robfinch
                                  WID==48 ? 34 :
72 6 robfinch
                                  WID==44 ? 31 :
73
                                  WID==42 ? 29 :
74
                                  WID==40 ? 28 :
75
                                  WID==32 ? 22 :
76
                                  WID==24 ? 15 : 9;
77
 
78 8 robfinch
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
79
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
80 6 robfinch
 
81
input clk;
82
input ce;
83
input  [WID:1] a, b;
84 8 robfinch
output [EX:0] o;
85 6 robfinch
output sign_exe;
86
output inf;
87
output overflow;
88
output underflow;
89
 
90
reg [EMSB:0] xo1;                // extra bit for sign
91 8 robfinch
reg [FX:0] mo1;
92 6 robfinch
 
93
// constants
94
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
95
// The following is the value for an exponent of zero, with the offset
96
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
97
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
98
// The following is a template for a quiet nan. (MSB=1)
99
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
100
 
101
// variables
102 8 robfinch
reg [FX:0] fract1,fract1a;
103
wire [FX:0] fracto;
104 6 robfinch
wire [EMSB+2:0] ex1;     // sum of exponents
105
wire [EMSB  :0] ex2;
106
 
107
// Decompose the operands
108
wire sa, sb;                    // sign bit
109
wire [EMSB:0] xa, xb;    // exponent bits
110
wire [FMSB+1:0] fracta, fractb;
111
wire a_dn, b_dn;                        // a/b is denormalized
112 8 robfinch
wire aNan, bNan, aNan1, bNan1;
113 6 robfinch
wire az, bz;
114
wire aInf, bInf, aInf1, bInf1;
115
 
116
 
117
// -----------------------------------------------------------
118
// First clock
119
// - decode the input operands
120
// - derive basic information
121
// - calculate exponent
122
// - calculate fraction
123
// -----------------------------------------------------------
124
 
125 8 robfinch
fpDecomp #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
126
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
127 6 robfinch
 
128
// Compute the sum of the exponents.
129
// correct the exponent for denormalized operands
130
// adjust the sum by the exponent offset (subtract 127)
131
// mul: ex1 = xa + xb,  result should always be < 1ffh
132
assign ex1 = (az|bz) ? 0 : (xa|a_dn) + (xb|b_dn) - bias;
133 8 robfinch
 
134 10 robfinch
generate
135
if (WID==64) begin
136 8 robfinch
reg [35:0] p00,p01,p02;
137
reg [35:0] p10,p11,p12;
138
reg [35:0] p20,p21,p22;
139 6 robfinch
        always @(posedge clk)
140
        if (ce) begin
141
                p00 <= fracta[17: 0] * fractb[17: 0];
142
                p01 <= fracta[35:18] * fractb[17: 0];
143
                p02 <= fracta[52:36] * fractb[17: 0];
144
                p10 <= fracta[17: 0] * fractb[35:18];
145
                p11 <= fracta[35:18] * fractb[35:18];
146
                p12 <= fracta[52:36] * fractb[35:18];
147
                p20 <= fracta[17: 0] * fractb[52:36];
148
                p21 <= fracta[35:18] * fractb[52:36];
149
                p22 <= fracta[52:36] * fractb[52:36];
150
                fract1 <=                                   {p02,36'b0} + {p01,18'b0} + p00 +
151
                                                                  {p12,54'b0} + {p11,36'b0} + {p10,18'b0} +
152
                                        {p22,72'b0} + {p21,54'b0} + {p20,36'b0}
153
                                ;
154
        end
155
end
156
else if (WID==32) begin
157 10 robfinch
reg [23:0] p00,p01,p02;
158
reg [23:0] p10,p11,p12;
159
reg [23:0] p20,p21,p22;
160 6 robfinch
        always @(posedge clk)
161
        if (ce) begin
162 10 robfinch
                p00 <= fracta[11: 0] * fractb[11: 0];
163
                p01 <= fracta[23:12] * fractb[11: 0];
164
                p10 <= fracta[11: 0] * fractb[23:12];
165
                p11 <= fracta[23:12] * fractb[23:12];
166
                fract1 <= {p11,p00} + {p01,12'b0} + {p10,12'b0};
167 6 robfinch
        end
168
end
169 8 robfinch
else begin
170 10 robfinch
reg [35:0] p00,p01,p02;
171
reg [35:0] p10,p11,p12;
172
reg [35:0] p20,p21,p22;
173 8 robfinch
        always @(posedge clk)
174 10 robfinch
    if (ce) begin
175
        fract1a <= fracta * fractb;
176
        fract1 <= fract1a;
177
    end
178 8 robfinch
end
179 6 robfinch
endgenerate
180
 
181
// Status
182
wire under1, over1;
183
wire under = ex1[EMSB+2];       // exponent underflow
184
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
185
 
186
delay2 #(EMSB+1) u3 (.clk(clk), .ce(ce), .i(ex1[EMSB:0]), .o(ex2) );
187
delay2 u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
188
delay2 u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
189
delay2 u6  (.clk(clk), .ce(ce), .i(under), .o(under1) );
190
delay2 u7  (.clk(clk), .ce(ce), .i(over), .o(over1) );
191
 
192
// determine when a NaN is output
193
wire qNaNOut;
194 8 robfinch
wire [WID-1:0] a1,b1;
195 6 robfinch
delay2 u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
196 8 robfinch
delay2 u14 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
197
delay2 u15 (.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
198
delay2 #(WID) u16 (.clk(clk), .ce(ce), .i(a), .o(a1) );
199
delay2 #(WID) u17 (.clk(clk), .ce(ce), .i(b), .o(b1) );
200 6 robfinch
 
201
// -----------------------------------------------------------
202
// Second clock
203
// - correct xponent and mantissa for exceptional conditions
204
// -----------------------------------------------------------
205
 
206
wire so1;
207
delay3 u8 (.clk(clk), .ce(ce), .i(sa ^ sb), .o(so1) );// two clock delay!
208
 
209
always @(posedge clk)
210
        if (ce)
211 10 robfinch
                casez({qNaNOut|aNan1|bNan1,aInf1,bInf1,over1,under1})
212
                5'b1????:       xo1 = infXp;    // qNaN - infinity * zero
213
                5'b01???:       xo1 = infXp;    // 'a' infinite
214
                5'b001??:       xo1 = infXp;    // 'b' infinite
215
                5'b0001?:       xo1 = infXp;    // result overflow
216
                5'b00001:       xo1 = ex2[EMSB:0];//0;           // underflow
217 6 robfinch
                default:        xo1 = ex2[EMSB:0];       // situation normal
218
                endcase
219
 
220
always @(posedge clk)
221
        if (ce)
222 10 robfinch
                casez({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1})
223 11 robfinch
                6'b1?????:  mo1 = {1'b1,a1[FMSB:0],{FMSB+1{1'b0}}};
224
        6'b01????:  mo1 = {1'b1,b1[FMSB:0],{FMSB+1{1'b0}}};
225
                6'b001???:      mo1 = {1'b1,qNaN|3'd4,{FMSB+1{1'b0}}};  // multiply inf * zero
226 10 robfinch
                6'b0001??:      mo1 = 0; // mul inf's
227
                6'b00001?:      mo1 = 0; // mul inf's
228 8 robfinch
                6'b000001:      mo1 = 0; // mul overflow
229 10 robfinch
                default:        mo1 = fract1;
230 6 robfinch
                endcase
231
 
232
delay3 u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
233
delay1 u11 (.clk(clk), .ce(ce), .i(over1),  .o(overflow) );
234
delay1 u12 (.clk(clk), .ce(ce), .i(over1),  .o(inf) );
235
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
236
 
237
assign o = {so1,xo1,mo1};
238
 
239
endmodule
240
 
241 10 robfinch
module fpMulnr(clk, ce, a, b, o, rm, sign_exe, inf, overflow, underflow);
242
parameter WID=32;
243
localparam MSB = WID-1;
244
localparam EMSB = WID==128 ? 14 :
245
                  WID==96 ? 14 :
246
                  WID==80 ? 14 :
247
                  WID==64 ? 10 :
248
                                  WID==52 ? 10 :
249
                                  WID==48 ? 11 :
250
                                  WID==44 ? 10 :
251
                                  WID==42 ? 10 :
252
                                  WID==40 ?  9 :
253
                                  WID==32 ?  7 :
254
                                  WID==24 ?  6 : 4;
255
localparam FMSB = WID==128 ? 111 :
256
                  WID==96 ? 79 :
257
                  WID==80 ? 63 :
258
                  WID==64 ? 51 :
259
                                  WID==52 ? 39 :
260
                                  WID==48 ? 34 :
261
                                  WID==44 ? 31 :
262
                                  WID==42 ? 29 :
263
                                  WID==40 ? 28 :
264
                                  WID==32 ? 22 :
265
                                  WID==24 ? 15 : 9;
266
 
267
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
268
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
269
input clk;
270
input ce;
271
input  [MSB:0] a, b;
272
output [MSB:0] o;
273
input [2:0] rm;
274
output sign_exe;
275
output inf;
276
output overflow;
277
output underflow;
278
 
279
wire [EX:0] o1;
280
wire sign_exe1, inf1, overflow1, underflow1;
281
wire [MSB+3:0] fpn0;
282
 
283
fpMul       #(WID) u1 (clk, ce, a, b, o1, sign_exe1, inf1, overflow1, underflow1);
284
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(underflow1), .i(o1), .o(fpn0) );
285
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
286
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
287
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
288
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
289
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
290
endmodule
291
 
292 6 robfinch
module fpMul_tb();
293
reg clk;
294
 
295
initial begin
296
        clk = 0;
297
end
298
always #10 clk <= ~clk;
299
 
300
fpMul u1 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o1), .sign_exe(sgnx1), .inf(inf1), .overflow(of1), .underflow(uf1));
301 8 robfinch
fpMul u2 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o1), .sign_exe(sgnx1), .inf(inf1), .overflow(of1), .underflow(uf1));
302 6 robfinch
 
303
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.