array or functions what is better?

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

array or functions what is better?

Postby Uri Blass » 29 Oct 2007, 15:03

strelka has a big array Distance[64][64] to calculate distance between squares

I have the following definition in movei to calculate the same thing

#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define FileDistance(a,b) abs(((a)&7) - ((b)&7))
#define RankDistance(a,b) abs(((a)>>3) - ((b)>>3))
#define Distance(a,b) Max(FileDistance(a,b),RankDistance(a,b))

I added this definition to strelka and found that Distance(i,j) in my code is always equal to Distance[i][j] in strelka for 0<=i,j<=63

My question is which code is better for speed.

Uri
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: array or functions what is better?

Postby Roman Hartmann » 29 Oct 2007, 16:39

Uri Blass wrote:strelka has a big array Distance[64][64] to calculate distance between squares

I have the following definition in movei to calculate the same thing

#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define FileDistance(a,b) abs(((a)&7) - ((b)&7))
#define RankDistance(a,b) abs(((a)>>3) - ((b)>>3))
#define Distance(a,b) Max(FileDistance(a,b),RankDistance(a,b))

I added this definition to strelka and found that Distance(i,j) in my code is always equal to Distance[i][j] in strelka for 0<=i,j<=63

My question is which code is better for speed.

Uri


Hi Uri,
I guess you need to run the program to see which version is faster. I too have such a distance array in my engine and what I noticed is that it seems to matter from where you access such an array.

While I had a tremendous speedup by using the distance array in my move generation when generating legal moves it didn't gave me a speedup when calling the array directly from the search when generating pseudo legal moves. It was not faster than calling attack() (to check if the move leads/leaves in check) after making every move.

So I guess you need to actually test to conclude which version is faster.

regards
Roman
User avatar
Roman Hartmann
 
Posts: 155
Joined: 11 Oct 2004, 14:21

Re: array or functions what is better?

Postby bob » 29 Oct 2007, 18:18

The problem I see with your macros is that you introduce several branches, and if you use that very often, the branches multiply. The commonly-used array elements will be cached and become almost free.

Too many interactions can influence the results. My advice is to test both ways. I _always_ do that if I want to make a decision on which is faster... it has to be faster in my program, not just faster on paper. Often things turn out differently than expected when tested.
User avatar
bob
 
Posts: 156
Joined: 10 May 2006, 17:59

Re: array or functions what is better?

Postby Dann Corbit » 31 Oct 2007, 01:00

bob wrote:The problem I see with your macros is that you introduce several branches, and if you use that very often, the branches multiply. The commonly-used array elements will be cached and become almost free.

Too many interactions can influence the results. My advice is to test both ways. I _always_ do that if I want to make a decision on which is faster... it has to be faster in my program, not just faster on paper. Often things turn out differently than expected when tested.


Interesting way to do branchless compare/move:
http://www.x86.org/secrets/opcodes/fcmov.htm

Of course, even if you benchmark both routines in your code, the answer may change when you run it on a different machine with a bigger cache or even change your compiler.
Dann Corbit
 

Re: array or functions what is better?

Postby Gerd Isenberg » 31 Oct 2007, 08:53

Uri Blass wrote:strelka has a big array Distance[64][64] to calculate distance between squares

I have the following definition in movei to calculate the same thing

#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define FileDistance(a,b) abs(((a)&7) - ((b)&7))
#define RankDistance(a,b) abs(((a)>>3) - ((b)>>3))
#define Distance(a,b) Max(FileDistance(a,b),RankDistance(a,b))

I added this definition to strelka and found that Distance(i,j) in my code is always equal to Distance[i][j] in strelka for 0<=i,j<=63

My question is which code is better for speed.

Uri

Likely the lookup is fastest here.

There are branchless ways to compute max and abs with a better chance to compute things in parallel but that takes registers. So it heavily depends whether the context does more register- or memory/cache stuff or whether your L1-cache are already heavily polluted.

Other option is to use 0x88 difference as index of much smaller table[240] and/or to make array of structs with similar stuff like taxiDistance to safe some cacheline here and there.
Code: Select all
0x88delta = sq2+(sq2&56)-sq1-(sq1&56)+120
See also the discussion on squares between two squares with similar issues:
http://wbforum.vpittlik.org/viewtopic.p ... 856&t=6573
Gerd Isenberg
 
Posts: 285
Joined: 31 Jan 2005, 20:31
Location: Hattingen, Germany


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 8 guests