Subversion Repositories Games.Chess Giants

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
112 pmbaty 1
/*
2
    Protector -- a UCI chess engine
3
 
4
    Copyright (C) 2009-2010 Raimund Heid (Raimund_Heid@yahoo.com)
5
 
6
    This program is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
19
*/
20
 
21
#ifndef _bitboard_h_
22
#define _bitboard_h_
23
 
24
#include "protector.h"
25
#include <string.h>
26
 
27
typedef UINT64 Bitboard;
28
 
29
#define EMPTY_BITBOARD ULONG_ZERO
30
#define IMAX_ROOK    4096
31
#define IMAX_BISHOP  512
32
 
33
typedef struct
34
{
35
   int hLane, vLane, uLane, dLane;
36
   BYTE hLaneSetMask, vLaneSetMask, uLaneSetMask, dLaneSetMask;
37
   BYTE hLaneClearMask, vLaneClearMask, uLaneClearMask, dLaneClearMask;
38
}
39
SquareLaneInfo;
40
 
41
typedef struct
42
{
43
   int numSetSquares;
44
   UINT8 setSquares[16];
45
}
46
SetSquaresInfo;
47
 
48
typedef struct
49
{
50
   int hLaneNumber, vLaneNumber, uLaneNumber, dLaneNumber;
51
   Bitboard hLane[256], vLane[256], uLane[256], dLane[256];
52
}
53
ObstacleSquareInfo;
54
 
55
typedef struct
56
{
57
   Bitboard preMask, magicNumber;
58
   Bitboard moves[IMAX_ROOK];
59
}
60
MagicSquareInfoRook;
61
 
62
typedef struct
63
{
64
   Bitboard preMask, magicNumber;
65
   Bitboard moves[IMAX_BISHOP];
66
}
67
MagicSquareInfoBishop;
68
 
69
#define SLI(square) (squareLaneInfo[(square)])
70
 
71
extern UINT64 minValue[_64_];
72
extern UINT64 maxValue[_64_];
73
extern INT8 highestBit[0x10000];
74
extern INT8 lowestBit[0x10000];
75
extern Bitboard hlane[_64_][256];
76
extern Bitboard vlane[_64_][256];
77
extern Bitboard ulane[_64_][256];
78
extern Bitboard dlane[_64_][256];
79
extern ObstacleSquareInfo obsi[_64_];
80
extern Bitboard castlings[2][16][256];
81
extern int castlingLane[2];
82
extern int castlingsOfColor[2];
83
extern SquareLaneInfo squareLaneInfo[_64_];
84
extern Bitboard generalMoves[0x0f][_64_];
85
extern const Bitboard squaresOfFile[8];
86
extern const Bitboard squaresOfRank[8];
87
extern const Bitboard squaresOfLateralFiles[8];
88
extern Bitboard squaresOfFileRange[8][8];
89
extern Bitboard squaresOfRankRange[8][8];
90
extern Bitboard pawnMoves[2][_64_][256];
91
extern Bitboard promotionCandidates[2];
92
extern SetSquaresInfo setSquares[4][0x10000];
93
extern INT8 numSetBits[0x10000];
94
extern UINT8 rankOverlay[0x10000];
95
extern UINT8 bitshiftGap[8][256];
96
extern Bitboard squaresBehind[_64_][_64_];
97
extern Bitboard squaresBetween[_64_][_64_];
98
extern Bitboard squaresInDistance[8][_64_];
99
extern Bitboard squaresInTaxiDistance[15][_64_];
100
extern Bitboard squaresAbove[2][_64_];
101
extern Bitboard squaresBelow[2][_64_];
102
extern Bitboard squaresLeftOf[_64_];
103
extern Bitboard squaresRightOf[_64_];
104
extern Bitboard orthoKingAttackers[_64_];
105
extern Bitboard diaKingAttackers[_64_];
106
extern Bitboard knightKingAttackers[_64_];
107
extern Bitboard pawnKingAttackers[2][_64_];
108
extern Bitboard interestedPawns[2][_64_][256];
109
extern Bitboard nonA, nonH, border, center, lightSquares, darkSquares,
110
   queenSide, kingSide, centerFiles, extendedCenter;
111
extern int hLaneNumber[_64_], vLaneNumber[_64_];
112
extern int uLaneNumber[_64_], dLaneNumber[_64_];
113
extern Bitboard preMaskRook[64], preMaskBishop[64];
114
extern Bitboard magicRookMoves[64][IMAX_ROOK];
115
extern Bitboard magicBishopMoves[64][IMAX_BISHOP];
116
extern const Bitboard magicRookNumber[64];
117
extern const Bitboard magicBishopNumber[64];
118
extern MagicSquareInfoRook magicSquareInfoRook[64];
119
extern MagicSquareInfoBishop magicSquareInfoBishop[64];
120
 
121
#define setSquare(bitboard,square) ((bitboard) |= minValue[(square)]);
122
#define clearSquare(bitboard,square) ((bitboard) &= maxValue[(square)]);
123
#define excludeSquares(bitboard,toBeExcluded) ((bitboard) &= ~(toBeExcluded))
124
 
125
/**
126
 * The number of lanes used to hold information
127
 * about the state of all files, rows and diagonals
128
 */
129
#define NUM_LANES 46
130
 
131
#define UHEX_FFFF 0xFFFFllu
132
#define UHEX_FFFFffff00000000 0xFFFFffff00000000llu
133
#define UHEX_00000000FFFF0000 0x00000000FFFF0000llu
134
#define UHEX_FFFF000000000000 0xFFFF000000000000llu
135
#define UHEX_0000ffff00000000 0x0000ffff00000000llu
136
#define UHEX_00000000FFFFffff 0x00000000FFFFffffllu
137
#define UHEX_000000000000ffff 0x000000000000ffffllu
138
 
139
bool testSquare(const Bitboard bitboard, const Square square);
140
Bitboard getKingMoves(const Square square);
141
Bitboard getCastlingMoves(const Color color,
142
                          const BYTE castlingRights,
143
                          const Bitboard obstacles);
144
/*
145
 Bitboard getQueenMoves(const Square square, const BYTE * obstacles);
146
*/
147
int getWidth(const Bitboard set);
148
Bitboard getMagicQueenMoves(const Square square, const Bitboard obstacles);
149
Bitboard getRookMoves(const Square square, const BYTE * obstacles);
150
Bitboard getMagicRookMoves(const Square square, const Bitboard obstacles);
151
Bitboard getBishopMoves(const Square square, const BYTE * obstacles);
152
Bitboard getMagicBishopMoves(const Square square, const Bitboard obstacles);
153
Bitboard getKnightMoves(const Square square);
154
Bitboard getPawnCaptures(const Piece piece, const Square square,
155
                         const Bitboard allPieces);
156
Bitboard getPawnAdvances(const Color color, const Square square,
157
                         const Bitboard obstacles);
158
Bitboard getInterestedPawns(const Color color,
159
                            const Square square, const Bitboard obstacles);
160
Bitboard getSquaresBetween(const Square square1, const Square square2);
161
Bitboard getSquaresBehind(const Square target, const Square viewpoint);
162
Bitboard shiftLeft(const Bitboard bitboard);
163
Bitboard shiftRight(const Bitboard bitboard);
164
Bitboard getLateralSquares(const Bitboard squares);
165
Bitboard getSquaresOfFile(const File file);
166
Bitboard getSquaresOfRank(const Rank rank);
167
int getNumberOfSetSquares(const Bitboard bitboard);
168
int getRankOverlay(const Bitboard bitboard);
169
Bitboard getMoves(Square square, Piece piece, Bitboard allPieces);
170
Bitboard getCaptureMoves(Square square, Piece piece, Bitboard allPieces);
171
void setObstacleSquare(Square square, BYTE obstacles[NUM_LANES]);
172
void clearObstacleSquare(Square square, BYTE obstacles[NUM_LANES]);
173
void calculateObstacles(Bitboard board, BYTE obstacles[NUM_LANES]);
174
Square getLastSquare(Bitboard * vector);
175
void floodBoard(Bitboard * board);
176
Bitboard getWhitePawnTargets(const Bitboard whitePawns);
177
Bitboard getBlackPawnTargets(const Bitboard blackPawns);
178
void floodBoard(Bitboard * board);
179
Square getLastSquare(Bitboard * vector);
180
Square getFirstSquare(Bitboard * vector);
181
int getSetSquares(const Bitboard board, UINT8 squares[_64_]);
182
Bitboard getMultipleSquaresBetween(const Square origin, Bitboard targets);
183
 
184
#define ITERATE_BITBOARD(b,sq) while ( ( sq = getLastSquare(b) ) >= 0 )
185
 
186
unsigned int getMinimumDistance(const Bitboard targets, const Square square);
187
unsigned int getMaximumDistance(const Bitboard targets, const Square square);
188
int getFloodValue(const Square origin, const Bitboard targets,
189
                  const Bitboard permittedSquares);
190
Bitboard getFlippedBitboard(Bitboard original);
191
 
192
/**
193
 * Initialize this module.
194
 *
195
 * @return 0 if no errors occurred.
196
 */
197
int initializeModuleBitboard(void);
198
 
199
/**
200
 * Test this module.
201
 *
202
 * @return 0 if all tests succeed.
203
 */
204
int testModuleBitboard(void);
205
 
206
#endif