Re: copy-bit macro

James A (jamesa@demon.net)
09 Dec 1997 18:06:49 +0000


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;
> if (input & INPUT_FLAG_BAR)
> output |= OUTPUT_FLAG_BAR
> if (input & INPUT_FLAG_BAZ)
> output |= OUTPUT_FLAG_BAZ
> etc.
>
> 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;
> output |= ((input / INPUT_FLAG_BAR) & 1) * OUTPUT_FLAG_BAR;
> output |= ((input / INPUT_FLAG_BAZ) & 1) * OUTPUT_FLAG_BAZ;

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.

output |= (input & INPUT_FLAG_FOO) ? OUTPUT_FLAG_FOO : 0;

Might be better, and useful inside a macro. If it really would
affect speed then I guess you could go for an inline asm function.

-- 
James Antill