Overflow in mpz_cmp

Marco Bodrato bodrato at mail.dm.unipi.it
Tue Feb 11 14:24:21 UTC 2020


Ciao,

Il 2020-02-11 14:42 nisse at lysator.liu.se ha scritto:
> Marco Bodrato <bodrato at mail.dm.unipi.it> writes:

>> diff -r f5601c2a8b11 mpz/cmp.c
>> --- a/mpz/cmp.c	Sun Feb 09 16:16:19 2020 +0100
>> +++ b/mpz/cmp.c	Tue Feb 11 14:20:39 2020 +0100
>> @@ -35,15 +35,15 @@

>> +  cmp = (usize > vsize) - (usize < vsize);
>> +  if (cmp != 0)
>> +    return cmp;
> 
> I would be tempted to keep it simple,
> 
>   if (usize != vsize)
>     return (usize > vsize) ? 1 : -1;

> It's not clear to me if this is worth micro optimizing, and ensure we
> get only a single branch.

I believe that current compilers on modern architectures should compile 
the (usize > vsize)?1:-1 expression with no branches.

I tested gcc on amd64 and on arm64, and on both archs your code is 
compiled with the single (usize != vsize) branch.

Your proposal is simpler to read too.

Ĝis,
m

-- 
http://bodrato.it/papers/


More information about the gmp-devel mailing list