Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Countbits() Function

Author: Carsten Kossendey

Date: 07:19:17 01/03/99

Go up one level in this thread


On January 03, 1999 at 08:29:23, Roberto Waldteufel wrote:

>Hi all,
>
>I recently did some code timing experiments, and I thought the results might be
>useful to some of you here...
>
>I coded two versions of a function to count the number of 1-bits in a bitboard.
>One method is to use a loop with the bitscan instructions, repeated on the low
>and high order 32-bit chunks. The other uses only logical instructions, with no
>loops. See Ernst Heinz's DarkThought site for a detailed account of these
>methods. I was interested to compare the methods on Pentium 32-bit architecture:
>the loop is best when only a few bits are set (it terminates quickly) with time
>proportional to the number of bits set, and the second method is faster if many
>bits are set, since it does not depend on how many bits are set.
>
>It turned out for me that the loop is preferable if at most 6 bits are set, and
>for 7 or more bits the logical instructions perform best. With exactly 7 bits,
>the difference is less than 1%. That was on a PII 333MHz box. The break-even
>point would be much higher on any Intel without fast bit-scans, ie bit scans are
>*much* faster on PII and PPro than on earlier Intel CPU's, whereas the logical
>instructon method would take about the same time for any bitboard, dependant
>only on clock speed.
>
>Hope this is useful for someone....:-)
>
>Roberto

Jumping onto the same train, I did similar tests on the PowerPC a good while
ago, with around the same results (loops faster up to 6 or 7 bits), which is
interesting considering that the PPC doesn't have BSF/BSR instructions, but you
must rather use two instructions to do that:

  cntlzw rTemp,   rSource               ; find first 1 bit
  rlwnm. rSource, rSource, rTemp, 0, 30 ; rotate to LSB and mask out

Carsten



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.