@@ -570,7 +570,10 @@ bits efficiently, which is possible on most platforms; it is abstracted here as
570
570
``` python
571
571
def count_trailing_zeros (v ):
572
572
""" For a non-zero value v, find z such that v=(d<<z) for some odd d."""
573
- return (v & - v).bit_length() - 1
573
+ if v == 0 :
574
+ return N
575
+ else :
576
+ return (v & - v).bit_length() - 1
574
577
575
578
i = N # divsteps left to do
576
579
while True :
@@ -601,7 +604,7 @@ becomes negative, or when *i* reaches *0*. Combined, this is equivalent to addin
601
604
It is easy to find what that multiple is: we want a number * w* such that * g+w&thinsp ; f* has a few bottom
602
605
zero bits. If that number of bits is * L* , we want * g+w&thinsp ; f mod 2<sup >L</sup > = 0* , or * w = -g/f mod 2<sup >L</sup >* . Since * f*
603
606
is odd, such a * w* exists for any * L* . * L* cannot be more than * i* steps (as we'd finish the loop before
604
- doing more) or more than * &eta ; +1* steps (as we'd run ` eta, f, g = -eta, g, f ` at that point), but
607
+ doing more) or more than * &eta ; +1* steps (as we'd run ` eta, f, g = -eta, g, - f ` at that point), but
605
608
apart from that, we're only limited by the complexity of computing * w* .
606
609
607
610
This code demonstrates how to cancel up to 4 bits per step:
@@ -618,7 +621,7 @@ while True:
618
621
break
619
622
# We know g is odd now
620
623
if eta < 0 :
621
- eta, f, g = - eta, g, f
624
+ eta, f, g = - eta, g, - f
622
625
# Compute limit on number of bits to cancel
623
626
limit = min (min (eta + 1 , i), 4 )
624
627
# Compute w = -g/f mod 2**limit, using the table value for -1/f mod 2**4. Note that f is
0 commit comments