Overflow in mpz_cmp

Marco Bodrato bodrato at mail.dm.unipi.it
Tue Feb 11 15:02:39 UTC 2020


Ciao,

Il 2020-02-11 14:56 Marc Glisse ha scritto:
> On Tue, 11 Feb 2020, Niels Möller wrote:
>>  if (usize != vsize)
>>    return (usize > vsize) ? 1 : -1;

> On x86_64, both gcc and clang optimize (usize > vsize) ? 1 : -1 to 2 *
> (usize > vsize) - 1 (as a single LEA for gcc, 2 ADD for llvm). So the
> generated code may be just as good with the simple code.

We know, optimising is a complex task, and we are not writing a compiler 
here. But it is funnier to observe how the compilers translate the last 
line of mpz/cmp.c:
   return (usize >= 0 ? cmp : -cmp);
in the branches where the compiler "knows" that cmp is zero :-)

Ĝis,
m


More information about the gmp-devel mailing list