speed of bitboard operations question

Discussion of chess software programming and technical issues.

Moderator: Ras

Gerd Isenberg
Posts: 2251
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: speed of bitboard operations question

Post by Gerd Isenberg »

Dann Corbit wrote: {General advice, not targeted at Dr. Hyatt, who already knows this well}:
The first rule of optimization is:
1. DON'T DO IT.
And the second rule (for experts only) is:
2. DON'T DO IT YET.

The general idea is that we should write code that is as clear and expressive as possible. Later on, when we want to make our program faster the right way to do it is to improve the algorithm. For instance, going from something that is O(N*N) to something that is O(N*log(N)) will create an enormous speedup and bit fiddling will never create anything more than a small linear factor.

So, when we have the code as clear and expressive as possible, we profile it. After we profile it, we speed up the hot spots. We can spend weeks making a slow routine faster only to discover that it is hardly ever called and so even though it is ten times faster than before the program shaves only one second off of a one hour run. A profile will tell you where to look to improve things.
We apply algebra not only for machine-, but also human understanding or "optimization" - nice if they are conform.

Code: Select all

1)  a*c + a*d + a*e
2)  a * (c + d + e)
May be a matter of taste - factoring things out is imho more human readable, since each identifier occurs only once - if the structure of the expression (like conjunctive or disjunctive normal form) becomes not too ugly with too many nested brackets without a clear pattern.

Recently I was quite surprised, that those expressions were identical:

Code: Select all

(1)  a &      ~b 
(2)  a & ( a ^ b )
(3)  a ^ ( a & b )
(4)  a - ( a & b )
(5)  b ^ ( a | b )
(6)  ( a | b ) - b
What is better readable for you, to get pawn-islands? You can interpret each on it's own. All sub-expressions have their own semantics.

Code: Select all

BYTE islandsEastFiles(BYTE f) {return f & (f ^ (f >> 1));}
BYTE islandsEastFiles(BYTE f) {return f ^ (f & (f >> 1));}
BYTE islandsEastFiles(BYTE f) {return f &     ~(f >> 1) ;}
Gerd
Uri Blass
Posts: 10792
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: speed of bitboard operations question

Post by Uri Blass »

You are right and I decided to modify my private version of strelka for the shorter code because the shorter code is easier to understand.

My private version of strelka is probably not shorter than the original code becasue I also added a lot of constants and got rid of many magic numbers but part of my steps do it shorter.

Uri