Skip to content

Commit d5e73d5

Browse files
author
syzygy
committed
Updated to "Retire KingDanger array".
official-stockfish/Stockfish@01f2466
1 parent 011494f commit d5e73d5

File tree

9 files changed

+95
-107
lines changed

9 files changed

+95
-107
lines changed

src/bitboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void bitboards_init();
3232
void print_pretty(Bitboard b);
3333

3434
#define DarkSquares 0xAA55AA55AA55AA55ULL
35-
#define LightSquares 0x55AA55AA55AA55AAULL
35+
#define LightSquares (~DarkSquares)
3636

3737
#define FileABB 0x0101010101010101ULL
3838
#define FileBBB (FileABB << 1)

src/evaluate.c

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,15 @@ static const Score TrappedBishopA1H1 = S(50, 50);
216216
#undef S
217217
#undef V
218218

219-
// King danger constants and variables. The king danger scores are looked-up
220-
// in KingDanger[]. Various little "meta-bonuses" measuring the strength
221-
// of the enemy attack are added up into an integer, which is used as an
222-
// index to KingDanger[].
223-
static Score KingDanger[400];
224-
225219
// KingAttackWeights[PieceType] contains king attack weights by piece type
226-
static const int KingAttackWeights[8] = { 0, 0, 7, 5, 4, 1 };
220+
static const int KingAttackWeights[8] = { 0, 0, 78, 56, 45, 11 };
227221

228222
// Penalties for enemy's safe checks
229-
#define QueenContactCheck 89
230-
#define QueenCheck 62
231-
#define RookCheck 57
232-
#define BishopCheck 48
233-
#define KnightCheck 78
223+
#define QueenContactCheck 997
224+
#define QueenCheck 695
225+
#define RookCheck 638
226+
#define BishopCheck 538
227+
#define KnightCheck 874
234228

235229

236230
// eval_init() initializes king and attack bitboards for a given color
@@ -410,7 +404,7 @@ INLINE Score evaluate_king(Pos *pos, EvalInfo *ei, int Us)
410404
const int Up = (Us == WHITE ? DELTA_N : DELTA_S);
411405

412406
Bitboard undefended, b, b1, b2, safe, other;
413-
int attackUnits;
407+
int kingDanger;
414408
const Square ksq = square_of(Us, KING);
415409

416410
// King shelter and enemy pawns storm
@@ -428,24 +422,24 @@ INLINE Score evaluate_king(Pos *pos, EvalInfo *ei, int Us)
428422
b = ei->attackedBy[Them][0] & ~ei->attackedBy[Us][0]
429423
& ei->kingRing[Us] & ~pieces_c(Them);
430424

431-
// Initialize the 'attackUnits' variable, which is used later on as an
432-
// index into the KingDanger[] array. The initial value is based on the
425+
// Initialize the 'kingDanger' variable, which will be transformed
426+
// later into a king danger score. The initial value is based on the
433427
// number and types of the enemy's attacking pieces, the number of
434428
// attacked and undefended squares around our king and the quality of
435429
// the pawn shelter (current 'score' value).
436-
attackUnits = min(72, ei->kingAttackersCount[Them] * ei->kingAttackersWeight[Them])
437-
+ 9 * ei->kingAdjacentZoneAttacksCount[Them]
438-
+ 21 * popcount(undefended)
439-
+ 12 * (popcount(b) + !!ei->pinnedPieces[Us])
440-
- 64 * !pieces_cp(Them, QUEEN)
441-
- mg_value(score) / 8;
430+
kingDanger = min(807, ei->kingAttackersCount[Them] * ei->kingAttackersWeight[Them])
431+
+ 101 * ei->kingAdjacentZoneAttacksCount[Them]
432+
+ 235 * popcount(undefended)
433+
+ 134 * (popcount(b) + !!ei->pinnedPieces[Us])
434+
- 717 * !pieces_cp(Them, QUEEN)
435+
- 7 * mg_value(score) / 5 - 5;
442436

443437
// Analyse the enemy's safe queen contact checks. Firstly, find the
444438
// undefended squares around the king reachable by the enemy queen...
445439
b = undefended & ei->attackedBy[Them][QUEEN] & ~pieces_c(Them);
446440

447441
// ...and keep squares supported by another enemy piece
448-
attackUnits += QueenContactCheck * popcount(b & ei->attackedBy2[Them]);
442+
kingDanger += QueenContactCheck * popcount(b & ei->attackedBy2[Them]);
449443

450444
// Analyse the safe enemy's checks which are possible on next move...
451445
safe = ~(ei->attackedBy[Us][0] | pieces_c(Them));
@@ -460,7 +454,7 @@ INLINE Score evaluate_king(Pos *pos, EvalInfo *ei, int Us)
460454

461455
// Enemy queen safe checks
462456
if ((b1 | b2) & ei->attackedBy[Them][QUEEN] & safe) {
463-
attackUnits += QueenCheck;
457+
kingDanger += QueenCheck;
464458
score -= SafeCheck;
465459
}
466460

@@ -472,7 +466,7 @@ INLINE Score evaluate_king(Pos *pos, EvalInfo *ei, int Us)
472466

473467
// Enemy rooks safe and other checks
474468
if (b1 & ei->attackedBy[Them][ROOK] & safe) {
475-
attackUnits += RookCheck;
469+
kingDanger += RookCheck;
476470
score -= SafeCheck;
477471
}
478472

@@ -481,7 +475,7 @@ INLINE Score evaluate_king(Pos *pos, EvalInfo *ei, int Us)
481475

482476
// Enemy bishops safe and other checks
483477
if (b2 & ei->attackedBy[Them][BISHOP] & safe) {
484-
attackUnits += BishopCheck;
478+
kingDanger += BishopCheck;
485479
score -= SafeCheck;
486480
}
487481

@@ -491,16 +485,17 @@ INLINE Score evaluate_king(Pos *pos, EvalInfo *ei, int Us)
491485
// Enemy knights safe and other checks
492486
b = attacks_from_knight(ksq) & ei->attackedBy[Them][KNIGHT];
493487
if (b & safe) {
494-
attackUnits += KnightCheck;
488+
kingDanger += KnightCheck;
495489
score -= SafeCheck;
496490
}
497491

498492
else if (b & other)
499493
score -= OtherCheck;
500494

495+
// Compute the king danger score and subtract it from the evaluation.
501496
// Finally, extract the king danger score from the KingDanger[]
502-
// array and subtract the score from the evaluation.
503-
score -= KingDanger[max(min(attackUnits, 399), 0)];
497+
if (kingDanger > 0)
498+
score -= make_score(min(kingDanger * kingDanger / 4096, 2 * BishopValueMg), 0);
504499
}
505500

506501
// King tropism: firstly, find squares that we attack in the enemy king flank
@@ -930,17 +925,3 @@ void eval_trace(Pos *pos)
930925
}
931926
#endif
932927

933-
934-
// eval_init() computes evaluation weights, usually at startup.
935-
936-
void eval_init() {
937-
int MaxSlope = 322;
938-
int Peak = 47410;
939-
int t = 0;
940-
941-
for (int i = 0; i < 400; ++i) {
942-
t = min(Peak, min(i * i - 16, t + MaxSlope));
943-
KingDanger[i] = make_score(t * 268 / 7700, 0);
944-
}
945-
}
946-

src/evaluate.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ typedef struct Pos Pos;
77

88
#define Tempo ((Value)20)
99

10-
void eval_init();
1110
void trace(Pos *pos);
1211

1312
//template<bool DoTrace = false>

src/main.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#include "bitboard.h"
2424
#include "endgame.h"
25-
#include "evaluate.h"
2625
#include "pawns.h"
2726
#include "position.h"
2827
#include "search.h"
@@ -40,7 +39,6 @@ int main(int argc, char **argv)
4039
bitboards_init();
4140
bitbases_init();
4241
search_init();
43-
eval_init();
4442
pawn_init();
4543
endgames_init();
4644
threads_init();

src/movegen2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ INLINE ExtMove *generate_pawn_moves(Pos *pos, ExtMove *list, Bitboard target,
142142
// if the pawn is not on the same file as the enemy king, because we
143143
// don't generate captures. Note that a possible discovery check
144144
// promotion has been already generated amongst the captures.
145-
Bitboard dcCandidates = discovered_check_candidates(pos);
145+
Bitboard dcCandidates = blockers_for_king(pos, Them);
146146
if (pawnsNotOn7 & dcCandidates) {
147147
Bitboard dc1 = shift_bb(Up, pawnsNotOn7 & dcCandidates) & emptySquares & ~file_bb_s(st->ksq);
148148
Bitboard dc2 = shift_bb(Up, dc1 & TRank3BB) & emptySquares;
@@ -235,7 +235,7 @@ INLINE ExtMove *generate_moves(Pos *pos, ExtMove *list, int us,
235235
&& !(PseudoAttacks[Pt][from] & target & pos->st->checkSquares[Pt]))
236236
continue;
237237

238-
if (discovered_check_candidates(pos) & sq_bb(from))
238+
if (blockers_for_king(pos, us ^ 1) & sq_bb(from))
239239
continue;
240240
}
241241

src/movepick.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void mp_init_q(Pos *pos, Move ttm, Depth depth, Square s)
118118
else if (depth > DEPTH_QS_RECAPTURES)
119119
st->stage = ST_QSEARCH_WITHOUT_CHECKS;
120120
else {
121-
st->stage = ST_RECAPTURES;
121+
st->stage = ST_RECAPTURES_GEN;
122122
st->recaptureSquare = s;
123123
return;
124124
}
@@ -156,7 +156,7 @@ void mp_init_pc(Pos *pos, Move ttm, Value threshold)
156156
// score() assigns a numerical value to each move in a move list. The moves with
157157
// highest values will be picked first.
158158

159-
void score_captures(Pos *pos)
159+
static void score_captures(Pos *pos)
160160
{
161161
Stack *st = pos->st;
162162

@@ -168,7 +168,7 @@ void score_captures(Pos *pos)
168168
- (Value)(200 * relative_rank_s(pos_stm(), to_sq(m->move)));
169169
}
170170

171-
void score_quiets(Pos *pos)
171+
static void score_quiets(Pos *pos)
172172
{
173173
Stack *st = pos->st;
174174
HistoryStats *history = pos->history;
@@ -177,7 +177,7 @@ void score_quiets(Pos *pos)
177177
CounterMoveStats *cm = (st-1)->counterMoves;
178178
CounterMoveStats *fm = (st-2)->counterMoves;
179179
CounterMoveStats *f2 = (st-4)->counterMoves;
180-
180+
//if(!cm || !fm || !f2)printf("st->ply = %d (%d,%d,%d)\n", st->ply, !!cm, !!fm, !!f2);
181181
int c = pos_stm();
182182

183183
for (ExtMove *m = st->cur; m < st->endMoves; m++)
@@ -188,7 +188,7 @@ void score_quiets(Pos *pos)
188188
+ ft_get(*fromTo, c, m->move);
189189
}
190190

191-
void score_evasions(Pos *pos)
191+
static void score_evasions(Pos *pos)
192192
{
193193
Stack *st = pos->st;
194194
// Try winning and equal captures ordered by MVV/LVA, then non-captures
@@ -227,13 +227,13 @@ Move next_move(Pos *pos)
227227
st->stage++;
228228
return st->ttMove;
229229

230-
case ST_GOOD_CAPTURES:
230+
case ST_CAPTURES_GEN:
231231
st->endBadCaptures = st->cur = (st-1)->endMoves;
232232
st->endMoves = generate_captures(pos, st->cur);
233233
score_captures(pos);
234234
st->stage++;
235235

236-
case ST_GOOD_CAPTURES_2:
236+
case ST_GOOD_CAPTURES:
237237
while (st->cur < st->endMoves) {
238238
move = pick_best(st->cur++, st->endMoves);
239239
if (move != st->ttMove) {
@@ -267,7 +267,7 @@ Move next_move(Pos *pos)
267267
&& !is_capture(pos, move))
268268
return move;
269269

270-
case ST_QUIET:
270+
case ST_QUIET_GEN:
271271
st->cur = st->endBadCaptures;
272272
st->endMoves = generate_quiets(pos, st->cur);
273273
score_quiets(pos);
@@ -278,7 +278,7 @@ Move next_move(Pos *pos)
278278
insertion_sort(st->cur, st->endMoves);
279279
st->stage++;
280280

281-
case ST_QUIET_2:
281+
case ST_QUIET:
282282
while (st->cur < st->endMoves) {
283283
move = (st->cur++)->move;
284284
if ( move != st->ttMove && move != st->killers[0]
@@ -291,33 +291,31 @@ Move next_move(Pos *pos)
291291
case ST_BAD_CAPTURES:
292292
if (st->cur < st->endBadCaptures)
293293
return (st->cur++)->move;
294-
return 0;
294+
break;
295295

296-
case ST_EVASIONS_1:
296+
case ST_ALL_EVASIONS:
297297
st->cur = (st-1)->endMoves;
298298
st->endMoves = generate_evasions(pos, st->cur);
299299
if (st->endMoves - st->cur > 1)
300300
score_evasions(pos);
301301
st->stage = ST_REMAINING;
302-
goto remaining;
303302

304-
case ST_QCAPTURES_CHECKS_1:
305-
case ST_QCAPTURES_NO_CHECKS_1:
306-
st->cur = (st-1)->endMoves;
307-
st->endMoves = generate_captures(pos, st->cur);
308-
score_captures(pos);
309-
st->stage++;
303+
if (st->stage != ST_REMAINING) {
304+
case ST_QCAPTURES_CHECKS_GEN: case ST_QCAPTURES_NO_CHECKS_GEN:
305+
st->cur = (st-1)->endMoves;
306+
st->endMoves = generate_captures(pos, st->cur);
307+
score_captures(pos);
308+
st->stage++;
309+
}
310310

311-
remaining:
312-
case ST_QCAPTURES_CHECKS_2:
313-
case ST_REMAINING:
311+
case ST_QCAPTURES_CHECKS: case ST_REMAINING:
314312
while (st->cur < st->endMoves) {
315313
move = pick_best(st->cur++, st->endMoves);
316314
if (move != st->ttMove)
317315
return move;
318316
}
319-
if (st->stage != ST_QCAPTURES_CHECKS_2)
320-
return 0;
317+
if (st->stage != ST_QCAPTURES_CHECKS)
318+
break;
321319
st->cur = (st-1)->endMoves;
322320
st->endMoves = generate_quiet_checks(pos, st->cur);
323321
st->stage++;
@@ -328,23 +326,23 @@ Move next_move(Pos *pos)
328326
if (move != st->ttMove)
329327
return move;
330328
}
331-
return 0;
329+
break;
332330

333-
case ST_RECAPTURES:
331+
case ST_RECAPTURES_GEN:
334332
st->cur = (st-1)->endMoves;
335333
st->endMoves = generate_captures(pos, st->cur);
336334
score_captures(pos);
337335
st->stage++;
338336

339-
case ST_RECAPTURES_2:
337+
case ST_RECAPTURES:
340338
while (st->cur < st->endMoves) {
341339
move = pick_best(st->cur++, st->endMoves);
342340
if (to_sq(move) == st->recaptureSquare)
343341
return move;
344342
}
345-
return 0;
343+
break;
346344

347-
case ST_PROBCUT_1:
345+
case ST_PROBCUT_GEN:
348346
st->cur = (st-1)->endMoves;
349347
st->endMoves = generate_captures(pos, st->cur);
350348
score_captures(pos);
@@ -356,12 +354,13 @@ Move next_move(Pos *pos)
356354
if (move != st->ttMove && see_test(pos, move, st->threshold + 1))
357355
return move;
358356
}
359-
return 0;
357+
break;
360358

361359
default:
362360
assume(0);
363-
return 0;
364361

365362
}
363+
364+
return 0;
366365
}
367366

0 commit comments

Comments
 (0)