mpz_addmul_ui with ui = 1

Jim White mathimagics at
Wed Jan 28 13:31:45 CET 2009

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?

Jim White
ANU, Canberra

More information about the gmp-discuss mailing list