11
11
*
12
12
* You should have received a copy of the GNU General Public License along with this program. If
13
13
* not, see <http://www.gnu.org/licenses/>.
14
- */
15
- #include <stdio.h>
14
+ */
16
15
#include "bitboard.h"
16
+ #include <stdio.h>
17
17
18
18
bitboard_t Rank [NB_RANK ], File [NB_FILE ];
19
19
bitboard_t PawnAttacks [NB_COLOR ][NB_SQUARE ], KnightAttacks [NB_SQUARE ], KingAttacks [NB_SQUARE ];
20
20
bitboard_t Segment [NB_SQUARE ][NB_SQUARE ], Ray [NB_SQUARE ][NB_SQUARE ];
21
21
22
- static void safe_set_bit (bitboard_t * b , int rank , int file )
23
- {
22
+ static void safe_set_bit (bitboard_t * b , int rank , int file ) {
24
23
if (0 <= rank && rank < NB_RANK && 0 <= file && file < NB_FILE )
25
24
bb_set (b , square_from (rank , file ));
26
25
}
27
26
28
27
static const bitboard_t RookMagic [NB_SQUARE ] = {
29
28
0x808000645080c000 , 0x208020001480c000 , 0x4180100160008048 , 0x8180100018001680 ,
30
29
0x4200082010040201 , 0x8300220400010008 , 0x3100120000890004 , 0x4080004500012180 ,
31
- 0x1548000a1804008 , 0x4881004005208900 , 0x480802000801008 , 0x2e8808010008800 ,
32
- 0x8cd804800240080 , 0x8a058002008c0080 , 0x514000c480a1001 , 0x101000282004d00 ,
30
+ 0x1548000a1804008 , 0x4881004005208900 , 0x480802000801008 , 0x2e8808010008800 ,
31
+ 0x8cd804800240080 , 0x8a058002008c0080 , 0x514000c480a1001 , 0x101000282004d00 ,
33
32
0x2048848000204000 , 0x3020088020804000 , 0x4806020020841240 , 0x6080420008102202 ,
34
- 0x10050011000800 , 0xac00808004000200 , 0x10100020004 , 0x1500020004004581 ,
35
- 0x4c00180052080 , 0x220028480254000 , 0x2101200580100080 , 0x407201200084200 ,
36
- 0x18004900100500 , 0x100200020008e410 , 0x81020400100811 , 0x12200024494 ,
37
- 0x8006c002808006a5 , 0x4201000404000 , 0x5402202001180 , 0x81001002100 ,
38
- 0x100801000500 , 0x4000020080800400 , 0x4005050214001008 , 0x810100118b000042 ,
39
- 0xd01020040820020 , 0x140a010014000 , 0x420001500210040 , 0x54210010030009 ,
40
- 0x4000408008080 , 0x2000400090100 , 0x840200010100 , 0x233442820004 ,
41
- 0x800a42002b008200 , 0x240200040009080 , 0x242001020408200 , 0x4000801000480480 ,
42
- 0x2288008044000880 , 0xa800400020180 , 0x30011002880c00 , 0x41110880440200 ,
43
- 0x2001100442082 , 0x1a0104002208101 , 0x80882014010200a , 0x100100600409 ,
44
- 0x2011048204402 , 0x12000168041002 , 0x80100008a000421 , 0x240022044031182
45
- };
33
+ 0x10050011000800 , 0xac00808004000200 , 0x10100020004 , 0x1500020004004581 ,
34
+ 0x4c00180052080 , 0x220028480254000 , 0x2101200580100080 , 0x407201200084200 ,
35
+ 0x18004900100500 , 0x100200020008e410 , 0x81020400100811 , 0x12200024494 ,
36
+ 0x8006c002808006a5 , 0x4201000404000 , 0x5402202001180 , 0x81001002100 ,
37
+ 0x100801000500 , 0x4000020080800400 , 0x4005050214001008 , 0x810100118b000042 ,
38
+ 0xd01020040820020 , 0x140a010014000 , 0x420001500210040 , 0x54210010030009 ,
39
+ 0x4000408008080 , 0x2000400090100 , 0x840200010100 , 0x233442820004 ,
40
+ 0x800a42002b008200 , 0x240200040009080 , 0x242001020408200 , 0x4000801000480480 ,
41
+ 0x2288008044000880 , 0xa800400020180 , 0x30011002880c00 , 0x41110880440200 ,
42
+ 0x2001100442082 , 0x1a0104002208101 , 0x80882014010200a , 0x100100600409 ,
43
+ 0x2011048204402 , 0x12000168041002 , 0x80100008a000421 , 0x240022044031182 };
46
44
47
45
static const bitboard_t BishopMagic [NB_SQUARE ] = {
48
- 0x88b030028800d040 , 0x18242044c008010 , 0x10008200440000 , 0x4311040888800a00 ,
49
- 0x1910400000410a , 0x2444240440000000 , 0xcd2080108090008 , 0x2048242410041004 ,
50
- 0x8884441064080180 , 0x42131420a0240 , 0x28882800408400 , 0x204384040b820200 ,
51
- 0x402040420800020 , 0x20910282304 , 0x96004b10082200 , 0x4000a44218410802 ,
52
- 0x808034002081241 , 0x101805210e1408 , 0x9020400208010220 , 0x820050c010044 ,
53
- 0x24005480a00000 , 0x200200900890 , 0x808040049c100808 , 0x9020202200820802 ,
54
- 0x410282124200400 , 0x90106008010110 , 0x8001100501004201 , 0x104080004030c10 ,
55
- 0x80840040802008 , 0x2008008102406000 , 0x2000888004040460 , 0xd0421242410410 ,
56
- 0x8410100401280800 , 0x801012000108428 , 0x402080300b04 , 0xc20020080480080 ,
46
+ 0x88b030028800d040 , 0x18242044c008010 , 0x10008200440000 , 0x4311040888800a00 ,
47
+ 0x1910400000410a , 0x2444240440000000 , 0xcd2080108090008 , 0x2048242410041004 ,
48
+ 0x8884441064080180 , 0x42131420a0240 , 0x28882800408400 , 0x204384040b820200 ,
49
+ 0x402040420800020 , 0x20910282304 , 0x96004b10082200 , 0x4000a44218410802 ,
50
+ 0x808034002081241 , 0x101805210e1408 , 0x9020400208010220 , 0x820050c010044 ,
51
+ 0x24005480a00000 , 0x200200900890 , 0x808040049c100808 , 0x9020202200820802 ,
52
+ 0x410282124200400 , 0x90106008010110 , 0x8001100501004201 , 0x104080004030c10 ,
53
+ 0x80840040802008 , 0x2008008102406000 , 0x2000888004040460 , 0xd0421242410410 ,
54
+ 0x8410100401280800 , 0x801012000108428 , 0x402080300b04 , 0xc20020080480080 ,
57
55
0x40100e0201502008 , 0x4014208200448800 , 0x4050020607084501 , 0x1002820180020288 ,
58
- 0x800610040540a0c0 , 0x301009014081004 , 0x2200610040502800 , 0x300442011002800 ,
59
- 0x1022009002208 , 0x110011000202100 , 0x1464082204080240 , 0x21310205800200 ,
60
- 0x814020210040109 , 0xc102008208c200a0 , 0xc100702128080000 , 0x1044205040000 ,
61
- 0x1041002020000 , 0x4200040408021000 , 0x4004040c494000 , 0x2010108900408080 ,
62
- 0x820801040284 , 0x800004118111000 , 0x203040201108800 , 0x2504040804208803 ,
63
- 0x228000908030400 , 0x10402082020200 , 0xa0402208010100 , 0x30c0214202044104
64
- };
56
+ 0x800610040540a0c0 , 0x301009014081004 , 0x2200610040502800 , 0x300442011002800 ,
57
+ 0x1022009002208 , 0x110011000202100 , 0x1464082204080240 , 0x21310205800200 ,
58
+ 0x814020210040109 , 0xc102008208c200a0 , 0xc100702128080000 , 0x1044205040000 ,
59
+ 0x1041002020000 , 0x4200040408021000 , 0x4004040c494000 , 0x2010108900408080 ,
60
+ 0x820801040284 , 0x800004118111000 , 0x203040201108800 , 0x2504040804208803 ,
61
+ 0x228000908030400 , 0x10402082020200 , 0xa0402208010100 , 0x30c0214202044104 };
65
62
66
63
static bitboard_t RookDB [0x19000 ], BishopDB [0x1480 ];
67
64
static bitboard_t * BishopAttacks [NB_SQUARE ], * RookAttacks [NB_SQUARE ];
@@ -71,8 +68,7 @@ static unsigned BishopShift[NB_SQUARE], RookShift[NB_SQUARE];
71
68
72
69
// Compute (from scratch) the squares attacked by a sliding piece, moving in directions dir, given
73
70
// board occupancy occ.
74
- static bitboard_t slider_attacks (int square , bitboard_t occ , const int dir [4 ][2 ])
75
- {
71
+ static bitboard_t slider_attacks (int square , bitboard_t occ , const int dir [4 ][2 ]) {
76
72
bitboard_t result = 0 ;
77
73
78
74
for (int i = 0 ; i < 4 ; i ++ ) {
@@ -94,17 +90,15 @@ static bitboard_t slider_attacks(int square, bitboard_t occ, const int dir[4][2]
94
90
return result ;
95
91
}
96
92
97
- static unsigned slider_index (bitboard_t occ , bitboard_t mask , bitboard_t magic , unsigned shift )
98
- {
93
+ static unsigned slider_index (bitboard_t occ , bitboard_t mask , bitboard_t magic , unsigned shift ) {
99
94
return (unsigned )(((occ & mask ) * magic ) >> shift );
100
95
}
101
96
102
97
static void init_slider_attacks (int square , bitboard_t mask [NB_SQUARE ],
103
- const bitboard_t magic [NB_SQUARE ], unsigned shift [NB_SQUARE ], bitboard_t * attacksPtr [NB_SQUARE ],
104
- const int dir [4 ][2 ])
105
- {
98
+ const bitboard_t magic [NB_SQUARE ], unsigned shift [NB_SQUARE ],
99
+ bitboard_t * attacksPtr [NB_SQUARE ], const int dir [4 ][2 ]) {
106
100
const bitboard_t edges = ((Rank [RANK_1 ] | Rank [RANK_8 ]) & ~Rank [rank_of (square )]) |
107
- ((File [RANK_1 ] | File [RANK_8 ]) & ~File [file_of (square )]);
101
+ ((File [RANK_1 ] | File [RANK_8 ]) & ~File [file_of (square )]);
108
102
mask [square ] = slider_attacks (square , 0 , dir ) & ~edges ;
109
103
shift [square ] = (unsigned )(64 - bb_count (mask [square ]));
110
104
@@ -115,19 +109,20 @@ static void init_slider_attacks(int square, bitboard_t mask[NB_SQUARE],
115
109
bitboard_t occ = 0 ;
116
110
117
111
do {
118
- attacksPtr [square ][slider_index (occ , mask [square ], magic [square ], shift [square ])]
119
- = slider_attacks (square , occ , dir );
120
- occ = (occ - mask [square ]) & mask [square ]; // Carry-Rippler trick
112
+ attacksPtr [square ][slider_index (occ , mask [square ], magic [square ], shift [square ])] =
113
+ slider_attacks (square , occ , dir );
114
+ occ = (occ - mask [square ]) & mask [square ]; // Carry-Rippler trick
121
115
} while (occ );
122
116
}
123
117
124
- static __attribute__((constructor )) void bb_init (void )
125
- {
126
- static const int PawnDir [2 ][2 ] = {{1 ,-1 }, {1 ,1 }};
127
- static const int KnightDir [8 ][2 ] = {{-2 ,-1 }, {-2 ,1 }, {-1 ,-2 }, {-1 ,2 }, {1 ,-2 }, {1 ,2 }, {2 ,-1 }, {2 ,1 }};
128
- static const int KingDir [8 ][2 ] = {{-1 ,-1 }, {-1 ,0 }, {-1 ,1 }, {0 ,-1 }, {0 ,1 }, {1 ,-1 }, {1 ,0 }, {1 ,1 }};
129
- static const int BishopDir [4 ][2 ] = {{-1 ,-1 }, {-1 ,1 }, {1 ,-1 }, {1 ,1 }};
130
- static const int RookDir [4 ][2 ] = {{-1 ,0 }, {0 ,-1 }, {0 ,1 }, {1 ,0 }};
118
+ static __attribute__((constructor )) void bb_init (void ) {
119
+ static const int PawnDir [2 ][2 ] = {{1 , -1 }, {1 , 1 }};
120
+ static const int KnightDir [8 ][2 ] = {{-2 , -1 }, {-2 , 1 }, {-1 , -2 }, {-1 , 2 },
121
+ {1 , -2 }, {1 , 2 }, {2 , -1 }, {2 , 1 }};
122
+ static const int KingDir [8 ][2 ] = {{-1 , -1 }, {-1 , 0 }, {-1 , 1 }, {0 , -1 },
123
+ {0 , 1 }, {1 , -1 }, {1 , 0 }, {1 , 1 }};
124
+ static const int BishopDir [4 ][2 ] = {{-1 , -1 }, {-1 , 1 }, {1 , -1 }, {1 , 1 }};
125
+ static const int RookDir [4 ][2 ] = {{-1 , 0 }, {0 , -1 }, {0 , 1 }, {1 , 0 }};
131
126
132
127
// Initialise Rank[] and File[]
133
128
for (int i = 0 ; i < 8 ; i ++ ) {
@@ -181,140 +176,110 @@ static __attribute__((constructor)) void bb_init(void)
181
176
}
182
177
}
183
178
184
- int opposite (int color )
185
- {
179
+ int opposite (int color ) {
186
180
BOUNDS (color , NB_COLOR );
187
- return color ^ BLACK ; // branchless for: color == WHITE ? BLACK : WHITE
181
+ return color ^ BLACK ; // branchless for: color == WHITE ? BLACK : WHITE
188
182
}
189
183
190
- int push_inc (int color )
191
- {
184
+ int push_inc (int color ) {
192
185
BOUNDS (color , NB_COLOR );
193
- return UP - color * (UP - DOWN ); // branchless for: color == WHITE ? UP : DOWN
186
+ return UP - color * (UP - DOWN ); // branchless for: color == WHITE ? UP : DOWN
194
187
}
195
188
196
- int square_from (int rank , int file )
197
- {
189
+ int square_from (int rank , int file ) {
198
190
BOUNDS (rank , NB_RANK );
199
191
BOUNDS (file , NB_FILE );
200
192
return NB_FILE * rank + file ;
201
193
}
202
194
203
- int rank_of (int square )
204
- {
195
+ int rank_of (int square ) {
205
196
BOUNDS (square , NB_SQUARE );
206
197
return square / NB_FILE ;
207
198
}
208
199
209
- int file_of (int square )
210
- {
200
+ int file_of (int square ) {
211
201
BOUNDS (square , NB_SQUARE );
212
202
return square % NB_FILE ;
213
203
}
214
204
215
- int relative_rank (int color , int rank )
216
- {
205
+ int relative_rank (int color , int rank ) {
217
206
BOUNDS (color , NB_COLOR );
218
207
BOUNDS (rank , NB_RANK );
219
- return rank ^ (RANK_8 * color ); // branchless for: color == WHITE ? rank : RANK_8 - rank
208
+ return rank ^ (RANK_8 * color ); // branchless for: color == WHITE ? rank : RANK_8 - rank
220
209
}
221
210
222
- move_t move_build (int from , int to , int prom )
223
- {
211
+ move_t move_build (int from , int to , int prom ) {
224
212
BOUNDS (from , NB_SQUARE );
225
213
BOUNDS (to , NB_SQUARE );
226
214
assert ((unsigned )prom <= QUEEN || prom == NB_PIECE );
227
215
return (move_t )(from | (to << 6 ) | (prom << 12 ));
228
216
}
229
217
230
- int move_from (move_t m )
231
- {
232
- return m & 077 ;
233
- }
218
+ int move_from (move_t m ) { return m & 077 ; }
234
219
235
- int move_to (move_t m )
236
- {
237
- return (m >> 6 ) & 077 ;
238
- }
220
+ int move_to (move_t m ) { return (m >> 6 ) & 077 ; }
239
221
240
- int move_prom (move_t m )
241
- {
222
+ int move_prom (move_t m ) {
242
223
const unsigned prom = m >> 12 ;
243
224
assert (prom <= QUEEN || prom == NB_PIECE );
244
225
return (int )prom ;
245
226
}
246
227
247
- bitboard_t bb_bishop_attacks (int square , bitboard_t occ )
248
- {
228
+ bitboard_t bb_bishop_attacks (int square , bitboard_t occ ) {
249
229
BOUNDS (square , NB_SQUARE );
250
230
return BishopAttacks [square ][slider_index (occ , BishopMask [square ], BishopMagic [square ],
251
- BishopShift [square ])];
231
+ BishopShift [square ])];
252
232
}
253
233
254
- bitboard_t bb_rook_attacks (int square , bitboard_t occ )
255
- {
234
+ bitboard_t bb_rook_attacks (int square , bitboard_t occ ) {
256
235
BOUNDS (square , NB_SQUARE );
257
- return RookAttacks [square ][ slider_index ( occ , RookMask [ square ], RookMagic [ square ],
258
- RookShift [square ])];
236
+ return RookAttacks [square ]
237
+ [ slider_index ( occ , RookMask [ square ], RookMagic [ square ], RookShift [square ])];
259
238
}
260
239
261
- bool bb_test (bitboard_t b , int square )
262
- {
240
+ bool bb_test (bitboard_t b , int square ) {
263
241
BOUNDS (square , NB_SQUARE );
264
242
return b & (1ULL << square );
265
243
}
266
244
267
- void bb_clear (bitboard_t * b , int square )
268
- {
245
+ void bb_clear (bitboard_t * b , int square ) {
269
246
BOUNDS (square , NB_SQUARE );
270
247
assert (bb_test (* b , square ));
271
248
* b ^= 1ULL << square ;
272
249
}
273
250
274
- void bb_set (bitboard_t * b , int square )
275
- {
251
+ void bb_set (bitboard_t * b , int square ) {
276
252
BOUNDS (square , NB_SQUARE );
277
253
assert (!bb_test (* b , square ));
278
254
* b ^= 1ULL << square ;
279
255
}
280
256
281
- bitboard_t bb_shift (bitboard_t b , int i )
282
- {
283
- assert (-63 <= i && i <= 63 ); // oversized shift is undefined
257
+ bitboard_t bb_shift (bitboard_t b , int i ) {
258
+ assert (-63 <= i && i <= 63 ); // oversized shift is undefined
284
259
return i > 0 ? b << i : b >> - i ;
285
260
}
286
261
287
- int bb_lsb (bitboard_t b )
288
- {
289
- assert (b ); // lsb(0) is undefined
262
+ int bb_lsb (bitboard_t b ) {
263
+ assert (b ); // lsb(0) is undefined
290
264
return __builtin_ctzll (b );
291
265
}
292
266
293
- int bb_msb (bitboard_t b )
294
- {
295
- assert (b ); // msb(0) is undefined
267
+ int bb_msb (bitboard_t b ) {
268
+ assert (b ); // msb(0) is undefined
296
269
return 63 - __builtin_clzll (b );
297
270
}
298
271
299
- int bb_pop_lsb (bitboard_t * b )
300
- {
272
+ int bb_pop_lsb (bitboard_t * b ) {
301
273
const int square = bb_lsb (* b );
302
274
* b &= * b - 1 ;
303
275
return square ;
304
276
}
305
277
306
- bool bb_several (bitboard_t b )
307
- {
308
- return b & (b - 1 );
309
- }
278
+ bool bb_several (bitboard_t b ) { return b & (b - 1 ); }
310
279
311
- int bb_count (bitboard_t b )
312
- {
313
- return __builtin_popcountll (b );
314
- }
280
+ int bb_count (bitboard_t b ) { return __builtin_popcountll (b ); }
315
281
316
- void bb_print (bitboard_t b )
317
- {
282
+ void bb_print (bitboard_t b ) {
318
283
for (int rank = RANK_8 ; rank >= RANK_1 ; rank -- ) {
319
284
char line [] = ". . . . . . . ." ;
320
285
0 commit comments