BitBoard.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file BitBoard.h This file contains the declaration of class BitBoard.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef BITBOARD_H
25 #define BITBOARD_H
26 
27 #ifndef USE_PCH
28 #include <cstdint>
29 #endif
30 
31 #include "Index.h"
32 
33 #define DEBUG_BITBOARD_INITIALIZATION 0
34 
35 #if DEBUG_BITBOARD_INITIALIZATION
36 #include <cassert>
37 #endif
38 
39 /** @namespace cwchess
40  * @brief A namespace for all chess related objects that are not related to the GUI.
41 * /
42 namespace cwchess {
43 
44 /** @brief The type of an internal BitBoard mask.
45 *
46  * Don't use this, use \link cwchess::BitBoard BitBoard \endlink.
47 * /
48 typedef uint64_t mask_t;
49 
50 /** @brief Convert Index to a mask_t.* /
51 inline mask_t index2mask(Index index)
52 {
53  return CW_MASK_T_CONST(1) << index();
54 }
55 
56 /** @brief Convert a \a col, \a row pair to a mask_t.* /
57 inline mask_t colrow2mask(int col, int row)
58 {
59  return CW_MASK_T_CONST(1) << (col + (row << 3));
60 }
61 
62 /** @brief Convert a single bit mask into it's Index.
63 *
64  * The provided \a mask should have a single bit set.
65  * However, if that is not the case, then this function
66  * returns either 64 if no bit is set, or the index
67  * of the first (least significant) bit that is set.
68 * /
69 inline Index mask2index(mask_t mask)
70 {
71  Index result(index_pre_begin);
72  result.next_bit_in(mask);
73  return result;
74 }
75 
76 /** @brief The POD base type of class BitBoard.
77 *
78  * This class contains a 64-bit unsigned integer
79  * where each bit represents one square on the chessboard.
80 *
81  * It may not have constructors or destructors, because
82  * it is needed that this struct is POD in order for
83  * optimisation to work.
84 *
85  * @sa BitBoard
86 * /
87 struct BitBoardData {
88  /** @brief A one-bit-per-square chessboard mask.
89  *
90  * The least significant bit represents square a1.
91  * The next bit b1, c1, ... h1, a2, b2, ... etcetera till h8.
92  * /
93  mask_t M_bitmask;
94 };
95 
96 BitBoardData const a1 = { CW_MASK_T_CONST(0x1) }; //!< The square a1.
97 BitBoardData const b1 = { CW_MASK_T_CONST(0x2) }; //!< The square b1.
98 BitBoardData const c1 = { CW_MASK_T_CONST(0x4) }; //!< The square c1.
99 BitBoardData const d1 = { CW_MASK_T_CONST(0x8) }; //!< The square d1.
100 BitBoardData const e1 = { CW_MASK_T_CONST(0x10) }; //!< The square e1.
101 BitBoardData const f1 = { CW_MASK_T_CONST(0x20) }; //!< The square f1.
102 BitBoardData const g1 = { CW_MASK_T_CONST(0x40) }; //!< The square g1.
103 BitBoardData const h1 = { CW_MASK_T_CONST(0x80) }; //!< The square h1.
104 BitBoardData const a2 = { CW_MASK_T_CONST(0x100) }; //!< The square a2.
105 BitBoardData const b2 = { CW_MASK_T_CONST(0x200) }; //!< The square b2.
106 BitBoardData const c2 = { CW_MASK_T_CONST(0x400) }; //!< The square c2.
107 BitBoardData const d2 = { CW_MASK_T_CONST(0x800) }; //!< The square d2.
108 BitBoardData const e2 = { CW_MASK_T_CONST(0x1000) }; //!< The square e2.
109 BitBoardData const f2 = { CW_MASK_T_CONST(0x2000) }; //!< The square f2.
110 BitBoardData const g2 = { CW_MASK_T_CONST(0x4000) }; //!< The square g2.
111 BitBoardData const h2 = { CW_MASK_T_CONST(0x8000) }; //!< The square h2.
112 BitBoardData const a3 = { CW_MASK_T_CONST(0x10000) }; //!< The square a3.
113 BitBoardData const b3 = { CW_MASK_T_CONST(0x20000) }; //!< The square b3.
114 BitBoardData const c3 = { CW_MASK_T_CONST(0x40000) }; //!< The square c3.
115 BitBoardData const d3 = { CW_MASK_T_CONST(0x80000) }; //!< The square d3.
116 BitBoardData const e3 = { CW_MASK_T_CONST(0x100000) }; //!< The square e3.
117 BitBoardData const f3 = { CW_MASK_T_CONST(0x200000) }; //!< The square f3.
118 BitBoardData const g3 = { CW_MASK_T_CONST(0x400000) }; //!< The square g3.
119 BitBoardData const h3 = { CW_MASK_T_CONST(0x800000) }; //!< The square h3.
120 BitBoardData const a4 = { CW_MASK_T_CONST(0x1000000) }; //!< The square a4.
121 BitBoardData const b4 = { CW_MASK_T_CONST(0x2000000) }; //!< The square b4.
122 BitBoardData const c4 = { CW_MASK_T_CONST(0x4000000) }; //!< The square c4.
123 BitBoardData const d4 = { CW_MASK_T_CONST(0x8000000) }; //!< The square d4.
124 BitBoardData const e4 = { CW_MASK_T_CONST(0x10000000) }; //!< The square e4.
125 BitBoardData const f4 = { CW_MASK_T_CONST(0x20000000) }; //!< The square f4.
126 BitBoardData const g4 = { CW_MASK_T_CONST(0x40000000) }; //!< The square g4.
127 BitBoardData const h4 = { CW_MASK_T_CONST(0x80000000) }; //!< The square h4.
128 BitBoardData const a5 = { CW_MASK_T_CONST(0x100000000) }; //!< The square a5.
129 BitBoardData const b5 = { CW_MASK_T_CONST(0x200000000) }; //!< The square b5.
130 BitBoardData const c5 = { CW_MASK_T_CONST(0x400000000) }; //!< The square c5.
131 BitBoardData const d5 = { CW_MASK_T_CONST(0x800000000) }; //!< The square d5.
132 BitBoardData const e5 = { CW_MASK_T_CONST(0x1000000000) }; //!< The square e5.
133 BitBoardData const f5 = { CW_MASK_T_CONST(0x2000000000) }; //!< The square f5.
134 BitBoardData const g5 = { CW_MASK_T_CONST(0x4000000000) }; //!< The square g5.
135 BitBoardData const h5 = { CW_MASK_T_CONST(0x8000000000) }; //!< The square h5.
136 BitBoardData const a6 = { CW_MASK_T_CONST(0x10000000000) }; //!< The square a6.
137 BitBoardData const b6 = { CW_MASK_T_CONST(0x20000000000) }; //!< The square b6.
138 BitBoardData const c6 = { CW_MASK_T_CONST(0x40000000000) }; //!< The square c6.
139 BitBoardData const d6 = { CW_MASK_T_CONST(0x80000000000) }; //!< The square d6.
140 BitBoardData const e6 = { CW_MASK_T_CONST(0x100000000000) }; //!< The square e6.
141 BitBoardData const f6 = { CW_MASK_T_CONST(0x200000000000) }; //!< The square f6.
142 BitBoardData const g6 = { CW_MASK_T_CONST(0x400000000000) }; //!< The square g6.
143 BitBoardData const h6 = { CW_MASK_T_CONST(0x800000000000) }; //!< The square h6.
144 BitBoardData const a7 = { CW_MASK_T_CONST(0x1000000000000) }; //!< The square a7.
145 BitBoardData const b7 = { CW_MASK_T_CONST(0x2000000000000) }; //!< The square b7.
146 BitBoardData const c7 = { CW_MASK_T_CONST(0x4000000000000) }; //!< The square c7.
147 BitBoardData const d7 = { CW_MASK_T_CONST(0x8000000000000) }; //!< The square d7.
148 BitBoardData const e7 = { CW_MASK_T_CONST(0x10000000000000) }; //!< The square e7.
149 BitBoardData const f7 = { CW_MASK_T_CONST(0x20000000000000) }; //!< The square f7.
150 BitBoardData const g7 = { CW_MASK_T_CONST(0x40000000000000) }; //!< The square g7.
151 BitBoardData const h7 = { CW_MASK_T_CONST(0x80000000000000) }; //!< The square h7.
152 BitBoardData const a8 = { CW_MASK_T_CONST(0x100000000000000) }; //!< The square a8.
153 BitBoardData const b8 = { CW_MASK_T_CONST(0x200000000000000) }; //!< The square b8.
154 BitBoardData const c8 = { CW_MASK_T_CONST(0x400000000000000) }; //!< The square c8
155 BitBoardData const d8 = { CW_MASK_T_CONST(0x800000000000000) }; //!< The square d8
156 BitBoardData const e8 = { CW_MASK_T_CONST(0x1000000000000000) }; //!< The square e8.
157 BitBoardData const f8 = { CW_MASK_T_CONST(0x2000000000000000) }; //!< The square f8.
158 BitBoardData const g8 = { CW_MASK_T_CONST(0x4000000000000000) }; //!< The square g8.
159 BitBoardData const h8 = { CW_MASK_T_CONST(0x8000000000000000) }; //!< The square h8.
160 
161 // Compare constants (this should never be needed, but why not add it).
162 inline bool operator==(BitBoardData b1, BitBoardData b2) { return b1.M_bitmask == b2.M_bitmask; }
163 inline bool operator!=(BitBoardData b1, BitBoardData b2) { return b1.M_bitmask != b2.M_bitmask; }
164 
165 /** @brief Calculate the union of two bit board constants.* /
167 {
168  BitBoardData result;
169  result.M_bitmask = x.M_bitmask | y.M_bitmask;
170  return result;
171 }
172 
173 /** @brief Calculate the intersection of two bit board constants.* /
175 {
176  BitBoardData result;
177  result.M_bitmask = x.M_bitmask & y.M_bitmask;
178  return result;
179 }
180 
181 /** @brief Calculate the union minus the intersection of two bit board constants.* /
183 {
184  BitBoardData result;
185  result.M_bitmask = x.M_bitmask ^ y.M_bitmask;
186  return result;
187 }
188 
189 /** @brief Calculate the inverse of the bitboard constant.* /
191 {
192  BitBoardData result;
193  result.M_bitmask = ~x.M_bitmask;
194  return result;
195 }
196 
197 BitBoardData const file_a = a1|a2|a3|a4|a5|a6|a7|a8; //!< The a-file.
198 BitBoardData const file_b = b1|b2|b3|b4|b5|b6|b7|b8; //!< The b-file.
199 BitBoardData const file_c = c1|c2|c3|c4|c5|c6|c7|c8; //!< The c-file.
200 BitBoardData const file_d = d1|d2|d3|d4|d5|d6|d7|d8; //!< The d-file.
201 BitBoardData const file_e = e1|e2|e3|e4|e5|e6|e7|e8; //!< The e-file.
202 BitBoardData const file_f = f1|f2|f3|f4|f5|f6|f7|f8; //!< The f-file.
203 BitBoardData const file_g = g1|g2|g3|g4|g5|g6|g7|g8; //!< The g-file.
204 BitBoardData const file_h = h1|h2|h3|h4|h5|h6|h7|h8; //!< The h-file.
205 
206 BitBoardData const rank_1 = a1|b1|c1|d1|e1|f1|g1|h1; //!< The first rank.
207 BitBoardData const rank_2 = a2|b2|c2|d2|e2|f2|g2|h2; //!< The second rank.
208 BitBoardData const rank_3 = a3|b3|c3|d3|e3|f3|g3|h3; //!< The third rank.
209 BitBoardData const rank_4 = a4|b4|c4|d4|e4|f4|g4|h4; //!< The fourth rank.
210 BitBoardData const rank_5 = a5|b5|c5|d5|e5|f5|g5|h5; //!< The fifth rank.
211 BitBoardData const rank_6 = a6|b6|c6|d6|e6|f6|g6|h6; //!< The sixth rank.
212 BitBoardData const rank_7 = a7|b7|c7|d7|e7|f7|g7|h7; //!< The seventh rank.
213 BitBoardData const rank_8 = a8|b8|c8|d8|e8|f8|g8|h8; //!< The eighth rank.
214 
215 BitBoardData const bdl_1 = h8; //!< The black diagonal from h8 to h8.
216 BitBoardData const bdl_2 = f8|g7|h6; //!< The black diagonal from f8 to h6.
217 BitBoardData const bdl_3 = d8|e7|f6|g5|h4; //!< The black diagonal from d8 to h4.
218 BitBoardData const bdl_4 = b8|c7|d6|e5|f4|g3|h2; //!< The black diagonal from b8 to h2.
219 BitBoardData const bdl_5 = a7|b6|c5|d4|e3|f2|g1; //!< The black diagonal from a7 to g1.
220 BitBoardData const bdl_6 = a5|b4|c3|d2|e1; //!< The black diagonal from a5 to e1.
221 BitBoardData const bdl_7 = a3|b2|c1; //!< The black diagonal from a3 to c1.
222 BitBoardData const bdl_8 = a1; //!< The black diagonal from a1 to a1.
223 
224 BitBoardData const bdr_1 = a7|b8; //!< The black diagonal from a7 to b8.
225 BitBoardData const bdr_2 = a5|b6|c7|d8; //!< The black diagonal from a5 to d8.
226 BitBoardData const bdr_3 = a3|b4|c5|d6|e7|f8; //!< The black diagonal from a3 to f8.
227 BitBoardData const bdr_4 = a1|b2|c3|d4|e5|f6|g7|h8; //!< The black diagonal from a1 to h8.
228 BitBoardData const bdr_5 = c1|d2|e3|f4|g5|h6; //!< The black diagonal from c1 to h6.
229 BitBoardData const bdr_6 = e1|f2|g3|h4; //!< The black diagonal from e1 to h4.
230 BitBoardData const bdr_7 = g1|h2; //!< The black diagonal from g1 ti h2.
231 
232 BitBoardData const wdr_1 = a8; //!< The white diagonal from a8 to a8.
233 BitBoardData const wdr_2 = a6|b7|c8; //!< The white diagonal from a6 to c8.
234 BitBoardData const wdr_3 = a4|b5|c6|d7|e8; //!< The white diagonal from a4 to e8.
235 BitBoardData const wdr_4 = a2|b3|c4|d5|e6|f7|g8; //!< The white diagonal from a2 to g8.
236 BitBoardData const wdr_5 = b1|c2|d3|e4|f5|g6|h7; //!< The white diagonal from b1 to h7.
237 BitBoardData const wdr_6 = d1|e2|f3|g4|h5; //!< The white diagonal from d1 to h5.
238 BitBoardData const wdr_7 = f1|g2|h3; //!< The white diagonal from f1 to h3.
239 BitBoardData const wdr_8 = h1; //!< The white diagonal from h1 to h1.
240 
241 BitBoardData const wdl_1 = g8|h7; //!< The white diagonal from g8 to h7.
242 BitBoardData const wdl_2 = e8|f7|g6|h5; //!< The white diagonal from e8 to h5.
243 BitBoardData const wdl_3 = c8|d7|e6|f5|g4|h3; //!< The white diagonal from c8 to h3.
244 BitBoardData const wdl_4 = a8|b7|c6|d5|e4|f3|g2|h1; //!< The white diagonal from a8 to h1.
245 BitBoardData const wdl_5 = a6|b5|c4|d3|e2|f1; //!< The white diagonal from a6 to f1.
246 BitBoardData const wdl_6 = a4|b3|c2|d1; //!< The white diagonal from a4 to d1.
247 BitBoardData const wdl_7 = a2|b1; //!< The white diagonal from a2 to b1.
248 
249 #if DEBUG_BITBOARD_INITIALIZATION
250 uint64_t const bitboard_initialization_magic = 1212121212121212121;
251 uint64_t const bitboard_destructed_magic = 555555555555555555;
252 #endif
253 
254 /** @brief A one-boolean-per-square chessboard.
255 *
256  * This class defines the interface for access to the bit-board.
257 *
258  * Normally one should be hidden from the bit-level implemention (mask_t thus).
259  * Instead use BitBoardData constant:
260 *
261  * \link cwchess::a1 a1 \endlink, ..., \link cwchess::h8 h8 \endlink<br>
262  * \link cwchess::file_a file_a \endlink, ..., \link cwchess::file_h file_h \endlink<br>
263  * \link cwchess::rank_1 rank_1 \endlink, ..., \link cwchess::rank_8 rank_8 \endlink<br>
264  * and others.
265 * /
266 class BitBoard : protected BitBoardData {
267 #if DEBUG_BITBOARD_INITIALIZATION
268  private:
269  uint64_t M_initialized;
270 #endif
271 
272  public:
273 #if DEBUG_BITBOARD_INITIALIZATION
274  BitBoard() : M_initialized(0) { }
275  ~BitBoard() { M_initialized = bitboard_destructed_magic; }
276 
277  bool is_initialized() const { return M_initialized == bitboard_initialization_magic; }
278 #else
279  //! @name Constructors
280  //@{
281 
282  //! Construct an uninitialized BitBoard.
283  BitBoard() { }
284 
285 #endif
286 
287  //! Construct a BitBoard from another BitBoard.
288  BitBoard(BitBoard const& other)
289  {
290 #if DEBUG_BITBOARD_INITIALIZATION
291  assert(other.is_initialized());
292  M_initialized = bitboard_initialization_magic;
293 #endif
294  M_bitmask = other.M_bitmask;
295  }
296 
297  //! Construct a BitBoard with a single bit set at \a index.
298  BitBoard(Index const& index)
299  {
300 #if DEBUG_BITBOARD_INITIALIZATION
301  M_initialized = bitboard_initialization_magic;
302 #endif
303  M_bitmask = index2mask(index);
304  }
305 
306  //! Construct a BitBoard from a constant.
308  {
309 #if DEBUG_BITBOARD_INITIALIZATION
310  M_initialized = bitboard_initialization_magic;
311 #endif
312  M_bitmask = data.M_bitmask;
313  }
314 
315  //! Construct a BitBoard with a single bit set at \a col, \a row.
316  BitBoard(int col, int row)
317  {
318 #if DEBUG_BITBOARD_INITIALIZATION
319  M_initialized = bitboard_initialization_magic;
320 #endif
321  M_bitmask = colrow2mask(col, row);
322  }
323 
324  //! Construct a BitBoard from a constant or mask (for internal use only).
325  explicit BitBoard(mask_t bitmask)
326  {
327 #if DEBUG_BITBOARD_INITIALIZATION
328  M_initialized = bitboard_initialization_magic;
329 #endif
330  M_bitmask = bitmask;
331  }
332 
333  //@}
334 
335  //! @name Assignment operators
336  //@{
337 
338  //! Assignment from other BitBoard.
339  BitBoard& operator=(BitBoard const& bitboard)
340  {
341 #if DEBUG_BITBOARD_INITIALIZATION
342  assert(bitboard.is_initialized());
343  M_initialized = bitboard_initialization_magic;
344 #endif
345  M_bitmask = bitboard.M_bitmask;
346  return* this;
347  }
348 
349  //! Assignment from a constant.
351  {
352 #if DEBUG_BITBOARD_INITIALIZATION
353  M_initialized = bitboard_initialization_magic;
354 #endif
355  M_bitmask = bitboard.M_bitmask;
356  return* this;
357  }
358 
359  //@}
360 
361  //! @name Comparison operators
362  //@{
363 
364  friend bool operator==(BitBoard const& b1, BitBoard const& b2) { return b1.M_bitmask == b2.M_bitmask; }
365  friend bool operator==(BitBoard const& b1, BitBoardData b2) { return b1.M_bitmask == b2.M_bitmask; }
366  friend bool operator==(BitBoardData b1, BitBoard const& b2) { return b1.M_bitmask == b2.M_bitmask; }
367  friend bool operator!=(BitBoard const& b1, BitBoard const& b2) { return b1.M_bitmask != b2.M_bitmask; }
368  friend bool operator!=(BitBoard const& b1, BitBoardData b2) { return b1.M_bitmask != b2.M_bitmask; }
369  friend bool operator!=(BitBoardData b1, BitBoard const& b2) { return b1.M_bitmask != b2.M_bitmask; }
370 
371  //@}
372 
373  //! @name Initialization
374  //@{
375 
376  //! Set all values to FALSE.
377  void reset()
378  {
379 #if DEBUG_BITBOARD_INITIALIZATION
380  M_initialized = bitboard_initialization_magic;
381 #endif
382  M_bitmask = 0;
383  }
384 
385  //! Set all values to TRUE.
386  void set()
387  {
388 #if DEBUG_BITBOARD_INITIALIZATION
389  M_initialized = bitboard_initialization_magic;
390 #endif
391  M_bitmask = CW_MASK_T_CONST(0xffffffffffffffff);
392  }
393 
394  //@}
395 
396  //! @name Bit fiddling
397  //@{
398 
399  //! @brief Reset the bit at \a col, \a row.
400  void reset(int col, int row) { M_bitmask& = ~colrow2mask(col, row); }
401 
402  //! @brief Reset the bit at \a index.
403  void reset(Index const& index) { M_bitmask& = ~index2mask(index); }
404 
405  //! @brief Reset the bits from \a mask.
406  void reset(mask_t mask) { M_bitmask& = ~mask; }
407 
408  //! @brief Reset the bits from \a bitboard.
409  void reset(BitBoardData bitboard) { M_bitmask& = ~bitboard.M_bitmask; }
410 
411  //! @brief Reset the bits from \a bitboard.
412  void reset(BitBoard const& bitboard) { M_bitmask& = ~bitboard.M_bitmask; }
413 
414  //! @brief Set the bit at \a col, \a row.
415  void set(int col, int row) { M_bitmask |= colrow2mask(col, row); }
416 
417  //! @brief Set the bit at \a index.
418  void set(Index const& index) { M_bitmask |= index2mask(index); }
419 
420  //! @brief Set the bits from \a mask.
421  void set(mask_t mask) { M_bitmask |= mask; }
422 
423  //! @brief Set the bits from \a bitboard.
424  void set(BitBoardData bitboard) { M_bitmask |= bitboard.M_bitmask; }
425 
426  //! @brief Set the bits from \a bitboard.
427  void set(BitBoard const& bitboard) { M_bitmask |= bitboard.M_bitmask; }
428 
429  //! @brief Toggle the bit at \a col, \a row.
430  void toggle(int col, int row) { M_bitmask ^= colrow2mask(col, row); }
431 
432  //! @brief Toggle the bit at \a index.
433  void toggle(Index const& index) { M_bitmask ^= index2mask(index); }
434 
435  //! @brief Toggle the bits from \a mask.
436  void toggle(mask_t mask) { M_bitmask ^= mask; }
437 
438  //! @brief Toggle the bits from \a bitboard.
439  void toggle(BitBoardData bitboard) { M_bitmask ^= bitboard.M_bitmask; }
440 
441  //! @brief Toggle the bits from \a bitboard.
442  void toggle(BitBoard const& bitboard) { M_bitmask ^= bitboard.M_bitmask; }
443 
444  //@}
445 
446  //! @name Accessors
447  //@{
448 
449  //! @brief Test if any bit is set at all.
450  bool test() const { return M_bitmask; }
451 
452  //! @brief Test if the bit at \a col, \a row is set.
453  bool test(int col, int row) const { return M_bitmask & colrow2mask(col, row); }
454 
455  //! @brief Test if the bit at \a index is set.
456  bool test(Index const& index) const { return M_bitmask & index2mask(index); }
457 
458  //! @brief Test if any bit in \a mask is set.
459  bool test(mask_t mask) const { return M_bitmask & mask; }
460 
461  //! @brief Test if any bit in \a bitboard is set.
462  bool test(BitBoardData bitboard) const { return M_bitmask & bitboard.M_bitmask; }
463 
464  //! @brief Test if any bit in \a bitboard is set.
465  bool test(BitBoard const& bitboard) const { return M_bitmask & bitboard.M_bitmask; }
466 
467  //! @brief Return the inverse of the bitboard.
468  BitBoard operator~() const { return BitBoard(~M_bitmask); }
469 
470  //! @brief Return TRUE if the bitboard is not empty.
471 #ifdef __x86_64
472  operator void*() const { return reinterpret_cast<void*>(M_bitmask); }
473 #else
474  // Casting to void* would only give us the lower 32 bits.
475  operator void*() const { return M_bitmask ? (void*)1 : 0; }
476 #endif
477 
478  //! @brief Return the underlaying bitmask.
479  mask_t operator()() const { return M_bitmask; }
480 
481  //@}
482 
483  //! @name Bit-wise OR operators with another BitBoard
484  //@{
485  BitBoard& operator|=(BitBoard const& bitboard) { M_bitmask |= bitboard.M_bitmask; return* this; }
486  BitBoard& operator|=(BitBoardData bitboard) { M_bitmask |= bitboard.M_bitmask; return* this; }
487  BitBoard& operator|=(mask_t bitmask) { M_bitmask |= bitmask; return* this; }
488  friend BitBoard operator|(BitBoard const& bitboard1, BitBoard const& bitboard2) { return BitBoard(bitboard1.M_bitmask | bitboard2.M_bitmask); }
489  friend BitBoard operator|(BitBoard const& bitboard1, BitBoardData bitboard2) { return BitBoard(bitboard1.M_bitmask | bitboard2.M_bitmask); }
490  friend BitBoard operator|(BitBoardData bitboard1, BitBoard const& bitboard2) { return BitBoard(bitboard1.M_bitmask | bitboard2.M_bitmask); }
491  //@}
492 
493  //! @name Bit-wise AND operators with another BitBoard
494  //@{
495  BitBoard& operator&=(BitBoard const& bitboard) { M_bitmask& = bitboard.M_bitmask; return* this; }
496  BitBoard& operator&=(BitBoardData bitboard) { M_bitmask& = bitboard.M_bitmask; return* this; }
497  BitBoard& operator&=(mask_t bitmask) { M_bitmask& = bitmask; return* this; }
498  friend BitBoard operator&(BitBoard bitboard1, BitBoard bitboard2) { return BitBoard(bitboard1.M_bitmask & bitboard2.M_bitmask); }
499  friend BitBoard operator&(BitBoard bitboard1, BitBoardData bitboard2) { return BitBoard(bitboard1.M_bitmask & bitboard2.M_bitmask); }
500  friend BitBoard operator&(BitBoardData bitboard1, BitBoard bitboard2) { return BitBoard(bitboard1.M_bitmask & bitboard2.M_bitmask); }
501  //@}
502 
503  //! @name Bit-wise XOR operators with another BitBoard
504  //@{
505  BitBoard& operator^=(BitBoard const& bitboard) { M_bitmask ^= bitboard.M_bitmask; return* this; }
506  BitBoard& operator^=(BitBoardData bitboard) { M_bitmask ^= bitboard.M_bitmask; return* this; }
507  BitBoard& operator^=(mask_t bitmask) { M_bitmask ^= bitmask; return* this; }
508  friend BitBoard operator^(BitBoard bitboard1, BitBoard bitboard2) { return BitBoard(bitboard1.M_bitmask ^ bitboard2.M_bitmask); }
509  friend BitBoard operator^(BitBoard bitboard1, BitBoardData bitboard2) { return BitBoard(bitboard1.M_bitmask ^ bitboard2.M_bitmask); }
510  friend BitBoard operator^(BitBoardData bitboard1, BitBoard bitboard2) { return BitBoard(bitboard1.M_bitmask ^ bitboard2.M_bitmask); }
511  //@}
512 
513 };
514 
515 } // namespace cwchess
516 
517 #endif // BITBOARD_H
BitBoardData const g6
The square g6.
Definition: BitBoard.h:142
BitBoardData const b8
The square b8.
Definition: BitBoard.h:153
BitBoardData const wdl_3
The white diagonal from c8 to h3.
Definition: BitBoard.h:243
BitBoardData const d8
The square d8.
Definition: BitBoard.h:155
BitBoardData const bdl_4
The black diagonal from b8 to h2.
Definition: BitBoard.h:218
bool test(BitBoard const& bitboard) const
Test if any bit in bitboard is set.
Definition: BitBoard.h:465
BitBoardData const b7
The square b7.
Definition: BitBoard.h:145
BitBoardData const a8
The square a8.
Definition: BitBoard.h:152
BitBoardData const c8
The square c8.
Definition: BitBoard.h:154
void reset(Index const& index)
Reset the bit at index.
Definition: BitBoard.h:403
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
BitBoard(mask_t bitmask)
Construct a BitBoard from a constant or mask (for internal use only).
Definition: BitBoard.h:325
mask_t M_bitmask
A one-bit-per-square chessboard mask.
Definition: BitBoard.h:93
void toggle(BitBoardData bitboard)
Toggle the bits from bitboard.
Definition: BitBoard.h:439
BitBoardData const c4
The square c4.
Definition: BitBoard.h:122
BitBoardData const file_h
The h-file.
Definition: BitBoard.h:204
BitBoardData const h7
The square h7.
Definition: BitBoard.h:151
BitBoardData const bdr_5
The black diagonal from c1 to h6.
Definition: BitBoard.h:228
This file contains the declaration of class Index.
BitBoardData const rank_2
The second rank.
Definition: BitBoard.h:207
BitBoardData const bdl_8
The black diagonal from a1 to a1.
Definition: BitBoard.h:222
BitBoardData const wdr_6
The white diagonal from d1 to h5.
Definition: BitBoard.h:237
BitBoardData const wdr_2
The white diagonal from a6 to c8.
Definition: BitBoard.h:233
BitBoardData const f1
The square f1.
Definition: BitBoard.h:101
BitBoardData operator~(BitBoardData x)
Calculate the inverse of the bitboard constant.
Definition: BitBoard.h:190
BitBoardData const a5
The square a5.
Definition: BitBoard.h:128
Index mask2index(mask_t mask)
Convert a single bit mask into it&#39;s Index.
Definition: BitBoard.h:69
BitBoardData const wdr_4
The white diagonal from a2 to g8.
Definition: BitBoard.h:235
mask_t index2mask(Index index)
Convert Index to a mask_t.
Definition: BitBoard.h:51
BitBoardData const f3
The square f3.
Definition: BitBoard.h:117
BitBoardData const bdr_6
The black diagonal from e1 to h4.
Definition: BitBoard.h:229
BitBoardData const h1
The square h1.
Definition: BitBoard.h:103
BitBoardData const h2
The square h2.
Definition: BitBoard.h:111
BitBoardData const file_a
The a-file.
Definition: BitBoard.h:197
void reset(BitBoardData bitboard)
Reset the bits from bitboard.
Definition: BitBoard.h:409
BitBoardData const d6
The square d6.
Definition: BitBoard.h:139
BitBoardData const bdr_7
The black diagonal from g1 ti h2.
Definition: BitBoard.h:230
bool test(BitBoardData bitboard) const
Test if any bit in bitboard is set.
Definition: BitBoard.h:462
BitBoardData const a1
The square a1.
Definition: BitBoard.h:96
void next_bit_in(uint64_t mask)
Advance the index to the next bit that is set in mask.
Definition: Index.h:360
BitBoardData const a4
The square a4.
Definition: BitBoard.h:120
The POD base type of class BitBoard.
Definition: BitBoard.h:87
BitBoardData const file_b
The b-file.
Definition: BitBoard.h:198
BitBoardData const wdr_7
The white diagonal from f1 to h3.
Definition: BitBoard.h:238
BitBoardData const file_g
The g-file.
Definition: BitBoard.h:203
BitBoardData const bdl_7
The black diagonal from a3 to c1.
Definition: BitBoard.h:221
BitBoardData const a7
The square a7.
Definition: BitBoard.h:144
BitBoardData const h4
The square h4.
Definition: BitBoard.h:127
BitBoardData const wdl_1
The white diagonal from g8 to h7.
Definition: BitBoard.h:241
BitBoard()
Construct an uninitialized BitBoard.
Definition: BitBoard.h:283
BitBoardData const c3
The square c3.
Definition: BitBoard.h:114
bool test(mask_t mask) const
Test if any bit in mask is set.
Definition: BitBoard.h:459
BitBoardData const g5
The square g5.
Definition: BitBoard.h:134
BitBoardData const e1
The square e1.
Definition: BitBoard.h:100
BitBoardData const rank_7
The seventh rank.
Definition: BitBoard.h:212
BitBoardData const c1
The square c1.
Definition: BitBoard.h:98
BitBoardData const file_d
The d-file.
Definition: BitBoard.h:200
BitBoardData const b5
The square b5.
Definition: BitBoard.h:129
BitBoardData const h6
The square h6.
Definition: BitBoard.h:143
A one-boolean-per-square chessboard.
Definition: BitBoard.h:266
The index of a chess square.
Definition: Index.h:211
bool test() const
Test if any bit is set at all.
Definition: BitBoard.h:450
BitBoard(BitBoardData data)
Construct a BitBoard from a constant.
Definition: BitBoard.h:307
BitBoardData const e8
The square e8.
Definition: BitBoard.h:156
BitBoardData const g2
The square g2.
Definition: BitBoard.h:110
BitBoardData operator|(BitBoardData x, BitBoardData y)
Calculate the union of two bit board constants.
Definition: BitBoard.h:166
bool test(int col, int row) const
Test if the bit at col, row is set.
Definition: BitBoard.h:453
BitBoardData const bdl_2
The black diagonal from f8 to h6.
Definition: BitBoard.h:216
BitBoardData const b3
The square b3.
Definition: BitBoard.h:113
BitBoardData const wdr_8
The white diagonal from h1 to h1.
Definition: BitBoard.h:239
BitBoardData operator&(BitBoardData x, BitBoardData y)
Calculate the intersection of two bit board constants.
Definition: BitBoard.h:174
BitBoardData const e6
The square e6.
Definition: BitBoard.h:140
BitBoardData const g7
The square g7.
Definition: BitBoard.h:150
BitBoardData const a3
The square a3.
Definition: BitBoard.h:112
BitBoardData const file_e
The e-file.
Definition: BitBoard.h:201
BitBoardData const a6
The square a6.
Definition: BitBoard.h:136
BitBoardData const d5
The square d5.
Definition: BitBoard.h:131
void reset(int col, int row)
Reset the bit at col, row.
Definition: BitBoard.h:400
uint64_t mask_t
The type of an internal BitBoard mask.
Definition: BitBoard.h:48
BitBoardData const d1
The square d1.
Definition: BitBoard.h:99
BitBoardData const e5
The square e5.
Definition: BitBoard.h:132
BitBoard(int col, int row)
Construct a BitBoard with a single bit set at col, row.
Definition: BitBoard.h:316
BitBoardData const g4
The square g4.
Definition: BitBoard.h:126
BitBoard(Index const& index)
Construct a BitBoard with a single bit set at index.
Definition: BitBoard.h:298
BitBoardData const c6
The square c6.
Definition: BitBoard.h:138
BitBoardData const bdr_2
The black diagonal from a5 to d8.
Definition: BitBoard.h:225
BitBoardData const bdl_5
The black diagonal from a7 to g1.
Definition: BitBoard.h:219
BitBoardData const rank_3
The third rank.
Definition: BitBoard.h:208
BitBoardData const b1
The square b1.
Definition: BitBoard.h:97
BitBoardData const g8
The square g8.
Definition: BitBoard.h:158
BitBoardData const b2
The square b2.
Definition: BitBoard.h:105
BitBoardData const c7
The square c7.
Definition: BitBoard.h:146
BitBoardData const rank_8
The eighth rank.
Definition: BitBoard.h:213
BitBoardData const e4
The square e4.
Definition: BitBoard.h:124
BitBoardData const b6
The square b6.
Definition: BitBoard.h:137
BitBoardData const b4
The square b4.
Definition: BitBoard.h:121
BitBoardData const f4
The square f4.
Definition: BitBoard.h:125
BitBoardData const wdr_3
The white diagonal from a4 to e8.
Definition: BitBoard.h:234
BitBoardData const bdl_6
The black diagonal from a5 to e1.
Definition: BitBoard.h:220
BitBoardData const e2
The square e2.
Definition: BitBoard.h:108
void toggle(int col, int row)
Toggle the bit at col, row.
Definition: BitBoard.h:430
BitBoardData const h8
The square h8.
Definition: BitBoard.h:159
bool test(Index const& index) const
Test if the bit at index is set.
Definition: BitBoard.h:456
BitBoardData const bdl_3
The black diagonal from d8 to h4.
Definition: BitBoard.h:217
BitBoardData const f2
The square f2.
Definition: BitBoard.h:109
BitBoard operator~() const
Return the inverse of the bitboard.
Definition: BitBoard.h:468
BitBoardData const file_f
The f-file.
Definition: BitBoard.h:202
void toggle(mask_t mask)
Toggle the bits from mask.
Definition: BitBoard.h:436
void toggle(Index const& index)
Toggle the bit at index.
Definition: BitBoard.h:433
void toggle(BitBoard const& bitboard)
Toggle the bits from bitboard.
Definition: BitBoard.h:442
BitBoard(BitBoard const& other)
Construct a BitBoard from another BitBoard.
Definition: BitBoard.h:288
BitBoardData const wdl_2
The white diagonal from e8 to h5.
Definition: BitBoard.h:242
BitBoardData const wdr_5
The white diagonal from b1 to h7.
Definition: BitBoard.h:236
IndexData const index_pre_begin
A constant representing& #39;one before the start&#39;.
Definition: Index.h:182
BitBoardData const f8
The square f8.
Definition: BitBoard.h:157
BitBoardData const rank_5
The fifth rank.
Definition: BitBoard.h:210
BitBoardData const wdr_1
The white diagonal from a8 to a8.
Definition: BitBoard.h:232
BitBoardData operator^(BitBoardData x, BitBoardData y)
Calculate the union minus the intersection of two bit board constants.
Definition: BitBoard.h:182
BitBoard & operator=(BitBoard const& bitboard)
Assignment from other BitBoard.
Definition: BitBoard.h:339
BitBoardData const g1
The square g1.
Definition: BitBoard.h:102
BitBoardData const wdl_7
The white diagonal from a2 to b1.
Definition: BitBoard.h:247
BitBoardData const bdr_3
The black diagonal from a3 to f8.
Definition: BitBoard.h:226
BitBoardData const rank_4
The fourth rank.
Definition: BitBoard.h:209
mask_t operator()() const
Return the underlaying bitmask.
Definition: BitBoard.h:479
BitBoardData const d2
The square d2.
Definition: BitBoard.h:107
BitBoardData const wdl_6
The white diagonal from a4 to d1.
Definition: BitBoard.h:246
BitBoardData const f5
The square f5.
Definition: BitBoard.h:133
BitBoardData const e7
The square e7.
Definition: BitBoard.h:148
BitBoardData const bdr_4
The black diagonal from a1 to h8.
Definition: BitBoard.h:227
BitBoardData const d3
The square d3.
Definition: BitBoard.h:115
BitBoardData const rank_6
The sixth rank.
Definition: BitBoard.h:211
void reset(mask_t mask)
Reset the bits from mask.
Definition: BitBoard.h:406
BitBoardData const d7
The square d7.
Definition: BitBoard.h:147
BitBoardData const wdl_4
The white diagonal from a8 to h1.
Definition: BitBoard.h:244
void reset(BitBoard const& bitboard)
Reset the bits from bitboard.
Definition: BitBoard.h:412
mask_t colrow2mask(int col, int row)
Convert a col, row pair to a mask_t.
Definition: BitBoard.h:57
BitBoardData const h5
The square h5.
Definition: BitBoard.h:135
void reset()
Set all values to FALSE.
Definition: BitBoard.h:377
BitBoardData const file_c
The c-file.
Definition: BitBoard.h:199
BitBoardData const h3
The square h3.
Definition: BitBoard.h:119
BitBoardData const a2
The square a2.
Definition: BitBoard.h:104
BitBoardData const wdl_5
The white diagonal from a6 to f1.
Definition: BitBoard.h:245
BitBoardData const c2
The square c2.
Definition: BitBoard.h:106
BitBoardData const bdr_1
The black diagonal from a7 to b8.
Definition: BitBoard.h:224
BitBoardData const c5
The square c5.
Definition: BitBoard.h:130
BitBoardData const bdl_1
The black diagonal from h8 to h8.
Definition: BitBoard.h:215
BitBoardData const rank_1
The first rank.
Definition: BitBoard.h:206
BitBoard & operator=(BitBoardData bitboard)
Assignment from a constant.
Definition: BitBoard.h:350
BitBoardData const g3
The square g3.
Definition: BitBoard.h:118
BitBoardData const e3
The square e3.
Definition: BitBoard.h:116
BitBoardData const d4
The square d4.
Definition: BitBoard.h:123
BitBoardData const f6
The square f6.
Definition: BitBoard.h:141
BitBoardData const f7
The square f7.
Definition: BitBoard.h:149

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.