Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Puzzling Assembler Question

Author: Dieter Buerssner

Date: 03:06:32 02/20/04

Go up one level in this thread


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");
>}

untested (did not even try to compile it):

inline int FirstSetBit(unsigned long long a)
{
  int res;
  __asm__ __volatile__
   ("bsfl	4(%1), %0     \n"
    "addl	$32, %0       \n"
    "bsfl	(%1), %0      \n"
    : "=r&" (res) : "r"(&a): "cc");
  return res;
}

Or perhaps:

inline int FirstSetBit(unsigned long long a)
{
  int res;
  __asm__ __volatile__
   ("bsfl	%1, %0     \n"
    "addl	$32, %0    \n"
    "bsfl	%2, %0     \n"
    : "=r&" (res)
    : "g"((unsigned long)a), "g" ((unsigned long)(a>>32))
    : "cc");
  return res;
}

Might be faster (no need to calculate the adress of a in general).

You are probably aware, that you depend on undocumented behaviour of bsf (that
it leaves the output unchanged for 0)

Regards,
Dieter



This page took 0.01 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.