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