Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: "Using de Bruijn Sequences to Index a 1 in a Computer Word"

Author: Eugene Nalimov

Date: 21:47:21 02/12/02

Go up one level in this thread


For VC it doesn't matter what you will write --
    b = ((i & 0xFFFF0000) != 0);
or
    if ((i & 0xFFFF0000) != 0)
        b = 1;
    else
        b = 0;

Also it would generate not 'setne', but instruction sequence that is more
efficient on some x86 CPUs due to lack of partial registers stalls:

; Listing generated by Microsoft (R) Optimizing Compiler Version 13.00.9351

	TITLE	t.c
	.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT	SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT	ENDS
_DATA	SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA	ENDS
CONST	SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST	ENDS
_BSS	SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS	ENDS
$$SYMBOLS	SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS	ENDS
_TLS	SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS	ENDS
FLAT	GROUP _DATA, CONST, _BSS
	ASSUME	CS: FLAT, DS: FLAT, SS: FLAT
endif

INCLUDELIB LIBC
INCLUDELIB OLDNAMES

PUBLIC	_Lookup8
EXTRN	_tab:BYTE
; Function compile flags: /Ogty
_TEXT	SEGMENT
_i$ = 8
_Lookup8 PROC NEAR
; File c:\temp\t.c
; Line 7
	mov	edx, DWORD PTR _i$[esp-4]
	mov	eax, edx
	and	eax, -65536				; ffff0000H
	neg	eax
	sbb	eax, eax
	neg	eax
; Line 8
	shl	eax, 4
	push	esi
	mov	esi, eax
; Line 9
	mov	ecx, eax
	shr	edx, cl
; Line 10
	mov	eax, edx
	and	eax, 65280				; 0000ff00H
	neg	eax
	sbb	eax, eax
	neg	eax
; Line 12
	lea	ecx, DWORD PTR [eax*8]
	shr	edx, cl
; Line 13
	movzx	eax, BYTE PTR _tab[edx]
	add	eax, ecx
	add	eax, esi
	pop	esi
; Line 14
	ret	0
_Lookup8 ENDP
_TEXT	ENDS

Eugene

On February 11, 2002 at 18:49:49, Angrim wrote:

>On February 11, 2002 at 13:03:57, Eugene Nalimov wrote:
><snip>
>>(3) They did not try to remove poorly predicted branch from their implementation
>>of the LOOKUP algorithm. For example, on x86 there is no branches in the
>>following variant:
>>
>>  extern unsigned char tab[256];
>>
>>  unsigned Lookup8 (unsigned i)
>>  {
>>      unsigned b, n;
>>
>>      b = ((i & 0xFFFF0000) != 0);
>>      n = b * 16;
>>      i >>= b * 16;
>>      b = ((i & 0x0000FF00) != 0);
>>      n += b * 8;
>>      i >>= b * 8;
>>      return n + tab[i];
>>  }
>>
>>Eugene
>
>Cool, your posts are a nice source to learn more about program
>optimization.  I had thought that code like
> b= (i!=0);
>would be the same as
> if(i!=0) b=1; else b=0;
>but now I know that the compiler(even gcc) is smart enough to use
>"test" and "setne" instructions to avoid branches here.
>
>Now to glance through my engine a bit and see if I can find some
>places that I can use this information.
>
>Angrim



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.