Author: Tim Foden
Date: 07:42:05 04/09/02
Go up one level in this thread
On April 08, 2002 at 18:08:13, martin fierz wrote:
>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?
I'll give it a try. If anyone sees any bugs in the following code, please
reply, as I haven't tested it.
You could use the following if you are sure that the bitmap will NOT be zero
before calling the function.
inline int BitScanForward( UINT32 bitmap )
{
__asm bsf eax, [bitmap]
// value returned in eax
}
inline int BitScanReverse( UINT32 bitmap )
{
__asm bsr eax, [bitmap]
// value returned in eax
}
If you are unsure whether the bitmap may be zero, you should use something like:
inline int BitScanForward( UINT32 bitmap )
{
__asm
{
bsf eax, [bitmap]
jnz done
mov eax, -1 // change -1 to error value you want
done:
}
// value returned in eax
}
inline int BitScanReverse( UINT32 bitmap )
{
__asm
{
bsr eax, [bitmap]
jnz done
mov eax, -1 // change -1 to error value you want
done:
}
// value returned in eax
}
Or if you're willing to risk the fact that the result of a BSF or BSR is
undefined if bitmap is zero (but by current testing seems they don't change and
values in this case), the slightly faster:
inline int BitScanForward( UINT32 bitmap )
{
__asm
{
mov eax, -1 // change -1 to error value you want
bsf eax, [bitmap]
}
// value returned in eax
}
inline int BitScanReverse( UINT32 bitmap )
{
__asm
{
mov eax, -1 // change -1 to error value you want
bsr eax, [bitmap]
}
// value returned in eax
}
Hope this helps.
Cheers, Tim.
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.