-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbit.cpp
More file actions
168 lines (118 loc) · 3.37 KB
/
bit.cpp
File metadata and controls
168 lines (118 loc) · 3.37 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
// includes
#include "bit.hpp"
#include "common.hpp"
#include "libmy.hpp"
#include "var.hpp"
namespace bit {
// variables
static Bit File[File_Size];
static Bit Rank[Rank_Size];
static Bit Capture_Mask[Square_Size][64];
static Bit Beyond[Square_Size][64];
static int Line_Inc[Square_Size][64];
static Bit Man_Moves[Square_Size];
static Bit King_Moves[Square_Size];
static Bit Man_Captures[Square_Size];
static Bit King_Captures[Square_Size];
// prototypes
static Bit ray_first (Square from, Inc inc);
static Bit ray_last (Square from, Inc inc);
static Bit ray_all (Square from, Inc inc);
// functions
void init() {
static_assert(sizeof(Bit) == 8, "");
// files and ranks
for (Square sq : Squares) {
set(File[square_file(sq)], sq);
set(Rank[square_rank(sq)], sq);
}
// king attacks
for (Square from : Squares) {
Man_Captures[from] = Bit(0);
King_Captures[from] = Bit(0);
for (int dir = 0; dir < Dir_Size; dir++) {
Inc inc = dir_inc(dir);
if (dir < 4) {
Man_Moves[from] |= ray_first(from, inc); // HACK: all directions
King_Moves[from] |= ray_all (from, inc);
}
if (dir < 4 || var::Variant == var::Frisian) {
Man_Captures[from] |= ray_first(from, inc) & ~ray_last(from, inc);
King_Captures[from] |= ray_all (from, inc) & ~ray_last(from, inc);
}
for (Square to : ray_all(from, inc)) {
Capture_Mask[from][to] = (ray_all(from, inc) & ~bit(to) & ~ray_all(to, inc)) | ray_first(to, inc);
Beyond[from][to] = ray_all(to, inc);
Line_Inc[from][to] = inc;
}
}
}
}
static Bit ray_first(Square from, Inc inc) {
Bit b {};
if (square_is_ok(from + inc)) set(b, square_make(from + inc));
return b;
}
static Bit ray_last(Square from, Inc inc) {
Bit b {};
if (square_is_ok(from + inc)) {
int sq = from + inc;
while (square_is_ok(sq + inc)) sq += inc;
set(b, square_make(sq));
}
return b;
}
static Bit ray_all(Square from, Inc inc) {
Bit b {};
for (int sq = from + inc; square_is_ok(sq); sq += inc) {
set(b, square_make(sq));
}
return b;
}
Bit file(int fl) {
assert(fl >= 0 && fl < File_Size);
return File[fl];
}
Bit rank(int rk) {
assert(rk >= 0 && rk < Rank_Size);
return Rank[rk];
}
Bit rank(int rk, Side sd) {
assert(rk >= 0 && rk < Rank_Size);
if (sd != Black) rk = (Rank_Size - 1) - rk;
return Rank[rk];
}
Bit capture_mask(Square from, Square to) {
assert(has(King_Captures[from], to));
return Capture_Mask[from][to];
}
Bit beyond(Square from, Square to) {
assert(has(King_Captures[from], to));
return Beyond[from][to];
}
Inc line_inc(Square from, Square to) {
assert(has(King_Captures[from], to));
return inc_make(Line_Inc[from][to]);
}
Bit man_moves(Square from) {
return Man_Moves[from];
}
Bit man_captures(Square from) {
return Man_Captures[from];
}
Bit king_captures(Square from) {
return King_Captures[from];
}
Bit king_moves(Square from, Bit empty) {
return attack(from, King_Moves[from], empty);
}
Bit king_captures(Square from, Bit empty) {
return attack(from, King_Captures[from], empty);
}
Bit attack(Square from, Bit tos, Bit empty) {
for (Square sq : tos & King_Captures[from] & ~empty) {
tos &= ~Beyond[from][sq];
}
return tos;
}
} // namespace bit