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

Subversion Repositories ft816float

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

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 8 robfinch
//   \\__/ o\    (C) 2006-2016  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 8 robfinch
parameter WID = 128;
54 6 robfinch
localparam MSB = WID-1;
55 8 robfinch
localparam EMSB = WID==128 ? 14 :
56
                  WID==96 ? 14 :
57
                  WID==80 ? 14 :
58
                  WID==64 ? 10 :
59 6 robfinch
                                  WID==52 ? 10 :
60
                                  WID==48 ? 10 :
61
                                  WID==44 ? 10 :
62
                                  WID==42 ? 10 :
63
                                  WID==40 ?  9 :
64
                                  WID==32 ?  7 :
65
                                  WID==24 ?  6 : 4;
66 8 robfinch
localparam FMSB = WID==128 ? 111 :
67
                  WID==96 ? 79 :
68
                  WID==80 ? 63 :
69
                  WID==64 ? 51 :
70 6 robfinch
                                  WID==52 ? 39 :
71
                                  WID==48 ? 35 :
72
                                  WID==44 ? 31 :
73
                                  WID==42 ? 29 :
74
                                  WID==40 ? 28 :
75
                                  WID==32 ? 22 :
76
                                  WID==24 ? 15 : 9;
77
 
78 8 robfinch
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
79
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
80 6 robfinch
 
81
input clk;
82
input ce;
83
input  [WID:1] a, b;
84 8 robfinch
output [EX:0] o;
85 6 robfinch
output sign_exe;
86
output inf;
87
output overflow;
88
output underflow;
89
 
90
reg [EMSB:0] xo1;                // extra bit for sign
91 8 robfinch
reg [FX:0] mo1;
92 6 robfinch
 
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 8 robfinch
reg [FX:0] fract1,fract1a;
103
wire [FX:0] fracto;
104 6 robfinch
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 8 robfinch
wire aNan, bNan, aNan1, bNan1;
113 6 robfinch
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 8 robfinch
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 6 robfinch
 
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 8 robfinch
 
134
reg [35:0] p00,p01,p02;
135
reg [35:0] p10,p11,p12;
136
reg [35:0] p20,p21,p22;
137 6 robfinch
generate
138
if (WID==64) begin
139
        always @(posedge clk)
140
        if (ce) begin
141
                p00 <= fracta[17: 0] * fractb[17: 0];
142
                p01 <= fracta[35:18] * fractb[17: 0];
143
                p02 <= fracta[52:36] * fractb[17: 0];
144
                p10 <= fracta[17: 0] * fractb[35:18];
145
                p11 <= fracta[35:18] * fractb[35:18];
146
                p12 <= fracta[52:36] * fractb[35:18];
147
                p20 <= fracta[17: 0] * fractb[52:36];
148
                p21 <= fracta[35:18] * fractb[52:36];
149
                p22 <= fracta[52:36] * fractb[52:36];
150
                fract1 <=                                   {p02,36'b0} + {p01,18'b0} + p00 +
151
                                                                  {p12,54'b0} + {p11,36'b0} + {p10,18'b0} +
152
                                        {p22,72'b0} + {p21,54'b0} + {p20,36'b0}
153
                                ;
154
        end
155
end
156
else if (WID==32) begin
157
        always @(posedge clk)
158
        if (ce) begin
159
                p00 <= fracta[17: 0] * fractb[17: 0];
160
                p01 <= fracta[23:18] * fractb[17: 0];
161
                p10 <= fracta[17: 0] * fractb[23:18];
162
                p11 <= fracta[23:18] * fractb[23:18];
163
                fract1 <= {p11,p00} + {p01,18'b0} + {p10,18'b0};
164
        end
165
end
166 8 robfinch
else begin
167
        always @(posedge clk)
168
    if (ce)
169
        fract1 <= fracta * fractb;
170
end
171 6 robfinch
endgenerate
172
 
173
// Status
174
wire under1, over1;
175
wire under = ex1[EMSB+2];       // exponent underflow
176
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
177
 
178
delay2 #(EMSB+1) u3 (.clk(clk), .ce(ce), .i(ex1[EMSB:0]), .o(ex2) );
179 8 robfinch
delay2 #(FX+1) u4 (.clk(clk), .ce(ce), .i(fract1), .o(fracto) );
180 6 robfinch
delay2 u2a (.clk(clk), .ce(ce), .i(aInf), .o(aInf1) );
181
delay2 u2b (.clk(clk), .ce(ce), .i(bInf), .o(bInf1) );
182
delay2 u6  (.clk(clk), .ce(ce), .i(under), .o(under1) );
183
delay2 u7  (.clk(clk), .ce(ce), .i(over), .o(over1) );
184
 
185
// determine when a NaN is output
186
wire qNaNOut;
187 8 robfinch
wire [WID-1:0] a1,b1;
188 6 robfinch
delay2 u5 (.clk(clk), .ce(ce), .i((aInf&bz)|(bInf&az)), .o(qNaNOut) );
189 8 robfinch
delay2 u14 (.clk(clk), .ce(ce), .i(aNan), .o(aNan1) );
190
delay2 u15 (.clk(clk), .ce(ce), .i(bNan), .o(bNan1) );
191
delay2 #(WID) u16 (.clk(clk), .ce(ce), .i(a), .o(a1) );
192
delay2 #(WID) u17 (.clk(clk), .ce(ce), .i(b), .o(b1) );
193 6 robfinch
 
194
// -----------------------------------------------------------
195
// Second clock
196
// - correct xponent and mantissa for exceptional conditions
197
// -----------------------------------------------------------
198
 
199
wire so1;
200
delay3 u8 (.clk(clk), .ce(ce), .i(sa ^ sb), .o(so1) );// two clock delay!
201
 
202
always @(posedge clk)
203
        if (ce)
204 8 robfinch
                casex({qNaNOut|aNan1|bNan1,aInf1,bInf1,over1,under1})
205 6 robfinch
                5'b1xxxx:       xo1 = infXp;    // qNaN - infinity * zero
206
                5'b01xxx:       xo1 = infXp;    // 'a' infinite
207
                5'b001xx:       xo1 = infXp;    // 'b' infinite
208
                5'b0001x:       xo1 = infXp;    // result overflow
209
                5'b00001:       xo1 = 0;         // underflow
210
                default:        xo1 = ex2[EMSB:0];       // situation normal
211
                endcase
212
 
213
always @(posedge clk)
214
        if (ce)
215 8 robfinch
                casex({aNan1,bNan1,qNaNOut,aInf1,bInf1,over1})
216
                6'b1xxxxx:  mo1 = {1'b0,a1[FMSB:0],{FMSB+1{1'b0}}};
217
        6'bx1xxxx:  mo1 = {1'b0,b1[FMSB:0],{FMSB+1{1'b0}}};
218
                6'bxx1xxx:      mo1 = {1'b0,qNaN|3'd4,{FMSB+1{1'b0}}};  // multiply inf * zero
219
                6'b0001xx:      mo1 = 0; // mul inf's
220
                6'b00001x:      mo1 = 0; // mul inf's
221
                6'b000001:      mo1 = 0; // mul overflow
222 6 robfinch
                default:        mo1 = fracto;
223
                endcase
224
 
225
delay3 u10 (.clk(clk), .ce(ce), .i(sa & sb), .o(sign_exe) );
226
delay1 u11 (.clk(clk), .ce(ce), .i(over1),  .o(overflow) );
227
delay1 u12 (.clk(clk), .ce(ce), .i(over1),  .o(inf) );
228
delay1 u13 (.clk(clk), .ce(ce), .i(under1), .o(underflow) );
229
 
230
assign o = {so1,xo1,mo1};
231
 
232
endmodule
233
 
234
module fpMul_tb();
235
reg clk;
236
 
237
initial begin
238
        clk = 0;
239
end
240
always #10 clk <= ~clk;
241
 
242
fpMul u1 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o1), .sign_exe(sgnx1), .inf(inf1), .overflow(of1), .underflow(uf1));
243 8 robfinch
fpMul u2 (.clk(clk), .ce(1'b1), .a(0), .b(0), .o(o1), .sign_exe(sgnx1), .inf(inf1), .overflow(of1), .underflow(uf1));
244 6 robfinch
 
245
endmodule

powered by: WebSVN 2.1.0

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