-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitboard.h
More file actions
127 lines (116 loc) · 3.18 KB
/
bitboard.h
File metadata and controls
127 lines (116 loc) · 3.18 KB
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
/*
a chess library (bonus: you can integrate more piece types!) which
supports Chess960 and is decently fast enough
Copyright (C) 2025-2026 winapiadmin
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 3 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. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "types.h"
#if defined(_MSC_VER)
#include <intrin.h>
#endif
#include <immintrin.h>
namespace chess {
// -------------------------------
// constexpr fallbacks
// -------------------------------
constexpr int popcount_constexpr(Bitboard x) noexcept {
int count = 0;
while (x) {
x &= x - 1;
++count;
}
return count;
}
constexpr int lsb_constexpr(Bitboard x) noexcept {
int pos = 0;
while ((x & 1) == 0) {
x >>= 1;
++pos;
}
return pos;
}
constexpr int msb_constexpr(Bitboard x) noexcept {
int pos = 63;
Bitboard mask = 1ULL << 63;
while ((x & mask) == 0) {
mask >>= 1;
--pos;
}
return pos;
}
// -------------------------------
// runtime + constexpr aware
// -------------------------------
#if defined(__GNUG__) || defined(__clang__)
[[gnu::const]]
#endif
inline constexpr int popcount(Bitboard x) noexcept {
#if defined(__GNUG__) || defined(__clang__)
if (!is_constant_evaluated())
return __builtin_popcountll(x);
#elif defined(_MSC_VER)
if (!is_constant_evaluated())
return static_cast<int>(_mm_popcnt_u64(x));
#endif
return popcount_constexpr(x);
}
#if defined(__GNUG__) || defined(__clang__)
[[gnu::const]]
#endif
inline constexpr int lsb(Bitboard x) noexcept {
#if defined(__GNUG__) || defined(__clang__)
if (!is_constant_evaluated())
return __builtin_ctzll(x);
#elif defined(_MSC_VER)
if (!is_constant_evaluated()) {
unsigned long index = 0;
_BitScanForward64(&index, x);
return static_cast<int>(index);
}
#endif
return lsb_constexpr(x);
}
#if defined(__GNUG__) || defined(__clang__)
[[gnu::const]]
#endif
inline constexpr int msb(Bitboard x) noexcept {
#if defined(__GNUG__) || defined(__clang__)
if (!is_constant_evaluated())
return 63 - __builtin_clzll(x);
#elif defined(_MSC_VER)
if (!is_constant_evaluated()) {
unsigned long index = 0;
_BitScanReverse64(&index, x);
return static_cast<int>(index);
}
#endif
return msb_constexpr(x);
}
// -------------------------------
// destructive variants
// -------------------------------
inline int pop_lsb(Bitboard &b) noexcept {
int c = lsb(b);
#ifndef __BMI2__
b &= b - 1;
#else
b = _blsr_u64(b);
#endif
return c;
}
inline int pop_msb(Bitboard &b) noexcept {
int c = msb(b);
b &= ~(1ULL << c);
return c;
}
} // namespace chess