Repository navigation
Expand file tree
/
Copy pathattacks.cpp
More file actions
169 lines (139 loc) · 4.88 KB
/
Copy pathattacks.cpp
File metadata and controls
169 lines (139 loc) · 4.88 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
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
//
// Created by Ashish Kumar Nanda on 8/9/26.
//
#include "attacks.h"
#include <iostream>
Bitboard rook_table[64][4096]{};
Bitboard bishop_table[64][512]{};
Bitboard rook_masks[64]{};
Bitboard bishop_masks[64]{};
int rook_shifts[64]{};
int bishop_shifts[64]{};
Bitboard slow_rook_attacks(Square rook_square, Bitboard occupations) {
Bitboard attacks = 0;
constexpr std::array<std::pair<int, int>, 4> rook_dirs = {{
{1, 0}, {0, 1}, {-1, 0}, {0, -1}
}};
const int rank = rook_square / 8;
const int file = rook_square % 8;
for (auto [dr, df] : rook_dirs) {
int r = rank + dr;
int f = file + df;
while (r >= 0 && r < 8 && f >= 0 && f < 8) {
const int square = f + 8 * r;
attacks |= 1ULL << square;
//inc
if ((1ULL << square) & occupations) {
break;
}
r += dr;
f += df;
}
}
return attacks;
}
Bitboard slow_bishop_attacks(Square bishop_square, Bitboard occupations) {
Bitboard attacks = 0;
constexpr std::array<std::pair<int, int>, 4> bishop_dirs = {{
{-1, -1}, {1, 1}, {-1, 1}, {1, -1}
}};
const int rank = bishop_square / 8;
const int file = bishop_square % 8;
for (auto [dr, df] : bishop_dirs) {
int r = rank + dr;
int f = file + df;
while (r >= 0 && r < 8 && f >= 0 && f < 8) {
const int square = f + 8 * r;
attacks |= 1ULL << square;
if ((1ULL << square) & occupations) {
break;
}
//inc
r += dr;
f += df;
}
}
return attacks;
}
Bitboard relevant_occupancy_rook_mask(Square rook_square) {
Bitboard attacks = 0;
constexpr std::array<std::pair<int, int>, 4> rook_dirs = {{
{1, 0}, {0, 1}, {-1, 0}, {0, -1}
}};
const int rank = rook_square / 8;
const int file = rook_square % 8;
for (auto [dr, df] : rook_dirs) {
int r = rank + dr;
int f = file + df;
const int upper_rank_lim = (rank == 7) ? 8 : 7;
const int lower_rank_lim = (rank == 0) ? 0 : 1;
const int upper_file_lim = (file == 7) ? 8 : 7;
const int lower_file_lim = (file == 0) ? 0 : 1;
while (r >= lower_rank_lim && r < upper_rank_lim && f >= lower_file_lim && f < upper_file_lim) {
const int square = f + 8 * r;
attacks |= 1ULL << square;
//inc
r += dr;
f += df;
}
}
return attacks;
}
Bitboard relevant_occupancy_bishop_mask(Square bishop_square) {
Bitboard attacks = 0;
constexpr std::array<std::pair<int, int>, 4> bishop_dirs = {{
{-1, -1}, {1, 1}, {-1, 1}, {1, -1}
}};
const int rank = bishop_square / 8;
const int file = bishop_square % 8;
for (auto [dr, df] : bishop_dirs) {
int r = rank + dr;
int f = file + df;
const int upper_rank_lim = (rank == 7) ? 8 : 7;
const int lower_rank_lim = (rank == 0) ? 0 : 1;
const int upper_file_lim = (file == 7) ? 8 : 7;
const int lower_file_lim = (file == 0) ? 0 : 1;
while (r >= lower_rank_lim && r < upper_rank_lim && f >= lower_file_lim && f < upper_file_lim) {
const int square = f + 8 * r;
attacks |= 1ULL << square;
//inc
r += dr;
f += df;
}
}
return attacks;
}
void init_magics() {
for (int i = 0; i < 64; i++) {
Bitboard rmask = relevant_occupancy_rook_mask(static_cast<Square>(i));
Bitboard bmask = relevant_occupancy_bishop_mask(static_cast<Square>(i));
int rook_num_relevant_bits = std::popcount(rmask);
int bishop_num_relevant_bits = std::popcount(bmask);
rook_masks[i] = rmask;
rook_shifts[i] = 64 - rook_num_relevant_bits;
bishop_masks[i] = bmask;
bishop_shifts[i] = 64 - bishop_num_relevant_bits;
Bitboard rsubset = 0;
Bitboard bsubset = 0;
do {
int rindex = (rsubset * rook_magics[i]) >> rook_shifts[i];
Bitboard rattacks = slow_rook_attacks(static_cast<Square>(i), rsubset);
if (rook_table[i][rindex] != 0 && rook_table[i][rindex] != rattacks){
std::cout << "BIG CRASH" << std::endl;
exit(1);
}
rook_table[i][rindex] = rattacks;
rsubset = (rsubset - rmask) & rmask;
} while (rsubset);
do {
int bindex = (bsubset * bishop_magics[i]) >> bishop_shifts[i];
Bitboard battacks = slow_bishop_attacks(static_cast<Square>(i), bsubset);
if (bishop_table[i][bindex] != 0 && bishop_table[i][bindex] != battacks) {
std::cout << "BIG CRASH (bishop)" << std::endl;
exit(1);
}
bishop_table[i][bindex] = battacks;
bsubset = (bsubset - bmask) & bmask;
} while (bsubset);
}
}