mpz_{eq,lt,gt}_{ui,si}_p macros?

bodrato at bodrato at
Thu Feb 14 08:51:22 CET 2013


Il Mer, 13 Febbraio 2013 9:28 am, Marc Glisse ha scritto:
> On Wed, 13 Feb 2013, bodrato at wrote:

>> Are inline functions as portable as macros?
> Not quite, but I don't know any current compiler that doesn't support
> them. Besides, I thought we were talking about __builtin_constant_p, and
> thus gcc (or compatible compilers).

If we define the symbols in gmp.h, we should also document them: functions
or macros?
In the #if GCC case, we can define the corresponding _mpz inline
functions; but I'd like to avoid adding function calls in the #else case.

>> Assume we write cmp_ui (z,limb) as gt()?1:lt()?-1:0

> I tested (i>0?1:i<0?-1:0)<0 (and >0 and ==0) and gcc optimized it to just
> i<0 (resp. i>0, i==0).

The problem arises if gt() and lt() are not so simple.

>>>> variants, I'm not sure I'm handling correctly the negative constants.

> If -(SI) is the only issue, then -(unsigned long)(SI) should work, no?


It is nearly unreadable...

#define mpz_lt_si_p(Z,SI)						\
  (__builtin_constant_p ((SI) >= 0) && (SI) >= 0			\
   ? mpz_lt_ui_p(Z, __GMP_CAST (unsigned long, SI))			\
   : __builtin_constant_p ((SI) <= 0 && -__GMP_CAST (unsigned long, SI) <=
   && ((SI) <= 0 && -__GMP_CAST (unsigned long, SI) <= GMP_NUMB_MAX)	\
   ? (Z)->_mp_size < -((Z)->_mp_d[0] <=-__GMP_CAST (unsigned long, SI))	\
   : _mpz_cmp_si (Z,SI) < 0)

... and perhaps I should add one more cast to mp_limb_t before the
comparison with _mp_d[0]...



More information about the gmp-devel mailing list