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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpDivide.sv] - Blame information for rev 49

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3 49 robfinch
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4 48 robfinch
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8 49 robfinch
//      fpDivide.sv
9 48 robfinch
//    - floating point divider
10
//    - parameterized width
11
//    - IEEE 754 representation
12
//
13
//
14 49 robfinch
// 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 48 robfinch
//
18 49 robfinch
// 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
//      Floating Point Divider
41
//
42 48 robfinch
//Properties:
43
//+-inf * +-inf = -+inf    (this is handled by exOver)
44
//+-inf * 0     = QNaN
45
//+-0 / +-0      = QNaN
46
// ============================================================================
47
 
48
import fp::*;
49
//`define GOLDSCHMIDT   1'b1
50
 
51
module fpDivide(rst, clk, clk4x, ce, ld, op, a, b, o, done, sign_exe, overflow, underflow);
52
// FADD is a constant that makes the divider width a multiple of four and includes eight extra bits.
53
localparam FADD = FPWID==128 ? 9 :
54
                                  FPWID==96 ? 9 :
55
                                  FPWID==84 ? 9 :
56
                                  FPWID==80 ? 9 :
57
                                  FPWID==64 ? 13 :
58
                                  FPWID==52 ? 9 :
59
                                  FPWID==48 ? 10 :
60
                                  FPWID==44 ? 9 :
61
                                  FPWID==42 ? 11 :
62
                                  FPWID==40 ? 8 :
63
                                  FPWID==32 ? 10 :
64
                                  FPWID==24 ? 9 : 11;
65
input rst;
66
input clk;
67
input clk4x;
68
input ce;
69
input ld;
70
input op;
71
input [MSB:0] a, b;
72
output [EX:0] o;
73
output done;
74
output sign_exe;
75
output overflow;
76
output underflow;
77
 
78
// registered outputs
79
reg sign_exe=0;
80
reg inf=0;
81
reg     overflow=0;
82
reg     underflow=0;
83
 
84
reg so;
85
reg [EMSB:0] xo;
86
reg [FX:0] mo;
87
assign o = {so,xo,mo};
88
 
89
// constants
90
wire [EMSB:0] infXp = {EMSB+1{1'b1}};   // infinite / NaN - all ones
91
// The following is the value for an exponent of zero, with the offset
92
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
93
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};       //2^0 exponent
94
// The following is a template for a quiet nan. (MSB=1)
95
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
96
 
97
// variables
98
wire [EMSB+2:0] ex1;    // sum of exponents
99
`ifndef GOLDSCHMIDT
100
wire [(FMSB+FADD)*2-1:0] divo;
101
`else
102
wire [(FMSB+5)*2-1:0] divo;
103
`endif
104
 
105
// Operands
106
wire sa, sb;                    // sign bit
107
wire [EMSB:0] xa, xb;   // exponent bits
108
wire [FMSB+1:0] fracta, fractb;
109
wire a_dn, b_dn;                        // a/b is denormalized
110
wire az, bz;
111
wire aInf, bInf;
112
wire aNan,bNan;
113
wire done1;
114
wire signed [7:0] lzcnt;
115
 
116
// -----------------------------------------------------------
117
// - decode the input operands
118
// - derive basic information
119
// - calculate exponent
120
// - calculate fraction
121
// -----------------------------------------------------------
122
 
123
fpDecomp u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
124
fpDecomp u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
125
 
126
// Compute the exponent.
127
// - correct the exponent for denormalized operands
128
// - adjust the difference by the bias (add 127)
129
// - also factor in the different decimal position for division
130
`ifndef GOLDSCHMIDT
131
assign ex1 = (xa|a_dn) - (xb|b_dn) + bias + FMSB + (FADD-1) - lzcnt - 8'd1;
132
`else
133
assign ex1 = (xa|a_dn) - (xb|b_dn) + bias + FMSB - lzcnt + 8'd4;
134
`endif
135
 
136
// check for exponent underflow/overflow
137
wire under = ex1[EMSB+2];       // MSB set = negative exponent
138
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
139
 
140
// Perform divide
141
// Divider width must be a multiple of four
142
`ifndef GOLDSCHMIDT
143
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));
144
//fpdivr2 #(FMSB+FADD) u2 (.clk4x(clk4x), .ld(ld), .a({3'b0,fracta,8'b0}), .b({3'b0,fractb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
145
wire [(FMSB+FADD)*2-1:0] divo1 = divo[(FMSB+FADD)*2-1:0] << (lzcnt-2);
146
`else
147
DivGoldschmidt #(.WID(FMSB+6),.WHOLE(1),.POINTS(FMSB+5))
148
        u2 (.rst(rst), .clk(clk), .ld(ld), .a({fracta,4'b0}), .b({fractb,4'b0}), .q(divo), .done(done1), .lzcnt(lzcnt));
149
wire [(FMSB+6)*2+1:0] divo1 =
150
        lzcnt > 8'd5 ? divo << (lzcnt-8'd6) :
151
        divo >> (8'd6-lzcnt);
152
        ;
153
`endif
154
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(done1), .o(done));
155
 
156
 
157
// determine when a NaN is output
158
wire qNaNOut = (az&bz)|(aInf&bInf);
159
 
160
always @(posedge clk)
161
// Simulation likes to see these values reset to zero on reset. Otherwise the
162
// values propagate in sim as X's.
163
if (rst) begin
164
        xo <= 1'd0;
165
        mo <= 1'd0;
166
        so <= 1'd0;
167
        sign_exe <= 1'd0;
168
        overflow <= 1'd0;
169
        underflow <= 1'd0;
170
end
171
else if (ce) begin
172
                if (done1) begin
173
                        casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
174
                        5'b1????:               xo <= infXp;    // NaN exponent value
175
                        5'b01???:               xo <= 1'd0;             // divide by inf
176
                        5'b001??:               xo <= infXp;    // divide by zero
177
                        5'b0001?:               xo <= infXp;    // overflow
178
                        5'b00001:               xo <= 1'd0;             // underflow
179
                        default:                xo <= ex1;      // normal or underflow: passthru neg. exp. for normalization
180
                        endcase
181
 
182
                        casez({aNan,bNan,qNaNOut,bInf,bz,over,aInf&bInf,az&bz})
183
                        8'b1???????:    mo <= {1'b1,a[FMSB:0],{FMSB+1{1'b0}}};
184
                        8'b01??????:    mo <= {1'b1,b[FMSB:0],{FMSB+1{1'b0}}};
185
                        8'b001?????:    mo <= {1'b1,qNaN[FMSB:0]|{aInf,1'b0}|{az,bz},{FMSB+1{1'b0}}};
186
                        8'b0001????:    mo <= 1'd0;     // div by inf
187
                        8'b00001???:    mo <= 1'd0;     // div by zero
188
                        8'b000001??:    mo <= 1'd0;     // Inf exponent
189
                        8'b0000001?:    mo <= {1'b1,qNaN|`QINFDIV,{FMSB+1{1'b0}}};      // infinity / infinity
190
                        8'b00000001:    mo <= {1'b1,qNaN|`QZEROZERO,{FMSB+1{1'b0}}};    // zero / zero
191
`ifndef GOLDSCHMIDT
192
                        default:                mo <= divo1[(FMSB+FADD)*2-1:(FADD-2)*2-2];      // plain div
193
`else
194
                        default:                mo <= divo1[(FMSB+6)*2+1:2];    // plain div
195
`endif
196
                        endcase
197
 
198
                        so              <= sa ^ sb;
199
                        sign_exe        <= sa & sb;
200
                        overflow        <= over;
201
                        underflow       <= under;
202
                end
203
        end
204
 
205
endmodule
206
 
207
module fpDividenr(rst, clk, clk4x, ce, ld, op, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
208
input rst;
209
input clk;
210
input clk4x;
211
input ce;
212
input ld;
213
input op;
214
input  [MSB:0] a, b;
215
output [MSB:0] o;
216
input [2:0] rm;
217
output sign_exe;
218
output done;
219
output inf;
220
output overflow;
221
output underflow;
222
 
223
wire [EX:0] o1;
224
wire sign_exe1, inf1, overflow1, underflow1;
225
wire [MSB+3:0] fpn0;
226
wire done1;
227
 
228
fpDivide    #(FPWID) u1 (rst, clk, clk4x, ce, ld, op, a, b, o1, done1, sign_exe1, overflow1, underflow1);
229
fpNormalize #(FPWID) u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
230
fpRound     #(FPWID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
231
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
232
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
233
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
234
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
235
delay2            #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done));
236
endmodule
237
 

powered by: WebSVN 2.1.0

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