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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [rtl/] [fpUnit/] [fpMul.v] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 51 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2006-2018  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch<remove>@finitron.ca
7
//       ||
8
//
9
//      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
// 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
//                                                                          
30
//      Floating Point Multiplier / Divider
31
//
32
//      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
//
39
//
40
//      Floating Point Multiplier
41
//
42
//      Properties:
43
//      +-inf * +-inf = -+inf   (this is handled by exOver)
44
//      +-inf * 0     = QNaN
45
//      
46
//      1 sign number
47
//      8 exponent
48
//      48 mantissa
49
//
50
// ============================================================================
51
 
52
module fpMul (clk, ce, a, b, o, sign_exe, inf, overflow, underflow);
53
parameter WID = 128;
54
localparam MSB = WID-1;
55
localparam EMSB = WID==128 ? 14 :
56
                  WID==96 ? 14 :
57
                  WID==80 ? 14 :
58
                  WID==64 ? 10 :
59
                                  WID==52 ? 10 :
60
                                  WID==48 ? 11 :
61
                                  WID==44 ? 10 :
62
                                  WID==42 ? 10 :
63
                                  WID==40 ?  9 :
64
                                  WID==32 ?  7 :
65
                                  WID==24 ?  6 : 4;
66
localparam FMSB = WID==128 ? 111 :
67
                  WID==96 ? 79 :
68
                  WID==80 ? 63 :
69
                  WID==64 ? 51 :
70
                                  WID==52 ? 39 :
71
                                  WID==48 ? 34 :
72
                                  WID==44 ? 31 :
73
                                  WID==42 ? 29 :
74
                                  WID==40 ? 28 :
75
                                  WID==32 ? 22 :
76
                                  WID==24 ? 15 : 9;
77
 
78
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
79
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
80
 
81
input clk;
82
input ce;
83
input  [WID:1] a, b;
84
output [EX:0] o;
85
output sign_exe;
86
output inf;
87
output overflow;
88
output underflow;
89
 
90
reg [EMSB:0] xo1;                // extra bit for sign
91
reg [FX:0] mo1;
92
 
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
reg [FX:0] fract1,fract1a;
103
wire [FX:0] fracto;
104
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
wire aNan, bNan, aNan1, bNan1;
113
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
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
 
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
 
134
generate
135
if (WID==80) begin
136
reg [31:0] p00,p01,p02,p03;
137
reg [31:0] p10,p11,p12,p13;
138
reg [31:0] p20,p21,p22,p23;
139
reg [31:0] p30,p31,p32,p33;
140
        always @(posedge clk)
141
        if (ce) begin
142
                p00 <= fracta[15: 0] * fractb[15: 0];
143
                p01 <= fracta[31:16] * fractb[15: 0];
144
                p02 <= fracta[47:32] * fractb[15: 0];
145
                p03 <= fracta[63:48] * fractb[15: 0];
146
 
147
                p10 <= fracta[15: 0] * fractb[31:16];
148
                p11 <= fracta[31:16] * fractb[31:16];
149
                p12 <= fracta[47:32] * fractb[31:16];
150
                p13 <= fracta[63:48] * fractb[31:16];
151
 
152
                p20 <= fracta[15: 0] * fractb[47:32];
153
                p21 <= fracta[31:16] * fractb[47:32];
154
                p22 <= fracta[47:32] * fractb[47:32];
155
                p23 <= fracta[63:48] * fractb[47:32];
156
 
157
                p30 <= fracta[15: 0] * fractb[63:48];
158
                p31 <= fracta[31:16] * fractb[63:48];
159
                p32 <= fracta[47:32] * fractb[63:48];
160
                p33 <= fracta[63:48] * fractb[63:48];
161
 
162
                fract1 <=                                               {p03,48'b0} + {p02,32'b0} + {p01,16'b0} + p00 +
163
                                                                  {p13,64'b0} + {p12,48'b0} + {p11,32'b0} + {p10,16'b0} +
164
                                        {p23,80'b0} + {p22,64'b0} + {p21,48'b0} + {p20,32'b0} +
165
      {p33,96'b0} + {p32,80'b0} + {p31,64'b0} + {p30,48'b0}
166
                                ;
167
        end
168
end
169
else if (WID==64) begin
170
reg [35:0] p00,p01,p02;
171
reg [35:0] p10,p11,p12;
172
reg [35:0] p20,p21,p22;
173
        always @(posedge clk)
174
        if (ce) begin
175
                p00 <= fracta[17: 0] * fractb[17: 0];
176
                p01 <= fracta[35:18] * fractb[17: 0];
177
                p02 <= fracta[52:36] * fractb[17: 0];
178
                p10 <= fracta[17: 0] * fractb[35:18];
179
                p11 <= fracta[35:18] * fractb[35:18];
180
                p12 <= fracta[52:36] * fractb[35:18];
181
                p20 <= fracta[17: 0] * fractb[52:36];
182
                p21 <= fracta[35:18] * fractb[52:36];
183
                p22 <= fracta[52:36] * fractb[52:36];
184
                fract1 <=                                   {p02,36'b0} + {p01,18'b0} + p00 +
185
                                                                  {p12,54'b0} + {p11,36'b0} + {p10,18'b0} +
186
                                        {p22,72'b0} + {p21,54'b0} + {p20,36'b0}
187
                                ;
188
        end
189
end
190
else if (WID==32) begin
191
reg [23:0] p00,p01,p02;
192
reg [23:0] p10,p11,p12;
193
reg [23:0] p20,p21,p22;
194
        always @(posedge clk)
195
        if (ce) begin
196
                p00 <= fracta[11: 0] * fractb[11: 0];
197
                p01 <= fracta[23:12] * fractb[11: 0];
198
                p10 <= fracta[11: 0] * fractb[23:12];
199
                p11 <= fracta[23:12] * fractb[23:12];
200
                fract1 <= {p11,p00} + {p01,12'b0} + {p10,12'b0};
201
        end
202
end
203
else begin
204
        always @(posedge clk)
205
    if (ce) begin
206
        fract1a <= fracta * fractb;
207
        fract1 <= fract1a;
208
    end
209
end
210
endgenerate
211
 
212
// Status
213
wire under1, over1;
214
wire under = ex1[EMSB+2];       // exponent underflow
215
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
216
 
217
delay2 #(EMSB+1) u3 (.clk(clk), .ce(ce), .i(ex1[EMSB:0]), .o(ex2) );
218
delay2 u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
219
delay2 u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
220
delay2 u6  (.clk(clk), .ce(ce), .i(under), .o(under1) );
221
delay2 u7  (.clk(clk), .ce(ce), .i(over), .o(over1) );
222
 
223
// determine when a NaN is output
224
wire qNaNOut;
225
wire [WID-1:0] a1,b1;
226
delay2 u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
227
delay2 u14 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
228
delay2 u15 (.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
229
delay2 #(WID) u16 (.clk(clk), .ce(ce), .i(a), .o(a1) );
230
delay2 #(WID) u17 (.clk(clk), .ce(ce), .i(b), .o(b1) );
231
 
232
// -----------------------------------------------------------
233
// Second clock
234
// - correct xponent and mantissa for exceptional conditions
235
// -----------------------------------------------------------
236
 
237
wire so1;
238
delay3 u8 (.clk(clk), .ce(ce), .i(sa ^ sb), .o(so1) );// two clock delay!
239
 
240
always @(posedge clk)
241
        if (ce)
242
                casez({qNaNOut|aNan1|bNan1,aInf1,bInf1,over1,under1})
243
                5'b1????:       xo1 = infXp;    // qNaN - infinity * zero
244
                5'b01???:       xo1 = infXp;    // 'a' infinite
245
                5'b001??:       xo1 = infXp;    // 'b' infinite
246
                5'b0001?:       xo1 = infXp;    // result overflow
247
                5'b00001:       xo1 = ex2[EMSB:0];//0;           // underflow
248
                default:        xo1 = ex2[EMSB:0];       // situation normal
249
                endcase
250
 
251
always @(posedge clk)
252
        if (ce)
253
                casez({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1})
254
                6'b1?????:  mo1 = {1'b1,a1[FMSB:0],{FMSB+1{1'b0}}};
255
        6'b01????:  mo1 = {1'b1,b1[FMSB:0],{FMSB+1{1'b0}}};
256
                6'b001???:      mo1 = {1'b1,qNaN|3'd4,{FMSB+1{1'b0}}};  // multiply inf * zero
257
                6'b0001??:      mo1 = 0; // mul inf's
258
                6'b00001?:      mo1 = 0; // mul inf's
259
                6'b000001:      mo1 = 0; // mul overflow
260
                default:        mo1 = fract1;
261
                endcase
262
 
263
delay3 u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
264
delay1 u11 (.clk(clk), .ce(ce), .i(over1),  .o(overflow) );
265
delay1 u12 (.clk(clk), .ce(ce), .i(over1),  .o(inf) );
266
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
267
 
268
assign o = {so1,xo1,mo1};
269
 
270
endmodule
271
 
272
 
273
// Multiplier with normalization and rounding.
274
 
275
module fpMulnr(clk, ce, a, b, o, rm, sign_exe, inf, overflow, underflow);
276
parameter WID=32;
277
localparam MSB = WID-1;
278
localparam EMSB = WID==128 ? 14 :
279
                  WID==96 ? 14 :
280
                  WID==80 ? 14 :
281
                  WID==64 ? 10 :
282
                                  WID==52 ? 10 :
283
                                  WID==48 ? 11 :
284
                                  WID==44 ? 10 :
285
                                  WID==42 ? 10 :
286
                                  WID==40 ?  9 :
287
                                  WID==32 ?  7 :
288
                                  WID==24 ?  6 : 4;
289
localparam FMSB = WID==128 ? 111 :
290
                  WID==96 ? 79 :
291
                  WID==80 ? 63 :
292
                  WID==64 ? 51 :
293
                                  WID==52 ? 39 :
294
                                  WID==48 ? 34 :
295
                                  WID==44 ? 31 :
296
                                  WID==42 ? 29 :
297
                                  WID==40 ? 28 :
298
                                  WID==32 ? 22 :
299
                                  WID==24 ? 15 : 9;
300
 
301
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
302
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
303
input clk;
304
input ce;
305
input  [MSB:0] a, b;
306
output [MSB:0] o;
307
input [2:0] rm;
308
output sign_exe;
309
output inf;
310
output overflow;
311
output underflow;
312
 
313
wire [EX:0] o1;
314
wire sign_exe1, inf1, overflow1, underflow1;
315
wire [MSB+3:0] fpn0;
316
 
317
fpMul       #(WID) u1 (clk, ce, a, b, o1, sign_exe1, inf1, overflow1, underflow1);
318
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(underflow1), .i(o1), .o(fpn0) );
319
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
320
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
321
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
322
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
323
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
324
endmodule
325
 

powered by: WebSVN 2.1.0

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