Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: assembly popcount, msb, lsb for visual C and unsigned 32-bit integers?

Author: Steve Maughan

Date: 07:03:00 04/09/02

Go up one level in this thread


Martin,

I do use some assembler routines similar to what you're looking for - the only
difference is that they are using Delphi's inline assembler.

Anyway I think the following will be useful.

1) To find the least significant bit use the BSF assember directive.  This is
fast in Pentium Pro's and above e.g.

function BSF(i : Longword) : Integer; register; overload;
asm
  BSF   EAX,EAX
end;


2) To find the most significant bit use BSR in the same way as BSF


3) The best method for bit counting depends upon the average number of bits that
are likely to be set.  If there are normally few bits (e.g. <5), which is
normally the case with Chess / Checkers, then assembler may not be any faster
than the high level language e.g.

function PopCount(var i : int64) : Integer;
var
  j : int64;
begin
  j := i;
  result := 0;
  while j <> 0 do
  begin
    j := j and (j - 1);
    inc(result);
  end;
end;

However if you can use MMX there are some routines floating around the internet
that are supposedly faster if you have many bits set.

Hope this helps,

Regards,

Steve



>aloha!
>
>i'm sure this has been asked and answered before: my checkers program uses
>bitboards, but unlike chess, i only need 32 bits. my basic data type is
>therefore an unsigned 32-bit integer. i'm looking for assembly code to stuff in
>my C source code under visual C for bitcount, most significant and least
>significant bit. i know absolutely no assembler, so i can look at the crafty
>assembly code for 64 bits, but i can't adapt it :-(
>can anyone help?
>
>cheers
>  martin



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.