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

Subversion Repositories ft816float

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

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 49 robfinch
//              - two cycle latency minimum (latency depends on precision)
11 48 robfinch
//              - can issue every clock cycle
12
//              - parameterized width
13
//              - IEEE 754 representation
14
//
15
//
16 49 robfinch
// BSD 3-Clause License
17
// Redistribution and use in source and binary forms, with or without
18
// modification, are permitted provided that the following conditions are met:
19
//
20
// 1. Redistributions of source code must retain the above copyright notice, this
21
//    list of conditions and the following disclaimer.
22
//
23
// 2. Redistributions in binary form must reproduce the above copyright notice,
24
//    this list of conditions and the following disclaimer in the documentation
25
//    and/or other materials provided with the distribution.
26
//
27
// 3. Neither the name of the copyright holder nor the names of its
28
//    contributors may be used to endorse or promote products derived from
29
//    this software without specific prior written permission.
30
//
31
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
35
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 48 robfinch
//
42
//
43 49 robfinch
//      Floating Point Multiplier
44
//
45
//      This multiplier handles denormalized numbers.
46 48 robfinch
//      The output format is of an internal expanded representation
47
//      in preparation to be fed into a normalization unit, then
48
//      rounding. Basically, it's the same as the regular format
49
//      except the mantissa is doubled in size, the leading two
50
//      bits of which are assumed to be whole bits.
51
//
52
//
53
//      Floating Point Multiplier
54
//
55
//      Properties:
56
//      +-inf * +-inf = -+inf   (this is handled by exOver)
57
//      +-inf * 0     = QNaN
58
//
59
// ============================================================================
60
 
61
import fp::*;
62
 
63
module fpMultiply(clk, ce, a, b, o, sign_exe, inf, overflow, underflow);
64
input clk;
65
input ce;
66
input  [MSB:0] a, b;
67
output [EX:0] o;
68
output sign_exe;
69
output inf;
70
output overflow;
71
output underflow;
72 49 robfinch
parameter DELAY =
73
  (FPWID == 128 ? 17 :
74
  FPWID == 80 ? 17 :
75
  FPWID == 64 ? 13 :
76
  FPWID == 40 ? 8 :
77
  FPWID == 32 ? 2 :
78
  FPWID == 16 ? 2 : 2);
79 48 robfinch
 
80
reg [EMSB:0] xo1;               // extra bit for sign
81
reg [FX:0] mo1;
82
 
83
// constants
84
wire [EMSB:0] infXp = {EMSB+1{1'b1}};   // infinite / NaN - all ones
85
// The following is the value for an exponent of zero, with the offset
86
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
87
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};       //2^0 exponent
88
// The following is a template for a quiet nan. (MSB=1)
89
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
90
 
91
// variables
92
reg [FX:0] fract1,fract1a;
93
wire [FX:0] fracto;
94
wire [EMSB+2:0] ex1;    // sum of exponents
95
wire [EMSB  :0] ex2;
96
 
97
// Decompose the operands
98
wire sa, sb;                    // sign bit
99
wire [EMSB:0] xa, xb;   // exponent bits
100
wire [FMSB+1:0] fracta, fractb;
101
wire a_dn, b_dn;                        // a/b is denormalized
102
wire aNan, bNan, aNan1, bNan1;
103
wire az, bz;
104
wire aInf, bInf, aInf1, bInf1;
105
 
106
 
107 49 robfinch
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108
// Clock #1
109 48 robfinch
// - decode the input operands
110
// - derive basic information
111
// - calculate exponent
112
// - calculate fraction
113 49 robfinch
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114
 
115 48 robfinch
// -----------------------------------------------------------
116 49 robfinch
// First clock
117
// -----------------------------------------------------------
118 48 robfinch
 
119
fpDecomp u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
120
fpDecomp u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
121
 
122
// Compute the sum of the exponents.
123
// correct the exponent for denormalized operands
124
// adjust the sum by the exponent offset (subtract 127)
125
// mul: ex1 = xa + xb,  result should always be < 1ffh
126 49 robfinch
`ifdef SUPPORT_DENORMALS
127 48 robfinch
assign ex1 = (az|bz) ? 0 : (xa|a_dn) + (xb|b_dn) - bias;
128 49 robfinch
`else
129
assign ex1 = (az|bz) ? 0 : xa + xb - bias;
130
`endif
131 48 robfinch
 
132
generate
133 49 robfinch
if (FPWID==128) begin
134
  wire [255:0] fractoo;
135
  mult128x128 umul1 (.clk(clk), .ce(ce), .a({16'b0,fracta}), .b({16'b0,fractb}), .o(fractoo));
136
  always @(posedge clk)
137
    if (ce) fract1 <= fractoo[224:0];
138 48 robfinch
end
139 49 robfinch
else if (FPWID==80) begin
140
  wire [255:0] fractoo;
141
  mult128x128 umul1 (.clk(clk), .ce(ce), .a({63'd0,fracta}), .b({63'd0,fractb}), .o(fractoo));
142
  always @(posedge clk)
143
    if (ce) fract1 <= fractoo[130:0];
144
end
145 48 robfinch
else if (FPWID==64) begin
146 49 robfinch
  wire [127:0] fractoo;
147
  mult64x64 umul1 (.clk(clk), .ce(ce), .a({11'd0,fracta}), .b({11'd0,fractb}), .o(fractoo));
148
  always @(posedge clk)
149
    if (ce) fract1 <= fractoo[106:0];
150 48 robfinch
end
151 49 robfinch
else if (FPWID==40) begin
152
  wire [63:0] fractoo;
153
  mult32x32 umul1 (.clk(clk), .ce(ce), .a({3'd0,fracta}), .b({3'd0,fractb}), .o(fractoo));
154
  always @(posedge clk)
155
    if (ce) fract1 <= fractoo[58:0];
156
end
157 48 robfinch
else if (FPWID==32) begin
158 49 robfinch
  reg [23:0] p00,p11;
159
  always @(posedge clk)
160
        if (ce) begin
161
          p00 <= fracta[23: 0] * fractb[11: 0];
162
          p11 <= fracta[23: 0] * fractb[23:12];
163
                fract1 <= {p11,12'b0} + p00;
164
        end
165 48 robfinch
end
166
else begin
167
        always @(posedge clk)
168
    if (ce) begin
169
        fract1a <= fracta * fractb;
170 49 robfinch
      fract1 <= fract1a;
171 48 robfinch
    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 49 robfinch
delay #(.WID(EMSB+1),.DEP(DELAY)) u3 (.clk(clk), .ce(ce), .i(ex1[EMSB:0]), .o(ex2) );
181
delay #(.WID(1),.DEP(DELAY)) u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
182
delay #(.WID(1),.DEP(DELAY)) u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
183
delay #(.WID(1),.DEP(DELAY)) u6  (.clk(clk), .ce(ce), .i(under), .o(under1) );
184
delay #(.WID(1),.DEP(DELAY)) u7  (.clk(clk), .ce(ce), .i(over), .o(over1) );
185 48 robfinch
 
186
// determine when a NaN is output
187
wire qNaNOut;
188
wire [FPWID-1:0] a1,b1;
189 49 robfinch
delay #(.WID(1),.DEP(DELAY)) u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
190
delay #(.WID(1),.DEP(DELAY)) u14 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
191
delay #(.WID(1),.DEP(DELAY)) u15 (.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
192
delay #(.WID(FPWID),.DEP(DELAY))  u16 (.clk(clk), .ce(ce), .i(a), .o(a1) );
193
delay #(.WID(FPWID),.DEP(DELAY))  u17 (.clk(clk), .ce(ce), .i(b), .o(b1) );
194 48 robfinch
 
195
// -----------------------------------------------------------
196
// Second clock
197
// - correct xponent and mantissa for exceptional conditions
198
// -----------------------------------------------------------
199
 
200
wire so1;
201 49 robfinch
delay #(.WID(1),.DEP(DELAY+1)) u8 (.clk(clk), .ce(ce), .i(sa ^ sb), .o(so1) );// two clock delay!
202 48 robfinch
 
203
always @(posedge clk)
204
        if (ce)
205
                casez({qNaNOut|aNan1|bNan1,aInf1,bInf1,over1,under1})
206
                5'b1????:       xo1 = infXp;    // qNaN - infinity * zero
207
                5'b01???:       xo1 = infXp;    // 'a' infinite
208
                5'b001??:       xo1 = infXp;    // 'b' infinite
209
                5'b0001?:       xo1 = infXp;    // result overflow
210
                5'b00001:       xo1 = ex2[EMSB:0];//0;          // underflow
211
                default:        xo1 = ex2[EMSB:0];      // situation normal
212
                endcase
213
 
214 49 robfinch
// Force mantissa to zero when underflow or zero exponent when not supporting denormals.
215 48 robfinch
always @(posedge clk)
216
        if (ce)
217 49 robfinch
`ifdef SUPPORT_DENORMALS
218 48 robfinch
                casez({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1})
219 49 robfinch
`else
220
                casez({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1|under1})
221
`endif
222 48 robfinch
                6'b1?????:  mo1 = {1'b1,a1[FMSB:0],{FMSB+1{1'b0}}};
223
    6'b01????:  mo1 = {1'b1,b1[FMSB:0],{FMSB+1{1'b0}}};
224
                6'b001???:      mo1 = {1'b1,qNaN|3'd4,{FMSB+1{1'b0}}};  // multiply inf * zero
225
                6'b0001??:      mo1 = 0;        // mul inf's
226
                6'b00001?:      mo1 = 0;        // mul inf's
227
                6'b000001:      mo1 = 0;        // mul overflow
228
                default:        mo1 = fract1;
229
                endcase
230
 
231 49 robfinch
delay #(.WID(1),.DEP(DELAY+1)) u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
232 48 robfinch
delay1 u11 (.clk(clk), .ce(ce), .i(over1),  .o(overflow) );
233
delay1 u12 (.clk(clk), .ce(ce), .i(over1),  .o(inf) );
234
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
235
 
236
assign o = {so1,xo1,mo1};
237
 
238
endmodule
239
 
240
 
241
// Multiplier with normalization and rounding.
242
 
243 49 robfinch
module fpMultiplynr(clk, ce, a, b, o, rm, sign_exe, inf, overflow, underflow);
244 48 robfinch
input clk;
245
input ce;
246
input  [MSB:0] a, b;
247
output [MSB:0] o;
248
input [2:0] rm;
249
output sign_exe;
250
output inf;
251
output overflow;
252
output underflow;
253
 
254
wire [EX:0] o1;
255
wire sign_exe1, inf1, overflow1, underflow1;
256
wire [MSB+3:0] fpn0;
257
 
258 49 robfinch
fpMultiply  u1 (clk, ce, a, b, o1, sign_exe1, inf1, overflow1, underflow1);
259
fpNormalize u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
260
fpRound     u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
261 48 robfinch
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
262
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
263
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
264
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
265
endmodule

powered by: WebSVN 2.1.0

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