Commented the right function
[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 << 14
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 return sqrt(mantissa << 1), # shift mantissa up
80 ((exponent - 1) / 2) # subtract 1 from exp to compensate
81 return sqrt(mantissa), # mantissa as-is
82 (exponent / 2) # no compensating needed on exp
83
84
85 if __name__ == '__main__':
86 for Q in range(1, int(1e7)):
87 print(Q, sqrt(Q), sqrtsimple(Q), int(Q**0.5))
88 assert int(Q**0.5) == sqrtsimple(Q), "Q sqrtsimpl fail %d" % Q
89 assert int(Q**0.5) == sqrt(Q), "Q sqrt fail %d" % Q
90
91 """
92 //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
93 //
94
95 module testbench;
96
97 reg [15:0] sqr;
98
99 //Verilog function to find square root of a 32 bit number.
100 //The output is 16 bit.
101 function [15:0] sqrt;
102 input [31:0] num; //declare input
103 //intermediate signals.
104 reg [31:0] a;
105 reg [15:0] q;
106 reg [17:0] left,right,r;
107 integer i;
108 begin
109 //initialize all the variables.
110 a = num;
111 q = 0;
112 i = 0;
113 left = 0; //input to adder/sub
114 right = 0; //input to adder/sub
115 r = 0; //remainder
116 //run the calculations for 16 iterations.
117 for(i=0;i<16;i=i+1) begin
118 right = {q,r[17],1'b1};
119 left = {r[15:0],a[31:30]};
120 a = {a[29:0],2'b00}; //left shift by 2 bits.
121 if (r[17] == 1) //add if r is negative
122 r = left + right;
123 else //subtract if r is positive
124 r = left - right;
125 q = {q[14:0],!r[17]};
126 end
127 sqrt = q; //final assignment of output.
128 end
129 endfunction //end of Function
130
131
132 c version (from paper linked from URL)
133
134 unsigned squart(D, r) /*Non-Restoring sqrt*/
135 unsigned D; /*D:32-bit unsigned integer to be square rooted */
136 int *r;
137 {
138 unsigned Q = 0; /*Q:16-bit unsigned integer (root)*/
139 int R = 0; /*R:17-bit integer (remainder)*/
140 int i;
141 for (i = 15;i>=0;i--) /*for each root bit*/
142 {
143 if (R>=0)
144 { /*new remainder:*/
145 R = R<<2)|((D>>(i+i))&3);
146 R = R-((Q<<2)|1); /*-Q01*/
147 }
148 else
149 { /*new remainder:*/
150 R = R<<2)|((D>>(i+i))&3);
151 R = R+((Q<<2)|3); /*+Q11*/
152 }
153 if (R>=0) Q = Q<<1)|1; /*new Q:*/
154 else Q = Q<<1)|0; /*new Q:*/
155 }
156
157 /*remainder adjusting*/
158 if (R<0) R = R+((Q<<1)|1);
159 *r = R; /*return remainder*/
160 return(Q); /*return root*/
161 }
162
163 From wikipedia page:
164
165 short isqrt(short num) {
166 short res = 0;
167 short bit = 1 << 14; // The second-to-top bit is set: 1 << 30 for 32 bits
168
169 // "bit" starts at the highest power of four <= the argument.
170 while (bit > num)
171 bit >>= 2;
172
173 while (bit != 0) {
174 if (num >= res + bit) {
175 num -= res + bit;
176 res = (res >> 1) + bit;
177 }
178 else
179 res >>= 1;
180 bit >>= 2;
181 }
182 return res;
183 }
184
185 """