minor noises made by gcc and -std=iso9899:1999 with -Wall -pedantic -Wextra -pedantic-errors

Torbjörn Granlund tg at gmplib.org
Mon Jul 1 21:12:07 UTC 2019

Vincent Lefevre <vincent at vinc17.net> writes:

  >     mp_size_t n = 1 + (2 * an >= 3 * bn ? (an - 1) / (size_t) 3 : (bn - 1) >> 1);
  >   What's the point of the cast to size_t?
  > It is a slight performance optimisation.

  Could you explain?

The code for division by constants is shorter for unsigned than for
signed dividends.

  I would say that, on the contrary, it may make
  the code slower, e.g. on platforms where size_t > mp_size_t: the
  compiler does not know that an > 0, thus it could not reduce the
  size of the division to the one of mp_size_t in order to support
  negative arguments implicitly converted to size_t.

Do you have some real-world platform in mind?  Please show me the asm
code which is slower with that (size_t) cast than without it.

  >   Moreover, GMP favors signed arithmetic with a signed type mp_size_t,
  >   thus artificially switching to unsigned arithmetic with this cast is
  >   a bad idea.
  > Why?  We have full control of the ranges of the involved variables.

  You do, but the compiler does not know the ranges. And if the compiler
  knew the ranges, the presence of the cast would not change anything
  for the compiler.

I thought you argued against casting to size_t, but here you seem to
prefer additional such casting.

Please encrypt, key id 0xC8601622

More information about the gmp-discuss mailing list