4c6d07c6aec6f451d4ac1a3acd447dc019f71150
[ieee754fpu.git] / src / add / fsqrt.py
1 from sfpy import Float32
2
3
4 # XXX DO NOT USE, fails on num=65536. wark-wark...
5 def sqrtsimple(num):
6 res = 0
7 bit = 1
8
9 while (bit < num):
10 bit <<= 2
11
12 while (bit != 0):
13 if (num >= res + bit):
14 num -= res + bit
15 res = (res >> 1) + bit
16 else:
17 res >>= 1
18 bit >>= 2
19
20 return res
21
22
23 def sqrt(num):
24 D = num # D is input (from num)
25 Q = 0
26 R = 0
27 r = 0 # remainder
28 for i in range(15, -1, -1): # negative ranges are weird...
29
30 if (R>=0):
31
32 R = (R<<2)|((D>>(i+i))&3)
33 R = R-((Q<<2)|1) #/*-Q01*/
34
35 else:
36
37 R = (R<<2)|((D>>(i+i))&3)
38 R = R+((Q<<2)|3) #/*+Q11*/
39
40 if (R>=0):
41 Q = (Q<<1)|1 #/*new Q:*/
42 else:
43 Q = (Q<<1)|0 #/*new Q:*/
44
45
46 if (R<0):
47 R = R+((Q<<1)|1)
48 r = R
49 return Q
50
51
52 # grabbed these from unit_test_single (convenience, this is just experimenting)
53
54 def get_mantissa(x):
55 return 0x7fffff & x
56
57 def get_exponent(x):
58 return ((x & 0x7f800000) >> 23) - 127
59
60 def set_exponent(x, e):
61 return (x & ~0x7f800000) | ((e+127) << 23)
62
63 def get_sign(x):
64 return ((x & 0x80000000) >> 31)
65
66 # convert FP32 to s/e/m
67 def create_fp32(s, e, m):
68 """ receive sign, exponent, mantissa, return FP32 """
69 return set_exponent((s << 31) | get_mantissa(m))
70
71 # convert s/e/m to FP32
72 def decode_fp32(x):
73 """ receive FP32, return sign, exponent, mantissa """
74 return get_sign(x), get_exponent(x), get_mantissa(x)
75
76
77 # main function, takes mantissa and exponent as separate arguments
78 # returns a tuple, sqrt'd mantissa, sqrt'd exponent
79
80 def main(mantissa, exponent):
81 if exponent & 1 != 0:
82 # shift mantissa up, subtract 1 from exp to compensate
83 return sqrt(mantissa << 1), (exponent - 1) >> 1
84 # mantissa as-is, no compensating needed on exp
85 return sqrt(mantissa), (exponent >> 1)
86
87
88 def fsqrt_test(x):
89
90 xbits = x.bits
91 print ("x", x, type(x))
92 sq_test = x.sqrt()
93 print ("sqrt", sq_test)
94
95 print (xbits, type(xbits))
96 s, e, m = decode_fp32(xbits)
97 print("x decode", s, e, m, hex(m))
98
99 #m |= 1<<23
100
101 sm, se = main(m, e)
102 print("our sqrt", s, se, sm, hex(sm), bin(sm))
103
104 sq_xbits = sq_test.bits
105 s, e, m = decode_fp32(sq_xbits)
106 print ("sf32 sqrt", s, e, m, hex(m), bin(m))
107 print ()
108
109 if __name__ == '__main__':
110
111 # quick test up to 1000 of two sqrt functions
112 for Q in range(1, int(1e4)):
113 print(Q, sqrt(Q), sqrtsimple(Q), int(Q**0.5))
114 assert int(Q**0.5) == sqrtsimple(Q), "Q sqrtsimpl fail %d" % Q
115 assert int(Q**0.5) == sqrt(Q), "Q sqrt fail %d" % Q
116
117 # quick mantissa/exponent demo
118 for e in range(26):
119 for m in range(26):
120 ms, es = main(m, e)
121 print("m:%d e:%d sqrt: m:%d e:%d" % (m, e, ms, es))
122
123 x = Float32(1234.123456789)
124 fsqrt_test(x)
125 x = Float32(8.0)
126 fsqrt_test(x)
127
128 """
129
130 Notes:
131 https://pdfs.semanticscholar.org/5060/4e9aff0e37089c4ab9a376c3f35761ffe28b.pdf
132
133 //This is the main code of integer sqrt function found here:http://verilogcodes.blogspot.com/2017/11/a-verilog-function-for-finding-square-root.html
134 //
135
136 module testbench;
137
138 reg [15:0] sqr;
139
140 //Verilog function to find square root of a 32 bit number.
141 //The output is 16 bit.
142 function [15:0] sqrt;
143 input [31:0] num; //declare input
144 //intermediate signals.
145 reg [31:0] a;
146 reg [15:0] q;
147 reg [17:0] left,right,r;
148 integer i;
149 begin
150 //initialize all the variables.
151 a = num;
152 q = 0;
153 i = 0;
154 left = 0; //input to adder/sub
155 right = 0; //input to adder/sub
156 r = 0; //remainder
157 //run the calculations for 16 iterations.
158 for(i=0;i<16;i=i+1) begin
159 right = {q,r[17],1'b1};
160 left = {r[15:0],a[31:30]};
161 a = {a[29:0],2'b00}; //left shift by 2 bits.
162 if (r[17] == 1) //add if r is negative
163 r = left + right;
164 else //subtract if r is positive
165 r = left - right;
166 q = {q[14:0],!r[17]};
167 end
168 sqrt = q; //final assignment of output.
169 end
170 endfunction //end of Function
171
172
173 c version (from paper linked from URL)
174
175 unsigned squart(D, r) /*Non-Restoring sqrt*/
176 unsigned D; /*D:32-bit unsigned integer to be square rooted */
177 int *r;
178 {
179 unsigned Q = 0; /*Q:16-bit unsigned integer (root)*/
180 int R = 0; /*R:17-bit integer (remainder)*/
181 int i;
182 for (i = 15;i>=0;i--) /*for each root bit*/
183 {
184 if (R>=0)
185 { /*new remainder:*/
186 R = R<<2)|((D>>(i+i))&3);
187 R = R-((Q<<2)|1); /*-Q01*/
188 }
189 else
190 { /*new remainder:*/
191 R = R<<2)|((D>>(i+i))&3);
192 R = R+((Q<<2)|3); /*+Q11*/
193 }
194 if (R>=0) Q = Q<<1)|1; /*new Q:*/
195 else Q = Q<<1)|0; /*new Q:*/
196 }
197
198 /*remainder adjusting*/
199 if (R<0) R = R+((Q<<1)|1);
200 *r = R; /*return remainder*/
201 return(Q); /*return root*/
202 }
203
204 From wikipedia page:
205
206 short isqrt(short num) {
207 short res = 0;
208 short bit = 1 << 14; // The second-to-top bit is set: 1 << 30 for 32 bits
209
210 // "bit" starts at the highest power of four <= the argument.
211 while (bit > num)
212 bit >>= 2;
213
214 while (bit != 0) {
215 if (num >= res + bit) {
216 num -= res + bit;
217 res = (res >> 1) + bit;
218 }
219 else
220 res >>= 1;
221 bit >>= 2;
222 }
223 return res;
224 }
225
226 """