fn_engine.c

filter: Statementstmt 100% 298 / 298 Branchbran 90.3% 244 / 270 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.4% 542 / 568 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« fn_search.c ↑ Summary bin/fanorona »
1 /* fn_engine.c - the fanorona board, and nothing that judges a move.
2 *
3 * PERL-FREE by design: see include/fn_abi.h. This file compiles into the XS,
4 * into a plain program, or to wasm, and knows nothing about any of them.
5 *
6 * THIS FILE'S SCOPE. The grid, the padded index, the lines, put and lift, the
7 * side to move, the zobrist key, the position string in and out, and the three
8 * fields of the chain as plain state.
9 */
10  
11 #include <stdlib.h>
12 #include <string.h>
13 #include "fn_abi.h"
14  
15 /* ---- the position ---------------------------------------------------------
16 *
17 * A flat 77-cell array, the size of the board inside it, the side to move, the
18 * stones of each colour, and a key maintained incrementally. No pointers, so a
19 * copy is a struct assignment and two boards never share anything.
20 */
21  
22 struct fn_board {
23 unsigned char cell[FN_CELLS];
24 int cols;
25 int rows;
26 int side;
27 int stones[FN_BLACK + 1];
28 unsigned long long key;
29 int chain_at; /* -1 when no chain is open */
30 int chain_dir;
31 unsigned long long chain_seen; /* a bit a point, by point_bit */
32 };
33  
34 /* Boards handed out and not yet dropped. It exists so a test can show that a
35 * destructor ran exactly once; it is a plain int and is not synchronised. */
36 static int FN_LIVE = 0;
37  
38 /* ---- the zobrist table -----------------------------------------------------
39 *
40 * DERIVED, NOT COMMITTED. A fixed function of a fixed seed is the same in every
41 * process and on every machine, and cannot suffer the transcription error a
42 * hand-pasted array can. splitmix64 is published and has no weak seeds.
43 *
44 * CHANGING THIS GENERATOR, ITS SEED OR THE ORDER THE TABLE IS FILLED IN
45 * CHANGES EVERY KEY. t/02-position.t pins values so that is a red test.
46 */
47  
48 static unsigned long long ZOB[FN_BLACK + 1][FN_CELLS];
49 static unsigned long long ZOB_SIDE;
50 static unsigned long long ZOB_SIZE[FN_COLS_MAX + 1][FN_ROWS_MAX + 1];
51 static int ZOB_READY = 0;
52  
53 13432 static unsigned long long splitmix64(unsigned long long *state)
54 {
55 unsigned long long z;
56 13432 *state += 0x9E3779B97F4A7C15ULL;
57 13432 z = *state;
58 13432 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
59 13432 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
60 13432 return z ^ (z >> 31);
61 }
62  
63 111499773 static void zob_init(void)
64 {
65 111499773 unsigned long long s = 0x66616E6F726F6E61ULL; /* "fanorona" */
66 int p, c, r;
67 111499773 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 111499727 46 111499773
68 184 ▶ for (p = 0; p <= FN_BLACK; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p <= FN_BLACK; p++) 138 46 184
69 10764 ▶ for (c = 0; c < FN_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < FN_CELLS; c++) 10626 138 10764
70 10626 ZOB[p][c] = splitmix64(&s);
71 46 ZOB_SIDE = splitmix64(&s);
72 506 ▶ for (c = 0; c <= FN_COLS_MAX; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c <= FN_COLS_MAX; c++) 460 46 506
73 3220 ▶ for (r = 0; r <= FN_ROWS_MAX; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r <= FN_ROWS_MAX; r++) 2760 460 3220
74 2760 ZOB_SIZE[c][r] = splitmix64(&s);
75 46 ZOB_READY = 1;
76 }
77  
78 /* ---- geometry that does not depend on the size ------------------------------ */
79  
80 1 static int fn_stride(void) { return FN_STRIDE; }
81  
82 /* Odd widths 3 to 9, heights 3 and 5. An even side has no centre point. */
83 2824589 static int fn_size_ok(int cols, int rows)
84 {
85 2824589 ▶ if (cols < 3 || cols > FN_COLS_MAX || cols % 2 == 0) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (cols < 3 || cols > FN_COLS_MAX || cols % 2 == 0) return 0; 2824583 6 2824589
if (cols < 3 || cols > FN_COLS_MAX || cols % 2 == 0) return 0; 2824581 2 2824583
if (cols < 3 || cols > FN_COLS_MAX || cols % 2 == 0) return 0; 8 2824573 2824581
86 2824573 ▶ if (rows != 3 && rows != 5) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rows != 3 && rows != 5) return 0; 2808629 15944 2824573
if (rows != 3 && rows != 5) return 0; 8 2808621 2808629
87 2824565 return 1;
88 }
89  
90 1827994882 static int fn_square_of(int file, int rank)
91 {
92 1827994882 ▶ if (file < 0 || file >= FN_COLS_MAX || rank < 0 || rank >= FN_ROWS_MAX) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (file < 0 || file >= FN_COLS_MAX || rank < 0 || rank >= FN_ROWS_MAX) return -1; 1827994881 1 1827994882
if (file < 0 || file >= FN_COLS_MAX || rank < 0 || rank >= FN_ROWS_MAX) return -1; 1827994880 1 1827994881
if (file < 0 || file >= FN_COLS_MAX || rank < 0 || rank >= FN_ROWS_MAX) return -1; 1827994880 0 1827994880
if (file < 0 || file >= FN_COLS_MAX || rank < 0 || rank >= FN_ROWS_MAX) return -1; 1 1827994879 1827994880
93 1827994879 return (rank + 1) * FN_STRIDE + (file + 1);
94 }
95  
96 16753099861 static int in_grid(int sq)
97 {
98 int f, r;
99 16753099861 ▶ if (sq < 0 || sq >= FN_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (sq < 0 || sq >= FN_CELLS) return 0; 16753099858 3 16753099861
if (sq < 0 || sq >= FN_CELLS) return 0; 5 16753099853 16753099858
100 16753099853 f = sq % FN_STRIDE;
101 16753099853 r = sq / FN_STRIDE;
102 16753099853 ▶ return f >= 1 && f <= FN_COLS_MAX && r >= 1 && r <= FN_ROWS_MAX;
Branch: 4 branches
BranchTrueFalseTotal
return f >= 1 && f <= FN_COLS_MAX && r >= 1 && r <= FN_ROWS_MAX; 16713230814 39869039 16753099853
return f >= 1 && f <= FN_COLS_MAX && r >= 1 && r <= FN_ROWS_MAX; 16668809447 44421367 16713230814
return f >= 1 && f <= FN_COLS_MAX && r >= 1 && r <= FN_ROWS_MAX; 16602784973 66024474 16668809447
return f >= 1 && f <= FN_COLS_MAX && r >= 1 && r <= FN_ROWS_MAX; 16533090014 69694959 16602784973
103 }
104  
105 5486237239 static int fn_file_of(int sq)
106 {
107 5486237239 ▶ if (!in_grid(sq)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!in_grid(sq)) return -1; 2 5486237237 5486237239
108 5486237237 return (sq % FN_STRIDE) - 1;
109 }
110  
111 5486059731 static int fn_rank_of(int sq)
112 {
113 5486059731 ▶ if (!in_grid(sq)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!in_grid(sq)) return -1; 1 5486059730 5486059731
114 5486059730 return (sq / FN_STRIDE) - 1;
115 }
116  
117 /* A STRONG POINT HAS DIAGONALS, and this is the one place that says which. */
118 516213133 static int fn_is_strong(int sq)
119 {
120 516213133 ▶ if (!in_grid(sq)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!in_grid(sq)) return 0; 1 516213132 516213133
121 516213132 return (fn_file_of(sq) + fn_rank_of(sq)) % 2 == 0;
122 }
123  
124 static const int DIR_OFFSET[FN_DIRS] = {
125 FN_STRIDE, FN_STRIDE + 1, 1, -FN_STRIDE + 1,
126 -FN_STRIDE, -FN_STRIDE - 1, -1, FN_STRIDE - 1
127 };
128  
129 225 static int fn_dir_opposite(int dir)
130 {
131 225 ▶ if (dir < 0 || dir >= FN_DIRS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (dir < 0 || dir >= FN_DIRS) return -1; 225 0 225
if (dir < 0 || dir >= FN_DIRS) return -1; 1 224 225
132 224 return (dir + 4) % FN_DIRS;
133 }
134  
135 1267266601 static int fn_dir_is_diagonal(int dir)
136 {
137 1267266601 ▶ if (dir < 0 || dir >= FN_DIRS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (dir < 0 || dir >= FN_DIRS) return 0; 1267266601 0 1267266601
if (dir < 0 || dir >= FN_DIRS) return 0; 0 1267266601 1267266601
138 1267266601 return dir % 2 == 1;
139 }
140  
141 /* ---- the size of one board ---------------------------------------------------- */
142  
143 1731697594 static int fn_cols(const struct fn_board *b) { return b->cols; }
144 209972465 static int fn_rows(const struct fn_board *b) { return b->rows; }
145 4 static int fn_points(const struct fn_board *b) { return b->cols * b->rows; }
146  
147 38 static int fn_centre(const struct fn_board *b)
148 {
149 38 return fn_square_of(b->cols / 2, b->rows / 2);
150 }
151  
152 5180908234 static int fn_on_board(const struct fn_board *b, int sq)
153 {
154 5180908234 ▶ if (!in_grid(sq)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!in_grid(sq)) return 0; 220009842 4960898392 5180908234
155 4960898392 ▶ return fn_file_of(sq) < b->cols && fn_rank_of(sq) < b->rows;
Branch: 2 branches
BranchTrueFalseTotal
return fn_file_of(sq) < b->cols && fn_rank_of(sq) < b->rows; 4960721005 177387 4960898392
return fn_file_of(sq) < b->cols && fn_rank_of(sq) < b->rows; 4960612031 108974 4960721005
156 }
157  
158 /* ---- the lines ---------------------------------------------------------------- */
159  
160 1267266388 static int fn_next(const struct fn_board *b, int sq, int dir)
161 {
162 int to;
163 1267266388 ▶ if (dir < 0 || dir >= FN_DIRS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (dir < 0 || dir >= FN_DIRS) return -1; 1267266387 1 1267266388
if (dir < 0 || dir >= FN_DIRS) return -1; 1 1267266386 1267266387
164 1267266386 ▶ if (!fn_on_board(b, sq)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!fn_on_board(b, sq)) return -1; 9 1267266377 1267266386
165 1267266377 ▶ if (fn_dir_is_diagonal(dir) && !fn_is_strong(sq)) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fn_dir_is_diagonal(dir) && !fn_is_strong(sq)) return -1; 516212838 751053539 1267266377
if (fn_dir_is_diagonal(dir) && !fn_is_strong(sq)) return -1; 143059745 373153093 516212838
166 1124206632 to = sq + DIR_OFFSET[dir];
167 1124206632 ▶ return fn_on_board(b, to) ? to : -1;
Branch: 1 branch
BranchTrueFalseTotal
return fn_on_board(b, to) ? to : -1; 903911017 220295615 1124206632
168 }
169  
170 12 static int fn_degree(const struct fn_board *b, int sq)
171 {
172 12 int d, n = 0;
173 108 ▶ for (d = 0; d < FN_DIRS; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < FN_DIRS; d++) 96 12 108
174 96 ▶ if (fn_next(b, sq, d) >= 0) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (fn_next(b, sq, d) >= 0) n++; 61 35 96
175 12 return n;
176 }
177  
178 /* Each segment is found from its LOWER end, which is the end the four
179 * directions N, NE, E and NW climb or advance away from. SE is left out
180 * because a diagonal that descends is found from its other end as NW. */
181 12 static int fn_segments(const struct fn_board *b, int *out, int max)
182 {
183 static const int forward[4] = { FN_N, FN_NE, FN_E, FN_NW };
184 12 int rank, file, i, n = 0;
185 62 ▶ for (rank = 0; rank < b->rows; rank++) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = 0; rank < b->rows; rank++) { 50 12 62
186 366 ▶ for (file = 0; file < b->cols; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < b->cols; file++) { 316 50 366
187 316 int sq = fn_square_of(file, rank);
188 1580 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 1264 316 1580
189 1264 int to = fn_next(b, sq, forward[i]);
190 1264 ▶ if (to < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (to < 0) continue; 552 712 1264
191 712 ▶ if (out && n < max) {
Branch: 2 branches
BranchTrueFalseTotal
if (out && n < max) { 712 0 712
if (out && n < max) { 712 0 712
192 712 out[2 * n] = sq < to ? sq : to;
193 712 out[2 * n + 1] = sq < to ? to : sq;
194 }
195 712 n++;
196 }
197 }
198 }
199 12 return n;
200 }
201  
202 /* ---- the key ----------------------------------------------------------------
203 *
204 * The key covers the size of the board, the stones, their points, and the side
205 * to move, and NOTHING else, so a position repeating is a position repeating.
206 */
207  
208 74556218 static unsigned long long fn_zobrist(int stone, int sq)
209 {
210 74556218 zob_init();
211 74556218 ▶ if (!FN_IS_STONE(stone)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!FN_IS_STONE(stone)) return 0; 34006452 40549766 74556218
if (!FN_IS_STONE(stone)) return 0; 2 34006450 34006452
212 74556216 ▶ if (!in_grid(sq)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!in_grid(sq)) return 0; 1 74556215 74556216
213 74556215 return ZOB[stone][sq];
214 }
215  
216 35997411 static unsigned long long fn_zobrist_side(void)
217 {
218 35997411 zob_init();
219 35997411 return ZOB_SIDE;
220 }
221  
222 946098 static unsigned long long fn_zobrist_size(int cols, int rows)
223 {
224 946098 zob_init();
225 946098 ▶ if (!fn_size_ok(cols, rows)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!fn_size_ok(cols, rows)) return 0; 1 946097 946098
226 946097 return ZOB_SIZE[cols][rows];
227 }
228  
229 /* ---- structure, judging nothing ------------------------------------------------ */
230  
231 938088 static struct fn_board *fn_board_empty(int cols, int rows)
232 {
233 struct fn_board *b;
234 int rank, file;
235 938088 ▶ if (!fn_size_ok(cols, rows)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!fn_size_ok(cols, rows)) return NULL; 0 938088 938088
236 938088 b = (struct fn_board *) malloc(sizeof(struct fn_board));
237 938088 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 938088 938088
238 938088 memset(b, 0, sizeof(struct fn_board));
239 938088 memset(b->cell, FN_BORDER, sizeof(b->cell));
240 938088 b->cols = cols;
241 938088 b->rows = rows;
242 5618444 ▶ for (rank = 0; rank < rows; rank++)
Branch: 1 branch
BranchTrueFalseTotal
for (rank = 0; rank < rows; rank++) 4680356 938088 5618444
243 46691884 ▶ for (file = 0; file < cols; file++)
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < cols; file++) 42011528 4680356 46691884
244 42011528 b->cell[fn_square_of(file, rank)] = FN_EMPTY;
245 938088 b->side = FN_WHITE;
246 938088 b->key = fn_zobrist_size(cols, rows);
247 938088 b->chain_at = -1;
248 938088 b->chain_dir = -1;
249 938088 FN_LIVE++;
250 938088 return b;
251 }
252  
253 74393293 static void fn_put(struct fn_board *b, int sq, int stone)
254 {
255 int old;
256 74393293 ▶ if (!fn_on_board(b, sq)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!fn_on_board(b, sq)) return; 171 74393122 74393293
257 74393122 ▶ if (stone != FN_EMPTY && !FN_IS_STONE(stone)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (stone != FN_EMPTY && !FN_IS_STONE(stone)) return; 38734309 35658813 74393122
if (stone != FN_EMPTY && !FN_IS_STONE(stone)) return; 17690565 21043744 38734309
if (stone != FN_EMPTY && !FN_IS_STONE(stone)) return; 2 17690563 17690565
258 74393120 old = b->cell[sq];
259 74393120 ▶ if (old != FN_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (old != FN_EMPTY) { 35658493 38734627 74393120
260 35658493 b->key ^= fn_zobrist(old, sq);
261 35658493 b->stones[old]--;
262 }
263 74393120 b->cell[sq] = (unsigned char) stone;
264 74393120 ▶ if (stone != FN_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (stone != FN_EMPTY) { 38734307 35658813 74393120
265 38734307 b->key ^= fn_zobrist(stone, sq);
266 38734307 b->stones[stone]++;
267 }
268 }
269  
270 35655884 static void fn_lift(struct fn_board *b, int sq)
271 {
272 35655884 fn_put(b, sq, FN_EMPTY);
273 35655884 }
274  
275 2699800424 static int fn_at(const struct fn_board *b, int sq)
276 {
277 2699800424 ▶ if (!fn_on_board(b, sq)) return FN_BORDER;
Branch: 1 branch
BranchTrueFalseTotal
if (!fn_on_board(b, sq)) return FN_BORDER; 204 2699800220 2699800424
278 2699800220 return b->cell[sq];
279 }
280  
281 86190026 static int fn_side(const struct fn_board *b) { return b->side; }
282  
283 37492968 static void fn_set_side(struct fn_board *b, int side)
284 {
285 37492968 ▶ if (!FN_IS_STONE(side)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!FN_IS_STONE(side)) return; 18671158 18821810 37492968
if (!FN_IS_STONE(side)) return; 1 18671157 18671158
286 37492967 ▶ if (side == b->side) return;
Branch: 1 branch
BranchTrueFalseTotal
if (side == b->side) return; 1499514 35993453 37492967
287 35993453 b->side = side;
288 35993453 b->key ^= fn_zobrist_side();
289 }
290  
291 16369935 static int fn_count(const struct fn_board *b, int stone)
292 {
293 16369935 ▶ if (!FN_IS_STONE(stone)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!FN_IS_STONE(stone)) return 0; 8239749 8130186 16369935
if (!FN_IS_STONE(stone)) return 0; 0 8239749 8239749
294 16369935 return b->stones[stone];
295 }
296  
297 26127392 static unsigned long long fn_key(const struct fn_board *b) { return b->key; }
298  
299 /* The key from nothing. A test holds the maintained key to it after every
300 * kind of change, which is the only way a missed update shows. */
301 8007 static unsigned long long fn_key_full(const struct fn_board *b)
302 {
303 8007 unsigned long long k = fn_zobrist_size(b->cols, b->rows);
304 int rank, file;
305 48042 ▶ for (rank = 0; rank < b->rows; rank++) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = 0; rank < b->rows; rank++) { 40035 8007 48042
306 400350 ▶ for (file = 0; file < b->cols; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < b->cols; file++) { 360315 40035 400350
307 360315 int sq = fn_square_of(file, rank);
308 360315 int p = b->cell[sq];
309 360315 ▶ if (p != FN_EMPTY) k ^= fn_zobrist(p, sq);
Branch: 1 branch
BranchTrueFalseTotal
if (p != FN_EMPTY) k ^= fn_zobrist(p, sq); 163412 196903 360315
310 }
311 }
312 8007 ▶ if (b->side == FN_BLACK) k ^= fn_zobrist_side();
Branch: 1 branch
BranchTrueFalseTotal
if (b->side == FN_BLACK) k ^= fn_zobrist_side(); 3957 4050 8007
313 8007 return k;
314 }
315  
316 /* ---- the chain ------------------------------------------------------------------
317 *
318 * Which stone is in the middle of its move, the way it last went, and where it
319 * has stood. THE KEY DOES NOT COVER IT: a position is compared between moves,
320 * and between moves there is no chain. The rules that read it are fn_moves.c's;
321 * here it is three fields set together.
322 */
323  
324 9125308 static int fn_point_bit(int sq)
325 {
326 9125308 ▶ if (!in_grid(sq)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!in_grid(sq)) return -1; 0 9125308 9125308
327 9125308 return fn_rank_of(sq) * FN_COLS_MAX + fn_file_of(sq);
328 }
329  
330 97323362 static int fn_chain_at(const struct fn_board *b) { return b->chain_at; }
331 20484577 ▶static int fn_chain_dir(const struct fn_board *b) { return b->chain_at < 0 ? -1 : b->chain_dir; }
Branch: 1 branch
BranchTrueFalseTotal
static int fn_chain_dir(const struct fn_board *b) { return b->chain_at < 0 ? -1 : b->chain_dir; } 5845497 14639080 20484577
332  
333 15801044 static unsigned long long fn_chain_mask(const struct fn_board *b)
334 {
335 15801044 ▶ return b->chain_at < 0 ? 0 : b->chain_seen;
Branch: 1 branch
BranchTrueFalseTotal
return b->chain_at < 0 ? 0 : b->chain_seen; 1161966 14639078 15801044
336 }
337  
338 4097297 static int fn_chain_seen(const struct fn_board *b, int sq)
339 {
340 4097297 int bit = fn_point_bit(sq);
341 4097297 ▶ if (bit < 0 || b->chain_at < 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (bit < 0 || b->chain_at < 0) return 0; 4097297 0 4097297
if (bit < 0 || b->chain_at < 0) return 0; 90 4097207 4097297
342 4097207 return (int) ((b->chain_seen >> bit) & 1ULL);
343 }
344  
345 33964940 static void fn_chain_set(struct fn_board *b, int at, int dir, unsigned long long mask)
346 {
347 33964940 ▶ if (at < 0 || !fn_on_board(b, at)) {
Branch: 2 branches
BranchTrueFalseTotal
if (at < 0 || !fn_on_board(b, at)) { 4170799 29794141 33964940
if (at < 0 || !fn_on_board(b, at)) { 0 4170799 4170799
348 29794141 b->chain_at = -1;
349 29794141 b->chain_dir = -1;
350 29794141 b->chain_seen = 0;
351 29794141 return;
352 }
353 4170799 b->chain_at = at;
354 4170799 ▶ b->chain_dir = (dir >= 0 && dir < FN_DIRS) ? dir : -1;
Branch: 2 branches
BranchTrueFalseTotal
b->chain_dir = (dir >= 0 && dir < FN_DIRS) ? dir : -1; 4170799 0 4170799
b->chain_dir = (dir >= 0 && dir < FN_DIRS) ? dir : -1; 4170799 0 4170799
355 4170799 b->chain_seen = mask;
356 }
357  
358 /* ---- the set-up ---------------------------------------------------------------
359 *
360 * The rows below the middle are White's and the rows above it Black's. The
361 * middle row alternates OUTWARD from the centre, which is empty: the point
362 * directly left of the centre is White and the point directly right of it is
363 * Black. On the nine by five that is b w b w . b w b w.
364 */
365  
366 105761 static int setup_stone(int cols, int rows, int file, int rank)
367 {
368 105761 int mid_rank = rows / 2, mid_file = cols / 2, away;
369 105761 ▶ if (rank < mid_rank) return FN_WHITE;
Branch: 1 branch
BranchTrueFalseTotal
if (rank < mid_rank) return FN_WHITE; 41863 63898 105761
370 63898 ▶ if (rank > mid_rank) return FN_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if (rank > mid_rank) return FN_BLACK; 41863 22035 63898
371 22035 ▶ if (file == mid_file) return FN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (file == mid_file) return FN_EMPTY; 2929 19106 22035
372 19106 ▶ if (file < mid_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (file < mid_file) { 9553 9553 19106
373 9553 away = mid_file - file;
374 9553 ▶ return away % 2 == 1 ? FN_WHITE : FN_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
return away % 2 == 1 ? FN_WHITE : FN_BLACK; 5127 4426 9553
375 }
376 9553 away = file - mid_file;
377 9553 ▶ return away % 2 == 1 ? FN_BLACK : FN_WHITE;
Branch: 1 branch
BranchTrueFalseTotal
return away % 2 == 1 ? FN_BLACK : FN_WHITE; 5127 4426 9553
378 }
379  
380 2929 static struct fn_board *fn_board_new(int cols, int rows)
381 {
382 2929 struct fn_board *b = fn_board_empty(cols, rows);
383 int rank, file;
384 2929 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 2929 2929
385 16248 ▶ for (rank = 0; rank < rows; rank++)
Branch: 1 branch
BranchTrueFalseTotal
for (rank = 0; rank < rows; rank++) 13319 2929 16248
386 119080 ▶ for (file = 0; file < cols; file++)
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < cols; file++) 105761 13319 119080
387 105761 fn_put(b, fn_square_of(file, rank), setup_stone(cols, rows, file, rank));
388 2929 return b;
389 }
390  
391 498771 static struct fn_board *fn_board_copy(const struct fn_board *b)
392 {
393 struct fn_board *n;
394 498771 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 498771 498771
395 498771 n = (struct fn_board *) malloc(sizeof(struct fn_board));
396 498771 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 498771 498771
397 498771 *n = *b;
398 498771 FN_LIVE++;
399 498771 return n;
400 }
401  
402 1436859 static void fn_board_drop(struct fn_board *b)
403 {
404 1436859 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 1436859 1436859
405 1436859 FN_LIVE--;
406 1436859 free(b);
407 }
408  
409 12 static int fn_live(void) { return FN_LIVE; }
410  
411 /* ---- the position string ------------------------------------------------------
412 *
413 * bbbbbbbbb/bbbbbbbbb/bwbw1bwbw/wwwwwwwww/wwwwwwwww w
414 *
415 * Rows from the TOP rank down, `w` and `b`, a digit 1 to 9 for a run of empty
416 * points, `/` between rows, one space, and the side to move. The size of the
417 * board is read from the string: the number of rows and their width.
418 */
419  
420 938288 static struct fn_board *fn_board_of_string(const char *str, int *err)
421 {
422 struct fn_board *b;
423 const char *s, *end;
424 int width[FN_ROWS_MAX + 1];
425 938288 int nrows = 1, i, rank, file, cols, side;
426 938288 int code = FN_OK;
427 size_t len;
428  
429 938288 ▶ if (err) *err = FN_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = FN_OK; 938288 0 938288
430 938288 ▶ if (!str || !*str) { code = FN_POS_NULL; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (!str || !*str) { code = FN_POS_NULL; goto refused; } 938288 0 938288
if (!str || !*str) { code = FN_POS_NULL; goto refused; } 2 938286 938288
431 938286 len = strlen(str);
432 938286 ▶ if (len >= FN_POS_MAX) { code = FN_POS_LONG; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (len >= FN_POS_MAX) { code = FN_POS_LONG; goto refused; } 1 938285 938286
433  
434 14476650 ▶ for (end = str; *end && *end != ' '; end++) {
Branch: 2 branches
BranchTrueFalseTotal
for (end = str; *end && *end != ' '; end++) { 14476567 83 14476650
for (end = str; *end && *end != ' '; end++) { 13539579 936988 14476567
435 13539579 char c = *end;
436 13539579 ▶ if (c != 'w' && c != 'b' && c != '/' && !(c >= '1' && c <= '9')) {
Branch: 5 branches
BranchTrueFalseTotal
if (c != 'w' && c != 'b' && c != '/' && !(c >= '1' && c <= '9')) { 11917323 1622256 13539579
if (c != 'w' && c != 'b' && c != '/' && !(c >= '1' && c <= '9')) { 10452670 1464653 11917323
if (c != 'w' && c != 'b' && c != '/' && !(c >= '1' && c <= '9')) { 6714104 3738566 10452670
if (c != 'w' && c != 'b' && c != '/' && !(c >= '1' && c <= '9')) { 6713708 396 6714104
if (c != 'w' && c != 'b' && c != '/' && !(c >= '1' && c <= '9')) { 818 6712890 6713708
437 1214 code = FN_POS_LETTER;
438 1214 goto refused;
439 }
440 }
441  
442 14462337 ▶ for (s = str; s < end; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; s < end; s++) 13525266 937071 14462337
443 13525266 ▶ if (*s == '/') nrows++;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') nrows++; 3736783 9788483 13525266
444 937071 ▶ if (nrows != 3 && nrows != 5) { code = FN_POS_ROWS; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (nrows != 3 && nrows != 5) { code = FN_POS_ROWS; goto refused; } 931872 5199 937071
if (nrows != 3 && nrows != 5) { code = FN_POS_ROWS; goto refused; } 749 931123 931872
445  
446 6554254 ▶ for (i = 0; i <= FN_ROWS_MAX; i++) width[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= FN_ROWS_MAX; i++) width[i] = 0; 5617932 936322 6554254
447 14448838 ▶ for (s = str, i = 0; s < end; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = str, i = 0; s < end; s++) { 13512516 936322 14448838
448 13512516 ▶ if (*s == '/') { i++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { i++; continue; } 3734890 9777626 13512516
449 9777626 ▶ width[i] += (*s >= '1' && *s <= '9') ? (*s - '0') : 1;
Branch: 2 branches
BranchTrueFalseTotal
width[i] += (*s >= '1' && *s <= '9') ? (*s - '0') : 1; 9777626 0 9777626
width[i] += (*s >= '1' && *s <= '9') ? (*s - '0') : 1; 6708666 3068960 9777626
450 }
451 936322 cols = width[0];
452 4667864 ▶ for (i = 1; i < nrows; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < nrows; i++) 3732978 934886 4667864
453 3732978 ▶ if (width[i] != cols) { code = FN_POS_WIDTH; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (width[i] != cols) { code = FN_POS_WIDTH; goto refused; } 1436 3731542 3732978
454 934886 ▶ if (!fn_size_ok(cols, nrows)) { code = FN_POS_WIDTH; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (!fn_size_ok(cols, nrows)) { code = FN_POS_WIDTH; goto refused; } 2 934884 934886
455  
456 934884 ▶ if (end[0] != ' ' || (end[1] != 'w' && end[1] != 'b') || end[2] != '\0') {
Branch: 4 branches
BranchTrueFalseTotal
if (end[0] != ' ' || (end[1] != 'w' && end[1] != 'b') || end[2] != '\0') { 934883 1 934884
if (end[0] != ' ' || (end[1] != 'w' && end[1] != 'b') || end[2] != '\0') { 455020 479863 934883
if (end[0] != ' ' || (end[1] != 'w' && end[1] != 'b') || end[2] != '\0') { 454873 147 455020
if (end[0] != ' ' || (end[1] != 'w' && end[1] != 'b') || end[2] != '\0') { 170 934566 934736
457 318 code = FN_POS_SIDE;
458 318 goto refused;
459 }
460 934566 ▶ side = end[1] == 'w' ? FN_WHITE : FN_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
side = end[1] == 'w' ? FN_WHITE : FN_BLACK; 479727 454839 934566
461  
462 934566 b = fn_board_empty(cols, nrows);
463 934566 ▶ if (!b) { code = FN_POS_NULL; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { code = FN_POS_NULL; goto refused; } 0 934566 934566
464 934566 rank = nrows - 1;
465 934566 file = 0;
466 14409746 ▶ for (s = str; s < end; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; s < end; s++) { 13475180 934566 14409746
467 13475180 ▶ if (*s == '/') { rank--; file = 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { rank--; file = 0; continue; } 3729508 9745672 13475180
468 9745672 ▶ if (*s >= '1' && *s <= '9') { file += *s - '0'; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*s >= '1' && *s <= '9') { file += *s - '0'; continue; } 9745672 0 9745672
if (*s >= '1' && *s <= '9') { file += *s - '0'; continue; } 6702180 3043492 9745672
469 3043492 ▶ fn_put(b, fn_square_of(file, rank), *s == 'w' ? FN_WHITE : FN_BLACK);
Branch: 1 branch
BranchTrueFalseTotal
fn_put(b, fn_square_of(file, rank), *s == 'w' ? FN_WHITE : FN_BLACK); 1603097 1440395 3043492
470 3043492 file++;
471 }
472 934566 fn_set_side(b, side);
473 934566 return b;
474  
475 3722 refused:
476 3722 ▶ if (err) *err = code;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = code; 3722 0 3722
477 3722 return NULL;
478 }
479  
480 /* Writes the string and returns its length, or -1 when `len` cannot hold it. */
481 2901783 static int fn_to_string(const struct fn_board *b, char *buf, int len)
482 {
483 char tmp[FN_POS_MAX];
484 2901783 int n = 0, rank, file, run;
485 17395650 ▶ for (rank = b->rows - 1; rank >= 0; rank--) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = b->rows - 1; rank >= 0; rank--) { 14493867 2901783 17395650
486 14493867 run = 0;
487 144772746 ▶ for (file = 0; file < b->cols; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < b->cols; file++) { 130278879 14493867 144772746
488 130278879 int p = b->cell[fn_square_of(file, rank)];
489 130278879 ▶ if (p == FN_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (p == FN_EMPTY) { run++; continue; } 121167237 9111642 130278879
490 9111642 ▶ if (run) { tmp[n++] = (char) ('0' + run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { tmp[n++] = (char) ('0' + run); run = 0; } 7388221 1723421 9111642
491 9111642 ▶ tmp[n++] = p == FN_WHITE ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
tmp[n++] = p == FN_WHITE ? 'w' : 'b'; 4813873 4297769 9111642
492 }
493 14493867 ▶ if (run) tmp[n++] = (char) ('0' + run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) tmp[n++] = (char) ('0' + run); 13487013 1006854 14493867
494 14493867 ▶ if (rank > 0) tmp[n++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (rank > 0) tmp[n++] = '/'; 11592084 2901783 14493867
495 }
496 2901783 tmp[n++] = ' ';
497 2901783 ▶ tmp[n++] = b->side == FN_WHITE ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
tmp[n++] = b->side == FN_WHITE ? 'w' : 'b'; 1460935 1440848 2901783
498 2901783 ▶ if (!buf || len < n + 1) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || len < n + 1) return -1; 2901783 0 2901783
if (!buf || len < n + 1) return -1; 0 2901783 2901783
499 2901783 memcpy(buf, tmp, (size_t) n);
500 2901783 buf[n] = '\0';
501 2901783 return n;
502 }
503  
504 /* ---- the table ----------------------------------------------------------------
505 *
506 * fn_moves.c, fn_rules.c and fn_search.c install themselves into the table here. The two files call into each
507 * other and it terminates: FN_TABLE_READY is set before the installer runs, and
508 * fn_moves.c only reaches fn_abi_table from inside a function the installer
509 * has already handed over.
510 */
511  
512 static struct fn_abi FN_TABLE;
513 static int FN_TABLE_READY = 0;
514  
515 void fn_moves_install(struct fn_abi *t);
516 void fn_rules_install(struct fn_abi *t);
517 void fn_search_install(struct fn_abi *t);
518  
519 3975289652 const struct fn_abi *fn_abi_table(void)
520 {
521 3975289652 ▶ if (!FN_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!FN_TABLE_READY) { 46 3975289606 3975289652
522 46 zob_init();
523 46 FN_TABLE.abi_version = FN_ABI_VERSION;
524  
525 46 FN_TABLE.board_new = fn_board_new;
526 46 FN_TABLE.board_empty = fn_board_empty;
527 46 FN_TABLE.board_of_string = fn_board_of_string;
528 46 FN_TABLE.board_copy = fn_board_copy;
529 46 FN_TABLE.board_drop = fn_board_drop;
530 46 FN_TABLE.live = fn_live;
531  
532 46 FN_TABLE.at = fn_at;
533 46 FN_TABLE.put = fn_put;
534 46 FN_TABLE.lift = fn_lift;
535 46 FN_TABLE.side = fn_side;
536 46 FN_TABLE.set_side = fn_set_side;
537 46 FN_TABLE.count = fn_count;
538  
539 46 FN_TABLE.cols = fn_cols;
540 46 FN_TABLE.rows = fn_rows;
541 46 FN_TABLE.points = fn_points;
542 46 FN_TABLE.centre = fn_centre;
543 46 FN_TABLE.on_board = fn_on_board;
544  
545 46 FN_TABLE.key = fn_key;
546 46 FN_TABLE.key_full = fn_key_full;
547 46 FN_TABLE.zobrist = fn_zobrist;
548 46 FN_TABLE.zobrist_side = fn_zobrist_side;
549 46 FN_TABLE.zobrist_size = fn_zobrist_size;
550 46 FN_TABLE.to_string = fn_to_string;
551  
552 46 FN_TABLE.size_ok = fn_size_ok;
553 46 FN_TABLE.stride = fn_stride;
554 46 FN_TABLE.square_of = fn_square_of;
555 46 FN_TABLE.file_of = fn_file_of;
556 46 FN_TABLE.rank_of = fn_rank_of;
557 46 FN_TABLE.is_strong = fn_is_strong;
558 46 FN_TABLE.dir_opposite = fn_dir_opposite;
559 46 FN_TABLE.dir_is_diagonal = fn_dir_is_diagonal;
560  
561 46 FN_TABLE.next = fn_next;
562 46 FN_TABLE.degree = fn_degree;
563 46 FN_TABLE.segments = fn_segments;
564  
565 46 FN_TABLE.chain_at = fn_chain_at;
566 46 FN_TABLE.chain_dir = fn_chain_dir;
567 46 FN_TABLE.chain_seen = fn_chain_seen;
568 46 FN_TABLE.chain_mask = fn_chain_mask;
569 46 FN_TABLE.chain_set = fn_chain_set;
570 46 FN_TABLE.point_bit = fn_point_bit;
571  
572 46 FN_TABLE_READY = 1; /* before the installer, or it recurses */
573 46 fn_moves_install(&FN_TABLE);
574 46 fn_rules_install(&FN_TABLE);
575 46 fn_search_install(&FN_TABLE);
576 }
577 3975289652 return &FN_TABLE;
578 }
« fn_search.c ↑ Summary bin/fanorona »