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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpMultiply.sv] - Blame information for rev 48

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

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

powered by: WebSVN 2.1.0

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