+ or |

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

+ or |

Postby Robert Pope » 09 Nov 2007, 21:16

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?
Robert Pope
 
Posts: 39
Joined: 08 Apr 2006, 17:27

Re: + or |

Postby bob » 09 Nov 2007, 22:12

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.
User avatar
bob
 
Posts: 156
Joined: 10 May 2006, 17:59

Re: + or |

Postby Aleks Peshkov » 09 Nov 2007, 23:13

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.
Aleks Peshkov
 
Posts: 27
Joined: 13 Jul 2007, 13:14


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 46 guests