Author: Gerd Isenberg
Date: 14:42:36 07/31/03
Go up one level in this thread
On July 31, 2003 at 16:46:57, Rick Bischoff wrote:
>Hi,
>
>I am porting my engine to windows since my G4 is too slow :-) Anyway, I need a
>suitable "extract bit and return position" function (preferably inline GCC
>assembly with no table lookup). I thereby donate my PowerPC assembly/C++
>routine to the public domain for payment:
>
>/* Extract first set bit from X and return position */
>inline int exbit(u64& X) {
>
>u32 high = X >> 32;
>u32 low = (u32)X;
>
>int z;
>
>/* Find out how many zeroes: */
>if (high == 0) {
>
>/* Step 1: Count the leading zeroes (this is a single instruction on the PPC)
> Step 2: Translate that into a bit position (i.e., 31-z)
> Step 3: Clear that bit in the word
> Step 4: return the result
>*/
>
>asm ( "cntlzw %0, %1" : "=r"(z) : "r"(low) );
>z = 31 - z;
>X = low ^ (1<<z);
>return z;
>
>} else {
>
>asm ( "cntlzw %0, %1" : "=r"(z) : "r"(high) );
>z = 31 - z;
>X = (u64) (((u64)high ^ (1<<z)) << 32) | low;
>
>return z|32;
>}
>}
Hi Rick,
on x86 platforms there is no leading zero count, but "bit scan reverse" (from
MSB, bit 31 to LSB bit 0) instead. If cntlzw is zero, bsr scans already 31.
So you may replace "cntlzw" by "bsr" and skip the 31-z, but i'm not sure about
the GCC-syntax.
intel syntax (target, source) is:
bsr reg32, reg32/mem32
Regards,
Gerd
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.