A little warning in GMP.h

Brian Hurt bhurt at spnz.org
Thu Sep 23 17:00:11 CEST 2004


On Thu, 23 Sep 2004, Patrick Pelissier wrote:

> > Anyway, I was wondering if just writing
> > 
> >       return __gmp_l & (-(signed)(mp_limb_t) (__gmp_n != 0));
> > 
> > would work, since this would solve the problem with all compilers without
> > any option tweak....
> 
>  Some problems may occur if sizeof(int) != sizeof (mp_limb_t) (16 or 64 bits systems).
> 

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?  How widely used 
are they?  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.

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.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian



More information about the gmp-discuss mailing list