1 |
56 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2018-2020 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// DFPSqrt.v
|
9 |
|
|
// - decimal floating point square root
|
10 |
|
|
// - parameterized width
|
11 |
|
|
// - IEEE 754 representation
|
12 |
|
|
//
|
13 |
|
|
//
|
14 |
|
|
// 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 |
|
|
//
|
18 |
|
|
// 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 |
|
|
// ============================================================================
|
41 |
|
|
|
42 |
|
|
import fp::*;
|
43 |
|
|
|
44 |
|
|
module DFPSqrt(rst, clk, ce, ld, a, o, done, sqrinf, sqrneg);
|
45 |
|
|
parameter N=33;
|
46 |
|
|
localparam pShiftAmt =
|
47 |
|
|
FPWID==80 ? 48 :
|
48 |
|
|
FPWID==64 ? 36 :
|
49 |
|
|
FPWID==32 ? 7 : (FMSB+1-16);
|
50 |
|
|
input rst;
|
51 |
|
|
input clk;
|
52 |
|
|
input ce;
|
53 |
|
|
input ld;
|
54 |
|
|
input [N*4+16+4-1:0] a;
|
55 |
|
|
output reg [(N+1)*4*2+16+4-1:0] o;
|
56 |
|
|
output done;
|
57 |
|
|
output sqrinf;
|
58 |
|
|
output sqrneg;
|
59 |
|
|
|
60 |
|
|
// registered outputs
|
61 |
|
|
reg sign_exe;
|
62 |
|
|
reg inf;
|
63 |
|
|
reg overflow;
|
64 |
|
|
reg underflow;
|
65 |
|
|
|
66 |
|
|
wire so;
|
67 |
|
|
wire [15:0] xo;
|
68 |
|
|
wire [(N+1)*4*2-1:0] mo;
|
69 |
|
|
|
70 |
|
|
// constants
|
71 |
|
|
wire [15:0] infXp = 16'h9999; // infinite / NaN - all ones
|
72 |
|
|
// The following is a template for a quiet nan. (MSB=1)
|
73 |
|
|
wire [N*4-1:0] qNaN = {4'h1,{N*4-4{1'b0}}};
|
74 |
|
|
|
75 |
|
|
// variables
|
76 |
|
|
wire [15:0] ex1; // sum of exponents
|
77 |
|
|
wire ex1c;
|
78 |
|
|
wire [(N+1)*4*2-1:0] sqrto;
|
79 |
|
|
|
80 |
|
|
// Operands
|
81 |
|
|
wire sa; // sign bit
|
82 |
|
|
wire sx; // sign of exponent
|
83 |
|
|
wire [15:0] xa; // exponent bits
|
84 |
|
|
wire [N*4-1:0] siga;
|
85 |
|
|
wire a_dn; // a/b is denormalized
|
86 |
|
|
wire az;
|
87 |
|
|
wire aInf;
|
88 |
|
|
wire aNan;
|
89 |
|
|
wire done1;
|
90 |
|
|
wire [7:0] lzcnt;
|
91 |
|
|
wire [N*4-1:0] aa;
|
92 |
|
|
|
93 |
|
|
// -----------------------------------------------------------
|
94 |
|
|
// - decode the input operand
|
95 |
|
|
// - derive basic information
|
96 |
|
|
// - calculate exponent
|
97 |
|
|
// - calculate fraction
|
98 |
|
|
// -----------------------------------------------------------
|
99 |
|
|
|
100 |
|
|
DFPDecomposeReg u1
|
101 |
|
|
(
|
102 |
|
|
.clk(clk),
|
103 |
|
|
.ce(ce),
|
104 |
|
|
.i(a),
|
105 |
|
|
.sgn(sa),
|
106 |
|
|
.sx(sx),
|
107 |
|
|
.exp(xa),
|
108 |
|
|
.sig(siga),
|
109 |
|
|
.xz(a_dn),
|
110 |
|
|
.vz(az),
|
111 |
|
|
.inf(aInf),
|
112 |
|
|
.nan(aNan)
|
113 |
|
|
);
|
114 |
|
|
|
115 |
|
|
BCDAddN #(.N(4)) u4 (.ci(1'b0), .a(xa), .b(16'h0001), .o(ex1), .co() );
|
116 |
|
|
BCDSRL #(.N(4)) u5 (.ci(1'b0), .i(ex1), .o(xo), .co());
|
117 |
|
|
|
118 |
|
|
assign so = 1'b0; // square root of positive numbers only
|
119 |
|
|
assign mo = aNan ? {4'h1,aa[N*4-1:0],{N*4{1'b0}}} : sqrto; //(sqrto << pShiftAmt);
|
120 |
|
|
assign sqrinf = aInf;
|
121 |
|
|
assign sqrneg = !az & so;
|
122 |
|
|
|
123 |
|
|
wire [(N+1)*4-1:0] siga1 = xa[0] ? {siga,4'h0} : {4'h0,siga};
|
124 |
|
|
|
125 |
|
|
wire ldd;
|
126 |
|
|
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(ld), .o(ldd));
|
127 |
|
|
|
128 |
|
|
// Ensure an even number of digits are processed.
|
129 |
|
|
dfisqrt #((N+2)&-2) u2
|
130 |
|
|
(
|
131 |
|
|
.rst(rst),
|
132 |
|
|
.clk(clk),
|
133 |
|
|
.ce(ce),
|
134 |
|
|
.ld(ldd),
|
135 |
|
|
.a({4'h0,siga1}),
|
136 |
|
|
.o(sqrto),
|
137 |
|
|
.done(done)
|
138 |
|
|
);
|
139 |
|
|
|
140 |
|
|
always @*
|
141 |
|
|
casez({aNan,sqrinf,sqrneg})
|
142 |
|
|
3'b1??: o <= {1'b1,sa,1'b0,sx,xa,mo};
|
143 |
|
|
3'b01?: o <= {1'b1,sa,1'b1,sx,4'h1,qNaN|4'h5,{N*4-4{1'b0}}};
|
144 |
|
|
3'b001: o <= {1'b1,sa,1'b0,sx,4'h1,qNaN|4'h6,{N*4-4{1'b0}}};
|
145 |
|
|
default: o <= {1'b0,1'b1,1'b0,sx,xo,mo};
|
146 |
|
|
endcase
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
endmodule
|
150 |
|
|
|
151 |
|
|
module DFPSqrtnr(rst, clk, ce, ld, a, o, rm, done, inf, sqrinf, sqrneg);
|
152 |
|
|
parameter N=33;
|
153 |
|
|
input rst;
|
154 |
|
|
input clk;
|
155 |
|
|
input ce;
|
156 |
|
|
input ld;
|
157 |
|
|
input [N*4+16+4-1:0] a;
|
158 |
|
|
output [N*4+16+4-1:0] o;
|
159 |
|
|
input [2:0] rm;
|
160 |
|
|
output done;
|
161 |
|
|
output inf;
|
162 |
|
|
output sqrinf;
|
163 |
|
|
output sqrneg;
|
164 |
|
|
|
165 |
|
|
wire [(N+1)*4*2+16+4-1:0] o1;
|
166 |
|
|
wire inf1;
|
167 |
|
|
wire [N*4+16+4-1+4:0] fpn0;
|
168 |
|
|
wire done1;
|
169 |
|
|
wire done2;
|
170 |
|
|
|
171 |
|
|
DFPSqrt #(.N(N)) u1 (rst, clk, ce, ld, a, o1, done1, sqrinf, sqrneg);
|
172 |
|
|
DFPNormalize #(.N(N)) u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
|
173 |
|
|
DFPRound #(.N(N)) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
|
174 |
|
|
delay2 #(1) u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
|
175 |
|
|
delay2 #(1) u8(.clk(clk), .ce(ce), .i(done1), .o(done2));
|
176 |
|
|
assign done = done1&done2;
|
177 |
|
|
|
178 |
|
|
endmodule
|