Mail Archives: djgpp/1997/04/14/04:18:03
John M. Aldrich wrote:
>It should also be noted that gcc is very smart when it comes to
>optimizations, and will probably convert modulus operators into masks
>when you're modding with powers of two. It's okay to trust the
>compiler! ;)
This has been true with all recent compilers I've used (Borland,
Microsoft and a couple of Unix ones). They seem to do that piece
of optimisation even with optimisation turned off. The same for
multiplication and division by constant powers of 2. I find it
clearer to write:
lsb = word % 256;
msb = word / 256;
than the equivalent lowlevel operations:
lsb = word & 0xFF;
msb = word >> 8;
(assuming that 'word' is unsigned short, of course, for clarity).
I've never had a speed or size penalty because of it...
Chris
- Raw text -