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/>.
25 typedef unsigned long long int Int64
;
26 const Int64 one
= 1ULL;
40 Int64 white
[4], black
[4];
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
));
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
){
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
){
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
){
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
);
126 void calculatePositionValue(Position
* pos
){
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
; }