1 |
39 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2020 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// positSqrt.v
|
9 |
|
|
// - posit number square root function
|
10 |
|
|
// - parameterized width
|
11 |
|
|
//
|
12 |
|
|
//
|
13 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
14 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
15 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
16 |
|
|
// (at your option) any later version.
|
17 |
|
|
//
|
18 |
|
|
// This source file is distributed in the hope that it will be useful,
|
19 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
// GNU General Public License for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with this program. If not, see .
|
25 |
|
|
//
|
26 |
|
|
// ============================================================================
|
27 |
|
|
|
28 |
|
|
`include "positConfig.sv"
|
29 |
|
|
|
30 |
|
|
module positSqrt(clk, ce, i, o, start, done, zero, inf);
|
31 |
|
|
`include "positSize.sv"
|
32 |
|
|
localparam rs = $clog2(PSTWID-1)-1;
|
33 |
|
|
input clk;
|
34 |
|
|
input ce;
|
35 |
|
|
input [PSTWID-1:0] i;
|
36 |
|
|
output reg [PSTWID-1:0] o;
|
37 |
|
|
input start;
|
38 |
|
|
output done;
|
39 |
|
|
output zero;
|
40 |
|
|
output inf;
|
41 |
|
|
|
42 |
|
|
wire si, so;
|
43 |
|
|
wire [rs:0] rgmi;
|
44 |
|
|
wire rgsi;
|
45 |
|
|
wire [es-1:0] expi;
|
46 |
|
|
wire [PSTWID-es-1:0] sigi;
|
47 |
|
|
wire zeri;
|
48 |
|
|
wire infi;
|
49 |
|
|
wire inf = infi;
|
50 |
|
|
wire zero = zeri;
|
51 |
|
|
|
52 |
|
|
positDecompose #(PSTWID,es) u1 (
|
53 |
|
|
.i(i),
|
54 |
|
|
.sgn(si),
|
55 |
|
|
.rgs(rgsi),
|
56 |
|
|
.rgm(rgmi),
|
57 |
|
|
.exp(expi),
|
58 |
|
|
.sig(sigi),
|
59 |
|
|
.zer(zeri),
|
60 |
|
|
.inf(infi)
|
61 |
|
|
);
|
62 |
|
|
|
63 |
|
|
assign so = si; // square root of positive numbers only
|
64 |
41 |
robfinch |
// Compute length of significand. This length is needed to align the
|
65 |
|
|
// significand input to the square root module.
|
66 |
|
|
//wire [rs+1:0] rgml1 = rgsi ? rgmi + 2'd2 : rgmi + 2'd1;
|
67 |
|
|
//wire [rs+1:0] sigl = PSTWID-rgml1-es-1;
|
68 |
|
|
// The length could be zero or less
|
69 |
|
|
//wire [rs:0] sigl1 = sigl[rs+1] ? {rs{1'b0}} : sigl;
|
70 |
|
|
|
71 |
|
|
// Compute exponent
|
72 |
39 |
robfinch |
wire [rs+1:0] rgm1 = rgsi ? rgmi : -rgmi;
|
73 |
|
|
wire [rs+es+1:0] rx1 = {rgm1,expi};
|
74 |
|
|
// If exponent is odd, make it even. May need to shift the significand later.
|
75 |
|
|
wire [rs+es+1:0] rxtmp = {{2{rx1[rs+es+1]}},rx1} >> 1; // right shift takes square root of exponent
|
76 |
|
|
|
77 |
|
|
assign sqrinf = infi;
|
78 |
|
|
assign sqrneg = so;
|
79 |
|
|
// If the exponent was made even, shift the significand left.
|
80 |
41 |
robfinch |
wire [PSTWID-1:0] sig1 = (rx1[0] ^ ~es[0]) ? {sigi,1'b0} : {1'b0,sigi};
|
81 |
39 |
robfinch |
|
82 |
|
|
wire ldd;
|
83 |
|
|
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(start), .o(ldd));
|
84 |
|
|
wire [PSTWID*3-1:0] sqrto;
|
85 |
|
|
|
86 |
|
|
wire [rs:0] lzcnt;
|
87 |
|
|
|
88 |
|
|
// iqsrt2 left aligns the number
|
89 |
|
|
isqrt2 #(PSTWID*3/2) u2
|
90 |
|
|
(
|
91 |
|
|
.rst(rst),
|
92 |
|
|
.clk(clk),
|
93 |
|
|
.ce(ce),
|
94 |
|
|
.ld(ldd),
|
95 |
41 |
robfinch |
// Align the input according to odd/even length
|
96 |
|
|
.a({sig1,{PSTWID/2{1'b0}}}),
|
97 |
39 |
robfinch |
.o(sqrto),
|
98 |
|
|
.done(done),
|
99 |
|
|
.lzcnt(lzcnt)
|
100 |
|
|
);
|
101 |
|
|
|
102 |
|
|
// There should not be very many leading zeros in the number as the number is
|
103 |
|
|
// always between 1 and 2, so the square root is between 1.0 and 1.414....
|
104 |
|
|
// May want to change the leading zero detect to be a little more efficient.
|
105 |
|
|
//positCntlz #(.PSTWID(PSTWID)) u4 (.i(sqrto[PSTWID-1:0]), .o(lzcnt));
|
106 |
|
|
wire [PSTWID*2-1:0] sqrt1 = sqrto[PSTWID*3-1:PSTWID];// << (lzcnt + PSTWID/2);
|
107 |
|
|
|
108 |
|
|
// Make a negative rx positive
|
109 |
|
|
wire [rs+es+1:0] rxtmp2c = rxtmp[rs+es+1] ? ~rxtmp + 2'd1 : rxtmp;
|
110 |
|
|
// Break out the exponent and regime portions
|
111 |
|
|
wire [es-1:0] exp = rxtmp[es-1:0];
|
112 |
|
|
// Take absolute value of regime portion
|
113 |
|
|
wire srxtmp = rxtmp[rs+es+1];
|
114 |
|
|
wire [rs:0] rgm = srxtmp ? -rxtmp[rs+es+1:es] : rxtmp[rs+es+1:es];
|
115 |
|
|
// Compute the length of the regime bit string, +1 for positive regime
|
116 |
|
|
wire [rs:0] rgml = srxtmp ? rxtmp2c[rs+es:es] + 2'd1: rxtmp2c[rs+es:es] + 2'd2;
|
117 |
|
|
// Build expanded posit number:
|
118 |
|
|
// trim one leading bit off the product bits
|
119 |
|
|
// and keep guard, round bits, and create sticky bit
|
120 |
|
|
wire [PSTWID*3-1+9-es:0] tmp;
|
121 |
|
|
generate begin : gTmp
|
122 |
|
|
case(es)
|
123 |
|
|
0: assign tmp = {{PSTWID-1{~srxtmp}},srxtmp,sqrt1[PSTWID*2-2:0],{9-es{1'b0}}};
|
124 |
|
|
1,2,3,4,5,6:
|
125 |
|
|
assign tmp = {{PSTWID-1{~srxtmp}},srxtmp,exp,sqrt1[PSTWID*2-2:0],{9-es{1'b0}}};
|
126 |
|
|
default:
|
127 |
|
|
always @*
|
128 |
|
|
begin
|
129 |
|
|
$display("positSqrt: unsupported es");
|
130 |
|
|
$finish;
|
131 |
|
|
end
|
132 |
|
|
endcase
|
133 |
|
|
end
|
134 |
|
|
endgenerate
|
135 |
|
|
wire [PSTWID*3-1+9-es:0] tmp1 = tmp >> rgml;
|
136 |
|
|
|
137 |
|
|
// Rounding
|
138 |
|
|
// Guard, Round, and Sticky
|
139 |
|
|
wire L = tmp1[PSTWID+8], G = tmp1[PSTWID+7], R = tmp1[PSTWID+6], St = |tmp1[PSTWID+5:0],
|
140 |
|
|
ulp = ((G & (R | St)) | (L & G & ~(R | St)));
|
141 |
|
|
wire [PSTWID-1:0] rnd_ulp = {{PSTWID-1{1'b0}},ulp};
|
142 |
|
|
|
143 |
|
|
wire [PSTWID:0] tmp1_rnd_ulp = tmp1[2*PSTWID+7:PSTWID+8] + rnd_ulp;
|
144 |
|
|
wire [PSTWID-1:0] tmp1_rnd = (rgml < PSTWID-es-2) ? tmp1_rnd_ulp[PSTWID-1:0] : tmp1[2*PSTWID+7:PSTWID+8];
|
145 |
|
|
|
146 |
|
|
always @*
|
147 |
|
|
casez({infi,sqrinf,sqrneg,zero})
|
148 |
|
|
4'b1???: o = {1'b1,{PSTWID-1{1'b0}}};
|
149 |
|
|
4'b01??: o = {1'b1,{PSTWID-1{1'b0}}};
|
150 |
|
|
4'b001?: o = {1'b1,{PSTWID-1{1'b0}}};
|
151 |
|
|
4'b0001: o = {PSTWID{1'b0}};
|
152 |
|
|
default: o = {1'b0,tmp1_rnd[PSTWID-1:1]};
|
153 |
|
|
endcase
|
154 |
|
|
|
155 |
|
|
endmodule
|