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 37044 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 37044 h[0] = 0x6a09e667u; h[1] = 0xbb67ae85u; h[2] = 0x3c6ef372u; h[3] = 0xa54ff53au;
62 37044 h[4] = 0x510e527fu; h[5] = 0x9b05688cu; h[6] = 0x1f83d9abu; h[7] = 0x5be0cd19u;
63  
64 37044 memset(blk, 0, sizeof(blk));
65 37044 memcpy(blk, msg, (size_t)len);
66 37044 blk[len] = 0x80;
67 /* the message is short, so the length in bits fits the last two bytes */
68 37044 blk[62] = (unsigned char)(((unsigned)len * 8u) >> 8);
69 37044 blk[63] = (unsigned char)(((unsigned)len * 8u) & 0xffu);
70  
71 629748 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 592704 37044 629748
72 592704 w[i] = ((unsigned int)blk[i * 4] << 24) | ((unsigned int)blk[i * 4 + 1] << 16)
73 592704 | ((unsigned int)blk[i * 4 + 2] << 8) | (unsigned int)blk[i * 4 + 3];
74 1815156 ▶ for (i = 16; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 16; i < 64; i++) { 1778112 37044 1815156
75 1778112 unsigned int s0 = ROR32(w[i - 15], 7) ^ ROR32(w[i - 15], 18) ^ M32(w[i - 15] >> 3);
76 1778112 unsigned int s1 = ROR32(w[i - 2], 17) ^ ROR32(w[i - 2], 19) ^ M32(w[i - 2] >> 10);
77 1778112 w[i] = M32(w[i - 16] + s0 + w[i - 7] + s1);
78 }
79  
80 {
81 37044 unsigned int a = h[0], b = h[1], c = h[2], d = h[3];
82 37044 unsigned int e = h[4], f = h[5], g = h[6], hh = h[7];
83 2407860 ▶ for (i = 0; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 64; i++) { 2370816 37044 2407860
84 2370816 unsigned int S1 = ROR32(e, 6) ^ ROR32(e, 11) ^ ROR32(e, 25);
85 2370816 unsigned int ch = (e & f) ^ ((~e) & g);
86 2370816 unsigned int t1 = M32(hh + S1 + ch + SHA_K[i] + w[i]);
87 2370816 unsigned int S0 = ROR32(a, 2) ^ ROR32(a, 13) ^ ROR32(a, 22);
88 2370816 unsigned int mj = (a & b) ^ (a & c) ^ (b & c);
89 2370816 unsigned int t2 = M32(S0 + mj);
90 2370816 hh = g; g = f; f = e; e = M32(d + t1);
91 2370816 d = c; c = b; b = a; a = M32(t1 + t2);
92 }
93 37044 h[0] = M32(h[0] + a); h[1] = M32(h[1] + b); h[2] = M32(h[2] + c);
94 37044 h[3] = M32(h[3] + d); h[4] = M32(h[4] + e); h[5] = M32(h[5] + f);
95 37044 h[6] = M32(h[6] + g); h[7] = M32(h[7] + hh);
96 }
97  
98 333396 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 296352 37044 333396
99 296352 out[i * 4] = (unsigned char)((h[i] >> 24) & 0xffu);
100 296352 out[i * 4 + 1] = (unsigned char)((h[i] >> 16) & 0xffu);
101 296352 out[i * 4 + 2] = (unsigned char)((h[i] >> 8) & 0xffu);
102 296352 out[i * 4 + 3] = (unsigned char)(h[i] & 0xffu);
103 }
104 37044 }
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 74088 static int put_dec(unsigned char *at, int n)
114 {
115 unsigned char tmp[12];
116 74088 int i = 0, j = 0;
117 74088 ▶ if (n == 0) { at[0] = '0'; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { at[0] = '0'; return 1; } 84 74004 74088
118 212856 ▶ 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; } 138852 74004 212856
119 212856 ▶ while (i > 0) at[j++] = tmp[--i];
Branch: 1 branch
BranchTrueFalseTotal
while (i > 0) at[j++] = tmp[--i]; 138852 74004 212856
120 74004 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 37044 static int zob_label(unsigned char *buf, int colour, int pt)
126 {
127 static const char pre[] = "go-zobrist:";
128 37044 int n = (int)(sizeof(pre) - 1);
129 37044 memcpy(buf, pre, (size_t)n);
130 37044 n += put_dec(buf + n, colour);
131 37044 buf[n++] = ':';
132 37044 n += put_dec(buf + n, pt);
133 37044 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 9415647 static void zob_init(void)
139 {
140 unsigned char msg[64], dig[32];
141 int colour, pt, i;
142  
143 9415647 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 9415605 42 9415647
144 126 ▶ for (colour = GO_BLACK; colour <= GO_WHITE; colour++) {
Branch: 1 branch
BranchTrueFalseTotal
for (colour = GO_BLACK; colour <= GO_WHITE; colour++) { 84 42 126
145 37128 ▶ for (pt = 0; pt < GO_MAX_PTS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < GO_MAX_PTS; pt++) { 37044 84 37128
146 37044 unsigned long long v = 0;
147 37044 int len = zob_label(msg, colour, pt);
148 37044 sha256_short(msg, len, dig);
149 333396 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 296352 37044 333396
150 296352 v = (v << 8) | (unsigned long long)dig[i];
151 37044 ZOB[colour][pt] = v;
152 }
153 }
154 42 ZOB_READY = 1;
155 }
156  
157 9414965 static unsigned long long zob(int colour, int pt)
158 {
159 9414965 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 4925758 4489207 9414965
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 2 4925756 4925758
160 9414963 ▶ if (pt < 0 || pt >= GO_MAX_PTS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= GO_MAX_PTS) return 0; 9414963 0 9414963
if (pt < 0 || pt >= GO_MAX_PTS) return 0; 0 9414963 9414963
161 9414963 zob_init();
162 9414963 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 684 static struct go_board *go_board_new(int size)
170 {
171 struct go_board *b;
172 int r, c;
173  
174 684 ▶ 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; 684 0 684
if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL; 0 684 684
175 684 zob_init();
176  
177 684 b = (struct go_board *)calloc(1, sizeof(struct go_board));
178 684 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 684 684
179  
180 684 b->size = size;
181 684 b->stride = size + 2;
182 684 b->pts = b->stride * b->stride;
183 684 b->nbr[0] = -1;
184 684 b->nbr[1] = 1;
185 684 b->nbr[2] = -b->stride;
186 684 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 684 b->ko_point = -1;
191 684 b->ko_colour = GO_EMPTY;
192 684 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 8734 ▶ for (r = 0; r < b->stride; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->stride; r++) { 8050 684 8734
197 107404 ▶ for (c = 0; c < b->stride; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->stride; c++) { 99354 8050 107404
198 99354 ▶ 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) 91304 8050 99354
if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1) 83254 8050 91304
if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1) 76572 6682 83254
if (r == 0 || r == b->stride - 1 || c == 0 || c == b->stride - 1) 6682 69890 76572
199 29464 b->colour[r * b->stride + c] = GO_BORDER;
200 }
201 }
202 684 return b;
203 }
204  
205 79555 ▶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); } 79555 0 79555
206  
207 78871 static struct go_board *go_board_copy(const struct go_board *b)
208 {
209 struct go_board *n;
210 78871 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 78871 78871
211 78871 n = (struct go_board *)malloc(sizeof(struct go_board));
212 78871 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 78871 78871
213 /* No pointers in the struct, so a copy is a flat byte copy and the two
214 * boards share nothing at all. */
215 78871 memcpy(n, b, sizeof(struct go_board));
216 78871 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 14962661 static int next_stamp(struct go_board *b)
226 {
227 14962661 ▶ if (b->mark_stamp >= 0x7ffffffe) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->mark_stamp >= 0x7ffffffe) { 0 14962661 14962661
228 0 memset(b->mark, 0, sizeof(b->mark));
229 0 b->mark_stamp = 0;
230 }
231 14962661 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 14962661 static void recount(struct go_board *b, int root)
238 {
239 14962661 int stamp, n = 0, pt;
240 14962661 ▶ if (root < 0 || root >= b->pts) return;
Branch: 2 branches
BranchTrueFalseTotal
if (root < 0 || root >= b->pts) return; 14962661 0 14962661
if (root < 0 || root >= b->pts) return; 0 14962661 14962661
241 14962661 ▶ 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; 7378280 7584381 14962661
if (b->colour[root] != GO_BLACK && b->colour[root] != GO_WHITE) return; 0 7378280 7378280
242  
243 14962661 stamp = next_stamp(b);
244 14962661 pt = root;
245 do {
246 int i;
247 1256770750 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 1005416600 251354150 1256770750
248 1005416600 int q = pt + b->nbr[i];
249 1005416600 ▶ if (b->colour[q] == GO_EMPTY && b->mark[q] != stamp) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[q] == GO_EMPTY && b->mark[q] != stamp) { 150795406 854621194 1005416600
if (b->colour[q] == GO_EMPTY && b->mark[q] != stamp) { 71466607 79328799 150795406
250 71466607 b->mark[q] = stamp;
251 71466607 n++;
252 }
253 }
254 251354150 pt = b->chain_next[pt];
255 251354150 ▶ } while (pt != root);
Branch: 1 branch
BranchTrueFalseTotal
} while (pt != root); 236391489 14962661 251354150
256  
257 14962661 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 7507115 static int merge(struct go_board *b, int ra, int rb)
264 {
265 int big, small, pt, tmp;
266  
267 7507115 ▶ if (ra == rb) return ra;
Branch: 1 branch
BranchTrueFalseTotal
if (ra == rb) return ra; 2038201 5468914 7507115
268 5468914 ▶ 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; } 2204894 3264020 5468914
269 3264020 else { big = rb; small = ra; }
270  
271 5468914 pt = small;
272 7750735 ▶ 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); 2281821 5468914 7750735
273  
274 5468914 tmp = b->chain_next[big];
275 5468914 b->chain_next[big] = b->chain_next[small];
276 5468914 b->chain_next[small] = tmp;
277  
278 5468914 b->chain_size[big] += b->chain_size[small];
279 5468914 b->chain_size[small] = 0;
280 5468914 b->chain_libs[small] = 0;
281 5468914 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 6281256 static void go_put(struct go_board *b, int pt, int colour)
289 {
290 int i, root;
291  
292 6281256 ▶ if (!b || pt < 0 || pt >= b->pts) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return; 6281256 0 6281256
if (!b || pt < 0 || pt >= b->pts) return; 6281256 0 6281256
if (!b || pt < 0 || pt >= b->pts) return; 0 6281256 6281256
293 6281256 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return; 3104765 3176491 6281256
if (colour != GO_BLACK && colour != GO_WHITE) return; 0 3104765 3104765
294 6281256 ▶ 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 6281254 6281256
295  
296 6281254 b->colour[pt] = (signed char)colour;
297 6281254 b->hash ^= zob(colour, pt);
298 6281254 b->stones[colour]++;
299  
300 6281254 b->chain_root[pt] = pt;
301 6281254 b->chain_next[pt] = pt;
302 6281254 b->chain_size[pt] = 1;
303 6281254 b->chain_libs[pt] = 0;
304  
305 6281254 root = pt;
306 31406270 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 25125016 6281254 31406270
307 25125016 int q = pt + b->nbr[i];
308 25125016 ▶ 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]); 7507115 17617901 25125016
309 }
310 6281254 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 31406270 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 25125016 6281254 31406270
315 25125016 int q = pt + b->nbr[i];
316 25125016 ▶ 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]); 12705960 12419056 25125016
if (b->colour[q] == GO_OTHER(colour)) recount(b, b->chain_root[q]); 6628069 18496947 25125016
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 1309535 static int go_lift(struct go_board *b, int pt)
323 {
324 int pts[GO_MAX_PTS];
325 int roots[GO_MAX_PTS];
326 1309535 int n = 0, nroots = 0, i, k, root, cur, colour;
327  
328 1309535 ▶ if (!b || pt < 0 || pt >= b->pts) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return 0; 1309535 0 1309535
if (!b || pt < 0 || pt >= b->pts) return 0; 1309535 0 1309535
if (!b || pt < 0 || pt >= b->pts) return 0; 0 1309535 1309535
329 1309535 colour = b->colour[pt];
330 1309535 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 727812 581723 1309535
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 0 727812 727812
331  
332 1309535 root = b->chain_root[pt];
333 1309535 cur = root;
334 3071057 ▶ 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); 1761522 1309535 3071057
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 4380592 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3071057 1309535 4380592
339 15355285 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 12284228 3071057 15355285
340 12284228 int q = pts[i] + b->nbr[k];
341 12284228 ▶ if (b->colour[q] == GO_OTHER(colour)) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[q] == GO_OTHER(colour)) { 5154036 7130192 12284228
if (b->colour[q] == GO_OTHER(colour)) { 6570373 5713855 12284228
342 6570373 int r = b->chain_root[q], j, seen = 0;
343 8482543 ▶ 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; } 4517035 1912170 6429205
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 6429205 2053338 8482543
344 6570373 ▶ if (!seen) roots[nroots++] = r;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) roots[nroots++] = r; 2053338 4517035 6570373
345 }
346 }
347 }
348  
349 4380592 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3071057 1309535 4380592
350 3071057 b->colour[pts[i]] = GO_EMPTY;
351 3071057 b->hash ^= zob(colour, pts[i]);
352 3071057 b->chain_root[pts[i]] = 0;
353 3071057 b->chain_next[pts[i]] = 0;
354 3071057 b->chain_size[pts[i]] = 0;
355 3071057 b->chain_libs[pts[i]] = 0;
356 }
357 1309535 b->stones[colour] -= n;
358  
359 3362873 ▶ for (i = 0; i < nroots; i++) recount(b, roots[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nroots; i++) recount(b, roots[i]); 2053338 1309535 3362873
360 1309535 return n;
361 }
362  
363 /* ----------------------------------------------------------- reading it ------ */
364  
365 704806425 static int go_at(const struct go_board *b, int pt)
366 {
367 704806425 ▶ if (!b || pt < 0 || pt >= b->pts) return GO_BORDER;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return GO_BORDER; 704806425 0 704806425
if (!b || pt < 0 || pt >= b->pts) return GO_BORDER; 704806424 1 704806425
if (!b || pt < 0 || pt >= b->pts) return GO_BORDER; 1 704806423 704806424
368 704806423 return b->colour[pt];
369 }
370  
371 300 static int go_libs(const struct go_board *b, int pt)
372 {
373 300 ▶ if (!b || pt < 0 || pt >= b->pts) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return -1; 300 0 300
if (!b || pt < 0 || pt >= b->pts) return -1; 300 0 300
if (!b || pt < 0 || pt >= b->pts) return -1; 0 300 300
374 300 ▶ 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 203 300
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return -1; 1 96 97
375 299 return b->chain_libs[b->chain_root[pt]];
376 }
377  
378 638 static int go_chain_size(const struct go_board *b, int pt)
379 {
380 638 ▶ if (!b || pt < 0 || pt >= b->pts) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return 0; 638 0 638
if (!b || pt < 0 || pt >= b->pts) return 0; 638 0 638
if (!b || pt < 0 || pt >= b->pts) return 0; 0 638 638
381 638 ▶ 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 419 638
if (b->colour[pt] != GO_BLACK && b->colour[pt] != GO_WHITE) return 0; 1 218 219
382 637 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 5917 static int go_pack(const struct go_board *b, unsigned char *buf)
399 {
400 5917 int r, c, n = 0;
401 5917 ▶ if (!b || !buf) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !buf) return 0; 5917 0 5917
if (!b || !buf) return 0; 0 5917 5917
402 60164 ▶ for (r = 0; r < b->size; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) 54247 5917 60164
403 561040 ▶ for (c = 0; c < b->size; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) 506793 54247 561040
404 506793 buf[n++] = (unsigned char)b->colour[(r + 1) * b->stride + (c + 1)];
405 5917 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 704351239 static int go_point_of(const struct go_board *b, int col, int row)
419 {
420 704351239 ▶ 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; 704351239 0 704351239
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 704351233 6 704351239
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 704351232 1 704351233
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 704351227 5 704351232
if (!b || col < 0 || row < 0 || col >= b->size || row >= b->size) return -1; 4 704351223 704351227
421 704351223 return (row + 1) * b->stride + (col + 1);
422 }
423  
424 4338 static int go_col_of(const struct go_board *b, int pt)
425 {
426 4338 ▶ if (!b || pt < 0 || pt >= b->pts) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return -1; 4338 0 4338
if (!b || pt < 0 || pt >= b->pts) return -1; 4338 0 4338
if (!b || pt < 0 || pt >= b->pts) return -1; 0 4338 4338
427 4338 return pt % b->stride - 1;
428 }
429  
430 4338 static int go_row_of(const struct go_board *b, int pt)
431 {
432 4338 ▶ if (!b || pt < 0 || pt >= b->pts) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return -1; 4338 0 4338
if (!b || pt < 0 || pt >= b->pts) return -1; 4338 0 4338
if (!b || pt < 0 || pt >= b->pts) return -1; 0 4338 4338
433 4338 return pt / b->stride - 1;
434 }
435  
436 76682 ▶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; } 76682 0 76682
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 677 static struct go_hist *go_hist_new(int size)
469 {
470 struct go_hist *h;
471 677 ▶ 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; 677 0 677
if (size < GO_MIN_SIZE || size > GO_MAX_SIZE) return NULL; 0 677 677
472  
473 677 h = (struct go_hist *)calloc(1, sizeof(struct go_hist));
474 677 ▶ if (!h) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return NULL; 0 677 677
475  
476 677 h->pts = size * size;
477 677 h->cap = 64;
478 677 h->hash = (unsigned long long *)malloc(sizeof(unsigned long long) * (size_t)h->cap);
479 677 h->pos = (unsigned char *)malloc((size_t)h->cap * (size_t)h->pts);
480 677 ▶ if (!h->hash || !h->pos) {
Branch: 2 branches
BranchTrueFalseTotal
if (!h->hash || !h->pos) { 677 0 677
if (!h->hash || !h->pos) { 0 677 677
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 677 return h;
487 }
488  
489 679 static void go_hist_drop(struct go_hist *h)
490 {
491 679 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 679 679
492 679 ▶ if (h->hash) free(h->hash);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hash) free(h->hash); 679 0 679
493 679 ▶ if (h->pos) free(h->pos);
Branch: 1 branch
BranchTrueFalseTotal
if (h->pos) free(h->pos); 679 0 679
494 679 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 5900 static int go_hist_push(struct go_hist *h, const struct go_board *b)
547 {
548 5900 ▶ if (!h || !b) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !b) return 0; 5900 0 5900
if (!h || !b) return 0; 0 5900 5900
549 5900 ▶ 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 5900 5900
550 5900 ▶ 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 5871 5900
if (h->n == h->cap && !hist_grow(h)) return 0; 0 29 29
551 5900 h->hash[h->n] = b->hash;
552 5900 (void)go_pack(b, h->pos + (size_t)h->n * (size_t)h->pts);
553 5900 h->n++;
554 5900 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 223765204 static int dies_to(const struct go_board *b, int q)
594 {
595 223765204 return b->chain_libs[b->chain_root[q]] == 1;
596 }
597  
598 /* The hash the position WOULD have, without touching the board. */
599 56481 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 56481 int nroots = 0;
604 int i, j, cur, them;
605  
606 56481 ▶ if (!b || pt < 0 || pt >= b->pts) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || pt < 0 || pt >= b->pts) return 0; 56481 0 56481
if (!b || pt < 0 || pt >= b->pts) return 0; 56481 0 56481
if (!b || pt < 0 || pt >= b->pts) return 0; 0 56481 56481
607 56481 ▶ if (colour != GO_BLACK && colour != GO_WHITE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 33915 22566 56481
if (colour != GO_BLACK && colour != GO_WHITE) return 0; 0 33915 33915
608  
609 56481 ▶ them = GO_OTHER(colour);
Branch: 1 branch
BranchTrueFalseTotal
them = GO_OTHER(colour); 22566 33915 56481
610 56481 hsh = b->hash ^ zob(colour, pt);
611  
612 282405 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 225924 56481 282405
613 225924 int q = pt + b->nbr[i];
614 225924 int r, seen = 0;
615 225924 ▶ if (b->colour[q] != them) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] != them) continue; 209123 16801 225924
616 16801 ▶ if (!dies_to(b, q)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!dies_to(b, q)) continue; 14769 2032 16801
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 56481 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 255085803 static int go_legal(const struct go_board *b, const struct go_hist *h, int pt, int colour)
687 {
688 255085803 int i, breathes = 0;
689  
690 255085803 ▶ if (!b) return GO_ILL_OFF;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return GO_ILL_OFF; 0 255085803 255085803
691 255085803 ▶ 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; 127750242 127335561 255085803
if (colour != GO_BLACK && colour != GO_WHITE) return GO_ILL_COLOUR; 0 127750242 127750242
692 255085803 ▶ if (pt < 0 || pt >= b->pts) return GO_ILL_OFF;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= b->pts) return GO_ILL_OFF; 255085803 0 255085803
if (pt < 0 || pt >= b->pts) return GO_ILL_OFF; 1 255085802 255085803
693 255085802 ▶ 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 255085801 255085802
694 255085801 ▶ 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 255085784 255085801
695 255085784 ▶ 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; 54227 255031557 255085784
if (pt == b->ko_point && colour == b->ko_colour) return GO_ILL_KO; 54225 2 54227
696  
697 1275157795 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 1020126236 255031559 1275157795
698 1020126236 int q = pt + b->nbr[i];
699 1020126236 int c = b->colour[q];
700 1020126236 ▶ if (c == GO_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == GO_EMPTY) { 475342260 544783976 1020126236
701 475342260 breathes = 1;
702 544783976 ▶ } else if (c == GO_OTHER(colour)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == GO_OTHER(colour)) { 272634943 272149033 544783976
} else if (c == GO_OTHER(colour)) { 223748395 321035581 544783976
703 223748395 ▶ 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 */ 15261306 208487089 223748395
704 321035581 ▶ } else if (c == colour) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == colour) { 218580809 102454772 321035581
705 218580809 ▶ 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; 204639230 13941579 218580809
706 }
707 }
708 255031559 ▶ if (!breathes) return GO_ILL_SUICIDE;
Branch: 1 branch
BranchTrueFalseTotal
if (!breathes) return GO_ILL_SUICIDE; 19935061 235096498 255031559
709  
710 235096498 ▶ if (h && b->superko_on) {
Branch: 2 branches
BranchTrueFalseTotal
if (h && b->superko_on) { 57517 235038981 235096498
if (h && b->superko_on) { 56480 1037 57517
711 unsigned char packed[GO_MAX_PTS];
712 56480 unsigned long long after = go_hash_after(b, pt, colour);
713 int i2;
714 1528805 ▶ for (i2 = 0; i2 < h->n; i2++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i2 = 0; i2 < h->n; i2++) { 1472327 56478 1528805
715 1472327 ▶ if (h->hash[i2] != after) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (h->hash[i2] != after) continue; 1472325 2 1472327
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 235096496 return GO_OK;
722 }
723  
724 1287 static int go_legal_moves(const struct go_board *b, const struct go_hist *h,
725 int colour, int *out)
726 {
727 1287 int r, c, n = 0;
728 1287 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 1287 1287
729 12947 ▶ for (r = 0; r < b->size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) { 11660 1287 12947
730 118038 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 106378 11660 118038
731 106378 int pt = (r + 1) * b->stride + (c + 1);
732 106378 ▶ 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 */ 52506 53872 106378
733 53872 ▶ 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 52256 53872
734 52256 ▶ if (out) out[n] = pt;
Branch: 1 branch
BranchTrueFalseTotal
if (out) out[n] = pt; 52256 0 52256
735 52256 n++;
736 }
737 }
738 1287 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 6280746 static void ko_set(struct go_board *b, int pt, int ncaps, int captured, int colour)
750 {
751 6280746 b->ko_point = -1;
752 6280746 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 6280746 ▶ if (ncaps != 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ncaps != 1) return; 5662354 618392 6280746
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 618392 ▶ if (b->chain_size[b->chain_root[pt]] != 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_size[b->chain_root[pt]] != 1) return; 510956 107436 618392
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 107436 ▶ if (b->chain_libs[b->chain_root[pt]] != 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_libs[b->chain_root[pt]] != 1) return; 53192 54244 107436
768  
769 54244 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 54244 ▶ b->ko_colour = GO_OTHER(colour);
Branch: 1 branch
BranchTrueFalseTotal
b->ko_colour = GO_OTHER(colour); 25224 29020 54244
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 75910 static void analyse(const struct go_board *b, const int *seki, int nseki,
812 struct go_score *out)
813 {
814 75910 analyse_map(b, seki, nseki, out, NULL);
815 75910 }
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 78851 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 78851 out->eyes_b = out->eyes_w = 0;
836 78851 out->territory_b = out->territory_w = 0;
837 78851 out->dame = 0;
838 78851 out->area_b = out->area_w = 0;
839 78851 out->stones_b = b->stones[GO_BLACK];
840 78851 out->stones_w = b->stones[GO_WHITE];
841  
842 10162350 ▶ for (i = 0; i < b->pts; i++) seen[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->pts; i++) seen[i] = 0; 10083499 78851 10162350
843  
844 807318 ▶ for (r = 0; r < b->size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->size; r++) { 728467 78851 807318
845 7582694 ▶ for (c = 0; c < b->size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < b->size; c++) { 6854227 728467 7582694
846 6854227 int p = (r + 1) * b->stride + (c + 1);
847 6854227 int touches_b = 0, touches_w = 0, is_seki = 0;
848  
849 6854227 ▶ if (b->colour[p] != GO_EMPTY || seen[p]) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[p] != GO_EMPTY || seen[p]) continue; 814647 6039580 6854227
if (b->colour[p] != GO_EMPTY || seen[p]) continue; 5188 809459 814647
850  
851 /* the region, and which colours it reaches */
852 809459 n = 0;
853 809459 sp = 0;
854 809459 stack[sp++] = p;
855 809459 seen[p] = 1;
856 1624106 ▶ while (sp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (sp > 0) { 814647 809459 1624106
857 814647 int cur = stack[--sp];
858 814647 region[n++] = cur;
859 4073235 ▶ for (k = 0; k < 4; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 4; k++) { 3258588 814647 4073235
860 3258588 int q = cur + b->nbr[k];
861 3258588 ▶ if (b->colour[q] == GO_BLACK) { touches_b = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == GO_BLACK) { touches_b = 1; continue; } 1611701 1646887 3258588
862 1646887 ▶ if (b->colour[q] == GO_WHITE) { touches_w = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] == GO_WHITE) { touches_w = 1; continue; } 1192757 454130 1646887
863 454130 ▶ if (b->colour[q] != GO_EMPTY) continue; /* the ring */
Branch: 1 branch
BranchTrueFalseTotal
if (b->colour[q] != GO_EMPTY) continue; /* the ring */ 436848 17282 454130
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 809476 ▶ for (i = 0; i < nseki && !is_seki; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nseki && !is_seki; i++) 18 809458 809476
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 809459 ▶ if (touches_b && !touches_w) {
Branch: 2 branches
BranchTrueFalseTotal
if (touches_b && !touches_w) { 462016 347443 809459
if (touches_b && !touches_w) { 461932 84 462016
876 461932 out->eyes_b += n;
877 461932 out->area_b += n;
878 461932 ▶ if (!is_seki) out->territory_b += n;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_seki) out->territory_b += n; 461927 5 461932
879 461932 ▶ if (owner && !is_seki)
Branch: 2 branches
BranchTrueFalseTotal
if (owner && !is_seki) 12554 449378 461932
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 347527 ▶ } else if (touches_w && !touches_b) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (touches_w && !touches_b) { 347526 1 347527
} else if (touches_w && !touches_b) { 347442 84 347526
882 347442 out->eyes_w += n;
883 347442 out->area_w += n;
884 347442 ▶ if (!is_seki) out->territory_w += n;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_seki) out->territory_w += n; 347442 0 347442
885 347442 ▶ if (owner && !is_seki)
Branch: 2 branches
BranchTrueFalseTotal
if (owner && !is_seki) 9867 337575 347442
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 78851 out->area_b += out->stones_b;
895 78851 out->area_w += out->stones_w;
896 78851 }
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 75832 static void go_score_area(const struct go_board *b, struct go_score *out)
928 {
929 75832 ▶ if (!b || !out) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !out) return; 75832 0 75832
if (!b || !out) return; 0 75832 75832
930 75832 analyse(b, NULL, 0, out);
931 75832 out->prisoners_b = out->prisoners_w = 0;
932 75832 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 6280770 static void go_play(struct go_board *b, struct go_hist *h, int pt, int colour,
1150 struct go_played *out)
1151 {
1152 6280770 int i, j, code, nroots = 0;
1153 int roots[4];
1154  
1155 6280794 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 6280770 6280770
1156 6280770 out->code = GO_ILL_OFF;
1157 6280770 out->ncaps = 0;
1158 6280770 out->ko_point = -1;
1159 6280770 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 6280770 6280770
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 6280770 code = go_legal(b, h, pt, colour);
1164 6280770 out->code = code;
1165 6280770 out->ko_point = b->ko_point;
1166 6280770 ▶ if (code != GO_OK) return;
Branch: 1 branch
BranchTrueFalseTotal
if (code != GO_OK) return; 24 6280746 6280770
1167  
1168 6280746 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 31403730 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 25122984 6280746 31403730
1174 25122984 int q = pt + b->nbr[i];
1175 25122984 int r, seen = 0, cur;
1176 25122984 ▶ if (b->colour[q] != GO_OTHER(colour)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (b->colour[q] != GO_OTHER(colour)) continue; 12704276 12418708 25122984
if (b->colour[q] != GO_OTHER(colour)) continue; 18659776 6463208 25122984
1177 6463208 ▶ if (b->chain_libs[b->chain_root[q]] != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->chain_libs[b->chain_root[q]] != 0) continue; 5153785 1309423 6463208
1178 1309423 r = b->chain_root[q];
1179 1481599 ▶ 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 172176 172176
for (j = 0; j < nroots; j++) if (roots[j] == r) { seen = 1; break; } 172176 1309423 1481599
1180 1309423 ▶ if (seen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (seen) continue; 0 1309423 1309423
1181 1309423 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 1309423 cur = r;
1186 3070894 ▶ 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); 1761471 1309423 3070894
1187 1309423 (void)go_lift(b, r);
1188 }
1189  
1190 6280746 ▶ 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); 618392 5662354 6280746
1191 6280746 out->ko_point = b->ko_point;
1192  
1193 6280746 ▶ if (h) (void)go_hist_push(h, b);
Branch: 1 branch
BranchTrueFalseTotal
if (h) (void)go_hist_push(h, b); 5174 6275572 6280746
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 411996 static void go_pass(struct go_board *b, int colour)
1206 {
1207 (void)colour;
1208 411996 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 411996 411996
1209 411996 b->ko_point = -1;
1210 411996 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 65 const struct go_abi *go_abi_table(void)
1236 {
1237 65 ▶ if (!GO_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GO_TABLE_READY) { 48 17 65
1238 48 GO_TABLE.abi_version = GO_ABI_VERSION;
1239  
1240 48 GO_TABLE.board_new = go_board_new;
1241 48 GO_TABLE.board_drop = go_board_drop;
1242 48 GO_TABLE.board_copy = go_board_copy;
1243  
1244 48 GO_TABLE.at = go_at;
1245 48 GO_TABLE.libs = go_libs;
1246 48 GO_TABLE.chain_at = go_chain_at;
1247 48 GO_TABLE.chain_size = go_chain_size;
1248 48 GO_TABLE.pack = go_pack;
1249 48 GO_TABLE.hash = go_hash;
1250 48 GO_TABLE.stones = go_stones;
1251  
1252 48 GO_TABLE.point_of = go_point_of;
1253 48 GO_TABLE.col_of = go_col_of;
1254 48 GO_TABLE.row_of = go_row_of;
1255 48 GO_TABLE.size_of = go_size_of;
1256 48 GO_TABLE.stride_of = go_stride_of;
1257  
1258 48 GO_TABLE.put = go_put;
1259 48 GO_TABLE.lift = go_lift;
1260  
1261 48 GO_TABLE.zobrist = go_zobrist;
1262  
1263 /* appended at version 2. Nothing above this line moved. */
1264 48 GO_TABLE.hist_new = go_hist_new;
1265 48 GO_TABLE.hist_drop = go_hist_drop;
1266 48 GO_TABLE.hist_copy = go_hist_copy;
1267 48 GO_TABLE.hist_len = go_hist_len;
1268 48 GO_TABLE.hist_clear = go_hist_clear;
1269 48 GO_TABLE.hist_push = go_hist_push;
1270 48 GO_TABLE.hist_has = go_hist_has;
1271  
1272 48 GO_TABLE.legal = go_legal;
1273 48 GO_TABLE.legal_moves = go_legal_moves;
1274 48 GO_TABLE.play = go_play;
1275 48 GO_TABLE.pass = go_pass;
1276  
1277 48 GO_TABLE.ko_point = go_ko_point;
1278 48 GO_TABLE.ko_colour = go_ko_colour;
1279 48 GO_TABLE.set_superko = go_set_superko;
1280 48 GO_TABLE.superko_on = go_superko_on;
1281 48 GO_TABLE.hash_after = go_hash_after;
1282  
1283 /* appended at version 3 */
1284 48 GO_TABLE.alive = go_alive;
1285  
1286 /* appended at version 4 */
1287 48 GO_TABLE.score_jp = go_score_jp;
1288 48 GO_TABLE.score_area = go_score_area;
1289  
1290 /* appended at version 5 */
1291 48 GO_TABLE.territory = go_territory;
1292  
1293 /* appended at version 6, from go_search.c */
1294 48 go_search_install(&GO_TABLE);
1295  
1296 48 GO_TABLE_READY = 1;
1297 }
1298 65 return &GO_TABLE;
1299 }
« Go.xs ↑ Summary go_search.c »