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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpDiv.v] - Blame information for rev 22

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 10 robfinch
//   \\__/ o\    (C) 2006-2018  Robert Finch, Waterloo
5 6 robfinch
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch<remove>@finitron.ca
7
//       ||
8
//
9 8 robfinch
//      fpDiv.v
10
//    - floating point divider
11
//    - parameterized width
12
//    - IEEE 754 representation
13
//
14
//
15 6 robfinch
// This source file is free software: you can redistribute it and/or modify 
16
// it under the terms of the GNU Lesser General Public License as published 
17
// by the Free Software Foundation, either version 3 of the License, or     
18
// (at your option) any later version.                                      
19
//                                                                          
20
// This source file is distributed in the hope that it will be useful,      
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
23
// GNU General Public License for more details.                             
24
//                                                                          
25
// You should have received a copy of the GNU General Public License        
26
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
27 8 robfinch
//                                                                          
28
//      Floating Point Multiplier / Divider
29 6 robfinch
//
30 8 robfinch
//Properties:
31
//+-inf * +-inf = -+inf    (this is handled by exOver)
32
//+-inf * 0     = QNaN
33
//+-0 / +-0      = QNaN
34 6 robfinch
// ============================================================================
35 8 robfinch
 
36 13 robfinch
`include "fp_defines.v"
37 14 robfinch
`define GOLDSCHMIDT     1'b1
38 6 robfinch
 
39 14 robfinch
module fpDiv(rst, clk, ce, ld, op, a, b, o, done, sign_exe, overflow, underflow);
40 13 robfinch
 
41 8 robfinch
parameter WID = 128;
42 6 robfinch
localparam MSB = WID-1;
43 8 robfinch
localparam EMSB = WID==128 ? 14 :
44
                  WID==96 ? 14 :
45
                  WID==80 ? 14 :
46 6 robfinch
                  WID==64 ? 10 :
47
                                  WID==52 ? 10 :
48 10 robfinch
                                  WID==48 ? 11 :
49 6 robfinch
                                  WID==44 ? 10 :
50
                                  WID==42 ? 10 :
51
                                  WID==40 ?  9 :
52
                                  WID==32 ?  7 :
53
                                  WID==24 ?  6 : 4;
54 8 robfinch
localparam FMSB = WID==128 ? 111 :
55
                  WID==96 ? 79 :
56
                  WID==80 ? 63 :
57 6 robfinch
                  WID==64 ? 51 :
58
                                  WID==52 ? 39 :
59 10 robfinch
                                  WID==48 ? 34 :
60 6 robfinch
                                  WID==44 ? 31 :
61
                                  WID==42 ? 29 :
62
                                  WID==40 ? 28 :
63
                                  WID==32 ? 22 :
64
                                  WID==24 ? 15 : 9;
65 13 robfinch
// FADD is a constant that makes the divider width a multiple of four and includes eight extra bits.                    
66
localparam FADD = WID==128 ? 9 :
67
                                  WID==96 ? 9 :
68
                                  WID==80 ? 9 :
69
                                  WID==64 ? 13 :
70
                                  WID==52 ? 9 :
71
                                  WID==48 ? 10 :
72
                                  WID==44 ? 9 :
73
                                  WID==42 ? 11 :
74
                                  WID==40 ? 8 :
75
                                  WID==32 ? 10 :
76
                                  WID==24 ? 9 : 11;
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 14 robfinch
input rst;
81 6 robfinch
input clk;
82
input ce;
83
input ld;
84 13 robfinch
input op;
85 6 robfinch
input [MSB:0] a, b;
86 8 robfinch
output [EX:0] o;
87 6 robfinch
output done;
88
output sign_exe;
89
output overflow;
90
output underflow;
91
 
92
// registered outputs
93 14 robfinch
reg sign_exe=0;
94
reg inf=0;
95
reg     overflow=0;
96
reg     underflow=0;
97 6 robfinch
 
98
reg so;
99
reg [EMSB:0] xo;
100 8 robfinch
reg [FX:0] mo;
101 6 robfinch
assign o = {so,xo,mo};
102
 
103
// constants
104
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
105
// The following is the value for an exponent of zero, with the offset
106
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
107
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
108
// The following is a template for a quiet nan. (MSB=1)
109
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
110
 
111
// variables
112
wire [EMSB+2:0] ex1;     // sum of exponents
113 14 robfinch
`ifndef GOLDSCHMIDT
114 13 robfinch
wire [(FMSB+FADD)*2-1:0] divo;
115 14 robfinch
`else
116
wire [(FMSB+5)*2-1:0] divo;
117
`endif
118 6 robfinch
 
119
// Operands
120
wire sa, sb;                    // sign bit
121
wire [EMSB:0] xa, xb;    // exponent bits
122
wire [FMSB+1:0] fracta, fractb;
123
wire a_dn, b_dn;                        // a/b is denormalized
124
wire az, bz;
125
wire aInf, bInf;
126 8 robfinch
wire aNan,bNan;
127 10 robfinch
wire done1;
128 14 robfinch
wire signed [7:0] lzcnt;
129 6 robfinch
 
130
// -----------------------------------------------------------
131
// - decode the input operands
132
// - derive basic information
133
// - calculate exponent
134
// - calculate fraction
135
// -----------------------------------------------------------
136
 
137 8 robfinch
fpDecomp #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
138
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
139 6 robfinch
 
140
// Compute the exponent.
141
// - correct the exponent for denormalized operands
142
// - adjust the difference by the bias (add 127)
143
// - also factor in the different decimal position for division
144 14 robfinch
`ifndef GOLDSCHMIDT
145
assign ex1 = (xa|a_dn) - (xb|b_dn) + bias + FMSB + (FADD-1) - lzcnt - 8'd1;
146
`else
147
assign ex1 = (xa|a_dn) - (xb|b_dn) + bias + FMSB - lzcnt + 8'd4;
148
`endif
149 6 robfinch
 
150
// check for exponent underflow/overflow
151
wire under = ex1[EMSB+2];       // MSB set = negative exponent
152
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
153
 
154
// Perform divide
155 13 robfinch
// Divider width must be a multiple of four
156 14 robfinch
`ifndef GOLDSCHMIDT
157 13 robfinch
fpdivr16 #(FMSB+FADD) u2 (.clk(clk), .ld(ld), .a({3'b0,fracta,8'b0}), .b({3'b0,fractb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
158
wire [(FMSB+FADD)*2-1:0] divo1 = divo[(FMSB+FADD)*2-1:0] << (lzcnt-2);
159 14 robfinch
`else
160
DivGoldschmidt #(.WID(FMSB+6),.WHOLE(1),.POINTS(FMSB+5))
161
        u2 (.rst(rst), .clk(clk), .ld(ld), .a({fracta,4'b0}), .b({fractb,4'b0}), .q(divo), .done(done1), .lzcnt(lzcnt));
162
wire [(FMSB+6)*2+1:0] divo1 =
163
        lzcnt > 8'd5 ? divo << (lzcnt-8'd6) :
164
        divo >> (8'd6-lzcnt);
165
        ;
166
`endif
167 10 robfinch
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(done1), .o(done));
168 6 robfinch
 
169 10 robfinch
 
170 6 robfinch
// determine when a NaN is output
171
wire qNaNOut = (az&bz)|(aInf&bInf);
172
 
173
always @(posedge clk)
174 14 robfinch
// Simulation likes to see these values reset to zero on reset. Otherwise the
175
// values propagate in sim as X's.
176
if (rst) begin
177
        xo <= 1'd0;
178
        mo <= 1'd0;
179
        so <= 1'd0;
180
        sign_exe <= 1'd0;
181
        overflow <= 1'd0;
182
        underflow <= 1'd0;
183
end
184
else if (ce) begin
185 10 robfinch
                if (done1) begin
186
                        casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
187 14 robfinch
                        5'b1????:               xo <= infXp;    // NaN exponent value
188
                        5'b01???:               xo <= 1'd0;             // divide by inf
189
                        5'b001??:               xo <= infXp;    // divide by zero
190
                        5'b0001?:               xo <= infXp;    // overflow
191
                        5'b00001:               xo <= 1'd0;             // underflow
192
                        default:                xo <= ex1;      // normal or underflow: passthru neg. exp. for normalization
193 6 robfinch
                        endcase
194
 
195 13 robfinch
                        casez({aNan,bNan,qNaNOut,bInf,bz,over,aInf&bInf,az&bz})
196 14 robfinch
                        8'b1???????:    mo <= {1'b1,a[FMSB:0],{FMSB+1{1'b0}}};
197
                        8'b01??????:    mo <= {1'b1,b[FMSB:0],{FMSB+1{1'b0}}};
198
                        8'b001?????:    mo <= {1'b1,qNaN[FMSB:0]|{aInf,1'b0}|{az,bz},{FMSB+1{1'b0}}};
199
                        8'b0001????:    mo <= 1'd0;     // div by inf
200
                        8'b00001???:    mo <= 1'd0;     // div by zero
201
                        8'b000001??:    mo <= 1'd0;     // Inf exponent
202
                        8'b0000001?:    mo <= {1'b1,qNaN|`QINFDIV,{FMSB+1{1'b0}}};      // infinity / infinity
203
                        8'b00000001:    mo <= {1'b1,qNaN|`QZEROZERO,{FMSB+1{1'b0}}};    // zero / zero
204
`ifndef GOLDSCHMIDT
205
                        default:                mo <= divo1[(FMSB+FADD)*2-1:(FADD-2)*2-2];      // plain div
206
`else
207
                        default:                mo <= divo1[(FMSB+6)*2+1:2];    // plain div
208
`endif
209 6 robfinch
                        endcase
210
 
211 14 robfinch
                        so              <= sa ^ sb;
212
                        sign_exe        <= sa & sb;
213
                        overflow        <= over;
214
                        underflow       <= under;
215 6 robfinch
                end
216
        end
217
 
218
endmodule
219 10 robfinch
 
220 14 robfinch
module fpDivnr(rst, clk, ce, ld, op, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
221 10 robfinch
parameter WID=32;
222
localparam MSB = WID-1;
223
localparam EMSB = WID==128 ? 14 :
224
                  WID==96 ? 14 :
225
                  WID==80 ? 14 :
226
                  WID==64 ? 10 :
227
                                  WID==52 ? 10 :
228
                                  WID==48 ? 11 :
229
                                  WID==44 ? 10 :
230
                                  WID==42 ? 10 :
231
                                  WID==40 ?  9 :
232
                                  WID==32 ?  7 :
233
                                  WID==24 ?  6 : 4;
234
localparam FMSB = WID==128 ? 111 :
235
                  WID==96 ? 79 :
236
                  WID==80 ? 63 :
237
                  WID==64 ? 51 :
238
                                  WID==52 ? 39 :
239
                                  WID==48 ? 34 :
240
                                  WID==44 ? 31 :
241
                                  WID==42 ? 29 :
242
                                  WID==40 ? 28 :
243
                                  WID==32 ? 22 :
244
                                  WID==24 ? 15 : 9;
245
 
246
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
247
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
248 14 robfinch
input rst;
249 10 robfinch
input clk;
250
input ce;
251
input ld;
252 13 robfinch
input op;
253 10 robfinch
input  [MSB:0] a, b;
254
output [MSB:0] o;
255
input [2:0] rm;
256
output sign_exe;
257
output done;
258
output inf;
259
output overflow;
260
output underflow;
261
 
262
wire [EX:0] o1;
263
wire sign_exe1, inf1, overflow1, underflow1;
264
wire [MSB+3:0] fpn0;
265
wire done1;
266
 
267 14 robfinch
fpDiv       #(WID) u1 (rst, clk, ce, ld, op, a, b, o1, done1, sign_exe1, overflow1, underflow1);
268 10 robfinch
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(underflow1), .i(o1), .o(fpn0) );
269
fpRoundReg  #(WID) 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 14 robfinch
delay2            #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done));
275 10 robfinch
endmodule
276
 

powered by: WebSVN 2.1.0

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