Fanorona.xs

filter: Statementstmt 97.0% 266 / 274 Branchbran 72.0% 111 / 154 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.0% 377 / 428 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/Fanorona/Move.pm ↑ Summary fn_rules.c »
1 /* Fanorona.xs - the door to the C board.
2 *
3 * At the TOP of the distribution and not under lib/, because an XSMULTI build's
4 * export list and Strawberry's import library rule never agree on a name. The
5 * siblings that learned this the hard way say so in Go.xs and Xiangqi.xs.
6 *
7 * The XSUBs land in Game::Fanorona::Engine, not in Game::Fanorona, and are named
8 * with a leading underscore: they take a raw pointer as a UV and there is no
9 * typemap. The Perl that owns the lifetime is lib/Game/Fanorona/Engine.pm.
10 *
11 * `_put` and `_lift` are the structure primitives from fn_abi.h and CHECK
12 * NOTHING.
13 */
14  
15 #define PERL_NO_GET_CONTEXT
16  
17 #include "EXTERN.h"
18 #include "perl.h"
19 #include "XSUB.h"
20  
21 #include "fn_abi.h"
22  
23 static const struct fn_abi *FN = NULL;
24  
25 #define BOARD(u) INT2PTR(struct fn_board *, (u))
26  
27 /* A 64-bit value crosses as a 16-character hex string, never as a UV.
28 *
29 * On a perl with 32-bit IVs a UV cannot hold it and the top half would vanish
30 * silently, which for a zobrist key means a test that passes while comparing
31 * half a number. Hand-rolled rather than via my_snprintf, as Brandubh.xs has it.
32 */
33 36519 static SV *fn_hex64(pTHX_ unsigned long long v)
34 {
35 static const char digits[] = "0123456789abcdef";
36 char buf[16];
37 int i;
38 620823 ▶ for (i = 15; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 15; i >= 0; i--) { 584304 36519 620823
39 584304 buf[i] = digits[v & 0xfULL];
40 584304 v >>= 4;
41 }
42 36519 return newSVpvn(buf, 16);
43 }
44  
45 MODULE = Game::Fanorona PACKAGE = Game::Fanorona::Engine
46  
47 PROTOTYPES: DISABLE
48  
49 BOOT:
50 43 FN = fn_abi_table();
51  
52 UV
53 _abi_ptr()
54 CODE:
55 1 ▶ RETVAL = PTR2UV(FN);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(FN); 0 1 1
56 OUTPUT:
57 RETVAL
58  
59 UV
60 _abi_version()
61 CODE:
62 2 ▶ RETVAL = (UV)FN->abi_version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)FN->abi_version; 1 1 2
63 OUTPUT:
64 RETVAL
65  
66 # A size the engine does not build answers 0, which the Perl turns into a
67 # croak with the size in it.
68 UV
69 _new_board(cols, rows)
70 int cols
71 int rows
72 CODE:
73 1919 ▶ RETVAL = PTR2UV((FN->board_new)(cols, rows));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((FN->board_new)(cols, rows)); 1890 29 1919
74 OUTPUT:
75 RETVAL
76  
77 UV
78 _new_empty(cols, rows)
79 int cols
80 int rows
81 CODE:
82 593 ▶ RETVAL = PTR2UV((FN->board_empty)(cols, rows));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((FN->board_empty)(cols, rows)); 587 6 593
83 OUTPUT:
84 RETVAL
85  
86 UV
87 _copy_board(u)
88 UV u
89 CODE:
90 4 ▶ RETVAL = PTR2UV((FN->board_copy)(BOARD(u)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((FN->board_copy)(BOARD(u))); 1 3 4
91 OUTPUT:
92 RETVAL
93  
94 # The board is let go by an object of its own, held by the board's object and
95 # freed with it. Its DESTROY is C and calls no perl.
96 SV *
97 _hold_board(u)
98 UV u
99 CODE:
100 472180 RETVAL = sv_setref_uv(newSV(0), "Game::Fanorona::Engine::Hold", u);
101 OUTPUT:
102 RETVAL
103  
104 int
105 _live()
106 CODE:
107 12 RETVAL = (FN->live)();
108 OUTPUT:
109 RETVAL
110  
111 # A string that will not parse returns undef and the code, rather than
112 # croaking: the reason is what a caller shows a person, and six of them are
113 # distinguishable.
114 void
115 _of_string(str)
116 const char *str
117 PREINIT:
118 struct fn_board *b;
119 473385 int err = 0;
120 PPCODE:
121 473385 b = (FN->board_of_string)(str, &err);
122 473385 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 473385 473385
123 473385 ▶ if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b))));
Branch: 1 branch
BranchTrueFalseTotal
if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); 469664 3721 473385
124 3721 else PUSHs(&PL_sv_undef);
125 473385 PUSHs(sv_2mortal(newSViv(err)));
126  
127 SV *
128 _to_string(u)
129 UV u
130 PREINIT:
131 char buf[FN_POS_MAX];
132 int n;
133 CODE:
134 2900443 n = (FN->to_string)(BOARD(u), buf, (int)sizeof(buf));
135 2900443 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 2900443 2900443
136 2900443 RETVAL = newSVpvn(buf, (STRLEN)n);
137 OUTPUT:
138 RETVAL
139  
140 int
141 _at(u, sq)
142 UV u
143 int sq
144 CODE:
145 419452 RETVAL = (FN->at)(BOARD(u), sq);
146 OUTPUT:
147 RETVAL
148  
149 void
150 _put(u, sq, stone)
151 UV u
152 int sq
153 int stone
154 CODE:
155 192742 (FN->put)(BOARD(u), sq, stone);
156  
157 void
158 _lift(u, sq)
159 UV u
160 int sq
161 CODE:
162 181702 (FN->lift)(BOARD(u), sq);
163  
164 int
165 _side(u)
166 UV u
167 CODE:
168 25456 RETVAL = (FN->side)(BOARD(u));
169 OUTPUT:
170 RETVAL
171  
172 void
173 _set_side(u, side)
174 UV u
175 int side
176 CODE:
177 1862 (FN->set_side)(BOARD(u), side);
178  
179 int
180 _count(u, stone)
181 UV u
182 int stone
183 CODE:
184 30123 RETVAL = (FN->count)(BOARD(u), stone);
185 OUTPUT:
186 RETVAL
187  
188 int
189 _board_cols(u)
190 UV u
191 CODE:
192 6599 RETVAL = (FN->cols)(BOARD(u));
193 OUTPUT:
194 RETVAL
195  
196 int
197 _board_rows(u)
198 UV u
199 CODE:
200 6597 RETVAL = (FN->rows)(BOARD(u));
201 OUTPUT:
202 RETVAL
203  
204 int
205 _board_points(u)
206 UV u
207 CODE:
208 4 RETVAL = (FN->points)(BOARD(u));
209 OUTPUT:
210 RETVAL
211  
212 int
213 _board_centre(u)
214 UV u
215 CODE:
216 38 RETVAL = (FN->centre)(BOARD(u));
217 OUTPUT:
218 RETVAL
219  
220 int
221 _on_board(u, sq)
222 UV u
223 int sq
224 CODE:
225 1052 RETVAL = (FN->on_board)(BOARD(u), sq);
226 OUTPUT:
227 RETVAL
228  
229 SV *
230 _key_hex(u)
231 UV u
232 CODE:
233 28493 RETVAL = fn_hex64(aTHX_ (FN->key)(BOARD(u)));
234 OUTPUT:
235 RETVAL
236  
237 SV *
238 _key_full_hex(u)
239 UV u
240 CODE:
241 8007 RETVAL = fn_hex64(aTHX_ (FN->key_full)(BOARD(u)));
242 OUTPUT:
243 RETVAL
244  
245 SV *
246 _zobrist_hex(stone, sq)
247 int stone
248 int sq
249 CODE:
250 6 RETVAL = fn_hex64(aTHX_ (FN->zobrist)(stone, sq));
251 OUTPUT:
252 RETVAL
253  
254 SV *
255 _zobrist_side_hex()
256 CODE:
257 1 RETVAL = fn_hex64(aTHX_ (FN->zobrist_side)());
258 OUTPUT:
259 RETVAL
260  
261 SV *
262 _zobrist_size_hex(cols, rows)
263 int cols
264 int rows
265 CODE:
266 3 RETVAL = fn_hex64(aTHX_ (FN->zobrist_size)(cols, rows));
267 OUTPUT:
268 RETVAL
269  
270 int
271 _size_ok(cols, rows)
272 int cols
273 int rows
274 CODE:
275 3965 RETVAL = (FN->size_ok)(cols, rows);
276 OUTPUT:
277 RETVAL
278  
279 int
280 _stride()
281 CODE:
282 1 RETVAL = (FN->stride)();
283 OUTPUT:
284 RETVAL
285  
286 int
287 _square_of(file, rank)
288 int file
289 int rank
290 CODE:
291 239540 RETVAL = (FN->square_of)(file, rank);
292 OUTPUT:
293 RETVAL
294  
295 int
296 _file_of(sq)
297 int sq
298 CODE:
299 407 RETVAL = (FN->file_of)(sq);
300 OUTPUT:
301 RETVAL
302  
303 int
304 _rank_of(sq)
305 int sq
306 CODE:
307 286 RETVAL = (FN->rank_of)(sq);
308 OUTPUT:
309 RETVAL
310  
311 int
312 _is_strong(sq)
313 int sq
314 CODE:
315 291 RETVAL = (FN->is_strong)(sq);
316 OUTPUT:
317 RETVAL
318  
319 int
320 _dir_opposite(dir)
321 int dir
322 CODE:
323 225 RETVAL = (FN->dir_opposite)(dir);
324 OUTPUT:
325 RETVAL
326  
327 int
328 _dir_is_diagonal(dir)
329 int dir
330 CODE:
331 224 RETVAL = (FN->dir_is_diagonal)(dir);
332 OUTPUT:
333 RETVAL
334  
335 int
336 _next(u, sq, dir)
337 UV u
338 int sq
339 int dir
340 CODE:
341 3355 RETVAL = (FN->next)(BOARD(u), sq, dir);
342 OUTPUT:
343 RETVAL
344  
345 int
346 _degree(u, sq)
347 UV u
348 int sq
349 CODE:
350 12 RETVAL = (FN->degree)(BOARD(u), sq);
351 OUTPUT:
352 RETVAL
353  
354 # The joined pairs as a flat list, two points a segment.
355 void
356 _segments(u)
357 UV u
358 PREINIT:
359 int out[2 * FN_SEGMENTS_MAX];
360 int n, i;
361 PPCODE:
362 12 n = (FN->segments)(BOARD(u), out, FN_SEGMENTS_MAX);
363 12 ▶ if (n > FN_SEGMENTS_MAX) n = FN_SEGMENTS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > FN_SEGMENTS_MAX) n = FN_SEGMENTS_MAX; 0 12 12
364 12 ▶ EXTEND(SP, 2 * n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, 2 * n); 12 0 12
EXTEND(SP, 2 * n); 0 12 12
365 1436 ▶ for (i = 0; i < 2 * n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2 * n; i++) 1424 12 1436
366 1424 PUSHs(sv_2mortal(newSViv(out[i])));
367  
368 # ---- version 2: one stone, one step ------------------------------------------
369 #
370 # A step crosses as an array reference: [ from, to, dir, kind, taken... ].
371  
372 void
373 _gen_steps(u)
374 UV u
375 PREINIT:
376 struct fn_step list[FN_STEPS_MAX];
377 int n, i, j;
378 PPCODE:
379 5618 n = (FN->gen_steps)(BOARD(u), list, FN_STEPS_MAX);
380 5618 ▶ if (n > FN_STEPS_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (n > FN_STEPS_MAX) 0 5618 5618
381 0 croak("Game::Fanorona::Engine: %d steps, more than the %d a board can have", n, FN_STEPS_MAX);
382 5618 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 5618 0 5618
EXTEND(SP, n); 0 5618 5618
383 49020 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 43402 5618 49020
384 43402 AV *av = newAV();
385 43402 av_extend(av, 3 + list[i].taken_n);
386 43402 av_push(av, newSViv(list[i].from));
387 43402 av_push(av, newSViv(list[i].to));
388 43402 av_push(av, newSViv(list[i].dir));
389 43402 av_push(av, newSViv(list[i].kind));
390 64805 ▶ for (j = 0; j < list[i].taken_n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list[i].taken_n; j++) 21403 43402 64805
391 21403 av_push(av, newSViv(list[i].taken[j]));
392 43402 PUSHs(sv_2mortal(newRV_noinc((SV *) av)));
393 }
394  
395 void
396 _steps_of(u, sq)
397 UV u
398 int sq
399 PREINIT:
400 struct fn_step list[16];
401 int n, i, j;
402 PPCODE:
403 6 n = (FN->steps_of)(BOARD(u), sq, list, 16);
404 6 ▶ if (n > 16) n = 16;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 16) n = 16; 0 6 6
405 6 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 6 0 6
EXTEND(SP, n); 0 6 6
406 19 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 6 19
407 13 AV *av = newAV();
408 13 av_extend(av, 3 + list[i].taken_n);
409 13 av_push(av, newSViv(list[i].from));
410 13 av_push(av, newSViv(list[i].to));
411 13 av_push(av, newSViv(list[i].dir));
412 13 av_push(av, newSViv(list[i].kind));
413 18 ▶ for (j = 0; j < list[i].taken_n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list[i].taken_n; j++) 5 13 18
414 5 av_push(av, newSViv(list[i].taken[j]));
415 13 PUSHs(sv_2mortal(newRV_noinc((SV *) av)));
416 }
417  
418 int
419 _can_capture(u, side)
420 UV u
421 int side
422 CODE:
423 9 RETVAL = (FN->can_capture)(BOARD(u), side);
424 OUTPUT:
425 RETVAL
426  
427 int
428 _step_why(u, from, dir, kind)
429 UV u
430 int from
431 int dir
432 int kind
433 CODE:
434 64830 RETVAL = (FN->step_why)(BOARD(u), from, dir, kind, NULL);
435 OUTPUT:
436 RETVAL
437  
438 # The step as an array reference, or nothing when there is no such step to
439 # build.
440 void
441 _step_build(u, from, dir, kind)
442 UV u
443 int from
444 int dir
445 int kind
446 PREINIT:
447 struct fn_step s;
448 int j;
449 PPCODE:
450 10 ▶ if ((FN->step_build)(BOARD(u), from, dir, kind, &s)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((FN->step_build)(BOARD(u), from, dir, kind, &s)) { 2 8 10
451 2 AV *av = newAV();
452 2 av_extend(av, 3 + s.taken_n);
453 2 av_push(av, newSViv(s.from));
454 2 av_push(av, newSViv(s.to));
455 2 av_push(av, newSViv(s.dir));
456 2 av_push(av, newSViv(s.kind));
457 2 ▶ for (j = 0; j < s.taken_n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < s.taken_n; j++) 0 2 2
458 0 av_push(av, newSViv(s.taken[j]));
459 2 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 2 2
460 2 PUSHs(sv_2mortal(newRV_noinc((SV *) av)));
461 }
462  
463 # (removed, token), or nothing when the step could not be made. The token is
464 # the undo record's own bytes: opaque to Perl, and checked for length on the
465 # way back.
466 void
467 _do_step(u, from, dir, kind)
468 UV u
469 int from
470 int dir
471 int kind
472 PREINIT:
473 struct fn_step s;
474 struct fn_undo undo;
475 int removed;
476 PPCODE:
477 5014 ▶ if (!(FN->step_build)(BOARD(u), from, dir, kind, &s))
Branch: 1 branch
BranchTrueFalseTotal
if (!(FN->step_build)(BOARD(u), from, dir, kind, &s)) 1 5013 5014
478 1 XSRETURN_EMPTY;
479 5013 removed = (FN->do_step)(BOARD(u), &s, &undo);
480 5013 ▶ if (removed < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (removed < 0) 0 5013 5013
481 0 XSRETURN_EMPTY;
482 5013 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 5013 5013
483 5013 PUSHs(sv_2mortal(newSViv(removed)));
484 5013 PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo))));
485  
486 void
487 _undo_step(u, token)
488 UV u
489 SV *token
490 PREINIT:
491 STRLEN len;
492 const char *bytes;
493 struct fn_undo undo;
494 CODE:
495 5007 bytes = SvPV(token, len);
496 5007 ▶ if (len != sizeof(undo))
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof(undo)) 1 5006 5007
497 1 croak("Game::Fanorona::Engine: that is not an undo token");
498 5006 Copy(bytes, &undo, 1, struct fn_undo);
499 5006 (FN->undo_step)(BOARD(u), &undo);
500  
501 # A count crosses as a decimal STRING, for the reason a key crosses as hex: a
502 # perft total outgrows a 32-bit IV within a few plies.
503 SV *
504 _perft_steps(u, depth)
505 UV u
506 int depth
507 PREINIT:
508 unsigned long long v;
509 char buf[24];
510 int i;
511 CODE:
512 10 v = (FN->perft_steps)(BOARD(u), depth);
513 10 i = (int) sizeof(buf);
514 do {
515 24 buf[--i] = (char) ('0' + (int) (v % 10ULL));
516 24 v /= 10ULL;
517 24 ▶ } while (v);
Branch: 1 branch
BranchTrueFalseTotal
} while (v); 14 10 24
518 10 RETVAL = newSVpvn(buf + i, (STRLEN) (sizeof(buf) - i));
519 OUTPUT:
520 RETVAL
521  
522 int
523 _steps_max()
524 CODE:
525 3 ▶ RETVAL = FN_STEPS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FN_STEPS_MAX; 0 3 3
526 OUTPUT:
527 RETVAL
528  
529 int
530 _line_max()
531 CODE:
532 2 ▶ RETVAL = FN_LINE_MAX;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FN_LINE_MAX; 0 2 2
533 OUTPUT:
534 RETVAL
535  
536 # ---- version 3: the chain, and the whole move ---------------------------------
537  
538 int
539 _chain_at(u)
540 UV u
541 CODE:
542 9494742 RETVAL = (FN->chain_at)(BOARD(u));
543 OUTPUT:
544 RETVAL
545  
546 int
547 _chain_dir(u)
548 UV u
549 CODE:
550 5 RETVAL = (FN->chain_dir)(BOARD(u));
551 OUTPUT:
552 RETVAL
553  
554 int
555 _chain_seen(u, sq)
556 UV u
557 int sq
558 CODE:
559 225 RETVAL = (FN->chain_seen)(BOARD(u), sq);
560 OUTPUT:
561 RETVAL
562  
563 int
564 _point_bit(sq)
565 int sq
566 CODE:
567 0 RETVAL = (FN->point_bit)(sq);
568 OUTPUT:
569 RETVAL
570  
571 void
572 _play_steps(u)
573 UV u
574 PREINIT:
575 struct fn_step list[FN_STEPS_MAX];
576 int n, i, j;
577 PPCODE:
578 3111 n = (FN->play_steps)(BOARD(u), list, FN_STEPS_MAX);
579 3111 ▶ if (n > FN_STEPS_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (n > FN_STEPS_MAX) 0 3111 3111
580 0 croak("Game::Fanorona::Engine: %d steps, more than the %d a board can have", n, FN_STEPS_MAX);
581 3111 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 3111 0 3111
EXTEND(SP, n); 0 3111 3111
582 9227 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6116 3111 9227
583 6116 AV *av = newAV();
584 6116 av_extend(av, 3 + list[i].taken_n);
585 6116 av_push(av, newSViv(list[i].from));
586 6116 av_push(av, newSViv(list[i].to));
587 6116 av_push(av, newSViv(list[i].dir));
588 6116 av_push(av, newSViv(list[i].kind));
589 12932 ▶ for (j = 0; j < list[i].taken_n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list[i].taken_n; j++) 6816 6116 12932
590 6816 av_push(av, newSViv(list[i].taken[j]));
591 6116 PUSHs(sv_2mortal(newRV_noinc((SV *) av)));
592 }
593  
594 int
595 _can_stop(u)
596 UV u
597 CODE:
598 2 RETVAL = (FN->can_stop)(BOARD(u));
599 OUTPUT:
600 RETVAL
601  
602 int
603 _play_why(u, from, dir, kind)
604 UV u
605 int from
606 int dir
607 int kind
608 CODE:
609 94706 RETVAL = (FN->play_why)(BOARD(u), from, dir, kind, NULL);
610 OUTPUT:
611 RETVAL
612  
613 # (removed, token), or nothing, as _do_step.
614 void
615 _play_step(u, from, dir, kind)
616 UV u
617 int from
618 int dir
619 int kind
620 PREINIT:
621 struct fn_step s;
622 struct fn_undo undo;
623 int removed;
624 PPCODE:
625 21822 ▶ if (!(FN->step_build)(BOARD(u), from, dir, kind, &s))
Branch: 1 branch
BranchTrueFalseTotal
if (!(FN->step_build)(BOARD(u), from, dir, kind, &s)) 0 21822 21822
626 0 XSRETURN_EMPTY;
627 21822 removed = (FN->play_step)(BOARD(u), &s, &undo);
628 21822 ▶ if (removed < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (removed < 0) 0 21822 21822
629 0 XSRETURN_EMPTY;
630 21822 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 21822 21822
631 21822 PUSHs(sv_2mortal(newSViv(removed)));
632 21822 PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo))));
633  
634 # The token, or nothing when no chain was open.
635 void
636 _play_stop(u)
637 UV u
638 PREINIT:
639 struct fn_undo undo;
640 PPCODE:
641 2960 ▶ if (!(FN->play_stop)(BOARD(u), &undo))
Branch: 1 branch
BranchTrueFalseTotal
if (!(FN->play_stop)(BOARD(u), &undo)) 1 2959 2960
642 1 XSRETURN_EMPTY;
643 2959 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 2959 2959
644 2959 PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo))));
645  
646 void
647 _play_undo(u, token)
648 UV u
649 SV *token
650 PREINIT:
651 STRLEN len;
652 const char *bytes;
653 struct fn_undo undo;
654 CODE:
655 24761 bytes = SvPV(token, len);
656 24761 ▶ if (len != sizeof(undo))
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof(undo)) 0 24761 24761
657 0 croak("Game::Fanorona::Engine: that is not an undo token");
658 24761 Copy(bytes, &undo, 1, struct fn_undo);
659 24761 (FN->play_undo)(BOARD(u), &undo);
660  
661 SV *
662 _count_moves(u, cap)
663 UV u
664 UV cap
665 PREINIT:
666 unsigned long long v;
667 char buf[24];
668 int i;
669 CODE:
670 406 v = (FN->count_moves)(BOARD(u), (unsigned long long) cap);
671 406 i = (int) sizeof(buf);
672 do {
673 625 buf[--i] = (char) ('0' + (int) (v % 10ULL));
674 625 v /= 10ULL;
675 625 ▶ } while (v);
Branch: 1 branch
BranchTrueFalseTotal
} while (v); 219 406 625
676 406 RETVAL = newSVpvn(buf + i, (STRLEN) (sizeof(buf) - i));
677 OUTPUT:
678 RETVAL
679  
680 # Each move as an array reference of its steps, each step an array reference
681 # as _gen_steps gives it. The list is counted first, no further than one past
682 # `limit`, and the buffer sized to it; a position with more moves than `limit`
683 # croaks and writes nothing.
684 void
685 _gen_moves(u, limit)
686 UV u
687 int limit
688 PREINIT:
689 struct fn_move *list;
690 struct fn_step steps[FN_CHAIN_MAX];
691 int n, i, j, k, m;
692 PPCODE:
693 462321 ▶ if (limit < 0) limit = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limit < 0) limit = 0; 0 462321 462321
694 462321 n = (int) (FN->count_moves)(BOARD(u), (unsigned long long) limit + 1ULL);
695 462321 ▶ if (n > limit)
Branch: 1 branch
BranchTrueFalseTotal
if (n > limit) 1 462320 462321
696 1 croak("Game::Fanorona::Engine: more than %d whole moves", limit);
697 462320 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 28 462292 462320
698 28 XSRETURN_EMPTY;
699 462292 Newx(list, n, struct fn_move);
700 462292 SAVEFREEPV(list);
701 462292 (FN->gen_moves)(BOARD(u), list, n);
702 462292 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 462292 0 462292
EXTEND(SP, n); 1 462291 462292
703 2820939 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2358647 462292 2820939
704 2358647 AV *mv = newAV();
705 2358647 m = (FN->move_steps)(BOARD(u), &list[i], steps);
706 4948065 ▶ for (k = 0; k < m; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < m; k++) { 2589418 2358647 4948065
707 2589418 AV *av = newAV();
708 2589418 av_extend(av, 3 + steps[k].taken_n);
709 2589418 av_push(av, newSViv(steps[k].from));
710 2589418 av_push(av, newSViv(steps[k].to));
711 2589418 av_push(av, newSViv(steps[k].dir));
712 2589418 av_push(av, newSViv(steps[k].kind));
713 3125266 ▶ for (j = 0; j < steps[k].taken_n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < steps[k].taken_n; j++) 535848 2589418 3125266
714 535848 av_push(av, newSViv(steps[k].taken[j]));
715 2589418 av_push(mv, newRV_noinc((SV *) av));
716 }
717 2358647 PUSHs(sv_2mortal(newRV_noinc((SV *) mv)));
718 }
719  
720 SV *
721 _perft_moves(u, depth)
722 UV u
723 int depth
724 PREINIT:
725 unsigned long long v;
726 char buf[24];
727 int i;
728 CODE:
729 18 v = (FN->perft_moves)(BOARD(u), depth);
730 18 i = (int) sizeof(buf);
731 do {
732 37 buf[--i] = (char) ('0' + (int) (v % 10ULL));
733 37 v /= 10ULL;
734 37 ▶ } while (v);
Branch: 1 branch
BranchTrueFalseTotal
} while (v); 19 18 37
735 18 RETVAL = newSVpvn(buf + i, (STRLEN) (sizeof(buf) - i));
736 OUTPUT:
737 RETVAL
738  
739 int
740 _chain_max()
741 CODE:
742 2 ▶ RETVAL = FN_CHAIN_MAX;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FN_CHAIN_MAX; 0 2 2
743 OUTPUT:
744 RETVAL
745  
746 MODULE = Game::Fanorona PACKAGE = Game::Fanorona::Rules
747  
748 PROTOTYPES: DISABLE
749  
750 # ---- version 4: a game -----------------------------------------------------------
751 #
752 # A game crosses as a raw pointer in a UV, as a board does. Each limit is an
753 # int, and a negative one means the default.
754  
755 UV
756 _new_game(position, quiet, repeat, cap)
757 const char *position
758 int quiet
759 int repeat
760 int cap
761 PREINIT:
762 struct fn_board *b;
763 struct fn_variant v;
764 464787 ▶ int err = 0;
Branch: 1 branch
BranchTrueFalseTotal
int err = 0; 464787 0 464787
765 CODE:
766 464787 (FN->variant_default)(&v);
767 464787 v.quiet_limit = quiet;
768 464787 v.repeat = repeat;
769 464787 ▶ if (cap > 0) v.ply_cap = cap;
Branch: 1 branch
BranchTrueFalseTotal
if (cap > 0) v.ply_cap = cap; 1134 463653 464787
770 464787 b = (FN->board_of_string)(position, &err);
771 464787 ▶ if (!b) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) XSRETURN_UNDEF; 1 464786 464787
772 464786 RETVAL = PTR2UV((FN->game_new)(b, &v));
773 464786 (FN->board_drop)(b);
774 OUTPUT:
775 RETVAL
776  
777 UV
778 _copy_game(g)
779 UV g
780 CODE:
781 31110 ▶ RETVAL = PTR2UV((FN->game_copy)(INT2PTR(struct fn_game *, g)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((FN->game_copy)(INT2PTR(struct fn_game *, g))); 31100 10 31110
782 OUTPUT:
783 RETVAL
784  
785 SV *
786 _hold_game(g)
787 UV g
788 CODE:
789 495896 RETVAL = sv_setref_uv(newSV(0), "Game::Fanorona::Rules::Hold", g);
790 OUTPUT:
791 RETVAL
792  
793 int
794 _games_live()
795 CODE:
796 9 RETVAL = (FN->games_live)();
797 OUTPUT:
798 RETVAL
799  
800 # The game's own board, BORROWED: an address to read through and nothing else.
801 UV
802 _game_board(g)
803 UV g
804 CODE:
805 12188544 ▶ RETVAL = PTR2UV((FN->game_board)(INT2PTR(struct fn_game *, g)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((FN->game_board)(INT2PTR(struct fn_game *, g))); 12188515 29 12188544
806 OUTPUT:
807 RETVAL
808  
809 void
810 _game_variant(g)
811 UV g
812 PREINIT:
813 struct fn_variant v;
814 PPCODE:
815 417 (FN->game_variant)(INT2PTR(struct fn_game *, g), &v);
816 417 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 417 417
817 417 PUSHs(sv_2mortal(newSViv(v.quiet_limit)));
818 417 PUSHs(sv_2mortal(newSViv(v.repeat)));
819 417 PUSHs(sv_2mortal(newSViv(v.ply_cap)));
820  
821 int
822 _game_ply(g)
823 UV g
824 CODE:
825 4490250 RETVAL = (FN->game_ply)(INT2PTR(struct fn_game *, g));
826 OUTPUT:
827 RETVAL
828  
829 int
830 _game_quiet(g)
831 UV g
832 CODE:
833 9785 RETVAL = (FN->game_quiet)(INT2PTR(struct fn_game *, g));
834 OUTPUT:
835 RETVAL
836  
837 int
838 _game_depth(g)
839 UV g
840 CODE:
841 4471012 RETVAL = (FN->game_depth)(INT2PTR(struct fn_game *, g));
842 OUTPUT:
843 RETVAL
844  
845 int
846 _game_outcome(g)
847 UV g
848 CODE:
849 3210734 RETVAL = (FN->game_outcome)(INT2PTR(struct fn_game *, g));
850 OUTPUT:
851 RETVAL
852  
853 int
854 _game_winner(g)
855 UV g
856 CODE:
857 6748 RETVAL = (FN->game_winner)(INT2PTR(struct fn_game *, g));
858 OUTPUT:
859 RETVAL
860  
861 SV *
862 _game_key_hex(g, ply)
863 UV g
864 int ply
865 CODE:
866 9 RETVAL = fn_hex64(aTHX_ (FN->game_key_at)(INT2PTR(struct fn_game *, g), ply));
867 OUTPUT:
868 RETVAL
869  
870 int
871 _game_repeats(g)
872 UV g
873 CODE:
874 7 RETVAL = (FN->repeats)(INT2PTR(struct fn_game *, g));
875 OUTPUT:
876 RETVAL
877  
878 void
879 _game_steps(g)
880 UV g
881 PREINIT:
882 struct fn_step list[FN_STEPS_MAX];
883 int n, i, j;
884 PPCODE:
885 74566 n = (FN->game_steps)(INT2PTR(struct fn_game *, g), list, FN_STEPS_MAX);
886 74566 ▶ if (n > FN_STEPS_MAX) n = FN_STEPS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > FN_STEPS_MAX) n = FN_STEPS_MAX; 0 74566 74566
887 74566 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 74566 0 74566
EXTEND(SP, n); 0 74566 74566
888 490469 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 415903 74566 490469
889 415903 AV *av = newAV();
890 415903 av_extend(av, 3 + list[i].taken_n);
891 415903 av_push(av, newSViv(list[i].from));
892 415903 av_push(av, newSViv(list[i].to));
893 415903 av_push(av, newSViv(list[i].dir));
894 415903 av_push(av, newSViv(list[i].kind));
895 686697 ▶ for (j = 0; j < list[i].taken_n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list[i].taken_n; j++) 270794 415903 686697
896 270794 av_push(av, newSViv(list[i].taken[j]));
897 415903 PUSHs(sv_2mortal(newRV_noinc((SV *) av)));
898 }
899  
900 int
901 _game_can_stop(g)
902 UV g
903 CODE:
904 11722 RETVAL = (FN->game_can_stop)(INT2PTR(struct fn_game *, g));
905 OUTPUT:
906 RETVAL
907  
908 # (answer, why): FN_PLAY_OK and the rest, and the reason when it is illegal.
909 void
910 _game_step(g, from, dir, kind)
911 UV g
912 int from
913 int dir
914 int kind
915 PREINIT:
916 2315304 int answer, why = 0;
917 PPCODE:
918 2315304 answer = (FN->game_step)(INT2PTR(struct fn_game *, g), from, dir, kind, &why);
919 2315304 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 2315304 2315304
920 2315304 PUSHs(sv_2mortal(newSViv(answer)));
921 2315304 PUSHs(sv_2mortal(newSViv(why)));
922  
923 int
924 _game_stop(g)
925 UV g
926 CODE:
927 13449 RETVAL = (FN->game_stop)(INT2PTR(struct fn_game *, g));
928 OUTPUT:
929 RETVAL
930  
931 int
932 _game_undo(g)
933 UV g
934 CODE:
935 2248075 RETVAL = (FN->game_undo)(INT2PTR(struct fn_game *, g));
936 OUTPUT:
937 RETVAL
938  
939 # ---- version 5: choosing a move ------------------------------------------------
940 #
941 # The four weights of the evaluation, in the order the struct has them.
942 void
943 _weights_default()
944 PREINIT:
945 struct fn_weights w;
946 PPCODE:
947 2481 (FN->weights_default)(&w);
948 2481 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 2481 2481
949 2481 PUSHs(sv_2mortal(newSViv(w.stone)));
950 2481 PUSHs(sv_2mortal(newSViv(w.strong)));
951 2481 PUSHs(sv_2mortal(newSViv(w.centre)));
952 2481 PUSHs(sv_2mortal(newSViv(w.room)));
953  
954 # The game's board scored for White.
955 int
956 _game_evaluate(g, stone, strong, centre, room)
957 UV g
958 int stone
959 int strong
960 int centre
961 int room
962 PREINIT:
963 struct fn_weights w;
964 CODE:
965 27 (FN->weights_default)(&w);
966 27 w.stone = stone; w.strong = strong; w.centre = centre; w.room = room;
967 27 RETVAL = (FN->evaluate)((FN->game_board)(INT2PTR(struct fn_game *, g)), &w);
968 OUTPUT:
969 RETVAL
970  
971 # (found, nodes, depth, score, stopped, from, dir, kind, dir, kind, ...). The
972 # count of positions crosses as a string, for the reason a perft total does.
973 # The seed is a UV and so at most 32 bits on some perls, which is all the
974 # seed is for: it settles ties.
975 void
976 _game_search(g, budget, seed, max_depth, stone, strong, centre, room)
977 UV g
978 UV budget
979 UV seed
980 int max_depth
981 int stone
982 int strong
983 int centre
984 int room
985 PREINIT:
986 struct fn_weights w;
987 struct fn_move best;
988 struct fn_search_info info;
989 unsigned long long v;
990 char buf[24];
991 int found, i, k;
992 PPCODE:
993 2452 (FN->weights_default)(&w);
994 2452 w.stone = stone; w.strong = strong; w.centre = centre; w.room = room;
995 2452 found = (FN->search)(INT2PTR(struct fn_game *, g), &w, (unsigned long long) budget,
996 (unsigned long long) seed, max_depth, &best, &info);
997 2452 v = info.nodes;
998 2452 i = (int) sizeof(buf);
999 do {
1000 7689 buf[--i] = (char) ('0' + (int) (v % 10ULL));
1001 7689 v /= 10ULL;
1002 7689 ▶ } while (v);
Branch: 1 branch
BranchTrueFalseTotal
} while (v); 5237 2452 7689
1003 2452 ▶ EXTEND(SP, 6 + 2 * best.n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, 6 + 2 * best.n); 2452 0 2452
EXTEND(SP, 6 + 2 * best.n); 0 2452 2452
1004 2452 PUSHs(sv_2mortal(newSViv(found)));
1005 2452 PUSHs(sv_2mortal(newSVpvn(buf + i, (STRLEN) (sizeof(buf) - i))));
1006 2452 PUSHs(sv_2mortal(newSViv(info.depth)));
1007 2452 PUSHs(sv_2mortal(newSViv(info.score)));
1008 2452 PUSHs(sv_2mortal(newSViv(info.stopped)));
1009 2452 PUSHs(sv_2mortal(newSViv(best.from)));
1010 5084 ▶ for (k = 0; k < best.n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < best.n; k++) { 2632 2452 5084
1011 2632 PUSHs(sv_2mortal(newSViv(best.dir[k])));
1012 2632 PUSHs(sv_2mortal(newSViv(best.kind[k])));
1013 }
1014  
1015 int
1016 _win_score()
1017 CODE:
1018 2 ▶ RETVAL = FN_WIN_SCORE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FN_WIN_SCORE; 0 2 2
1019 OUTPUT:
1020 RETVAL
1021  
1022 MODULE = Game::Fanorona PACKAGE = Game::Fanorona::Engine::Hold
1023  
1024 PROTOTYPES: DISABLE
1025  
1026 # ---- letting go -----------------------------------------------------------------
1027 #
1028 # WHY THIS IS NOT A DEMOLISH IN PERL. It was, through eight phases. On perls
1029 # 5.14 to 5.26 an eval that runs inside a destructor, while an exception is
1030 # unwinding past a TEMPORARY of that class, leaves $@ empty: the caller's die
1031 # arrives with no message. Object::Proto calls DEMOLISH under an eval, so any
1032 # class with one has this. `eval { f(Class->new) }` with f dying is enough.
1033 # The packaging matrix found it; a DESTROY written in C runs no eval.
1034  
1035 void
1036 DESTROY(self)
1037 SV *self
1038 PREINIT:
1039 SV *inner;
1040 UV u;
1041 CODE:
1042 472180 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 472180 0 472180
1043 472180 inner = SvRV(self);
1044 472180 u = SvUV(inner);
1045 472180 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 472180 0 472180
1046 472180 sv_setuv(inner, 0);
1047 472180 (FN->board_drop)(BOARD(u));
1048 }
1049 }
1050  
1051 MODULE = Game::Fanorona PACKAGE = Game::Fanorona::Rules::Hold
1052  
1053 PROTOTYPES: DISABLE
1054  
1055 void
1056 DESTROY(self)
1057 SV *self
1058 PREINIT:
1059 SV *inner;
1060 UV u;
1061 CODE:
1062 495896 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 495896 0 495896
1063 495896 inner = SvRV(self);
1064 495896 u = SvUV(inner);
1065 495896 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 495896 0 495896
1066 495896 sv_setuv(inner, 0);
1067 495896 (FN->game_drop)(INT2PTR(struct fn_game *, u));
1068 }
1069 }
« blib/lib/Game/Fanorona/Move.pm ↑ Summary fn_rules.c »