mini-gmp

Niels Möller nisse at lysator.liu.se
Fri Jan 17 09:51:28 UTC 2014


Vincent Lefevre <vincent at vinc17.net> writes:

> IMHO, if a non-negative value fits in a signed type and is regarded
> more as an element of the ring of integers Z than a bit array (said
> otherwise, it will be used in arithmetic expressions), then the type
> should be signed.

I see your point. Personally, I tend to use unsigned for non-negative
numbers such as loop counters (but then I have to be careful to avoid
underflow).

In the context of GMP, it's maybe worth noting that mp_bitcnt_t is an
unsigned type, and that for micro optimization, we prefer to use
unsigned types whenever dividing a non-negative number by a constant.
For illustration, compile the following with gcc -O3. Instruction counts
for x86_64.

  int
  sdiv2(int x) /* 5 instr */
  {
    return x / 2;
  }
  
  unsigned
  udiv2(unsigned x) /* 3 instr */
  {
    return x / 2U;
  }
  
  int
  sdiv3(int x) /* 7 instr */
  {
    return x / 3;
  }
  
  unsigned
  udiv3(unsigned x) /* 6 instr */
  {
    return x / 3U;
  }

Regards,
/Niels

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.


More information about the gmp-devel mailing list