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

Subversion Repositories ft816float

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

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 26 robfinch
parameter WID = 32;
54
`include "fpSize.sv"
55 6 robfinch
 
56
input clk;
57
input ce;
58
input  [WID:1] a, b;
59 8 robfinch
output [EX:0] o;
60 6 robfinch
output sign_exe;
61
output inf;
62
output overflow;
63
output underflow;
64
 
65
reg [EMSB:0] xo1;                // extra bit for sign
66 8 robfinch
reg [FX:0] mo1;
67 6 robfinch
 
68
// constants
69
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
70
// The following is the value for an exponent of zero, with the offset
71
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
72
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
73
// The following is a template for a quiet nan. (MSB=1)
74
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
75
 
76
// variables
77 8 robfinch
reg [FX:0] fract1,fract1a;
78
wire [FX:0] fracto;
79 6 robfinch
wire [EMSB+2:0] ex1;     // sum of exponents
80
wire [EMSB  :0] ex2;
81
 
82
// Decompose the operands
83
wire sa, sb;                    // sign bit
84
wire [EMSB:0] xa, xb;    // exponent bits
85
wire [FMSB+1:0] fracta, fractb;
86
wire a_dn, b_dn;                        // a/b is denormalized
87 8 robfinch
wire aNan, bNan, aNan1, bNan1;
88 6 robfinch
wire az, bz;
89
wire aInf, bInf, aInf1, bInf1;
90
 
91
 
92
// -----------------------------------------------------------
93
// First clock
94
// - decode the input operands
95
// - derive basic information
96
// - calculate exponent
97
// - calculate fraction
98
// -----------------------------------------------------------
99
 
100 8 robfinch
fpDecomp #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
101
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
102 6 robfinch
 
103
// Compute the sum of the exponents.
104
// correct the exponent for denormalized operands
105
// adjust the sum by the exponent offset (subtract 127)
106
// mul: ex1 = xa + xb,  result should always be < 1ffh
107
assign ex1 = (az|bz) ? 0 : (xa|a_dn) + (xb|b_dn) - bias;
108 8 robfinch
 
109 10 robfinch
generate
110 26 robfinch
if (WID==80) begin
111
reg [31:0] p00,p01,p02,p03;
112
reg [31:0] p10,p11,p12,p13;
113
reg [31:0] p20,p21,p22,p23;
114
reg [31:0] p30,p31,p32,p33;
115
        always @(posedge clk)
116
        if (ce) begin
117
                p00 <= fracta[15: 0] * fractb[15: 0];
118
                p01 <= fracta[31:16] * fractb[15: 0];
119
                p02 <= fracta[47:32] * fractb[15: 0];
120
                p03 <= fracta[63:48] * fractb[15: 0];
121
 
122
                p10 <= fracta[15: 0] * fractb[31:16];
123
                p11 <= fracta[31:16] * fractb[31:16];
124
                p12 <= fracta[47:32] * fractb[31:16];
125
                p13 <= fracta[63:48] * fractb[31:16];
126
 
127
                p20 <= fracta[15: 0] * fractb[47:32];
128
                p21 <= fracta[31:16] * fractb[47:32];
129
                p22 <= fracta[47:32] * fractb[47:32];
130
                p23 <= fracta[63:48] * fractb[47:32];
131
 
132
                p30 <= fracta[15: 0] * fractb[63:48];
133
                p31 <= fracta[31:16] * fractb[63:48];
134
                p32 <= fracta[47:32] * fractb[63:48];
135
                p33 <= fracta[63:48] * fractb[63:48];
136
 
137
                fract1 <=                                               {p03,48'b0} + {p02,32'b0} + {p01,16'b0} + p00 +
138
                                                                  {p13,64'b0} + {p12,48'b0} + {p11,32'b0} + {p10,16'b0} +
139
                                        {p23,80'b0} + {p22,64'b0} + {p21,48'b0} + {p20,32'b0} +
140
      {p33,96'b0} + {p32,80'b0} + {p31,64'b0} + {p30,48'b0}
141
                                ;
142
        end
143
end
144
else if (WID==64) begin
145 8 robfinch
reg [35:0] p00,p01,p02;
146
reg [35:0] p10,p11,p12;
147
reg [35:0] p20,p21,p22;
148 6 robfinch
        always @(posedge clk)
149
        if (ce) begin
150
                p00 <= fracta[17: 0] * fractb[17: 0];
151
                p01 <= fracta[35:18] * fractb[17: 0];
152
                p02 <= fracta[52:36] * fractb[17: 0];
153
                p10 <= fracta[17: 0] * fractb[35:18];
154
                p11 <= fracta[35:18] * fractb[35:18];
155
                p12 <= fracta[52:36] * fractb[35:18];
156
                p20 <= fracta[17: 0] * fractb[52:36];
157
                p21 <= fracta[35:18] * fractb[52:36];
158
                p22 <= fracta[52:36] * fractb[52:36];
159
                fract1 <=                                   {p02,36'b0} + {p01,18'b0} + p00 +
160
                                                                  {p12,54'b0} + {p11,36'b0} + {p10,18'b0} +
161
                                        {p22,72'b0} + {p21,54'b0} + {p20,36'b0}
162
                                ;
163
        end
164
end
165
else if (WID==32) begin
166 10 robfinch
reg [23:0] p00,p01,p02;
167
reg [23:0] p10,p11,p12;
168
reg [23:0] p20,p21,p22;
169 6 robfinch
        always @(posedge clk)
170
        if (ce) begin
171 10 robfinch
                p00 <= fracta[11: 0] * fractb[11: 0];
172
                p01 <= fracta[23:12] * fractb[11: 0];
173
                p10 <= fracta[11: 0] * fractb[23:12];
174
                p11 <= fracta[23:12] * fractb[23:12];
175
                fract1 <= {p11,p00} + {p01,12'b0} + {p10,12'b0};
176 6 robfinch
        end
177
end
178 8 robfinch
else begin
179
        always @(posedge clk)
180 10 robfinch
    if (ce) begin
181
        fract1a <= fracta * fractb;
182
        fract1 <= fract1a;
183
    end
184 8 robfinch
end
185 6 robfinch
endgenerate
186
 
187
// Status
188
wire under1, over1;
189
wire under = ex1[EMSB+2];       // exponent underflow
190
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
191
 
192
delay2 #(EMSB+1) u3 (.clk(clk), .ce(ce), .i(ex1[EMSB:0]), .o(ex2) );
193
delay2 u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
194
delay2 u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
195
delay2 u6  (.clk(clk), .ce(ce), .i(under), .o(under1) );
196
delay2 u7  (.clk(clk), .ce(ce), .i(over), .o(over1) );
197
 
198
// determine when a NaN is output
199
wire qNaNOut;
200 8 robfinch
wire [WID-1:0] a1,b1;
201 6 robfinch
delay2 u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
202 8 robfinch
delay2 u14 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
203
delay2 u15 (.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
204
delay2 #(WID) u16 (.clk(clk), .ce(ce), .i(a), .o(a1) );
205
delay2 #(WID) u17 (.clk(clk), .ce(ce), .i(b), .o(b1) );
206 6 robfinch
 
207
// -----------------------------------------------------------
208
// Second clock
209
// - correct xponent and mantissa for exceptional conditions
210
// -----------------------------------------------------------
211
 
212
wire so1;
213
delay3 u8 (.clk(clk), .ce(ce), .i(sa ^ sb), .o(so1) );// two clock delay!
214
 
215
always @(posedge clk)
216
        if (ce)
217 10 robfinch
                casez({qNaNOut|aNan1|bNan1,aInf1,bInf1,over1,under1})
218
                5'b1????:       xo1 = infXp;    // qNaN - infinity * zero
219
                5'b01???:       xo1 = infXp;    // 'a' infinite
220
                5'b001??:       xo1 = infXp;    // 'b' infinite
221
                5'b0001?:       xo1 = infXp;    // result overflow
222
                5'b00001:       xo1 = ex2[EMSB:0];//0;           // underflow
223 6 robfinch
                default:        xo1 = ex2[EMSB:0];       // situation normal
224
                endcase
225
 
226
always @(posedge clk)
227
        if (ce)
228 10 robfinch
                casez({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1})
229 11 robfinch
                6'b1?????:  mo1 = {1'b1,a1[FMSB:0],{FMSB+1{1'b0}}};
230
        6'b01????:  mo1 = {1'b1,b1[FMSB:0],{FMSB+1{1'b0}}};
231
                6'b001???:      mo1 = {1'b1,qNaN|3'd4,{FMSB+1{1'b0}}};  // multiply inf * zero
232 10 robfinch
                6'b0001??:      mo1 = 0; // mul inf's
233
                6'b00001?:      mo1 = 0; // mul inf's
234 8 robfinch
                6'b000001:      mo1 = 0; // mul overflow
235 10 robfinch
                default:        mo1 = fract1;
236 6 robfinch
                endcase
237
 
238
delay3 u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
239
delay1 u11 (.clk(clk), .ce(ce), .i(over1),  .o(overflow) );
240
delay1 u12 (.clk(clk), .ce(ce), .i(over1),  .o(inf) );
241
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
242
 
243
assign o = {so1,xo1,mo1};
244
 
245
endmodule
246
 
247 26 robfinch
 
248
// Multiplier with normalization and rounding.
249
 
250 10 robfinch
module fpMulnr(clk, ce, a, b, o, rm, sign_exe, inf, overflow, underflow);
251
parameter WID=32;
252 26 robfinch
`include "fpSize.sv"
253 10 robfinch
 
254
input clk;
255
input ce;
256
input  [MSB:0] a, b;
257
output [MSB:0] o;
258
input [2:0] rm;
259
output sign_exe;
260
output inf;
261
output overflow;
262
output underflow;
263
 
264
wire [EX:0] o1;
265
wire sign_exe1, inf1, overflow1, underflow1;
266
wire [MSB+3:0] fpn0;
267
 
268
fpMul       #(WID) u1 (clk, ce, a, b, o1, sign_exe1, inf1, overflow1, underflow1);
269
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(underflow1), .i(o1), .o(fpn0) );
270
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
271
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
272
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
273
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
274
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
275
endmodule
276
 

powered by: WebSVN 2.1.0

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