CountBoard.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file CountBoard.h This file contains the declaration of class CountBoard.
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 COUNTBOARD_H
25 #define COUNTBOARD_H
26 
27 #ifndef USE_PCH
28 #include <cstring>
29 #ifndef DOXYGEN
30 #include "debug.h"
31 #endif
32 #endif
33 
34 #include "BitBoard.h"
35 
36 namespace cwchess {
37 
38 class CountBoard {
39  private:
40  BitBoard M_bits[4];
41  BitBoard M_any;
42 
43  public:
44  CountBoard(void) { }
45  CountBoard(CountBoard const& count_board) { std::memcpy(this,& count_board, sizeof(CountBoard)); }
46  CountBoard& operator=(CountBoard const& count_board) { std::memcpy(this,& count_board, sizeof(CountBoard)); return* this; }
47 
48  void add(BitBoard const& bit_board)
49  {
50 #ifdef CWDEBUG
51  if (debug::channels::dc::countboard.is_on())
52  {
53  // Get the location that we were called from.
54  libcwd::location_ct location((char*)__builtin_return_address(0) + libcwd::builtin_return_address_offset);
55  // Demangle the function name of the location that we were called from.
56  std::string demangled_function_name;
57  libcwd::demangle_symbol(location.mangled_function_name(), demangled_function_name);
58  // Print it.
59  Dout(dc::countboard, "CountBoard::add() with this = " << (void*)this << " [called from " << demangled_function_name << '(' << location << ")], adds:");
60  for (int row = 7; row >= 0; --row)
61  {
62  for (int col = 0; col <= 7; ++col)
63  {
64  if (bit_board.test(col, row))
65  std::cout << "1 ";
66  else
67  std::cout << "0 ";
68  }
69  std::cout << '\n';
70  }
71  }
72 #endif
73  // This whole function takes typically 4 clock cycles on a QX6700.
74  BitBoard input(bit_board);
75  M_any |= input;
76  BitBoard bits_and1(M_bits[0] & input);
77  M_bits[0] ^= input;
78  BitBoard bits_and2(M_bits[1] & bits_and1);
79  M_bits[1] ^= bits_and1;
80  bits_and1 = M_bits[2] & bits_and2;
81  M_bits[2] ^= bits_and2;
82  M_bits[3] ^= bits_and1;
83  }
84 
85  void sub(BitBoard const& bit_board)
86  {
87 #ifdef CWDEBUG
88  if (debug::channels::dc::countboard.is_on())
89  {
90  // Get the location that we were called from.
91  libcwd::location_ct location((char*)__builtin_return_address(0) + libcwd::builtin_return_address_offset);
92  // Demangle the function name of the location that we were called from.
93  std::string demangled_function_name;
94  libcwd::demangle_symbol(location.mangled_function_name(), demangled_function_name);
95  // Print it.
96  Dout(dc::countboard, "CountBoard::sub() with this = " << (void*)this << " [called from " << demangled_function_name << '(' << location << ")], subtracts:");
97  for (int row = 7; row >= 0; --row)
98  {
99  for (int col = 0; col <= 7; ++col)
100  {
101  if (bit_board.test(col, row))
102  std::cout << "1 ";
103  else
104  std::cout << "0 ";
105  }
106  std::cout << '\n';
107  }
108  }
109 #endif
110  // This whole function takes typically 5 clock cycles on a QX6700.
111  BitBoard input(bit_board);
112  BitBoard bits_and1(~M_bits[0] & input);
113  BitBoard collect(M_bits[0] ^= input);
114  BitBoard bits_and2(~M_bits[1] & bits_and1);
115  collect |= (M_bits[1] ^= bits_and1);
116  bits_and1 = ~M_bits[2] & bits_and2;
117  collect |= (M_bits[2] ^= bits_and2);
118  collect |= (M_bits[3] ^= bits_and1);
119  M_any = collect;
120  }
121 
122  void reset(void)
123  {
124  std::memset(this, 0, sizeof(CountBoard));
125 #if DEBUG_BITBOARD_INITIALIZATION
126  M_bits[0].reset();
127  M_bits[1].reset();
128  M_bits[2].reset();
129  M_bits[3].reset();
130  M_any.reset();
131 #endif
132  }
133 
134  BitBoard any(void) const { return M_any; }
135 
136  int count(BitBoard const& pos) const
137  {
138  return ((M_bits[0] & pos) ? 1 : 0) | ((M_bits[1] & pos) ? 2 : 0) | ((M_bits[2] & pos) ? 4 : 0) | ((M_bits[3] & pos) ? 8 : 0);
139  }
140 
141  int count(Index const& index) const
142  {
143  BitBoard pos(index);
144  return count(pos);
145  }
146 };
147 
148 } // namespace cwchess
149 
150 #endif // COUNTBOARD_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
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
This file contains the declaration of class BitBoard.
void reset()
Set all values to FALSE.
Definition: BitBoard.h:377

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