3 Copyright (c) Scott Gasch
11 Routines dealing with bitboards. If you build with the
12 testbitboard.c code enabled it will give you a rough benchmark of
13 these routines' speeds. Here are some typical results (from a
16 SLOWCOOR_TO_BB: 10 cycles/op
17 COOR_TO_BB: 5 cycles/op
18 SlowCountBits: 82 cycles/op
19 CountBits: 2 cycles/op
20 SlowLastBit: 158 cycles/op
22 SlowFirstBit: 22 cycles/op
25 As you can see the inline assembly language code is quite a bit
26 faster so use it if you can. And if, on the off chance, you are
27 porting this code to another hardware platform, it might be worth
28 your time to research what your instruction set has in the way of
29 bit twiddling opcodes.
37 $Id: bitboard.c 345 2007-12-02 22:56:42Z scott $
45 // Mapping from square -> bit
47 BITBOARD BBSQUARE[64] =
81 //--------------------------------------------------
86 0x1000000000ULL, // 37
87 0x2000000000ULL, // 38
88 0x4000000000ULL, // 39
89 0x8000000000ULL, // 40
90 0x10000000000ULL, // 41
91 0x20000000000ULL, // 42
92 0x40000000000ULL, // 43
93 0x80000000000ULL, // 44
94 0x100000000000ULL, // 45
95 0x200000000000ULL, // 46
96 0x400000000000ULL, // 47
97 0x800000000000ULL, // 48
98 0x1000000000000ULL, // 49
99 0x2000000000000ULL, // 50
100 0x4000000000000ULL, // 51
101 0x8000000000000ULL, // 52
102 0x10000000000000ULL, // 53
103 0x20000000000000ULL, // 54
104 0x40000000000000ULL, // 55
105 0x80000000000000ULL, // 56
106 0x100000000000000ULL, // 57
107 0x200000000000000ULL, // 58
108 0x400000000000000ULL, // 59
109 0x800000000000000ULL, // 60
110 0x1000000000000000ULL, // 61
111 0x2000000000000000ULL, // 62
112 0x4000000000000000ULL, // 63
113 0x8000000000000000ULL, // 64
119 // The white colored squares on the board
121 BITBOARD BBWHITESQ = (
122 SLOWCOOR_TO_BB(A2) | SLOWCOOR_TO_BB(A4) | SLOWCOOR_TO_BB(A6) |
123 SLOWCOOR_TO_BB(A8) | SLOWCOOR_TO_BB(B1) | SLOWCOOR_TO_BB(B3) |
124 SLOWCOOR_TO_BB(B5) | SLOWCOOR_TO_BB(B7) | SLOWCOOR_TO_BB(C2) |
125 SLOWCOOR_TO_BB(C4) | SLOWCOOR_TO_BB(C6) | SLOWCOOR_TO_BB(C8) |
126 SLOWCOOR_TO_BB(D1) | SLOWCOOR_TO_BB(D3) | SLOWCOOR_TO_BB(D5) |
127 SLOWCOOR_TO_BB(D7) | SLOWCOOR_TO_BB(E2) | SLOWCOOR_TO_BB(E4) |
128 SLOWCOOR_TO_BB(E6) | SLOWCOOR_TO_BB(E8) | SLOWCOOR_TO_BB(F1) |
129 SLOWCOOR_TO_BB(F3) | SLOWCOOR_TO_BB(F5) | SLOWCOOR_TO_BB(F7) |
130 SLOWCOOR_TO_BB(G2) | SLOWCOOR_TO_BB(G4) | SLOWCOOR_TO_BB(G6) |
131 SLOWCOOR_TO_BB(G8) | SLOWCOOR_TO_BB(H1) | SLOWCOOR_TO_BB(H3) |
132 SLOWCOOR_TO_BB(H5) | SLOWCOOR_TO_BB(H7)
136 // The black colored squares on the board
138 BITBOARD BBBLACKSQ = ~(
139 SLOWCOOR_TO_BB(A2) | SLOWCOOR_TO_BB(A4) | SLOWCOOR_TO_BB(A6) |
140 SLOWCOOR_TO_BB(A8) | SLOWCOOR_TO_BB(B1) | SLOWCOOR_TO_BB(B3) |
141 SLOWCOOR_TO_BB(B5) | SLOWCOOR_TO_BB(B7) | SLOWCOOR_TO_BB(C2) |
142 SLOWCOOR_TO_BB(C4) | SLOWCOOR_TO_BB(C6) | SLOWCOOR_TO_BB(C8) |
143 SLOWCOOR_TO_BB(D1) | SLOWCOOR_TO_BB(D3) | SLOWCOOR_TO_BB(D5) |
144 SLOWCOOR_TO_BB(D7) | SLOWCOOR_TO_BB(E2) | SLOWCOOR_TO_BB(E4) |
145 SLOWCOOR_TO_BB(E6) | SLOWCOOR_TO_BB(E8) | SLOWCOOR_TO_BB(F1) |
146 SLOWCOOR_TO_BB(F3) | SLOWCOOR_TO_BB(F5) | SLOWCOOR_TO_BB(F7) |
147 SLOWCOOR_TO_BB(G2) | SLOWCOOR_TO_BB(G4) | SLOWCOOR_TO_BB(G6) |
148 SLOWCOOR_TO_BB(G8) | SLOWCOOR_TO_BB(H1) | SLOWCOOR_TO_BB(H3) |
149 SLOWCOOR_TO_BB(H5) | SLOWCOOR_TO_BB(H7)
154 // A way to select one file
156 BITBOARD BBFILE[8] = {
168 // A way to select one rank
170 BITBOARD BBRANK[9] = {
183 // Two files: A and H
185 BITBOARD BBROOK_PAWNS = BBFILEA | BBFILEH;
189 SlowCountBits(BITBOARD bb)
194 Strictly-C implementation of bit counting. How many bits are
195 asserted in a particular bitboard?
199 BITBOARD bb : the bitboard to count
203 ULONG : number of bits set in bb
217 static const int foldedTable[] = {
218 63,30, 3,32,59,14,11,33,
219 60,24,50, 9,55,19,21,34,
220 61,29, 2,53,51,23,41,18,
221 56,28, 1,43,46,27, 0,35,
222 62,31,58, 4, 5,49,54, 6,
223 15,52,12,40, 7,42,45,16,
224 25,57,48,13,10,39, 8,44,
225 20,47,38,22,17,37,36,26,
229 DeBruijnFirstBit(BITBOARD bb)
232 if (bb == 0ULL) return(0);
234 folded = ((int)bb) ^ ((int)(bb >> 32));
235 return foldedTable[(folded * 0x78291ACF) >> 26] + 1;
239 SlowFirstBit(BITBOARD bb)
244 Strictly-C implementation of "find the number of the first (lowest
245 order) asserted bit in the bitboard".
249 BITBOARD bb : the bitboard to test
253 ULONG : the number of the first bit from the low order side of the
254 bb that is asserted. The lowest order bit is #1.
258 static ULONG uTable[16] =
259 { // 0000 0001 0010 0011 0100 0101 0110 0111
260 0, 1, 2, 1, 3, 1, 2, 1,
261 // 1000 1001 1010 1011 1100 1101 1110 1111
262 4, 1, 2, 1, 3, 1, 2, 1 };
271 return(uTable[u] + (uShifts * 4));
281 SlowLastBit(BITBOARD bb)
286 Strictly-C implementation of "find the number of the last (highest
287 order) bit asserted in the bitboard".
289 Note: On every system benchmarked this code sucked. Using the x86
290 bsr instruction is way faster on modern x86 family processors.
294 BITBOARD bb : the bitboard to test
298 ULONG : the number of the first bit from the high order side of bb
299 that is asserted. The highest order bit is #64.
303 static ULONG uTable[16] =
304 { // 0000 0001 0010 0011 0100 0101 0110 0111
305 0, 1, 2, 2, 3, 3, 3, 3,
306 // 1000 1001 1010 1011 1100 1101 1110 1111
307 4, 4, 4, 4, 4, 4, 4, 4 };
313 u = (ULONG)((bb & 0xF000000000000000ULL) >> 60);
316 return(uTable[u] + (uShifts * 4));
320 ASSERT(uShifts < 15);
326 CoorFromBitBoardRank8ToRank1(BITBOARD *pbb)
331 Return the square cooresponding to the first asserted bit in the
332 bitboard or ILLEGAL_COOR if there are no bits asserted in the
333 bitboard. If a valid COOR is returned, clear that bit in the
346 COOR c = ILLEGAL_COOR;
347 ULONG uFirstBit = FirstBit(*pbb);
349 ASSERT(uFirstBit == SlowFirstBit(*pbb));
352 uFirstBit--; // bit 1 is 1 << 0
353 c = BIT_NUMBER_TO_COOR(uFirstBit);
354 ASSERT(c == SLOW_BIT_NUMBER_TO_COOR(uFirstBit));
355 ASSERT(IS_ON_BOARD(c));
363 CoorFromBitBoardRank1ToRank8(BITBOARD *pbb)
368 Return the square cooresponding to the last asserted bit in the
369 bitboard or ILLEGAL_COOR if there are no bits asserted in the
370 bitboard. If a valid COOR is returned, clear that bit in the
384 ULONG uLastBit = LastBit(*pbb);
385 ASSERT(SlowLastBit(*pbb) == uLastBit);
393 c = BIT_NUMBER_TO_COOR(uLastBit);
394 ASSERT(c == SLOW_BIT_NUMBER_TO_COOR(uLastBit));
395 ASSERT(IS_ON_BOARD(c));