Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: fast bit counting

Author: Dan Newman

Date: 11:13:05 04/19/00

Go up one level in this thread


On April 19, 2000 at 04:31:48, Tony Werten wrote:

>On April 19, 2000 at 03:27:16, Dan Newman wrote:
>
>>On April 18, 2000 at 22:44:25, Flemming Rodler wrote:
>>
>>>Hi,
>>>
>>>I am trying to implement a bitboard based chess program on a Pentium or AMD
>>>computer. I need to be able to find the following information fast:
>>>
>>>1) The position of the first and/or last bit in a sequence of 64 bits.
>>>2) Count the number of bits that are 1 in a sequence of 64 bits.
>>>
>>>I know there is a method that works linear in the number of on-bits for
>>>problem 2:
>>>
>>>    for(count = 0; bitboard; count++, bitboard &= (bitboard -1));
>>>
>>>
>>>Is there anything faster, ie. such lookuptables or machine code intrutions?
>>>
>>>What about problem 1?
>>>
>>>Thanks in advance for any reply
>>>Flemming
>>
>>
>>The following is what I use in my program to find bit indices
>>(it works only with MSVC).
>>
>>#pragma warning( push)
>>#pragma warning( disable: 4035)
>>inline int bsf( unsigned long bitpat)
>>{
>>    __asm mov eax, bitpat
>
>Just a small question. Can you leave this mov away ? Since bitpat will be passed
>in the eax register anyway ? Or doesn't this work for inline functions ?
>( I'm a delphi man, and delphi doesn't support inline )
>
>Tony
>

Well, I haven't tried that.  I think that MSVC's default argument passing
scheme is by stack rather than register.  I'll have to try it (with passing
via registers turned on, I guess)...  (It could be, since this is inline,
that the optimizer gets rid of any extra move anyway and just arranges for
a copy of the argument to end up in eax just before the BSF.)

-Dan.

>>    __asm bsf eax, eax
>>}
>>inline int bsr( unsigned long bitpat)
>>{
>>    __asm mov eax, bitpat
>>    __asm bsr eax, eax
>>}
>>#pragma warning( pop)
>>
>>
>>-Dan.



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.