Plisco/Engine/Position.pm

filter: Statementstmt 96.9% 282 / 291 Branchbran 98.6% 75 / 76 Conditioncond 100% 39 / 39 MC/DCmcdc 100% 21 / 21 Subroutinesub 88.4% 23 / 26 Podpod 50.0% 4 / 8 totaltotal 96.3% 444 / 461 CC 29 SCAR 19.7
CC
29
Cov
98%
move
15.0
evaluate
10.0
formatMoves
6.0
CRAP 7.1
« Plisco/EPD/Record.pm ↑ Summary Plisco/Engine/TranspositionTable.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 package Chess::Plisco::Engine::Position;
13 23 $Chess::Plisco::Engine::Position::VERSION = 'v1.0.3';
14 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 23 ▶use Chess::Plisco qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # Macros from Chess::Plisco::Macro are already expanded here!
19  
20 23 ▶use base qw(Chess::Plisco Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Additional fields.
23 23 ▶use constant CP_POS_SIGNATURE => CP_POS_USR1;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 23 ▶use constant CP_POS_REVERSIBLE_CLOCK => CP_POS_USR2;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 23 ▶use constant CP_POS_GAME_PHASE => CP_POS_USR3;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 23 ▶use constant CP_POS_OPENING_SCORE => CP_POS_USR4;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 23 ▶use constant CP_POS_ENDGAME_SCORE => CP_POS_USR5;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 23 ▶use constant CP_POS_POPCOUNT => CP_POS_USR6;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 23 our @EXPORT_OK = qw(CP_POS_REVERSIBLE_CLOCK CP_POS_POPCOUNT);
31  
32 23 ▶use constant PAWN_PHASE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 23 ▶use constant KNIGHT_PHASE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 23 ▶use constant BISHOP_PHASE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 23 ▶use constant ROOK_PHASE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 23 ▶use constant QUEEN_PHASE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 23 ▶use constant TOTAL_PHASE => PAWN_PHASE * 16
Subroutine BEGIN: called CC=1 SCAR=0.0
38 + KNIGHT_PHASE * 4 + BISHOP_PHASE * 4
39 23 + ROOK_PHASE * 4 + QUEEN_PHASE * 2;
40 23 ▶use constant PHASE_INC => [
Subroutine BEGIN: called CC=1 SCAR=0.0
41 0,
42 PAWN_PHASE,
43 KNIGHT_PHASE,
44 BISHOP_PHASE,
45 ROOK_PHASE,
46 QUEEN_PHASE,
47 0,
48 23 ];
49  
50  
51 23 my @op_value = (0, 82, 337, 365, 477, 1025, 0);
52 23 my @eg_value = (0, 94, 281, 297, 512, 936, 0);
53  
54 # piece/sq tables
55 # values from Rofchade: http://www.talkchess.com/forum3/viewtopic.php?f=2&t=68311&start=19
56  
57 23 my @op_pawn_table = (
58 0, 0, 0, 0, 0, 0, 0, 0,
59 98, 134, 61, 95, 68, 126, 34, -11,
60 -6, 7, 26, 31, 65, 56, 25, -20,
61 -14, 13, 6, 21, 23, 12, 17, -23,
62 -27, -2, -5, 12, 17, 6, 10, -25,
63 -26, -4, -4, -10, 3, 3, 33, -12,
64 # 47, 81, 62, 59, 67, 106, 120, 60
65 -35, -1, -20, -23, -15, 24, 38, -22,
66 0, 0, 0, 0, 0, 0, 0, 0,
67 );
68  
69 23 my @eg_pawn_table = (
70 0, 0, 0, 0, 0, 0, 0, 0,
71 178, 173, 158, 134, 147, 132, 165, 187,
72 94, 100, 85, 67, 56, 53, 82, 84,
73 32, 24, 13, 5, -2, 4, 17, 17,
74 13, 9, -3, -7, -7, -8, 3, -1,
75 4, 7, -6, 1, 0, -5, -1, -8,
76 13, 8, 8, 10, 13, 0, 2, -7,
77 0, 0, 0, 0, 0, 0, 0, 0,
78 );
79  
80 23 my @op_knight_table = (
81 -167, -89, -34, -49, 61, -97, -15, -107,
82 -73, -41, 72, 36, 23, 62, 7, -17,
83 -47, 60, 37, 65, 84, 129, 73, 44,
84 -9, 17, 19, 53, 37, 69, 18, 22,
85 -13, 4, 16, 13, 28, 19, 21, -8,
86 -23, -9, 12, 10, 19, 17, 25, -16,
87 -29, -53, -12, -3, -1, 18, -14, -19,
88 -105, -21, -58, -33, -17, -28, -19, -23,
89 );
90  
91 23 my @eg_knight_table = (
92 -58, -38, -13, -28, -31, -27, -63, -99,
93 -25, -8, -25, -2, -9, -25, -24, -52,
94 -24, -20, 10, 9, -1, -9, -19, -41,
95 -17, 3, 22, 22, 22, 11, 8, -18,
96 -18, -6, 16, 25, 16, 17, 4, -18,
97 -23, -3, -1, 15, 10, -3, -20, -22,
98 -42, -20, -10, -5, -2, -20, -23, -44,
99 -29, -51, -23, -15, -22, -18, -50, -64,
100 );
101  
102 23 my @op_bishop_table = (
103 -29, 4, -82, -37, -25, -42, 7, -8,
104 -26, 16, -18, -13, 30, 59, 18, -47,
105 -16, 37, 43, 40, 35, 50, 37, -2,
106 -4, 5, 19, 50, 37, 37, 7, -2,
107 -6, 13, 13, 26, 34, 12, 10, 4,
108 0, 15, 15, 15, 14, 27, 18, 10,
109 4, 15, 16, 0, 7, 21, 33, 1,
110 -33, -3, -14, -21, -13, -12, -39, -21,
111 );
112  
113 23 my @eg_bishop_table = (
114 -14, -21, -11, -8, -7, -9, -17, -24,
115 -8, -4, 7, -12, -3, -13, -4, -14,
116 2, -8, 0, -1, -2, 6, 0, 4,
117 -3, 9, 12, 9, 14, 10, 3, 2,
118 -6, 3, 13, 19, 7, 10, -3, -9,
119 -12, -3, 8, 10, 13, 3, -7, -15,
120 -14, -18, -7, -1, 4, -9, -15, -27,
121 -23, -9, -23, -5, -9, -16, -5, -17,
122 );
123  
124 23 my @op_rook_table = (
125 32, 42, 32, 51, 63, 9, 31, 43,
126 27, 32, 58, 62, 80, 67, 26, 44,
127 -5, 19, 26, 36, 17, 45, 61, 16,
128 -24, -11, 7, 26, 24, 35, -8, -20,
129 -36, -26, -12, -1, 9, -7, 6, -23,
130 -45, -25, -16, -17, 3, 0, -5, -33,
131 -44, -16, -20, -9, -1, 11, -6, -71,
132 -19, -13, 1, 17, 16, 7, -37, -26,
133 );
134  
135 23 my @eg_rook_table = (
136 13, 10, 18, 15, 12, 12, 8, 5,
137 11, 13, 13, 11, -3, 3, 8, 3,
138 7, 7, 7, 5, 4, -3, -5, -3,
139 4, 3, 13, 1, 2, 1, -1, 2,
140 3, 5, 8, 4, -5, -6, -8, -11,
141 -4, 0, -5, -1, -7, -12, -8, -16,
142 -6, -6, 0, 2, -9, -9, -11, -3,
143 -9, 2, 3, -1, -5, -13, 4, -20,
144 );
145  
146 23 my @op_queen_table = (
147 -28, 0, 29, 12, 59, 44, 43, 45,
148 -24, -39, -5, 1, -16, 57, 28, 54,
149 -13, -17, 7, 8, 29, 56, 47, 57,
150 -27, -27, -16, -16, -1, 17, -2, 1,
151 -9, -26, -9, -10, -2, -4, 3, -3,
152 -14, 2, -11, -2, -5, 2, 14, 5,
153 -35, -8, 11, 2, 8, 15, -3, 1,
154 -1, -18, -9, 10, -15, -25, -31, -50,
155 );
156  
157 23 my @eg_queen_table = (
158 -9, 22, 22, 27, 27, 19, 10, 20,
159 -17, 20, 32, 41, 58, 25, 30, 0,
160 -20, 6, 9, 49, 47, 35, 19, 9,
161 3, 22, 24, 45, 57, 40, 57, 36,
162 -18, 28, 19, 47, 31, 34, 39, 23,
163 -16, -27, 15, 6, 9, 17, 10, 5,
164 -22, -23, -30, -16, -16, -23, -36, -32,
165 -33, -28, -22, -43, -5, -32, -20, -41,
166 );
167  
168 23 my @op_king_table = (
169 -65, 23, 16, -15, -56, -34, 2, 13,
170 29, -1, -20, -7, -8, -4, -38, -29,
171 -9, 24, 2, -16, -20, 6, 22, -22,
172 -17, -20, -12, -27, -30, -25, -14, -36,
173 -49, -1, -27, -39, -46, -44, -33, -51,
174 -14, -14, -22, -46, -44, -30, -15, -27,
175 1, 7, -8, -64, -43, -16, 9, 8,
176 -15, 36, 12, -54, 8, -28, 24, 14,
177 );
178  
179 23 my @eg_king_table = (
180 -74, -35, -18, -18, -11, 15, 4, -17,
181 -12, 17, 14, 17, 17, 38, 23, 11,
182 10, 17, 23, 15, 20, 45, 44, 13,
183 -8, 22, 24, 27, 26, 33, 26, 3,
184 -18, -4, 21, 24, 27, 23, 9, -11,
185 -19, -3, 11, 21, 23, 16, 7, -9,
186 -27, -11, 4, 13, 14, 4, -5, -17,
187 -53, -34, -21, -11, -28, -14, -24, -43
188 );
189  
190 23 my @op_pesto_table = (
191 undef,
192 \@op_pawn_table,
193 \@op_knight_table,
194 \@op_bishop_table,
195 \@op_rook_table,
196 \@op_queen_table,
197 \@op_king_table
198 );
199  
200 23 my @eg_pesto_table = (
201 undef,
202 \@eg_pawn_table,
203 \@eg_knight_table,
204 \@eg_bishop_table,
205 \@eg_rook_table,
206 \@eg_queen_table,
207 \@eg_king_table
208 );
209  
210 23 my @op_table;
211 my @eg_table;
212  
213 # The required signature changes for the castlings.
214 23 my @castling_rook_zk_updates;
215  
216 # The indices are the to squares of the king.
217 23 my @zk_pieces = Chess::Plisco->_zkPieces;
218 23 $castling_rook_zk_updates[CP_C1] = $zk_pieces[384] ^ $zk_pieces[387];
219 23 $castling_rook_zk_updates[CP_G1] = $zk_pieces[389] ^ $zk_pieces[391];
220 23 $castling_rook_zk_updates[CP_C8] = $zk_pieces[504] ^ $zk_pieces[507];
221 23 $castling_rook_zk_updates[CP_G8] = $zk_pieces[509] ^ $zk_pieces[511];
222  
223 23 my @zk_ep_files = Chess::Plisco->_zkEpFiles;
224 23 my @zk_castling = Chess::Plisco->_zkCastling;
225 23 my $zk_colour = Chess::Plisco->_zkColour;
226  
227 # Init tables.
228 23 ▶for (my $piece = CP_PAWN; $piece <= CP_KING; ++$piece) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece <= 6) 138 23 161
229 138 ▶ for (my $shift = 0; $shift < 64; ++$shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shift < 64) 8832 138 8970
230 8832 my $windex = (CP_WHITE << 9) | ($piece << 6) | $shift;
231 8832 my $bindex = (CP_BLACK << 9) | ($piece << 6) | $shift;
232 8832 $op_table[$windex] = $op_value[$piece] + $op_pesto_table[$piece]->[$shift ^ 56];
233 8832 $eg_table[$windex] = $eg_value[$piece] + $eg_pesto_table[$piece]->[$shift ^ 56];
234 8832 $op_table[$bindex] = $op_value[$piece] + $op_pesto_table[$piece]->[$shift];
235 8832 $eg_table[$bindex] = $eg_value[$piece] + $eg_pesto_table[$piece]->[$shift];
236 }
237 }
238  
239 23 my @pieces = (' ', 'P', 'N', 'B', 'R', 'Q', 'K');
240 23 ▶for (my $i = 0; $i < @op_table; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @op_table) 22080 23 22103
241 22080 my $op_score = $op_table[$i];
242 22080 ▶ next if !defined $op_score;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op_score 4416 17664 22080
243 17664 my $eg_score = $eg_table[$i];
244 17664 my $shift = $i & 0x3f;
245 17664 my $piece = ($i >> 6) & 0x7;
246 17664 my $colour = ($i >> 9);
247 17664 my $piece_char = $pieces[$piece];
248 17664 ▶ $piece_char = lc $piece_char if $colour;
Branch: 1 branch
BranchTrueFalseTotal
if $colour 8832 8832 17664
249 17664 my $square = Chess::Plisco->shiftToSquare($shift);
250 }
251  
252 # For all combinations of victim and promotion piece, calculate the change in
253 # game phase. These values are positive and meant to be added to the phase;
254 23 my @move_phase_deltas = (0) x 369;
255 23 foreach my $victim (CP_NO_PIECE, CP_PAWN .. CP_QUEEN) {
256 138 foreach my $promote (CP_NO_PIECE, CP_KNIGHT .. CP_QUEEN) {
257 690 ▶ next if $promote && $victim == CP_PAWN;
Branch: 1 branch
BranchTrueFalseTotal
if $promote and $victim == 1 92 598 690
Condition100%3/3
$promote and $victim == 1
!ll&&!rl&&r
13846092
MC/DC100%2/2
$promote and $victim == 1
$promote $victim == 1
Truth table100%3/3
A and B
A= $promoteB= $victim == 1
ABresult
0X0
100
111
258 598 ▶ next if !$victim && !$promote;
Branch: 1 branch
BranchTrueFalseTotal
unless $victim or $promote 23 575 598
Condition100%3/3
$victim or $promote
l!l&&r!l&&!r
4839223
MC/DC100%2/2
$victim or $promote
$victim $promote
Truth table100%3/3
A or B
A= $victimB= $promote
ABresult
1X1
011
000
259 575 my $delta = -PHASE_INC->[$victim];
260 575 ▶ if ($promote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($promote) 460 115 575
261 460 $delta -= (Chess::Plisco::Engine::Position::PAWN_PHASE
262 - PHASE_INC->[$promote]);
263 }
264 575 $move_phase_deltas[($victim << 3) | $promote] = $delta;
265 }
266 }
267  
268 # Lookup tables for the resulting opening and endgame scores for each
269 # possible move.
270 23 my @opening_deltas;
271 my @endgame_deltas;
272  
273 23 foreach my $move (Chess::Plisco->moveNumbers) {
274 1043188 my $is_ep;
275 1043188 my $colour = 1 & ($move >> CP_MOVE_COLOUR_OFFSET);
276 1043188 my $captured = 0x7 & ($move >> CP_MOVE_CAPTURED_OFFSET);
277 1043188 ▶ if ($captured == CP_KING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($captured == 6) 644 1042544 1043188
278 644 $captured = CP_PAWN;
279 644 $is_ep = 1;
280 }
281 1043188 my ($to, $from, $promote, $piece) = (
282 Chess::Plisco->moveTo($move),
283 Chess::Plisco->moveFrom($move),
284 Chess::Plisco->movePromote($move),
285 Chess::Plisco->movePiece($move),
286 );
287  
288 1043188 my $from_index = ($colour << 9) | ($piece << 6) | $from;
289 1043188 my $to_index = ($colour << 9) | ($piece << 6) | $to;
290 1043188 my $opening_delta = $op_table[$from_index] - $op_table[$to_index];
291 1043188 my $endgame_delta = $eg_table[$from_index] - $eg_table[$to_index];
292 1043188 ▶ if ($is_ep) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_ep) { } 644 1042544 1043188
elsif ($captured) { } 870044 172500 1042544
293 644 my $ep_to;
294 644 ▶ if ($colour == CP_WHITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour == 0) { } 322 322 644
295 322 $ep_to = $to - 8;
296 } else {
297 322 $ep_to = $to + 8;
298 }
299 644 my $ep_index = ($colour << 9) | (CP_PAWN) << 6 | $ep_to;
300 644 $opening_delta -= $op_table[$ep_index];
301 644 $endgame_delta -= $eg_table[$ep_index];
302 } elsif ($captured) {
303 # The captured piece must be viewed from the other side.
304 870044 my $captured_index = ((!$colour) << 9) | ($captured << 6) | $to;
305 870044 $opening_delta -= $op_table[$captured_index];
306 870044 $endgame_delta -= $eg_table[$captured_index];
307 }
308  
309 1043188 ▶ if ($promote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($promote) 11776 1031412 1043188
310 11776 my $promote_index = ($colour << 9) | ($promote << 6) | $to;
311 11776 my $promote_pawn_index = ($colour << 9) | (CP_PAWN << 6) | $to;
312 11776 $opening_delta -= $op_table[$promote_index]
313 - $op_table[$promote_pawn_index];
314 11776 $endgame_delta -= $eg_table[$promote_index]
315 - $eg_table[$promote_pawn_index];
316 }
317  
318 # Handle castlings.
319 1043188 ▶ if (CP_KING == $piece && CP_E8 == $from) {
Branch: 2 branches
BranchTrueFalseTotal
if (6 == $piece and 60 == $from) { } 1426 1041762 1043188
elsif (6 == $piece and 4 == $from) { } 1426 1967512 1968938
Condition100%3/3
6 == $piece and 60 == $from
!ll&&!rl&&r
9271761145861426
MC/DC100%2/2
6 == $piece and 60 == $from
6 == $piece 60 == $from
Truth table100%3/3
A and B
A= 6 == $pieceB= 60 == $from
ABresult
0X0
100
111
320 1426 ▶ if (CP_C8 == $to) {
Branch: 2 branches
BranchTrueFalseTotal
if (58 == $to) { } 23 1403 1426
elsif (62 == $to) { } 23 1380 1403
321 23 my $rook_a8_index = (CP_BLACK << 9) | (CP_ROOK << 6) | CP_A8;
322 23 my $rook_d8_index = (CP_BLACK << 9) | (CP_ROOK << 6) | CP_D8;
323 23 $opening_delta -= $op_table[$rook_d8_index]
324 - $op_table[$rook_a8_index];
325 23 $endgame_delta -= $eg_table[$rook_d8_index]
326 - $eg_table[$rook_a8_index];
327 } elsif (CP_G8 == $to) {
328 23 my $rook_h8_index = (CP_BLACK << 9) | (CP_ROOK << 6) | CP_H8;
329 23 my $rook_f8_index = (CP_BLACK << 9) | (CP_ROOK << 6) | CP_F8;
330 23 $opening_delta -= $op_table[$rook_f8_index]
331 - $op_table[$rook_h8_index];
332 23 $endgame_delta -= $eg_table[$rook_f8_index]
333 - $eg_table[$rook_h8_index];
334 }
335 ▶ } elsif (CP_KING == $piece && CP_E1 == $from) {
Condition100%3/3
6 == $piece and 4 == $from
!ll&&!rl&&r
9271761131601426
MC/DC100%2/2
6 == $piece and 4 == $from
6 == $piece 4 == $from
Truth table100%3/3
A and B
A= 6 == $pieceB= 4 == $from
ABresult
0X0
100
111
336 1426 ▶ if (CP_C1 == $to) {
Branch: 2 branches
BranchTrueFalseTotal
if (2 == $to) { } 23 1403 1426
elsif (6 == $to) { } 23 1380 1403
337 23 my $rook_a1_index = (CP_WHITE << 9) | (CP_ROOK << 6) | CP_A1;
338 23 my $rook_d1_index = (CP_WHITE << 9) | (CP_ROOK << 6) | CP_D1;
339 23 $opening_delta -= $op_table[$rook_d1_index]
340 - $op_table[$rook_a1_index];
341 23 $endgame_delta -= $eg_table[$rook_d1_index]
342 - $eg_table[$rook_a1_index];
343 } elsif (CP_G1 == $to) {
344 23 my $rook_h1_index = (CP_WHITE << 9) | (CP_ROOK << 6) | CP_H1;
345 23 my $rook_f1_index = (CP_WHITE << 9) | (CP_ROOK << 6) | CP_F1;
346 23 $opening_delta -= $op_table[$rook_f1_index]
347 - $op_table[$rook_h1_index];
348 23 $endgame_delta -= $eg_table[$rook_f1_index]
349 - $eg_table[$rook_h1_index];
350 }
351 }
352  
353 1043188 ▶ $opening_deltas[$move] = $colour ? $opening_delta : -$opening_delta;
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 521594 521594 1043188
354 1043188 ▶ $endgame_deltas[$move] = $colour ? $endgame_delta : -$endgame_delta;
Branch: 1 branch
BranchTrueFalseTotal
$colour ? : 521594 521594 1043188
355 }
356  
357  
358  
359 3249 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
360 3249 my ($class, @args) = @_;
361  
362 3249 my $self = $class->SUPER::new(@args);
363  
364 3249 $self->__init;
365  
366 3249 return $self;
367 }
368  
369 2862 ▶sub newFromFEN {
Subroutine newFromFEN: called CC=1 SCAR=0.0
Pod documented
370 2862 my ($class, @args) = @_;
371  
372 2862 my $self = $class->SUPER::newFromFEN(@args);
373  
374 2862 $self->__init;
375  
376 2862 return $self;
377 }
378  
379 6111 ▶sub __init {
Subroutine __init: called CC=5 SCAR=16.1
380 6111 my ($self) = @_;
381  
382  
383 6111 my $op_phase = 0;
384  
385 6111 my $op_score = 0;
386 6111 my $eg_score = 0;
387  
388 6111 my $white = $self->[CP_POS_WHITE_PIECES];
389 6111 my $black = $self->[CP_POS_BLACK_PIECES];
390 6111 my $popcount;
391 6111 ▶ { my $_b = $white | $black; for ($popcount = 0; $_b; ++$popcount) { $_b &= $_b - 1; } };
Branch: 1 branch
BranchTrueFalseTotal
if ($_b) 142778 6111 148889
392 6111 $self->[CP_POS_POPCOUNT] = $popcount;
393  
394 6111 foreach my $piece (CP_PAWN .. CP_KING) {
395 36666 my $pieces = $self->[$piece];
396 36666 my $white_pieces = $pieces & $white;
397 36666 my $black_pieces = $pieces & $black;
398 36666 my $phase_inc = PHASE_INC->[$piece];
399 36666 ▶ while ($white_pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($white_pieces) 71406 36666 108072
400 71406 my $shift = (do { my $B = $white_pieces & -$white_pieces; 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);});
401 71406 my $idx = (CP_WHITE << 9) | ($piece << 6) | $shift;
402 71406 $op_score += $op_table[$idx];
403 71406 $eg_score += $eg_table[$idx];
404 71406 $white_pieces = (($white_pieces) & (($white_pieces) - 1));
405 71406 $op_phase += $phase_inc;
406 }
407 36666 ▶ while ($black_pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($black_pieces) 71372 36666 108038
408 71372 my $shift = (do { my $B = $black_pieces & -$black_pieces; 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);});
409 71372 my $idx = (CP_BLACK << 9) | ($piece << 6) | $shift;
410 71372 $op_score -= $op_table[$idx];
411 71372 $eg_score -= $eg_table[$idx];
412 71372 $black_pieces = (($black_pieces) & (($black_pieces) - 1));
413 71372 $op_phase += $phase_inc;
414 }
415 }
416  
417 6111 $self->[CP_POS_SIGNATURE] = $self->SUPER::signature();
418  
419 6111 $self->[CP_POS_OPENING_SCORE] = $op_score;
420 6111 $self->[CP_POS_ENDGAME_SCORE] = $eg_score;
421  
422 6111 $self->[CP_POS_GAME_PHASE] = $op_phase;
423 6111 $self->[CP_POS_REVERSIBLE_CLOCK] = $self->[CP_POS_HALFMOVE_CLOCK];
424  
425 6111 return $self;
426 }
427  
428 1656472 ▶sub move {
Subroutine move: called CC=15 SCAR=27.1
Pod documented
429 1656472 my ($self, $move) = @_;
430  
431 1656472 my $castling = $self->[CP_POS_CASTLING_RIGHTS];
432 1656472 my $to_move = $self->[CP_POS_TURN];
433 1656472 my $ep_shift = $self->[CP_POS_EN_PASSANT_SHIFT];
434 1656472 ▶ my $zk_update = $ep_shift ? ($zk_ep_files[$ep_shift & 7]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ep_shift ? : 2475 1653997 1656472
435  
436 1656472 ▶ my $state = $self->SUPER::move($move) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $self->SUPER::move($move) or return 1656472 0 1656472
437 1656472 $move = $self->[CP_POS_LAST_MOVE];
438  
439 1656472 my $piece = (($move) & 0x7);
440 1656472 my $from = ((($move) >> 9) & 0x3f);
441 1656472 my $to = ((($move) >> 15) & 0x3f);
442 1656472 ▶ if ($piece == CP_KING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece == 6) 288913 1367559 1656472
443 288913 ▶ if ((($from - $to) & 0x3) == 0x2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($from - $to & 3) == 2) 62 288851 288913
444 # Castling. FIXME! Maybe use a lookup table instead.
445 62 $zk_update ^= $castling_rook_zk_updates[$to];
446 }
447 }
448  
449 1656472 my $captured = ((($move) >> 3) & 0x7);
450 1656472 ▶ if ($captured != CP_NO_PIECE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($captured != 0) 1036612 619860 1656472
451 1036612 --$self->[CP_POS_POPCOUNT];
452 1036612 ▶ if ($piece == CP_PAWN && $ep_shift && $to == $ep_shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece == 1 and $ep_shift and $to == $ep_shift) { } 26 1036586 1036612
Condition100%6/6
$piece == 1 and $ep_shift and $to == $ep_shift
!ll&&!rl&&r
10364909626
$piece == 1 and $ep_shift
!ll&&!rl&&r
910067126423122
MC/DC100%3/3
$piece == 1 and $ep_shift and $to == $ep_shift
$piece == 1 $ep_shift $to == $ep_shift
Truth table100%4/4
A and B and C
A= $piece == 1B= $ep_shiftC= $to == $ep_shift
ABCresult
0XX0
10X0
1100
1111
453 # En passant. The captured piece is not on the to square.
454 26 my $ep_file = $to & 0x7;
455 26 my $captured_to = 24 + !$to_move * 8 + $ep_file;
456 26 $zk_update ^= $zk_pieces[(((CP_PAWN) << 7) | ((!$to_move) << 6) | ($captured_to)) - 128];
457 } else {
458 1036586 $zk_update ^= $zk_pieces[((($captured) << 7) | ((!$to_move) << 6) | ($to)) - 128];
459 }
460 }
461  
462 1656472 $zk_update ^= $zk_pieces[((($piece) << 7) | (($to_move) << 6) | ($from)) - 128];
463 1656472 $zk_update ^= $zk_pieces[((($piece) << 7) | (($to_move) << 6) | ($to)) - 128];
464  
465 1656472 my $promote = ((($move) >> 6) & 0x7);
466 1656472 ▶ if ($promote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($promote) 124012 1532460 1656472
467 124012 $zk_update ^= $zk_pieces[(((CP_PAWN) << 7) | (($to_move) << 6) | ($to)) - 128];
468 124012 $zk_update ^= $zk_pieces[((($promote) << 7) | (($to_move) << 6) | ($to)) - 128];
469 }
470  
471 1656472 ▶ if ($piece == CP_PAWN && (!(($to - $from) & 0x9))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece == 1 and not $to - $from & 9) 3924 1652548 1656472
Condition100%3/3
$piece == 1 and not $to - $from & 9
!ll&&!rl&&r
14165382360103924
MC/DC100%2/2
$piece == 1 and not $to - $from & 9
$piece == 1 not $to - $from & 9
Truth table100%3/3
A and not(B)
A= $piece == 1B= not $to - $from & 9
ABresult
0X0
100
111
472 3924 $zk_update ^= $zk_ep_files[$from & 0x7];
473 }
474  
475 1656472 $zk_update ^= $zk_colour;
476  
477 1656472 $self->[CP_POS_GAME_PHASE] += $move_phase_deltas[
478 ($captured << 3) | $promote
479 ];
480  
481 1656472 my $score_index = ($move & 0x3fffff & ~(1 << (CP_MOVE_COLOUR_OFFSET))) | ($to_move << (CP_MOVE_COLOUR_OFFSET));
482 1656472 $self->[CP_POS_OPENING_SCORE] += $opening_deltas[$score_index];
483 1656472 $self->[CP_POS_ENDGAME_SCORE] += $endgame_deltas[$score_index];
484  
485 1656472 my $new_castling = $self->[CP_POS_CASTLING_RIGHTS];
486 1656472 my $castling_changed = $new_castling != $castling;
487 1656472 ▶ if ($castling_changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($castling_changed) 158 1656314 1656472
488 158 $zk_update ^= $zk_castling[$castling]
489 ^ $zk_castling[$new_castling];
490 }
491  
492 1656472 $self->[CP_POS_SIGNATURE] ^= $zk_update;
493  
494 1656472 ▶ if (!($self->[CP_POS_HALFMOVE_CLOCK])
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->[11] or $castling_changed) { } 1150155 506317 1656472
Condition100%3/3
not $self->[11] or $castling_changed
l!l&&r!l&&!r
1150001154506317
MC/DC100%2/2
not $self->[11] or $castling_changed
not $self->[11] $castling_changed
Truth table100%3/3
not(A) or B
A= not $self->[11]B= $castling_changed
ABresult
1X1
011
000
495 || $castling_changed) {
496 1150155 $self->[CP_POS_REVERSIBLE_CLOCK] = 0;
497 } else {
498 506317 ++$self->[CP_POS_REVERSIBLE_CLOCK];
499 }
500  
501 1656472 return $state;
502 }
503  
504 1252632 ▶sub evaluate {
Subroutine evaluate: called CC=10 SCAR=23.0
Pod undocumented
505 1252632 my ($self) = @_;
506  
507 1252632 my $material = $self->[CP_POS_MATERIAL];
508 1252632 my $white_pieces = $self->[CP_POS_WHITE_PIECES];
509 1252632 my $black_pieces = $self->[CP_POS_BLACK_PIECES];
510 1252632 my $pawns = $self->[CP_POS_PAWNS];
511 1252632 my $knights = $self->[CP_POS_KNIGHTS];
512 1252632 my $bishops = $self->[CP_POS_BISHOPS];
513 1252632 my $rooks = $self->[CP_POS_ROOKS];
514 1252632 my $queens = $self->[CP_POS_QUEENS];
515 1252632 my $kings = $self->[CP_POS_KINGS];
516  
517 # We simply assume that a position without pawns is in general a draw.
518 # If one side is a minor piece ahead, it is considered a draw, when there
519 # are no rooks or queens on the board. Important exception is KBB vs KN.
520 # But in that case the material delta is B + B - N which is greater
521 # than B. On the other hand KBB vs KB is a draw and the material balance
522 # in that case is exactly one bishop.
523 # These simple formulas do not take into account that there may be more
524 # than two knights or bishops for one side on the board but in the
525 # exceptional case that this happens, the result would be close enough
526 # anyway.
527 #
528 # FIXME! Just call insufficientMaterial?
529 1252632 ▶ if (!$pawns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pawns) 5142 1247490 1252632
530 5142 my $delta = (do { my $mask = $material >> CP_INT_SIZE * CP_CHAR_BIT - 1; ($material + $mask) ^ $mask;});
531 5142 ▶ if ($delta < CP_PAWN_VALUE
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < 100 or not $rooks || $queens and $delta <= 330 || $delta == 640 || $delta == 650) 226 4916 5142
Condition100%15/15
$delta < 100 or not $rooks || $queens and $delta <= 330 || $delta == 640 || $delta == 650
l!l&&r!l&&!r
158684916
not $rooks || $queens and $delta <= 330 || $delta == 640 || $delta == 650
!ll&&!rl&&r
48744268
$rooks || $queens
l!l&&r!l&&!r
4506368110
$delta <= 330 or $delta == 640 or $delta == 650
l!l&&r!l&&!r
383042
$delta <= 330 or $delta == 640
l!l&&r!l&&!r
271172
MC/DC100%6/6
$delta < 100 or not $rooks || $queens and $delta <= 330 || $delta == 640 || $delta == 650
$delta < 100 $rooks $queens $delta <= 330 $delta == 640 $delta == 650
Truth table100%7/7
A or not(B || C) and D or E or F
A= $delta < 100B= $rooksC= $queensD= $delta <= 330E= $delta == 640F= $delta == 650
ABCDEFresult
1XXXXX1
0001XX1
00001X1
0000011
01XXXX0
001XXX0
0000000
532 || (!$rooks && !$queens
533 && (($delta <= CP_BISHOP_VALUE)
534 || ($delta == 2 * CP_KNIGHT_VALUE)
535 || ($delta == CP_KNIGHT_VALUE + CP_BISHOP_VALUE)))) {
536 226 return 0;
537 }
538 }
539  
540 1252406 my $op_phase = $self->[CP_POS_GAME_PHASE];
541  
542 1252406 ▶ $op_phase = TOTAL_PHASE if $op_phase > TOTAL_PHASE;
Branch: 1 branch
BranchTrueFalseTotal
if $op_phase > 24 13188 1239218 1252406
543 1252406 my $eg_phase = TOTAL_PHASE - $op_phase;
544  
545 1252406 my $score = ($self->[CP_POS_OPENING_SCORE] * $op_phase
546 + $self->[CP_POS_ENDGAME_SCORE] * $eg_phase) / TOTAL_PHASE;
547  
548 1252406 ▶ return ($self->[CP_POS_TURN]) ? -$score : $score;
Branch: 1 branch
BranchTrueFalseTotal
$self->[12] ? : 967907 284499 1252406
549 }
550  
551  
552  
553 0 ▶sub openingDelta {
Subroutine openingDelta: not called CC=1 SCAR=6.9
Pod undocumented
554 0 my ($self, $index) = @_;
555  
556 0 return $opening_deltas[$index];
557 }
558  
559 0 ▶sub endgameDelta {
Subroutine endgameDelta: not called CC=1 SCAR=6.9
Pod undocumented
560 0 my ($self, $index) = @_;
561  
562 0 return $endgame_deltas[$index];
563 }
564  
565 # This is only for debugging.
566 0 ▶sub formatMoves {
Subroutine formatMoves: not called CC=2 SCAR=17.9
Pod undocumented
567 0 my ($self, @moves) = @_;
568  
569 0 my @formatted;
570 0 foreach my $move (@moves) {
571 0 push @formatted, $self->moveCoordinateNotation($move);
572 }
573  
574 0 return @formatted;
575 }
576  
577 # And this is only implemented for testing.
578 12392 ▶sub signature {
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
579 12392 shift->[CP_POS_SIGNATURE];
580 }
581  
582 23 1;
« Plisco/EPD/Record.pm ↑ Summary Plisco/Engine/TranspositionTable.pm »