Computer Chess Club Archives


Search

Terms

Messages

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

Author: martin fierz

Date: 18:32:31 04/08/02

Go up one level in this thread


On April 08, 2002 at 20:02:41, Dann Corbit wrote:

>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?
>
>Here are some pure C bit counting functions:
>ftp://cap.connx.com/pub/bitcount/BITC.ZIP

hi dann,

thanks for the link. but i already have good C bit counting functions :-)
what i'm looking for, more specifically, is an adaption of the crafty vcinline.h
(pasted below) file to 32 bits - someone told me that specially MSB and LSB was
only one instruction in assembly - that would really be nice...

aloha
  martin

extern unsigned char  first_ones[65536];
extern unsigned char  last_ones[65536];

#if _MSC_VER >= 1200
#define FORCEINLINE __forceinline
#else
#define FORCEINLINE __inline
#endif


FORCEINLINE int PopCnt(BITBOARD a) {

/* Because Crafty bitboards are typically sparsely populated, we use a
   streamlined version of the boolean.c algorithm instead of the one in x86.s */

  __asm {
        mov     ecx, dword ptr a
        xor     eax, eax
        test    ecx, ecx
        jz      l1
    l0: lea     edx, [ecx-1]
        inc     eax
        and     ecx, edx
        jnz     l0
    l1: mov     ecx, dword ptr a+4
        test    ecx, ecx
        jz      l3
    l2: lea     edx, [ecx-1]
        inc     eax
        and     ecx, edx
        jnz     l2
    l3:
  }
}


FORCEINLINE int FirstOne(BITBOARD a) {

#if _M_IX86 <= 500 /* on plain Pentiums, use boolean.c algorithm */
  __asm {
        movzx   edx, word ptr a+6
        xor     eax, eax
        test    edx, edx
        jnz     l1
        mov     dx, word ptr a+4
        mov     eax, 16
        test    edx, edx
        jnz     l1
        mov     dx, word ptr a+2
        mov     eax, 32
        test    edx, edx
        jnz     l1
        mov     dx, word ptr a
        mov     eax, 48
  l1:   add     al, byte ptr first_ones[edx]
  }
#else /* BSF and BSR are *fast* instructions on PPro/PII */
  __asm {
        bsr     edx, dword ptr a+4
        mov     eax, 31
        jnz     l1
        bsr     edx, dword ptr a
        mov     eax, 63
        jnz     l1
        mov     edx, -1
  l1:   sub     eax, edx
  }
#endif /* _M_IX86 > 500 */
}

FORCEINLINE int LastOne(BITBOARD a) {

#if _M_IX86 <= 500 /* on plain Pentiums, use boolean.c algorithm */
  __asm {
        movzx   edx, word ptr a
        mov     eax, 48
        test    edx, edx
        jnz     l1
        mov     dx, word ptr a+2
        mov     eax, 32
        test    edx, edx
        jnz     l1
        mov     dx, word ptr a+4
        mov     eax, 16
        test    edx, edx
        jnz     l1
        mov     dx, word ptr a+6
        xor     eax, eax
        test    edx, edx
        jnz     l1
        mov     eax, 48
l1:     add     al, byte ptr last_ones[edx]
  }
#else /* BSF and BSR are *fast* instructions on PPro/PII */
  __asm {
        bsf     edx, dword ptr a
        mov     eax, 63
        jnz     l1
        bsf     edx, dword ptr a+4
        mov     eax, 31
        jnz     l1
        mov     edx, -33
  l1:   sub     eax, edx
  }
#endif /* _M_IX386 > 500 */
}



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.