News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Retrieve First and last bit (bsf, bsr)

Started by Andrea Lanza, June 17, 2006, 12:04:45 PM

Previous topic - Next topic

Andrea Lanza

 :8) Hi,
I wrote two function to accomplish these:

procedure PrimoBitx86(I: uns64);
begin PrimoBitx86;
   mov(0, edx);
   bsf((type dword I), eax);
   jnz Fatto;
   mov(32, edx);
   bsf((type dword I[4]), eax);
   jnz Fatto;
   mov(32, eax);
Fatto:   
   add(edx, eax);
end PrimoBitx86;

procedure UltimoBitx86(I: uns64);
begin UltimoBitx86;
   mov(32, edx);
   bsr((type dword I[4]), eax);
   jnz Fatto;
   mov(0, edx);
   bsr((type dword I), eax);
   jnz Fatto;
   mov(64, eax);
Fatto:   
   add(edx, eax);
end UltimoBitx86;


These two functions works well and retrieve the first bit and the last bit set from a 64 bit number, in x86 processor.
Can this two functions be opimize or nothing more can be done?
This two functions are implemented in a DLL that i compiled with HLA 1.81 but i think that don't work on a x64 machine.
is there a way to compile this DLL for x64 machine and eventually use the new 64 register (rax, rbx ....)?

Thank's all
Andrea

Sevag.K

You could try the discrete instructions (shifting and checking carry set), though you'll probably get different speed results on different CPUs so it's really not worth it.  You could use the @nodisplay and @noalignstack procedure options to make if a tiny bit more efficient.

HLA does not support 64bit (probably won't for a good while).  One way to do so would be to compile it to FASM source and tweak it to 64 bits using FASM.

Andrea Lanza

 :U I recompiled the DLL with @nodisplay and @noalignstack and seems a little bit faster.
For FASM it's more difficult for me. I don't know if exist the BSF and BSR instruction for the 64-bit register.
Do you know if exist some instruction to accomplish this task?

Thank's for your quick reply...
Andrea

Sevag.K

They exist unchanged in 64bit mode.  Just use 64bit register/memory.

You can find the technical documentation of the AMD64 series here
http://developer.amd.com/documentation.aspx

For me, I'm stuck using a 32 bit OS installed on a 64 bit AMD so I'm limited on the 64 bit assembly experiments I can do.

Andrea Lanza