A little warning in GMP.h
Torbjorn Granlund
tege at swox.com
Sat Sep 25 00:53:59 CEST 2004
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.
--
Torbjörn
More information about the gmp-discuss
mailing list