@@ -569,8 +569,14 @@ bits efficiently, which is possible on most platforms; it is abstracted here as
569
569
570
570
``` python
571
571
def count_trailing_zeros (v ):
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
572
+ """
573
+ When v is zero, consider all N zero bits as "trailing".
574
+ For a non-zero value v, find z such that v=(d<<z) for some odd d.
575
+ """
576
+ if v == 0 :
577
+ return N
578
+ else :
579
+ return (v & - v).bit_length() - 1
574
580
575
581
i = N # divsteps left to do
576
582
while True :
@@ -601,7 +607,7 @@ becomes negative, or when *i* reaches *0*. Combined, this is equivalent to addin
601
607
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
608
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
609
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
610
+ doing more) or more than * &eta ; +1* steps (as we'd run ` eta, f, g = -eta, g, - f ` at that point), but
605
611
apart from that, we're only limited by the complexity of computing * w* .
606
612
607
613
This code demonstrates how to cancel up to 4 bits per step:
@@ -618,7 +624,7 @@ while True:
618
624
break
619
625
# We know g is odd now
620
626
if eta < 0 :
621
- eta, f, g = - eta, g, f
627
+ eta, f, g = - eta, g, - f
622
628
# Compute limit on number of bits to cancel
623
629
limit = min (min (eta + 1 , i), 4 )
624
630
# Compute w = -g/f mod 2**limit, using the table value for -1/f mod 2**4. Note that f is
0 commit comments