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