Computer Chess Club Archives


Search

Terms

Messages

Subject: Branchless code

Author: Russell Reagan

Date: 17:04:23 11/18/02


Branchless code has the potential to run faster than the equivalent code that
uses conditionals, or so I've heard. I was wondering about how people go about
getting rid of conditional code, since it would seem to make a significant
difference in some cases (such as in move generation or evaluation).

One idea that came to mind is to use an array of function pointers instead of a
conditional. For example, instead of something like:

if (side_to_move == WHITE) {
    // do white stuff here
}
else {
    // do black stuff here
}

You could do something like:

// Forgive me if my syntax for using function pointers is wrong
Function do_stuff[] = { DoWhiteStuff, DoBlackStuff };
do_stuff[side_to_move]();

That would eliminate the potential branch misprediction penalty, and only cost a
call and ret instruction. So as long as the call+ret instructions don't cost
more than 10-20 cycles that you would suffer for a branch misprediction, you win
here.

In thinking about this, it sounded similar to the OOP refactoring technique
known as "replace conditional with polymorphism". Is that essentially what is
happening when I replace a conditional with a lookup table of function pointers?

Anyway, I was wondering if someone more knowledgable than myself about these
things could shed some light on which method would be more efficient, and about
the real worth of branchless code. Is it worth a great amount of extra work to
achieve branchless code? Or is it only going to provide minimal speedups?

Thanks,
Russell



This page took 0.01 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.