-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbmi2.cpp
92 lines (74 loc) · 3.48 KB
/
bmi2.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
/*
Clarity
Copyright (C) 2024 Joseph Pasfield
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/>.
*/
#include "slidey.h"
#include <immintrin.h>
// the nested arrays used to lookup the attacks
std::array<std::array<uint64_t, 512>, 64> bishopAttackLookup;
std::array<std::array<uint64_t, 4096>, 64> rookAttackLookup;
int getBishopBlockerCombinations(std::array<uint64_t, 512> &blockers, const uint64_t mask) {
// calculates the total amount of possible blocker configuration (2^n)
int totalPatterns = 1 << __builtin_popcountll(mask);
assert(totalPatterns <= 512);
// generates them, using pdep
for(int i = 0; i < totalPatterns; i++) {
blockers[i] = _pdep_u64(i, mask);
}
return totalPatterns;
}
// seperated so that I can have the assertions be right
int getRookBlockerCombinations(std::array<uint64_t, 4096> &blockers, const uint64_t mask) {
// calculates the total amount of possible blocker configuration (2^n)
int totalPatterns = 1 << __builtin_popcountll(mask);
assert(totalPatterns <= 4096);
// generates them, using pdep
for(int i = 0; i < totalPatterns; i++) {
blockers[i] = _pdep_u64(i, mask);
}
return totalPatterns;
}
// generates the lookup tables for each square, ran on startup
void generateLookups() {
// loops through each square
for(int i = 0; i < 64; i++) {
// generate the different blocker patterns
std::array<uint64_t, 4096> rookBlockers;
int rookPatterns = getRookBlockerCombinations(rookBlockers, rookMasks[i]);
// calculate the moves for each one
for(int j = 0; j < rookPatterns; j++) {
rookAttackLookup[i][calculateRookIndex(rookBlockers[j], i)] = getRookAttacksOld(i, rookBlockers[j]);
}
// bishop blocker patterns
std::array<uint64_t, 512> bishopBlockers;
int bishopPatterns = getBishopBlockerCombinations(bishopBlockers, bishopMasks[i]);
// calculate the moves for each one
for(int j = 0; j < bishopPatterns; j++) {
bishopAttackLookup[i][calculateBishopIndex(bishopBlockers[j], i)] = getBishopAttacksOld(i, bishopBlockers[j]);
}
}
}
// uses pext to calculate the index for each square and blockers
uint64_t calculateRookIndex(const uint64_t occupiedBitboard, const int square) {
return _pext_u64(occupiedBitboard, rookMasks[square]);
}
uint64_t calculateBishopIndex(const uint64_t occupiedBitboard, const int square) {
return _pext_u64(occupiedBitboard, bishopMasks[square]);
}
// gets the attacks from the tables so the values don't have to be public
uint64_t getRookAttacksFromTable(const uint64_t occupiedBitboard, const int square) {
return rookAttackLookup[square][calculateRookIndex(occupiedBitboard, square)];
}
uint64_t getBishopAttacksFromTable(const uint64_t occupiedBitboard, const int square) {
return bishopAttackLookup[square][calculateBishopIndex(occupiedBitboard, square)];
}