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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [positVerilog/] [positDivide.sv] - Blame information for rev 81

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

Line No. Rev Author Line
1 43 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      positDivide.sv
9
//    - posit number division function
10
//    - parameterized width
11
//
12
// Parts of this code extracted from the PACoGen project:
13
//    Copyright (c) 2019, Manish Kumar Jaiswal
14
//    All rights reserved.
15
//
16
// BSD 3-Clause License
17
// Redistribution and use in source and binary forms, with or without
18
// modification, are permitted provided that the following conditions are met:
19
//
20
// 1. Redistributions of source code must retain the above copyright notice, this
21
//    list of conditions and the following disclaimer.
22
//
23
// 2. Redistributions in binary form must reproduce the above copyright notice,
24
//    this list of conditions and the following disclaimer in the documentation
25
//    and/or other materials provided with the distribution.
26
//
27
// 3. Neither the name of the copyright holder nor the names of its
28
//    contributors may be used to endorse or promote products derived from
29
//    this software without specific prior written permission.
30
//
31
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
35
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
//
42
// ============================================================================
43
 
44 48 robfinch
import posit::*;
45 43 robfinch
 
46
module positDivide(clk, ce, a, b, o, start, done, zero, inf);
47
localparam rs = $clog2(PSTWID-1)-1;
48
input clk;
49
input ce;
50
input [PSTWID-1:0] a;
51
input [PSTWID-1:0] b;
52
output reg [PSTWID-1:0] o;
53
input start;
54 48 robfinch
output reg done;
55
output reg zero;
56
output reg inf;
57 43 robfinch
 
58
localparam N = PSTWID;
59
localparam M = N-es;
60
localparam Bs = $clog2(N-1);
61
localparam NR_Iter = M > 88 ? 4 : M > 44 ? 3 : M > 22 ? 2 : M > 11 ? 1 : 0;             // 2 for 32 bits, 1 for 16 bits, 0 for 8bits
62
localparam NRB = 2**NR_Iter;
63
localparam IW_MAX = 10;                                                 //Max intial approximation storage bit-width
64
localparam IW = 10;//(NRB == 1 ) ? M : (M/NRB*2 + ((M%NRB > 0) ? 1 : 0));       //(must be <= IW_MAX) 1/4th width of Mantissa: inverse width to be used in NR iterations multiplication
65
localparam AW_MAX = 11;                                                 //Max Address width of the intial approximation storage
66
localparam AW = 11;//(NRB == 1) ? M : (M/NRB*2 + ((M%NRB > 0) ? 1 : 0));        //Actual address width used for initial approximation (AW must be <= AW_MAX)
67
 
68
wire sa, sb, so;
69
wire [rs:0] rgma, rgmb;
70
wire rgsa, rgsb;
71
wire [es-1:0] expa, expb;
72
wire [M-1:0] siga, sigb;
73
wire zera, zerb;
74
wire infa, infb;
75
 
76 48 robfinch
positDecompose #(PSTWID) u1 (
77 43 robfinch
  .i(a),
78
  .sgn(sa),
79
  .rgs(rgsa),
80
  .rgm(rgma),
81
  .exp(expa),
82
  .sig(siga),
83
  .zer(zera),
84
  .inf(infa)
85
);
86
 
87 48 robfinch
positDecompose #(PSTWID) u2 (
88 43 robfinch
  .i(b),
89
  .sgn(sb),
90
  .rgs(rgsb),
91
  .rgm(rgmb),
92
  .exp(expb),
93
  .sig(sigb),
94
  .zer(zerb),
95
  .inf(infb)
96
);
97
 
98
wire [M:0] m1 = siga << 1;
99
wire [M:0] m2 = sigb << 1;
100
wire [15:0] m2_inv0_tmp;
101
 
102
assign so = sa ^ sb;
103
wire [Bs+1:0] argma = rgsa ? {2'b0,rgma} : -rgma;
104
wire [Bs+1:0] argmb = rgsb ? {2'b0,rgmb} : -rgmb;
105
 
106
generate begin : gDivLut
107
if (M < AW_MAX)
108 48 robfinch
div_lut lut1 (.clk(clk), .ce(ce), .i({m2[M-1:0],{AW_MAX-M{1'b0}}}), .o(m2_inv0_tmp));
109 43 robfinch
else if (M==AW_MAX)
110 48 robfinch
div_lut lut1 (.clk(clk), .ce(ce), .i(m2[M-1:0]), .o(m2_inv0_tmp));
111 43 robfinch
else if (M > AW_MAX)
112 48 robfinch
div_lut lut1 (.clk(clk), .ce(ce), .i(m2[M-1:M-AW_MAX]), .o(m2_inv0_tmp));
113 43 robfinch
end
114
endgenerate
115
 
116
wire [IW:0] m2_inv0;
117
assign m2_inv0 = m2_inv0_tmp[15:5];
118
 
119
wire [2*M+1:0] div_m;
120
wire [2*M+1:0] div_m4;
121
 
122
genvar i;
123
generate begin
124
        wire [2*M+1:0] m2_inv [NR_Iter:0];
125
 
126
        if (NR_Iter > 0) begin
127
                assign m2_inv[0] = {1'b0,m2_inv0,{M-IW{1'b0}},{M{1'b0}}};
128
                wire [2*M+1:0] m2_inv_X_m2 [NR_Iter-1:0];
129
                wire [M+1:0] two_m2_inv_X_m2 [NR_Iter-1:0];
130 48 robfinch
                for (i = 0; i < NR_Iter; i=i+1) begin : NR_Iteration
131 43 robfinch
                        assign m2_inv_X_m2[i] = {m2_inv[i][2*M:2*M-IW*(i+1)],{2*M-IW*(i+1)-M{1'b0}}} * m2;
132 48 robfinch
                        assign two_m2_inv_X_m2[i] = {1'b1,{M{1'b0}}} - {1'b0,m2_inv_X_m2[i][2*M+1:M+3],|m2_inv_X_m2[i][M+2:0]};
133 43 robfinch
                        assign m2_inv[i+1] = {m2_inv[i][2*M:2*M-IW*(i+1)],{M-IW*(i+1){1'b0}}} * {two_m2_inv_X_m2[i][M-1:0],1'b0};
134
                end
135
        end
136
        else begin
137
                assign m2_inv[0] = {1'b0,m2_inv0,{M{1'b0}}};
138
        end
139 48 robfinch
        assign div_m4 = ~|sigb[M-2:0] ? {1'b0,m1,{M{1'b0}}} : m1 * m2_inv[NR_Iter][2*M:M];
140 43 robfinch
end
141
endgenerate
142
 
143 48 robfinch
delay #(.WID(PSTWID), .DEP(4)) ud4 (.clk(clk), .ce(ce), .i(div_m4), .o(div_m));
144
delay #(.WID(1),.DEP(5)) ud1 (.clk(clk), .ce(ce), .i(start), .o(done));
145
delay #(.WID(1),.DEP(5)) ud2 (.clk(clk), .ce(ce), .i(infa|infb), .o(inf));
146
delay #(.WID(1),.DEP(5)) ud3 (.clk(clk), .ce(ce), .i(zera|zerb), .o(zero));
147 43 robfinch
 
148 48 robfinch
wire div_m_udf = div_m[2*M+1];
149
wire [2*M+1:0] div_mN = ~div_m_udf ? div_m << 1'b1 : div_m;
150 43 robfinch
 
151
//Exponent and Regime Computation
152
wire bin = (~|sigb[M-2:0] | div_m_udf) ? 0 : 1;
153
wire [Bs+es+1:0] div_e = {argma, expa} - {argmb, expb} - bin;// 1 + ~|mant2 + div_m_udf;
154
wire [es-1:0] e_o = div_e[es-1:0];
155
wire [Bs+es:0] exp_oN = div_e[es+Bs+1] ? -div_e[es+Bs:0] : div_e[es+Bs:0];
156
wire [Bs:0] r_o = (~div_e[es+Bs+1] || |(exp_oN[es-1:0])) ? exp_oN[Bs+es:es] + 1 : exp_oN[es+Bs:es];
157
 
158
//Exponent and Mantissa Packing
159
wire [2*N-1+3:0] tmp_o = {{N{~div_e[es+Bs+1]}},div_e[es+Bs+1],e_o,div_mN[2*M:2*M-(N-es-1)+1], div_mN[2*M-(N-es-1):2*M-(N-es-1)-1],|div_mN[2*M-(N-es-1)-2:0] };
160
 
161
//Including Regime bits in Exponent-Mantissa Packing
162
wire [3*N-1+3:0] tmp1_o = {tmp_o,{N{1'b0}}} >> (r_o[Bs] ? {Bs{1'b1}} : r_o);
163
 
164
//Rounding RNE : ulp_add = G.(R + S) + L.G.(~(R+S))
165
wire L = tmp1_o[N+4], G = tmp1_o[N+3], R = tmp1_o[N+2], St = |tmp1_o[N+1:0],
166
     ulp = ((G & (R | St)) | (L & G & ~(R | St)));
167
wire [N-1:0] rnd_ulp = {{N-1{1'b0}},ulp};
168
 
169
wire [N:0] tmp1_o_rnd_ulp = tmp1_o[2*N-1+3:N+3] + rnd_ulp;
170
wire [N-1:0] tmp1_o_rnd = (r_o < M-2) ? tmp1_o_rnd_ulp[N-1:0] : tmp1_o[2*N-1+3:N+3];
171
 
172
//Final Output
173
wire [N-1:0] tmp1_oN = so ? -tmp1_o_rnd : tmp1_o_rnd;
174 48 robfinch
always @(posedge clk)
175
  if (ce) o <= inf|zero|(~div_mN[2*M+1]) ? {inf,{N-1{1'b0}}} : {so, tmp1_oN[N-1:1]};
176 43 robfinch
 
177
endmodule

powered by: WebSVN 2.1.0

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