Page 1 of 1

+ or |

PostPosted: 09 Nov 2007, 21:16
by Robert Pope
If I want to set certain bits in a variable, is it faster to add or to "OR"?

For example,
int value=piece+128; vs. int value=piece|128;

I know that piece<128, so either one is safe, but the compiler doesn't know that, so it must produce different code in each situation. Is one operation better to use than the other?

Re: + or |

PostPosted: 09 Nov 2007, 22:12
by bob
Robert Pope wrote:If I want to set certain bits in a variable, is it faster to add or to "OR"?

For example,
int value=piece+128; vs. int value=piece|128;

I know that piece<128, so either one is safe, but the compiler doesn't know that, so it must produce different code in each situation. Is one operation better to use than the other?


Not much difference. OR is safer since that works even if the bit is already set, whereas ADD will not produce the right answer if the bit is already set.

Re: + or |

PostPosted: 09 Nov 2007, 23:13
by Aleks Peshkov
ADD and OR asm commands are identical in speed. But compiler can sometimes optimize addition using unique x86 assembler instruction LEA. So + can be sometimes faster.

ADD version is slower in 32-bit bitboard code. Compiler have to chain ADD and ADC pair to get the right arithmetic result. Logical operations in both 32-bit parts of 64-bit int can go in parallel.