New mini-gmp macro GMP_CMP?

Marco Bodrato bodrato at mail.dm.unipi.it
Wed Nov 23 23:05:50 UTC 2016


Ciao,

Il Mer, 23 Novembre 2016 11:28 pm, Niels Möller ha scritto:
>   int
>   mpz_cmp_si (const mpz_t u, long v)
>   {
>     if (v >= 0)
>       return mpz_cmp_ui (u, v);
>     else if (u->_mp_size >= 0)
>       return 1;
>     else
>       return - mpz_cmpabs_ui (u, GMP_NEG_CAST(mp_limb_t, v));
>   }

I like this version.

> If we *really* go for conciseness, we could do something like
[...]
>     if ((sign ^ v) >= 0)
>       sign *= mpz_cmpabs_ui (u, ABS_GMP_CAST(mp_limb_t, v));
[...]
> But we shouldn't try to be too clever.

And, IMO, we shouldn't use a multiplication... ;-)

>> #define GMP_CMP(a,b) (((a) < (b)) ? -1 : ((a) > (b)))
>> It is not as elegant as, nor symmetric, but maybe it's easier to
>> understand.
>
> I like your symmetric version; once you get it it's clear and obvious. I

Ok.

With the same strategy you applied to cmp_si, we may change also get_si.

Current:

long int
mpz_get_si (const mpz_t u)
{
  mp_size_t us = u->_mp_size;

  if (us > 0)
    return (long) (u->_mp_d[0] & ~GMP_LIMB_HIGHBIT);
  else if (us < 0)
    return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
  else
    return 0;
}

Proposal:

long int
mpz_get_si (const mpz_t u)
{
  if (u->_mp_size < 0)
    return (long) (- u->_mp_d[0] | GMP_LIMB_HIGHBIT);
  else
    return (long) (mpz_get_ui (u) & ~GMP_LIMB_HIGHBIT);
}

Regards,
m

-- 
http://bodrato.it/



More information about the gmp-devel mailing list