A little warning in GMP.h

Brian Gladman brg at gladman.plus.com
Sat Sep 25 08:20:47 CEST 2004


Torbjorn Granlund wrote:

> Brian Hurt <bhurt at spnz.org> writes:
> 
>   Can I ask a possibly stupid question: this rather confusing idiom is 
>   trying to replace:
>       return (__gmp_n != 0)?__gmp_l:0;
>   
>   with code that doesn't have the branch.  Are there compilers that, given 
>   the above code, don't produce the branchless versions?
> 
> At least in the past GCC didn't always handle that as one would
> wish.  (This is target dependent, the GCC macro BRANCH_COST need
> to be set properly for GCC to do extra computatiuons for avoiding
> branches.)
> 
>   The advantage of the branched version is that it doesn't depend 
>   upon the system being twos complement.  And gcc (at least version 3.2.2) 
>   emits branchless code using the sete instruction with just -O2.
> 
> On which platforms?
>   
>   Another alternative would be:
>       return __gmp_l & (((mp_limb_t) (__gmp_n == 0)) - 1u);
>   
>   Another alternative would be to have a signed mp_limb_t type around.
>   
> We're not going to go through the GMP code just to work around a
> bogus Microsoft compiler warning.  Please use GCC for compiling
> GMP if Microsoft's compiler's bogus warnings bother you.

I don't think this is bogus in this case or in any other of the many 
places in GMP where it turns up.

Rather it is correct but unecessary because this is an intentional 
feature of the source code which I assume is designed to cope with the 
often hopeless optimisation in earlier GCC based compilers on some 
platforms.

   Brian Gladman



More information about the gmp-discuss mailing list