mpz_get_si: possible undefined behaviour? [Was: Micro-GMP]
Niels Möller
nisse at lysator.liu.se
Thu Dec 6 15:54:07 UTC 2018
tg at gmplib.org (Torbjörn Granlund) writes:
> 2. We should avoid undefined behaviour which might be relevant only to
> systems we support. E.g. we might want to avoid signed overflow, or
> conversion between negative signed and unsigned integers.
Regarding the second example: As far as I'm aware, conversion from
signed to unsigned is perfectly well defined by C specs. It's arithmetic
modulo (ULONG_MAX+1), or whatever is the corresponding *_MAX value for
the target type.
Which is why the NEG_CAST macro seems a bit over-engineered.
Conversion the *other* direction, with signed target type, is well
defined only when the value in question fits the new type.
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