Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Determining location of LSB/MSB

Author: Gerd Isenberg

Date: 07:24:27 02/09/04

Go up one level in this thread


On February 09, 2004 at 08:15:18, Omid David Tabibi wrote:

>On February 09, 2004 at 05:09:07, Renze Steenhuisen wrote:
>
>>
>>Hi there!
>>
>>does anyone know the fastest way to determine the lcoation of the Least/Most
>>Significant Bit, and to clear that bit?
>>
>>In my case the most important platforms (at the moment) are Pentium III and IV.
>>
>
>For 32 bit data:
>
>
>__forceinline int findFirstBitTrue(int a) {
>	__asm   bsf		eax, dword ptr[a]
>};
>
>__forceinline int findLastBitTrue(int a) {
>	__asm   bsr		eax, dword ptr[a]
>};
>
>
>If you know that a certain bit is 1, and you want to clear it, probably the best
>way to do that is:
>
>#define XorBit(a,i)		(a ^= (1 << i))
>
>Otherwise, if you don't know whether a bit is set or not, you can use the
>following macros to set it to 1 or 0:
>
>#define SetBit(a,i)		(a |= (1 << i))
>#define DelBit(a,i)		(a &= ~(1 << i))
>
>Or in assembly:
>
>__forceinline void setBitTrue(int *a, int i) {
>	__asm {
>		mov		ebx, dword ptr[a]
>		mov		eax, dword ptr[i]
>		bts		[ebx], eax
>	}
>};
>
>__forceinline void setBitFalse(int *a, int i) {
>	__asm {
>		mov		ebx, dword ptr[a]
>		mov		eax, dword ptr[i]
>		btr		[ebx], eax
>	}
>};
>
>But here I don't think the assembly version will be faster than the C macro.
>

Yes, on x86-32 btX is rather expensive, specially the btX mem,reg32 version.
On PIII/P4 it's a "complex" instruction, on Athlon32 a 9-cycle vector-path
instruction:

Athlon32 latencies of the BT family:

BT  mreg16/32, reg16/32 0Fh A3h 11-xxx-xxx DirectPath 1
BT  mem16/32, reg16/32  0Fh A3h mm-xxx-xxx VectorPath 8
BT  mreg16/32, imm8     0Fh BAh 11-100-xxx DirectPath 1
BT  mem16/32, imm8      0Fh BAh mm-100-xxx DirectPath 4
BTC mreg16/32, reg16/32 0Fh BBh 11-xxx-xxx VectorPath 2
BTC mem16/32, reg16/32  0Fh BBh mm-xxx-xxx VectorPath 9
BTC mreg16/32, imm8     0Fh BAh 11-111-xxx VectorPath 2
BTC mem16/32, imm8      0Fh BAh mm-111-xxx VectorPath 6
BTR mreg16/32, reg16/32 0Fh B3h 11-xxx-xxx VectorPath 2
BTR mem16/32, reg16/32  0Fh B3h mm-xxx-xxx VectorPath 9
BTR mreg16/32, imm8     0Fh BAh 11-110-xxx VectorPath 2
BTR mem16/32, imm8      0Fh BAh mm-110-xxx VectorPath 6
BTS mreg16/32, reg16/32 0Fh ABh 11-xxx-xxx VectorPath 2
BTS mem16/32, reg16/32  0Fh ABh mm-xxx-xxx VectorPath 9
BTS mreg16/32, imm8     0Fh BAh 11-101-xxx VectorPath 2
BTS mem16/32, imm8      0Fh BAh mm-101-xxx VectorPath 6


>
>>Thanks!
>>
>>Renze



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.