mpz_addmul_ui with ui = 1

Paul Zimmermann Paul.Zimmermann at loria.fr
Thu Jan 29 09:31:39 CET 2009


       Dear Roberto,

> Date: Wed, 28 Jan 2009 13:55:06 +0100
> From: Roberto Bagnara <bagnara at cs.unipr.it>
> 
> Jim White wrote:
> > I have a loop that spends 99+% of its time executing these two statements:
> > 
> >       mpz_addmul_ui(BB, B, a);
> >       mpz_swap     (B, BB);
> > 
> > To my surprise, it runs 25% faster if I replace it with:
> > 
> >       if (a==1) mpz_add      (BB, B, BB);
> >       else      mpz_addmul_ui(BB, B, a);
> >       mpz_swap(B, BB);
> > 
> > It's an RCF (regular continued-fraction) algorithm, so a=1 about 41.5% of the time.  
> > 
> > No big deal, but perhaps in future revs  mpz_addmul_ui and its sister function might auto-detect this case and reroute the call?
> 
> In the Parma Polyhedra Library, we found out that,
> for mpz_mul(), checking the source arguments against 0
> gives rise to significant speedups in some algorithms.

this surprises me, since 0 has a special representation in mpz, and thus
is early detected in mpz_add and mpz_mul. Please can you give an example?

For an unsigned int argument (the 3rd argument 'a' above in mpz_addmul_ui)
there is no special treatement for 0 (or 1) if I remember correctly, so it is
indeed the responsibility of the user to check that case.

Paul Zimmermann


More information about the gmp-discuss mailing list