Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Puzzling Assembler Question

Author: Dann Corbit

Date: 15:23:56 02/19/04

Go up one level in this thread


On February 19, 2004 at 18:16:17, Dann Corbit wrote:

>On February 19, 2004 at 17:41:59, James Robertson wrote:
>
>>... puzzling for me at least. A short time ago the following code compiled
>>nicely on the server machines using linux/g++ at my school:
>>
>>inline int FirstSetBit(unsigned long long a)
>>{
>>  asm ("
>>    bsf	4(%0), %%eax
>>    add	$32, %%eax
>>    bsf	(%0), %%eax
>>    ":: "r"(&a): "%eax");
>>}
>>
>>Since that time I suppose that there have been a number of upgrades and so forth
>>to my school computers. Unfortunately, now I get a number of errors. I fixed the
>>first few by simply appending ; \ to each line, but the compiler is still
>>unhappy. Specifically, it complains that the '%eax' register is unknown on the
>>final line. Can anyone give me any help?
>
>You might want to examine here and below for an alternative pure C model that
>seems to be just about the same speed:
>http://www.talkchess.com/forums/1/message.html?349781

Oops.  Never mind.  YOU are doing first bit, and his post was for popcount.
But there are some C equivalents that are about as fast for this one too.

The CCC archives will probably turn it up.

Something you can try when you want to see the assembly syntax for a compiler is
to simply generate it.  Here is an example using one of Deiter's routines:

#ifdef _MSC_VER
typedef unsigned __int64 BITBOARD;
#else
typedef unsigned long long BITBOARD;
#endif

typedef union tag_bu {
    BITBOARD        b;
    unsigned long   w[2];
}   BU;

int    dieter_popc(BU a)
{
    unsigned long   w;
    int             n = 0;
    w = a.w[0];
    if (w)
        do
            n++;
        while ((w &= w - 1) != 0);
    w = a.w[1];
    if (w)
        do
            n++;
        while ((w &= w - 1) != 0);
    return n;
}

/*
        .file   "pop.c"
        .text
        .p2align 4,,15
.globl _dieter_popc
        .def    _dieter_popc;   .scl    2;      .type   32;     .endef
_dieter_popc:
        pushl   %ebp
        xorl    %ecx, %ecx
        movl    %esp, %ebp
        pushl   %esi
        movl    8(%ebp), %eax
        movl    12(%ebp), %esi
        pushl   %ebx
        testl   %eax, %eax
        movl    %eax, %edx
        je      L2
        .p2align 4,,15
L3:
        leal    -1(%edx), %ebx
        incl    %ecx
        andl    %ebx, %edx
        jne     L3
L2:
        testl   %esi, %esi
        movl    %esi, %edx
        je      L7
        .p2align 4,,15
L8:
        leal    -1(%edx), %esi
        incl    %ecx
        andl    %esi, %edx
        jne     L8
L7:
        popl    %ebx
        movl    %ecx, %eax
        popl    %esi
        popl    %ebp
        ret
*/



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.