mpz_mod inside loop

Paul Underwood paulunderwood at mindless.com
Fri May 17 11:36:18 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.
>
>

Apart from this compound statement, I found this to be most efficient (with GMP):

  mpz_mul_2exp (align, n, (GMP_NUMB_BITS/2 - mpz_sizeinbase ( n, 2 )) % ( GMP_NUMB_BITS/2 ) );

Best

Paul


More information about the gmp-discuss mailing list