Shogi.xs

filter: Statementstmt 100% 138 / 138 Branchbran 67.6% 88 / 130 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.3% 226 / 268 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/Game/Shogi/Install/Files.pm ↑ Summary shg_moves.c »
1 /* Shogi.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::Shogi::Engine, not in Game::Shogi, 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/Shogi/Engine.pm.
10 *
11 * `_put`, `_lift` and `_set_hand` are the structure primitives from shg_abi.h
12 * and CHECK 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 "shg_abi.h"
22  
23 static const struct shg_abi *SHG = NULL;
24  
25 #define BOARD(u) INT2PTR(struct shg_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 Sternhalma.xs has
32 * it.
33 */
34 40756 static SV *shg_hex64(pTHX_ unsigned long long v)
35 {
36 static const char digits[] = "0123456789abcdef";
37 char buf[16];
38 int i;
39 692852 ▶ for (i = 15; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 15; i >= 0; i--) { 652096 40756 692852
40 652096 buf[i] = digits[v & 0xfULL];
41 652096 v >>= 4;
42 }
43 40756 return newSVpvn(buf, 16);
44 }
45  
46 MODULE = Game::Shogi PACKAGE = Game::Shogi::Engine
47  
48 PROTOTYPES: DISABLE
49  
50 BOOT:
51 24 SHG = shg_abi_table();
52  
53 UV
54 _abi_ptr()
55 CODE:
56 1 ▶ RETVAL = PTR2UV(SHG);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(SHG); 0 1 1
57 OUTPUT:
58 RETVAL
59  
60 UV
61 _abi_version()
62 CODE:
63 2 ▶ RETVAL = (UV)SHG->abi_version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)SHG->abi_version; 1 1 2
64 OUTPUT:
65 RETVAL
66  
67 int
68 _tables_ok()
69 CODE:
70 1 RETVAL = (SHG->tables_ok)();
71 OUTPUT:
72 RETVAL
73  
74 UV
75 _new_board()
76 CODE:
77 271 ▶ RETVAL = PTR2UV((SHG->board_new)());
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((SHG->board_new)()); 256 15 271
78 OUTPUT:
79 RETVAL
80  
81 UV
82 _new_empty()
83 CODE:
84 2344 ▶ RETVAL = PTR2UV((SHG->board_empty)());
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((SHG->board_empty)()); 2339 5 2344
85 OUTPUT:
86 RETVAL
87  
88 UV
89 _copy_board(u)
90 UV u
91 CODE:
92 1107 ▶ RETVAL = PTR2UV((SHG->board_copy)(BOARD(u)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((SHG->board_copy)(BOARD(u))); 1098 9 1107
93 OUTPUT:
94 RETVAL
95  
96 # The board is let go by an object of its own, held by the board's object and
97 # freed with it. Its DESTROY is C and calls no perl.
98 SV *
99 _hold_board(u)
100 UV u
101 CODE:
102 5834 RETVAL = sv_setref_uv(newSV(0), "Game::Shogi::Engine::Hold", u);
103 OUTPUT:
104 RETVAL
105  
106 int
107 _live()
108 CODE:
109 4 RETVAL = (SHG->live)();
110 OUTPUT:
111 RETVAL
112  
113 # An SFEN that will not parse returns undef and the code, rather than
114 # croaking: the reason is what a caller shows a person, and twelve of them are
115 # distinguishable.
116 void
117 _of_sfen(str)
118 const char *str
119 PREINIT:
120 struct shg_board *b;
121 2163 int err = 0;
122 PPCODE:
123 2163 b = (SHG->board_of_sfen)(str, &err);
124 2163 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 2163 2163
125 2163 ▶ if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b))));
Branch: 1 branch
BranchTrueFalseTotal
if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); 2112 51 2163
126 51 else PUSHs(&PL_sv_undef);
127 2163 PUSHs(sv_2mortal(newSViv(err)));
128  
129 SV *
130 _to_sfen(u)
131 UV u
132 PREINIT:
133 char buf[SHG_SFEN_MAX];
134 int n;
135 CODE:
136 2685 n = (SHG->to_sfen)(BOARD(u), buf, (int)sizeof(buf));
137 2685 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 2685 2685
138 2685 RETVAL = newSVpvn(buf, (STRLEN)n);
139 OUTPUT:
140 RETVAL
141  
142 int
143 _at(u, sq)
144 UV u
145 int sq
146 CODE:
147 74168 RETVAL = (SHG->at)(BOARD(u), sq);
148 OUTPUT:
149 RETVAL
150  
151 void
152 _put(u, sq, piece)
153 UV u
154 int sq
155 int piece
156 CODE:
157 2409 (SHG->put)(BOARD(u), sq, piece);
158  
159 void
160 _lift(u, sq)
161 UV u
162 int sq
163 CODE:
164 102 (SHG->lift)(BOARD(u), sq);
165  
166 int
167 _hand(u, side, kind)
168 UV u
169 int side
170 int kind
171 CODE:
172 12118 RETVAL = (SHG->hand)(BOARD(u), side, kind);
173 OUTPUT:
174 RETVAL
175  
176 void
177 _set_hand(u, side, kind, count)
178 UV u
179 int side
180 int kind
181 int count
182 CODE:
183 15 (SHG->set_hand)(BOARD(u), side, kind, count);
184  
185 int
186 _to_move(u)
187 UV u
188 CODE:
189 34304 RETVAL = (SHG->to_move)(BOARD(u));
190 OUTPUT:
191 RETVAL
192  
193 void
194 _set_to_move(u, side)
195 UV u
196 int side
197 CODE:
198 2329 (SHG->set_to_move)(BOARD(u), side);
199  
200 int
201 _move_number(u)
202 UV u
203 CODE:
204 30544 RETVAL = (SHG->move_number)(BOARD(u));
205 OUTPUT:
206 RETVAL
207  
208 void
209 _set_move_number(u, n)
210 UV u
211 int n
212 CODE:
213 3 (SHG->set_move_number)(BOARD(u), n);
214  
215 int
216 _king_sq(u, side)
217 UV u
218 int side
219 CODE:
220 9 RETVAL = (SHG->king_sq)(BOARD(u), side);
221 OUTPUT:
222 RETVAL
223  
224 SV *
225 _key_hex(u)
226 UV u
227 CODE:
228 38130 RETVAL = shg_hex64(aTHX_ (SHG->key)(BOARD(u)));
229 OUTPUT:
230 RETVAL
231  
232 SV *
233 _zobrist_hex(side, kind, sq)
234 int side
235 int kind
236 int sq
237 CODE:
238 2419 RETVAL = shg_hex64(aTHX_ (SHG->zobrist)(side, kind, sq));
239 OUTPUT:
240 RETVAL
241  
242 SV *
243 _zobrist_hand_hex(side, kind, count)
244 int side
245 int kind
246 int count
247 CODE:
248 205 RETVAL = shg_hex64(aTHX_ (SHG->zobrist_hand)(side, kind, count));
249 OUTPUT:
250 RETVAL
251  
252 SV *
253 _zobrist_move_hex()
254 CODE:
255 2 RETVAL = shg_hex64(aTHX_ (SHG->zobrist_move)());
256 OUTPUT:
257 RETVAL
258  
259 int
260 _sq_of(file, rank)
261 int file
262 int rank
263 CODE:
264 321971 RETVAL = (SHG->sq_of)(file, rank);
265 OUTPUT:
266 RETVAL
267  
268 int
269 _file_of(sq)
270 int sq
271 CODE:
272 1557604 RETVAL = (SHG->file_of)(sq);
273 OUTPUT:
274 RETVAL
275  
276 int
277 _rank_of(sq)
278 int sq
279 CODE:
280 1557685 RETVAL = (SHG->rank_of)(sq);
281 OUTPUT:
282 RETVAL
283  
284 int
285 _step(sq, dfile, drank)
286 int sq
287 int dfile
288 int drank
289 CODE:
290 1225 RETVAL = (SHG->step)(sq, dfile, drank);
291 OUTPUT:
292 RETVAL
293  
294 int
295 _in_zone(side, sq)
296 int side
297 int sq
298 CODE:
299 324 RETVAL = (SHG->in_zone)(side, sq);
300 OUTPUT:
301 RETVAL
302  
303 int
304 _last_distance(side, sq)
305 int side
306 int sq
307 CODE:
308 164 RETVAL = (SHG->last_distance)(side, sq);
309 OUTPUT:
310 RETVAL
311  
312 int
313 _promoted(kind)
314 int kind
315 CODE:
316 2029 RETVAL = (SHG->promoted)(kind);
317 OUTPUT:
318 RETVAL
319  
320 int
321 _unpromoted(kind)
322 int kind
323 CODE:
324 615 RETVAL = (SHG->unpromoted)(kind);
325 OUTPUT:
326 RETVAL
327  
328 int
329 _set_count(kind)
330 int kind
331 CODE:
332 30 RETVAL = (SHG->set_count)(kind);
333 OUTPUT:
334 RETVAL
335  
336 # ---- version 2: the board moves ---------------------------------------------------
337  
338 int
339 _attacked(u, sq, by)
340 UV u
341 int sq
342 int by
343 CODE:
344 213581 RETVAL = (SHG->attacked)(BOARD(u), sq, by);
345 OUTPUT:
346 RETVAL
347  
348 int
349 _in_check(u, side)
350 UV u
351 int side
352 CODE:
353 33218 RETVAL = (SHG->in_check)(BOARD(u), side);
354 OUTPUT:
355 RETVAL
356  
357 void
358 _pseudo_moves(u)
359 UV u
360 PREINIT:
361 int out[SHG_MOVES_MAX];
362 int n, i;
363 PPCODE:
364 2753 n = (SHG->pseudo_moves)(BOARD(u), out, SHG_MOVES_MAX);
365 2753 ▶ if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; 0 2753 2753
366 2753 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 2753 0 2753
EXTEND(SP, n); 0 2753 2753
367 37962 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 35209 2753 37962
368  
369 void
370 _board_moves(u)
371 UV u
372 PREINIT:
373 int out[SHG_MOVES_MAX];
374 int n, i;
375 PPCODE:
376 100 n = (SHG->board_moves)(BOARD(u), out, SHG_MOVES_MAX);
377 100 ▶ if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; 0 100 100
378 100 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 100 0 100
EXTEND(SP, n); 0 100 100
379 1578 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 1478 100 1578
380  
381 # The undo record crosses as the bytes of the struct. undo_move checks it
382 # against the board, so a string that was never one is refused, not obeyed.
383 SV *
384 _do_move(u, move)
385 UV u
386 int move
387 PREINIT:
388 struct shg_undo undo;
389 CODE:
390 33158 ▶ if (!(SHG->do_move)(BOARD(u), move, &undo)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(SHG->do_move)(BOARD(u), move, &undo)) XSRETURN_UNDEF; 11 33147 33158
391 33147 RETVAL = newSVpvn((const char *)&undo, sizeof undo);
392 OUTPUT:
393 RETVAL
394  
395 int
396 _undo_move(u, record)
397 UV u
398 SV *record
399 PREINIT:
400 struct shg_undo undo;
401 STRLEN len;
402 const char *bytes;
403 CODE:
404 1983 bytes = SvPV(record, len);
405 1983 ▶ if (len != sizeof undo) XSRETURN_IV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof undo) XSRETURN_IV(0); 2 1981 1983
406 1981 memcpy(&undo, bytes, sizeof undo);
407 1981 RETVAL = (SHG->undo_move)(BOARD(u), &undo);
408 OUTPUT:
409 RETVAL
410  
411 # ---- version 3: the drops ---------------------------------------------------------
412  
413 void
414 _drops(u)
415 UV u
416 PREINIT:
417 int out[SHG_MOVES_MAX];
418 int n, i;
419 PPCODE:
420 9 n = (SHG->drops)(BOARD(u), out, SHG_MOVES_MAX);
421 9 ▶ if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; 0 9 9
422 9 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 9 0 9
EXTEND(SP, n); 0 9 9
423 3757 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 3748 9 3757
424  
425 void
426 _legal(u)
427 UV u
428 PREINIT:
429 int out[SHG_MOVES_MAX];
430 int n, i;
431 PPCODE:
432 17395 n = (SHG->legal)(BOARD(u), out, SHG_MOVES_MAX);
433 17395 ▶ if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; 0 17395 17395
434 17395 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 17395 0 17395
EXTEND(SP, n); 0 17395 17395
435 758627 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 741232 17395 758627
436  
437 int
438 _has_legal_move(u)
439 UV u
440 CODE:
441 7 RETVAL = (SHG->has_legal_move)(BOARD(u));
442 OUTPUT:
443 RETVAL
444  
445 int
446 _drop_fault(u, kind, sq)
447 UV u
448 int kind
449 int sq
450 CODE:
451 32 RETVAL = (SHG->drop_fault)(BOARD(u), kind, sq);
452 OUTPUT:
453 RETVAL
454  
455 # The six counts as NVs: exact to 2**53, which no perft this dist runs comes
456 # near, and a UV on a 32-bit perl does not hold depth 7's fifteen billion.
457 void
458 _perft(u, depth)
459 UV u
460 int depth
461 PREINIT:
462 unsigned long long counts[SHG_PERFT_COUNTS];
463 int i;
464 PPCODE:
465 14 (SHG->perft)(BOARD(u), depth, counts);
466 14 ▶ EXTEND(SP, SHG_PERFT_COUNTS);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, SHG_PERFT_COUNTS); 0 14 14
467 98 ▶ for (i = 0; i < SHG_PERFT_COUNTS; i++) PUSHs(sv_2mortal(newSVnv((NV)counts[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SHG_PERFT_COUNTS; i++) PUSHs(sv_2mortal(newSVnv((NV)counts[i]))); 84 14 98
468  
469 # ---- version 4: how a position ends -----------------------------------------------
470  
471 int
472 _status(u)
473 UV u
474 CODE:
475 30747 RETVAL = (SHG->status)(BOARD(u));
476 OUTPUT:
477 RETVAL
478  
479 int
480 _move_fault(u, move)
481 UV u
482 int move
483 CODE:
484 129518 RETVAL = (SHG->move_fault)(BOARD(u), move);
485 OUTPUT:
486 RETVAL
487  
488 # ---- version 5: what needs the history ---------------------------------------------
489  
490 SV *
491 _pack(u)
492 UV u
493 PREINIT:
494 unsigned char buf[SHG_PACKED];
495 CODE:
496 63309 ▶ if ((SHG->pack)(BOARD(u), buf) != SHG_PACKED) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if ((SHG->pack)(BOARD(u), buf) != SHG_PACKED) XSRETURN_UNDEF; 0 63309 63309
497 63309 RETVAL = newSVpvn((const char *)buf, SHG_PACKED);
498 OUTPUT:
499 RETVAL
500  
501 int
502 _impasse_points(u, side)
503 UV u
504 int side
505 CODE:
506 8 RETVAL = (SHG->impasse_points)(BOARD(u), side);
507 OUTPUT:
508 RETVAL
509  
510 int
511 _impasse_pieces(u, side)
512 UV u
513 int side
514 CODE:
515 2 RETVAL = (SHG->impasse_pieces)(BOARD(u), side);
516 OUTPUT:
517 RETVAL
518  
519 int
520 _declare_fault(u)
521 UV u
522 CODE:
523 47 RETVAL = (SHG->declare_fault)(BOARD(u));
524 OUTPUT:
525 RETVAL
526  
527 # Three array references of the same length: the keys as sixteen hex digits,
528 # the packed positions, and whether the move into each gave check. Anything
529 # malformed is no repetition. Returns the code and the loser, -1 for none.
530 void
531 _repetition(keys_rv, packed_rv, checks_rv)
532 SV *keys_rv
533 SV *packed_rv
534 SV *checks_rv
535 PREINIT:
536 AV *ka, *pa, *ca;
537 SSize_t n, i;
538 unsigned long long *keys;
539 unsigned char *packed, *checks;
540 29555 int code = SHG_REP_NONE, loser = -1, ok = 1;
541 PPCODE:
542 29555 ▶ if (SvROK(keys_rv) && SvTYPE(SvRV(keys_rv)) == SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(keys_rv) && SvTYPE(SvRV(keys_rv)) == SVt_PVAV 29554 1 29555
if (SvROK(keys_rv) && SvTYPE(SvRV(keys_rv)) == SVt_PVAV 29554 0 29554
543 29554 ▶ && SvROK(packed_rv) && SvTYPE(SvRV(packed_rv)) == SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
&& SvROK(packed_rv) && SvTYPE(SvRV(packed_rv)) == SVt_PVAV 29554 0 29554
&& SvROK(packed_rv) && SvTYPE(SvRV(packed_rv)) == SVt_PVAV 29554 0 29554
544 29554 ▶ && SvROK(checks_rv) && SvTYPE(SvRV(checks_rv)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
&& SvROK(checks_rv) && SvTYPE(SvRV(checks_rv)) == SVt_PVAV) { 29554 0 29554
&& SvROK(checks_rv) && SvTYPE(SvRV(checks_rv)) == SVt_PVAV) { 29554 0 29554
545 29554 ka = (AV *)SvRV(keys_rv);
546 29554 pa = (AV *)SvRV(packed_rv);
547 29554 ca = (AV *)SvRV(checks_rv);
548 29554 n = av_len(ka) + 1;
549 29554 ▶ if (n > 0 && n == av_len(pa) + 1 && n == av_len(ca) + 1 && n < 100000) {
Branch: 4 branches
BranchTrueFalseTotal
if (n > 0 && n == av_len(pa) + 1 && n == av_len(ca) + 1 && n < 100000) { 29553 1 29554
if (n > 0 && n == av_len(pa) + 1 && n == av_len(ca) + 1 && n < 100000) { 29553 0 29553
if (n > 0 && n == av_len(pa) + 1 && n == av_len(ca) + 1 && n < 100000) { 29552 1 29553
if (n > 0 && n == av_len(pa) + 1 && n == av_len(ca) + 1 && n < 100000) { 29552 0 29552
550 29552 ▶ Newx(keys, n, unsigned long long);
Branch: 1 branch
BranchTrueFalseTotal
Newx(keys, n, unsigned long long); 0 29552 29552
551 29552 Newx(packed, (size_t)n * SHG_PACKED, unsigned char);
552 29552 Newx(checks, n, unsigned char);
553 2281828 ▶ for (i = 0; i < n && ok; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && ok; i++) { 2252278 29550 2281828
for (i = 0; i < n && ok; i++) { 2252278 0 2252278
554 2252278 SV **k = av_fetch(ka, i, 0), **p = av_fetch(pa, i, 0), **c = av_fetch(ca, i, 0);
555 STRLEN klen, plen;
556 const char *ks, *ps;
557 char *end;
558 2252280 ▶ if (!k || !p || !c) { ok = 0; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (!k || !p || !c) { ok = 0; break; } 2252278 0 2252278
if (!k || !p || !c) { ok = 0; break; } 2252278 0 2252278
if (!k || !p || !c) { ok = 0; break; } 0 2252278 2252278
559 2252278 ks = SvPV(*k, klen);
560 2252278 ▶ ps = SvPV(*p, plen);
Branch: 1 branch
BranchTrueFalseTotal
ps = SvPV(*p, plen); 2252278 0 2252278
561 2252278 ▶ if (klen != 16 || plen != SHG_PACKED) { ok = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (klen != 16 || plen != SHG_PACKED) { ok = 0; break; } 2252277 1 2252278
if (klen != 16 || plen != SHG_PACKED) { ok = 0; break; } 1 2252276 2252277
562 2252276 keys[i] = strtoull(ks, &end, 16);
563 2252276 ▶ if (end != ks + 16) { ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (end != ks + 16) { ok = 0; break; } 0 2252276 2252276
564 2252276 memcpy(packed + (size_t)i * SHG_PACKED, ps, SHG_PACKED);
565 2252276 checks[i] = SvTRUE(*c) ? 1 : 0;
566 }
567 29552 ▶ if (ok) code = (SHG->repetition)(keys, packed, checks, (int)n, &loser);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) code = (SHG->repetition)(keys, packed, checks, (int)n, &loser); 29550 2 29552
568 29552 Safefree(keys);
569 29552 Safefree(packed);
570 29552 Safefree(checks);
571 }
572 }
573 29555 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 29555 29555
574 29555 PUSHs(sv_2mortal(newSViv(code)));
575 29555 PUSHs(sv_2mortal(newSViv(loser)));
576  
577 # ---- version 6: the search ----------------------------------------------------------
578  
579 int
580 _evaluate(u, flags)
581 UV u
582 int flags
583 CODE:
584 10 RETVAL = (SHG->evaluate)(BOARD(u), flags);
585 OUTPUT:
586 RETVAL
587  
588 # The move, then the nodes (an NV, exact to 2**53), the depth, the score and
589 # whether the budget stopped it. `history` is an array reference of the keys,
590 # as sixteen hex digits, of the positions the game stood in before this one;
591 # anything else is no history.
592 void
593 _search(u, budget, max_depth, seed, flags, history)
594 UV u
595 NV budget
596 int max_depth
597 UV seed
598 int flags
599 SV *history
600 PREINIT:
601 struct shg_search_info info;
602 289 int move, n = 0;
603 289 unsigned long long *keys = NULL;
604 PPCODE:
605 289 ▶ if (SvROK(history) && SvTYPE(SvRV(history)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(history) && SvTYPE(SvRV(history)) == SVt_PVAV) { 289 0 289
if (SvROK(history) && SvTYPE(SvRV(history)) == SVt_PVAV) { 289 0 289
606 289 AV *av = (AV *)SvRV(history);
607 289 SSize_t i, len = av_len(av) + 1;
608 289 ▶ if (len > 0 && len < 100000) {
Branch: 2 branches
BranchTrueFalseTotal
if (len > 0 && len < 100000) { 170 119 289
if (len > 0 && len < 100000) { 170 0 170
609 170 ▶ Newx(keys, len, unsigned long long);
Branch: 1 branch
BranchTrueFalseTotal
Newx(keys, len, unsigned long long); 0 170 170
610 7664 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 7494 170 7664
611 7494 SV **k = av_fetch(av, i, 0);
612 STRLEN kl;
613 const char *ks;
614 7494 ▶ if (!k) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) continue; 0 7494 7494
615 7494 ks = SvPV(*k, kl);
616 7494 ▶ if (kl == 16) keys[n++] = strtoull(ks, NULL, 16);
Branch: 1 branch
BranchTrueFalseTotal
if (kl == 16) keys[n++] = strtoull(ks, NULL, 16); 7494 0 7494
617 }
618 }
619 }
620 289 ▶ move = (SHG->search)(BOARD(u), budget < 1 ? 1ULL : (unsigned long long) budget, max_depth,
Branch: 1 branch
BranchTrueFalseTotal
move = (SHG->search)(BOARD(u), budget < 1 ? 1ULL : (unsigned long long) budget, max_depth, 0 289 289
621 (unsigned int) seed, flags, keys, n, &info);
622 289 ▶ if (keys) Safefree(keys);
Branch: 1 branch
BranchTrueFalseTotal
if (keys) Safefree(keys); 170 119 289
623 289 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 289 289
624 289 PUSHs(sv_2mortal(newSViv(move)));
625 289 PUSHs(sv_2mortal(newSVnv((NV) info.nodes)));
626 289 PUSHs(sv_2mortal(newSViv(info.depth)));
627 289 PUSHs(sv_2mortal(newSViv(info.score)));
628 289 PUSHs(sv_2mortal(newSViv(info.stopped)));
629  
630 MODULE = Game::Shogi PACKAGE = Game::Shogi::Engine::Hold
631  
632 PROTOTYPES: DISABLE
633  
634 # ---- letting go -----------------------------------------------------------------
635 #
636 # WHY THIS IS NOT A DEMOLISH IN PERL. On perls 5.14 to 5.26 an eval that runs
637 # inside a destructor, while an exception is unwinding past a TEMPORARY of that
638 # class, leaves $@ empty: the caller's die arrives with no message. Object::Proto
639 # calls DEMOLISH under an eval, so any class with one has this. Fanorona found
640 # it; Sternhalma.xs carries the same fix. A DESTROY written in C runs no eval.
641  
642 void
643 DESTROY(self)
644 SV *self
645 PREINIT:
646 SV *inner;
647 UV u;
648 CODE:
649 5834 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 5834 0 5834
650 5834 inner = SvRV(self);
651 5834 u = SvUV(inner);
652 5834 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 5834 0 5834
653 5834 sv_setuv(inner, 0);
654 5834 (SHG->board_drop)(BOARD(u));
655 }
656 }
« blib/arch/Game/Shogi/Install/Files.pm ↑ Summary shg_moves.c »