-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.h
executable file
·213 lines (178 loc) · 6.01 KB
/
utils.h
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef UTILS_H
#define UTILS_H
#include "defines.h"
#include "constants.h"
#include <string>
#include <vector>
#include <cmath>
namespace Napoleon
{
namespace Utils
{
namespace Square
{
int GetA1H8DiagonalIndex(int, int);
int GetA1H8DiagonalIndex(int);
int GetH1A8AntiDiagonalIndex(int, int);
int GetH1A8AntiDiagonalIndex(int);
int GetFileIndex(int);
int GetRankIndex(int);
int GetSquareIndex(int, int);
int RelativeRank(Color, int);
int MirrorSquare(int);
int Distance(int, int);
int Parse(std::string);
std::string ToAlgebraic(Napoleon::Square);
}
namespace BitBoard
{
bool IsBitSet(Napoleon::BitBoard, int);
void Display(Napoleon::BitBoard);
void SetBit(Napoleon::BitBoard&, unsigned int);
std::string ToString(Napoleon::BitBoard);
int PopCount(Napoleon::BitBoard);
int BitScanForward(Napoleon::BitBoard);
int BitScanForwardReset(Napoleon::BitBoard&);
int BitScanReverse(Napoleon::BitBoard);
Napoleon::BitBoard SouthFill(Napoleon::BitBoard);
}
namespace Piece
{
Byte GetPiece(char);
char GetInitial(Byte);
char GetInitial(Napoleon::Piece);
Color GetOpposite(Color);
}
namespace Math
{
// visual studio does not provide log2 function
double Log2(double);
}
namespace String
{
void Split(std::vector<std::string> &, const std::string &, char);
}
INLINE void BitBoard::SetBit(Napoleon::BitBoard& bitboard, unsigned int sq)
{
bitboard |= (static_cast<Napoleon::BitBoard>(1) << sq);
}
#if defined(__GNUC__) && defined(__LP64__)
INLINE int BitBoard::BitScanForward(Napoleon::BitBoard bitBoard)
{
__asm__("bsfq %0, %0": "=r"(bitBoard): "0" (bitBoard) );
return bitBoard;
}
INLINE int BitBoard::BitScanForwardReset(Napoleon::BitBoard& bitBoard)
{
Napoleon::BitBoard index;
__asm__("bsfq %1, %0": "=r"(index): "rm"(bitBoard) );
bitBoard &= (bitBoard - 1);
return index;
}
#elif defined(__GNUC__)
INLINE int BitBoard::BitScanForward(Napoleon::BitBoard bitBoard)
{
return __builtin_ctzll(bitBoard);
}
INLINE int BitBoard::BitScanForwardReset(Napoleon::BitBoard& bitBoard)
{
Napoleon::BitBoard bb = bitBoard;
bitBoard &= (bitBoard - 1);
return __builtin_ctzll(bb);
}
#elif defined(_MSC_VER) && defined(_WIN64)
INLINE int BitBoard::BitScanForward(Napoleon::BitBoard bitBoard)
{
unsigned long index;
_BitScanForward64(&index, bitBoard);
return (int)index;
}
INLINE int BitBoard::BitScanForwardReset(Napoleon::BitBoard& bitBoard)
{
unsigned long index;
_BitScanForward64(&index, bitBoard);
bitBoard &= (bitBoard - 1);
return (int)index;
}
#else
INLINE int BitBoard::BitScanForward(Napoleon::BitBoard bitBoard)
{
return Constants::DeBrujinTable[((bitBoard & -bitBoard) * Constants::DeBrujinValue) >> 58];
}
INLINE int BitBoard::BitScanForwardReset(Napoleon::BitBoard& bitBoard)
{
Napoleon::BitBoard bb = bitBoard;
bitBoard &= (bitBoard - 1);
return Constants::DeBrujinTable[((bb & -bb) * Constants::DeBrujinValue) >> 58];
}
#endif
INLINE int BitBoard::PopCount(Napoleon::BitBoard bitBoard)
{
#ifdef __GNUG__
return __builtin_popcountll(bitBoard);
#else
bitBoard -= ((bitBoard >> 1) & 0x5555555555555555UL);
bitBoard = ((bitBoard >> 2) & 0x3333333333333333UL) + (bitBoard & 0x3333333333333333UL);
bitBoard = ((bitBoard >> 4) + bitBoard) & 0x0F0F0F0F0F0F0F0FUL;
return (int)((bitBoard * 0x0101010101010101UL) >> 56);
#endif
}
INLINE Napoleon::BitBoard BitBoard::SouthFill(Napoleon::BitBoard bitboard)
{
bitboard |= bitboard >> 8;
bitboard |= bitboard >> 16;
bitboard |= bitboard >> 32;
return bitboard;
}
inline int Square::GetA1H8DiagonalIndex(int file, int rank)
{
return 7 + rank - file;
}
inline int Square::GetA1H8DiagonalIndex(int squareIndex)
{
return 7 + GetRankIndex(squareIndex) - GetFileIndex(squareIndex);
}
inline int Square::GetH1A8AntiDiagonalIndex(int file, int rank)
{
return rank + file;
}
inline int Square::GetH1A8AntiDiagonalIndex(int squareIndex)
{
return GetRankIndex(squareIndex) + GetFileIndex(squareIndex);
}
inline int Square::GetFileIndex(int squareIndex)
{
return squareIndex & 7;
}
inline int Square::GetRankIndex(int squareIndex)
{
return squareIndex >> 3;
}
inline int Square::GetSquareIndex(int file, int rank)
{
return file + 8 * rank;
}
inline int Square::MirrorSquare(int square)
{
return square ^ 56;
}
inline int Square::Distance(int sq1, int sq2)
{
int f1, f2, r1, r2;
f1 = GetFileIndex(sq1);
f2 = GetFileIndex(sq2);
r1 = GetRankIndex(sq1);
r2 = GetRankIndex(sq2);
return std::max(std::abs(r1 - r2), std::abs(f1 - f2));
}
inline Color Piece::GetOpposite(Color color)
{
return Color(1 ^ color);
}
inline int Square::RelativeRank(Color color, int rank)
{
return color == PieceColor::White ? rank : 7 - rank;
}
}
}
#endif // UTILS_H