Post by LiloLilowhile I was looking for a fast way to detect the highest set bit
* Integer Log base 2 *)
function IntLog2 Value: integer ): integer;
asm
BSR EAX, EAX
end;
Till now I was doing this operation this way (C++)
unsigned BitSize(long long Data) { unsigned Size = 0; while (Data)
{ Data >>= 1; Size ++; } return Size; }
I'd like to understand if BSR EAX, EAX can fit into my c function to
speed up things. It can be done?
Ok. Other than the mention of "BSR EAX,EAX" this is really all C related...
Was that C or C++? Which compiler?
Post by LiloLiloI'd like to understand if BSR EAX, EAX can fit into my c function
C compilers usually support inline assembly. I'd think C++ compilers do
also. So, yes, it can fit into a C function.
Post by LiloLiloto speed up things.
You'll have to test it out. Usually, inline assembly in C has two problems.
First, inline assembly usually disables or interferes with code
optimization. Second, inline assembly forces the compiler to generate extra
instructions. All or most of the cpu registers are being used by the C
code. Extra instructions are used to save and restore the registers that
the C code is using which conflict with the register usage of the inline
assembly. So, the only way to know for sure if it'll speed up things is to
code it and test it.
Post by LiloLilodoing this operation this way
unsigned BitSize(long long Data) { unsigned Size = 0; while (Data)
{ Data >>= 1; Size ++; } return Size; }
First, do you want "unsigned long long Data"?
I'll assume a long long is 64-bits... If the compiler optimizes BitSize
into a small tight loop, it might be very difficult to outperform it. The
addition of a branch from a comparison can slow things down tremendously.
But, you could try some novice things like breaking Data into bytes. At
most, you'd need 8 shifts, but you'd need another loop to find the first
non-zero uppermost byte.
/* untested */
unsigned BitSize(unsigned long long Data)
{ unsigned Size = 56, Idx = 7, Byte;
/* find first non-zero upper most byte */
while (!(Byte=((unsigned char *)&Data)[Idx])&&Idx) {Idx--;Size-=8;}
/* shift Byte, not Data */
while (Byte){ Byte >>= 1; Size ++; }
return Size; }
However, my personal suspicions are that a loop and/or comparison will add
too much... but it is for 64-bits.
Rod Pemberton