Plisco/Tablebase/Syzygy.pm

filter: Statementstmt 95.6% 1346 / 1407 Branchbran 76.9% 351 / 456 Conditioncond 58.0% 58 / 100 MC/DCmcdc 31.0% 18 / 58 Subroutinesub 96.9% 158 / 163 Podpod 90.9% 10 / 11 totaltotal 88.4% 1941 / 2195 CC 319 SCAR 26.0
CC
319
Cov
89%
probeDtz
55.1
__ANON__
42.0
__probeDtzNoEP
36.5
CRAP 13.4
« Plisco/Engine/Book.pm ↑ Summary Plisco/EPD.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2021-2026 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software. It comes without any warranty, to
7 # the extent permitted by applicable law. You can redistribute it
8 # and/or modify it under the terms of the Do What the Fuck You Want
9 # to Public License, Version 2, as published by Sam Hocevar. See
10 # http://www.wtfpl.net/ for more details.
11  
12 # This file is heavily inspired by python-chess.
13  
14 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Locale::TextDomain qw('Chess-Plisco');
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 10 ▶use Chess::Plisco qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 # Macros from Chess::Plisco::Macro are already expanded here!
23  
24 10 my $TBPIECES = 7;
25  
26 10 ▶use constant PD_INDEXTABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use constant PD_SIZETABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 10 ▶use constant PD_DATA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 10 ▶use constant PD_OFFSET => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 10 ▶use constant PD_SYMLEN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 10 ▶use constant PD_SYMPAT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 10 ▶use constant PD_BLOCKSIZE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 10 ▶use constant PD_IDXBITS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 10 ▶use constant PD_MIN_LEN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 10 ▶use constant PD_BASE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 10 ▶use constant INVTRIANGLE => [1, 2, 3, 10, 11, 19, 0, 9, 18, 27];
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # FIXME! These are candidates for macros!
40 14043 ▶my $offdiag = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 14043 my ($shift) = @_;
42  
43 14043 my ($file, $rank) = ($shift & 0x7, $shift >> 3);
44  
45 14043 return $rank - $file;
46 10 };
47  
48 4870 ▶my $flipdiag = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 4870 my ($shift) = @_;
50  
51 4870 return (($shift >> 3) | ($shift << 3)) & 63;
52 10 };
53  
54 857961 ▶my $read_byte = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 857961 my ($data_ref, $offset) = @_;
56  
57 857961 return ord substr $$data_ref, $offset, 1;
58 10 };
59  
60 3075 ▶my $remove_ep = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 3075 my ($pos) = @_;
62  
63 3075 my $pos2 = $pos->copy;
64  
65 3075 $pos2->[CP_POS_EN_PASSANT_SHIFT] = 0;
66  
67 3075 return $pos2;
68 10 };
69  
70 0 ▶my $is_checkmate = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
71 0 my ($pos) = @_;
72  
73 0 ▶ my $game_over = $pos->gameOver or return;
Branch: 1 branch
BranchTrueFalseTotal
my $game_over = $pos->gameOver or return 0 0 0
74 0 ▶ return 1 if ($game_over && CP_GAME_WHITE_WINS);
Branch: 1 branch
BranchTrueFalseTotal
if $game_over and 2 0 0 0
Condition0%0/2
$game_over and 2
!ll
00
MC/DC0%0/1
$game_over and 2
$game_over
Truth table0%0/2
A
A= $game_over
Aresult
00
11
75 0 ▶ return 1 if ($game_over && CP_GAME_BLACK_WINS);
Branch: 1 branch
BranchTrueFalseTotal
if $game_over and 4 0 0 0
Condition0%0/2
$game_over and 4
!ll
00
MC/DC0%0/1
$game_over and 4
$game_over
Truth table0%0/2
A
A= $game_over
Aresult
00
11
76 10 };
77  
78 10 ▶use constant PD_INDEXTABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 10 ▶use constant PD_SIZETABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 10 ▶use constant PD_DATA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 10 ▶use constant PD_OFFSET => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 10 ▶use constant PD_SYMLEN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 10 ▶use constant PD_SYMPAT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 10 ▶use constant PD_BLOCKSIZE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 10 ▶use constant PD_IDXBITS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 10 ▶use constant PD_MIN_LEN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 10 ▶use constant PD_BASE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 10 my @PTWIST = (
90 0, 0, 0, 0, 0, 0, 0, 0,
91 47, 35, 23, 11, 10, 22, 34, 46,
92 45, 33, 21, 9, 8, 20, 32, 44,
93 43, 31, 19, 7, 6, 18, 30, 42,
94 41, 29, 17, 5, 4, 16, 28, 40,
95 39, 27, 15, 3, 2, 14, 26, 38,
96 37, 25, 13, 1, 0, 12, 24, 36,
97 0, 0, 0, 0, 0, 0, 0, 0,
98 );
99  
100 10 ▶use constant INVFLAP => [
Subroutine BEGIN: called CC=1 SCAR=0.0
101 8, 16, 24, 32, 40, 48,
102 9, 17, 25, 33, 41, 49,
103 10, 18, 26, 34, 42, 50,
104 11, 19, 27, 35, 43, 51,
105 10 ];
106  
107 10 ▶use constant TEST45_MASK => (1 << (CP_A5)) | (1 << (CP_A6)) | (1 << (CP_A7)) | (1 << (CP_B5)) | (1 << (CP_B6)) | (1 << (CP_C5));
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 0 ▶my $test45 = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 my ($shift) = @_;
111  
112 0 return !!((1 << $shift) & TEST45_MASK);
113 10 };
114  
115 10 ▶use constant MTWIST => [
Subroutine BEGIN: called CC=1 SCAR=0.0
116 15, 63, 55, 47, 40, 48, 56, 12,
117 62, 11, 39, 31, 24, 32, 8, 57,
118 54, 38, 7, 23, 16, 4, 33, 49,
119 46, 30, 22, 3, 0, 17, 25, 41,
120 45, 29, 21, 2, 1, 18, 26, 42,
121 53, 37, 6, 20, 19, 5, 34, 50,
122 61, 10, 36, 28, 27, 35, 9, 58,
123 14, 60, 52, 44, 43, 51, 59, 13,
124 10 ];
125  
126 10 my @PAWNIDX = ([], [], [], [], []);
127 10 my @PFACTOR = ([], [], [], [], []);
128  
129 5671 ▶my $binom = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 5671 my ($x, $y) = @_;
131  
132 5671 ▶ my $numerator = reduce { $a * $b } $x - $y + 1 .. $x;
Subroutine __ANON__: called CC=1 SCAR=0.0
133 5671 ▶ my $denominator = reduce { $a * $b } 1 .. $y;
Subroutine __ANON__: called CC=1 SCAR=0.0
134  
135 5671 return $numerator / $denominator;
136 10 };
137  
138 10 for my $i (0 .. 4) {
139 50 my $j = 0;
140  
141 50 my $s = 0;
142 50 ▶ while ($j < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 6) 300 50 350
143 300 $PAWNIDX[$i]->[$j] = $s;
144 300 ▶ $s += $i == 0 ? 1 : $binom->($PTWIST[INVFLAP->[$j]], $i);
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 60 240 300
145 300 ++$j;
146 }
147 50 $PFACTOR[$i]->[0] = $s;
148  
149 50 $s = 0;
150 50 ▶ while ($j < 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 12) 300 50 350
151 300 $PAWNIDX[$i]->[$j] = $s;
152 300 ▶ $s += $i == 0 ? 1 : $binom->($PTWIST[INVFLAP->[$j]], $i);
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 60 240 300
153 300 ++$j;
154 }
155 50 $PFACTOR[$i]->[1] = $s;
156  
157 50 $s = 0;
158 50 ▶ while ($j < 18) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 18) 300 50 350
159 300 $PAWNIDX[$i]->[$j] = $s;
160 300 ▶ $s += $i == 0 ? 1 : $binom->($PTWIST[INVFLAP->[$j]], $i);
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 60 240 300
161 300 ++$j;
162 }
163 50 $PFACTOR[$i]->[2] = $s;
164  
165 50 $s = 0;
166 50 ▶ while ($j < 24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 24) 300 50 350
167 300 $PAWNIDX[$i]->[$j] = $s;
168 300 ▶ $s += $i == 0 ? 1 : $binom->($PTWIST[INVFLAP->[$j]], $i);
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 60 240 300
169 300 ++$j;
170 }
171 50 $PFACTOR[$i][3] = $s;
172 }
173  
174 10 my @MULTIDX = ([], [], [], [], []);
175 10 my @MFACTOR;
176  
177 10 for my $i (0 .. 4) {
178 50 my $s = 0;
179 50 for my $j (0 .. 9) {
180 500 $MULTIDX[$i]->[$j] = $s;
181 500 ▶ $s += $i == 0 ? 1 : $binom->(MTWIST->[INVTRIANGLE->[$j]], $i);
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 100 400 500
182 500 ++$j;
183 }
184 50 $MFACTOR[$i] = $s;
185 }
186  
187 10 my @PCHR = ('K', 'Q', 'R', 'B', 'N', 'P');
188 10 my %PCHR_IDX = map { $PCHR[$_] => $_ } 0 .. $#PCHR;
189  
190 10 ▶use constant TABLENAME_REGEX => qr/^[KQRBNP]+v[KQRBNP]+\Z/;
Subroutine BEGIN: called CC=1 SCAR=0.0
191  
192 644 ▶my $normalise_tablename = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
193 644 my ($name, $mirror) = @_;
194  
195 644 my ($white, $black) = split /v/, $name, 2;
196  
197 # Sort pieces according to PCHR order.
198 644 $white = join '', sort { $PCHR_IDX{$a} <=> $PCHR_IDX{$b} } split //, $white;
199 644 $black = join '', sort { $PCHR_IDX{$a} <=> $PCHR_IDX{$b} } split //, $black;
200  
201 644 ▶ return $mirror ? $black . "v" . $white : $white . "v" . $black;
Branch: 1 branch
BranchTrueFalseTotal
$mirror ? : 112 532 644
202 10 };
203  
204 420 ▶my $is_tablename = sub {
Subroutine __ANON__: called CC=7 SCAR=22.7
205 420 my ($name, %__options) = @_;
206  
207 420 my %options = (
208 normalised => 1,
209 piece_count => $TBPIECES,
210 %__options,
211 );
212  
213 420 ▶ return if defined $options{piece_count} && length $name > $options{piece_count} + 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"piece_count"} and length $name > $options{"piece_count"} + 1 0 420 420
Condition33%1/3
defined $options{"piece_count"} and length $name > $options{"piece_count"} + 1
!ll&&!rl&&r
04200
MC/DC0%0/2
defined $options{"piece_count"} and length $name > $options{"piece_count"} + 1
defined $options{"piece_count"} length $name > $options{"piece_count"} + 1
Truth table33%1/3
A and B
A= defined $options{"piece_count"}B= length $name > $options{"piece_count"} + 1
ABresult
0X0
100
111
214 420 ▶ return if $name !~ TABLENAME_REGEX;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[KQRBNP]+v[KQRBNP]+\Z/ 0 420 420
215 420 ▶ return if $name =~ /K.*K.*K/; # More than three kings.
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /K.*K.*K/ 0 420 420
216 420 ▶ return if $options{normalised} && $normalise_tablename->($name) ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if $options{"normalised"} and &$normalise_tablename($name) ne $name 0 420 420
Condition33%1/3
$options{"normalised"} and &$normalise_tablename($name) ne $name
!ll&&!rl&&r
04200
MC/DC0%0/2
$options{"normalised"} and &$normalise_tablename($name) ne $name
$options{"normalised"} &$normalise_tablename($name) ne $name
Truth table33%1/3
A and B
A= $options{"normalised"}B= &$normalise_tablename($name) ne $name
ABresult
0X0
100
111
217  
218 420 return 1;
219 10 };
220  
221 8524 ▶my $calc_key = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
222 8524 my ($pos, $mirror) = @_;
223  
224 8524 ▶ $mirror //= 0;
Condition100%2/2
$mirror //= 0
l!l
18523
MC/DC100%1/1
$mirror //= 0
$mirror
Truth table100%2/2
A
A= $mirror
Aresult
11
00
225  
226 8524 my ($wh, $bl) = ($pos->[CP_POS_WHITE_PIECES], $pos->[CP_POS_BLACK_PIECES]);
227  
228 8524 my @pieces = qw(P N B R Q);
229  
230 8524 my ($wkey, $bkey) = ('K', 'K');
231 8524 foreach my $i (reverse CP_POS_PAWNS .. CP_POS_QUEENS) {
232 42620 my $popcount;
233  
234 42620 ▶ { my $_b = $pos->[$i] & $wh; for ($popcount = 0; $_b; ++$popcount) { $_b &= $_b - 1; } };
Branch: 1 branch
BranchTrueFalseTotal
if ($_b) 7394 42620 50014
235 42620 $wkey .= $pieces[$i - CP_POS_PAWNS] x $popcount;
236  
237 42620 ▶ { my $_b = $pos->[$i] & $bl; for ($popcount = 0; $_b; ++$popcount) { $_b &= $_b - 1; } };
Branch: 1 branch
BranchTrueFalseTotal
if ($_b) 8526 42620 51146
238 42620 $bkey .= $pieces[$i - CP_POS_PAWNS] x $popcount;
239 }
240  
241 8524 ▶ return $mirror ? (join 'v', $bkey, $wkey) : (join 'v', $wkey, $bkey);
Branch: 1 branch
BranchTrueFalseTotal
$mirror ? : 1 8523 8524
242 10 };
243  
244 # Some endgames are stored with a different key than their filename
245 # indicates: http://talkchess.com/forum/viewtopic.php?p=695509#695509
246 144 ▶my $recalc_key = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
247 144 my ($pieces, $mirror) = @_;
248  
249 144 ▶ $mirror //= 0;
Condition100%2/2
$mirror //= 0
l!l
7272
MC/DC100%1/1
$mirror //= 0
$mirror
Truth table100%2/2
A
A= $mirror
Aresult
11
00
250  
251 144 ▶ my ($w, $b) = $mirror ? (8, 0) : (0, 8);
Branch: 1 branch
BranchTrueFalseTotal
$mirror ? : 72 72 144
252 144 my @l = qw(K Q R B N P);
253  
254 my $white = join '', map
255 {
256 144 my $c = (6 - $_) ^ $w;
257 864 my $n = grep { $c == $_ } @$pieces;
258 864 $l[$_] x $n;
259 } 0 .. 5;
260 my $black = join '', map
261 {
262 144 my $c = (6 - $_) ^ $b;
263 864 my $n = grep { $c == $_ } @$pieces;
264 864 $l[$_] x $n;
265 } 0 .. 5;
266  
267 144 return join 'v', $white, $black;
268 10 };
269  
270 825 ▶my $subfactor = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
271 825 my ($k, $n) = @_;
272  
273 825 my $f = $n;
274 825 my $l = 1;
275  
276 825 for my $i (1 .. $k - 1) {
277 22 $f *= $n - $i;
278 22 $l *= $i + 1;
279 }
280  
281 825 return int($f / $l);
282 10 };
283  
284 1687 ▶my $dtz_before_zeroing = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
285 1687 my ($wdl) = @_;
286  
287 1687 ▶ my $sign = ($wdl > 0 ? 1 : 0) - ($wdl < 0 ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$wdl > 0 ? : 1611 76 1687
$wdl < 0 ? : 76 1611 1687
288 1687 ▶ my $factor = (abs($wdl) == 2 ? 1 : 101);
Branch: 1 branch
BranchTrueFalseTotal
abs $wdl == 2 ? : 1687 0 1687
289  
290 1687 return $sign * $factor;
291 10 };
292  
293 package Chess::Plisco::Tablebase::Syzygy::Testing;
294 10 $Chess::Plisco::Tablebase::Syzygy::Testing::VERSION = 'v1.0.3';
295 5 ▶sub calc_key {
Subroutine calc_key: called CC=1 SCAR=0.0
296 5 my (undef, $pos, $mirror) = @_;
297  
298 5 return $calc_key->($pos, $mirror);
299 }
300  
301 2 ▶sub normalise_tablename {
Subroutine normalise_tablename: called CC=1 SCAR=0.0
302 2 my (undef, $name, $mirror) = @_;
303  
304 2 return $normalise_tablename->($name, $mirror);
305 }
306  
307 64 ▶sub offdiag {
Subroutine offdiag: called CC=1 SCAR=0.0
308 64 my (undef, $shift) = @_;
309  
310 64 return $offdiag->($shift);
311 }
312  
313 64 ▶sub flipdiag {
Subroutine flipdiag: called CC=1 SCAR=0.0
314 64 my (undef, $shift) = @_;
315  
316 64 return $flipdiag->($shift);
317 }
318  
319 package Chess::Plisco::Tablebase::Syzygy::MissingTableException;
320 10 $Chess::Plisco::Tablebase::Syzygy::MissingTableException::VERSION = 'v1.0.3';
321 10 ▶use overload '""' => sub { ${$_[0]} };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
322  
323 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
324 4 my ($class, $msg) = @_;
325  
326 4 bless \$msg, $class;
327 }
328  
329 package Chess::Plisco::Tablebase::Syzygy::PairsData;
330 10 $Chess::Plisco::Tablebase::Syzygy::PairsData::VERSION = 'v1.0.3';
331 506 ▶sub new {
Subroutine new: called CC=11 SCAR=31.1
332 506 my ($class, %args) = @_;
333  
334 bless [
335 $args{indextable} // 0,
336 $args{sizetable} // 0,
337 $args{data} // 0,
338 $args{offset} // 0,
339 $args{symlen} // [],
340 $args{sympat} // 0,
341 $args{blocksize} // 0,
342 $args{idxbits} // 0,
343 $args{min_len} // 0,
344 506 ▶ $args{base} // [],
Condition50%10/20
$args{"indextable"} // 0
l!l
0506
$args{"sizetable"} // 0
l!l
0506
$args{"data"} // 0
l!l
0506
$args{"offset"} // 0
l!l
0506
$args{"symlen"} // []
l!l
0506
$args{"sympat"} // 0
l!l
0506
$args{"blocksize"} // 0
l!l
0506
$args{"idxbits"} // 0
l!l
0506
$args{"min_len"} // 0
l!l
0506
$args{"base"} // []
l!l
0506
MC/DC0%0/1
$args{"indextable"} // 0
$args{"indextable"}
MC/DC0%0/1
$args{"sizetable"} // 0
$args{"sizetable"}
MC/DC0%0/1
$args{"data"} // 0
$args{"data"}
MC/DC0%0/1
$args{"offset"} // 0
$args{"offset"}
MC/DC0%0/1
$args{"symlen"} // []
$args{"symlen"}
MC/DC0%0/1
$args{"sympat"} // 0
$args{"sympat"}
MC/DC0%0/1
$args{"blocksize"} // 0
$args{"blocksize"}
MC/DC0%0/1
$args{"idxbits"} // 0
$args{"idxbits"}
MC/DC0%0/1
$args{"min_len"} // 0
$args{"min_len"}
MC/DC0%0/1
$args{"base"} // []
$args{"base"}
Truth table50%1/2
A
A= $args{"indextable"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"sizetable"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"data"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"offset"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"symlen"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"sympat"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"blocksize"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"idxbits"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"min_len"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"base"}
Aresult
11
00
345 ], $class;
346 }
347  
348 package Chess::Plisco::Tablebase::Syzygy::PawnFileData;
349 10 $Chess::Plisco::Tablebase::Syzygy::PawnFileData::VERSION = 'v1.0.3';
350 300 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
351 300 my ($class) = @_;
352 300 bless {
353 precomp => [],
354 factor => [],
355 pieces => [],
356 norm => [],
357 }, $class;
358 }
359  
360 package Chess::Plisco::Tablebase::Syzygy::PawnFileDataDtz;
361 10 $Chess::Plisco::Tablebase::Syzygy::PawnFileDataDtz::VERSION = 'v1.0.3';
362 144 ▶sub new {
Subroutine new: called CC=5 SCAR=19.9
363 144 my ($class, %args) = @_;
364  
365 bless {
366 precomp => $args{precomp} // Chess::Plisco::Tablebase::Syzygy::PairsData->new(),
367 factor => $args{factor} // [],
368 pieces => $args{pieces} // [],
369 144 ▶ norm => $args{norm} // [],
Condition44%4/9
$args{"precomp"} // "Chess::Plisco::Tablebase::Syzygy::PairsData"->new
l!l&&r!l&&!r
01440
$args{"factor"} // []
l!l
0144
$args{"pieces"} // []
l!l
0144
$args{"norm"} // []
l!l
0144
MC/DC0%0/2
$args{"precomp"} // "Chess::Plisco::Tablebase::Syzygy::PairsData"->new
$args{"precomp"} "Chess::Plisco::Tablebase::Syzygy::PairsData"->new
MC/DC0%0/1
$args{"factor"} // []
$args{"factor"}
MC/DC0%0/1
$args{"pieces"} // []
$args{"pieces"}
MC/DC0%0/1
$args{"norm"} // []
$args{"norm"}
Truth table33%1/3
A // B
A= $args{"precomp"}B= "Chess::Plisco::Tablebase::Syzygy::PairsData"->new
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{"factor"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"pieces"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"norm"}
Aresult
11
00
370 }, $class;
371 }
372  
373 package Chess::Plisco::Tablebase::Syzygy::Table;
374 10 $Chess::Plisco::Tablebase::Syzygy::Table::VERSION = 'v1.0.3';
375 10 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
376  
377 10 ▶use Fcntl qw(O_RDONLY O_BINARY);
Subroutine BEGIN: called CC=1 SCAR=0.0
378 10 ▶use Sys::Mmap qw(mmap PROT_READ MAP_SHARED);
Subroutine BEGIN: called CC=1 SCAR=0.0
379 10 ▶use Math::Int64 qw(uint64);
Subroutine BEGIN: called CC=1 SCAR=0.0
380  
381 10 ▶use Locale::TextDomain qw(Chess-Plisco);
Subroutine BEGIN: called CC=1 SCAR=0.0
382  
383 10 ▶use constant FLAP => [
Subroutine BEGIN: called CC=1 SCAR=0.0
384 0, 0, 0, 0, 0, 0, 0, 0,
385 0, 6, 12, 18, 18, 12, 6, 0,
386 1, 7, 13, 19, 19, 13, 7, 1,
387 2, 8, 14, 20, 20, 14, 8, 2,
388 3, 9, 15, 21, 21, 15, 9, 3,
389 4, 10, 16, 22, 22, 16, 10, 4,
390 5, 11, 17, 23, 23, 17, 11, 5,
391 0, 0, 0, 0, 0, 0, 0, 0,
392 10 ];
393  
394 10 ▶use constant FILE_TO_FILE => [0, 1, 2, 3, 3, 2, 1, 0];
Subroutine BEGIN: called CC=1 SCAR=0.0
395  
396 10 ▶use constant TRIANGLE => [
Subroutine BEGIN: called CC=1 SCAR=0.0
397 6, 0, 1, 2, 2, 1, 0, 6,
398 0, 7, 3, 4, 4, 3, 7, 0,
399 1, 3, 8, 5, 5, 8, 3, 1,
400 2, 4, 5, 9, 9, 5, 4, 2,
401 2, 4, 5, 9, 9, 5, 4, 2,
402 1, 3, 8, 5, 5, 8, 3, 1,
403 0, 7, 3, 4, 4, 3, 7, 0,
404 6, 0, 1, 2, 2, 1, 0, 6,
405 10 ];
406  
407 10 ▶use constant LOWER => [
Subroutine BEGIN: called CC=1 SCAR=0.0
408 28, 0, 1, 2, 3, 4, 5, 6,
409 0, 29, 7, 8, 9, 10, 11, 12,
410 1, 7, 30, 13, 14, 15, 16, 17,
411 2, 8, 13, 31, 18, 19, 20, 21,
412 3, 9, 14, 18, 32, 22, 23, 24,
413 4, 10, 15, 19, 22, 33, 25, 26,
414 5, 11, 16, 20, 23, 25, 34, 27,
415 6, 12, 17, 21, 24, 26, 27, 35,
416 10 ];
417  
418 10 ▶use constant DIAG => [
Subroutine BEGIN: called CC=1 SCAR=0.0
419 0, 0, 0, 0, 0, 0, 0, 8,
420 0, 1, 0, 0, 0, 0, 9, 0,
421 0, 0, 2, 0, 0, 10, 0, 0,
422 0, 0, 0, 3, 11, 0, 0, 0,
423 0, 0, 0, 12, 4, 0, 0, 0,
424 0, 0, 13, 0, 0, 5, 0, 0,
425 0, 14, 0, 0, 0, 0, 6, 0,
426 15, 0, 0, 0, 0, 0, 0, 7,
427 10 ];
428  
429 10 ▶use constant KK_IDX => [[
Subroutine BEGIN: called CC=1 SCAR=0.0
430 -1, -1, -1, 0, 1, 2, 3, 4,
431 -1, -1, -1, 5, 6, 7, 8, 9,
432 10, 11, 12, 13, 14, 15, 16, 17,
433 18, 19, 20, 21, 22, 23, 24, 25,
434 26, 27, 28, 29, 30, 31, 32, 33,
435 34, 35, 36, 37, 38, 39, 40, 41,
436 42, 43, 44, 45, 46, 47, 48, 49,
437 50, 51, 52, 53, 54, 55, 56, 57,
438 ], [
439 58, -1, -1, -1, 59, 60, 61, 62,
440 63, -1, -1, -1, 64, 65, 66, 67,
441 68, 69, 70, 71, 72, 73, 74, 75,
442 76, 77, 78, 79, 80, 81, 82, 83,
443 84, 85, 86, 87, 88, 89, 90, 91,
444 92, 93, 94, 95, 96, 97, 98, 99,
445 100, 101, 102, 103, 104, 105, 106, 107,
446 108, 109, 110, 111, 112, 113, 114, 115,
447 ], [
448 116, 117, -1, -1, -1, 118, 119, 120,
449 121, 122, -1, -1, -1, 123, 124, 125,
450 126, 127, 128, 129, 130, 131, 132, 133,
451 134, 135, 136, 137, 138, 139, 140, 141,
452 142, 143, 144, 145, 146, 147, 148, 149,
453 150, 151, 152, 153, 154, 155, 156, 157,
454 158, 159, 160, 161, 162, 163, 164, 165,
455 166, 167, 168, 169, 170, 171, 172, 173,
456 ], [
457 174, -1, -1, -1, 175, 176, 177, 178,
458 179, -1, -1, -1, 180, 181, 182, 183,
459 184, -1, -1, -1, 185, 186, 187, 188,
460 189, 190, 191, 192, 193, 194, 195, 196,
461 197, 198, 199, 200, 201, 202, 203, 204,
462 205, 206, 207, 208, 209, 210, 211, 212,
463 213, 214, 215, 216, 217, 218, 219, 220,
464 221, 222, 223, 224, 225, 226, 227, 228,
465 ], [
466 229, 230, -1, -1, -1, 231, 232, 233,
467 234, 235, -1, -1, -1, 236, 237, 238,
468 239, 240, -1, -1, -1, 241, 242, 243,
469 244, 245, 246, 247, 248, 249, 250, 251,
470 252, 253, 254, 255, 256, 257, 258, 259,
471 260, 261, 262, 263, 264, 265, 266, 267,
472 268, 269, 270, 271, 272, 273, 274, 275,
473 276, 277, 278, 279, 280, 281, 282, 283,
474 ], [
475 284, 285, 286, 287, 288, 289, 290, 291,
476 292, 293, -1, -1, -1, 294, 295, 296,
477 297, 298, -1, -1, -1, 299, 300, 301,
478 302, 303, -1, -1, -1, 304, 305, 306,
479 307, 308, 309, 310, 311, 312, 313, 314,
480 315, 316, 317, 318, 319, 320, 321, 322,
481 323, 324, 325, 326, 327, 328, 329, 330,
482 331, 332, 333, 334, 335, 336, 337, 338,
483 ], [
484 -1, -1, 339, 340, 341, 342, 343, 344,
485 -1, -1, 345, 346, 347, 348, 349, 350,
486 -1, -1, 441, 351, 352, 353, 354, 355,
487 -1, -1, -1, 442, 356, 357, 358, 359,
488 -1, -1, -1, -1, 443, 360, 361, 362,
489 -1, -1, -1, -1, -1, 444, 363, 364,
490 -1, -1, -1, -1, -1, -1, 445, 365,
491 -1, -1, -1, -1, -1, -1, -1, 446,
492 ], [
493 -1, -1, -1, 366, 367, 368, 369, 370,
494 -1, -1, -1, 371, 372, 373, 374, 375,
495 -1, -1, -1, 376, 377, 378, 379, 380,
496 -1, -1, -1, 447, 381, 382, 383, 384,
497 -1, -1, -1, -1, 448, 385, 386, 387,
498 -1, -1, -1, -1, -1, 449, 388, 389,
499 -1, -1, -1, -1, -1, -1, 450, 390,
500 -1, -1, -1, -1, -1, -1, -1, 451,
501 ], [
502 452, 391, 392, 393, 394, 395, 396, 397,
503 -1, -1, -1, -1, 398, 399, 400, 401,
504 -1, -1, -1, -1, 402, 403, 404, 405,
505 -1, -1, -1, -1, 406, 407, 408, 409,
506 -1, -1, -1, -1, 453, 410, 411, 412,
507 -1, -1, -1, -1, -1, 454, 413, 414,
508 -1, -1, -1, -1, -1, -1, 455, 415,
509 -1, -1, -1, -1, -1, -1, -1, 456,
510 ], [
511 457, 416, 417, 418, 419, 420, 421, 422,
512 -1, 458, 423, 424, 425, 426, 427, 428,
513 -1, -1, -1, -1, -1, 429, 430, 431,
514 -1, -1, -1, -1, -1, 432, 433, 434,
515 -1, -1, -1, -1, -1, 435, 436, 437,
516 -1, -1, -1, -1, -1, 459, 438, 439,
517 -1, -1, -1, -1, -1, -1, 460, 440,
518 -1, -1, -1, -1, -1, -1, -1, 461,
519 10 ]];
520  
521 10 ▶use constant UINT64_BE => 'Q>'; # Unsigned 64-bit big-endian
Subroutine BEGIN: called CC=1 SCAR=0.0
522 10 ▶use constant UINT32 => 'V'; # Unsigned 32-bit little-endian
Subroutine BEGIN: called CC=1 SCAR=0.0
523 10 ▶use constant UINT32_BE => 'N'; # Unsigned 32-bit big-endian
Subroutine BEGIN: called CC=1 SCAR=0.0
524 10 ▶use constant UINT16 => 'v'; # Unsigned 16-bit little-endian
Subroutine BEGIN: called CC=1 SCAR=0.0
525  
526 10 ▶use constant PD_INDEXTABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
527 10 ▶use constant PD_SIZETABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
528 10 ▶use constant PD_DATA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
529 10 ▶use constant PD_OFFSET => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
530 10 ▶use constant PD_SYMLEN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
531 10 ▶use constant PD_SYMPAT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
532 10 ▶use constant PD_BLOCKSIZE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
533 10 ▶use constant PD_IDXBITS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
534 10 ▶use constant PD_MIN_LEN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
535 10 ▶use constant PD_BASE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
536  
537 111 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
538 111 my ($class, $path) = @_;
539  
540 111 my $self = {};
541  
542 111 $self->{path} = $path;
543  
544 # normalise tablename
545 111 my ($basename) = $path =~ m{([^/]+)$};
546 111 $basename =~ s/\.[^.]+$//;
547  
548 111 $self->{key} = $normalise_tablename->($basename);
549 111 $self->{mirrored_key} = $normalise_tablename->($basename, 1);
550 111 $self->{symmetric} = $self->{key} eq $self->{mirrored_key};
551  
552 111 $self->{num} = length($basename) - 1;
553  
554 111 $self->{has_pawns} = ($basename =~ /P/);
555  
556 # FIXME! That is taken from python-chess. But the variable names should
557 # be swapped.
558 111 my ($black_part, $white_part) = split /v/, $basename;
559  
560 111 ▶ if ($self->{has_pawns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'has_pawns'}) { } 39 72 111
561 $self->{pawns} = [
562 39 $white_part =~ tr/P/P/,
563 $black_part =~ tr/P/P/,
564 ];
565  
566 39 ▶ if ($self->{pawns}->[1] > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pawns"}[1] > 0 and $self->{"pawns"}[0] == 0 || $self->{"pawns"}[1] < $self->{"pawns"}[0]) 22 17 39
Condition83%5/6
$self->{"pawns"}[1] > 0 and $self->{"pawns"}[0] == 0 || $self->{"pawns"}[1] < $self->{"pawns"}[0]
!ll&&!rl&&r
12522
$self->{"pawns"}[0] == 0 or $self->{"pawns"}[1] < $self->{"pawns"}[0]
l!l&&r!l&&!r
2205
MC/DC66%2/3
$self->{"pawns"}[1] > 0 and $self->{"pawns"}[0] == 0 || $self->{"pawns"}[1] < $self->{"pawns"}[0]
$self->{"pawns"}[1] > 0 $self->{"pawns"}[0] == 0 $self->{"pawns"}[1] < $self->{"pawns"}[0]
Truth table75%3/4
A and B or C
A= $self->{"pawns"}[1] > 0B= $self->{"pawns"}[0] == 0C= $self->{"pawns"}[1] < $self->{"pawns"}[0]
ABCresult
0XX0
1000
11X1
1011
567 && ($self->{pawns}->[0] == 0 || $self->{pawns}->[1] < $self->{pawns}->[0]))
568 {
569 ($self->{pawns}->[0], $self->{pawns}->[1]) =
570 22 ($self->{pawns}->[1], $self->{pawns}->[0]);
571 }
572 } else {
573 72 my $j = 0;
574 map {
575 72 ▶ ++$j if 1 == (() = $white_part =~ /$_/g);
Branch: 1 branch
BranchTrueFalseTotal
if 1 == (() = $white_part =~ /$_/g) 96 336 432
576 432 ▶ ++$j if 1 == (() = $black_part =~ /$_/g);
Branch: 1 branch
BranchTrueFalseTotal
if 1 == (() = $black_part =~ /$_/g) 155 277 432
577 } @PCHR;
578  
579 72 ▶ if ($j >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 3) { } 59 13 72
580 59 $self->{enc_type} = 0;
581 } else {
582 13 $self->{enc_type} = 2;
583 }
584 }
585  
586 111 bless $self, $class;
587 }
588  
589 4261 ▶sub _initMmap {
Subroutine _initMmap: called CC=6 SCAR=18.5
590 4261 my ($self) = @_;
591  
592 4261 ▶ return if defined $self->{data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"data"} 4150 111 4261
593  
594 # Open the file.
595 sysopen(my $fh, $self->{path}, O_RDONLY | ($^O eq 'MSWin32' ? O_BINARY : 0))
596 or die __x("Cannot open '{path}': {error}\n",
597 111 ▶ path => $self->{path}, error => $@);
Branch: 2 branches
BranchTrueFalseTotal
sysopen my $fh, $self->{"path"}, 0 | ($^O eq "MSWin32" ? 0 : 0) or die &__x("Cannot open '{path}': {error}\n", "path", $self->{"path"}, "error", $@) 111 0 111
$^O eq 'MSWin32' ? : 0 111 111
598  
599 # Get the file size.
600 111 my $size = -s $fh;
601  
602 # Validate the file size.
603 die __x("Invalid file size: Ensure '{path}' is a valid syzygy tablebase.\n",
604 path => $self->{path})
605 111 ▶ if $size % 64 != 16;
Branch: 1 branch
BranchTrueFalseTotal
if $size % 64 != 16 0 111 111
606  
607 # Make sure that the file is not closed.
608 111 $self->{data_fh} = $fh;
609  
610 # Memory-map the file.
611 111 my $data;
612 mmap($data, $size, PROT_READ, MAP_SHARED, $fh)
613 or die __x("Cannot mmap '{path}': {error}\n",
614 111 ▶ path => $self->{path}, error => $@);
Branch: 1 branch
BranchTrueFalseTotal
&mmap($data, $size, scalar PROT_READ(), scalar MAP_SHARED(), $fh) or die &__x("Cannot mmap '{path}': {error}\n", "path", $self->{"path"}, "error", $@) 111 0 111
615  
616 # Only store the reference. Storing the scalar will make Perl read the
617 # entire file once.
618 111 $self->{data} = \$data;
619 }
620  
621 111 ▶sub _checkMagic {
Subroutine _checkMagic: called CC=5 SCAR=16.6
622 111 my ($self, $magic) = @_;
623  
624 111 my @valid_magics = ($magic); # Use list so that we can theoretically expand.
625  
626 111 my $header = substr(${$self->{data}}, 0, 4);
627  
628 111 my $ok = 0;
629 111 for my $m (@valid_magics) {
630 111 ▶ next unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 111 111
631 111 ▶ if ($header eq $m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header eq $m) 111 0 111
632 111 $ok = 1;
633 111 last;
634 }
635 }
636  
637 111 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) 0 111 111
638 die __x("Invalid magic header! Ensure that '{path}' is a valid syzygy tablebase file.\n",
639 0 path => $self->{path});
640 }
641 }
642  
643 362 ▶sub _setupPairs {
Subroutine _setupPairs: called CC=7 SCAR=19.5
644 362 my ($self, $data_ptr, $tb_size, $size_idx, $wdl) = @_;
645  
646 362 my $d = Chess::Plisco::Tablebase::Syzygy::PairsData->new;
647  
648 362 my $data = $self->{data};
649 362 $self->{_flags} = $read_byte->($data, $data_ptr);
650  
651 362 my $sizes = $self->{size};
652 362 ▶ if ($self->{_flags} & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_flags"} & 128) 44 318 362
653 44 $d->[PD_IDXBITS] = 0;
654  
655 44 ▶ if ($wdl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wdl) { } 44 0 44
656 44 $d->[PD_MIN_LEN] = $read_byte->($data, $data_ptr + 1);
657 } else {
658 # http://www.talkchess.com/forum/viewtopic.php?p=698093#698093
659 0 $d->[PD_MIN_LEN] = 0;
660 }
661  
662 44 $self->{_next} = $data_ptr + 2;
663 44 $sizes->[$size_idx + 0] = 0;
664 44 $sizes->[$size_idx + 1] = 0;
665 44 $sizes->[$size_idx + 2] = 0;
666  
667 44 return $d;
668 }
669  
670 318 $d->[PD_BLOCKSIZE] = $read_byte->($data, $data_ptr + 1);
671 318 $d->[PD_IDXBITS] = $read_byte->($data, $data_ptr + 2);
672  
673 318 my $real_num_blocks = $self->_readUint32($data_ptr + 4);
674 318 my $num_blocks = $real_num_blocks + $read_byte->($data, $data_ptr + 3);
675 318 my $max_len = $read_byte->($data, $data_ptr + 8);
676 318 my $min_len = $read_byte->($data, $data_ptr + 9);
677 318 my $h = $max_len - $min_len + 1;
678 318 my $num_syms = $self->_readUint16($data_ptr + 10 + 2 * $h);
679  
680 318 ${d}->[PD_OFFSET] = $data_ptr + 10;
681 318 ${d}->[PD_SYMLEN] = [(0) x ($h * 8 + $num_syms - 1)];
682 318 ${d}->[PD_SYMPAT] = $data_ptr + 12 + 2 * $h;
683 318 ${d}->[PD_MIN_LEN] = $min_len;
684  
685 318 $self->{_next} = $data_ptr + 12 + 2 * $h + 3 * $num_syms + ($num_syms & 1);
686  
687 318 my $num_indices = ($tb_size + (1 << $d->[PD_IDXBITS]) - 1) >> $d->[PD_IDXBITS];
688 318 $sizes->[$size_idx + 0] = 6 * $num_indices;
689 318 $sizes->[$size_idx + 1] = 2 * $num_blocks;
690 318 $sizes->[$size_idx + 2] = (1 << $d->[PD_BLOCKSIZE]) * $real_num_blocks;
691  
692 318 my @tmp = ((0) x ($num_syms - 1));
693 318 for my $i (0 .. $num_syms - 1) {
694 196471 ▶ if (!$tmp[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp[$i]) 134843 61628 196471
695 134843 $self->__calcSymlen($d, $i, \@tmp)
696 }
697 }
698  
699 318 my $base = $d->[PD_BASE] = [(0) x $h];
700 318 $base->[$h - 1] = 0;
701  
702 318 my $offset = $d->[PD_OFFSET];
703 318 for my $i (reverse 0 .. $h - 2) {
704 3158 $base->[$i] = uint64(($base->[$i + 1]
705 + $self->_readUint16($offset + $i * 2)
706 - $self->_readUint16($offset + $i * 2 + 2)) / 2);
707 }
708  
709 318 for my $i (0 .. $h) {
710 3794 $base->[$i] <<= 64 - ($min_len + $i);
711 }
712  
713 318 $d->[PD_OFFSET] -= 2 * $d->[PD_MIN_LEN];
714  
715 318 return $d;
716 }
717  
718 122 ▶sub _setNormPiece {
Subroutine _setNormPiece: called CC=5 SCAR=16.1
719 122 my ($self, $norm, $pieces) = @_;
720  
721 122 ▶ if ($self->{enc_type} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'enc_type'} == 0) { } 100 22 122
722 100 $norm->[0] = 3;
723 } else {
724 22 $norm->[0] = 2;
725 }
726  
727 122 my $i = $norm->[0];
728 122 ▶ while ($i < $self->{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"}) 101 122 223
729 101 my $j = $i;
730 101 ▶ while ($j < $self->{num} and $pieces->[$j] == $pieces->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $self->{"num"} and $pieces->[$j] == $pieces->[$i]) 123 101 224
Condition66%2/3
$j < $self->{"num"} and $pieces->[$j] == $pieces->[$i]
!ll&&!rl&&r
1010123
MC/DC50%1/2
$j < $self->{"num"} and $pieces->[$j] == $pieces->[$i]
$j < $self->{"num"} $pieces->[$j] == $pieces->[$i]
Truth table66%2/3
A and B
A= $j < $self->{"num"}B= $pieces->[$j] == $pieces->[$i]
ABresult
0X0
100
111
731 123 ++$norm->[$i];
732 123 ++$j;
733 }
734 101 $i += $norm->[$i];
735 }
736 }
737  
738 122 ▶sub _calcFactorsPiece {
Subroutine _calcFactorsPiece: called CC=5 SCAR=16.1
739 122 my ($self, $factor, $order, $norm) = @_;
740  
741 122 my @PIVFAC = (31332, 28056, 462);
742  
743 122 my $n = 64 - $norm->[0];
744  
745 122 my $f = 1;
746 122 my $i = $norm->[0];
747 122 my $k = 0;
748  
749 122 ▶ while ($i < $self->{num} || $k == $order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"} or $k == $order) 223 122 345
Condition100%3/3
$i < $self->{"num"} or $k == $order
l!l&&r!l&&!r
119104122
MC/DC100%2/2
$i < $self->{"num"} or $k == $order
$i < $self->{"num"} $k == $order
Truth table100%3/3
A or B
A= $i < $self->{"num"}B= $k == $order
ABresult
1X1
011
000
750 223 ▶ if ($k == $order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == $order) { } 122 101 223
751 122 $factor->[0] = $f;
752 122 ▶ if ($self->{enc_type} < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'enc_type'} < 4) { } 122 0 122
753 122 $f *= $PIVFAC[$self->{enc_type}];
754 } else {
755 0 $f *= $MFACTOR[$self->{enc_type} - 2];
756 }
757 } else {
758 101 $factor->[$i] = $f;
759 101 $f *= $subfactor->($norm->[$i], $n);
760 101 $n -= $norm->[$i];
761 101 $i += $norm->[$i];
762 }
763 223 ++$k;
764 }
765  
766 122 return $f;
767 }
768  
769 256 ▶sub _calcFactorsPawn {
Subroutine _calcFactorsPawn: called CC=7 SCAR=19.5
770 256 my ($self, $factor, $order, $order2, $norm, $f) = @_;
771  
772 256 my $i = $norm->[0];
773 256 ▶ if ($order2 < 0x0f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order2 < 15) 32 224 256
774 32 $i += $norm->[$i];
775 }
776 256 my $n = 64 - $i;
777  
778 256 my $fac = 1;
779 256 my $k = 0;
780  
781 256 ▶ while ($i < $self->{num} || $k == $order || $k == $order2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"} or $k == $order or $k == $order2) 980 256 1236
Condition100%6/6
$i < $self->{"num"} or $k == $order or $k == $order2
l!l&&r!l&&!r
96416256
$i < $self->{"num"} or $k == $order
l!l&&r!l&&!r
810154272
MC/DC100%3/3
$i < $self->{"num"} or $k == $order or $k == $order2
$i < $self->{"num"} $k == $order $k == $order2
Truth table100%4/4
A or B or C
A= $i < $self->{"num"}B= $k == $orderC= $k == $order2
ABCresult
1XX1
01X1
0011
0000
782 980 ▶ if ($k == $order) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k == $order) { } 256 724 980
elsif ($k == $order2) { } 32 692 724
783 256 $factor->[0] = $fac;
784 256 $fac *= $PFACTOR[$norm->[0] - 1]->[$f];
785 } elsif ($k == $order2) {
786 32 $factor->[$norm->[0]] = $fac;
787 32 $fac *= $subfactor->($norm->[$norm->[0]], 48 - $norm->[0]);
788 } else {
789 692 $factor->[$i] = $fac;
790 692 $fac *= $subfactor->($norm->[$i], $n);
791 692 $n -= $norm->[$i];
792 692 $i += $norm->[$i];
793 }
794  
795 980 ++$k;
796 }
797  
798 256 return $fac;
799 }
800  
801 256 ▶sub _setNormPawn {
Subroutine _setNormPawn: called CC=5 SCAR=16.1
802 256 my ($self, $norm, $pieces) = @_;
803  
804 256 $norm->[0] = $self->{pawns}->[0];
805  
806 256 ▶ if ($self->{pawns}->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pawns"}[1]) 32 224 256
807 32 $norm->[$self->{pawns}->[0]] = $self->{pawns}->[1];
808 }
809  
810 256 my $i = $self->{pawns}->[0] + $self->{pawns}->[1];
811 256 ▶ while ($i < $self->{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"}) 692 256 948
812 692 my $j = $i;
813 692 ▶ while ($j < $self->{num} && $pieces->[$j] == $pieces->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $self->{"num"} and $pieces->[$j] == $pieces->[$i]) 692 692 1384
Condition100%3/3
$j < $self->{"num"} and $pieces->[$j] == $pieces->[$i]
!ll&&!rl&&r
256436692
MC/DC100%2/2
$j < $self->{"num"} and $pieces->[$j] == $pieces->[$i]
$j < $self->{"num"} $pieces->[$j] == $pieces->[$i]
Truth table100%3/3
A and B
A= $j < $self->{"num"}B= $pieces->[$j] == $pieces->[$i]
ABresult
0X0
100
111
814 692 ++$norm->[$i];
815 692 ++$j;
816 }
817 692 $i += $norm->[$i];
818 }
819 }
820  
821 196471 ▶sub __calcSymlen {
Subroutine __calcSymlen: called CC=4 SCAR=13.9
822 196471 my ($self, $d, $s, $tmp) = @_;
823  
824 196471 my $w = $d->[PD_SYMPAT] + 3 * $s;
825  
826 196471 my $data = $self->{data};
827 196471 my $s2 = ($read_byte->($data, $w + 2) << 4) | ($read_byte->($data, $w + 1) >> 4);
828  
829 196471 my $symlen = $d->[PD_SYMLEN];
830 196471 ▶ if ($s2 == 0x0fff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s2 == 4095) { } 1387 195084 196471
831 1387 $symlen->[$s] = 0;
832 } else {
833 195084 my $s1 = (($read_byte->($data, $w + 1) & 0xf) << 8) | $read_byte->($data, $w);
834 195084 ▶ if (!$tmp->[$s1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp->[$s1]) 34647 160437 195084
835 34647 $self->__calcSymlen($d, $s1, $tmp);
836 }
837 195084 ▶ if (!$tmp->[$s2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp->[$s2]) 26981 168103 195084
838 26981 $self->__calcSymlen($d, $s2, $tmp);
839 }
840  
841 195084 $symlen->[$s] = $symlen->[$s1] + $symlen->[$s2] + 1;
842 }
843  
844 196471 $tmp->[$s] = 1;
845 }
846  
847 356 ▶sub _pawnFile {
Subroutine _pawnFile: called CC=3 SCAR=11.7
848 356 my ($self, $shifts) = @_;
849  
850 356 foreach my $i (1 .. $self->{pawns}->[0] - 1) {
851 4 ▶ if (FLAP->[$shifts->[0]] > FLAP->[$shifts->[$i]]) {
Branch: 1 branch
BranchTrueFalseTotal
if ([0,0,0,0,0,0,0,0,0,6,12,18,18,12,6,0,1,7,13,19,19,13,7,1,2,8,14,20,20,14,8,2,3,9,15,21,21,15,9,3,4,10,16,22,22,16,10,4,5,11,17,23,23,17,11,5,0,0,0,0,0,0,0,0]->[$shifts->[0]] > [0,0,0,0,0,0,0,0,0,6,12,18,18,12,6,0,1,7,13,19,19,13,7,1,2,8,14,20,20,14,8,2,3,9,15,21,21,15,9,3,4,10,16,22,22,16,10,4,5,11,17,23,23,17,11,5,0,0,0,0,0,0,0,0]->[$shifts->[$i]]) 0 4 4
852 0 ($shifts->[0], $shifts->[$i]) = ($shifts->[$i], $shifts->[0]);
853 }
854 }
855  
856 356 return FILE_TO_FILE->[$shifts->[0] & 0x07];
857 }
858  
859 3766 ▶sub _encodePiece {
Subroutine _encodePiece: called CC=21 SCAR=30.4
860 3766 my ($self, $norm, $shifts, $factor) = @_;
861  
862 3766 my $n = $self->{num};
863  
864 3766 ▶ if ($shifts->[0] & 0x04) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shifts->[0] & 4) 700 3066 3766
865 700 foreach my $i (0 .. $n - 1) {
866 2711 $shifts->[$i] ^= 0x07;
867 }
868 }
869  
870 3766 ▶ if ($shifts->[0] & 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shifts->[0] & 32) 2968 798 3766
871 2968 foreach my $i (0 .. $n - 1) {
872 11404 $shifts->[$i] ^= 0x38;
873 }
874 }
875  
876 3766 my $i;
877 3766 foreach (0 .. $n - 1) {
878 5205 $i = $_;
879 5205 ▶ if ($offdiag->($shifts->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$offdiag($shifts->[$i])) 3766 1439 5205
880 3766 last;
881 }
882 }
883  
884 3766 ▶ my $limit = $self->{enc_type} == 0 ? 3 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'enc_type'} == 0 ? : 3634 132 3766
885 3766 ▶ if ($i < $limit && $offdiag->($shifts->[$i]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $limit and &$offdiag($shifts->[$i]) > 0) 1233 2533 3766
Condition100%3/3
$i < $limit and &$offdiag($shifts->[$i]) > 0
!ll&&!rl&&r
425291233
MC/DC100%2/2
$i < $limit and &$offdiag($shifts->[$i]) > 0
$i < $limit &$offdiag($shifts->[$i]) > 0
Truth table100%3/3
A and B
A= $i < $limitB= &$offdiag($shifts->[$i]) > 0
ABresult
0X0
100
111
886 1233 foreach my $i (0 .. $n - 1) {
887 4806 $shifts->[$i] = $flipdiag->($shifts->[$i]);
888 }
889 }
890  
891 3766 my $idx;
892 3766 ▶ if ($self->{enc_type} == 0) { # 111
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'enc_type'} == 0) { } 3634 132 3766
893 3634 $i = $shifts->[1] > $shifts->[0];
894 3634 my $j = ($shifts->[2] > $shifts->[0]) + ($shifts->[2] > $shifts->[1]);
895  
896 3634 ▶ if ($offdiag->($shifts->[0])) {
Branch: 3 branches
BranchTrueFalseTotal
if (&$offdiag($shifts->[0])) { } 2371 1263 3634
elsif (&$offdiag($shifts->[1])) { } 1148 115 1263
elsif (&$offdiag($shifts->[2])) { } 113 2 115
897 2371 $idx = TRIANGLE->[$shifts->[0]] * 63 * 62 + ($shifts->[1] - $i) * 62 + ($shifts->[2] - $j);
898 } elsif ($offdiag->($shifts->[1])) {
899 1148 $idx = 6 * 63 * 62 + DIAG->[$shifts->[0]] * 28 * 62 + LOWER->[$shifts->[1]] * 62 + $shifts->[2] - $j;
900 } elsif ($offdiag->($shifts->[2])) {
901 113 $idx = 6 * 63 * 62 + 4 * 28 * 62 + (DIAG->[$shifts->[0]]) * 7 * 28 + (DIAG->[$shifts->[1]] - $i) * 28 + LOWER->[$shifts->[2]];
902 } else {
903 2 $idx = 6 * 63 * 62 + 4 * 28 * 62 + 4 * 7 * 28 + (DIAG->[$shifts->[0]] * 7 * 6) + (DIAG->[$shifts->[1]] - $i) * 6 + (DIAG->[$shifts->[2]] - $j);
904 }
905 3634 $i = 3;
906 } else { # K2
907 132 $idx = KK_IDX->[TRIANGLE->[$shifts->[0]]]->[$shifts->[1]];
908 132 $i = 2;
909 }
910  
911 3766 $idx *= $factor->[0];
912  
913 3766 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 3197 3766 6963
914 3197 my $t = $norm->[$i];
915  
916 3197 foreach my $j ($i .. $i + $t - 1) {
917 3329 foreach my $k ($j + 1 .. $i + $t - 1) {
918 # Swap.
919 132 ▶ if ($shifts->[$j] > $shifts->[$k]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shifts->[$j] > $shifts->[$k]) 76 56 132
920 76 ($shifts->[$j], $shifts->[$k]) = ($shifts->[$k], $shifts->[$j]);
921 }
922 }
923 }
924  
925 3197 my $s = 0;
926  
927 3197 foreach my $m ($i .. $i + $t - 1) {
928 3329 my $p = $shifts->[$m];
929 3329 my $j = 0;
930 3329 foreach my $l (0 .. $i - 1) {
931 9723 $j += $p > $shifts->[$l];
932 }
933  
934 3329 $s += $binom->($p - $j, $m - $i + 1);
935 }
936  
937 3197 $idx += $s * $factor->[$i];
938 3197 $i += $t;
939 }
940  
941 3766 return $idx;
942 }
943  
944 337 ▶sub _encodePawn {
Subroutine _encodePawn: called CC=19 SCAR=31.0
945 337 my ($self, $norm, $shifts, $factor) = @_;
946  
947 337 my $n = $self->{num};
948  
949 337 ▶ if ($shifts->[0] & 0x04) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shifts->[0] & 4) 222 115 337
950 222 foreach my $i (0 .. $n - 1) {
951 863 $shifts->[$i] ^= 0x07;
952 }
953 }
954  
955 337 foreach my $i (1 .. $self->{pawns}->[0] - 1) {
956 4 foreach my $j ($i + 1 .. $self->{pawns}->[0] - 1) {
957 0 ▶ if ($PTWIST[$shifts->[$i]] < $PTWIST[$shifts->[$j]]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PTWIST[$shifts->[$i]] < $PTWIST[$shifts->[$j]]) 0 0 0
958 0 ($shifts->[$i], $shifts->[$j]) = ($shifts->[$j], $shifts->[$i]);
959 }
960 }
961 }
962  
963 337 my $t = $self->{pawns}->[0] - 1;
964 337 my $idx = $PAWNIDX[$t]->[FLAP->[$shifts->[0]]];
965 337 foreach my $i (reverse 1 .. $t) {
966 4 $idx += $binom->($PTWIST[$shifts->[$i]], $t - $i + 1);
967 }
968 337 $idx *= $factor->[0];
969  
970 # Remaining pawns.
971 337 my $i = $self->{pawns}->[0];
972 337 $t = $i + $self->{pawns}->[1];
973 337 ▶ if ($t > $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t > $i) 27 310 337
974 27 foreach my $j ($i .. $t - 1){
975 27 foreach my $k ($j + 1 .. $t - 1) {
976 0 ▶ if ($shifts->[$j] > $shifts->[$k]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shifts->[$j] > $shifts->[$k]) 0 0 0
977 0 ($shifts->[$j], $shifts->[$k]) = ($shifts->[$k], $shifts->[$j]);
978 }
979 }
980 }
981  
982 27 my $s = 0;
983 27 foreach my $m ($i .. $t - 1) {
984 27 my $p = $shifts->[$m];
985 27 my $j = 0;
986  
987 27 foreach my $k (0 .. $i - 1) {
988 27 $j += $p > $shifts->[$k];
989 }
990  
991 27 $s += $binom->($p - $j - 8, $m - $i + 1);
992 }
993  
994 27 $idx += $s * $factor->[$i];
995 27 $i = $t;
996 }
997  
998 337 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 951 337 1288
999 951 $t = $norm->[$i];
1000 951 foreach my $j ($i .. $i + $t - 1) {
1001 951 foreach my $k ($j + 1 .. $i + $t - 1){
1002 0 ▶ if ($shifts->[$j] > $shifts->[$k]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shifts->[$j] > $shifts->[$k]) 0 0 0
1003 0 ($shifts->[$j], $shifts->[$k]) = ($shifts->[$k], $shifts->[$j]);
1004 }
1005 }
1006 }
1007  
1008 951 my $s = 0;
1009 951 foreach my $m ($i .. $i + $t - 1) {
1010 951 my $p = $shifts->[$m];
1011 951 my $j = 0;
1012 951 foreach my $k (0 .. $i - 1) {
1013 1904 $j += $p > $shifts->[$k];
1014 }
1015 951 $s += $binom->($p - $j, $m - $i + 1);
1016 }
1017  
1018 951 $idx += $s * $factor->[$i];
1019 951 $i += $t;
1020 }
1021  
1022 337 return $idx;
1023 }
1024  
1025 4103 ▶sub _decompressPairs {
Subroutine _decompressPairs: called CC=11 SCAR=24.0
1026 4103 my ($self, $d, $idx) = @_;
1027  
1028 4103 ▶ my $idxbits = $d->[PD_IDXBITS] or return $d->[PD_MIN_LEN];
Branch: 1 branch
BranchTrueFalseTotal
my $idxbits = $d->[7] or return $d->[8] 3842 261 4103
1029  
1030 3842 my $mainidx = $idx >> $idxbits;
1031 3842 my $litidx = ($idx & (1 << $idxbits) - 1) - (1 << ($idxbits - 1));
1032 3842 my $indextable = $d->[PD_INDEXTABLE];
1033 3842 my $block = $self->_readUint32($indextable + 6 * $mainidx);
1034  
1035 3842 my $idx_offset = $self->_readUint16($indextable + 6 * $mainidx + 4);
1036 3842 $litidx += $idx_offset;
1037  
1038 3842 my $sizetable = $d->[PD_SIZETABLE];
1039 3842 ▶ if ($litidx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($litidx < 0) { } 1647 2195 3842
1040 1647 ▶ while ($litidx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($litidx < 0) 9870 1647 11517
1041 9870 --$block;
1042 9870 $litidx += $self->_readUint16($sizetable + 2 * $block) + 1;
1043 }
1044 } else {
1045 2195 ▶ while ($litidx > $self->_readUint16($sizetable + 2 * $block)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($litidx > $self->_readUint16($sizetable + 2 * $block)) 8068 2195 10263
1046 8068 $litidx -= $self->_readUint16($sizetable + 2 * $block) + 1;
1047 8068 ++$block;
1048 }
1049 }
1050  
1051 3842 my $ptr = $d->[PD_DATA] + ($block << $d->[PD_BLOCKSIZE]);
1052  
1053 3842 my $m = $d->[PD_MIN_LEN];
1054 3842 my $base_idx = -$m;
1055 3842 my $symlen_idx = 0;
1056  
1057 3842 my $code = $self->_readUint64BE($ptr);
1058  
1059 3842 $ptr += 2 * 4;
1060 3842 my $bitcnt = 0; # Number of empty bits in code
1061 3842 my $sym;
1062  
1063 3842 my $base = $d->[PD_BASE];
1064 3842 my $offset = $d->[PD_OFFSET];
1065 3842 my $symlen = $d->[PD_SYMLEN];
1066 3842 while (1) {
1067 697893 my $l = $m;
1068 697893 ▶ while ($code < $base->[$base_idx + $l]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code < $$base[$base_idx + $l]) 2145614 697893 2843507
1069 2145614 ++$l;
1070 }
1071 697893 $sym = $self->_readUint16($offset + $l * 2);
1072 697893 $sym += ($code - $base->[$base_idx + $l]) >> (64 - $l);
1073 697893 ▶ if ($litidx < $symlen->[$symlen_idx + $sym] + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($litidx < $$symlen[$symlen_idx + $sym] + 1) 3842 694051 697893
1074 3842 last;
1075 }
1076 694051 $litidx -= $symlen->[$symlen_idx + $sym] + 1;
1077 694051 $code <<= $l;
1078 694051 $bitcnt += $l;
1079 694051 ▶ if ($bitcnt >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitcnt >= 32) 177530 516521 694051
1080 177530 $bitcnt -= 32;
1081 177530 $code |= $self->_readUint32BE($ptr) << $bitcnt;
1082 177530 $ptr += 4;
1083 }
1084 }
1085  
1086 3842 my $sympat = $d->[PD_SYMPAT];
1087 3842 my $data = $self->{data};
1088 3842 ▶ while ($symlen->[$symlen_idx + $sym]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$symlen[$symlen_idx + $sym]) 22326 3842 26168
1089 22326 my $w = $sympat + 3 * $sym;
1090 22326 my $s1 = (($read_byte->($data, $w + 1) & 0xf) << 8) | $read_byte->($data, $w);
1091 22326 ▶ if ($litidx < $symlen->[$symlen_idx + $s1] + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($litidx < $$symlen[$symlen_idx + $s1] + 1) { } 11681 10645 22326
1092 11681 $sym = $s1;
1093 } else {
1094 10645 $litidx -= $symlen->[$symlen_idx + $s1] + 1;
1095 10645 $sym = ($read_byte->($data, $w + 2) << 4) | ($read_byte->($data, $w + 1) >> 4);
1096 }
1097 }
1098  
1099 3842 my $w = $sympat + 3 * $sym;
1100 3842 ▶ if ($self->isa('DtzTable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa('DtzTable')) { } 0 3842 3842
1101 0 return (($read_byte->($data, $w + 1) & 0x0f) << 8) | $read_byte->($data, $w);
1102 } else {
1103 3842 return $read_byte->($data, $w);
1104 }
1105 }
1106  
1107 3842 ▶sub _readUint64BE {
Subroutine _readUint64BE: called CC=1 SCAR=0.0
1108 3842 my ($self, $data_ptr) = @_;
1109  
1110 3842 return uint64(unpack(UINT64_BE, substr(${$self->{data}}, $data_ptr, 8)));
1111 }
1112  
1113 4160 ▶sub _readUint32 {
Subroutine _readUint32: called CC=1 SCAR=0.0
1114 4160 my ($self, $data_ptr) = @_;
1115  
1116 4160 return uint64(unpack(UINT32, substr(${$self->{data}}, $data_ptr, 4)));
1117 }
1118  
1119 177530 ▶sub _readUint32BE {
Subroutine _readUint32BE: called CC=1 SCAR=0.0
1120 177530 my ($self, $data_ptr) = @_;
1121  
1122 177530 return unpack(UINT32_BE, substr(${$self->{data}}, $data_ptr, 4));
1123 }
1124  
1125 736570 ▶sub _readUint16 {
Subroutine _readUint16: called CC=1 SCAR=0.0
1126 736570 my ($self, $data_ptr) = @_;
1127  
1128 736570 return unpack(UINT16, substr(${$self->{data}}, $data_ptr, 2));
1129 }
1130  
1131 111 ▶sub close {
Subroutine close: called CC=3 SCAR=11.2
1132 111 my ($self) = @_;
1133  
1134 111 ▶ if (defined $self->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"data"}) 111 0 111
1135 111 delete $self->{data};
1136 }
1137  
1138 111 ▶ if (defined $self->{data_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"data_fh"}) 111 0 111
1139 111 close $self->{data_fh};
1140 }
1141  
1142 111 return;
1143 }
1144  
1145 111 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
1146 111 my ($self) = @_;
1147 111 $self->close;
1148 }
1149  
1150 package Chess::Plisco::Tablebase::Syzygy::WdlTable;
1151 10 $Chess::Plisco::Tablebase::Syzygy::WdlTable::VERSION = 'v1.0.3';
1152 10 ▶use Chess::Plisco qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
1153  
1154 10 ▶use base qw(Chess::Plisco::Tablebase::Syzygy::Table);
Subroutine BEGIN: called CC=1 SCAR=0.0
1155  
1156 10 ▶use constant TBW_MAGIC => "\x71\xe8\x23\x5d";
Subroutine BEGIN: called CC=1 SCAR=0.0
1157  
1158 10 ▶use constant PD_INDEXTABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1159 10 ▶use constant PD_SIZETABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1160 10 ▶use constant PD_DATA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1161 10 ▶use constant PD_OFFSET => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
1162 10 ▶use constant PD_SYMLEN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1163 10 ▶use constant PD_SYMPAT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1164 10 ▶use constant PD_BLOCKSIZE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
1165 10 ▶use constant PD_IDXBITS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
1166 10 ▶use constant PD_MIN_LEN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
1167 10 ▶use constant PD_BASE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
1168  
1169 75 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
1170 75 my ($class, @args) = @_;
1171  
1172 75 my $self = $class->SUPER::new(@args);
1173  
1174 75 $self->{_next} = 0;
1175 75 $self->{_flags} = 0;
1176  
1177 75 return $self;
1178 }
1179  
1180 2798 ▶sub __initTableWdl {
Subroutine __initTableWdl: called CC=17 SCAR=28.3
1181 2798 my ($self) = @_;
1182  
1183 2798 $self->_initMmap;
1184  
1185 2798 ▶ if ($self->{initialized}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"initialized"}) 2723 75 2798
1186 2723 return;
1187 }
1188  
1189 75 $self->_checkMagic(TBW_MAGIC);
1190  
1191 75 $self->{tb_size} = [(0) x 8];
1192 75 $self->{size} = [(0) x (8 * 3)];
1193  
1194 # Used if there are only pieces.
1195 75 $self->{precomp} = [];
1196 75 $self->{pieces} = [];
1197 75 $self->{factor} = [[(0) x $TBPIECES], [(0) x $TBPIECES]];
1198 75 $self->{norm} = [[(0) x $self->{num}], [(0) x $self->{num}]];
1199  
1200 # Used if there are pawns.
1201 $self->{files} = [
1202 75 Chess::Plisco::Tablebase::Syzygy::PawnFileData->new,
1203 Chess::Plisco::Tablebase::Syzygy::PawnFileData->new,
1204 Chess::Plisco::Tablebase::Syzygy::PawnFileData->new,
1205 Chess::Plisco::Tablebase::Syzygy::PawnFileData->new,
1206 ];
1207  
1208 75 my $code = $read_byte->($self->{data}, 4);
1209 75 my $split = $code & 0x01;
1210 75 ▶ my $files = $code & 0x02 ? 4 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$code & 2 ? : 25 50 75
1211  
1212 75 my $data_ptr = 5;
1213 75 ▶ if (!$self->{has_pawns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'has_pawns'}) { } 50 25 75
1214 50 $self->__setupPiecesPiece($data_ptr);
1215  
1216 50 $data_ptr += $self->{num} + 1;
1217 50 $data_ptr += $data_ptr & 0x01;
1218  
1219 50 $self->{precomp}->[0] = $self->_setupPairs($data_ptr, $self->{tb_size}->[0], 0, 1);
1220 50 $data_ptr = $self->{_next};
1221 50 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 46 4 50
1222 46 $self->{precomp}->[1] = $self->_setupPairs($data_ptr, $self->{tb_size}->[1], 3, 1);
1223 46 $data_ptr = $self->{_next};
1224 }
1225  
1226 50 $self->{precomp}->[0]->[PD_INDEXTABLE] = $data_ptr;
1227 50 $data_ptr += $self->{size}->[0];
1228 50 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 46 4 50
1229 46 $self->{precomp}->[1]->[PD_INDEXTABLE] = $data_ptr;
1230 46 $data_ptr += $self->{size}->[3];
1231 }
1232  
1233 50 $self->{precomp}->[0]->[PD_SIZETABLE] = $data_ptr;
1234 50 $data_ptr += $self->{size}->[1];
1235 50 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 46 4 50
1236 46 $self->{precomp}->[1]->[PD_SIZETABLE] = $data_ptr;
1237 46 $data_ptr += $self->{size}->[4];
1238 }
1239  
1240 50 $data_ptr = ($data_ptr + 0x3f) & ~0x3f;
1241 50 $self->{precomp}->[0]->[PD_DATA] = $data_ptr;
1242 50 $data_ptr += $self->{size}->[2];
1243 50 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 46 4 50
1244 46 $data_ptr = ($data_ptr + 0x3f) & ~0x3f;
1245 46 $self->{precomp}->[1]->[PD_DATA] = $data_ptr;
1246 }
1247  
1248 50 $self->{key} = $recalc_key->($self->{pieces}->[0]);
1249 50 $self->{mirrored_key} = $recalc_key->($self->{pieces}->[0], 1);
1250 } else {
1251 25 my $s = 1 + ($self->{pawns}->[1] > 0);
1252 25 foreach my $f (0 .. 3) {
1253 100 $self->__setupPiecesPawn($data_ptr, 2 * $f, $f);
1254 100 $data_ptr += $self->{num} + $s;
1255 }
1256 25 $data_ptr += $data_ptr & 0x01;
1257  
1258 25 foreach my $f (0 .. $files - 1) {
1259 100 $self->{files}->[$f]->{precomp}->[0] = $self->_setupPairs($data_ptr, $self->{tb_size}->[2 * $f], 6 * $f, 1);
1260 100 $data_ptr = $self->{_next};
1261 100 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 88 12 100
1262 88 $self->{files}->[$f]->{precomp}->[1] = $self->_setupPairs($data_ptr, $self->{tb_size}->[2 * $f + 1], 6 * $f + 3, 1);
1263 88 $data_ptr = $self->{_next};
1264 }
1265 }
1266  
1267 25 foreach my $f (0 .. $files - 1) {
1268 100 $self->{files}->[$f]->{precomp}->[0]->[PD_INDEXTABLE] = $data_ptr;
1269 100 $data_ptr += $self->{size}->[6 * $f];
1270 100 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 88 12 100
1271 88 $self->{files}->[$f]->{precomp}->[1]->[PD_INDEXTABLE] = $data_ptr;
1272 88 $data_ptr += $self->{size}->[6 * $f + 3];
1273 }
1274 }
1275  
1276 25 foreach my $f (0 .. $files - 1) {
1277 100 $self->{files}->[$f]->{precomp}->[0]->[PD_SIZETABLE] = $data_ptr;
1278 100 $data_ptr += $self->{size}[6 * $f + 1];
1279 100 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 88 12 100
1280 88 $self->{files}->[$f]->{precomp}->[1]->[PD_SIZETABLE] = $data_ptr;
1281 88 $data_ptr += $self->{size}->[6 * $f + 4];
1282 }
1283 }
1284  
1285 25 foreach my $f (0 .. $files - 1) {
1286 100 $data_ptr = ($data_ptr + 0x3f) & ~0x3f;
1287 100 $self->{files}->[$f]->{precomp}->[0]->[PD_DATA] = $data_ptr;
1288 100 $data_ptr += $self->{size}->[6 * $f + 2];
1289 100 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) 88 12 100
1290 88 $data_ptr = ($data_ptr + 0x3f) & ~0x3f;
1291 88 $self->{files}->[$f]->{precomp}->[1]->[PD_DATA] = $data_ptr;
1292 88 $data_ptr += $self->{size}->[6 * $f + 5];
1293 }
1294 }
1295 }
1296  
1297 75 $self->{initialized} = 1;
1298 }
1299  
1300 100 ▶sub __setupPiecesPawn {
Subroutine __setupPiecesPawn: called CC=5 SCAR=16.1
1301 100 my ($self, $p_data, $p_tb_size, $f) = @_;
1302  
1303 100 my $j = 1 + ($self->{pawns}->[1] > 0);
1304 100 my $order = $read_byte->($self->{data}, $p_data) & 0x0f;
1305 100 ▶ my $order2 = $self->{pawns}->[1] ? $read_byte->($self->{data}, $p_data + 1) & 0x0f : 0x0f;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pawns'}[1] ? : 12 88 100
1306 100 foreach my $i (0 .. $self->{num} - 1) {
1307 384 $self->{files}->[$f]->{pieces}->[0]->[$i] = $read_byte->($self->{data}, $p_data + $i + $j) & 0x0f;
1308 }
1309  
1310 100 $self->{files}->[$f]->{norm}->[0] = [(0) x $self->{num}]; $self->_setNormPawn($self->{files}->[$f]->{norm}->[0], $self->{files}->[$f]->{pieces}->[0]);
1311 100 $self->{files}->[$f]->{factor}->[0] = [(0) x $TBPIECES];
1312 100 $self->{tb_size}->[$p_tb_size] = $self->_calcFactorsPawn($self->{files}->[$f]->{factor}->[0], $order, $order2, $self->{files}->[$f]->{norm}->[0], $f);
1313  
1314 100 $order = $read_byte->($self->{data}, $p_data) >> 4;
1315 100 ▶ $order2 = $self->{pawns}->[1] ? $read_byte->($self->{data}, $p_data + 1) >> 4 : 0x0f;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pawns'}[1] ? : 12 88 100
1316 100 foreach my $i (0 .. $self->{num} - 1) {
1317 384 $self->{files}->[$f]->{pieces}->[1]->[$i] = $read_byte->($self->{data}, $p_data + $i + $j) >> 4;
1318 }
1319  
1320 100 $self->{files}->[$f]->{norm}->[1] = [(0) x ($self->{num} - 1)];
1321 100 $self->_setNormPawn($self->{files}->[$f]->{norm}->[1], $self->{files}->[$f]->{pieces}->[1]);
1322 100 $self->{files}->[$f]->{factor}->[1] = [(0) x $TBPIECES];
1323 100 $self->{tb_size}->[$p_tb_size + 1] = $self->_calcFactorsPawn($self->{files}->[$f]->{factor}->[1], $order, $order2, $self->{files}->[$f]->{norm}->[1], $f);
1324 }
1325  
1326 50 ▶sub __setupPiecesPiece {
Subroutine __setupPiecesPiece: called CC=3 SCAR=11.0
1327 50 my ($self, $p_data) = @_;
1328  
1329 50 foreach my $i (0 .. $self->{num} - 1) {
1330 190 $self->{pieces}->[0]->[$i] = $read_byte->($self->{data}, $p_data + $i + 1) & 0x0f;
1331 }
1332 50 my $order = $read_byte->($self->{data}, $p_data) & 0x0f;
1333 50 $self->_setNormPiece($self->{norm}->[0], $self->{pieces}->[0]);
1334 50 $self->{tb_size}->[0] = $self->_calcFactorsPiece($self->{factor}->[0], $order, $self->{norm}->[0]);
1335  
1336 50 foreach my $i (0 .. $self->{num} - 1) {
1337 190 $self->{pieces}->[1]->[$i] = $read_byte->($self->{data}, $p_data + $i + 1) >> 4;
1338 }
1339 50 $order = $read_byte->($self->{data}, $p_data) >> 4;
1340 50 $self->_setNormPiece($self->{norm}->[1], $self->{pieces}->[1]);
1341 50 $self->{tb_size}->[1] = $self->_calcFactorsPiece($self->{factor}->[1], $order, $self->{norm}->[1]);
1342 }
1343  
1344 2798 ▶sub probeWdlTable {
Subroutine probeWdlTable: called CC=14 SCAR=26.4
1345 2798 my ($self, $pos) = @_;
1346  
1347 2798 $self->__initTableWdl;
1348  
1349 2798 my $key = $calc_key->($pos);
1350  
1351 2798 my ($cmirror, $mirror, $bside);
1352 2798 my $to_move = $pos->[CP_POS_TURN];
1353 2798 ▶ if (!$self->{symmetric}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'symmetric'}) { } 2723 75 2798
1354 2723 ▶ if ($key ne $self->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne $self->{'key'}) { } 2168 555 2723
1355 2168 $cmirror = 8;
1356 2168 $mirror = 0x38;
1357 2168 $bside = $to_move == CP_WHITE;
1358 } else {
1359 555 $cmirror = $mirror = 0;
1360 555 $bside = $to_move != CP_WHITE;
1361 }
1362 } else {
1363 75 ▶ $cmirror = $to_move == CP_WHITE ? 0 : 8;
Branch: 1 branch
BranchTrueFalseTotal
$to_move == 0 ? : 33 42 75
1364 75 ▶ $mirror = $to_move == CP_WHITE ? 0 : 0x38;
Branch: 1 branch
BranchTrueFalseTotal
$to_move == 0 ? : 33 42 75
1365 75 $bside = 0
1366 }
1367  
1368 2798 my $res;
1369 2798 ▶ if (!$self->{has_pawns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'has_pawns'}) { } 2487 311 2798
1370 2487 my $p = [(0) x $TBPIECES];
1371 2487 my $i = 0;
1372 2487 ▶ while ($i < $self->{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"}) 9286 2487 11773
1373 9286 my $piece_type = $self->{pieces}->[$bside]->[$i] & 0x07;
1374 9286 my $colour = ($self->{pieces}->[$bside]->[$i] ^ $cmirror) >> 3;
1375 9286 ▶ my $bb = $colour ? ($pos->[$piece_type] & $pos->[CP_POS_BLACK_PIECES]) : ($pos->[$piece_type] & $pos->[CP_POS_WHITE_PIECES]);
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 4875 4411 9286
1376  
1377 9286 ▶ while ($bb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bb) 9380 9286 18666
1378 9380 my $shift = (do { my $B = $bb & -$bb; my $A = $B - 1 - ((($B - 1) >> 1) & 0x5555_5555_5555_5555); my $C = ($A & 0x3333_3333_3333_3333) + (($A >> 2) & 0x3333_3333_3333_3333); my $n = $C + ($C >> 32); $n = ($n & 0x0f0f0f0f) + (($n >> 4) & 0x0f0f0f0f); $n = ($n & 0xffff) + ($n >> 16); $n = ($n & 0xff) + ($n >> 8);});
1379 9380 $p->[$i++] = $shift;
1380  
1381 9380 $bb = (($bb) & (($bb) - 1));
1382 }
1383 }
1384  
1385 # FIXME! idx is not always used by decompressPairs()!
1386 2487 my $idx = $self->_encodePiece($self->{norm}->[$bside], $p, $self->{factor}->[$bside]);
1387 2487 $res = $self->_decompressPairs($self->{precomp}->[$bside], $idx);
1388 } else {
1389 311 my $p = [(0) x $TBPIECES];
1390 311 my $i = 0;
1391 311 my $k = $self->{files}->[0]->{pieces}->[0]->[0] ^ $cmirror;
1392 311 my $colour = $k >> 3;
1393 311 my $piece_type = $k & 0x07;
1394 311 ▶ my $bb = $colour ? ($pos->[$piece_type] & $pos->[CP_POS_BLACK_PIECES]) : ($pos->[$piece_type] & $pos->[CP_POS_WHITE_PIECES]);
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 195 116 311
1395  
1396 311 ▶ while ($bb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bb) 315 311 626
1397 315 my $shift = (do { my $B = $bb & -$bb; my $A = $B - 1 - ((($B - 1) >> 1) & 0x5555_5555_5555_5555); my $C = ($A & 0x3333_3333_3333_3333) + (($A >> 2) & 0x3333_3333_3333_3333); my $n = $C + ($C >> 32); $n = ($n & 0x0f0f0f0f) + (($n >> 4) & 0x0f0f0f0f); $n = ($n & 0xffff) + ($n >> 16); $n = ($n & 0xff) + ($n >> 8);});
1398 315 $p->[$i++] = $shift ^ $mirror;
1399  
1400 315 $bb = (($bb) & (($bb) - 1));
1401 }
1402  
1403 311 my $f = $self->_pawnFile($p);
1404 311 my $pc = $self->{files}->[$f]->{pieces}->[$bside];
1405  
1406 311 ▶ while ($i < $self->{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"}) 900 311 1211
1407 900 my $colour = ($pc->[$i] ^ $cmirror) >> 3;
1408 900 my $piece_type = $pc->[$i] & 0x07;
1409 900 ▶ my $bb = $colour ? ($pos->[$piece_type] & $pos->[CP_POS_BLACK_PIECES]) : ($pos->[$piece_type] & $pos->[CP_POS_WHITE_PIECES]);
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 448 452 900
1410  
1411 900 ▶ while ($bb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bb) 900 900 1800
1412 900 my $shift = (do { my $B = $bb & -$bb; my $A = $B - 1 - ((($B - 1) >> 1) & 0x5555_5555_5555_5555); my $C = ($A & 0x3333_3333_3333_3333) + (($A >> 2) & 0x3333_3333_3333_3333); my $n = $C + ($C >> 32); $n = ($n & 0x0f0f0f0f) + (($n >> 4) & 0x0f0f0f0f); $n = ($n & 0xffff) + ($n >> 16); $n = ($n & 0xff) + ($n >> 8);});
1413 900 $p->[$i++] = $shift ^ $mirror;
1414  
1415 900 $bb = (($bb) & (($bb) - 1));
1416 }
1417 }
1418  
1419 # FIXME! idx is not always used by decompressPairs()!
1420 311 my $idx = $self->_encodePawn($self->{files}->[$f]->{norm}->[$bside], $p, $self->{files}->[$f]->{factor}->[$bside]);
1421 311 $res = $self->_decompressPairs($self->{files}->[$f]->{precomp}->[$bside], $idx);
1422 }
1423  
1424 2798 return $res - 2;
1425 }
1426  
1427 package Chess::Plisco::Tablebase::Syzygy::DtzTable;
1428 10 $Chess::Plisco::Tablebase::Syzygy::DtzTable::VERSION = 'v1.0.3';
1429 10 ▶use Chess::Plisco qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
1430 # Macros from Chess::Plisco::Macro are already expanded here!
1431  
1432 10 ▶use base qw(Chess::Plisco::Tablebase::Syzygy::Table);
Subroutine BEGIN: called CC=1 SCAR=0.0
1433  
1434 10 ▶use constant TBZ_MAGIC => "\xd7\x66\x0c\xa5";
Subroutine BEGIN: called CC=1 SCAR=0.0
1435  
1436 10 ▶use constant WDL_TO_MAP => [1, 3, 0, 2, 0];
Subroutine BEGIN: called CC=1 SCAR=0.0
1437  
1438 10 ▶use constant PA_FLAGS => [8, 0, 0, 0, 4];
Subroutine BEGIN: called CC=1 SCAR=0.0
1439  
1440 10 ▶use constant PD_INDEXTABLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1441 10 ▶use constant PD_SIZETABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1442 10 ▶use constant PD_DATA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1443 10 ▶use constant PD_OFFSET => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
1444 10 ▶use constant PD_SYMLEN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1445 10 ▶use constant PD_SYMPAT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1446 10 ▶use constant PD_BLOCKSIZE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
1447 10 ▶use constant PD_IDXBITS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
1448 10 ▶use constant PD_MIN_LEN => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
1449 10 ▶use constant PD_BASE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
1450  
1451 1463 ▶sub __initTableDtz {
Subroutine __initTableDtz: called CC=20 SCAR=30.9
1452 1463 my ($self) = @_;
1453  
1454 1463 $self->_initMmap;
1455  
1456 1463 ▶ if ($self->{initialized}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"initialized"}) 1427 36 1463
1457 1427 return;
1458 }
1459  
1460 36 $self->_checkMagic(TBZ_MAGIC);
1461  
1462 36 $self->{factor} = [(0) x $TBPIECES];
1463 36 $self->{norm} = [(0) x $self->{num}];
1464 36 $self->{tb_size} = [0, 0, 0, 0];
1465 36 $self->{size} = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
1466 $self->{files} = [
1467 36 Chess::Plisco::Tablebase::Syzygy::PawnFileDataDtz->new,
1468 Chess::Plisco::Tablebase::Syzygy::PawnFileDataDtz->new,
1469 Chess::Plisco::Tablebase::Syzygy::PawnFileDataDtz->new,
1470 Chess::Plisco::Tablebase::Syzygy::PawnFileDataDtz->new,
1471 ];
1472  
1473  
1474 36 ▶ my $files = ($read_byte->($self->{data}, 4) & 0x2) ? 4 : 1;
Branch: 1 branch
BranchTrueFalseTotal
&$read_byte($self->{'data'}, 4) & 2 ? : 14 22 36
1475  
1476 36 my $p_data = 5;
1477  
1478 36 ▶ if (!$self->{has_pawns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'has_pawns'}) { } 22 14 36
1479 22 $self->{map_idx} = [[0, 0, 0, 0]];
1480  
1481 22 $self->__setupPiecesPieceDtz($p_data, 0);
1482 22 $p_data += $self->{num} + 1;
1483 22 $p_data += $p_data & 0x01;
1484  
1485 22 $self->{precomp} = $self->_setupPairs($p_data, $self->{tb_size}->[0], 0);
1486 22 $self->{flags} = $self->{_flags};
1487  
1488 22 $p_data = $self->{_next};
1489 22 $self->{p_map} = $p_data;
1490 22 ▶ if ($self->{flags} & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flags"} & 2) 2 20 22
1491 2 ▶ if (!($self->{flags} & 16)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'flags'} & 16) { } 2 0 2
1492 2 foreach my $i (0 .. 3) {
1493 8 $self->{map_idx}->[0]->[$i] = $p_data + 1 - $self->{p_map};
1494 8 $p_data += 1 + $read_byte->($self->{data}, $p_data);
1495 }
1496 } else {
1497 0 foreach my $i (0 .. 3) {
1498 0 ▶ $self->{map_idx}->[0]->[$i] = (defined $self->{p_map}) ? ($p_data + 2 - $self->{p_map}) : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'p_map'} ? : 0 0 0
1499 0 $p_data += 2 + 2 * $self->_readUint16($p_data);
1500 }
1501 }
1502 }
1503 22 $p_data += $p_data & 0x01;
1504  
1505 22 $self->{precomp}->[PD_INDEXTABLE] = $p_data;
1506 22 $p_data += $self->{size}->[0];
1507  
1508 22 $self->{precomp}->[PD_SIZETABLE] = $p_data;
1509 22 $p_data += $self->{size}->[1];
1510  
1511 22 $p_data = ($p_data + 0x3f) & ~0x3f;
1512 22 $self->{precomp}->[PD_DATA] = $p_data;
1513 22 $p_data += $self->{size}->[2];
1514  
1515 22 $self->{key} = $recalc_key->($self->{pieces});
1516 22 $self->{mirrored_key} = $recalc_key->($self->{pieces}, 1);
1517 } else {
1518 14 my $s = 1 + ($self->{pawns}->[1] > 0);
1519 14 foreach my $f (0 .. 3) {
1520 56 $self->__setupPiecesPawnDtz($p_data, $f, $f);
1521 56 $p_data += $self->{num} + $s;
1522 }
1523 14 $p_data += $p_data & 0x01;
1524  
1525 14 $self->{flags} = [];
1526 14 foreach my $f (0 .. ($files - 1)) {
1527 56 $self->{files}->[$f]->{precomp} = $self->_setupPairs($p_data, $self->{tb_size}->[$f], 3 * $f);
1528 56 $p_data = $self->{_next};
1529 56 push @{$self->{flags}}, $self->{_flags};
1530 }
1531  
1532 14 $self->{map_idx} = [];
1533 14 $self->{p_map} = $p_data;
1534 14 foreach my $f (0 .. ($files - 1)) {
1535 56 push @{$self->{map_idx}}, [];
1536 56 ▶ if ($self->{flags}->[$f] & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flags"}[$f] & 2) 14 42 56
1537 14 ▶ if (!($self->{flags}->[$f] & 16)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'flags'}[$f] & 16) { } 14 0 14
1538 14 foreach (0 .. 3) {
1539 56 push @{$self->{map_idx}->[-1]}, $p_data + 1 - $self->{p_map};
1540 56 $p_data += 1 + $read_byte->($self->{data}, $p_data);
1541 }
1542 } else {
1543 0 $p_data += $p_data & 0x01;
1544 0 foreach (0 .. 3) {
1545 0 ▶ if (defined $self->{p_map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'p_map'}) { } 0 0 0
1546 0 push @{$self->{map_idx}->[-1]}, $p_data + 2 - $self->{p_map};
1547 } else {
1548 0 push @{$self->{map_idx}->[-1]}, 2;
1549 }
1550 0 $p_data += 2 + 2 * $self->_readUint16($p_data);
1551 }
1552 }
1553 }
1554 }
1555 14 $p_data += $p_data & 0x01;
1556  
1557 14 foreach my $f (0 .. ($files - 1)) {
1558 56 $self->{files}->[$f]->{precomp}->[PD_INDEXTABLE] = $p_data;
1559 56 $p_data += $self->{size}->[3 * $f];
1560 }
1561  
1562 14 foreach my $f (0 .. ($files - 1)) {
1563 56 $self->{files}->[$f]->{precomp}->[PD_SIZETABLE] = $p_data;
1564 56 $p_data += $self->{size}->[3 * $f + 1];
1565 }
1566  
1567 14 foreach my $f (0 .. ($files - 1)) {
1568 56 $p_data = ($p_data + 0x3f) & ~0x3f;
1569 56 $self->{files}->[$f]->{precomp}->[PD_DATA] = $p_data;
1570 56 $p_data += $self->{size}->[3 * $f + 2];
1571 }
1572 }
1573  
1574 36 $self->{initialized} = 1;
1575 }
1576  
1577 22 ▶sub __setupPiecesPieceDtz {
Subroutine __setupPiecesPieceDtz: called CC=2 SCAR=6.9
1578 22 my ($self, $p_data, $p_tb_size) = @_;
1579  
1580 22 foreach my $i (0 .. $self->{num} - 1) {
1581 87 $self->{pieces}->[$i] = $read_byte->($self->{data}, $p_data + $i + 1);
1582 }
1583 22 my $order = $read_byte->($self->{data}, $p_data) & 0x0f;
1584 22 $self->_setNormPiece($self->{norm}, $self->{pieces});
1585 22 $self->{tb_size}->[$p_tb_size] = $self->_calcFactorsPiece($self->{factor}, $order, $self->{norm});
1586 }
1587  
1588 56 ▶sub __setupPiecesPawnDtz {
Subroutine __setupPiecesPawnDtz: called CC=4 SCAR=13.9
1589 56 my ($self, $p_data, $p_tb_size, $f) = @_;
1590  
1591 56 my $j = 1;
1592 56 ▶ ++$j if $self->{pawns}->[1] > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pawns"}[1] > 0 8 48 56
1593 56 my $order = $read_byte->($self->{data}, $p_data) & 0x0f;
1594 56 ▶ my $order2 = $self->{pawns}->[1] ? $read_byte->($self->{data}, $p_data + 1) & 0xf : 0xf;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pawns'}[1] ? : 8 48 56
1595 56 foreach my $i (0 .. $self->{num} - 1) {
1596 220 $self->{files}->[$f]->{pieces}->[$i] = $read_byte->($self->{data}, $p_data + $i + $j);
1597 }
1598  
1599 56 $self->{files}->[$f]->{norm} = [(0) x ($self->{num} - 1)];
1600 56 $self->_setNormPawn($self->{files}->[$f]->{norm}, $self->{files}->[$f]->{pieces});
1601  
1602 56 $self->{files}->[$f]->{factor} = [(0) x ($TBPIECES - 1)];
1603 56 $self->{tb_size}->[$p_tb_size] = $self->_calcFactorsPawn($self->{files}->[$f]->{factor}, $order, $order2, $self->{files}->[$f]->{norm}, $f);
1604 }
1605  
1606 1463 ▶sub probeDtzTable {
Subroutine probeDtzTable: called CC=25 SCAR=32.3
1607 1463 my ($self, $pos, $wdl) = @_;
1608  
1609 1463 $self->__initTableDtz;
1610  
1611 1463 my $key = $calc_key->($pos);
1612  
1613 1463 my ($cmirror, $mirror, $bside);
1614 1463 my $to_move = $pos->[CP_POS_TURN];
1615 1463 ▶ if (!$self->{symmetric}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'symmetric'}) { } 1459 4 1463
1616 1459 ▶ if ($key ne $self->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne $self->{'key'}) { } 1270 189 1459
1617 1270 $cmirror = 8;
1618 1270 $mirror = 0x38;
1619 1270 $bside = $to_move == CP_WHITE;
1620 } else {
1621 189 $cmirror = $mirror = 0;
1622 189 $bside = $to_move != CP_WHITE;
1623 }
1624 } else {
1625 4 ▶ $cmirror = $to_move == CP_WHITE ? 0 : 8;
Branch: 1 branch
BranchTrueFalseTotal
$to_move == 0 ? : 1 3 4
1626 4 ▶ $mirror = $to_move == CP_WHITE ? 0 : 0x38;
Branch: 1 branch
BranchTrueFalseTotal
$to_move == 0 ? : 1 3 4
1627 4 $bside = 0
1628 }
1629  
1630 1463 my $res;
1631 1463 ▶ if (!$self->{has_pawns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'has_pawns'}) { } 1418 45 1463
1632 1418 ▶ if (($self->{flags} & 1) != $bside && !$self->{symmetric}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"flags"} & 1) != $bside and not $self->{"symmetric"}) 139 1279 1418
Condition66%2/3
($self->{"flags"} & 1) != $bside and not $self->{"symmetric"}
!ll&&!rl&&r
12790139
MC/DC50%1/2
($self->{"flags"} & 1) != $bside and not $self->{"symmetric"}
($self->{"flags"} & 1) != $bside not $self->{"symmetric"}
Truth table66%2/3
A and not(B)
A= ($self->{"flags"} & 1) != $bsideB= not $self->{"symmetric"}
ABresult
0X0
100
111
1633 139 return 0, -1;
1634 }
1635  
1636 1279 my $pc = $self->{pieces};
1637 1279 my $p = [(0) x ($TBPIECES - 1)];
1638 1279 my $i = 0;
1639 1279 ▶ while ($i < $self->{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"}) 5077 1279 6356
1640 5077 my $piece_type = $pc->[$i] & 0x07;
1641 5077 my $colour = ($pc->[$i] ^ $cmirror) >> 3;
1642 5077 ▶ my $bb = $colour ? ($pos->[$piece_type] & $pos->[CP_POS_BLACK_PIECES]) : ($pos->[$piece_type] & $pos->[CP_POS_WHITE_PIECES]);
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 2534 2543 5077
1643  
1644 5077 ▶ while ($bb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bb) 5115 5077 10192
1645 5115 my $shift = (do { my $B = $bb & -$bb; my $A = $B - 1 - ((($B - 1) >> 1) & 0x5555_5555_5555_5555); my $C = ($A & 0x3333_3333_3333_3333) + (($A >> 2) & 0x3333_3333_3333_3333); my $n = $C + ($C >> 32); $n = ($n & 0x0f0f0f0f) + (($n >> 4) & 0x0f0f0f0f); $n = ($n & 0xffff) + ($n >> 16); $n = ($n & 0xff) + ($n >> 8);});
1646 5115 $p->[$i++] = $shift;
1647  
1648 5115 $bb = (($bb) & (($bb) - 1));
1649 }
1650 }
1651  
1652 # FIXME!
1653 1279 my $idx = $self->_encodePiece($self->{norm}, $p, $self->{factor});
1654 1279 $res = $self->_decompressPairs($self->{precomp}, $idx);
1655  
1656 1279 ▶ if ($self->{flags} & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flags"} & 2) 1021 258 1279
1657 1021 ▶ if (!($self->{flags} & 16)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'flags'} & 16) { } 1021 0 1021
1658 1021 $res = $read_byte->($self->{data}, $self->{p_map} + $self->{map_idx}->[0]->[WDL_TO_MAP->[$wdl + 2]] + $res);
1659 } else {
1660 0 $res = $self->_readUint16($self->{p_map} + 2 * ($self->{map_idx}->[0]->[WDL_TO_MAP->[$wdl + 2]] + $res));
1661 }
1662 }
1663  
1664 1279 ▶ if (!($self->{flags} & PA_FLAGS->[$wdl + 2]) || ($wdl & 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"flags"} & [8,0,0,0,4]->[$wdl + 2] or $wdl & 1) 1279 0 1279
Condition33%1/3
not $self->{"flags"} & [8,0,0,0,4]->[$wdl + 2] or $wdl & 1
l!l&&r!l&&!r
127900
MC/DC0%0/2
not $self->{"flags"} & [8,0,0,0,4]->[$wdl + 2] or $wdl & 1
not $self->{"flags"} & [8,0,0,0,4]->[$wdl + 2] $wdl & 1
Truth table33%1/3
not(A) or B
A= not $self->{"flags"} & [8,0,0,0,4]->[$wdl + 2]B= $wdl & 1
ABresult
1X1
011
000
1665 1279 $res *= 2;
1666 }
1667 } else {
1668 45 my $k = $self->{files}->[0]->{pieces}->[0] ^ $cmirror;
1669 45 my $piece_type = $k & 0x07;
1670 45 my $colour = $k >> 3;
1671 45 ▶ my $bb = $colour ? ($pos->[$piece_type] & $pos->[CP_POS_BLACK_PIECES]) : ($pos->[$piece_type] & $pos->[CP_POS_WHITE_PIECES]);
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 31 14 45
1672  
1673 45 my $i = 0;
1674 45 my $p = [(0) x ($TBPIECES - 1)];
1675 45 ▶ while ($bb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bb) 45 45 90
1676 45 my $shift = (do { my $B = $bb & -$bb; my $A = $B - 1 - ((($B - 1) >> 1) & 0x5555_5555_5555_5555); my $C = ($A & 0x3333_3333_3333_3333) + (($A >> 2) & 0x3333_3333_3333_3333); my $n = $C + ($C >> 32); $n = ($n & 0x0f0f0f0f) + (($n >> 4) & 0x0f0f0f0f); $n = ($n & 0xffff) + ($n >> 16); $n = ($n & 0xff) + ($n >> 8);});
1677 45 $p->[$i++] = $shift ^ $mirror;
1678  
1679 45 $bb = (($bb) & (($bb) - 1));
1680 }
1681  
1682 45 my $f = $self->_pawnFile($p);
1683 45 ▶ if (($self->{flags}->[$f] & 1) != $bside) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"flags"}[$f] & 1) != $bside) 19 26 45
1684 19 return 0, -1;
1685 }
1686  
1687 26 my $pc = $self->{files}->[$f]->{pieces};
1688 26 ▶ while ($i < $self->{num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"num"}) 78 26 104
1689 78 $piece_type = $pc->[$i] & 0x07;
1690 78 $colour = ($pc->[$i] ^ $cmirror) >> 3;
1691 78 ▶ my $bb = $colour ? ($pos->[$piece_type] & $pos->[CP_POS_BLACK_PIECES]) : ($pos->[$piece_type] & $pos->[CP_POS_WHITE_PIECES]);
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 31 47 78
1692  
1693 78 ▶ while ($bb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bb) 78 78 156
1694 78 my $shift = (do { my $B = $bb & -$bb; my $A = $B - 1 - ((($B - 1) >> 1) & 0x5555_5555_5555_5555); my $C = ($A & 0x3333_3333_3333_3333) + (($A >> 2) & 0x3333_3333_3333_3333); my $n = $C + ($C >> 32); $n = ($n & 0x0f0f0f0f) + (($n >> 4) & 0x0f0f0f0f); $n = ($n & 0xffff) + ($n >> 16); $n = ($n & 0xff) + ($n >> 8);});
1695 78 $p->[$i++] = $shift ^ $mirror;
1696  
1697 78 $bb = (($bb) & (($bb) - 1));
1698 }
1699 }
1700  
1701 26 my $idx = $self->_encodePawn($self->{files}->[$f]->{norm}, $p, $self->{files}->[$f]->{factor});
1702 26 $res = $self->_decompressPairs($self->{files}->[$f]->{precomp}, $idx);
1703  
1704 26 ▶ if ($self->{flags}->[$f] & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flags"}[$f] & 2) 10 16 26
1705 10 ▶ if (!($self->{flags}->[$f] & 16)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'flags'}[$f] & 16) { } 10 0 10
1706 10 $res = $read_byte->($self->{data}, $self->{p_map} + $self->{map_idx}->[$f]->[WDL_TO_MAP->[$wdl + 2]] + $res);
1707 } else {
1708 0 $res = $self->_readUint16($self->{p_map} + 2 * ($self->{map_idx}->[$f]->[WDL_TO_MAP->[$wdl + 2]] + $res));
1709 }
1710 }
1711  
1712 26 ▶ if (!($self->{flags}->[$f] & $->[$wdl + 2]) || ($wdl & 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"flags"}[$f] & $- > [$wdl + 2] or $wdl & 1) 26 0 26
Condition33%1/3
not $self->{"flags"}[$f] & $- > [$wdl + 2] or $wdl & 1
l!l&&r!l&&!r
2600
MC/DC0%0/2
not $self->{"flags"}[$f] & $- > [$wdl + 2] or $wdl & 1
not $self->{"flags"}[$f] & $- > [$wdl + 2] $wdl & 1
Truth table33%1/3
not(A) or B
A= not $self->{"flags"}[$f] & $- > [$wdl + 2]B= $wdl & 1
ABresult
1X1
011
000
1713 26 $res *= 2;
1714 }
1715 }
1716  
1717 1305 return $res, 1;
1718 }
1719  
1720 package Chess::Plisco::Tablebase::Syzygy;
1721 10 $Chess::Plisco::Tablebase::Syzygy::VERSION = 'v1.0.3';
1722 10 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
1723 10 ▶use File::Globstar qw(globstar);
Subroutine BEGIN: called CC=1 SCAR=0.0
1724 10 ▶use Locale::TextDomain qw('Chess-Plisco');
Subroutine BEGIN: called CC=1 SCAR=0.0
1725 10 ▶use Tie::Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
1726  
1727 10 ▶use Chess::Plisco qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
1728 # Macros from Chess::Plisco::Macro are already expanded here!
1729  
1730 10 ▶use base qw(Chess::Plisco::Tablebase);
Subroutine BEGIN: called CC=1 SCAR=0.0
1731  
1732 10 ▶use constant TBW_SUFFIX => 'rtbw';
Subroutine BEGIN: called CC=1 SCAR=0.0
1733 10 ▶use constant TBZ_SUFFIX => 'rtbz';
Subroutine BEGIN: called CC=1 SCAR=0.0
1734 10 ▶use constant WDL_TO_DTZ => [-1, -101, 0, 101, 1];
Subroutine BEGIN: called CC=1 SCAR=0.0
1735  
1736 7 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
1737 7 my ($class, $directory, %__options) = @_;
1738  
1739 7 my %options = (
1740 load_wdl => 1,
1741 load_dtz => 1,
1742 max_fds => 128,
1743 %__options
1744 );
1745  
1746 7 my %tables;
1747 7 ▶ if (0 + $options{max_fds} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $options{"max_fds"} > 0) 7 0 7
1748 7 tie %tables, 'Tie::Cache::LRU', $options{max_fds};
1749 }
1750  
1751 7 my $self = bless {
1752 tables => \%tables,
1753 wdl => {},
1754 dtz => {},
1755 wdl_files => 0,
1756 dtz_files => 0,
1757 }, $class;
1758  
1759 7 ▶ $self->addDirectory($directory, %options) if defined $directory;
Branch: 1 branch
BranchTrueFalseTotal
if defined $directory 6 1 7
1760  
1761 7 return $self;
1762 }
1763  
1764 6 ▶sub addDirectory {
Subroutine addDirectory: called CC=8 SCAR=24.5
Pod documented
1765 6 my ($self, $directory, %__options) = @_;
1766  
1767 6 my %options = (
1768 load_wdl => 1,
1769 load_dtz => 1,
1770 %__options
1771 );
1772  
1773 6 my (@rtbw_files, @rtbz_files);
1774 6 $directory = File::Spec->rel2abs($directory);
1775  
1776 6 ▶ if ($options{recursive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'recursive'}) { } 0 6 6
1777 0 ▶ @rtbw_files = globstar "$directory/**/*.rtbw" if $options{load_wdl};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"load_wdl"} 0 0 0
1778 0 ▶ @rtbz_files = globstar "$directory/**/*.rtbz" if $options{load_dtz};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"load_dtz"} 0 0 0
1779 } else {
1780 6 ▶ @rtbw_files = globstar "$directory/*.rtbw" if $options{load_wdl};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"load_wdl"} 6 0 6
1781 6 ▶ @rtbz_files = globstar "$directory/*.rtbz" if $options{load_dtz};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"load_dtz"} 6 0 6
1782 }
1783  
1784 6 my @files = (@rtbw_files, @rtbz_files);
1785  
1786 6 my $num_files = 0;
1787 6 foreach my $filename (@files) {
1788 420 ▶ ++$num_files if $self->__addFile($filename, %options)
Branch: 1 branch
BranchTrueFalseTotal
if $self->__addFile($filename, %options) 420 0 420
1789 }
1790  
1791 6 return $num_files;
1792 }
1793  
1794 420 ▶sub __addFile {
Subroutine __addFile: called CC=6 SCAR=18.1
1795 420 my ($self, $path, %options) = @_;
1796  
1797 420 my $basename = basename $path;
1798 420 my ($tablename, $ext) = split /\./, $basename, 2;
1799  
1800 420 ▶ if ($is_tablename->($tablename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_tablename($tablename)) 420 0 420
1801 420 my ($white_part, $black_part) = split 'v', $tablename;
1802 420 my $mirrored_tablename = join 'v', $black_part, $white_part;
1803 420 ▶ if ($ext eq TBW_SUFFIX) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ext eq 'rtbw') { } 210 210 420
elsif ($ext eq 'rtbz') { } 210 0 210
1804 210 ▶ if ($options{load_wdl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"load_wdl"}) 210 0 210
1805 210 $self->{wdl}->{$tablename} = $path;
1806 210 $self->{wdl}->{$mirrored_tablename} = $path;
1807 210 ++$self->{wdl_files};
1808  
1809 210 return 1;
1810 }
1811 } elsif ($ext eq TBZ_SUFFIX) {
1812 210 ▶ if ($options{load_dtz}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"load_dtz"}) 210 0 210
1813 210 $self->{dtz}->{$tablename} = $path;
1814 210 $self->{dtz}->{$mirrored_tablename} = $path;
1815 210 ++$self->{dtz_files};
1816  
1817 210 return 1;
1818 }
1819 }
1820 }
1821 }
1822  
1823 3 ▶sub largestWdl {
Subroutine largestWdl: called CC=3 SCAR=11.0
Pod documented
1824 3 my ($self) = @_;
1825  
1826 3 my $max = 0;
1827 3 foreach my $table (keys %{$self->{wdl}}) {
1828 65 my $num_pieces = (length $table) - 1;
1829 65 ▶ $max = $num_pieces if $num_pieces > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $num_pieces > $max 2 63 65
1830 }
1831  
1832 3 return $max;
1833 }
1834  
1835 1 ▶sub largestDtz {
Subroutine largestDtz: called CC=3 SCAR=11.0
Pod documented
1836 1 my ($self) = @_;
1837  
1838 1 my $max = 0;
1839 1 foreach my $table (keys %{$self->{dtz}}) {
1840 65 my $num_pieces = (length $table) - 1;
1841 65 ▶ $max = $num_pieces if $num_pieces > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $num_pieces > $max 1 64 65
1842 }
1843  
1844 1 return $max;
1845 }
1846  
1847 0 ▶sub largest {
Subroutine largest: not called CC=2 SCAR=17.9
Pod documented
1848 0 my ($self) = @_;
1849  
1850 0 my $largestWdl = $self->largestWdl;
1851 0 my $largestDtz = $self->largestDtz;
1852  
1853 0 ▶ return $largestWdl < $largestDtz ? $largestWdl : $largestDtz;
Branch: 1 branch
BranchTrueFalseTotal
$largestWdl < $largestDtz ? : 0 0 0
1854 }
1855  
1856 0 ▶sub numWdlFiles {
Subroutine numWdlFiles: not called CC=1 SCAR=6.9
Pod documented
1857 0 my ($self) = @_;
1858  
1859 0 return $self->{wdl_files};
1860 }
1861  
1862 0 ▶sub numDtzFiles {
Subroutine numDtzFiles: not called CC=1 SCAR=6.9
Pod undocumented
1863 0 my ($self) = @_;
1864  
1865 0 return $self->{dtz_files};
1866 }
1867  
1868 420 ▶sub __checkPosition {
Subroutine __checkPosition: called CC=4 SCAR=14.4
1869 420 my ($self, $pos) = @_;
1870  
1871 420 ▶ if ($pos->castlingRights) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->castlingRights) 0 420 420
1872 0 die __x("Syzygy tables do not contain positions with castling rights: {fen}",
1873 fen => $pos->toFEN);
1874 }
1875  
1876 420 my $piece_count;
1877 420 ▶ { my $_b = $pos->occupied; for ($piece_count = 0; $_b; ++$piece_count) { $_b &= $_b - 1; } };
Branch: 1 branch
BranchTrueFalseTotal
if ($_b) 1677 420 2097
1878  
1879 420 ▶ if ($piece_count > $TBPIECES + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece_count > $TBPIECES + 1) 0 420 420
1880 0 die __x("syzygy tables support up to {TBPIECES} pieces, not {piece_count}: {fen}",
1881 TBPIECES => $TBPIECES,
1882 piece_count => $piece_count,
1883 fen => $pos->toFEN);
1884 }
1885  
1886 420 return $self;
1887 }
1888  
1889 420 ▶sub probeWdl {
Subroutine probeWdl: called CC=12 SCAR=25.7
Pod documented
1890 420 my ($self, $pos) = @_;
1891  
1892 420 $self->__checkPosition($pos);
1893  
1894 420 my ($value) = $self->__probeAb($pos, -2, 2);
1895  
1896 # Positions where en passant is possible need special care because the
1897 # Syzygy tablebases assume that en passant is not possible. Otherwise,
1898 # we can just return the result of the probe.
1899 418 my $ep_shift = $pos->[CP_POS_EN_PASSANT_SHIFT];
1900 418 ▶ return $value if !$ep_shift;
Branch: 1 branch
BranchTrueFalseTotal
unless $ep_shift 415 3 418
1901  
1902 # Positions resulting from en passant captures have not been considered,
1903 # when generating the table. We do that now, in the wrapper code, by
1904 # trying to play these en passant captures. If any of them (maximum 2)
1905 # yields a better result than the probe, that move should be played.
1906 #
1907 # Test case: 8/8/8/pPk5/8/8/8/7K w - a6 0 1
1908 #
1909 # It is also possible that the only legal moves in a position are en
1910 # passant captures. Example: K7/3n1P2/1k6/pP6/8/8/8/8 w - a6 0 1. In this
1911 # case, the best of the captures have to be played.
1912 3 my $v1 = -3;
1913 3 my @legal_moves = $pos->legalMoves;
1914 3 my @ep_captures;
1915 3 foreach my $move (@legal_moves) {
1916 19 my $to = ((($move) >> 15) & 0x3f);
1917 19 ▶ next if $to != $ep_shift;
Branch: 1 branch
BranchTrueFalseTotal
if $to != $ep_shift 17 2 19
1918  
1919 2 my $piece = (($move) & 0x7);
1920 2 ▶ next if $piece != CP_PAWN;
Branch: 1 branch
BranchTrueFalseTotal
if $piece != 1 1 1 2
1921  
1922 1 push @ep_captures, $move;
1923 }
1924  
1925 3 foreach my $move (@ep_captures) {
1926 1 my $undo = $pos->doMove($move);
1927 1 eval {
1928 1 my ($v0_plus) = $self->__probeAb($pos, -2, 2);
1929 1 my $v0 = -$v0_plus;
1930  
1931 1 ▶ if ($v0 > $v1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v0 > $v1) 1 0 1
1932 1 $v1 = $v0;
1933 }
1934 };
1935 1 $pos->undoMove($undo);
1936 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
1937 }
1938  
1939 3 ▶ if ($v1 > -3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 > -3) 1 2 3
1940 1 ▶ if ($v1 >= $value) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v1 >= $value) { } 1 0 1
elsif ($value == 0) { } 0 0 0
1941 1 $value = $v1;
1942 } elsif ($value == 0) {
1943 0 ▶ if (@legal_moves == @ep_captures) {
Branch: 1 branch
BranchTrueFalseTotal
if (@legal_moves == @ep_captures) 0 0 0
1944 # All legal moves are en passant captures.
1945 0 $value = $v1;
1946 }
1947 }
1948 }
1949
1950 3 return $value;
1951 }
1952  
1953 2024 ▶sub probeDtz {
Subroutine probeDtz: called CC=20 SCAR=40.1
Pod documented
1954 2024 my ($self, $pos) = @_;
1955  
1956 2024 my $value = $self->__probeDtzNoEP($pos);
1957  
1958 # Positions where en passant is possible need special care because the
1959 # Syzygy tablebases assume that en passant is not possible. Otherwise,
1960 # we can just return the result of the probe.
1961 2022 my $ep_shift = $pos->[CP_POS_EN_PASSANT_SHIFT];
1962  
1963 2022 ▶ return $value if !$ep_shift;
Branch: 1 branch
BranchTrueFalseTotal
unless $ep_shift 2021 1 2022
1964  
1965 # Positions resulting from en passant captures have not been considered,
1966 # when generating the table. We do that now, in the wrapper code, by
1967 # trying to play these en passant captures. If any of them (maximum 2)
1968 # yields a better result than the probe, that move should be played.
1969 #
1970 # Test case: 8/8/8/pPk5/8/8/8/7K w - a6 0 1
1971 #
1972 # It is also possible that the only legal moves in a position are en
1973 # passant captures. Example: K7/3n1P2/1k6/pP6/8/8/8/8 w - a6 0 1. In this
1974 # case, the best of the captures have to be played.
1975 1 my $v1 = -3;
1976 1 my @legal_moves = $pos->legalMoves;
1977 1 my @ep_captures;
1978 1 foreach my $move (@legal_moves) {
1979 7 my $to = ((($move) >> 15) & 0x3f);
1980 7 ▶ next if $to != $ep_shift;
Branch: 1 branch
BranchTrueFalseTotal
if $to != $ep_shift 6 1 7
1981  
1982 1 my $piece = (($move) & 0x7);
1983 1 ▶ next if $piece != CP_PAWN;
Branch: 1 branch
BranchTrueFalseTotal
if $piece != 1 0 1 1
1984  
1985 1 push @ep_captures, $move;
1986 }
1987  
1988 1 foreach my $move (@ep_captures) {
1989 1 my $undo = $pos->doMove($move);
1990 1 eval {
1991 1 my ($v0_plus) = $self->__probeAb($pos, -2, 2);
1992 1 my $v0 = -$v0_plus;
1993  
1994 1 ▶ if ($v0 > $v1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v0 > $v1) 1 0 1
1995 1 $v1 = $v0;
1996 }
1997 };
1998 1 $pos->undoMove($undo);
1999 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
2000 }
2001  
2002 1 ▶ if ($v1 > -3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 > -3) 1 0 1
2003 1 $v1 = WDL_TO_DTZ->[$v1 + 2];
2004 1 ▶ if ($value < -100) {
Branch: 5 branches
BranchTrueFalseTotal
if ($value < -100) { } 0 1 1
elsif ($value < 0) { } 1 0 1
elsif ($value > 100) { } 0 0 0
elsif ($value > 0) { } 0 0 0
elsif ($v1 >= 0) { } 0 0 0
2005 0 ▶ if ($v1 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 >= 0) 0 0 0
2006 0 $value = $v1;
2007 }
2008 } elsif ($value < 0) {
2009 1 ▶ if ($v1 >= 0 || $v1 < -100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 >= 0 or $v1 < -100) 1 0 1
Condition33%1/3
$v1 >= 0 or $v1 < -100
l!l&&r!l&&!r
100
MC/DC0%0/2
$v1 >= 0 or $v1 < -100
$v1 >= 0 $v1 < -100
Truth table33%1/3
A or B
A= $v1 >= 0B= $v1 < -100
ABresult
1X1
011
000
2010 1 $value = $v1;
2011 }
2012 } elsif ($value > 100) {
2013 0 ▶ if ($v1 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 > 0) 0 0 0
2014 0 $value = $v1;
2015 }
2016 } elsif ($value > 0) {
2017 0 ▶ if ($v1 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v1 == 1) 0 0 0
2018 0 $value = $v1;
2019 }
2020 } elsif ($v1 >= 0) {
2021 0 $value = $v1;
2022 } else {
2023 0 ▶ if (@legal_moves == @ep_captures) {
Branch: 1 branch
BranchTrueFalseTotal
if (@legal_moves == @ep_captures) 0 0 0
2024 # All legal moves are en passant captures.
2025 0 $value = $v1;
2026 }
2027 }
2028 }
2029  
2030 1 return $value;
2031 }
2032  
2033 2025 ▶sub __probeDtzNoEP {
Subroutine __probeDtzNoEP: called CC=31 SCAR=36.0
2034 2025 my ($self, $pos) = @_;
2035  
2036 2025 my ($wdl, $success) = $self->__probeAb($pos, -2, 2);
2037 2023 ▶ return 0 if $wdl == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $wdl == 0 108 1915 2023
2038  
2039 1915 ▶ if ($success == 2
Branch: 1 branch
BranchTrueFalseTotal
if ($success == 2 or not $$pos[7 + $pos->[12]] & ~$pos->[1]) 382 1533 1915
Condition66%2/3
$success == 2 or not $$pos[7 + $pos->[12]] & ~$pos->[1]
l!l&&r!l&&!r
38201533
MC/DC50%1/2
$success == 2 or not $$pos[7 + $pos->[12]] & ~$pos->[1]
$success == 2 not $$pos[7 + $pos->[12]] & ~$pos->[1]
Truth table66%2/3
A or not(B)
A= $success == 2B= not $$pos[7 + $pos->[12]] & ~$pos->[1]
ABresult
1X1
011
000
2040 || !($pos->[CP_POS_WHITE_PIECES + $pos->[CP_POS_TURN]] & ~$pos->[CP_POS_PAWNS])) {
2041 382 return $dtz_before_zeroing->($wdl);
2042 }
2043  
2044 1533 my $moves;
2045 1533 ▶ if ($wdl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wdl > 0) 1304 229 1533
2046 # Generate all legal non-capturing pawn moves.
2047 1304 ▶ $moves //= [$pos->legalMoves];
Condition50%1/2
$moves //= [$pos->legalMoves]
l!l
01304
MC/DC0%0/1
$moves //= [$pos->legalMoves]
$moves
Truth table50%1/2
A
A= $moves
Aresult
11
00
2048 1304 foreach my $move (@$moves) {
2049 31094 ▶ next if (($move) & 0x7) != CP_PAWN;
Branch: 1 branch
BranchTrueFalseTotal
if ($move & 7) != 1 31020 74 31094
2050 74 ▶ next if ((($move) >> 3) & 0x7);
Branch: 1 branch
BranchTrueFalseTotal
if $move >> 3 & 7 0 74 74
2051  
2052 74 my $undo = $pos->doMove($move);
2053  
2054 74 my $v = eval { -$self->probeWdl($pos) };
2055 74 $pos->undoMove($undo);
2056 74 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 74 74
2057  
2058 74 ▶ if ($v == $wdl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == $wdl) 70 4 74
2059 70 ▶ return ($v == 2) ? 1 : 101;
Branch: 1 branch
BranchTrueFalseTotal
$v == 2 ? : 70 0 70
2060 }
2061 }
2062 }
2063  
2064 1463 my ($dtz, $success) = $self->__probeDtzTable($pos, $wdl);
2065 1463 ▶ if ($success >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success >= 0) 1305 158 1463
2066 1305 ▶ if ($wdl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wdl > 0) { } 1229 76 1305
2067 1229 return $dtz_before_zeroing->($wdl) + $dtz;
2068 } else {
2069 76 return $dtz_before_zeroing->($wdl) - $dtz;
2070 }
2071 }
2072  
2073 158 my $best;
2074  
2075 158 ▶ if ($wdl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wdl > 0) { } 5 153 158
2076 5 $best = 0xffff;
2077  
2078 # Generate all quite non-pawn moves.
2079 5 ▶ $moves //= [$pos->legalMoves];
Condition50%1/2
$moves //= [$pos->legalMoves]
l!l
50
MC/DC0%0/1
$moves //= [$pos->legalMoves]
$moves
Truth table50%1/2
A
A= $moves
Aresult
11
00
2080 5 foreach my $move (@$moves) {
2081 101 ▶ next if (($move) & 0x7) == CP_PAWN;
Branch: 1 branch
BranchTrueFalseTotal
if ($move & 7) == 1 0 101 101
2082 101 ▶ next if ((($move) >> 3) & 0x7);
Branch: 1 branch
BranchTrueFalseTotal
if $move >> 3 & 7 0 101 101
2083  
2084 101 my $undo = $pos->doMove($move);
2085 101 eval {
2086 101 my $v = -$self->probeDtz($pos);
2087  
2088 101 ▶ if ($v == 1 && $is_checkmate->($pos)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v == 1 and &$is_checkmate($pos)) { } 0 101 101
elsif ($v > 0 and $v + 1 < $best) { } 9 127 136
Condition33%1/3
$v == 1 and &$is_checkmate($pos)
!ll&&!rl&&r
10100
MC/DC0%0/2
$v == 1 and &$is_checkmate($pos)
$v == 1 &$is_checkmate($pos)
Truth table33%1/3
A and B
A= $v == 1B= &$is_checkmate($pos)
ABresult
0X0
100
111
2089 0 $best = 1;
2090 ▶ } elsif (($v > 0) && (($v + 1) < $best)) {
Condition100%3/3
$v > 0 and $v + 1 < $best
!ll&&!rl&&r
35579
MC/DC100%2/2
$v > 0 and $v + 1 < $best
$v > 0 $v + 1 < $best
Truth table100%3/3
A and B
A= $v > 0B= $v + 1 < $best
ABresult
0X0
100
111
2091 9 $best = $v + 1;
2092 }
2093 };
2094 101 $pos->undoMove($undo);
2095 101 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 101 101
2096 }
2097 } else {
2098 153 $best = -1;
2099  
2100 153 ▶ $moves //= [$pos->legalMoves];
Condition50%1/2
$moves //= [$pos->legalMoves]
l!l
0153
MC/DC0%0/1
$moves //= [$pos->legalMoves]
$moves
Truth table50%1/2
A
A= $moves
Aresult
11
00
2101 153 foreach my $move (@$moves) {
2102 1636 my $undo = $pos->doMove($move);
2103  
2104 1636 my $v;
2105 1636 eval {
2106 1636 ▶ if (!$pos->[CP_POS_HALFMOVE_CLOCK]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pos->[11]) { } 8 1628 1636
2107 8 ▶ if ($wdl == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wdl == -2) { } 8 0 8
2108 8 $v = -1;
2109 } else {
2110 # FIXME! No need to store $success.
2111 0 ($v, $success) = $self.__probeAb($pos, 1, 2);
2112 0 ▶ $v = ($v == 2) ? 0 : -101;
Branch: 1 branch
BranchTrueFalseTotal
$v == 2 ? : 0 0 0
2113 }
2114 } else {
2115 1628 $v = -$self->probeDtz($pos) - 1;
2116 }
2117 };
2118 1636 $pos->undoMove($undo);
2119 1636 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1636 1636
2120  
2121 1636 ▶ if ($v < $best) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v < $best) 333 1303 1636
2122 333 $best = $v;
2123 }
2124 }
2125 }
2126  
2127 158 return $best;
2128 }
2129  
2130 142 ▶sub safeProbeWdl {
Subroutine safeProbeWdl: called CC=4 SCAR=14.5
Pod documented
2131 142 my ($self, $pos, $default) = @_;
2132  
2133 142 my $result;
2134 142 eval {
2135 142 $result = $self->probeWdl($pos);
2136 };
2137 142 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 140 142
2138 2 ▶ if (ref $@
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ and $@->isa('Chess::Plisco::Tablebase::Syzygy::MissingTableException')) { } 2 0 2
Condition33%1/3
ref $@ and $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $@ and $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
ref $@ $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
Truth table33%1/3
A and B
A= ref $@B= $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
ABresult
0X0
100
111
2139 && $@->isa('Chess::Plisco::Tablebase::Syzygy::MissingTableException')) {
2140 2 return $default;
2141 } else {
2142 # Re-throw.
2143 0 die $@;
2144 }
2145 }
2146  
2147 140 return $result;
2148 }
2149  
2150 94 ▶sub safeProbeDtz {
Subroutine safeProbeDtz: called CC=4 SCAR=14.5
Pod documented
2151 94 my ($self, $pos, $default) = @_;
2152  
2153 94 my $result;
2154 94 eval {
2155 94 $result = $self->probeDtz($pos);
2156 };
2157 94 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 92 94
2158 2 ▶ if (ref $@ && $@->isa('Chess::Plisco::Tablebase::Syzygy::MissingTableException')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ and $@->isa('Chess::Plisco::Tablebase::Syzygy::MissingTableException')) { } 2 0 2
Condition33%1/3
ref $@ and $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $@ and $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
ref $@ $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
Truth table33%1/3
A and B
A= ref $@B= $@->isa("Chess::Plisco::Tablebase::Syzygy::MissingTableException")
ABresult
0X0
100
111
2159 2 return $default;
2160 } else {
2161 # Re-throw.
2162 0 die $@;
2163 }
2164 }
2165  
2166 92 return $result;
2167 }
2168  
2169 3075 ▶sub __probeAb {
Subroutine __probeAb: called CC=8 SCAR=20.8
2170 3075 my ($self, $pos, $alpha, $beta) = @_;
2171  
2172 # Iterate over all non-ep captures.
2173 3075 my $v;
2174 3075 foreach my $move ($remove_ep->($pos)->legalMoves) {
2175 47376 ▶ ((($move) >> 3) & 0x7) or next;
Branch: 1 branch
BranchTrueFalseTotal
$move >> 3 & 7 or next 628 46748 47376
2176  
2177 628 my $undo = $pos->doMove($move);
2178 628 eval {
2179 628 my ($v_plus) = $self->__probeAb($pos, -$beta, -$alpha);
2180 628 $v = -$v_plus;
2181 };
2182 628 $pos->undoMove($undo);
2183 628 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 628 628
2184  
2185 628 ▶ if ($v > $alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v > $alpha) 613 15 628
2186 613 ▶ if ($v >= $beta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v >= $beta) 422 191 613
2187 422 return $v, 2;
2188 }
2189  
2190 191 $alpha = $v;
2191 }
2192  
2193 }
2194  
2195 2653 $v = $self->__probeWdlTable($pos);
2196  
2197 2649 ▶ if ($alpha >= $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha >= $v) 1023 1626 2649
2198 1023 ▶ return $alpha > 0 ? ($alpha, 2) : ($alpha, 1);
Branch: 1 branch
BranchTrueFalseTotal
$alpha > 0 ? : 0 1023 1023
2199 }
2200  
2201 1626 return $v, 1
2202 }
2203  
2204 2854 ▶sub __probeWdlTable {
Subroutine __probeWdlTable: called CC=6 SCAR=18.0
2205 2854 my ($self, $pos) = @_;
2206  
2207 # Test for KvK.
2208 2854 ▶ if ($pos->[CP_POS_KINGS]
Branch: 1 branch
BranchTrueFalseTotal
if ($pos->[6] == ($pos->[7] | $pos->[8])) 59 2795 2854
2209 == ($pos->[CP_POS_WHITE_PIECES] | $pos->[CP_POS_BLACK_PIECES])) {
2210 59 return 0;
2211 }
2212  
2213 2795 my $key = $calc_key->($pos);
2214 2795 my $path = $self->{wdl}->{$key};
2215 2795 ▶ if (!defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 4 2791 2795
2216 4 die Chess::Plisco::Tablebase::Syzygy::MissingTableException->new(__x(
2217 __"Missing WDL table '{key}'.\n",
2218 key => $key,
2219 ));
2220 }
2221  
2222 2791 my $full_key = join '', $key, '.', TBW_SUFFIX;
2223 2791 my $table = $self->{tables}->{$full_key};
2224 2791 ▶ if (!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 73 2718 2791
2225 73 $table = Chess::Plisco::Tablebase::Syzygy::WdlTable->new($path);
2226 73 ▶ $self->{tables}->{$full_key} = $table if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 73 0 73
2227 }
2228  
2229 2791 ▶ if (!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 2791 2791
2230 0 die __x("Cannot initialize WDL table '{key}'.\n", key => $key);
2231 }
2232  
2233 2791 return $table->probeWdlTable($pos);
2234 }
2235  
2236 1463 ▶sub __probeDtzTable {
Subroutine __probeDtzTable: called CC=5 SCAR=16.7
2237 1463 my ($self, $pos, $wdl) = @_;
2238  
2239 1463 my $key = $calc_key->($pos);
2240 1463 my $path = $self->{dtz}->{$key};
2241 1463 ▶ if (!defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 1463 1463
2242 0 die Chess::Plisco::Tablebase::Syzygy::MissingTableException->new(__x(
2243 __"Missing DTZ table '{key}'.\n",
2244 key => $key,
2245 ));
2246 }
2247  
2248 1463 my $full_key = join '', $key, '.', TBZ_SUFFIX;
2249 1463 my $table = $self->{tables}->{$full_key};
2250 1463 ▶ if (!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 36 1427 1463
2251 36 $table = Chess::Plisco::Tablebase::Syzygy::DtzTable->new($path);
2252 36 ▶ $self->{tables}->{$full_key} = $table if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 36 0 36
2253 }
2254  
2255 1463 ▶ if (!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 1463 1463
2256 0 die __x("Cannot initialize DTZ table '{key}'.\n", key => $key);
2257 }
2258  
2259 1463 return $table->probeDtzTable($pos, $wdl);
2260 }
2261  
2262 10 1;
« Plisco/Engine/Book.pm ↑ Summary Plisco/EPD.pm »