Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C vs asm vs look-up optimization question

Author: Tony Werten

Date: 21:56:19 01/22/02

Go up one level in this thread


On January 22, 2002 at 16:06:19, Ed Schröder wrote:

>On January 21, 2002 at 18:38:44, Tim Foden wrote:
>
>>On January 21, 2002 at 16:16:10, Rafael Andrist wrote:
>>
>>>Well, I just rewrote the following function in assembler to get better speed (no
>>>conditional jumps, less memory access) but the speedup was only minimal. A
>>>possible problem of the asm code is, that the instructions doesn't pair well,
>>>but it should be still considerably faster. Has anyone an idea what the problem
>>>with the code below is? Should I perhaps throw this function out and use a
>>>look-up-table?
>>>
>>>INLINE int Diag045Rot(const int iSqNr)
>>>{
>>>#if defined (Use_Asm)
>>>// 0 <= iSqNr <= 63
>>>__asm
>>>{
>>>  mov eax, iFeldNr;
>>>  mov ah, al;
>>>  and al, 007h;	//x (iFeldNr%8) --> al
>>>  shr ah, 3;	//y (iFeldNr/8) --> ah
>>>  sub al, ah;	//x-y --> al
>>>  mov ah, al;	//    --> ah
>>>  and ah, 080h;	//ah &= 0x80 (isolate sign bit)
>>>  add ah, 080h;	//ah += 0x80 (setting the carry bit)
>>>  adc ah, 0;	//ah += carry bit
>>>  shl ah, 3;	//ah <<= 3;
>>>  add al, ah;	//al += 8*(x-y < 0)
>>>  xor ah, ah;
>>>}
>>
>>maybe you could try something like this, which is shorter, and looks to be quite
>>pairable.  On the down side it uses 3 registers.
>>
>>Disclaimer: It compiles, but I haven't tested it to see if it produces the
>>correct answer :)
>>
>>__asm
>>{
>>	mov	eax, square
>>	mov	ebx, eax
>
>I think that:
>
>	mov	eax, square
>	mov	ebx, square
>
>is faster, because it can be paired but it might be processor dependent.

Yes, this way they can be paired, but iirc this needs to be followed by a third
instruction (not using eax or ebx ) to (be sure to) prevent a stall.

Tony

>
>Ed
>
>
>
>>	and	eax, 0x07	// eax = x
>>	shr	ebx, 3		// ebx = y
>>	mov	ecx, 0
>>	sub	eax, ebx	// eax = x - y
>>	rol	ecx, 4		// ecx = 8 * (x < y)
>>	add	eax, ecx	// eax = x - y + 8 * (x < y)
>>}
>>
>>
>>>#else
>>>  int x, y;
>>>  x = iSqNr%8;
>>>  y = iSqNr/8;
>>>  return x-y + 8*(x-y < 0);
>>>#endif
>>
>>It may help if you get your compiler to output the assembly code, or if you can
>>look at it in a debugger, to see what it does when compiling the above code.
>>
>>Cheers, Tim.



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.