> > if (input & INPUT_FLAG_FOO)
> > output |= OUTPUT_FLAG_FOO;
> > The GCC output from this on the x86 is full of (slow) jumps.
> > Also, jumps inhibit optimization.
> > More efficient code, which can also be optimized around more,
> > is produced by
> > output |= ((input / INPUT_FLAG_FOO) & 1) * OUTPUT_FLAG_FOO;
> The only problem is that...
> #define INPUT_FLAG_FOO 1
> #define OUTPUT_FLAG_FOO 1
> input = 2;
> output |= ((input / INPUT_FLAG_FOO) & 1) * OUTPUT_FLAG_FOO;
> Doesn't work.
Yes it does. Why wouldn't it?
The only caveat (in general) is that the flags have to be unsigned -
otherwise gcc treats the division differently depending on whether
input<0 (which means there is still a jump in the code).
> output |= (input & INPUT_FLAG_FOO) ? OUTPUT_FLAG_FOO : 0;
> Might be better,
but contains a jump, so it is the same as the original.
imc