From: Aleksandar Kostovic Date: Sun, 28 Apr 2019 09:52:36 +0000 (+0200) Subject: Adapted the C version of sqrt to python X-Git-Tag: ls180-24jan2020~1165 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6f0a39ea4f0ae20946ce8b255d2c05f43b090e3c;p=ieee754fpu.git Adapted the C version of sqrt to python --- diff --git a/src/add/fsqrt.py b/src/add/fsqrt.py index bec42185..3a2e999e 100644 --- a/src/add/fsqrt.py +++ b/src/add/fsqrt.py @@ -1,5 +1,5 @@ def sqrt(num): - + """ res = 0 bit = 1 << 14 @@ -15,6 +15,38 @@ def sqrt(num): bit >>= 2 return res + """ + r = None + D = None + Q = 0 + R = 0 + for i in range(15): + i -= 1 + + 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) + r = R + return Q + + +for Q in range(1,20): + print(sqrt(Q)) """ //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