Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about importance of branchless code for speed

Author: Robert Hyatt

Date: 09:12:22 04/09/03

Go up one level in this thread


On April 09, 2003 at 11:11:01, Uri Blass wrote:

>Supppose file1 and file2 are numbers between 0 and 7.
>
>Am I correct to assume that in general it is better to not to use
>
>int A[8];
>If (file1<=file2) x+=A[file2-file1];
>
>
>but instead of it something like
>
>int PADDED_A[15];
>int * const A = PADDED_A+7;
>x+=A[file2-file1];
>
>\\A[x]=0 for x=-1,x=-2,...x=-7
>
>
>Uri


It is not simple to answer.  if the a[] array results in a cache miss, which
will happen if
it is rarely executed, then the penalty is _huge_.  And when I say huge, I mean
_far_
greater than the rather modest (by comparison) branch misprediction penalty.

If a branch is done right it is the best answer, period.  By "done right" I mean
that most of
the time it is consistently taken or not taken.  Then branch prediction will hit
it dead on and
there is no penalty of any kind.

Branchless code can be produced using arrays, which can be dangerous due to the
cache miss
penalty of loading the stuff from very slow latency memory.  It can be produced
by doing
two pieces of work and then choosing the right one arithmetically:

before:

if (wtm == 1)
  score+=bonus1
else
  score+=bonus2;

You could turn that into:

score+=wtm*bonus1 + (wtm^1)*bonus2;

the new code does more work, but has no branch to mispredict.




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.