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: 01:32:39 04/17/03

Go up one level in this thread


On April 16, 2003 at 22:08:54, Uri Blass wrote:

>I guess that I will first add updating some varaibles and in this case I am
>going to have 2 functions to update varaibles (one for white to move and one for
>black to move).
>
>I think that 2 functions is more readable than one big function that have a lot
>of side and 1^side.
>
>Maybe I should try also to save memory by using less memory in functions that I
>call only one time.
>
>Here is one example for a function that I call only in setup position.
>You can see that I have a similiar pattern in case that the piece is a
>pawn,knight,bishop,rook and queen and the question is if I should try to use
>pointers to do it shorter or maybe I can trust the compiler to detect that
>pattern and to do that job.
>
>static void addp(int square)
>{
>	pseudoadd(square);
>	switch (piece(square))
>	{
>	case PAWN:
>		numpiece[square]=numpawns[color(square)];
>		pawns[numpawns[color(square)]][color(square)]=square;
>		numpiece[square]=numpawns[color(square)];
>		numpawns[color(square)]+=1;
>		break;
>	case KNIGHT:
>		numpiece[square]=numknights[color(square)];
>		knights[numknights[color(square)]][color(square)]=square;
>		numpiece[square]=numknights[color(square)];
>		numknights[color(square)]+=1;
>		break;
>	case BISHOP:
>		numpiece[square]=numbishops[color(square)];
>		bishops[numbishops[color(square)]][color(square)]=square;
>		numpiece[square]=numbishops[color(square)];
>		numbishops[color(square)]+=1;
>		break;
>	case ROOK:
>		numpiece[square]=numrooks[color(square)];
>		rooks[numrooks[color(square)]][color(square)]=square;
>		numpiece[square]=numrooks[color(square)];
>		numrooks[color(square)]+=1;
>		break;
>	case QUEEN:
>		numpiece[square]=numqueens[color(square)];
>		queens[numqueens[color(square)]][color(square)]=square;
>		numpiece[square]=numqueens[color(square)];
>		numqueens[color(square)]+=1;
>		break;
>	case KING:
>		kingsquare[color(square)]=square;
>		break;
>	}
>}
>
>Uri

Don't waste any energy optimizing on a setup function.

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)

counter is 64 bits holding 32 bits for each color and 4 bits per piece (maximum
is 10 rooks/knights/bishops)

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))))

Maybe this is not faster in general, but sometimes you get to replace a large
switch with something much more compact :)

Unfortunately my pawns have value 1 instead of 0, a blunder I made in the dawn
of time...

-S.



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.