From 7541ca979084de96ebdf292e1baa0a03af64d3fc Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Tue, 30 Apr 2019 02:58:21 +0100 Subject: [PATCH] minor code-shuffle on sqrt() fn --- src/add/fsqrt.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/add/fsqrt.py b/src/add/fsqrt.py index c88aea39..a87af61d 100644 --- a/src/add/fsqrt.py +++ b/src/add/fsqrt.py @@ -23,30 +23,24 @@ def sqrtsimple(num): def sqrt(num): D = num # D is input (from num) Q = 0 - R = 0 - r = 0 # remainder + R = 0 # remainder for i in range(64, -1, -1): # negative ranges are weird... - if (R>=0): - - R = (R<<2)|((D>>(i+i))&3) - R = R-((Q<<2)|1) #/*-Q01*/ + R = (R<<2)|((D>>(i+i))&3) + if R >= 0: + R -= ((Q<<2)|1) # -Q01 else: + R += ((Q<<2)|3) # +Q11 - 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:*/ + Q <<= 1 + if R >= 0: + Q |= 1 # new Q + if R < 0: + R = R + ((Q<<1)|1) - if (R<0): - R = R+((Q<<1)|1) - r = R - return Q, r + return Q, R # grabbed these from unit_test_single (convenience, this is just experimenting) -- 2.30.2