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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPMultiply.sv] - Blame information for rev 54

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

Line No. Rev Author Line
1 53 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      DFPMultiply.v
9
//              - decimal floating point multiplier
10
//              - can issue every clock cycle
11
//              - parameterized width
12
//
13
//
14
// BSD 3-Clause License
15
// Redistribution and use in source and binary forms, with or without
16
// modification, are permitted provided that the following conditions are met:
17
//
18
// 1. Redistributions of source code must retain the above copyright notice, this
19
//    list of conditions and the following disclaimer.
20
//
21
// 2. Redistributions in binary form must reproduce the above copyright notice,
22
//    this list of conditions and the following disclaimer in the documentation
23
//    and/or other materials provided with the distribution.
24
//
25
// 3. Neither the name of the copyright holder nor the names of its
26
//    contributors may be used to endorse or promote products derived from
27
//    this software without specific prior written permission.
28
//
29
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
33
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
//
40
//
41
//      Floating Point Multiplier
42
//
43
//      This multiplier handles denormalized numbers.
44
//      The output format is of an internal expanded representation
45
//      in preparation to be fed into a normalization unit, then
46
//      rounding. Basically, it's the same as the regular format
47
//      except the mantissa is doubled in size, the leading two
48
//      bits of which are assumed to be whole bits.
49
//
50
//
51
//      Floating Point Multiplier
52
//
53
//      Properties:
54
//      +-inf * +-inf = -+inf   (this is handled by exOver)
55
//      +-inf * 0     = QNaN
56
//
57
// ============================================================================
58
 
59
import fp::*;
60
 
61 54 robfinch
//`define DFPMUL_PARALLEL       1'b1
62
 
63
module DFPMultiply(clk, ce, ld, a, b, o, sign_exe, inf, overflow, underflow, done);
64 53 robfinch
input clk;
65
input ce;
66 54 robfinch
input ld;
67 53 robfinch
input  [127:0] a, b;
68
output [243:0] o;
69
output sign_exe;
70
output inf;
71
output overflow;
72
output underflow;
73 54 robfinch
output done;
74 53 robfinch
parameter DELAY =
75
  (FPWID == 128 ? 17 :
76
  FPWID == 80 ? 17 :
77
  FPWID == 64 ? 13 :
78
  FPWID == 40 ? 8 :
79
  FPWID == 32 ? 2 :
80
  FPWID == 16 ? 2 : 2);
81
 
82
reg [15:0] xo1;         // extra bit for sign
83
reg [215:0] mo1;
84
 
85
// constants
86
wire [15:0] infXp = 16'h9999;   // infinite / NaN - all ones
87
// The following is the value for an exponent of zero, with the offset
88
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
89
// The following is a template for a quiet nan. (MSB=1)
90
wire [107:0] qNaN  = {4'h1,{104{1'b0}}};
91
 
92
// variables
93
reg [215:0] sig1;
94
wire [15:0] ex2;
95
 
96
// Decompose the operands
97
wire sa, sb;                    // sign bit
98
wire [15:0] xa, xb;     // exponent bits
99
wire sxa, sxb;
100
wire [107:0] siga, sigb;
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
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108
// Clock #1
109
// - decode the input operands
110
// - derive basic information
111
// - calculate exponent
112
// - calculate fraction
113
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114
 
115
// -----------------------------------------------------------
116
// First clock
117
// -----------------------------------------------------------
118
 
119
reg under, over;
120 54 robfinch
reg [15:0] sum_ex, sum_ex1;
121 53 robfinch
reg sx0;
122 54 robfinch
wire done1;
123 53 robfinch
 
124
DFPDecompose u1a (.i(a), .sgn(sa), .sx(sxa), .exp(xa), .sig(siga), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
125
DFPDecompose u1b (.i(b), .sgn(sb), .sx(sxb), .exp(xb), .sig(sigb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
126
 
127
// Compute the sum of the exponents.
128
// Exponents are sign-magnitude.
129
wire [15:0] xapxb, xamxb, xbmxa;
130
wire xapxbc, xamxbc, xbmxac;
131
BCDAddN #(.N(4)) u1c (.ci(1'b0), .a(xa), .b(xb), .o(xapxb), .co(xapxbc));
132
BCDSubN #(.N(4)) u1d (.ci(1'b0), .a(xa), .b(xb), .o(xamxb), .co(xamxbc));
133
BCDSubN #(.N(4)) u1e (.ci(1'b0), .a(xb), .b(xa), .o(xbmxa), .co(xbmxac));
134 54 robfinch
BCDSubN #(.N(5)) u1h (.ci(1'b0), .a(20'h10000), .b(sum_ex1), .o(sum_ex2), .co());
135 53 robfinch
 
136
always @*
137
        case({sxa,sxb})
138 54 robfinch
        2'b11:  begin sum_ex1 <= xapxb; over <= xapxbc; under <= 1'b0; sx0 <= sxa; end
139
        2'b01:  begin sum_ex1 <= xbmxa; over <= 1'b0; under <= 1'b0; sx0 <= ~xbmxac; end
140
        2'b10:  begin sum_ex1 <= xamxb; over <= 1'b0; under <= 1'b0; sx0 <= ~xamxbc; end
141
        2'b00:  begin sum_ex1 <= xapxb; over <= 1'b0; under <= xapxbc; sx0 <= sxa; end
142 53 robfinch
        endcase
143
 
144 54 robfinch
// Take nine's complement if exponent sign changed.
145
always @*
146
        if ((sxa^sxb)) begin
147
                if ((sxa & xamxbc) || (sxb & xbmxac))
148
                        sum_ex <= sum_ex2;
149
                else
150
                        sum_ex <= sum_ex1;
151
        end
152
        else
153
                sum_ex <= sum_ex1;
154
 
155 53 robfinch
wire [255:0] sigoo;
156 54 robfinch
`ifdef DFPMUL_PARALLEL
157 53 robfinch
BCDMul32 u1f (.a({20'h0,siga}),.b({20'h0,sigb}),.o(sigoo));
158 54 robfinch
`else
159
dfmul u1g
160
(
161
        .clk(clk),
162
        .ld(ld),
163
        .a(siga),
164
        .b(sigb),
165
        .p(sigoo),
166
        .done(done1)
167
);
168
`endif
169 53 robfinch
 
170
always @(posedge clk)
171
  if (ce) sig1 <= sigoo[215:0];
172
 
173
// Status
174
wire under1, over1;
175
 
176
delay #(.WID(16),.DEP(DELAY)) u3 (.clk(clk), .ce(ce), .i(sum_ex), .o(ex2) );
177
delay #(.WID(1),.DEP(DELAY)) u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
178
delay #(.WID(1),.DEP(DELAY)) u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
179
delay #(.WID(1),.DEP(DELAY)) u6  (.clk(clk), .ce(ce), .i(under), .o(under1) );
180
delay #(.WID(1),.DEP(DELAY)) u7  (.clk(clk), .ce(ce), .i(over), .o(over1) );
181
 
182
// determine when a NaN is output
183
wire qNaNOut;
184
wire [127:0] a1,b1;
185
delay #(.WID(1),.DEP(DELAY)) u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
186
delay #(.WID(1),.DEP(DELAY)) u14 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
187
delay #(.WID(1),.DEP(DELAY)) u15 (.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
188
delay #(.WID(128),.DEP(DELAY))  u16 (.clk(clk), .ce(ce), .i(a), .o(a1) );
189
delay #(.WID(128),.DEP(DELAY))  u17 (.clk(clk), .ce(ce), .i(b), .o(b1) );
190
 
191
// -----------------------------------------------------------
192
// Second clock
193
// - correct xponent and mantissa for exceptional conditions
194
// -----------------------------------------------------------
195
 
196
wire so1, sx1;
197
reg [3:0] st;
198 54 robfinch
wire done1a;
199 53 robfinch
 
200
delay #(.WID(1),.DEP(1)) u8 (.clk(clk), .ce(ce), .i(~(sa ^ sb)), .o(so1) );// two clock delay!
201
delay #(.WID(1),.DEP(1)) u9 (.clk(clk), .ce(ce), .i(sx0), .o(sx1) );// two clock delay!
202
 
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[15:0];//0;            // underflow
211
                default:        xo1 = ex2[15:0];        // situation normal
212
                endcase
213
 
214
// Force mantissa to zero when underflow or zero exponent when not supporting denormals.
215
always @(posedge clk)
216
        if (ce)
217
                casez({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1|under1})
218
                6'b1?????:  mo1 = {4'h1,a1[103:0],108'b0};
219
    6'b01????:  mo1 = {4'h1,b1[103:0],108'b0};
220
                6'b001???:      mo1 = {4'h1,qNaN|3'd4,108'b0};  // multiply inf * zero
221
                6'b0001??:      mo1 = 0;        // mul inf's
222
                6'b00001?:      mo1 = 0;        // mul inf's
223
                6'b000001:      mo1 = 0;        // mul overflow
224
                default:        mo1 = sig1;
225
                endcase
226
 
227
always @(posedge clk)
228
        if (ce) begin
229
                st[3] <= aNan1|bNan1;
230
                st[2] <= so1;
231
                st[1] <= aInf|bInf|over;
232
                st[0] <= sx1;
233
        end
234
 
235
delay #(.WID(1),.DEP(DELAY+1)) u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
236
delay1 u11 (.clk(clk), .ce(ce), .i(over1),  .o(overflow) );
237
delay1 u12 (.clk(clk), .ce(ce), .i(over1),  .o(inf) );
238
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
239 54 robfinch
delay #(.WID(1),.DEP(3)) u18 (.clk(clk), .ce(ce), .i(done1), .o(done1a) );
240 53 robfinch
 
241
assign o = {st,xo1,mo1,8'h00};
242 54 robfinch
assign done = done1&done1a;
243 53 robfinch
 
244
endmodule
245
 
246
 
247
// Multiplier with normalization and rounding.
248
 
249 54 robfinch
module DFPMultiplynr(clk, ce, ld, a, b, o, rm, sign_exe, inf, overflow, underflow, done);
250 53 robfinch
input clk;
251
input ce;
252 54 robfinch
input ld;
253 53 robfinch
input  [127:0] a, b;
254
output [127:0] o;
255
input [2:0] rm;
256
output sign_exe;
257
output inf;
258
output overflow;
259
output underflow;
260 54 robfinch
output done;
261 53 robfinch
 
262 54 robfinch
wire done1, done1a;
263 53 robfinch
wire [243:0] o1;
264
wire sign_exe1, inf1, overflow1, underflow1;
265
wire [131:0] fpn0;
266
 
267 54 robfinch
DFPMultiply  u1 (clk, ce, ld, a, b, o1, sign_exe1, inf1, overflow1, underflow1, done1);
268 53 robfinch
DFPNormalize u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
269
DFPRound     u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
270
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
271
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
272
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
273
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
274 54 robfinch
delay #(.WID(1),.DEP(11)) u10 (.clk(clk), .ce(ce), .i(done1), .o(done1a) );
275
assign done = done1 & done1a;
276
 
277 53 robfinch
endmodule

powered by: WebSVN 2.1.0

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