Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Branchless code

Author: Pham Hong Nguyen

Date: 18:07:57 11/18/02

Go up one level in this thread


On November 18, 2002 at 20:50:36, Russell Reagan wrote:

>On November 18, 2002 at 20:40:20, Pham Hong Nguyen wrote:
>
>>Don't use if - else ;)
>
>So, for example, if I was going to generate pawn moves, it would be better to
>have a lookup table of pawn directions for each side and a single function,
>rather than to have two seperate functions (GenWhitePawnMoves() and
>GenBlackPawnMoves, for example)?
>
>int directions[] = { 8, -8 };
>void GenPawnMoves() {
>    int direction = directions[side_to_move;
>    // generate moves...
>}
>
>So the above would be preferable to somehthing like:

Yes, I think you are right, at least in case of small lookup table.
For understanding more about if-else problem, I suggest you to take a look at
following address. The author quite "hates" if-else and he is an expert of
avoiding them:

http://www.azillionmonkeys.com/qed/optimize.html

>
>
>void GenWhitePawnMoves() {
>    int direction = 8;
>    // generate moves...
>}
>
>void GenBlackPawnMoves() {
>    int direction = -8;
>    // generate moves...
>}
>
>void GenPawnMoves() {
>    if (side_to_move == WHITE)
>        GenWhitePawnMoves();
>    else
>        GenBlackPawnMoveS();
>}
>
>Is that correct?



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.