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