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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DivGoldschmidt.v] - Blame information for rev 90

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

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2017-2018  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      DivGoldschmidt.v
9
//              
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
//
25
// ============================================================================
26
//
27
`include "fpConfig.sv"
28
 
29
module DivGoldschmidt(rst, clk, ld, a, b, q, done, lzcnt);
30
parameter FPWID=32;
31
parameter WHOLE=16;
32
parameter POINTS=16;
33
parameter LEFT=1'b1;
34
localparam SIZE=FPWID+WHOLE;
35
localparam POINTS2 = POINTS+WHOLE;
36
input rst;
37
input clk;
38
input ld;
39
input [FPWID-1:0] a;
40
input [FPWID-1:0] b;
41
output reg [FPWID*2-1:0] q;
42
output reg done;
43
output reg [7:0] lzcnt;
44
parameter IDLE = 2'd0;
45
parameter DIV = 2'd1;
46
parameter DONE = 2'd2;
47
parameter DIV2 = 2'd3;
48
 
49
integer n;
50
// Scale D so it is between 0 < D < 1 (shift)
51
reg [SIZE-1:0] F;
52
reg [SIZE*3-1:0] N, D;
53
wire [SIZE*3-1:0] N1, D1;
54
assign N1 = N * F;
55
assign D1 = D * F;
56
reg [1:0] state = IDLE;
57
reg [7:0] count = 0;
58
reg [7:0] lzcnt2;
59
wire [7:0] shft;
60
 
61
// Count the leading zeros on the b side input. Determines how much
62
// shifting is required.
63
always @*
64
begin
65
        lzcnt2 = 8'd0;
66
        if (b[FPWID-1]==1'b0)
67
                for (n = FPWID-2; n >= 0; n = n - 1)
68
                        if(b[n] && lzcnt2==8'd0)
69
                                lzcnt2 = (FPWID-1)-n;
70
end
71
 
72
 
73
// Count the leading zeros in the output. the float divider uses this.
74
always @*
75
begin
76
        lzcnt = 8'd0;
77
        if (q[FPWID*2-1]==1'b0)
78
                for (n = FPWID*2-2; n >= 0; n = n - 1)
79
                        if(q[n] && lzcnt==8'd0)
80
                                lzcnt = (FPWID*2-1)-n;
81
end
82
 
83
wire shift_left = lzcnt2 > WHOLE;
84
assign shft = shift_left ? lzcnt2-WHOLE : WHOLE-lzcnt2;
85
//assign done = (state==IDLE && !ld)||state==DONE;
86
 
87
always @(posedge clk)
88
if (rst) begin
89
        done <= 1'b0;
90
        count <= 6'd0;
91
        state <= IDLE;
92
end
93
else begin
94
        done <= 1'b0;
95
case(state)
96
IDLE:
97
        begin
98
                if (ld) begin
99
                        // Shifting the numerator and denomintor right or left using a barrel
100
                        // or funnel shifter is what gives Goldschmidt a lot of it's performance.
101
                        // Most of the divide is being performed by shifting.
102
                        // For most floating point numbers shifting left isn't required as the
103
                        // number is always between 1.0 and 2.0. Instead typically only a single
104
                        // shift to the right is required. For fixed point numbers however, we
105
                        // probably want to be able to shift left, hence the LEFT parameter.
106
                        // With no left shifting the only impact is for denormal numbers which
107
                        // take longer for the divide to converge.
108
                        if (shift_left) begin
109
                                if (LEFT) begin
110
                                        N <= {16'd0,a,{WHOLE{1'b0}}} << shft;
111
                                        D <= {16'd0,b,{WHOLE{1'd0}}} << shft;
112
                                        F <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}} << shft);
113
                                end
114
                                else begin
115
                                        N <= {16'd0,a,{WHOLE{1'b0}}};
116
                                        D <= {16'd0,b,{WHOLE{1'd0}}};
117
                                        F <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}});
118
                                end
119
                        end
120
                        else begin
121
                                N <= {16'd0,a,{WHOLE{1'b0}}} >> shft;
122
                                D <= {16'd0,b,{WHOLE{1'd0}}} >> shft;
123
                                F <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}} >> shft);
124
                        end
125
                        count <= 0;
126
                        state <= DIV;
127
                end
128
        end
129
DIV:
130
        begin
131
                $display("C: %d N: %x D: %x F: %x", count, N,D,F);
132
                N <= N1[SIZE*3-1:POINTS2] + N1[POINTS2-1];
133
                D <= D1[SIZE*3-1:POINTS2] + D1[POINTS2-1];
134
                F <= {16'd2,{POINTS2{1'd0}}} - (D1[SIZE*3-1:POINTS2] + D1[POINTS2-1]);
135
//              q <= N1[SIZE*2-1:POINTS2] + N1[POINTS2-1];
136
                if (D[SIZE*3-1:0]=={2'h1,{POINTS2{1'd0}}})
137
                        state <= DONE;
138
                count <= count + 1;
139
        end
140
DONE:
141
        begin
142
                done <= 1'b1;
143
                q <= N[SIZE*3-1:0];
144
                state <= IDLE;
145
        end
146
endcase
147
end
148
 
149
endmodule
150
 
151
module G_divider_tb();
152
parameter FPWID=4;
153
reg rst;
154
reg clk;
155
reg ld;
156
wire done;
157
wire [FPWID*2-1:0] qo;
158
reg [3:0] state;
159
reg [3:0] a, b;
160
reg [7:0] count;
161
 
162
initial begin
163
        clk = 1;
164
        rst = 0;
165
        #100 rst = 1;
166
        #100 rst = 0;
167
        #100 ld = 1;
168
        #150 ld = 0;
169
end
170
 
171
always #10 clk = ~clk;  //  50 MHz
172
 
173
always @(posedge clk)
174
if (rst) begin
175
        state <= 3'd0;
176
        count = 0;
177
end
178
else begin
179
case(state)
180
3'd0:
181
        begin
182
                ld <= 1;
183
                a <= count[7:4];
184
                b <= count[3:0];
185
        end
186
3'd1:
187
        if (done) begin
188
                $display("C: %x Q: %x  f: %x", count, qo, f0);
189
                state <= 3'd2;
190
        end
191
3'd2:
192
        begin
193
                count <= count + 8'd1;
194
                state <= 3'd0;
195
        end
196
endcase
197
end
198
 
199
DivGoldschmidt #(.FPWID(FPWID),.WHOLE(1),.POINTS(3)) u00
200
(
201
        .rst(rst),
202
        .clk(clk),
203
        .ld(ld),
204
//      .sgn(1'b1),
205
//      .isDivi(1'b0),
206
        .a(a),
207
        .b(b),
208
//      .imm(64'd123),
209
        .q(qo),
210
//      .ro(ro),
211
//      .dvByZr(),
212
        .done(done),
213
        .lzcnt()
214
);
215
 
216
endmodule
217
 

powered by: WebSVN 2.1.0

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