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

Subversion Repositories ft816float

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

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

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

powered by: WebSVN 2.1.0

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