mpz_invert (x, 1, 0)

Niels Möller nisse at
Fri Mar 2 11:14:04 CET 2012

Torbjorn Granlund <tg at> writes:

> Therefore, a series of disjoint unlikely conditions is not good.  If
> they can be combined to few disjoint unlikely conditions (possibly
> covering some unlikely case too much, to be rejected later) that I am
> much happier.  Unfortunetely, the change to invert adds such unlikely
> critical-path conditions.

Would it be an improvement to rewrite it as

  xsize == 0 || (nsize <= 1 && (nsize == 0 || PTR (n)[0] == 1))


The expression (nsize == 0 || PTR (n)[0] == 1) can also be rewritten as
some bitwose logic. We know that nsize can be only zero or one. And if
it's one, PTR(n)[0] can't be zero. So I think

  xsize == 0 || (nsize <= 1 && ( (- (mp_limb_t) nsize) & (PTR (n)[0] - 1)) == 0)

is a correct, if almost unreadable, test ;-)

Won't make much difference when n == 1, though, which I guess is an
important case.

What about documentation, if we choose Z0 == Z semantics because that
eliminates a branch, should we document that behaviour, or leave
inverses mod 0 undefined, in case we find some more clever conditions
later on?


Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.

More information about the gmp-devel mailing list