-
Notifications
You must be signed in to change notification settings - Fork 3
/
cu_Bitray.h
194 lines (166 loc) · 13.5 KB
/
cu_Bitray.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
#pragma once
#include "cu_Common.h"
#ifdef __CUDA_ARCH__
# define HOST_DEVICE_LOOKUP static constexpr __constant__
#else
# define HOST_DEVICE_LOOKUP static constexpr
#endif
#define BitFunction __inline__ __device__ uint64_t
#define bitswap(X) __brevll(X)
namespace Bitray
{
HOST_DEVICE_LOOKUP uint64_t const_ho[] = {
0x00000000000000FE, 0x00000000000000FC, 0x00000000000000F8, 0x00000000000000F0, 0x00000000000000E0, 0x00000000000000C0, 0x0000000000000080, 0x0000000000000000,
0x000000000000FE00, 0x000000000000FC00, 0x000000000000F800, 0x000000000000F000, 0x000000000000E000, 0x000000000000C000, 0x0000000000008000, 0x0000000000000000,
0x0000000000FE0000, 0x0000000000FC0000, 0x0000000000F80000, 0x0000000000F00000, 0x0000000000E00000, 0x0000000000C00000, 0x0000000000800000, 0x0000000000000000,
0x00000000FE000000, 0x00000000FC000000, 0x00000000F8000000, 0x00000000F0000000, 0x00000000E0000000, 0x00000000C0000000, 0x0000000080000000, 0x0000000000000000,
0x000000FE00000000, 0x000000FC00000000, 0x000000F800000000, 0x000000F000000000, 0x000000E000000000, 0x000000C000000000, 0x0000008000000000, 0x0000000000000000,
0x0000FE0000000000, 0x0000FC0000000000, 0x0000F80000000000, 0x0000F00000000000, 0x0000E00000000000, 0x0000C00000000000, 0x0000800000000000, 0x0000000000000000,
0x00FE000000000000, 0x00FC000000000000, 0x00F8000000000000, 0x00F0000000000000, 0x00E0000000000000, 0x00C0000000000000, 0x0080000000000000, 0x0000000000000000,
0xFE00000000000000, 0xFC00000000000000, 0xF800000000000000, 0xF000000000000000, 0xE000000000000000, 0xC000000000000000, 0x8000000000000000, 0x0000000000000000,
0x0000000000000000
};
HOST_DEVICE_LOOKUP uint64_t const_ve[] = {
0x0101010101010100, 0x0202020202020200, 0x0404040404040400, 0x0808080808080800, 0x1010101010101000, 0x2020202020202000, 0x4040404040404000, 0x8080808080808000,
0x0101010101010000, 0x0202020202020000, 0x0404040404040000, 0x0808080808080000, 0x1010101010100000, 0x2020202020200000, 0x4040404040400000, 0x8080808080800000,
0x0101010101000000, 0x0202020202000000, 0x0404040404000000, 0x0808080808000000, 0x1010101010000000, 0x2020202020000000, 0x4040404040000000, 0x8080808080000000,
0x0101010100000000, 0x0202020200000000, 0x0404040400000000, 0x0808080800000000, 0x1010101000000000, 0x2020202000000000, 0x4040404000000000, 0x8080808000000000,
0x0101010000000000, 0x0202020000000000, 0x0404040000000000, 0x0808080000000000, 0x1010100000000000, 0x2020200000000000, 0x4040400000000000, 0x8080800000000000,
0x0101000000000000, 0x0202000000000000, 0x0404000000000000, 0x0808000000000000, 0x1010000000000000, 0x2020000000000000, 0x4040000000000000, 0x8080000000000000,
0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000,
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
0x0000000000000000
};
HOST_DEVICE_LOOKUP uint64_t const_d1[] = {
0x8040201008040200, 0x0080402010080400, 0x0000804020100800, 0x0000008040201000, 0x0000000080402000, 0x0000000000804000, 0x0000000000008000, 0x0000000000000000,
0x4020100804020000, 0x8040201008040000, 0x0080402010080000, 0x0000804020100000, 0x0000008040200000, 0x0000000080400000, 0x0000000000800000, 0x0000000000000000,
0x2010080402000000, 0x4020100804000000, 0x8040201008000000, 0x0080402010000000, 0x0000804020000000, 0x0000008040000000, 0x0000000080000000, 0x0000000000000000,
0x1008040200000000, 0x2010080400000000, 0x4020100800000000, 0x8040201000000000, 0x0080402000000000, 0x0000804000000000, 0x0000008000000000, 0x0000000000000000,
0x0804020000000000, 0x1008040000000000, 0x2010080000000000, 0x4020100000000000, 0x8040200000000000, 0x0080400000000000, 0x0000800000000000, 0x0000000000000000,
0x0402000000000000, 0x0804000000000000, 0x1008000000000000, 0x2010000000000000, 0x4020000000000000, 0x8040000000000000, 0x0080000000000000, 0x0000000000000000,
0x0200000000000000, 0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
0x0000000000000000
};
HOST_DEVICE_LOOKUP uint64_t const_d2[] = {
0x0000000000000000, 0x0000000000000100, 0x0000000000010200, 0x0000000001020400, 0x0000000102040800, 0x0000010204081000, 0x0001020408102000, 0x0102040810204000,
0x0000000000000000, 0x0000000000010000, 0x0000000001020000, 0x0000000102040000, 0x0000010204080000, 0x0001020408100000, 0x0102040810200000, 0x0204081020400000,
0x0000000000000000, 0x0000000001000000, 0x0000000102000000, 0x0000010204000000, 0x0001020408000000, 0x0102040810000000, 0x0204081020000000, 0x0408102040000000,
0x0000000000000000, 0x0000000100000000, 0x0000010200000000, 0x0001020400000000, 0x0102040800000000, 0x0204081000000000, 0x0408102000000000, 0x0810204000000000,
0x0000000000000000, 0x0000010000000000, 0x0001020000000000, 0x0102040000000000, 0x0204080000000000, 0x0408100000000000, 0x0810200000000000, 0x1020400000000000,
0x0000000000000000, 0x0001000000000000, 0x0102000000000000, 0x0204000000000000, 0x0408000000000000, 0x0810000000000000, 0x1020000000000000, 0x2040000000000000,
0x0000000000000000, 0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x0800000000000000, 0x1000000000000000, 0x2000000000000000, 0x4000000000000000,
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
0x0000000000000000
};
HOST_DEVICE_LOOKUP uint64_t const_HO[] = {
0x0000000000000000, 0x8000000000000000, 0xC000000000000000, 0xE000000000000000, 0xF000000000000000, 0xF800000000000000, 0xFC00000000000000, 0xFE00000000000000,
0x0000000000000000, 0x0080000000000000, 0x00C0000000000000, 0x00E0000000000000, 0x00F0000000000000, 0x00F8000000000000, 0x00FC000000000000, 0x00FE000000000000,
0x0000000000000000, 0x0000800000000000, 0x0000C00000000000, 0x0000E00000000000, 0x0000F00000000000, 0x0000F80000000000, 0x0000FC0000000000, 0x0000FE0000000000,
0x0000000000000000, 0x0000008000000000, 0x000000C000000000, 0x000000E000000000, 0x000000F000000000, 0x000000F800000000, 0x000000FC00000000, 0x000000FE00000000,
0x0000000000000000, 0x0000000080000000, 0x00000000C0000000, 0x00000000E0000000, 0x00000000F0000000, 0x00000000F8000000, 0x00000000FC000000, 0x00000000FE000000,
0x0000000000000000, 0x0000000000800000, 0x0000000000C00000, 0x0000000000E00000, 0x0000000000F00000, 0x0000000000F80000, 0x0000000000FC0000, 0x0000000000FE0000,
0x0000000000000000, 0x0000000000008000, 0x000000000000C000, 0x000000000000E000, 0x000000000000F000, 0x000000000000F800, 0x000000000000FC00, 0x000000000000FE00,
0x0000000000000000, 0x0000000000000080, 0x00000000000000C0, 0x00000000000000E0, 0x00000000000000F0, 0x00000000000000F8, 0x00000000000000FC, 0x00000000000000FE,
0x0000000000000000
};
HOST_DEVICE_LOOKUP uint64_t const_VE[] = {
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
0x8000000000000000, 0x4000000000000000, 0x2000000000000000, 0x1000000000000000, 0x0800000000000000, 0x0400000000000000, 0x0200000000000000, 0x0100000000000000,
0x8080000000000000, 0x4040000000000000, 0x2020000000000000, 0x1010000000000000, 0x0808000000000000, 0x0404000000000000, 0x0202000000000000, 0x0101000000000000,
0x8080800000000000, 0x4040400000000000, 0x2020200000000000, 0x1010100000000000, 0x0808080000000000, 0x0404040000000000, 0x0202020000000000, 0x0101010000000000,
0x8080808000000000, 0x4040404000000000, 0x2020202000000000, 0x1010101000000000, 0x0808080800000000, 0x0404040400000000, 0x0202020200000000, 0x0101010100000000,
0x8080808080000000, 0x4040404040000000, 0x2020202020000000, 0x1010101010000000, 0x0808080808000000, 0x0404040404000000, 0x0202020202000000, 0x0101010101000000,
0x8080808080800000, 0x4040404040400000, 0x2020202020200000, 0x1010101010100000, 0x0808080808080000, 0x0404040404040000, 0x0202020202020000, 0x0101010101010000,
0x8080808080808000, 0x4040404040404000, 0x2020202020202000, 0x1010101010101000, 0x0808080808080800, 0x0404040404040400, 0x0202020202020200, 0x0101010101010100,
0x0000000000000000
};
HOST_DEVICE_LOOKUP uint64_t const_D1[] = {
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
0x0000000000000000, 0x8000000000000000, 0x4000000000000000, 0x2000000000000000, 0x1000000000000000, 0x0800000000000000, 0x0400000000000000, 0x0200000000000000,
0x0000000000000000, 0x0080000000000000, 0x8040000000000000, 0x4020000000000000, 0x2010000000000000, 0x1008000000000000, 0x0804000000000000, 0x0402000000000000,
0x0000000000000000, 0x0000800000000000, 0x0080400000000000, 0x8040200000000000, 0x4020100000000000, 0x2010080000000000, 0x1008040000000000, 0x0804020000000000,
0x0000000000000000, 0x0000008000000000, 0x0000804000000000, 0x0080402000000000, 0x8040201000000000, 0x4020100800000000, 0x2010080400000000, 0x1008040200000000,
0x0000000000000000, 0x0000000080000000, 0x0000008040000000, 0x0000804020000000, 0x0080402010000000, 0x8040201008000000, 0x4020100804000000, 0x2010080402000000,
0x0000000000000000, 0x0000000000800000, 0x0000000080400000, 0x0000008040200000, 0x0000804020100000, 0x0080402010080000, 0x8040201008040000, 0x4020100804020000,
0x0000000000000000, 0x0000000000008000, 0x0000000000804000, 0x0000000080402000, 0x0000008040201000, 0x0000804020100800, 0x0080402010080400, 0x8040201008040200,
0x0000000000000000
};
HOST_DEVICE_LOOKUP uint64_t const_D2[] = {
0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
0x4000000000000000, 0x2000000000000000, 0x1000000000000000, 0x0800000000000000, 0x0400000000000000, 0x0200000000000000, 0x0100000000000000, 0x0000000000000000,
0x2040000000000000, 0x1020000000000000, 0x0810000000000000, 0x0408000000000000, 0x0204000000000000, 0x0102000000000000, 0x0001000000000000, 0x0000000000000000,
0x1020400000000000, 0x0810200000000000, 0x0408100000000000, 0x0204080000000000, 0x0102040000000000, 0x0001020000000000, 0x0000010000000000, 0x0000000000000000,
0x0810204000000000, 0x0408102000000000, 0x0204081000000000, 0x0102040800000000, 0x0001020400000000, 0x0000010200000000, 0x0000000100000000, 0x0000000000000000,
0x0408102040000000, 0x0204081020000000, 0x0102040810000000, 0x0001020408000000, 0x0000010204000000, 0x0000000102000000, 0x0000000001000000, 0x0000000000000000,
0x0204081020400000, 0x0102040810200000, 0x0001020408100000, 0x0000010204080000, 0x0000000102040000, 0x0000000001020000, 0x0000000000010000, 0x0000000000000000,
0x0102040810204000, 0x0001020408102000, 0x0000010204081000, 0x0000000102040800, 0x0000000001020400, 0x0000000000010200, 0x0000000000000100, 0x0000000000000000,
0x0000000000000000
};
__shared__ uint64_t shared_ve[65];
__shared__ uint64_t shared_d1[65];
__shared__ uint64_t shared_d2[65];
__shared__ uint64_t shared_VE[65];
__shared__ uint64_t shared_D1[65];
__shared__ uint64_t shared_D2[65];
__shared__ uint8_t rank_attack[512];
BitFunction ray(uint64_t occ, uint64_t mask, uint64_t OCC, uint64_t MASK) {
uint64_t o = occ & mask;
uint64_t O = OCC & MASK;
return ((o ^ (o - 1ull)) & mask) | bitswap((O ^ (O - 1ull)) & MASK);
}
BitFunction horizontal_attack(int x, uint64_t pieces) {
uint64_t o = (pieces >> (x & 56)) & 126;
return rank_attack[o * 4 + (x & 7)] << (x & 56);
}
BitFunction Ray_VE(int sq, uint64_t occ, uint64_t OCC) {
return ray(occ, shared_ve[sq], OCC, shared_VE[sq]);
}
BitFunction Ray_D1(int sq, uint64_t occ, uint64_t OCC) {
return ray(occ, shared_d1[sq], OCC, shared_D1[sq]);
}
BitFunction Ray_D2(int sq, uint64_t occ, uint64_t OCC) {
return ray(occ, shared_d2[sq], OCC, shared_D2[sq]);
}
BitFunction Queen(int sq, uint64_t occ) {
uint64_t OCC = bitswap(occ);
return horizontal_attack(occ, sq) |
Ray_VE(sq, occ, OCC) |
Ray_D1(sq, occ, OCC) |
Ray_D2(sq, occ, OCC);
}
__inline__ __device__ uint8_t InitRank(int sq) {
int x = sq / 8;
int f = sq % 8;
int o = 2 * x;
int x2{}, y2{};
int b{};
y2 = 0;
for (x2 = f - 1; x2 >= 0; --x2) {
b = 1 << x2;
y2 |= b;
if ((o & b) == b) break;
}
for (x2 = f + 1; x2 < 8; ++x2) {
b = 1 << x2;
y2 |= b;
if ((o & b) == b) break;
}
return y2;
}
__inline__ __device__ void Prepare(unsigned int Idx)
{
if (Idx < 64)
{
shared_ve[Idx] = const_ve[Idx]; shared_VE[Idx] = const_VE[Idx];
shared_d1[Idx] = const_d1[Idx]; shared_D1[Idx] = const_D1[Idx];
shared_d2[Idx] = const_d2[Idx]; shared_D2[Idx] = const_D2[Idx];
for (int i = 0; i < 8; i++)
{
int idx = 8 * Idx + i;
rank_attack[idx] = InitRank(idx);
}
}
__syncthreads();
}
}