1 from sfpy
import Float32
4 # XXX DO NOT USE, fails on num=65536. wark-wark...
13 if (num
>= res
+ bit
):
15 res
= (res
>> 1) + bit
24 D
= num
# D is input (from num)
27 for i
in range(64, -1, -1): # negative ranges are weird...
29 R
= (R
<<2)|
((D
>>(i
+i
))&3)
32 R
-= ((Q
<<2)|
1) # -Q01
34 R
+= ((Q
<<2)|
3) # +Q11
46 # grabbed these from unit_test_single (convenience, this is just experimenting)
52 return ((x
& 0x7f800000) >> 23) - 127
54 def set_exponent(x
, e
):
55 return (x
& ~
0x7f800000) |
((e
+127) << 23)
58 return ((x
& 0x80000000) >> 31)
60 # convert FP32 to s/e/m
61 def create_fp32(s
, e
, m
):
62 """ receive sign, exponent, mantissa, return FP32 """
63 return set_exponent((s
<< 31) |
get_mantissa(m
))
65 # convert s/e/m to FP32
67 """ receive FP32, return sign, exponent, mantissa """
68 return get_sign(x
), get_exponent(x
), get_mantissa(x
)
71 # main function, takes mantissa and exponent as separate arguments
72 # returns a tuple, sqrt'd mantissa, sqrt'd exponent
74 def main(mantissa
, exponent
):
76 # shift mantissa up, subtract 1 from exp to compensate
80 return m
, r
, exponent
>> 1
83 #normalization function
84 def normalise(s
, m
, e
, lowbits
):
87 if get_mantissa(m
) == ((1<<24)-1):
89 #if the num is NaN, than adjust
90 if (e
== 128 & m
!=0):
95 #if the num is Inf, then adjust
107 print ("x", x
, type(x
))
109 print ("sqrt", sq_test
)
111 print (xbits
, type(xbits
))
112 s
, e
, m
= decode_fp32(xbits
)
113 print("x decode", s
, e
, m
, hex(m
))
115 m |
= 1<<23 # set top bit (the missing "1" from mantissa)
118 sm
, sr
, se
= main(m
, e
)
121 sm
= get_mantissa(sm
)
124 s
, sm
, se
= normalise(s
, sm
, se
, lowbits
)
126 print("our sqrt", s
, se
, sm
, hex(sm
), bin(sm
), "lowbits", lowbits
,
129 print ("probably needs rounding (+1 on mantissa)")
131 sq_xbits
= sq_test
.bits
132 s
, e
, m
= decode_fp32(sq_xbits
)
133 print ("sf32 sqrt", s
, e
, m
, hex(m
), bin(m
))
136 if __name__
== '__main__':
138 # quick test up to 1000 of two sqrt functions
139 for Q
in range(1, int(1e4
)):
140 print(Q
, sqrt(Q
), sqrtsimple(Q
), int(Q
**0.5))
141 assert int(Q
**0.5) == sqrtsimple(Q
), "Q sqrtsimpl fail %d" % Q
142 assert int(Q
**0.5) == sqrt(Q
)[0], "Q sqrt fail %d" % Q
144 # quick mantissa/exponent demo
147 ms
, mr
, es
= main(m
, e
)
148 print("m:%d e:%d sqrt: m:%d-%d e:%d" % (m
, e
, ms
, mr
, es
))
150 x
= Float32(1234.123456789)
160 x
= Float32(3.14159265358979323)
162 x
= Float32(12.99392923123123)
164 x
= Float32(0.123456)
173 https://pdfs.semanticscholar.org/5060/4e9aff0e37089c4ab9a376c3f35761ffe28b.pdf
175 //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
182 //Verilog function to find square root of a 32 bit number.
183 //The output is 16 bit.
184 function [15:0] sqrt;
185 input [31:0] num; //declare input
186 //intermediate signals.
189 reg [17:0] left,right,r;
192 //initialize all the variables.
196 left = 0; //input to adder/sub
197 right = 0; //input to adder/sub
199 //run the calculations for 16 iterations.
200 for(i=0;i<16;i=i+1) begin
201 right = {q,r[17],1'b1};
202 left = {r[15:0],a[31:30]};
203 a = {a[29:0],2'b00}; //left shift by 2 bits.
204 if (r[17] == 1) //add if r is negative
206 else //subtract if r is positive
208 q = {q[14:0],!r[17]};
210 sqrt = q; //final assignment of output.
212 endfunction //end of Function
215 c version (from paper linked from URL)
217 unsigned squart(D, r) /*Non-Restoring sqrt*/
218 unsigned D; /*D:32-bit unsigned integer to be square rooted */
221 unsigned Q = 0; /*Q:16-bit unsigned integer (root)*/
222 int R = 0; /*R:17-bit integer (remainder)*/
224 for (i = 15;i>=0;i--) /*for each root bit*/
228 R = R<<2)|((D>>(i+i))&3);
229 R = R-((Q<<2)|1); /*-Q01*/
233 R = R<<2)|((D>>(i+i))&3);
234 R = R+((Q<<2)|3); /*+Q11*/
236 if (R>=0) Q = Q<<1)|1; /*new Q:*/
237 else Q = Q<<1)|0; /*new Q:*/
240 /*remainder adjusting*/
241 if (R<0) R = R+((Q<<1)|1);
242 *r = R; /*return remainder*/
243 return(Q); /*return root*/
248 short isqrt(short num) {
250 short bit = 1 << 14; // The second-to-top bit is set: 1 << 30 for 32 bits
252 // "bit" starts at the highest power of four <= the argument.
257 if (num >= res + bit) {
259 res = (res >> 1) + bit;