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

bodrato at mail.dm.unipi.it bodrato at mail.dm.unipi.it
Wed Feb 13 08:37:50 CET 2013


Ciao Marc,

Il Lun, 11 Febbraio 2013 12:42 am, Marc Glisse ha scritto:
> Could we have inline functions instead of macros? I'd rather avoid macros

Are inline functions as portable as macros?
If a compiler does not inline the function corresponding to:

>> #define mpz_lt_ui_p(Z,UI) (_mpz_cmp_ui (Z,UI) < 0)

then we will have two function calls instead of one.

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

I don't know exactly, I didn't try. The _cmp_ functions return three
possible values. The compiler should be able to select.
We can define _cmp_ui (z,0) as (SIZ(z)>0)-(SIZ(z)<0), will the compiler
detect that this expression is >0 iif SIZ(z) is? and <0 iif SIZ(z) is? and
==0 iif SIZ(z)==0?

Even worse for cmp_ui (z,limb), it is hard to write a branchless expression.
Assume we write cmp_ui (z,limb) as gt()?1:lt()?-1:0, it is "easy" for the
compiler to understand that the expression >0 iif gt(), but what about >0?

If UI always fits in a LIMB, we can write (even for non-gcc compilers)

#define mpz_gt_ui_p(Z,UI) (SIZ(Z) > (PTR(Z)[0] <= (UI)))
#define mpz_lt_ui_p(Z,UI) (SIZ(Z) <= (PTR(Z)[0] < (UI)) - ((UI) == 0))
#define mpz_eq_ui_p(Z,UI) (SIZ(Z) == ((UI) != 0) && (((UI) == 0) ||
(PTR(Z)[0] == (UI)))

Can you suggest a definition for cmp_ui to obtain something similar?

>> 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.

... you are right ... I don't know if I'll be able to write a shortcut for
negative numbers...

Best regards,
m

-- 
http://bodrato.it/papers/



More information about the gmp-devel mailing list