Ciao,

Il Ven, 28 Dicembre 2012 12:03 am, Niels Möller ha scritto:
> bodr...@mail.dm.unipi.it writes:
>> About mpz_combit code, may I suggest replacing the two lines
>>        MPN_NORMALIZE (dp, dsize);
>>        ASSERT (dsize > 0);
>> with
>>        MPN_NORMALIZE_NOT_ZERO (dp, dsize);

> Conciseness is nice. Is that equivalent? I had some impression that the
> latter is less safe, but that may be completely wrong.

It's not a matter of conciseness, the latter is faster, but you are right:
it is not safe if the ASSERT is based on false assumptions.

In that branch, MPN_NORMALIZE_NOT_ZERO is safe (and the ASSERT is correct).

Looking deeper into, I realised that the branch-less normalization of at
most one limb is even better. The patch (for 5.2) is attached, I already
pushed specific checking code, as it can not introduce new bugs (in the
library).

Regards,
m

-- 
http://bodrato.it/software/combinatorics.html
diff -r 986d7f6dab98 mpz/combit.c
--- a/mpz/combit.c	Sat Dec 29 13:26:56 2012 +0100
+++ b/mpz/combit.c	Sun Dec 30 08:39:10 2012 +0100
@@ -56,9 +56,9 @@
 	{
 	  /* We toggle a zero bit, subtract from the absolute value. */
 	  MPN_DECR_U (dp + limb_index, dsize - limb_index, bit);
-	  MPN_NORMALIZE (dp, dsize);
-	  ASSERT (dsize > 0);
-	  SIZ(d) = -dsize;
+	  /* SIZ(d) is negative, the absolute value was shrinked by at most one bit. */
+	  SIZ (d) += dp[dsize - 1] == 0;
+	  ASSERT (SIZ (d) < 0 && dp[-SIZ (d) - 1] != 0);
 	}
     }
   else
_______________________________________________
gmp-devel mailing list
gmp-devel@gmplib.org
http://gmplib.org/mailman/listinfo/gmp-devel

Reply via email to