Mail Archives: djgpp/1998/02/17/11:43:03
Paul Derbyshire writes:
>I sincerely hope GCC's optimizer is smart enough to silently use a
>bitshift when optimizing
>
>int x;
>...
>y = x % 8;
>
>for instance.
The modulus operator and a bitmask are not exactly the same thing, so it
cannot do that. The result of a%b has a range from -b+1 to b-1, wheras
a&(b-1), where b is a power of two, ranges from 0 to b-1.
If you are using unsigned data, gcc is able to do this optimisation. But
in many cases when you are using signed values, it is both easier and
less error prone to write an explicit & operation than to keep casting
back and forth between signed and unsigned variables.
--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
"Pigs use it for a tambourine" - Frank Zappa
- Raw text -