mpz_mod inside loop

Torbjörn Granlund tg at gmplib.org
Fri May 17 10:02:01 UTC 2019


Hans L <thehans at gmail.com> writes:

  The "+ 2" looks quite wrong to me.

  I think the correct way to align a value in such a way would be:
  mpz_mul_2exp (align, n, 63 - ( ( mpz_sizeinbase ( n, 2 ) - 1 ) % 64) );

Why make it so complicated?

A simple

  mpz_mul_2exp (a, a, (64 - s) % 64);

would probably do what the original poster wants.  But to avoid slowdown
on machines with internal limb size != 64, I'd use

  mpz_mul_2exp (a, a, (GMP_NUMB_BITS - s) % GMP_NUMB_BITS);

instead.

  After the "% 64" we can add 1 back to result,

The range should clearly be [0,63] so adding anything after the % 64
cannot be right.


-- 
Torbjörn
Please encrypt, key id 0xC8601622


More information about the gmp-discuss mailing list