go_engine.c

filter: Statementstmt 97.2% 597 / 614 Branchbran 77.4% 491 / 634 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.1% 1088 / 1248 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Go.xs ↑ Summary go_search.c »
1 /* go_engine.c - the Go board, the chains, the liberties, and the zobrist hash.
2 *
3 * PERL-FREE. This file must never include perl.h, XSUB.h or stdio.h, and must
4 * never call rand, time, printf or anything that touches a handle. t/24-no-io.t
5 * scans this source for all of those after stripping comments, so a decimal
6 * number is formatted by hand below rather than with sprintf.
7 *
8 * What this file owns is in include/go_abi.h under "what this header covers,
9 * and what it does not": the board and only the board. Nothing here knows
10 * whether a move was legal.
11 */
12  
13 #include <stdlib.h>
14 #include <string.h>
15  
16 #include "go_abi.h"
17  
18 #define M32(x) ((x) & 0xFFFFFFFFu)
19  
20 /* ------------------------------------------------------------------ SHA-256 --
21 *
22 * Here for one reason: the zobrist table is derived from SHA-256 so that it is
23 * identical on every platform and every build, and so that a test can verify
24 * any entry from Perl with Digest::SHA. Deriving it from a PRNG would make the
25 * hash values a build artifact and put an unverifiable number in the middle of
26 * the legality path.
27 *
28 * Only ever called with a message shorter than 56 bytes (the longest label is
29 * "go-zobrist:2:440", sixteen characters), so this is the single-block case
30 * and needs no streaming state.
31 */
32  
33 static const unsigned int SHA_K[64] = {
34 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u,
35 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
36 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u,
37 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
38 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu,
39 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
40 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
41 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
42 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u,
43 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
44 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u,
45 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
46 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u,
47 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
48 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
49 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u
50 };
51  
52 #define ROR32(x, n) (M32(((x) >> (n)) | ((x) << (32 - (n)))))
53  
54 24696 static void sha256_short(const unsigned char *msg, int len, unsigned char out[32])
55 {
56 unsigned int h[8];
57 unsigned int w[64];
58 unsigned char blk[64];
59 int i;
60  
61 24696 h[0] = 0x6a09e667u; h[1] = 0xbb67ae85u; h[2] = 0x3c6ef372u; h[3] = 0xa54ff53au;
62 24696 h[4] = 0x510e527fu; h[5] = 0x9b05688cu; h[6] = 0x1f83d9abu; h[7] = 0x5be0cd19u;
63  
64 24696 memset(blk, 0, sizeof(blk));
65 24696 memcpy(blk, msg, (size_t)len);
66 24696 blk[len] = 0x80;
67 /* the message is short, so the length in bits fits the last two bytes */
68 24696 blk[62] = (unsigned char)(((unsigned)len * 8u) >> 8);
69 24696 blk[63] = (unsigned char)(((unsigned)len * 8u) & 0xffu);
70  
71 419832 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 395136 24696 419832
72 395136 w[i] = ((unsigned int)blk[i * 4] << 24) | ((unsigned int)blk[i * 4 + 1] << 16)
73 395136 | ((unsigned int)blk[i * 4 + 2] << 8) | (unsigned int)blk[i * 4 + 3];
74 1210104 ▶ for (i = 16; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 16; i < 64; i++) { 1185408 24696 1210104
75 1185408 unsigned int s0 = ROR32(w[i - 15], 7) ^ ROR32(w[i - 15], 18) ^ M32(w[i - 15] >> 3);
76 1185408 unsigned int s1 = ROR32(w[i - 2], 17) ^ ROR32(w[i - 2], 19) ^ M32(w[i - 2] >> 10);
77 1185408 w[i] = M32(w[i - 16] + s0 + w[i - 7] + s1);
78 }
79  
80 {
81 24696 unsigned int a = h[0], b = h[1], c = h[2], d = h[3];
82 24696 unsigned int e = h[4], f = h[5], g = h[6], hh = h[7];
83 1605240 ▶ for (i = 0; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 64; i++) { 1580544 24696 1605240
84 1580544 unsigned int S1 = ROR32(e, 6) ^ ROR32(e, 11) ^ ROR32(e, 25);
85 1580544 unsigned int ch = (e & f) ^ ((~e) & g);
86 1580544 unsigned int t1 = M32(hh + S1 + ch + SHA_K[i] + w[i]);
87 1580544 unsigned int S0 = ROR32(a, 2) ^ ROR32(a, 13) ^ ROR32(a, 22);
88 1580544 unsigned int mj = (a & b) ^ (a & c) ^ (b & c);
89 1580544 unsigned int t2 = M32(S0 + mj);
90 1580544 hh = g; g = f; f = e; e = M32(d + t1);
91 1580544 d = c; c = b; b = a; a = M32(t1 + t2);
92 }
93 24696 h[0] = M32(h[0] + a); h[1] = M32(h[1] + b); h[2] = M32(h[2] + c);
94 24696 h[3] = M32(h[3] + d); h[4] = M32(h[4] + e); h[5] = M32(h[5] + f);
95 24696 h[6] = M32(h[6] + g); h[7] = M32(h[7] + hh);
96 }
97  
98 222264 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 197568 24696 222264
99 197568 out[i * 4] = (unsigned char)((h[i] >> 24) & 0xffu);
100 197568 out[i * 4 + 1] = (unsigned char)((h[i] >> 16) & 0xffu);
101 197568 out[i * 4 + 2] = (unsigned char)((h[i] >> 8) & 0xffu);
102 197568 out[i * 4 + 3] = (unsigned char)(h[i] & 0xffu);
103 }
104 24696 }
105  
106 /* ------------------------------------------------------------ the zobrist ---- */
107  
108 static unsigned long long ZOB[4][GO_MAX_PTS];
109 static int ZOB_READY = 0;
110  
111 /* Decimal by hand, because sprintf would mean stdio.h and t/24-no-io.t scans
112 * for it. Returns the number of characters written. */
113 49392 static int put_dec(unsigned char *at, int n)
114 {
115 unsigned char tmp[12];
116 49392 int i = 0, j = 0;
117 49392 ▶ if (n == 0) { at[0] = '0'; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { at[0] = '0'; return 1; } 56 49336 49392
118 141904 ▶ while (n > 0) { tmp[i++] = (unsigned char)('0' + (n % 10)); n /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (n > 0) { tmp[i++] = (unsigned char)('0' + (n % 10)); n /= 10; } 92568 49336 141904
119 141904 ▶ while (i > 0) at[j++] = tmp[--i];
Branch: 1 branch
BranchTrueFalseTotal
while (i > 0) at[j++] = tmp[--i]; 92568 49336 141904
120 49336 return j;
121 }
122  
123 /* The label whose SHA-256 is the entry: go-zobrist:<colour>:<point>
124 * A test asserts this exactly, so the spelling is part of the ABI. */
125 24696 static int zob_label(unsigned char *buf, int colour, int pt)
126 {
127 static const char pre[] = "go-zobrist:";
128 24696 int n = (int)(sizeof(pre) - 1);
129 24696 memcpy(buf, pre, (size_t)n);
130 24696 n += put_dec(buf + n, colour);
131 24696 buf[n++] = ':';
132 24696 n += put_dec(buf + n, pt);
133 24696 return n;
134 }
135  
136 /* Deterministic, so the benign race of two threads building it at once writes
137 * identical bytes. That is the whole reason it is derived rather than random. */
138 9337755 static void zob_init(void)
139 {
140 unsigned char msg[64], dig[32];
141 int colour, pt, i;
142  
143 9337755 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 9337727 28 9337755
144 84 ▶ for (colour = GO_BLACK; colour <= GO_WHITE; colour++) {
Branch: 1 branch
BranchTrueFalseTotal
for (colour = GO_BLACK; colour <= GO_WHITE; colour++) { 56 28 84
145 24752 ▶ for (pt = 0; pt < GO_MAX_PTS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < GO_MAX_PTS; pt++) { 24696 56 24752
146 24696 unsigned long long v = 0;
147 24696 int len = zob_label(msg, colour, pt);
148 24696 sha256_short(msg, len, dig);
149 222264 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 197568 24696 222264
150 197568 v = (v << 8) | (unsigned long long)dig[i];
151 24696 ZOB[colour][pt] = v;
152 }
153 }
154 28 ZOB_READY = 1;
155 }
156  
157 9337100 static unsigned long long zob(int colour, int pt)
158 {
159 9337100 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 4885562 4451538 9337100
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 2 4885560 4885562
160 9337098 ▶ if (pt < 0 || pt >= GO_MAX_PTS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= GO_MAX_PTS) return 0; 9337098 0 9337098
if (pt < 0 || pt >= GO_MAX_PTS) return 0; 0 9337098 9337098
161 9337098 zob_init();
162 9337098 return ZOB[colour][pt];
163 }
164  
165 1797 static unsigned long long go_zobrist(int colour, int pt) { return zob(colour, pt); }
166  
167 /* ------------------------------------------------------------- the board ----- */
168  
169 657 static struct go_board *go_board_new(int size)
170 {
171 struct go_board *b;
172 int r, c;
173  
174 657 ▶ if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL; 657 0 657
if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL; 0 657 657
175 657 zob_init();
176  
177 657 b = (struct go_board *)calloc(1, sizeof(struct go_board));
178 657 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 657 657
179  
180 657 b->size = size;
181 657 b->stride = size + 2;
182 657 b->pts = b->stride * b->stride;
183 657 b->nbr[0] = -1;
184 657 b->nbr[1] = 1;
185 657 b->nbr[2] = -b->stride;
186 657 b->nbr[3] = b->stride;
187  
188 /* -1 and not 0: 0 is a real index into the padded array (the top left of
189 * the ring), so 0 as "no ko" would forbid a point that exists. */
190 657 b->ko_point = -1;
191 657 b->ko_colour = GO_EMPTY;
192 657 b->superko_on = 1;
193  
194 /* GO_EMPTY is 0 and calloc zeroed the interior, so only the ring is set.
195 * The ring is the only thing making the neighbour arithmetic safe. */
196 8410 ▶ for (r = 0; r < b->stride; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->stride; r++) { 7753 657 8410
197 103840 ▶ for (c = 0; c < b->stride; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->stride; c++) { 96087 7753 103840
198 96087 ▶ if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1)
Branch: 4 branches
BranchTrueFalseTotal
if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1) 88334 7753 96087
if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1) 80581 7753 88334
if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1) 74142 6439 80581
if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1) 6439 67703 74142
199 28384 b->colour[r * b->stride + c] = GO_BORDER;
200 }
201 }
202 657 return b;
203 }
204  
205 79028 ▶static void go_board_drop(struct go_board *b) { if (b) free(b); }
Branch: 1 branch
BranchTrueFalseTotal
static void go_board_drop(struct go_board *b) { if (b) free(b); } 79028 0 79028
206  
207 78371 static struct go_board *go_board_copy(const struct go_board *b)
208 {
209 struct go_board *n;
210 78371 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 78371 78371
211 78371 n = (struct go_board *)malloc(sizeof(struct go_board));
212 78371 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 78371 78371
213 /* No pointers in the struct, so a copy is a flat byte copy and the two
214 * boards share nothing at all. */
215 78371 memcpy(n, b, sizeof(struct go_board));
216 78371 return n;
217 }
218  
219 /* ------------------------------------------------------------- the chains ---- */
220  
221 /* A stamp rather than a clear, so the distinct-liberty count is O(chain).
222 * The wrap is handled rather than ignored: a gate run does hundreds of
223 * millions of counts and a wrapped stamp would silently collide with a stale
224 * mark, undercounting a liberty in one position in a way nothing reports. */
225 14836763 static int next_stamp(struct go_board *b)
226 {
227 14836763 ▶ if (b->mark_stamp >= 0x7ffffffe) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->mark_stamp >= 0x7ffffffe) { 0 14836763 14836763
228 0 memset(b->mark, 0, sizeof(b->mark));
229 0 b->mark_stamp = 0;
230 }
231 14836763 return ++b->mark_stamp;
232 }
233  
234 /* EXACT, and distinctness is the whole point: an empty point adjacent to two
235 * stones of the same chain is ONE liberty. Counting occurrences instead is the
236 * bug the liberty-delta shortcut has, and phase 01 chose not to have it. */
237 14836763 static void recount(struct go_board *b, int root)
238 {
239 14836763 int stamp, n = 0, pt;
240 14836763 ▶ if (root < 0 || root >= b->pts) return;
Branch: 2 branches
BranchTrueFalseTotal
if (root < 0 || root >= b->pts) return; 14836763 0 14836763
if (root < 0 || root >= b->pts) return; 0 14836763 14836763
241 14836763 ▶ if (b->colour[root] != GO_BLACK && b->colour[root] != GO_WHITE) return;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[root] != GO_BLACK && b->colour[root] != GO_WHITE) return; 7315714 7521049 14836763
if (b->colour[root] != GO_BLACK && b->colour[root] != GO_WHITE) return; 0 7315714 7315714
242  
243 14836763 stamp = next_stamp(b);
244 14836763 pt = root;
245 do {
246 int i;
247 1249875160 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 999900128 249975032 1249875160
248 999900128 int q = pt + b->nbr[i];
249 999900128 ▶ if (b->colour[q] == GO_EMPTY && b->mark[q] != stamp) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[q] == GO_EMPTY && b->mark[q] != stamp) { 149885806 850014322 999900128
if (b->colour[q] == GO_EMPTY && b->mark[q] != stamp) { 70972527 78913279 149885806
250 70972527 b->mark[q] = stamp;
251 70972527 n++;
252 }
253 }
254 249975032 pt = b->chain_next[pt];
255 249975032 ▶ } while (pt != root);
Branch: 1 branch
BranchTrueFalseTotal
} while (pt != root); 235138269 14836763 249975032
256  
257 14836763 b->chain_libs[root] = n;
258 }
259  
260 /* Relabel the SMALLER chain into the larger, which keeps chain_root a direct
261 * answer rather than a tree needing path compression, and makes the work
262 * O(smaller). Splicing two circular lists is two assignments. */
263 7447325 static int merge(struct go_board *b, int ra, int rb)
264 {
265 int big, small, pt, tmp;
266  
267 7447325 ▶ if (ra == rb) return ra;
Branch: 1 branch
BranchTrueFalseTotal
if (ra == rb) return ra; 2024541 5422784 7447325
268 5422784 ▶ if (b->chain_size[ra] >= b->chain_size[rb]) { big = ra; small = rb; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_size[ra] >= b->chain_size[rb]) { big = ra; small = rb; } 2184874 3237910 5422784
269 3237910 else { big = rb; small = ra; }
270  
271 5422784 pt = small;
272 7686169 ▶ do { b->chain_root[pt] = big; pt = b->chain_next[pt]; } while (pt != small);
Branch: 1 branch
BranchTrueFalseTotal
do { b->chain_root[pt] = big; pt = b->chain_next[pt]; } while (pt != small); 2263385 5422784 7686169
273  
274 5422784 tmp = b->chain_next[big];
275 5422784 b->chain_next[big] = b->chain_next[small];
276 5422784 b->chain_next[small] = tmp;
277  
278 5422784 b->chain_size[big] += b->chain_size[small];
279 5422784 b->chain_size[small] = 0;
280 5422784 b->chain_libs[small] = 0;
281 5422784 return big;
282 }
283  
284 /* A STRUCTURE PRIMITIVE. It does not check legality, does not capture, and does
285 * not know whose turn it is. An occupied or off-board point is a no-op rather
286 * than a corruption, because the rules layer is what refuses those and it
287 * should be the thing reporting the reason. */
288 6225526 static void go_put(struct go_board *b, int pt, int colour)
289 {
290 int i, root;
291  
292 6225526 ▶ if (!b || pt < 0 || pt >= b->pts) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return; 6225526 0 6225526
if (!b || pt < 0 || pt >= b->pts) return; 6225526 0 6225526
if (!b || pt < 0 || pt >= b->pts) return; 0 6225526 6225526
293 6225526 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return; 3076863 3148663 6225526
if (colour != GO_BLACK && colour != GO_WHITE) return; 0 3076863 3076863
294 6225526 ▶ if (b->colour[pt] != GO_EMPTY) return; /* border is never empty, so this covers it */
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[pt] != GO_EMPTY) return; /* border is never empty, so this covers it */ 2 6225524 6225526
295  
296 6225524 b->colour[pt] = (signed char)colour;
297 6225524 b->hash ^= zob(colour, pt);
298 6225524 b->stones[colour]++;
299  
300 6225524 b->chain_root[pt] = pt;
301 6225524 b->chain_next[pt] = pt;
302 6225524 b->chain_size[pt] = 1;
303 6225524 b->chain_libs[pt] = 0;
304  
305 6225524 root = pt;
306 31127620 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 24902096 6225524 31127620
307 24902096 int q = pt + b->nbr[i];
308 24902096 ▶ if (b->colour[q] == colour) root = merge(b, root, b->chain_root[q]);
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == colour) root = merge(b, root, b->chain_root[q]); 7447325 17454771 24902096
309 }
310 6225524 recount(b, root);
311  
312 /* every adjacent enemy chain just lost a liberty. Recounting the same root
313 * twice is idempotent because the count is exact, so no bookkeeping. */
314 31127620 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 24902096 6225524 31127620
315 24902096 int q = pt + b->nbr[i];
316 24902096 ▶ if (b->colour[q] == GO_OTHER(colour)) recount(b, b->chain_root[q]);
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[q] == GO_OTHER(colour)) recount(b, b->chain_root[q]); 12594648 12307448 24902096
if (b->colour[q] == GO_OTHER(colour)) recount(b, b->chain_root[q]); 6573463 18328633 24902096
317 }
318 }
319  
320 /* The other structure primitive: take the chain at pt off the board. Says
321 * nothing about why. Returns the number of stones removed. */
322 1300015 static int go_lift(struct go_board *b, int pt)
323 {
324 int pts[GO_MAX_PTS];
325 int roots[GO_MAX_PTS];
326 1300015 int n = 0, nroots = 0, i, k, root, cur, colour;
327  
328 1300015 ▶ if (!b || pt < 0 || pt >= b->pts) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return 0; 1300015 0 1300015
if (!b || pt < 0 || pt >= b->pts) return 0; 1300015 0 1300015
if (!b || pt < 0 || pt >= b->pts) return 0; 0 1300015 1300015
329 1300015 colour = b->colour[pt];
330 1300015 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 722868 577147 1300015
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 0 722868 722868
331  
332 1300015 root = b->chain_root[pt];
333 1300015 cur = root;
334 3049847 ▶ do { pts[n++] = cur; cur = b->chain_next[cur]; } while (cur != root);
Branch: 1 branch
BranchTrueFalseTotal
do { pts[n++] = cur; cur = b->chain_next[cur]; } while (cur != root); 1749832 1300015 3049847
335  
336 /* Collect the distinct neighbouring enemy chains BEFORE the stones go,
337 * because once they are empty the adjacency that named them is gone. */
338 4349862 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3049847 1300015 4349862
339 15249235 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 12199388 3049847 15249235
340 12199388 int q = pts[i] + b->nbr[k];
341 12199388 ▶ if (b->colour[q] == GO_OTHER(colour)) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[q] == GO_OTHER(colour)) { 5115100 7084288 12199388
if (b->colour[q] == GO_OTHER(colour)) { 6523333 5676055 12199388
342 6523333 int r = b->chain_root[q], j, seen = 0;
343 8420283 ▶ for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 4485557 1896950 6382507
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 6382507 2037776 8420283
344 6523333 ▶ if (!seen) roots[nroots++] = r;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) roots[nroots++] = r; 2037776 4485557 6523333
345 }
346 }
347 }
348  
349 4349862 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3049847 1300015 4349862
350 3049847 b->colour[pts[i]] = GO_EMPTY;
351 3049847 b->hash ^= zob(colour, pts[i]);
352 3049847 b->chain_root[pts[i]] = 0;
353 3049847 b->chain_next[pts[i]] = 0;
354 3049847 b->chain_size[pts[i]] = 0;
355 3049847 b->chain_libs[pts[i]] = 0;
356 }
357 1300015 b->stones[colour] -= n;
358  
359 3337791 ▶ for (i = 0; i < nroots; i++) recount(b, roots[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nroots; i++) recount(b, roots[i]); 2037776 1300015 3337791
360 1300015 return n;
361 }
362  
363 /* ----------------------------------------------------------- reading it ------ */
364  
365 700149328 static int go_at(const struct go_board *b, int pt)
366 {
367 700149328 ▶ if (!b || pt < 0 || pt >= b->pts) return GO_BORDER;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return GO_BORDER; 700149328 0 700149328
if (!b || pt < 0 || pt >= b->pts) return GO_BORDER; 700149327 1 700149328
if (!b || pt < 0 || pt >= b->pts) return GO_BORDER; 1 700149326 700149327
368 700149326 return b->colour[pt];
369 }
370  
371 298 static int go_libs(const struct go_board *b, int pt)
372 {
373 298 ▶ if (!b || pt < 0 || pt >= b->pts) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return -1; 298 0 298
if (!b || pt < 0 || pt >= b->pts) return -1; 298 0 298
if (!b || pt < 0 || pt >= b->pts) return -1; 0 298 298
374 298 ▶ if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return -1; 97 201 298
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return -1; 1 96 97
375 297 return b->chain_libs[b->chain_root[pt]];
376 }
377  
378 636 static int go_chain_size(const struct go_board *b, int pt)
379 {
380 636 ▶ if (!b || pt < 0 || pt >= b->pts) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return 0; 636 0 636
if (!b || pt < 0 || pt >= b->pts) return 0; 636 0 636
if (!b || pt < 0 || pt >= b->pts) return 0; 0 636 636
381 636 ▶ if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return 0; 219 417 636
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return 0; 1 218 219
382 635 return b->chain_size[b->chain_root[pt]];
383 }
384  
385 3255 static int go_chain_at(const struct go_board *b, int pt, int *out)
386 {
387 3255 int n = 0, root, cur;
388 3255 ▶ if (!b || pt < 0 || pt >= b->pts) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return 0; 3255 0 3255
if (!b || pt < 0 || pt >= b->pts) return 0; 3255 0 3255
if (!b || pt < 0 || pt >= b->pts) return 0; 0 3255 3255
389 3255 ▶ if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return 0; 693 2562 3255
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return 0; 3 690 693
390 3252 root = b->chain_root[pt];
391 3252 cur = root;
392 170922 ▶ do { if (out) out[n] = cur; n++; cur = b->chain_next[cur]; } while (cur != root);
Branch: 2 branches
BranchTrueFalseTotal
do { if (out) out[n] = cur; n++; cur = b->chain_next[cur]; } while (cur != root); 170922 0 170922
do { if (out) out[n] = cur; n++; cur = b->chain_next[cur]; } while (cur != root); 167670 3252 170922
393 3252 return n;
394 }
395  
396 /* One byte per REAL point, row by row from the top. This is what makes a
397 * zobrist match into a certainty: on a hash hit the caller memcmps this. */
398 5836 static int go_pack(const struct go_board *b, unsigned char *buf)
399 {
400 5836 int r, c, n = 0;
401 5836 ▶ if (!b || !buf) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !buf) return 0; 5836 0 5836
if (!b || !buf) return 0; 0 5836 5836
402 59354 ▶ for (r = 0; r < b->size; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) 53518 5836 59354
403 553750 ▶ for (c = 0; c < b->size; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) 500232 53518 553750
404 500232 buf[n++] = (unsigned char)b->colour[(r + 1) * b->stride + (c + 1)];
405 5836 return n;
406 }
407  
408 29 ▶static unsigned long long go_hash(const struct go_board *b) { return b ? b->hash : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static unsigned long long go_hash(const struct go_board *b) { return b ? b->hash : 0; } 29 0 29
409  
410 72 static int go_stones(const struct go_board *b, int colour)
411 {
412 72 ▶ if (!b || colour < 0 || colour > 3) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || colour < 0 || colour > 3) return 0; 72 0 72
if (!b || colour < 0 || colour > 3) return 0; 72 0 72
if (!b || colour < 0 || colour > 3) return 0; 0 72 72
413 72 return b->stones[colour];
414 }
415  
416 /* ---------------------------------------------------------------- points ----- */
417  
418 699690427 static int go_point_of(const struct go_board *b, int col, int row)
419 {
420 699690427 ▶ if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1;
Branch: 5 branches
BranchTrueFalseTotal
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 699690427 0 699690427
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 699690421 6 699690427
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 699690420 1 699690421
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 699690415 5 699690420
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 4 699690411 699690415
421 699690411 return (row + 1) * b->stride + (col + 1);
422 }
423  
424 4322 static int go_col_of(const struct go_board *b, int pt)
425 {
426 4322 ▶ if (!b || pt < 0 || pt >= b->pts) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return -1; 4322 0 4322
if (!b || pt < 0 || pt >= b->pts) return -1; 4322 0 4322
if (!b || pt < 0 || pt >= b->pts) return -1; 0 4322 4322
427 4322 return pt % b->stride - 1;
428 }
429  
430 4322 static int go_row_of(const struct go_board *b, int pt)
431 {
432 4322 ▶ if (!b || pt < 0 || pt >= b->pts) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return -1; 4322 0 4322
if (!b || pt < 0 || pt >= b->pts) return -1; 4322 0 4322
if (!b || pt < 0 || pt >= b->pts) return -1; 0 4322 4322
433 4322 return pt / b->stride - 1;
434 }
435  
436 76182 ▶static int go_size_of(const struct go_board *b) { return b ? b->size : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int go_size_of(const struct go_board *b) { return b ? b->size : 0; } 76182 0 76182
437 103 ▶static int go_stride_of(const struct go_board *b) { return b ? b->stride : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int go_stride_of(const struct go_board *b) { return b ? b->stride : 0; } 103 0 103
438  
439 /* The index into a packed position of a padded point, or -1 for the ring. */
440 4 static int buf_index(const struct go_board *b, int pt)
441 {
442 int row, col;
443 4 ▶ if (!b || pt < 0 || pt >= b->pts) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return -1; 4 0 4
if (!b || pt < 0 || pt >= b->pts) return -1; 4 0 4
if (!b || pt < 0 || pt >= b->pts) return -1; 0 4 4
444 4 row = pt / b->stride - 1;
445 4 col = pt % b->stride - 1;
446 4 ▶ if (row < 0 || col < 0 || row >= b->size || col >= b->size) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (row < 0 || col < 0 || row >= b->size || col >= b->size) return -1; 4 0 4
if (row < 0 || col < 0 || row >= b->size || col >= b->size) return -1; 4 0 4
if (row < 0 || col < 0 || row >= b->size || col >= b->size) return -1; 4 0 4
if (row < 0 || col < 0 || row >= b->size || col >= b->size) return -1; 0 4 4
447 4 return row * b->size + col;
448 }
449  
450 /* ==========================================================================
451 * THE RULES OF PLAY. Everything above this line judges nothing.
452 * ========================================================================== */
453  
454 /* ------------------------------------------------- the superko history ------
455 *
456 * A separate object, not a member of the board, and include/go_abi.h says why:
457 * a board carrying its own history would make every playout copy 110 KB.
458 */
459  
460 struct go_hist {
461 int n;
462 int cap;
463 int pts; /* bytes per position: size * size */
464 unsigned long long *hash;
465 unsigned char *pos;
466 };
467  
468 650 static struct go_hist *go_hist_new(int size)
469 {
470 struct go_hist *h;
471 650 ▶ if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL; 650 0 650
if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL; 0 650 650
472  
473 650 h = (struct go_hist *)calloc(1, sizeof(struct go_hist));
474 650 ▶ if (!h) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return NULL; 0 650 650
475  
476 650 h->pts = size * size;
477 650 h->cap = 64;
478 650 h->hash = (unsigned long long *)malloc(sizeof(unsigned long long) * (size_t)h->cap);
479 650 h->pos = (unsigned char *)malloc((size_t)h->cap * (size_t)h->pts);
480 650 ▶ if (!h->hash || !h->pos) {
Branch: 2 branches
BranchTrueFalseTotal
if (!h->hash || !h->pos) { 650 0 650
if (!h->hash || !h->pos) { 0 650 650
481 0 ▶ if (h->hash) free(h->hash);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hash) free(h->hash); 0 0 0
482 0 ▶ if (h->pos) free(h->pos);
Branch: 1 branch
BranchTrueFalseTotal
if (h->pos) free(h->pos); 0 0 0
483 0 free(h);
484 0 return NULL;
485 }
486 650 return h;
487 }
488  
489 652 static void go_hist_drop(struct go_hist *h)
490 {
491 652 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 652 652
492 652 ▶ if (h->hash) free(h->hash);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hash) free(h->hash); 652 0 652
493 652 ▶ if (h->pos) free(h->pos);
Branch: 1 branch
BranchTrueFalseTotal
if (h->pos) free(h->pos); 652 0 652
494 652 free(h);
495 }
496  
497 /* A cloned board must get its own history with the same contents. Sharing one
498 * would mean a move explored down one line refusing a move in the other, and
499 * truncating one to just the current position would silently turn superko off
500 * for the clone. Both failures look like one inexplicable refusal much later. */
501 2 static struct go_hist *go_hist_copy(const struct go_hist *h)
502 {
503 struct go_hist *n;
504 2 ▶ if (!h) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return NULL; 0 2 2
505  
506 2 n = (struct go_hist *)calloc(1, sizeof(struct go_hist));
507 2 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 2 2
508  
509 2 n->pts = h->pts;
510 2 n->cap = h->cap;
511 2 n->n = h->n;
512 2 n->hash = (unsigned long long *)malloc(sizeof(unsigned long long) * (size_t)n->cap);
513 2 n->pos = (unsigned char *)malloc((size_t)n->cap * (size_t)n->pts);
514 2 ▶ if (!n->hash || !n->pos) {
Branch: 2 branches
BranchTrueFalseTotal
if (!n->hash || !n->pos) { 2 0 2
if (!n->hash || !n->pos) { 0 2 2
515 0 ▶ if (n->hash) free(n->hash);
Branch: 1 branch
BranchTrueFalseTotal
if (n->hash) free(n->hash); 0 0 0
516 0 ▶ if (n->pos) free(n->pos);
Branch: 1 branch
BranchTrueFalseTotal
if (n->pos) free(n->pos); 0 0 0
517 0 free(n);
518 0 return NULL;
519 }
520 2 memcpy(n->hash, h->hash, sizeof(unsigned long long) * (size_t)h->n);
521 2 memcpy(n->pos, h->pos, (size_t)h->n * (size_t)h->pts);
522 2 return n;
523 }
524  
525 6 ▶static int go_hist_len(const struct go_hist *h) { return h ? h->n : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int go_hist_len(const struct go_hist *h) { return h ? h->n : 0; } 6 0 6
526 0 ▶static void go_hist_clear(struct go_hist *h) { if (h) h->n = 0; }
Branch: 1 branch
BranchTrueFalseTotal
static void go_hist_clear(struct go_hist *h) { if (h) h->n = 0; } 0 0 0
527  
528 29 static int hist_grow(struct go_hist *h)
529 {
530 unsigned long long *nh;
531 unsigned char *np;
532 29 int cap = h->cap * 2;
533  
534 29 nh = (unsigned long long *)realloc(h->hash, sizeof(unsigned long long) * (size_t)cap);
535 29 ▶ if (!nh) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nh) return 0; 0 29 29
536 29 h->hash = nh;
537  
538 29 np = (unsigned char *)realloc(h->pos, (size_t)cap * (size_t)h->pts);
539 29 ▶ if (!np) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!np) return 0; 0 29 29
540 29 h->pos = np;
541  
542 29 h->cap = cap;
543 29 return 1;
544 }
545  
546 5819 static int go_hist_push(struct go_hist *h, const struct go_board *b)
547 {
548 5819 ▶ if (!h || !b) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !b) return 0; 5819 0 5819
if (!h || !b) return 0; 0 5819 5819
549 5819 ▶ if (b->size * b->size != h->pts) return 0; /* a history is for one size */
Branch: 1 branch
BranchTrueFalseTotal
if (b->size * b->size != h->pts) return 0; /* a history is for one size */ 0 5819 5819
550 5819 ▶ if (h->n == h->cap && !hist_grow(h)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (h->n == h->cap && !hist_grow(h)) return 0; 29 5790 5819
if (h->n == h->cap && !hist_grow(h)) return 0; 0 29 29
551 5819 h->hash[h->n] = b->hash;
552 5819 (void)go_pack(b, h->pos + (size_t)h->n * (size_t)h->pts);
553 5819 h->n++;
554 5819 return 1;
555 }
556  
557 /* THE HASH IS A FILTER AND THE COMPARE IS THE VERDICT.
558 *
559 * A 64-bit collision across the few hundred positions of one game is somewhere
560 * around one in 10^14, and that is still not a reason to let a hash decide
561 * whether a move is legal: the symptom would be a legal move refused, once, in
562 * a game nobody can reproduce. So a hash match is followed by a memcmp of the
563 * packed position, and only an exact match refuses. The memcmp costs at most
564 * 361 bytes and happens only on a hit. */
565 4 static int hist_seen(const struct go_hist *h, unsigned long long hash,
566 const unsigned char *packed)
567 {
568 int i;
569 4 ▶ if (!h) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return 0; 0 4 4
570 8 ▶ for (i = 0; i < h->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->n; i++) { 8 0 8
571 8 ▶ if (h->hash[i] != hash) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (h->hash[i] != hash) continue; 4 4 8
572 4 ▶ if (memcmp(h->pos + (size_t)i * (size_t)h->pts, packed, (size_t)h->pts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(h->pos + (size_t)i * (size_t)h->pts, packed, (size_t)h->pts) == 0) 4 0 4
573 4 return 1;
574 }
575 0 return 0;
576 }
577  
578 2 static int go_hist_has(const struct go_hist *h, const struct go_board *b)
579 {
580 unsigned char packed[GO_MAX_PTS];
581 2 ▶ if (!h || !b) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !b) return 0; 2 0 2
if (!h || !b) return 0; 0 2 2
582 2 (void)go_pack(b, packed);
583 2 return hist_seen(h, b->hash, packed);
584 }
585  
586 /* ------------------------------------------------------------ would it? -----
587 *
588 * An enemy chain adjacent to an empty point pt is captured by a play at pt if
589 * and only if its liberty count is exactly 1. It needs no search for WHICH
590 * liberty: pt is empty and adjacent to that chain, so pt is one of its
591 * liberties, and if it has only one then pt is it.
592 */
593 222228926 static int dies_to(const struct go_board *b, int q)
594 {
595 222228926 return b->chain_libs[b->chain_root[q]] == 1;
596 }
597  
598 /* The hash the position WOULD have, without touching the board. */
599 55556 static unsigned long long go_hash_after(const struct go_board *b, int pt, int colour)
600 {
601 unsigned long long hsh;
602 int roots[4];
603 55556 int nroots = 0;
604 int i, j, cur, them;
605  
606 55556 ▶ if (!b || pt < 0 || pt >= b->pts) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return 0; 55556 0 55556
if (!b || pt < 0 || pt >= b->pts) return 0; 55556 0 55556
if (!b || pt < 0 || pt >= b->pts) return 0; 0 55556 55556
607 55556 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 33097 22459 55556
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 0 33097 33097
608  
609 55556 ▶ them = GO_OTHER(colour);
Branch: 1 branch
BranchTrueFalseTotal
them = GO_OTHER(colour); 22459 33097 55556
610 55556 hsh = b->hash ^ zob(colour, pt);
611  
612 277780 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 222224 55556 277780
613 222224 int q = pt + b->nbr[i];
614 222224 int r, seen = 0;
615 222224 ▶ if (b->colour[q] != them) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] != them) continue; 205474 16750 222224
616 16750 ▶ if (!dies_to(b, q)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!dies_to(b, q)) continue; 14718 2032 16750
617 2032 r = b->chain_root[q];
618 2150 ▶ for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 228 118 346
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 346 1804 2150
619 2032 ▶ if (seen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (seen) continue; 228 1804 2032
620 1804 roots[nroots++] = r;
621 1804 cur = r;
622 4376 ▶ do { hsh ^= zob(them, cur); cur = b->chain_next[cur]; } while (cur != r);
Branch: 1 branch
BranchTrueFalseTotal
do { hsh ^= zob(them, cur); cur = b->chain_next[cur]; } while (cur != r); 2572 1804 4376
623 }
624 55556 return hsh;
625 }
626  
627 /* The packed position the board WOULD have: pack what is there, then patch the
628 * played point and the captured stones. Cheaper than copying the board, and
629 * only ever reached on a hash hit. */
630 2 static void packed_after(const struct go_board *b, int pt, int colour, unsigned char *buf)
631 {
632 int roots[4];
633 2 int nroots = 0;
634 int i, j, cur, them, ix;
635  
636 2 (void)go_pack(b, buf);
637 2 ix = buf_index(b, pt);
638 2 ▶ if (ix >= 0) buf[ix] = (unsigned char)colour;
Branch: 1 branch
BranchTrueFalseTotal
if (ix >= 0) buf[ix] = (unsigned char)colour; 2 0 2
639  
640 2 ▶ them = GO_OTHER(colour);
Branch: 1 branch
BranchTrueFalseTotal
them = GO_OTHER(colour); 0 2 2
641 10 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 8 2 10
642 8 int q = pt + b->nbr[i];
643 8 int r, seen = 0;
644 8 ▶ if (b->colour[q] != them) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] != them) continue; 0 8 8
645 8 ▶ if (!dies_to(b, q)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!dies_to(b, q)) continue; 6 2 8
646 2 r = b->chain_root[q];
647 2 ▶ for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 0 0 0
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 0 2 2
648 2 ▶ if (seen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (seen) continue; 0 2 2
649 2 roots[nroots++] = r;
650 2 cur = r;
651 do {
652 2 ix = buf_index(b, cur);
653 2 ▶ if (ix >= 0) buf[ix] = GO_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (ix >= 0) buf[ix] = GO_EMPTY; 2 0 2
654 2 cur = b->chain_next[cur];
655 2 ▶ } while (cur != r);
Branch: 1 branch
BranchTrueFalseTotal
} while (cur != r); 0 2 2
656 }
657 2 }
658  
659 /* ---------------------------------------------------------------- legal ----- */
660  
661 /* THE FIVE STEPS, IN ORDER, AND THE ORDER IS THE RULE.
662 *
663 * 1. the point is on the board and empty -> GO_ILL_OFF / GO_ILL_TAKEN
664 * 2. it is not the ko point -> GO_ILL_KO
665 * 3. the capture is accounted for FIRST
666 * 4. only then, has the played chain a liberty -> GO_ILL_SUICIDE
667 * 5. the resulting position is new -> GO_ILL_REPEAT
668 *
669 * Article 5 puts 3 before 4: "the player must remove all these opposing
670 * stones ... the move is completed when the stones have been removed." So A
671 * MOVE THAT CAPTURES IS NEVER SUICIDE. Testing suicide before capture refuses
672 * every capture of a surrounded group, and does it in the way that is hardest
673 * to notice: a suite of capture-the-lone-stone tests still passes, because a
674 * lone stone leaves an empty point behind it, and only captures into a filled
675 * shape fail.
676 *
677 * NOTHING HERE MUTATES AND NOTHING HERE ALLOCATES, except a stack buffer on a
678 * superko hash hit. Steps 3 and 4 are folded into one pass over the four
679 * neighbours, which computes the same answer the place-then-clear-then-check
680 * procedure would: the played chain has a liberty if any neighbour is empty, or
681 * any adjacent enemy chain dies (freeing a point next to pt), or any adjacent
682 * friendly chain has a liberty that is not pt. legal_moves calls this up to 361
683 * times per node and a playout calls legal_moves per move, so a board copy here
684 * would be the cost of the whole search.
685 */
686 253161116 static int go_legal(const struct go_board *b, const struct go_hist *h, int pt, int colour)
687 {
688 253161116 int i, breathes = 0;
689  
690 253161116 ▶ if (!b) return GO_ILL_OFF;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return GO_ILL_OFF; 0 253161116 253161116
691 253161116 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return GO_ILL_COLOUR;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return GO_ILL_COLOUR; 126797148 126363968 253161116
if (colour != GO_BLACK && colour != GO_WHITE) return GO_ILL_COLOUR; 0 126797148 126797148
692 253161116 ▶ if (pt < 0 || pt >= b->pts) return GO_ILL_OFF;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= b->pts) return GO_ILL_OFF; 253161116 0 253161116
if (pt < 0 || pt >= b->pts) return GO_ILL_OFF; 1 253161115 253161116
693 253161115 ▶ if (b->colour[pt] == GO_BORDER) return GO_ILL_OFF;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[pt] == GO_BORDER) return GO_ILL_OFF; 1 253161114 253161115
694 253161114 ▶ if (b->colour[pt] != GO_EMPTY) return GO_ILL_TAKEN;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[pt] != GO_EMPTY) return GO_ILL_TAKEN; 17 253161097 253161114
695 253161097 ▶ if (pt == b->ko_point && colour == b->ko_colour) return GO_ILL_KO;
Branch: 2 branches
BranchTrueFalseTotal
if (pt == b->ko_point && colour == b->ko_colour) return GO_ILL_KO; 53883 253107214 253161097
if (pt == b->ko_point && colour == b->ko_colour) return GO_ILL_KO; 53881 2 53883
696  
697 1265536080 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 1012428864 253107216 1265536080
698 1012428864 int q = pt + b->nbr[i];
699 1012428864 int c = b->colour[q];
700 1012428864 ▶ if (c == GO_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == GO_EMPTY) { 471563778 540865086 1012428864
701 471563778 breathes = 1;
702 540865086 ▶ } else if (c == GO_OTHER(colour)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == GO_OTHER(colour)) { 270669591 270195495 540865086
} else if (c == GO_OTHER(colour)) { 222212168 318652918 540865086
703 222212168 ▶ if (dies_to(b, q)) breathes = 1; /* step 3, before step 4 */
Branch: 1 branch
BranchTrueFalseTotal
if (dies_to(b, q)) breathes = 1; /* step 3, before step 4 */ 15151192 207060976 222212168
704 318652918 ▶ } else if (c == colour) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == colour) { 217095619 101557299 318652918
705 217095619 ▶ if (b->chain_libs[b->chain_root[q]] > 1) breathes = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_libs[b->chain_root[q]] > 1) breathes = 1; 203256434 13839185 217095619
706 }
707 }
708 253107216 ▶ if (!breathes) return GO_ILL_SUICIDE;
Branch: 1 branch
BranchTrueFalseTotal
if (!breathes) return GO_ILL_SUICIDE; 19821357 233285859 253107216
709  
710 233285859 ▶ if (h && b->superko_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (h && b->superko_on) { 56592 233229267 233285859
if (h && b->superko_on) { 55555 1037 56592
711 unsigned char packed[GO_MAX_PTS];
712 55555 unsigned long long after = go_hash_after(b, pt, colour);
713 int i2;
714 1525315 ▶ for (i2 = 0; i2 < h->n; i2++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i2 = 0; i2 < h->n; i2++) { 1469762 55553 1525315
715 1469762 ▶ if (h->hash[i2] != after) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (h->hash[i2] != after) continue; 1469760 2 1469762
716 2 packed_after(b, pt, colour, packed);
717 2 ▶ if (hist_seen(h, after, packed)) return GO_ILL_REPEAT;
Branch: 1 branch
BranchTrueFalseTotal
if (hist_seen(h, after, packed)) return GO_ILL_REPEAT; 2 0 2
718 0 break;
719 }
720 }
721 233285857 return GO_OK;
722 }
723  
724 1276 static int go_legal_moves(const struct go_board *b, const struct go_hist *h,
725 int colour, int *out)
726 {
727 1276 int r, c, n = 0;
728 1276 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 1276 1276
729 12837 ▶ for (r = 0; r < b->size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) { 11561 1276 12837
730 117048 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 105487 11561 117048
731 105487 int pt = (r + 1) * b->stride + (c + 1);
732 105487 ▶ if (b->colour[pt] != GO_EMPTY) continue; /* the cheap test first */
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[pt] != GO_EMPTY) continue; /* the cheap test first */ 52486 53001 105487
733 53001 ▶ if (go_legal(b, h, pt, colour) != GO_OK) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (go_legal(b, h, pt, colour) != GO_OK) continue; 1616 51385 53001
734 51385 ▶ if (out) out[n] = pt;
Branch: 1 branch
BranchTrueFalseTotal
if (out) out[n] = pt; 51385 0 51385
735 51385 n++;
736 }
737 }
738 1276 return n;
739 }
740  
741 /* ------------------------------------------------------------------ ko ------ */
742  
743 /* ARTICLE 6'S SHAPE, AND ALL THREE CLAUSES ARE LOAD-BEARING.
744 *
745 * Article 6 describes a shape rather than a procedure: "A shape in which the
746 * players can alternately capture and recapture one opposing stone is called a
747 * 'ko'." So the test is whether THIS move created that shape, and it did only
748 * if all three hold. Each comment says what breaks without its clause. */
749 6225016 static void ko_set(struct go_board *b, int pt, int ncaps, int captured, int colour)
750 {
751 6225016 b->ko_point = -1;
752 6225016 b->ko_colour = GO_EMPTY;
753  
754 /* Without this, a capture of two or more stones sets a phantom ko and the
755 * opponent is refused a point Article 6 says nothing about. */
756 6225016 ▶ if (ncaps != 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ncaps != 1) return; 5610940 614076 6225016
757  
758 /* Without this, a stone that joins a larger group AND captures one stone
759 * sets a ko. Article 6 does not create one: the recapture would not be of
760 * a single stone in a shape that can repeat. */
761 614076 ▶ if (b->chain_size[b->chain_root[pt]] != 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_size[b->chain_root[pt]] != 1) return; 507416 106660 614076
762  
763 /* Without this, a capture that leaves the capturing stone with room sets a
764 * ko where no recapture was possible anyway. The symptom is the worst of
765 * the three: a legal move refused somewhere apparently unrelated, three
766 * moves later. */
767 106660 ▶ if (b->chain_libs[b->chain_root[pt]] != 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_libs[b->chain_root[pt]] != 1) return; 52760 53900 106660
768  
769 53900 b->ko_point = captured;
770  
771 /* AND THE COLOUR IT IS FORBIDDEN TO, which is the player whose stone was
772 * just taken. Article 6 names one player: "A player whose stone has been
773 * captured in a ko cannot recapture in that ko on the next move." The
774 * capturer is not restricted, and on a filled board sometimes wants to
775 * play the point to connect. Refusing both colours is the easy version and
776 * it refuses a legal move. */
777 53900 ▶ b->ko_colour = GO_OTHER(colour);
Branch: 1 branch
BranchTrueFalseTotal
b->ko_colour = GO_OTHER(colour); 25068 28832 53900
778 }
779  
780 38 ▶static int go_ko_point(const struct go_board *b) { return b ? b->ko_point : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int go_ko_point(const struct go_board *b) { return b ? b->ko_point : -1; } 38 0 38
781 2 ▶static int go_ko_colour(const struct go_board *b) { return b ? b->ko_colour : GO_EMPTY; }
Branch: 1 branch
BranchTrueFalseTotal
static int go_ko_colour(const struct go_board *b) { return b ? b->ko_colour : GO_EMPTY; } 2 0 2
782 34 ▶static int go_superko_on(const struct go_board *b) { return b ? b->superko_on : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int go_superko_on(const struct go_board *b) { return b ? b->superko_on : 0; } 34 0 34
783  
784 34 static void go_set_superko(struct go_board *b, int on)
785 {
786 34 ▶ if (b) b->superko_on = on ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (b) b->superko_on = on ? 1 : 0; 34 0 34
787 34 }
788  
789 /* ============================================================== the scorers ===
790 *
791 * Two of them over ONE flood fill, because both ask the same question of the
792 * empty points and differ only in what they do with the answer.
793 *
794 * Japanese territory: an empty region reaching only one colour is that
795 * colour's, a region reaching both is dame, and a region the players agreed
796 * is seki counts for nobody.
797 * Tromp-Taylor area: the same classification, plus your stones on the board.
798 *
799 * The second is here for two reasons. It needs nobody's agreement, so it is
800 * what a game whose players cannot agree falls through to; and the two must
801 * agree under a published condition, which is the only differential test this
802 * distribution can have, since the XS-only decision left one implementation of
803 * everything else.
804 */
805  
806 /* One pass over the empty points. Fills the eye counts, the dame count, the
807 * stone counts and the area scores; the caller applies prisoners and komi. */
808 static void analyse_map(const struct go_board *b, const int *seki, int nseki,
809 struct go_score *out, signed char *owner);
810  
811 75410 static void analyse(const struct go_board *b, const int *seki, int nseki,
812 struct go_score *out)
813 {
814 75410 analyse_map(b, seki, nseki, out, NULL);
815 75410 }
816  
817 /* The one classification. `owner`, when given, gets one value per padded point:
818 * the colour whose territory it is, or GO_EMPTY for dame, for an agreed seki,
819 * and for anything holding a stone.
820 *
821 * It is the same walk that produces the score because a SECOND implementation
822 * of this is a second thing to get wrong, and the way it goes wrong is subtle:
823 * territory is a property of a REGION, so an empty point beside one black stone
824 * on an otherwise open board belongs to NOBODY. A per-point version of this
825 * gave a nearly empty board eight points of territory where the scorer, quite
826 * rightly, gave it none. */
827 78351 static void analyse_map(const struct go_board *b, const int *seki, int nseki,
828 struct go_score *out, signed char *owner)
829 {
830 int seen[GO_MAX_PTS];
831 int stack[GO_MAX_PTS];
832 int region[GO_MAX_PTS];
833 int r, c, i, k, sp, n;
834  
835 78351 out->eyes_b = out->eyes_w = 0;
836 78351 out->territory_b = out->territory_w = 0;
837 78351 out->dame = 0;
838 78351 out->area_b = out->area_w = 0;
839 78351 out->stones_b = b->stones[GO_BLACK];
840 78351 out->stones_w = b->stones[GO_WHITE];
841  
842 10101350 ▶ for (i = 0; i < b->pts; i++) seen[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->pts; i++) seen[i] = 0; 10022999 78351 10101350
843  
844 802318 ▶ for (r = 0; r < b->size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) { 723967 78351 802318
845 7537694 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 6813727 723967 7537694
846 6813727 int p = (r + 1) * b->stride + (c + 1);
847 6813727 int touches_b = 0, touches_w = 0, is_seki = 0;
848  
849 6813727 ▶ if (b->colour[p] != GO_EMPTY || seen[p]) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[p] != GO_EMPTY || seen[p]) continue; 809513 6004214 6813727
if (b->colour[p] != GO_EMPTY || seen[p]) continue; 5188 804325 809513
850  
851 /* the region, and which colours it reaches */
852 804325 n = 0;
853 804325 sp = 0;
854 804325 stack[sp++] = p;
855 804325 seen[p] = 1;
856 1613838 ▶ while (sp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (sp > 0) { 809513 804325 1613838
857 809513 int cur = stack[--sp];
858 809513 region[n++] = cur;
859 4047565 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 3238052 809513 4047565
860 3238052 int q = cur + b->nbr[k];
861 3238052 ▶ if (b->colour[q] == GO_BLACK) { touches_b = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == GO_BLACK) { touches_b = 1; continue; } 1602419 1635633 3238052
862 1635633 ▶ if (b->colour[q] == GO_WHITE) { touches_w = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == GO_WHITE) { touches_w = 1; continue; } 1184479 451154 1635633
863 451154 ▶ if (b->colour[q] != GO_EMPTY) continue; /* the ring */
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] != GO_EMPTY) continue; /* the ring */ 433872 17282 451154
864 17282 ▶ if (seen[q]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (seen[q]) continue; 12094 5188 17282
865 5188 seen[q] = 1;
866 5188 stack[sp++] = q;
867 }
868 }
869  
870 /* agreed seki: any listed point taints the whole region it is in */
871 804342 ▶ for (i = 0; i < nseki && !is_seki; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nseki && !is_seki; i++) 18 804324 804342
for (i = 0; i < nseki && !is_seki; i++) 17 1 18
872 233 ▶ for (k = 0; k < n; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) 221 12 233
873 221 ▶ if (region[k] == seki[i]) { is_seki = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (region[k] == seki[i]) { is_seki = 1; break; } 5 216 221
874  
875 804325 ▶ if (touches_b && !touches_w) {
Branch: 2 branches
BranchTrueFalseTotal
if (touches_b && !touches_w) { 459314 345011 804325
if (touches_b && !touches_w) { 459230 84 459314
876 459230 out->eyes_b += n;
877 459230 out->area_b += n;
878 459230 ▶ if (!is_seki) out->territory_b += n;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_seki) out->territory_b += n; 459225 5 459230
879 459230 ▶ if (owner && !is_seki)
Branch: 2 branches
BranchTrueFalseTotal
if (owner && !is_seki) 12554 446676 459230
if (owner && !is_seki) 12554 0 12554
880 25108 ▶ for (k = 0; k < n; k++) owner[region[k]] = GO_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) owner[region[k]] = GO_BLACK; 12554 12554 25108
881 345095 ▶ } else if (touches_w && !touches_b) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (touches_w && !touches_b) { 345094 1 345095
} else if (touches_w && !touches_b) { 345010 84 345094
882 345010 out->eyes_w += n;
883 345010 out->area_w += n;
884 345010 ▶ if (!is_seki) out->territory_w += n;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_seki) out->territory_w += n; 345010 0 345010
885 345010 ▶ if (owner && !is_seki)
Branch: 2 branches
BranchTrueFalseTotal
if (owner && !is_seki) 9867 335143 345010
if (owner && !is_seki) 9867 0 9867
886 19734 ▶ for (k = 0; k < n; k++) owner[region[k]] = GO_WHITE;
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) owner[region[k]] = GO_WHITE; 9867 9867 19734
887 } else {
888 /* reaching both colours, or neither on an empty board */
889 85 out->dame += n;
890 }
891 }
892 }
893  
894 78351 out->area_b += out->stones_b;
895 78351 out->area_w += out->stones_w;
896 78351 }
897  
898 /* The same five steps score_jp takes, stopping after the classification. Dead
899 * stones come off on a copy first, because a point a dead stone was standing on
900 * is territory once it is gone. */
901 2941 static void go_territory(const struct go_board *b,
902 const int *dead, int ndead,
903 const int *seki, int nseki,
904 signed char *out)
905 {
906 struct go_board *work;
907 struct go_score s;
908 int i;
909  
910 2941 ▶ if (!b || !out) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !out) return; 2941 0 2941
if (!b || !out) return; 0 2941 2941
911 358802 ▶ for (i = 0; i < b->pts; i++) out[i] = GO_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->pts; i++) out[i] = GO_EMPTY; 355861 2941 358802
912  
913 2941 work = go_board_copy(b);
914 2941 ▶ if (!work) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!work) return; 0 2941 2941
915  
916 2941 ▶ for (i = 0; i < ndead; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ndead; i++) { 0 2941 2941
917 0 int pt = dead[i];
918 0 ▶ if (pt < 0 || pt >= work->pts) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= work->pts) continue; 0 0 0
if (pt < 0 || pt >= work->pts) continue; 0 0 0
919 0 ▶ if (work->colour[pt] != GO_BLACK && work->colour[pt] != GO_WHITE) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (work->colour[pt] != GO_BLACK && work->colour[pt] != GO_WHITE) continue; 0 0 0
if (work->colour[pt] != GO_BLACK && work->colour[pt] != GO_WHITE) continue; 0 0 0
920 0 (void)go_lift(work, pt);
921 }
922  
923 2941 analyse_map(work, seki, nseki, &s, out);
924 2941 go_board_drop(work);
925 }
926  
927 75332 static void go_score_area(const struct go_board *b, struct go_score *out)
928 {
929 75332 ▶ if (!b || !out) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !out) return; 75332 0 75332
if (!b || !out) return; 0 75332 75332
930 75332 analyse(b, NULL, 0, out);
931 75332 out->prisoners_b = out->prisoners_w = 0;
932 75332 out->score_b = out->score_w = 0;
933 }
934  
935 78 static void go_score_jp(const struct go_board *b,
936 const int *dead, int ndead,
937 const int *seki, int nseki,
938 int prisoners_b, int prisoners_w,
939 int komi_tenths,
940 struct go_score *out)
941 {
942 struct go_board *work;
943 int i;
944  
945 78 ▶ if (!b || !out) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !out) return; 78 0 78
if (!b || !out) return; 0 78 78
946  
947 /* Step 1. The dead come off, and each stone goes to the player who did NOT
948 * own it. Done on a COPY, because scoring a position must not change it:
949 * the site scores a game and then goes on serving the same board to a
950 * spectator. */
951 78 work = go_board_copy(b);
952 78 ▶ if (!work) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!work) return; 0 78 78
953  
954 109 ▶ for (i = 0; i < ndead; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ndead; i++) { 31 78 109
955 31 int pt = dead[i];
956 int colour, gone;
957 31 ▶ if (pt < 0 || pt >= work->pts) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= work->pts) continue; 31 0 31
if (pt < 0 || pt >= work->pts) continue; 0 31 31
958 31 colour = work->colour[pt];
959 31 ▶ if (colour != GO_BLACK && colour != GO_WHITE) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) continue; 30 1 31
if (colour != GO_BLACK && colour != GO_WHITE) continue; 0 30 30
960 31 gone = go_lift(work, pt);
961 31 ▶ if (colour == GO_BLACK) prisoners_w += gone; /* black stones, white holds them */
Branch: 1 branch
BranchTrueFalseTotal
if (colour == GO_BLACK) prisoners_w += gone; /* black stones, white holds them */ 1 30 31
962 30 else prisoners_b += gone;
963 }
964  
965 /* Steps 2 to 4. */
966 78 analyse(work, seki, nseki, out);
967  
968 78 out->prisoners_b = prisoners_b;
969 78 out->prisoners_w = prisoners_w;
970  
971 /* Step 5, and the direction of it is the rule. Article 10.2 fills each
972 * player's prisoners INTO THE OPPONENT'S TERRITORY, so they come off the
973 * opponent rather than going on to your own. */
974 78 out->score_b = (out->territory_b - prisoners_w) * 10;
975 78 out->score_w = (out->territory_w - prisoners_b) * 10 + komi_tenths;
976  
977 78 go_board_drop(work);
978 }
979  
980 /* ====================================================== Benson's algorithm ===
981 *
982 * D. B. Benson, "Life in the Game of Go", Information Sciences 10 (1976).
983 *
984 * The chains that are UNCONDITIONALLY ALIVE: alive even if their owner never
985 * answers another move, whatever the opponent does. It is the only part of life
986 * and death that is decidable without search, and this engine claims to know
987 * nothing else about it.
988 *
989 * The algorithm, so the code below can be read against it:
990 *
991 * X = every chain of the colour
992 * R = every region: a maximal connected set of points NOT of the colour
993 *
994 * repeat until nothing is removed:
995 * remove from X every chain with fewer than TWO VITAL regions in R,
996 * where a region is vital to a chain when EVERY EMPTY POINT in it is
997 * adjacent to that chain
998 * remove from R every region that touches a chain no longer in X
999 *
1000 * what remains in X is unconditionally alive
1001 *
1002 * TWO VITAL REGIONS, NOT TWO EYE POINTS, and the difference is the thing this
1003 * gets right that a count of eyes does not. A group around a single two-point
1004 * eye has two eye points and ONE vital region, and it is not unconditionally
1005 * alive. t/10-benson.t has that position.
1006 */
1007  
1008 /* Every empty point of the region is a liberty of the chain. */
1009 995 static int region_is_vital(const struct go_board *b, int root,
1010 const int *pts, int from, int len)
1011 {
1012 int i, k;
1013 10684 ▶ for (i = from; i < from + len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = from; i < from + len; i++) { 10197 487 10684
1014 10197 int p = pts[i];
1015 10197 int touches = 0;
1016 10197 ▶ if (b->colour[p] != GO_EMPTY) continue; /* opponent stones do not count */
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[p] != GO_EMPTY) continue; /* opponent stones do not count */ 9128 1069 10197
1017 3569 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 3061 508 3569
1018 3061 int q = p + b->nbr[k];
1019 3061 ▶ if (b->chain_root[q] == root && b->colour[q] != GO_EMPTY) { touches = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (b->chain_root[q] == root && b->colour[q] != GO_EMPTY) { touches = 1; break; } 561 2500 3061
if (b->chain_root[q] == root && b->colour[q] != GO_EMPTY) { touches = 1; break; } 561 0 561
1020 }
1021 1069 ▶ if (!touches) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!touches) return 0; 508 561 1069
1022 }
1023 487 return 1;
1024 }
1025  
1026 246 static int go_alive(const struct go_board *b, int colour, int *out)
1027 {
1028 int region_of[GO_MAX_PTS]; /* which region a point is in, or -1 */
1029 int pts[GO_MAX_PTS]; /* every region's points, laid end to end */
1030 int rstart[GO_MAX_PTS];
1031 int rlen[GO_MAX_PTS];
1032 int stack[GO_MAX_PTS];
1033 int live[GO_MAX_PTS]; /* by chain ROOT: is it still a candidate */
1034 int rlive[GO_MAX_PTS]; /* by region: is it still a candidate */
1035 246 int nregions = 0, npts = 0, n = 0;
1036 int r, c, i, k, sp, changed;
1037  
1038 246 ▶ if (!b || (colour != GO_BLACK && colour != GO_WHITE)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || (colour != GO_BLACK && colour != GO_WHITE)) return 0; 246 0 246
if (!b || (colour != GO_BLACK && colour != GO_WHITE)) return 0; 180 66 246
if (!b || (colour != GO_BLACK && colour != GO_WHITE)) return 0; 1 179 180
1039  
1040 29890 ▶ for (i = 0; i < b->pts; i++) { region_of[i] = -1; live[i] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->pts; i++) { region_of[i] = -1; live[i] = 0; } 29645 245 29890
1041  
1042 /* X starts as every chain of the colour. `live` is indexed by root, so a
1043 * point that is not a root simply never gets looked at. */
1044 2450 ▶ for (r = 0; r < b->size; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) 2205 245 2450
1045 22050 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 19845 2205 22050
1046 19845 int p = (r + 1) * b->stride + (c + 1);
1047 19845 ▶ if (b->colour[p] == colour) live[b->chain_root[p]] = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[p] == colour) live[b->chain_root[p]] = 1; 4353 15492 19845
1048 }
1049  
1050 /* R starts as every maximal connected set of points that are NOT the
1051 * colour. The sentinel ring is not a point, so it bounds the fill. */
1052 2450 ▶ for (r = 0; r < b->size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) { 2205 245 2450
1053 22050 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 19845 2205 22050
1054 19845 int p = (r + 1) * b->stride + (c + 1);
1055 19845 ▶ if (b->colour[p] == colour || region_of[p] >= 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[p] == colour || region_of[p] >= 0) continue; 15492 4353 19845
if (b->colour[p] == colour || region_of[p] >= 0) continue; 15024 468 15492
1056  
1057 468 rstart[nregions] = npts;
1058 468 sp = 0;
1059 468 stack[sp++] = p;
1060 468 region_of[p] = nregions;
1061 15960 ▶ while (sp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (sp > 0) { 15492 468 15960
1062 15492 int cur = stack[--sp];
1063 15492 pts[npts++] = cur;
1064 77460 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 61968 15492 77460
1065 61968 int q = cur + b->nbr[k];
1066 61968 ▶ if (b->colour[q] == GO_BORDER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == GO_BORDER) continue; 6566 55402 61968
1067 55402 ▶ if (b->colour[q] == colour) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == colour) continue; 3236 52166 55402
1068 52166 ▶ if (region_of[q] >= 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (region_of[q] >= 0) continue; 37142 15024 52166
1069 15024 region_of[q] = nregions;
1070 15024 stack[sp++] = q;
1071 }
1072 }
1073 468 rlen[nregions] = npts - rstart[nregions];
1074 468 rlive[nregions] = 1;
1075 468 nregions++;
1076 }
1077 }
1078  
1079 /* The two removals, alternating until neither fires. */
1080 do {
1081 466 changed = 0;
1082  
1083 /* a chain with fewer than two vital candidate regions goes */
1084 4660 ▶ for (r = 0; r < b->size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) { 4194 466 4660
1085 41940 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 37746 4194 41940
1086 37746 int p = (r + 1) * b->stride + (c + 1);
1087 37746 int root, vital = 0, seen[4], nseen = 0;
1088 45448 ▶ if (b->colour[p] != colour) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[p] != colour) continue; 29530 8216 37746
1089 8216 root = b->chain_root[p];
1090 8216 ▶ if (p != root || !live[root]) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (p != root || !live[root]) continue; 1008 7208 8216
if (p != root || !live[root]) continue; 494 514 1008
1091  
1092 /* count the distinct candidate regions adjacent to this chain
1093 * that are vital to it. Walk the chain's stones; a region can
1094 * touch a chain at many points and counts once. */
1095 {
1096 514 int cur = root;
1097 do {
1098 19550 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 15640 3910 19550
1099 15640 int q = cur + b->nbr[k];
1100 15640 int reg, j, dup = 0;
1101 15640 ▶ if (b->colour[q] == GO_BORDER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == GO_BORDER) continue; 2071 13569 15640
1102 13569 reg = region_of[q];
1103 13569 ▶ if (reg < 0 || !rlive[reg]) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (reg < 0 || !rlive[reg]) continue; 3015 10554 13569
if (reg < 0 || !rlive[reg]) continue; 0 3015 3015
1104 3962 ▶ for (j = 0; j < nseen; j++) if (seen[j] == reg) { dup = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < nseen; j++) if (seen[j] == reg) { dup = 1; break; } 2020 947 2967
for (j = 0; j < nseen; j++) if (seen[j] == reg) { dup = 1; break; } 2967 995 3962
1105 3015 ▶ if (dup) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (dup) continue; 2020 995 3015
1106 995 ▶ if (nseen < 4) seen[nseen++] = reg; /* two is all we need */
Branch: 1 branch
BranchTrueFalseTotal
if (nseen < 4) seen[nseen++] = reg; /* two is all we need */ 995 0 995
1107 995 ▶ if (region_is_vital(b, root, pts, rstart[reg], rlen[reg]))
Branch: 1 branch
BranchTrueFalseTotal
if (region_is_vital(b, root, pts, rstart[reg], rlen[reg])) 487 508 995
1108 487 vital++;
1109 }
1110 3910 cur = b->chain_next[cur];
1111 3910 ▶ } while (cur != root && vital < 2);
Branch: 2 branches
BranchTrueFalseTotal
} while (cur != root && vital < 2); 3411 499 3910
} while (cur != root && vital < 2); 3396 15 3411
1112 }
1113  
1114 514 ▶ if (vital < 2) { live[root] = 0; changed = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (vital < 2) { live[root] = 0; changed = 1; } 494 20 514
1115 }
1116 }
1117  
1118 /* a region touching a chain that has gone goes too */
1119 1333 ▶ for (i = 0; i < nregions; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nregions; i++) { 867 466 1333
1120 int j;
1121 867 ▶ if (!rlive[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!rlive[i]) continue; 399 468 867
1122 3835 ▶ for (j = rstart[i]; j < rstart[i] + rlen[i]; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = rstart[i]; j < rstart[i] + rlen[i]; j++) { 3766 69 3835
1123 3766 int p = pts[j];
1124 17729 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 14362 3367 17729
1125 14362 int q = p + b->nbr[k];
1126 14362 ▶ if (b->colour[q] != colour) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] != colour) continue; 13663 699 14362
1127 699 ▶ if (!live[b->chain_root[q]]) { rlive[i] = 0; changed = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!live[b->chain_root[q]]) { rlive[i] = 0; changed = 1; break; } 399 300 699
1128 }
1129 3766 ▶ if (!rlive[i]) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!rlive[i]) break; 399 3367 3766
1130 }
1131 }
1132 466 ▶ } while (changed);
Branch: 1 branch
BranchTrueFalseTotal
} while (changed); 221 245 466
1133  
1134 /* what survived, as points */
1135 2450 ▶ for (r = 0; r < b->size; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) 2205 245 2450
1136 22050 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 19845 2205 22050
1137 19845 int p = (r + 1) * b->stride + (c + 1);
1138 19845 ▶ if (b->colour[p] != colour) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[p] != colour) continue; 15492 4353 19845
1139 4353 ▶ if (!live[b->chain_root[p]]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!live[b->chain_root[p]]) continue; 3863 490 4353
1140 490 ▶ if (out) out[n] = p;
Branch: 1 branch
BranchTrueFalseTotal
if (out) out[n] = p; 490 0 490
1141 490 n++;
1142 }
1143  
1144 245 return n;
1145 }
1146  
1147 /* ---------------------------------------------------------------- play ------ */
1148  
1149 6225040 static void go_play(struct go_board *b, struct go_hist *h, int pt, int colour,
1150 struct go_played *out)
1151 {
1152 6225040 int i, j, code, nroots = 0;
1153 int roots[4];
1154  
1155 6225064 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 6225040 6225040
1156 6225040 out->code = GO_ILL_OFF;
1157 6225040 out->ncaps = 0;
1158 6225040 out->ko_point = -1;
1159 6225040 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 6225040 6225040
1160  
1161 /* Refuse BEFORE touching anything, so a refused play leaves the board
1162 * exactly as it was and a caller can offer the reason and try again. */
1163 6225040 code = go_legal(b, h, pt, colour);
1164 6225040 out->code = code;
1165 6225040 out->ko_point = b->ko_point;
1166 6225040 ▶ if (code != GO_OK) return;
Branch: 1 branch
BranchTrueFalseTotal
if (code != GO_OK) return; 24 6225016 6225040
1167  
1168 6225016 go_put(b, pt, colour);
1169  
1170 /* Article 5: the opposing stones come off, and the move is completed when
1171 * they have. An adjacent enemy chain is captured exactly when go_put's
1172 * recount has left it at zero. */
1173 31125080 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 24900064 6225016 31125080
1174 24900064 int q = pt + b->nbr[i];
1175 24900064 int r, seen = 0, cur;
1176 24900064 ▶ if (b->colour[q] != GO_OTHER(colour)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[q] != GO_OTHER(colour)) continue; 12592964 12307100 24900064
if (b->colour[q] != GO_OTHER(colour)) continue; 18490368 6409696 24900064
1177 6409696 ▶ if (b->chain_libs[b->chain_root[q]] != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_libs[b->chain_root[q]] != 0) continue; 5109793 1299903 6409696
1178 1299903 r = b->chain_root[q];
1179 1470857 ▶ for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 0 170954 170954
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 170954 1299903 1470857
1180 1299903 ▶ if (seen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (seen) continue; 0 1299903 1299903
1181 1299903 roots[nroots++] = r;
1182  
1183 /* record the stones before they go, because after go_lift the chain
1184 * that named them does not exist */
1185 1299903 cur = r;
1186 3049684 ▶ do { out->caps[out->ncaps++] = cur; cur = b->chain_next[cur]; } while (cur != r);
Branch: 1 branch
BranchTrueFalseTotal
do { out->caps[out->ncaps++] = cur; cur = b->chain_next[cur]; } while (cur != r); 1749781 1299903 3049684
1187 1299903 (void)go_lift(b, r);
1188 }
1189  
1190 6225016 ▶ ko_set(b, pt, out->ncaps, out->ncaps == 1 ? out->caps[0] : -1, colour);
Branch: 1 branch
BranchTrueFalseTotal
ko_set(b, pt, out->ncaps, out->ncaps == 1 ? out->caps[0] : -1, colour); 614076 5610940 6225016
1191 6225016 out->ko_point = b->ko_point;
1192  
1193 6225016 ▶ if (h) (void)go_hist_push(h, b);
Branch: 1 branch
BranchTrueFalseTotal
if (h) (void)go_hist_push(h, b); 5120 6219896 6225016
1194 }
1195  
1196 /* A pass clears the ko point, because Article 6 forbids the recapture on the
1197 * NEXT move only: after a pass, the recapture is the move after that.
1198 *
1199 * It pushes nothing onto the history. A pass changes no colouring, so the
1200 * colouring is already there from the move that made it, and pushing it again
1201 * would be a duplicate at best. The direction that actually bites is the other
1202 * one: an engine that treats a pass as a position-producing event and pushes
1203 * can end up refusing the next real move that returns to a colouring, for a
1204 * reason no player could ever see. */
1205 409720 static void go_pass(struct go_board *b, int colour)
1206 {
1207 (void)colour;
1208 409720 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 409720 409720
1209 409720 b->ko_point = -1;
1210 409720 b->ko_colour = GO_EMPTY;
1211 }
1212  
1213 /* ------------------------------------------------------------- the table ----- */
1214  
1215 /* NAMED ASSIGNMENT, NOT A POSITIONAL INITIALISER, and not a designated one
1216 * either. A positional list silently binds the wrong function to a member the
1217 * day somebody reorders the struct, and the symptom is a call through a
1218 * plausible-looking pointer. A designated initialiser would fix that and would
1219 * also put a C99-only construct in a file that has to build under old MSVC.
1220 * So the table is filled in by name, once, here. The writes are identical on
1221 * every call, so the benign race costs nothing. */
1222  
1223 static struct go_abi GO_TABLE;
1224 static int GO_TABLE_READY = 0;
1225  
1226 /* go_search.c fills in its own four members. Declared here rather than in
1227 * go_abi.h because it is not part of the published ABI: a consumer reaches the
1228 * search through the table like everything else.
1229 *
1230 * The two files call into each other, and it terminates: go_abi_table calls
1231 * this while building the table, and go_search.c only calls go_abi_table from
1232 * inside a search, which is long after the table is ready. */
1233 void go_search_install(struct go_abi *t);
1234  
1235 41 const struct go_abi *go_abi_table(void)
1236 {
1237 41 ▶ if (!GO_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GO_TABLE_READY) { 30 11 41
1238 30 GO_TABLE.abi_version = GO_ABI_VERSION;
1239  
1240 30 GO_TABLE.board_new = go_board_new;
1241 30 GO_TABLE.board_drop = go_board_drop;
1242 30 GO_TABLE.board_copy = go_board_copy;
1243  
1244 30 GO_TABLE.at = go_at;
1245 30 GO_TABLE.libs = go_libs;
1246 30 GO_TABLE.chain_at = go_chain_at;
1247 30 GO_TABLE.chain_size = go_chain_size;
1248 30 GO_TABLE.pack = go_pack;
1249 30 GO_TABLE.hash = go_hash;
1250 30 GO_TABLE.stones = go_stones;
1251  
1252 30 GO_TABLE.point_of = go_point_of;
1253 30 GO_TABLE.col_of = go_col_of;
1254 30 GO_TABLE.row_of = go_row_of;
1255 30 GO_TABLE.size_of = go_size_of;
1256 30 GO_TABLE.stride_of = go_stride_of;
1257  
1258 30 GO_TABLE.put = go_put;
1259 30 GO_TABLE.lift = go_lift;
1260  
1261 30 GO_TABLE.zobrist = go_zobrist;
1262  
1263 /* appended at version 2. Nothing above this line moved. */
1264 30 GO_TABLE.hist_new = go_hist_new;
1265 30 GO_TABLE.hist_drop = go_hist_drop;
1266 30 GO_TABLE.hist_copy = go_hist_copy;
1267 30 GO_TABLE.hist_len = go_hist_len;
1268 30 GO_TABLE.hist_clear = go_hist_clear;
1269 30 GO_TABLE.hist_push = go_hist_push;
1270 30 GO_TABLE.hist_has = go_hist_has;
1271  
1272 30 GO_TABLE.legal = go_legal;
1273 30 GO_TABLE.legal_moves = go_legal_moves;
1274 30 GO_TABLE.play = go_play;
1275 30 GO_TABLE.pass = go_pass;
1276  
1277 30 GO_TABLE.ko_point = go_ko_point;
1278 30 GO_TABLE.ko_colour = go_ko_colour;
1279 30 GO_TABLE.set_superko = go_set_superko;
1280 30 GO_TABLE.superko_on = go_superko_on;
1281 30 GO_TABLE.hash_after = go_hash_after;
1282  
1283 /* appended at version 3 */
1284 30 GO_TABLE.alive = go_alive;
1285  
1286 /* appended at version 4 */
1287 30 GO_TABLE.score_jp = go_score_jp;
1288 30 GO_TABLE.score_area = go_score_area;
1289  
1290 /* appended at version 5 */
1291 30 GO_TABLE.territory = go_territory;
1292  
1293 /* appended at version 6, from go_search.c */
1294 30 go_search_install(&GO_TABLE);
1295  
1296 30 GO_TABLE_READY = 1;
1297 }
1298 41 return &GO_TABLE;
1299 }
« Go.xs ↑ Summary go_search.c »