New mini-gmp macro GMP_CMP?
Niels Möller
nisse at lysator.liu.se
Thu Nov 24 15:44:28 UTC 2016
"Marco Bodrato" <bodrato at mail.dm.unipi.it> writes:
> 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.
Maybe, I find this use of mpz_cmpabs_ui a bit strange. Something with
mpz_cmpabs_ui (u, GMP_ABS_CAST(mp_limb_t, v))
would look more symmetric.
> And, IMO, we shouldn't use a multiplication... ;-)
I think what I intended was a conditional negation, which could be done
with (x ^ sign) - sign (where sign should be either 0 or ~0). But that's
maybe a bit too obscure.
> 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);
> }
Looks fine to me. I find the result on overflow a bit odd (but I think
it's the same as gmp). If I understand it correctly, the value v of
mpz_get_si (u), is defined by the conditions
sgn(v) == sgn(u) and
v == u (mod GMP_LIMB_HIGHBIT)
Is that right?
Regards,
/Niels
--
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.
More information about the gmp-devel
mailing list