Go.xs

filter: Statementstmt 96.5% 138 / 143 Branchbran 62.7% 74 / 118 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.2% 212 / 261 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/Go/Marking.pm ↑ Summary go_engine.c »
1 /* Go.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 * sibling that learned this the hard way says so in Balls.xs.
6 *
7 * The XSUBs land in Game::Go::Engine, not in Game::Go, and are named with a
8 * leading underscore: they take a raw pointer as a UV and there is no typemap.
9 * The Perl that owns the lifetime is lib/Game/Go/Engine.pm.
10 *
11 * PHASE 01 SCOPE. This door reaches the board and the chains only. `_put` and
12 * `_lift` are the structure primitives from go_abi.h and CHECK NOTHING: they
13 * exist so t/01-chains.t can build a position by hand. The rules XSUBs
14 * (legality, capture, ko, superko) are phase 02 and append to the table rather
15 * than changing it.
16 */
17  
18 #define PERL_NO_GET_CONTEXT
19  
20 #include "EXTERN.h"
21 #include "perl.h"
22 #include "XSUB.h"
23  
24 #include "go_abi.h"
25  
26 static const struct go_abi *GO = NULL;
27  
28 #define BOARD(u) INT2PTR(struct go_board *, (u))
29  
30 /* A history UV of 0 is a null history, which the engine reads as simple ko
31 * only. Zero is safe as the sentinel here in a way it is not for a point: a
32 * point of 0 is a real index into the padded board, but no allocation ever
33 * lands at address 0. */
34 #define HIST(u) ((u) ? INT2PTR(struct go_hist *, (u)) : (struct go_hist *)NULL)
35  
36 /* A 64-bit value crosses as a 16-character hex string, never as a UV.
37 *
38 * On a perl with 32-bit IVs a UV cannot hold it and the top half would vanish
39 * silently, which for a zobrist hash means a test that passes while comparing
40 * half a number. Hand-rolled rather than via my_snprintf, which takes a length
41 * argument that shadows a local called len in a way that has bitten the house
42 * before. */
43 1827 static SV *go_hex64(pTHX_ unsigned long long v)
44 {
45 static const char digits[] = "0123456789abcdef";
46 char buf[16];
47 int i;
48 31059 ▶ for (i = 15; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 15; i >= 0; i--) { 29232 1827 31059
49 29232 buf[i] = digits[v & 0xfULL];
50 29232 v >>= 4;
51 }
52 1827 return newSVpvn(buf, 16);
53 }
54  
55 MODULE = Game::Go PACKAGE = Game::Go::Engine
56  
57 PROTOTYPES: DISABLE
58  
59 BOOT:
60 26 GO = go_abi_table();
61  
62 UV
63 _abi_ptr()
64 CODE:
65 1 ▶ RETVAL = PTR2UV(GO);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(GO); 0 1 1
66 OUTPUT:
67 RETVAL
68  
69 UV
70 _abi_version()
71 CODE:
72 4 ▶ RETVAL = (UV)GO->abi_version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)GO->abi_version; 1 3 4
73 OUTPUT:
74 RETVAL
75  
76 UV
77 _new_board(size)
78 int size
79 PREINIT:
80 struct go_board *b;
81 CODE:
82 492 b = (GO->board_new)(size);
83 492 ▶ if (!b) croak("Game::Go::Engine: no board of size %d", size);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Game::Go::Engine: no board of size %d", size); 0 492 492
84 492 ▶ RETVAL = PTR2UV(b);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(b); 467 25 492
85 OUTPUT:
86 RETVAL
87  
88 void
89 _drop_board(u)
90 UV u
91 CODE:
92 514 (GO->board_drop)(BOARD(u));
93  
94 UV
95 _copy_board(u)
96 UV u
97 PREINIT:
98 struct go_board *b;
99 CODE:
100 22 b = (GO->board_copy)(BOARD(u));
101 22 ▶ if (!b) croak("Game::Go::Engine: could not copy the board");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Game::Go::Engine: could not copy the board"); 0 22 22
102 22 ▶ RETVAL = PTR2UV(b);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(b); 19 3 22
103 OUTPUT:
104 RETVAL
105  
106 int
107 _at(u, pt)
108 UV u
109 int pt
110 CODE:
111 21325 RETVAL = (GO->at)(BOARD(u), pt);
112 OUTPUT:
113 RETVAL
114  
115 int
116 _libs(u, pt)
117 UV u
118 int pt
119 CODE:
120 240 RETVAL = (GO->libs)(BOARD(u), pt);
121 OUTPUT:
122 RETVAL
123  
124 int
125 _chain_size(u, pt)
126 UV u
127 int pt
128 CODE:
129 578 RETVAL = (GO->chain_size)(BOARD(u), pt);
130 OUTPUT:
131 RETVAL
132  
133 void
134 _chain_at(u, pt)
135 UV u
136 int pt
137 PREINIT:
138 int pts[GO_MAX_PTS];
139 int n, i;
140 PPCODE:
141 502 n = (GO->chain_at)(BOARD(u), pt, pts);
142 502 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 502 0 502
EXTEND(SP, n); 0 502 502
143 4239 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 3737 502 4239
144 3737 PUSHs(sv_2mortal(newSViv(pts[i])));
145  
146 SV *
147 _pack(u)
148 UV u
149 PREINIT:
150 unsigned char buf[GO_MAX_PTS];
151 int n;
152 CODE:
153 13 n = (GO->pack)(BOARD(u), buf);
154 13 RETVAL = newSVpvn((char *)buf, (STRLEN)n);
155 OUTPUT:
156 RETVAL
157  
158 SV *
159 _hash_hex(u)
160 UV u
161 CODE:
162 29 RETVAL = go_hex64(aTHX_ (GO->hash)(BOARD(u)));
163 OUTPUT:
164 RETVAL
165  
166 SV *
167 _zobrist_hex(colour, pt)
168 int colour
169 int pt
170 CODE:
171 1797 RETVAL = go_hex64(aTHX_ (GO->zobrist)(colour, pt));
172 OUTPUT:
173 RETVAL
174  
175 int
176 _stones(u, colour)
177 UV u
178 int colour
179 CODE:
180 72 RETVAL = (GO->stones)(BOARD(u), colour);
181 OUTPUT:
182 RETVAL
183  
184 int
185 _point_of(u, col, row)
186 UV u
187 int col
188 int row
189 CODE:
190 39519 RETVAL = (GO->point_of)(BOARD(u), col, row);
191 OUTPUT:
192 RETVAL
193  
194 int
195 _col_of(u, pt)
196 UV u
197 int pt
198 CODE:
199 3996 RETVAL = (GO->col_of)(BOARD(u), pt);
200 OUTPUT:
201 RETVAL
202  
203 int
204 _row_of(u, pt)
205 UV u
206 int pt
207 CODE:
208 3996 RETVAL = (GO->row_of)(BOARD(u), pt);
209 OUTPUT:
210 RETVAL
211  
212 int
213 _size_of(u)
214 UV u
215 CODE:
216 776 RETVAL = (GO->size_of)(BOARD(u));
217 OUTPUT:
218 RETVAL
219  
220 int
221 _stride_of(u)
222 UV u
223 CODE:
224 5 RETVAL = (GO->stride_of)(BOARD(u));
225 OUTPUT:
226 RETVAL
227  
228 void
229 _put(u, pt, colour)
230 UV u
231 int pt
232 int colour
233 CODE:
234 464 (GO->put)(BOARD(u), pt, colour);
235  
236 int
237 _lift(u, pt)
238 UV u
239 int pt
240 CODE:
241 81 RETVAL = (GO->lift)(BOARD(u), pt);
242 OUTPUT:
243 RETVAL
244  
245 # ---------------------------------------------------------------------------
246 # PHASE 02: the rules of play.
247 #
248 # A history is a separate handle, because a board must stay pointer-free so a
249 # copy is a flat byte copy. A history UV of 0 means "no history", which the
250 # engine reads as simple ko and nothing else: that is the playout's rule and it
251 # is passed in rather than defaulted, so nobody inherits it by accident.
252 # ---------------------------------------------------------------------------
253  
254 UV
255 _new_hist(size)
256 int size
257 PREINIT:
258 struct go_hist *h;
259 CODE:
260 485 h = (GO->hist_new)(size);
261 485 ▶ if (!h) croak("Game::Go::Engine: no history for size %d", size);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Game::Go::Engine: no history for size %d", size); 0 485 485
262 485 ▶ RETVAL = PTR2UV(h);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(h); 461 24 485
263 OUTPUT:
264 RETVAL
265  
266 void
267 _drop_hist(h)
268 UV h
269 CODE:
270 487 ▶ (GO->hist_drop)(HIST(h));
Branch: 1 branch
BranchTrueFalseTotal
(GO->hist_drop)(HIST(h)); 487 0 487
271  
272 UV
273 _copy_hist(h)
274 UV h
275 PREINIT:
276 struct go_hist *n;
277 CODE:
278 2 ▶ n = (GO->hist_copy)(HIST(h));
Branch: 1 branch
BranchTrueFalseTotal
n = (GO->hist_copy)(HIST(h)); 2 0 2
279 2 ▶ if (!n) croak("Game::Go::Engine: could not copy the history");
Branch: 1 branch
BranchTrueFalseTotal
if (!n) croak("Game::Go::Engine: could not copy the history"); 0 2 2
280 2 ▶ RETVAL = PTR2UV(n);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(n); 0 2 2
281 OUTPUT:
282 RETVAL
283  
284 int
285 _hist_len(h)
286 UV h
287 CODE:
288 6 ▶ RETVAL = (GO->hist_len)(HIST(h));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (GO->hist_len)(HIST(h)); 6 0 6
289 OUTPUT:
290 RETVAL
291  
292 void
293 _hist_clear(h)
294 UV h
295 CODE:
296 0 ▶ (GO->hist_clear)(HIST(h));
Branch: 1 branch
BranchTrueFalseTotal
(GO->hist_clear)(HIST(h)); 0 0 0
297  
298 int
299 _hist_push(h, u)
300 UV h
301 UV u
302 CODE:
303 520 ▶ RETVAL = (GO->hist_push)(HIST(h), BOARD(u));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (GO->hist_push)(HIST(h), BOARD(u)); 520 0 520
304 OUTPUT:
305 RETVAL
306  
307 int
308 _hist_has(h, u)
309 UV h
310 UV u
311 CODE:
312 2 ▶ RETVAL = (GO->hist_has)(HIST(h), BOARD(u));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (GO->hist_has)(HIST(h), BOARD(u)); 2 0 2
313 OUTPUT:
314 RETVAL
315  
316 int
317 _legal(u, h, pt, colour)
318 UV u
319 UV h
320 int pt
321 int colour
322 CODE:
323 95 ▶ RETVAL = (GO->legal)(BOARD(u), HIST(h), pt, colour);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (GO->legal)(BOARD(u), HIST(h), pt, colour); 93 2 95
324 OUTPUT:
325 RETVAL
326  
327 void
328 _legal_moves(u, h, colour)
329 UV u
330 UV h
331 int colour
332 PREINIT:
333 int pts[GO_MAX_PTS];
334 int n, i;
335 PPCODE:
336 890 ▶ n = (GO->legal_moves)(BOARD(u), HIST(h), colour, pts);
Branch: 1 branch
BranchTrueFalseTotal
n = (GO->legal_moves)(BOARD(u), HIST(h), colour, pts); 890 0 890
337 890 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 890 0 890
EXTEND(SP, n); 0 890 890
338 32831 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 31941 890 32831
339 31941 PUSHs(sv_2mortal(newSViv(pts[i])));
340  
341 # Returns (code, ko_point, @captured). The code comes first because it is the
342 # only part a caller may ignore at its peril, and the captures come last
343 # because there can be any number of them.
344 void
345 _play(u, h, pt, colour)
346 UV u
347 UV h
348 int pt
349 int colour
350 PREINIT:
351 struct go_played res;
352 int i;
353 PPCODE:
354 3417 ▶ (GO->play)(BOARD(u), HIST(h), pt, colour, &res);
Branch: 1 branch
BranchTrueFalseTotal
(GO->play)(BOARD(u), HIST(h), pt, colour, &res); 3416 1 3417
355 3417 ▶ EXTEND(SP, 2 + res.ncaps);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, 2 + res.ncaps); 3417 0 3417
EXTEND(SP, 2 + res.ncaps); 0 3417 3417
356 3417 PUSHs(sv_2mortal(newSViv(res.code)));
357 3417 PUSHs(sv_2mortal(newSViv(res.ko_point)));
358 3830 ▶ for (i = 0; i < res.ncaps; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < res.ncaps; i++) 413 3417 3830
359 413 PUSHs(sv_2mortal(newSViv(res.caps[i])));
360  
361 void
362 _pass(u, colour)
363 UV u
364 int colour
365 CODE:
366 313 (GO->pass)(BOARD(u), colour);
367  
368 int
369 _ko_point(u)
370 UV u
371 CODE:
372 13 RETVAL = (GO->ko_point)(BOARD(u));
373 OUTPUT:
374 RETVAL
375  
376 int
377 _ko_colour(u)
378 UV u
379 CODE:
380 2 RETVAL = (GO->ko_colour)(BOARD(u));
381 OUTPUT:
382 RETVAL
383  
384 void
385 _set_superko(u, on)
386 UV u
387 int on
388 CODE:
389 28 (GO->set_superko)(BOARD(u), on);
390  
391 int
392 _superko_on(u)
393 UV u
394 CODE:
395 28 RETVAL = (GO->superko_on)(BOARD(u));
396 OUTPUT:
397 RETVAL
398  
399 SV *
400 _hash_after_hex(u, pt, colour)
401 UV u
402 int pt
403 int colour
404 CODE:
405 1 RETVAL = go_hex64(aTHX_ (GO->hash_after)(BOARD(u), pt, colour));
406 OUTPUT:
407 RETVAL
408  
409 # ---------------------------------------------------------------------------
410 # PHASE 04: the only part of life and death that is decidable.
411 # ---------------------------------------------------------------------------
412  
413 void
414 _alive(u, colour)
415 UV u
416 int colour
417 PREINIT:
418 int pts[GO_MAX_PTS];
419 int n, i;
420 PPCODE:
421 70 n = (GO->alive)(BOARD(u), colour, pts);
422 70 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 70 0 70
EXTEND(SP, n); 0 70 70
423 560 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 490 70 560
424 490 PUSHs(sv_2mortal(newSViv(pts[i])));
425  
426 # ---------------------------------------------------------------------------
427 # PHASE 05: the two scorers. Both fill one struct; the Perl picks what it wants.
428 # Scores come back in TENTHS, because komi is fractional and the engine has no
429 # floats at all.
430 # ---------------------------------------------------------------------------
431  
432 void
433 _score(u, dead, seki, prisoners_b, prisoners_w, komi_tenths)
434 UV u
435 AV *dead
436 AV *seki
437 int prisoners_b
438 int prisoners_w
439 int komi_tenths
440 PREINIT:
441 struct go_score s;
442 int dpts[GO_MAX_PTS];
443 int spts[GO_MAX_PTS];
444 int nd, ns, i;
445 PPCODE:
446 64 nd = (int)(av_len(dead) + 1);
447 64 ▶ if (nd > GO_MAX_PTS) nd = GO_MAX_PTS;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > GO_MAX_PTS) nd = GO_MAX_PTS; 0 64 64
448 71 ▶ for (i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) { 7 64 71
449 7 SV **e = av_fetch(dead, i, 0);
450 7 ▶ dpts[i] = e ? (int)SvIV(*e) : -1;
Branch: 1 branch
BranchTrueFalseTotal
dpts[i] = e ? (int)SvIV(*e) : -1; 7 0 7
451 }
452 64 ns = (int)(av_len(seki) + 1);
453 64 ▶ if (ns > GO_MAX_PTS) ns = GO_MAX_PTS;
Branch: 1 branch
BranchTrueFalseTotal
if (ns > GO_MAX_PTS) ns = GO_MAX_PTS; 0 64 64
454 70 ▶ for (i = 0; i < ns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ns; i++) { 6 64 70
455 6 SV **e = av_fetch(seki, i, 0);
456 6 ▶ spts[i] = e ? (int)SvIV(*e) : -1;
Branch: 1 branch
BranchTrueFalseTotal
spts[i] = e ? (int)SvIV(*e) : -1; 6 0 6
457 }
458  
459 64 (GO->score_jp)(BOARD(u), dpts, nd, spts, ns,
460 prisoners_b, prisoners_w, komi_tenths, &s);
461  
462 64 ▶ EXTEND(SP, 26);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 26); 0 64 64
463 64 PUSHs(sv_2mortal(newSVpvs("eyes_b"))); PUSHs(sv_2mortal(newSViv(s.eyes_b)));
464 64 PUSHs(sv_2mortal(newSVpvs("eyes_w"))); PUSHs(sv_2mortal(newSViv(s.eyes_w)));
465 64 PUSHs(sv_2mortal(newSVpvs("territory_b"))); PUSHs(sv_2mortal(newSViv(s.territory_b)));
466 64 PUSHs(sv_2mortal(newSVpvs("territory_w"))); PUSHs(sv_2mortal(newSViv(s.territory_w)));
467 64 PUSHs(sv_2mortal(newSVpvs("dame"))); PUSHs(sv_2mortal(newSViv(s.dame)));
468 64 PUSHs(sv_2mortal(newSVpvs("prisoners_b"))); PUSHs(sv_2mortal(newSViv(s.prisoners_b)));
469 64 PUSHs(sv_2mortal(newSVpvs("prisoners_w"))); PUSHs(sv_2mortal(newSViv(s.prisoners_w)));
470 64 PUSHs(sv_2mortal(newSVpvs("stones_b"))); PUSHs(sv_2mortal(newSViv(s.stones_b)));
471 64 PUSHs(sv_2mortal(newSVpvs("stones_w"))); PUSHs(sv_2mortal(newSViv(s.stones_w)));
472 64 PUSHs(sv_2mortal(newSVpvs("area_b"))); PUSHs(sv_2mortal(newSViv(s.area_b)));
473 64 PUSHs(sv_2mortal(newSVpvs("area_w"))); PUSHs(sv_2mortal(newSViv(s.area_w)));
474 64 PUSHs(sv_2mortal(newSVpvs("score_b"))); PUSHs(sv_2mortal(newSViv(s.score_b)));
475 64 PUSHs(sv_2mortal(newSVpvs("score_w"))); PUSHs(sv_2mortal(newSViv(s.score_w)));
476  
477 void
478 _score_area(u)
479 UV u
480 PREINIT:
481 struct go_score s;
482 PPCODE:
483 2 (GO->score_area)(BOARD(u), &s);
484 2 ▶ EXTEND(SP, 10);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 10); 0 2 2
485 2 PUSHs(sv_2mortal(newSVpvs("area_b"))); PUSHs(sv_2mortal(newSViv(s.area_b)));
486 2 PUSHs(sv_2mortal(newSVpvs("area_w"))); PUSHs(sv_2mortal(newSViv(s.area_w)));
487 2 PUSHs(sv_2mortal(newSVpvs("dame"))); PUSHs(sv_2mortal(newSViv(s.dame)));
488 2 PUSHs(sv_2mortal(newSVpvs("stones_b"))); PUSHs(sv_2mortal(newSViv(s.stones_b)));
489 2 PUSHs(sv_2mortal(newSVpvs("stones_w"))); PUSHs(sv_2mortal(newSViv(s.stones_w)));
490  
491 # Who owns each empty point, by the scorer's own classification. Returns a list
492 # of (point, colour) pairs for the points that belong to somebody.
493 void
494 _territory(u, dead, seki)
495 UV u
496 AV *dead
497 AV *seki
498 PREINIT:
499 signed char owner[GO_MAX_PTS];
500 int dpts[GO_MAX_PTS];
501 int spts[GO_MAX_PTS];
502 int nd, ns, i, n;
503 struct go_board *b;
504 PPCODE:
505 1 b = BOARD(u);
506 1 nd = (int)(av_len(dead) + 1);
507 1 ▶ if (nd > GO_MAX_PTS) nd = GO_MAX_PTS;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > GO_MAX_PTS) nd = GO_MAX_PTS; 0 1 1
508 1 ▶ for (i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nd; i++) { 0 1 1
509 0 SV **e = av_fetch(dead, i, 0);
510 0 ▶ dpts[i] = e ? (int)SvIV(*e) : -1;
Branch: 1 branch
BranchTrueFalseTotal
dpts[i] = e ? (int)SvIV(*e) : -1; 0 0 0
511 }
512 1 ns = (int)(av_len(seki) + 1);
513 1 ▶ if (ns > GO_MAX_PTS) ns = GO_MAX_PTS;
Branch: 1 branch
BranchTrueFalseTotal
if (ns > GO_MAX_PTS) ns = GO_MAX_PTS; 0 1 1
514 1 ▶ for (i = 0; i < ns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ns; i++) { 0 1 1
515 0 SV **e = av_fetch(seki, i, 0);
516 0 ▶ spts[i] = e ? (int)SvIV(*e) : -1;
Branch: 1 branch
BranchTrueFalseTotal
spts[i] = e ? (int)SvIV(*e) : -1; 0 0 0
517 }
518  
519 1 (GO->territory)(b, dpts, nd, spts, ns, owner);
520  
521 1 n = (GO->size_of)(b);
522 1 n = (n + 2) * (n + 2);
523 122 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 121 1 122
524 121 ▶ if (owner[i] != GO_BLACK && owner[i] != GO_WHITE) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (owner[i] != GO_BLACK && owner[i] != GO_WHITE) continue; 116 5 121
if (owner[i] != GO_BLACK && owner[i] != GO_WHITE) continue; 116 0 116
525 5 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 5 5
526 5 PUSHs(sv_2mortal(newSViv(i)));
527 5 PUSHs(sv_2mortal(newSViv(owner[i])));
528 }
529  
530 # ---------------------------------------------------------------------------
531 # PHASE 08: the search. Budget in PLAYOUTS, never in seconds.
532 # ---------------------------------------------------------------------------
533  
534 UV
535 _prng_next(state)
536 UV state
537 PREINIT:
538 unsigned int s;
539 CODE:
540 4099 s = (unsigned int)state;
541 4099 (void)(GO->prng_next)(&s);
542 4099 ▶ RETVAL = (UV)s;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)s; 4095 4 4099
543 OUTPUT:
544 RETVAL
545  
546 # Returns (score difference, moves played). THE MOVE COUNT IS THE OBSERVABLE
547 # THAT MATTERS: the playout runs on an internal copy, so the caller's board is
548 # untouched and there is nothing to inspect afterwards. Whether it SETTLED (two
549 # consecutive passes, few moves) or hit its cap (3 * size * size) is the only
550 # way to see the eye rule working from outside.
551 void
552 _playout(u, colour, seed)
553 UV u
554 int colour
555 UV seed
556 PREINIT:
557 unsigned int s;
558 120 int moves = 0;
559 int diff;
560 PPCODE:
561 120 s = (unsigned int)seed;
562 120 diff = (GO->playout)(BOARD(u), colour, &s, &moves);
563 120 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 120 120
564 120 PUSHs(sv_2mortal(newSViv(diff)));
565 120 PUSHs(sv_2mortal(newSViv(moves)));
566  
567 void
568 _search(u, colour, allowed, seed, playouts, explore)
569 UV u
570 int colour
571 AV *allowed
572 UV seed
573 int playouts
574 int explore
575 PREINIT:
576 struct go_search res;
577 int pts[GO_MAX_PTS + 1];
578 int n, i;
579 PPCODE:
580 799 n = (int)(av_len(allowed) + 1);
581 799 ▶ if (n > GO_MAX_PTS) n = GO_MAX_PTS;
Branch: 1 branch
BranchTrueFalseTotal
if (n > GO_MAX_PTS) n = GO_MAX_PTS; 0 799 799
582 29050 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 28251 799 29050
583 28251 SV **e = av_fetch(allowed, i, 0);
584 28251 ▶ pts[i] = e ? (int)SvIV(*e) : -1;
Branch: 1 branch
BranchTrueFalseTotal
pts[i] = e ? (int)SvIV(*e) : -1; 28251 0 28251
585 }
586  
587 799 (GO->search)(BOARD(u), colour, pts, n,
588 (unsigned int)seed, playouts, explore, &res);
589  
590 799 ▶ EXTEND(SP, 12);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 12); 0 799 799
591 799 PUSHs(sv_2mortal(newSVpvs("point"))); PUSHs(sv_2mortal(newSViv(res.point)));
592 799 PUSHs(sv_2mortal(newSVpvs("playouts"))); PUSHs(sv_2mortal(newSViv(res.playouts)));
593 799 PUSHs(sv_2mortal(newSVpvs("visits"))); PUSHs(sv_2mortal(newSViv(res.visits)));
594 799 PUSHs(sv_2mortal(newSVpvs("win_permille"))); PUSHs(sv_2mortal(newSViv(res.win_permille)));
595 799 PUSHs(sv_2mortal(newSVpvs("capped"))); PUSHs(sv_2mortal(newSViv(res.capped)));
596 799 PUSHs(sv_2mortal(newSVpvs("moves"))); PUSHs(sv_2mortal(newSViv(res.moves)));
597  
598 void
599 _dead_guess(u, seed, playouts)
600 UV u
601 UV seed
602 int playouts
603 PREINIT:
604 int pts[GO_MAX_PTS];
605 int n, i;
606 PPCODE:
607 22 n = (GO->dead_guess)(BOARD(u), (unsigned int)seed, playouts, pts);
608 22 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 22 0 22
EXTEND(SP, n); 0 22 22
609 46 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 24 22 46
610 24 PUSHs(sv_2mortal(newSViv(pts[i])));
« blib/lib/Game/Go/Marking.pm ↑ Summary go_engine.c »