+from sfpy import Float32
+
+
+# XXX DO NOT USE, fails on num=65536. wark-wark...
def sqrtsimple(num):
res = 0
- bit = 1 << 14
+ bit = 1
- while (bit > num):
- bit >>= 2
+ while (bit < num):
+ bit <<= 2
while (bit != 0):
if (num >= res + bit):
return res
-# XXX DO NOT USE, fails on num=65536. wark-wark...
def sqrt(num):
D = num # D is input (from num)
Q = 0
for i in range(15, -1, -1): # negative ranges are weird...
if (R>=0):
-
+
R = (R<<2)|((D>>(i+i))&3)
R = R-((Q<<2)|1) #/*-Q01*/
-
+
else:
R = (R<<2)|((D>>(i+i))&3)
R = R+((Q<<2)|3) #/*+Q11*/
-
+
if (R>=0):
Q = (Q<<1)|1 #/*new Q:*/
else:
Q = (Q<<1)|0 #/*new Q:*/
-
+
if (R<0):
R = R+((Q<<1)|1)
def get_sign(x):
return ((x & 0x80000000) >> 31)
+# convert FP32 to s/e/m
+def create_fp32(s, e, m):
+ """ receive sign, exponent, mantissa, return FP32 """
+ return set_exponent((s << 31) | get_mantissa(m))
+
+# convert s/e/m to FP32
+def decode_fp32(x):
+ """ receive FP32, return sign, exponent, mantissa """
+ return get_sign(x), get_exponent(x), get_mantissa(x)
+
+
# main function, takes mantissa and exponent as separate arguments
# returns a tuple, sqrt'd mantissa, sqrt'd exponent
def main(mantissa, exponent):
if exponent & 1 != 0:
- return sqrt(mantissa << 1), # shift mantissa up
- ((exponent - 1) / 2) # subtract 1 from exp to compensate
- return sqrt(mantissa), # mantissa as-is
- (exponent / 2) # no compensating needed on exp
+ # shift mantissa up, subtract 1 from exp to compensate
+ return sqrt(mantissa << 1), (exponent - 1) >> 1
+ # mantissa as-is, no compensating needed on exp
+ return sqrt(mantissa), (exponent >> 1)
if __name__ == '__main__':
- for Q in range(1, int(1e7)):
+
+ # quick test up to 1000 of two sqrt functions
+ for Q in range(1, int(1e4)):
print(Q, sqrt(Q), sqrtsimple(Q), int(Q**0.5))
assert int(Q**0.5) == sqrtsimple(Q), "Q sqrtsimpl fail %d" % Q
assert int(Q**0.5) == sqrt(Q), "Q sqrt fail %d" % Q
+ # quick mantissa/exponent demo
+ for e in range(26):
+ for m in range(26):
+ ms, es = main(m, e)
+ print("m:%d e:%d sqrt: m:%d e:%d" % (m, e, ms, es))
+
+ x = Float32(1234.123456789)
+ xbits = x.bits
+
+ print (x, type(x))
+ print (xbits, type(xbits))
+ s, e, m = decode_fp32(xbits)
+ print(s, e, m, hex(m))
+
+ se, sm = main(e, m)
+ print("our sqrt", se, sm, hex(sm))
+
+ sq_test = x.sqrt()
+ sq_xbits = sq_test.bits
+ s, e, m = decode_fp32(sq_xbits)
+ print ("sf16 sqrt", s, e, m, hex(m))
"""
+
+Notes:
+https://pdfs.semanticscholar.org/5060/4e9aff0e37089c4ab9a376c3f35761ffe28b.pdf
+
//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
//