/
bitboard.cpp
94 lines (78 loc) · 1.82 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
90
91
92
93
94
#include <iostream>
#include "utils.h"
#include "consts.h"
#include "bitboard.h"
using namespace std;
namespace eia_v0_5
{
BBTables::BBTables()
{
for (int i = 0; i < 65536; i++)
{
int cnt = 0;
for (U64 j = +i; j; j = rlsb(j)) cnt++;
lut[i] = cnt;
}
}
ostream & operator << (ostream & os, const BitBoard & bb)
{
for (int y = 7; y >= 0; y--)
{
os << " ";
for (int x = 0; x < 8; x++)
{
SQ sq = sq_(x, y);
U64 bit = BIT << sq;
char ch = (bb.val & bit) ? '*' : '.';
os << ch;
}
os << endl;
}
os << endl;
return os;
}
void print64(U64 bb)
{
cout << BitBoard{bb} << endl;
}
inline U64 lsb(U64 bb)
{
return bb & (EMPTY - bb);
}
inline U64 rlsb(U64 bb)
{
return bb & (bb - BIT);
}
inline U64 msb(U64 bb)
{
unsigned n = 0;
while (bb >>= 1) n++;
return 1ull << n;
}
#if (defined(_M_AMD64) || defined(_M_X64)) // [SSE2 x64]
#include <intrin.h>
inline int popcnt(U64 bb)
{
return static_cast<int>(__popcnt64(bb));
}
inline SQ bitscan(U64 bb)
{
unsigned long btscn_indx;
_BitScanForward64(&btscn_indx, bb);
return static_cast<SQ>(btscn_indx);
}
#else
inline int popcnt(U64 bb)
{
int a = bb >> 48;
int b = (bb >> 32) & 0xFFFF;
int c = (bb >> 16) & 0xFFFF;
int d = bb & 0xFFFF;
return BBT->lut[a] + BBT->lut[b] + BBT->lut[c] + BBT->lut[d];
}
inline SQ bitscan(U64 bb)
{
return static_cast<SQ>(BBT->btscn[(lsb(bb) * BBT->DEBRUIJN) >> 58]);
}
#endif
}