mini-gmp "bug" missing mpz_fits_sint_p / mpz_fits_uint_p

Niels Möller nisse at lysator.liu.se
Sun Apr 19 10:23:03 UTC 2020


Marco Bodrato <bodrato at mail.dm.unipi.it> writes:

> So that the compiler may optimise your code to:
>
> return mpz_cmp_ui (u, INT_MAX) <= 0 &&
>        (u->_mp_size >= 0 ||
>          mpz_cmpabs_ui (u, GMP_NEG_CAST (unsigned long int, INT_MIN))
> <= 0) ;
>
> which basically is the condition I wrote, with one more branch.

I too would hope the compiler can inline mpz_cmp_si and propagate
constants. But my point was to make the source code simpler. By using
mpz_cmp_si the new functions become considerably simpler, imo. I don't
think this mini-gmp function needs to be optimized for performance.

(BTW, mpz_fits_slong_p could be simplified the same way. While for the
corresponding ulong functions, we seem to have a dependency in the
opposite direction, with mpz_cmpabs_ui calling mpn_absfits_ulong_p).

>> BTW, do we have any C implementation where INT_MAX + INT_MIN == 0,
>> i.e.,
>> not using two's complement?
>
> I'm almost sure the compiler can optimise that out at compile time.

Agreed. But if we attempt to support non two's complement, it would be
interesting if there were some obscure but conforming C implementation
that could be used to test it. Some C interpreter, not targeting any
particular hardware, would do fine for this purpose.

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