Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Is it good to give a function a struct of 4 chars or maybe it is

Author: Sune Fischer

Date: 08:36:15 04/17/03

Go up one level in this thread


On April 17, 2003 at 08:33:19, Uri Blass wrote:
[snip]
>>But I can perhaps give you a suggestion about those numxxxx[] arrays, something
>>which I use anyway.
>>You can store it all in one unsigned int, use macros to extract what you need.
>>The idea is that you don't have to do a lot of conditionals like
>>if (piece==QUEEN) x=numqueens[sq], or switch or pointers or whatever,
>>instead you replace all that "expensive" stuff with a few shifts:
>>x=NUM(piece,color).
>>
>>Where NUM is a macro to mask out the number, eg.
>>#define NUM(p,c) ((counter>>(32*(c)+4*(p))&15)
>
>Thanks for the advice.
>
>
>I have in my move generator many loops like
>
>for (i=0;i<numpawns[LIGHT];i++)
>				generatewhitepawnsimple(pawns[i][LIGHT]);
>
>I guess that you suggest to change it to for (i=0;i<NUMWHITE(PAWN);i++) when I
>have a global varaible numwhitepieceinfo and I have
>
>#define NUMWHITE(p) ((numwhitepieceinfo>>4*(p))&15)
>#define NUMBLACK(p) ((numwhitepieceinfo>>4*(p))&15)

You probably need () around 4*(p) or it won't know whether to shift or multiply
first.

>Maybe it can help me to do my perft faster and I do not know but I prefer to
>hear advices from other people before trying it.
>The question is if >> and & is not slower than array.
>
>If it is not clear thing then I guess that I will not change it because
>It is better if I spend more time on improvement of evaluation and search and
>not on testing speed options when it is not clear and I only want not to avoid
>having something that is obviously slower.

It might be a big change, and for sure it's nothing major it will bring you, but
there are a few neat tricks you can do when you have all the material
information packed uniquely in a key.

E.g. if you want to check for mating material it can be done pretty fast,
because you can check for pawns, rooks and queens all at the same time by simply
AND'ing the counter with the proper mask. E.g.
#define HAS_BRQ(c) ((counter>>(32(c))&0x000FFF00)

More over, you have bits left over in the counter, which can be used for other
things, like a counter for the total number of pieces. Nice to know this because
it makes it faster to find the number of non-pawns. For this it might be better
to use a (very) small array to hold the numbers, then you can increment both
counters at the same time by adding say 0x01000010. Note you don't have to keep
track of the kings since they are always there, that means you can have no more
than 15 pieces left, 4 bits precisely enough for that - perfect fit :)

Finally with a material mask like that, you can check for specific material
configurations by calculating the right key. Say you want to know if white has
exactly 1 rook and 3 pawns, you do: if (((counter&0xFFFFFFFF)^0x00001003)==0).
Not something you might need very often, but ...

Arasan has a class built on this trick.

>Note that pawns[i][LIGHT] is a sqaure in the board so it may be also possible
>to use my 64 bitboard number for white pawns but I do not know if using bitboard
>and firstone is faster or slower than using arrays.
>
>For calculating perft it may be slower because for calculating perft I do not
>use that bitboard and calculation of the bitboard after every pawn move or pawn
>capture may be expensive but for my program I need the bitboard for evaluating
>pawn structure so I do not need to calculate it again.
>
>For other pieces I do not have bitboards. so I need to decide if to change my
>program
>
><snipped>
>>Of course you can hardwire a few special macros suited to specific pieces or
>>color. Also needed are increment and decrement macros, e.g
>>
>>#define INC(p,c) (counter + (1<<(32*(c)+4*(p))))
>
>I do not use macros for this purpose but
>again I may consider to have 2 macros for increasing white and increasing black.

Just use macros to hide the ugly stuff, I don't think myarray[i]++ should be
written as a macro. I use classes myself and have replaced these macros by
inlined functions, but that was not important for the idea. :)




>Uri



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.