help: gcc asm-> vc++ asm

  • Follow


Hi,

I'm not a very good asm programer (in fact not at all) but i
desperately need to convert some functions from gcc asm inlined to the
vc++ format.

These are found in glibc and other places like tlsf memory allocator:

/* find first bit set */
static inline int _ffs(int x) {
        int r;

        __asm__("bsfl %1,%0\n\t"
                "jnz 1f\n\t"
                "movl $-1,%0\n"
                "1:" : "=r" (r) : "g" (x));
        return r;
}

/* find last bit set */
static inline int _fls(int x) {
        int r;

        __asm__("bsrl %1,%0\n\t"
                "jnz 1f\n\t"
		"movl $-1,%0\n"
                "1:" : "=r" (r) : "g" (x));
        return r;
}

Thanks,
Felix

0
Reply spamtrap2 (1628) 2/22/2006 4:28:02 AM

"felix"  <spamtrap@crayne.org> wrote:
>
>I'm not a very good asm programer (in fact not at all) but i
>desperately need to convert some functions from gcc asm inlined to the
>vc++ format.

It is very hard for me to believe that you "desperately" need this.

>These are found in glibc and other places like tlsf memory allocator:
>
>/* find first bit set */
>static inline int _ffs(int x) {
>        int r;
>
>        __asm__("bsfl %1,%0\n\t"
>                "jnz 1f\n\t"
>                "movl $-1,%0\n"
>                "1:" : "=r" (r) : "g" (x));
>        return r;
>}

int __inline ffs( int x )
{
    __asm {
        mov ecx, [x]
        bsf eax, ecx
        jnz ffs1
        mov eax, -1
    ffs1:
    }
}

>/* find last bit set */
>static inline int _fls(int x) {
>        int r;
>
>        __asm__("bsrl %1,%0\n\t"
>                "jnz 1f\n\t"
>		"movl $-1,%0\n"
>                "1:" : "=r" (r) : "g" (x));
>        return r;
>}

int __inline fls( int x )
{
    __asm {
        mov ecx, [x]
        bsr eax, ecx
        jnz ffs1
        mov eax, -1
    fls1:
    }
}
-- 
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.

0
Reply Tim 2/23/2006 6:50:52 AM


Tim,

Thanks for the help!

> It is very hard for me to believe that you "desperately" need this.

Yes, I am, I am! :)

I'm writing memory allocator (like many others before me) but with a
special purpose: cache friendly for intense vector anaylitics (for both
Linux and Windows on 32 and 64 bit).

On another note, I just discovered that vc++ (6/7/..) has some builtins
that do that in portable manner between 32 and 64 bit compilers but i
haven't found if GCC has an equivalent yet.

http://msdn2.microsoft.com/en-us/library(d=robot)/wfd9z0bb.aspx

Regards,
Felix

0
Reply felix 2/23/2006 11:34:02 AM

"Tim Roberts" <spamtrap@crayne.org> wrote in message
news:gcmqv19rvd2tmfhq4pvtgk6fal712s9q16@4ax.com...
> "felix"  <spamtrap@crayne.org> wrote:
> >
> >I'm not a very good asm programer (in fact not at all) but i
> >desperately need to convert some functions from gcc asm inlined to the
> >vc++ format.
>
> It is very hard for me to believe that you "desperately" need this.
>
> >These are found in glibc and other places like tlsf memory allocator:
> >
> >/* find first bit set */
> >static inline int _ffs(int x) {
> >        int r;
> >
> >        __asm__("bsfl %1,%0\n\t"
> >                "jnz 1f\n\t"
> >                "movl $-1,%0\n"
> >                "1:" : "=r" (r) : "g" (x));
> >        return r;
> >}
>
> int __inline ffs( int x )
> {
>     __asm {
>         mov ecx, [x]
>         bsf eax, ecx
>         jnz ffs1
>         mov eax, -1
>     ffs1:
>     }
> }
>
> >/* find last bit set */
> >static inline int _fls(int x) {
> >        int r;
> >
> >        __asm__("bsrl %1,%0\n\t"
> >                "jnz 1f\n\t"
> > "movl $-1,%0\n"
> >                "1:" : "=r" (r) : "g" (x));
> >        return r;
> >}
>
> int __inline fls( int x )
> {
>     __asm {
>         mov ecx, [x]
>         bsr eax, ecx
>         jnz ffs1
>         mov eax, -1
>     fls1:
>     }
> }

Thanks Tim!  Your post showed a feature of WASM (or MASM?) I wasn't familiar
with. :-)  Unfortunately, I think there are a couple of errors in the code
you posted.  So, I'm posting the changes I believe are necessary:

int __inline _ffs( int x )
{
    int r;
    __asm {
        mov ecx, [x]
        bsf eax, ecx
        jnz ffs1
        mov eax, -1
    ffs1:
        mov [r], eax
    }
    return(r);
}

int __inline _fls( int x )
{
    int r;
    __asm {
        mov ecx, [x]
        bsr eax, ecx
        jnz fls1
        mov eax, -1
    fls1:
        mov [r], eax
    }
    return(r);
}


Rod Pemberton


0
Reply Rod 2/26/2006 9:24:57 PM

"Rod Pemberton"  <spamtrap@crayne.org> wrote:
>
>int __inline _ffs( int x )
>{
>    int r;
>    __asm {
>        mov ecx, [x]
>        bsf eax, ecx
>        jnz ffs1
>        mov eax, -1
>    ffs1:
>        mov [r], eax
>    }
>    return(r);
>}

You can improve this just slightly and eliminate the temporary by using
__declspec(naked):

int __inline __declspec(naked) _ffs( int x )
{
    __asm {
        mov ecx. [x]
        bsf eax, ecx
        jnz ffs1
        mov eax, -1
     ffs1:
    }
}
-- 
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.

0
Reply Tim 2/27/2006 4:53:33 AM

4 Replies
480 Views

(page loaded in 0.064 seconds)

Similiar Articles:













7/19/2012 7:56:56 PM


Reply: