Attempting to optimize a multiply

Torbjorn Granlund tege at swox.com
Fri Feb 18 22:40:30 CET 2005


"Richard Cavell" <richardcavell at mail.com> writes:

  I have a calculation which goes like this:
  
  mpz_t a;   This is initialised to some number
  mpz_t b;   This is initialised to some number
  mpz_t c, d;
  
  mpz_mul (c, a, b);
  mpz_setbit(a, 25);    // I know categorically that bit 25 is clear before this line.
  mpz_mul (d, a b);
  
  I tried to optimize it like this:
  
  mpz_t a;   This is initialised to some number
  mpz_t b;   This is initialised to some number
  mpz_t c, d;
  
  mpz_mul (c, a, b);
  mpz_mul_2exp (b, b, 25);
  mpz_add (d, c, b);
  
  In theory, this should work.  It should eliminate a multiply and instead
  convert it to a left shift and add.  But it grows my execution time
  considerably (on a G4 using GCC, 32-bit size operands).
  
  1.  Is this what you would expect?
  2.  For operands in the region of 10 to the power 100, what would you expect?
  
I don't understand.  There is at least one syntax error above, so
you clearly haven't run that version of the code.

With the syntax error fixed, the two code versions don't compute
the same function.  You might need to redo your maths.

Since different things are computed, disparate timing results
are to be expected.

--
Torbjörn


More information about the gmp-discuss mailing list