-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitboard.cpp
89 lines (73 loc) · 1.83 KB
/
bitboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* $Id$
This file is a part of ponder, a English/American checkers game.
Copyright (c) 2006, 2007 Quux Information.
Gong Jie <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/** @file bitboard.cpp
* @brief Bitboard.
*/
#include <cassert>
#include <stdexcept>
#include "bitboard.hpp"
namespace checkers
{
unsigned int bitboard::count(void) const
{
uint32_t x = this->_bitboard;
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0f0f0f0f;
x = x + (x >> 8);
x = x + (x >> 16);
return x & 0x0000003f;
}
unsigned int bitboard::ntz(void) const
{
uint32_t x = this->_bitboard;
if (0 == x)
{
return 32;
}
unsigned int n = 1;
if (0 == (x & 0x0000ffff))
{
n += 16;
x >>= 16;
}
if (0 == (x & 0x000000ff))
{
n += 8;
x >>= 8;
}
if (0 == (x & 0x0000000f))
{
n += 4;
x >>= 4;
}
if (0 == (x & 0x00000003))
{
n += 2;
x >>= 2;
}
return n - (x & 1);
}
std::ostream& operator <<(std::ostream& os, const bitboard& rhs)
{
assert(1 == rhs.count());
os << (rhs.ntz() + 1);
return os;
}
}
// End of file