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

Marc Glisse marc.glisse at inria.fr
Mon Feb 11 00:42:06 CET 2013


On Sun, 10 Feb 2013, bodrato at mail.dm.unipi.it wrote:

> I played with gmpxx.h for a while, because Torbjorn said something about
> possible functions for comparison with shortcuts to detect specific
> conditions (>, =, or <)...
>
> After some tests with C++ I moved my experiments to plain C.
>
> The current status of the experiments is shown by the attached patch. The
> proposal is: let's introduce new macros like mpz_lt_ui_p, with default

Could we have inline functions instead of macros? I'd rather avoid macros 
if they aren't necessary.

> definition like the following:
> #define mpz_lt_ui_p(Z,UI) (_mpz_cmp_ui (Z,UI) < 0)
> but possibly handling special cases with faster code...
>
> Do you think it can make sense?

I am wondering (true question) how much this gains compared to refining 
the existing mpz_cmp_[su]i macros.

> The attached patch is able to sensibly speed-up the execution of the
> simple C++ loop I'm testing:
>
> #include "gmpxx.h"
>
> #define BIGN 123456789
> #define BIGS 987654321
>
> int main (void)
> {
>  mpz_class z(BIGN);
>  unsigned r = 0;
>
>  for (z*=BIGS; z>r; z-=BIGS)
>    ++r;
>
>  printf ("%u",r);
> }
>
> If you like the idea, then please read carefully the lt_si and gt_si
> variants, I'm not sure I'm handling correctly the negative constants.

-(SI) seems wrong, you need to cast to an unsigned type before taking the 
opposite, or LONG_MIN gives you undefined behavior.

-- 
Marc Glisse


More information about the gmp-devel mailing list