lots
[arrocco.git] / position.c
blob53b8f0fe3a08ad1a01c0259e1e78c835e648ed97
1 /* position.c
2 28 Feb 2007 */
4 /*
5 This file is part of Arrocco, which is Copyright 2007 Thomas Plick
6 (tomplick 'at' gmail.com).
8 Arrocco is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 Arrocco is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdlib.h>
23 #include <string.h>
25 typedef unsigned long long int Int64;
26 const Int64 one = 1ULL;
28 typedef struct {
29 char board[64];
30 short castling[4];
31 short turn;
32 short epFile;
34 short value[2];
35 short branch[32];
36 int branchLength;
38 Int64 pieces[14];
40 Int64 white[4], black[4];
41 int isStale;
42 } Position;
44 int sizeOfPosition(){ return sizeof(Position); }
47 extern const Int64 twoToThe[64];
48 // #define TWOTOTHE(n) (twoToThe[n])
49 #define TWOTOTHE(n) (one << (n))
52 void newPosition(Position * pos){
53 memset(pos, 0, sizeof(Position));
54 pos->epFile = -1;
57 inline int getPieceAt(Position * pos, const int sq){
58 return pos->board[sq];
61 // These are defined in Python.
62 extern const Int64 rotMasks[64][8];
64 static inline void maskSetEmptyPiece_inline(Int64 * restrict white, const int sq){
65 int i;
66 const Int64 * restrict squareMasks = rotMasks[sq];
67 for (i = 0; i < 8; i++)
68 // *white++ &= *squareMasks++;
69 white[i] &= squareMasks[i];
72 void maskSetEmptyPiece(Int64 * const restrict white,
73 Int64 * const restrict black, const int sq){
74 maskSetEmptyPiece_inline(white, sq);
76 void maskSetWhitePiece(Int64 * const restrict white,
77 Int64 * const restrict black, const int sq){
78 int i;
79 const Int64 * restrict squareMasks = rotMasks[sq];
80 for (i = 0; i < 4; i++){
81 black[i] &= ~(white[i] |= ~squareMasks[i]);
84 void maskSetBlackPiece(Int64 * const restrict white,
85 Int64 * const restrict black, const int sq){
86 int i;
87 const Int64 * restrict squareMasks = rotMasks[sq];
88 for (i = 0; i < 4; i++){
89 white[i] &= ~(black[i] |= ~squareMasks[i]);
93 typedef void (*MaskFunction)(Int64 * const restrict white,
94 Int64 * const restrict black, const int sq);
95 MaskFunction maskFunctions[14] = {maskSetEmptyPiece, maskSetEmptyPiece,
96 maskSetWhitePiece, maskSetBlackPiece, maskSetWhitePiece, maskSetBlackPiece,
97 maskSetWhitePiece, maskSetBlackPiece, maskSetWhitePiece, maskSetBlackPiece,
98 maskSetWhitePiece, maskSetBlackPiece, maskSetWhitePiece, maskSetBlackPiece};
100 void setPieceAt(Position * const pos, const int sq, const int piece){
101 const Int64 mask = TWOTOTHE(sq);
102 pos->pieces[pos->board[sq]] ^= mask;
103 pos->pieces[piece] |= mask;
104 pos->board[sq] = piece;
106 maskFunctions[piece](pos->white, pos->black, sq);
109 void removePieceAt(Position * const pos, const int sq){
110 const Int64 mask = TWOTOTHE(sq);
111 pos->pieces[pos->board[sq]] ^= mask;
112 pos->pieces[pos->board[sq] = 0] |= mask;
114 maskSetEmptyPiece_inline(pos->white, sq);
117 void afterMove(const Position * restrict pos, const int sq1, const int sq2,
118 Position * restrict child){
119 memcpy(child, pos, sizeof(Position));
120 setPieceAt(child, sq2, pos->board[sq1]);
121 removePieceAt(child, sq1);
123 child->turn ^= 1;
126 void calculatePositionValue(Position * pos){
127 int sq;
128 pos->value[0] = 0;
129 for (sq = 0; sq < 64; sq++){
130 pos->value[0] += pieceValues[pos->board[sq]];
132 pos->value[1] = -(pos->value[0]);
135 int getTurn(const Position * const pos){ return pos->turn; }
136 void setTurn(Position * const pos, int t){ pos->turn = t; }
138 int getBranch(Position * const pos, int i){ return pos->branch[i]; }
140 int getValue(const Position * const pos){ return pos->value[0]; }
141 void setValue(Position * const pos, int v){ pos->value[0] = v; pos->value[1] = -v; }