Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: knight distance

Author: Gerd Isenberg

Date: 09:02:44 12/15/05

Go up one level in this thread


for better readability with inlined abs:

const int edge[64] = {
  2, 0, 0, 0, 0, 0, 0, 2,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  2, 0, 0, 0, 0, 0, 0, 2,
};
const int knightD[64] = {
  0, 3, 2, 3, 2, 3, 4, 5,
  3, 2, 1, 2, 3, 4, 3, 4,
  2, 1, 4, 3, 2, 3, 4, 5,
  3, 2, 3, 2, 3, 4, 3, 4,
  2, 3, 2, 3, 4, 3, 4, 5,
  3, 4, 3, 4, 3, 4, 5, 4,
  4, 3, 4, 3, 4, 5, 4, 5,
  5, 4, 5, 4, 5, 4, 5, 6,
};

__forceinline int abs(int x) {return ( x + (x>>31) ) ^ (x>>31);}

int knightDistance(int a, int b) {
  int c = abs((a&7)-(b&7)) + 8*abs((a>>3)-(b>>3));
  return ((edge[a]+edge[b]) & -(c == 9)) + knightD[c];
}

// msvc6 generated fastcall assembly
?knightDistance@@YIHHH@Z PROC NEAR			; knightDistance
  00000	53		 push	 ebx
  00001	56		 push	 esi
  00002	8b c1		 mov	 eax, ecx
  00004	83 e0 07	 and	 eax, 7
  00007	57		 push	 edi
  00008	8b f2		 mov	 esi, edx
  0000a	83 e6 07	 and	 esi, 7
  0000d	2b c6		 sub	 eax, esi
  0000f	8b f1		 mov	 esi, ecx
  00011	c1 fe 03	 sar	 esi, 3
  00014	8b fa		 mov	 edi, edx
  00016	c1 ff 03	 sar	 edi, 3
  00019	2b f7		 sub	 esi, edi
  0001b	8b d8		 mov	 ebx, eax
  0001d	8b fe		 mov	 edi, esi
  0001f	c1 ff 1f	 sar	 edi, 31			; 0000001fH
  00022	c1 fb 1f	 sar	 ebx, 31			; 0000001fH
  00025	03 c3		 add	 eax, ebx
  00027	03 f7		 add	 esi, edi
  00029	33 c3		 xor	 eax, ebx
  0002b	33 f7		 xor	 esi, edi
  0002d	8d 34 f0	 lea	 esi, DWORD PTR [eax+esi*8]
  00030	8b 04 8d 00 00
	00 00		 mov	 eax, DWORD PTR _edge[ecx*4]
  00037	03 04 95 00 00
	00 00		 add	 eax, DWORD PTR _edge[edx*4]
  0003e	33 c9		 xor	 ecx, ecx
  00040	83 fe 09	 cmp	 esi, 9
  00043	0f 94 c1	 sete	 cl
  00046	5f		 pop	 edi
  00047	f7 d9		 neg	 ecx
  00049	23 c1		 and	 eax, ecx
  0004b	8b 0c b5 00 00
	00 00		 mov	 ecx, DWORD PTR _knightD[esi*4]
  00052	5e		 pop	 esi
  00053	03 c1		 add	 eax, ecx
  00055	5b		 pop	 ebx
  00056	c3		 ret	 0
?knightDistance@@YIHHH@Z ENDP				; knightDistance



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.