Re: copy-bit macro

Ian Collier (imc@comlab.ox.ac.uk)
Wed, 10 Dec 1997 11:52:03 +0000 (GMT)


On 09 Dec 1997 18:06:49 +0000, James A said:
> Colin Plumb <colin@nyx.net> writes:
> > I notice that a lot of code contains long lines of

> > 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