Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Inline assembly with g++/gcc

Author: Tom Likens

Date: 06:58:00 02/06/03

Go up one level in this thread


On February 06, 2003 at 06:30:17, David Rasmussen wrote:

>I do this (in a header file, because most implementations can't inline across
>compilation units) when compiling with MSVC/Intel:
>
>INLINE int FirstBit(const BitBoard bitboard)
>{
>	__asm
>	{
>		bsf eax,[bitboard+4]
>		xor eax,32
>		bsf eax,[bitboard]
>	}
>}
>
>INLINE int LastBit(const BitBoard bitboard)
>{
>	__asm
>	{
>		bsr eax,[bitboard]
>		sub eax,32
>		bsr eax,[bitboard+4]
>		add eax,32
>	}
>}
>
>INLINE int PopCount(BitBoard a)
>{
>    __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:
>    }
>}
>
>Is there anyway to do the same when using g++/gcc ?
>
>/David


Yes, an example would be,

inline int bitmap_t::findlsb(void) const
{
    int result;
    asm("    bsfl  %0,%0\n\t"
	"    jnz   1f\n\t"
	"    movl  %2,%0\n\t"
	"    bsfl  %0,%0\n\t"
	"    addl  $32,%0\n\t"
	"1:\n\t"
	: "=r" (result)
	: "0" (b32[LSB]), "r" (b32[MSB]));
    return result;
}

One of the best references on the web (IMHO) is:

http://www-106.ibm.com/developerworks/linux/library/l-ia.html?dwzone=linux

regards,
--tom



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.