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