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
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
sdiv2(int x) /* 5 instr */
return x / 2;
udiv2(unsigned x) /* 3 instr */
return x / 2U;
sdiv3(int x) /* 7 instr */
return x / 3;
udiv3(unsigned x) /* 6 instr */
return x / 3U;
Niels Möller. PGP-encrypted email is preferred. Keyid C0B98E26.
Internet email is subject to wholesale government surveillance.
More information about the gmp-devel