C++ ternary operations

Marc Glisse marc.glisse at inria.fr
Sat Feb 19 18:46:58 CET 2011


Hello,

in 2006, the C++ ternary operation z=w+v*u was removed from gmpxx because 
it had a broken implementation when z==v:

   static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v, mpz_srcptr u)
   { mpz_set(z, w); mpz_addmul(z, v, u); }

I recently had a piece of code that did a lot of this kind of operation 
(think scalar product) and where reintroducing a correct version made a 
significant difference in execution time.

   static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v, mpz_srcptr u) {
     if(z==w)
       mpz_addmul(z,v,u);
     else {
       mpz_mul(z, v, u);
       mpz_add(z, z, w);
     }
   }

(and similar things for the other versions)

This may not be significant for large numbers, but for small numbers, 
simply replacing a real allocation (in gmpxx.h) by a temporary allocation 
(alloca in libgmp) can make a difference.

I think this is something to keep in mind for reintroduction at some point 
(maybe a new item in the tasks file?)

-- 
Marc Glisse


More information about the gmp-discuss mailing list