forked from gekkehenker/fire
-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate.cpp
1118 lines (946 loc) · 39.6 KB
/
evaluate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Fire is a freeware UCI chess playing engine authored by Norman Schmidt.
Fire utilizes many state-of-the-art chess programming ideas and techniques
which have been documented in detail at https://www.chessprogramming.org/
and demonstrated via the very strong open-source chess engine Stockfish...
https://github.com/official-stockfish/Stockfish.
Fire is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version.
You should have received a copy of the GNU General Public License with
this program: copying.txt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "evaluate.h"
#include "endgame.h"
#include "bitboard.h"
#include "fire.h"
#include "material.h"
#include "pawn.h"
#include "macro/score.h"
#include "thread.h"
namespace evaluate
{
// init arrays
void init()
{
#ifndef TUNER
// init mobility mult values
constexpr auto mob_factor_p = 270;
constexpr auto mmrq_factor_p = 0;
constexpr auto mmfq_factor_p = 6;
constexpr auto mmc_factor_p = 2;
constexpr auto mmr_factor_p = 0;
constexpr auto mme_factor_p = 5;
constexpr auto mob_factor_b1 = 256;
constexpr auto mob_factor_b2 = 249;
constexpr auto mmrq_factor_b2 = 8;
constexpr auto mmfq_factor_b2 = 3;
constexpr auto mmc_factor_b2 = 0;
constexpr auto mmr_factor_b2 = 3;
constexpr auto mme_factor_b2 = 4;
constexpr auto mob_factor_r = 255;
constexpr auto mmrq_factor_r = 1;
constexpr auto mmfq_factor_r = 5;
constexpr auto mmc_factor_r = 6;
constexpr auto mmr_factor_r = 1;
constexpr auto mme_factor_r = 2;
constexpr auto mob_factor_q = 272;
constexpr auto mmrq_factor_q = 2;
constexpr auto mmfq_factor_q = 4;
constexpr auto mmc_factor_q = 1;
constexpr auto mmr_factor_q = 2;
constexpr auto mme_factor_q = 8;
// init pawn mobility values
constexpr auto pawn_mg_mult = 207.32;
constexpr auto pawn_mg_sub = 417.0;
constexpr auto pawn_eg_mult = 252.68;
constexpr auto pawn_eg_sub = 509.0;
// init bishop mobility values
constexpr auto b1_mg_mult = 92.43;
constexpr auto b1_mg_sub = 171.0;
constexpr auto b1_eg_mult = 104.75;
constexpr auto b1_eg_sub = 194.0;
constexpr auto b2_mg_mult = 221.48;
constexpr auto b2_mg_sub = 374.0;
constexpr auto b2_eg_mult = 203.99;
constexpr auto b2_eg_sub = 344.0;
// init rook mobility values
constexpr auto rook_mg_mult = 125.90;
constexpr auto rook_mg_sub = 190.0;
constexpr auto rook_eg_mult = 218.96;
constexpr auto rook_eg_sub = 331.0;
constexpr auto mob_r_mult = 7;
constexpr auto mob_r_div = 8;
// init queen mobility values
constexpr auto queen_mg_mult = 203.42;
constexpr auto queen_mg_sub = 616.0;
constexpr auto queen_eg_mult = 165.33;
constexpr auto queen_eg_sub = 555.0;
// init distance values
constexpr auto p_k_distance = 1114123;
constexpr auto p_k_distance_mult = 3;
constexpr auto b_k_distance = 65545;
constexpr auto b_k_distance_mult = 3;
// init pawn-bishop color values
constexpr auto pawn_on_bishop_color = -1769515;
constexpr auto pawn_on_other_bishop_color = 3014673;
constexpr auto pawn_file_width_mg = 0;
constexpr auto pawn_file_width_eg = 2;
constexpr auto threats_score = 14418116;
// init passed pawn values
constexpr auto pp_dvd_mgfactor = 46;
constexpr auto pp_dvd_egfactor = 33;
constexpr auto pp_ndvd_mgfactor = 49;
constexpr auto pp_ndvd_egfactor = 34;
constexpr auto pp_fp_base_mg = 1;
constexpr auto pp_fp_base_eg = 3;
constexpr auto pp_fp_mg = 10;
constexpr auto pp_fp_eg = 60;
constexpr auto pp_fp_mul = 272;
constexpr auto pp_fp_div = 256;
constexpr auto pp_as_base_mg = 1;
constexpr auto pp_as_base_eg = 3;
constexpr auto pp_as_mg = 10;
constexpr auto pp_as_eg = 36;
constexpr auto pp_as_mul = 304;
constexpr auto pp_as_div = 256;
constexpr auto pp_ab_base_mg = 1;
constexpr auto pp_ab_base_eg = 3;
constexpr auto pp_support_proximity_factor = 30;
constexpr auto pp_mk_kdfp_factor = 40;
constexpr auto pp_yk_kdfp_factor = 76;
constexpr auto pp_mk_factor = 32;
constexpr auto pp_mk_div = 35;
constexpr auto pp_yk_factor = 32;
constexpr auto pp_yk_div = 35;
#endif
// init mobility mult arrays
for (auto n = 0; n < 64; n++)
{
mob_mult_p[n] = mob_factor_p * mob_mult_const[n] + ((mmrq_factor_p * mob_mult_rank_quad[n] + mmfq_factor_p * mob_mult_file_quad[n]) >> 1)
- mmc_factor_p * mob_mult_center[n] + mmr_factor_p * mob_mult_rank[n] + mme_factor_p * mob_mult_edge[n];
mob_mult_b1[n] = mob_factor_b1;
mob_mult_b2[n] = mob_factor_b2 * mob_mult_const[n] + ((-mmrq_factor_b2 * mob_mult_rank_quad[n] - mmfq_factor_b2 * mob_mult_file_quad[n]) >> 1)
+ mmc_factor_b2 * mob_mult_center[n] - mmr_factor_b2 * mob_mult_rank[n] - mme_factor_b2 * mob_mult_edge[n];
mob_mult_r[n] = mob_factor_r * mob_mult_const[n] + ((mmrq_factor_r * mob_mult_rank_quad[n] - mmfq_factor_r * mob_mult_file_quad[n]) >> 1)
- mmc_factor_r * mob_mult_center[n] - mmr_factor_r * mob_mult_rank[n] - mme_factor_r * mob_mult_edge[n];
mob_mult_q[n] = mob_factor_q * mob_mult_const[n] + ((-mmrq_factor_q * mob_mult_rank_quad[n] + mmfq_factor_q * mob_mult_file_quad[n]) >> 1)
+ mmc_factor_q * mob_mult_center[n] - mmr_factor_q * mob_mult_rank[n] - mme_factor_q * mob_mult_edge[n];
}
// init mobility arrays
for (auto n = 0; n < 256; n++)
{
auto v38 = sqrt(0.125 * n + 1.5) - sqrt(1.5);
mobility_p[n] = make_score(std::lround(v38 * pawn_mg_mult - pawn_mg_sub), std::lround(v38 * pawn_eg_mult - pawn_eg_sub));
mobility_b1[n] = make_score(std::lround(v38 * b1_mg_mult - b1_mg_sub), std::lround(v38 * b1_eg_mult - b1_eg_sub));
mobility_b2[n] = make_score(std::lround(v38 * b2_mg_mult - b2_mg_sub), std::lround(v38 * b2_eg_mult - b2_eg_sub));
mobility_r[n] = make_score(std::lround(v38 * rook_mg_mult - rook_mg_sub) * mob_r_mult / mob_r_div, std::lround(v38 * rook_eg_mult - rook_eg_sub) * mob_r_mult / mob_r_div);
v38 = sqrt(0.25 * n + 1.5) - sqrt(1.5);
mobility_q[n] = make_score(std::lround(v38 * queen_mg_mult - queen_mg_sub), std::lround(v38 * queen_eg_mult - queen_eg_sub));
}
// init distance arrays
for (auto n = 0; n < 8; n++)
{
distance_p_k[n] = static_cast<score>(p_k_distance * (p_k_distance_mult - n));
distance_b_k[n] = static_cast<score>(b_k_distance * (b_k_distance_mult - n));
}
// init pawn-bishop color arrays
for (auto n = 0; n < 9; n++)
{
pawn_on_color_bishop[n] = static_cast<score>(pawn_on_bishop_color * (n - 2));
pawn_other_color_bishop[n] = static_cast<score>(pawn_on_other_bishop_color * (n - 2));
pawn_file_width[n] = make_score(pawn_file_width_mg, pawn_file_width_eg * (n > 5 ? 9 * n - 36 : n * n - 16));
threats[n] = static_cast<score>(threats_score * (n > 1 ? n + 2 : n));
}
// init passed pawn arrays
for (auto n = 0; n <= 5; n++)
{
passed_pawn_dvd[n] = make_score(pp_dvd_mgfactor * (n - 1) * n, pp_dvd_egfactor * (n * n + 1));
passed_pawn_not_dvd[n] = make_score(pp_ndvd_mgfactor * (n - 1) * n, pp_ndvd_egfactor * (n * n + 1));
passed_pawn_free_passage[n] = make_score(pp_fp_base_mg, pp_fp_base_eg) * (n - 1) * n + mul_div(make_score(pp_fp_mg, pp_fp_eg) * (n - 1) * n, pp_fp_mul, pp_fp_div);
passed_pawn_advance_supported[n] = make_score(pp_as_base_mg, pp_as_base_eg) * (n - 1) * n + mul_div(make_score(pp_as_mg, pp_as_eg) * (n - 1) * n, pp_as_mul, pp_as_div);
passed_pawn_advance_blocked[n] = make_score(pp_ab_base_mg, pp_ab_base_eg) * (n - 1) * n;
for (auto distance = 0; distance < 8; distance++)
{
const auto support = pp_support_proximity_factor * passed_pawn_proximity[distance];
const auto free_path = (sqrt(distance + 1.0) - 1.0) * (static_cast<double>(n) - static_cast<double>(1)) * n;
passed_pawn_my_k[n][distance] = make_score(0, (support - std::lround(free_path * pp_mk_kdfp_factor)) * pp_mk_factor / pp_mk_div);
passed_pawn_your_k[n][distance] = make_score(0, (std::lround(free_path * pp_yk_kdfp_factor) - support) * pp_yk_factor / pp_yk_div);
}
}
// init king_danger & safety table array
auto prev = 0;
for (auto n = 0; n < 128; n++)
{
const auto val = king_danger[n];
safety_table[8 * n] = make_score(val, 0);
if (n > 0)
for (auto i = 1; i < 8; i++)
safety_table[8 * n - 8 + i] = make_score((i * val + (8 - i) * prev) / 8, 0);
prev = val;
}
}
// calculate and return piece attack bitboard
template <side me>
uint64_t calculate_attack(const position& pos)
{
auto sq = no_square;
auto attack = pos.attack_from<pt_king>(pos.king(me));
attack |= pawn_attack<me>(pos.pieces(me, pt_pawn));
const auto* p_square = pos.piece_list(me, pt_knight);
while ((sq = *p_square++) != no_square)
attack |= pos.attack_from<pt_knight>(sq);
p_square = pos.piece_list(me, pt_bishop);
while ((sq = *p_square++) != no_square)
attack |= pos.attack_from<pt_bishop>(sq);
p_square = pos.piece_list(me, pt_rook);
while ((sq = *p_square++) != no_square)
attack |= pos.attack_from<pt_rook>(sq);
p_square = pos.piece_list(me, pt_queen);
while ((sq = *p_square++) != no_square)
attack |= pos.attack_from<pt_queen>(sq);
return attack;
}
// calculate scale factor based on material hash, opposite colored bishops, passed pawns, etc
sfactor calculate_scale_factor(const position& pos, const material::mat_hash_entry* material_entry, const int value)
{
#ifndef TUNER
const auto sf_mult = 3;
constexpr auto sf_div = 4;
#endif
const auto strong_side = value > draw_eval ? white : black;
auto scale_factor = material_entry->scale_factor_from_function(pos, strong_side);
if (abs(value) <= bishop_eval && (scale_factor == normal_factor || scale_factor == one_pawn_factor))
{
if (pos.different_color_bishops())
{
if (pos.non_pawn_material(white) == mat_bishop
&& pos.non_pawn_material(black) == mat_bishop)
scale_factor = pos.number(strong_side, pt_pawn) > 1 ? static_cast<sfactor>(50) : static_cast<sfactor>(12);
else
scale_factor = static_cast<sfactor>(scale_factor * sf_mult / sf_div);
}
else if (pos.number(strong_side, pt_pawn) <= 2
&& !pos.is_passed_pawn(~strong_side, pos.king(~strong_side)))
scale_factor = static_cast<sfactor>(58 + 11 * pos.number(strong_side, pt_pawn));
}
return scale_factor;
}
int eval_after_null_move(const int eval)
{
const auto result = -eval + 2 * value_tempo;
return result;
}
template <side me>
inline void eval_init(const position& pos, attack_info& ai, const pawn::pawn_hash_entry* pawn_entry)
{
ai.attack[me][pt_king] = pos.attack_from<pt_king>(pos.king(me));
ai.attack[me][pt_pawn] = pawn_entry->pawn_attack(me);
ai.attack[me][pt_knight] = 0;
ai.attack[me][pt_bishop] = 0;
ai.attack[me][pt_rook] = 0;
ai.attack[me][pt_queen] = 0;
ai.attack[me][pieces_without_king] = ai.attack[me][pt_pawn];
ai.double_attack[me] = 0;
ai.pinned[me] = pos.info()->x_ray[me];
ai.k_zone[me] = king_zone[pos.king(me)];
ai.k_attack_score[me] = 0;
}
template <side me>
inline int eval_bishops(const position& pos, attack_info& ai, const pawn::pawn_hash_entry* pawn_entry)
{
constexpr auto you = me == white ? black : white;
auto score = 0;
#ifndef TUNER
constexpr auto k_zone_attack_bonus = 8;
constexpr auto bishop_in_front_of_king = 7602176;
constexpr auto bishop_in_corner = 6553753;
constexpr auto trapped_bishop_extra = 69731368;
constexpr auto trapped_bishop = 33030648;
constexpr auto bishop_dominates_pawn = 2097182;
#endif
auto squares = pos.pieces(me, pt_bishop);
assert(squares);
if (shift_up<me>(pos.pieces(me, pt_king)) & squares)
score += bishop_in_front_of_king;
if constexpr (me == white)
{
if (squares & bb2(a1, h1))
{
// white bishop fianchetto bonus
if (pos.pieces(me) & squares << 9 & b2)
score -= bishop_in_corner;
if (pos.pieces(me) & squares << 7 & g2)
score -= bishop_in_corner;
}
}
else
{
if (squares & bb2(a8, h8))
{
// black bishop fianchetto bonus
if (pos.pieces(me) & squares >> 7 & b7)
score -= bishop_in_corner;
if (pos.pieces(me) & squares >> 9 & g7)
score -= bishop_in_corner;
}
}
do
{
const auto sq = pop_lsb(&squares);
score += distance_b_k[distance(sq, pos.king(me))];
// bishop pins queen or rook
auto bb_pin_rq = empty_attack[pt_bishop][sq] & pos.pieces(you, pt_rook, pt_queen);
while (bb_pin_rq)
{
const auto square_rq = pop_lsb(&bb_pin_rq);
if (const auto b = bb_between(square_rq, sq) & pos.pieces(); b && !more_than_one(b))
score += bishop_pin[me][pos.piece_on_square(lsb(b))];
}
auto attack = attack_bb_bishop(sq, pos.pieces(pt_pawn));
score += mobility_b1[(popcnt(attack) * mob_mult_b1[relative_square(me, sq)] + 16) / 32];
// bishop trapped underneath pawns
if (pos.pieces(pt_pawn) & trapped_bishop_b3_c2[me][sq])
{
if (pos.pieces(pt_pawn) & trapped_bishop_b3_c2_extra[sq])
score -= trapped_bishop_extra;
else
score -= trapped_bishop;
}
attack = attack_bb_bishop(sq, pos.pieces() ^ pos.pieces(me, pt_queen));
if (attack & ai.k_zone[you])
ai.k_attack_score[me] += k_zone_attack_bonus;
ai.attack[me][pt_bishop] |= attack;
ai.double_attack[me] |= ai.attack[me][pieces_without_king] & attack;
ai.attack[me][pieces_without_king] |= attack;
attack &= ai.mobility_mask[me];
if (ai.pinned[me] & sq)
attack &= bb_between(pos.king(me), sq);
const uint32_t mobility = popcnt(attack);
score += mobility_b2[(mobility * mob_mult_b2[relative_square(me, sq)] + 16) / 32];
const auto pawns_on_color = pawn_on_color_bishop[pawn_entry->pawns_on_color(me, sq)];
score += pawns_on_color;
score += pawn_other_color_bishop[pawn_entry->pawns_not_on_color(me, sq)];
if (const auto squares_same_color = dark_squares & sq ? dark_squares : ~dark_squares; pos.pieces(me, pt_bishop)
& ~squares_same_color && !(squares_same_color & pos.pieces(you, pt_bishop)))
score += pawns_on_color;
if (pos.pieces(you, pt_knight) & bb_b_dominates_p[me][sq])
score += bishop_dominates_pawn;
} while (squares);
return score;
}
inline int eval_initiative(const position& pos, const pawn::pawn_hash_entry* pawn_entry, const int eg)
{
#ifndef TUNER
constexpr auto initiative_mult = 38;
#endif
const auto k_distance = file_distance(pos.king(white), pos.king(black)) - rank_distance(pos.king(white), pos.king(black));
const auto initiative = (2 * pawn_entry->asymmetry + k_distance + 3 * pos.number(eg < 0 ? b_pawn : w_pawn) - 15) * initiative_mult;
const auto bonus = ((eg > 0) - (eg < 0)) * std::max(initiative, -abs(eg >> 1));
return bonus;
}
template <side me>
inline int eval_king_attack(const position& pos, const attack_info& ai, const int attack_score)
{
const auto you = me == white ? black : white;
#ifndef TUNER
const auto k_attack_index_factor = 16;
const auto k_attack_pin_factor = 12;
const auto k_attack_sd_factor = 11;
const auto cspan_safe = 70;
const auto cspan = 30;
const auto csbab_safe = 54;
constexpr auto csbab = 22;
constexpr auto csrar_safe = 70;
constexpr auto csrar = 30;
constexpr auto qcayk_all = 86;
constexpr auto qcayk = 38;
constexpr auto queen_check_bonus = 120;
#endif
auto attack_index = attack_score;
attack_index += k_attack_index_factor * popcnt(ai.attack[you][pt_king] & ai.attack[me][all_pieces] & ~ai.attack[you][pieces_without_king]);
if (ai.pinned[you])
attack_index += k_attack_pin_factor;
attack_index += k_attack_sd_factor * popcnt(shift_down<me>(ai.attack[you][pt_king]) & ~ai.attack[you][all_pieces] & ai.attack[me][all_pieces] & ~pos.pieces(me));
const auto square_k = pos.king(you);
const auto check_ok = ~pos.pieces(me);
const auto check_squares_r = pos.attack_from<pt_rook>(square_k) & check_ok;
const auto check_squares_b = pos.attack_from<pt_bishop>(square_k) & check_ok;
const auto check_squares_p = pos.attack_from<pt_knight>(square_k) & check_ok;
const auto safe_pbr = ~ai.attack[you][all_pieces] | (ai.double_attack[me] & (~ai.double_attack[you] & (ai.attack[you][pt_king] | ai.attack[you][pt_queen])));
if (check_squares_p & ai.attack[me][pt_knight])
{
if (check_squares_p & ai.attack[me][pt_knight] & safe_pbr)
attack_index += cspan_safe;
else
attack_index += cspan;
}
if (check_squares_b & ai.attack[me][pt_bishop])
{
if (check_squares_b & ai.attack[me][pt_bishop] & safe_pbr)
attack_index += csbab_safe;
else
attack_index += csbab;
}
if (check_squares_r & ai.attack[me][pt_rook])
{
if (check_squares_r & ai.attack[me][pt_rook] & safe_pbr)
attack_index += csrar_safe;
else
attack_index += csrar;
}
if (auto queen_check = (check_squares_b | check_squares_r) & ai.attack[me][pt_queen]; queen_check)
{
if (queen_check & ~ai.attack[you][pt_king])
{
if (queen_check & ~ai.attack[you][all_pieces])
attack_index += qcayk_all;
else
attack_index += qcayk;
}
queen_check &= ai.attack[you][pt_king] & (pos.pieces(you, pt_queen) | ai.double_attack[me]) & ~ai.attack[you][pieces_without_king];
if (queen_check)
attack_index += queen_check_bonus;
}
if (attack_index < 0)
attack_index = 0;
if (attack_index > 1000)
attack_index = 1000;
return safety_table[attack_index];
}
template <side me>
inline int eval_knights(const position& pos, attack_info& ai)
{
constexpr auto you = me == white ? black : white;
auto score = 0;
#ifndef TUNER
constexpr auto knight_attack_king = 24;
constexpr auto p_mobility_add = 16;
constexpr auto p_mobility_div = 32;
#endif
auto squares = pos.pieces(me, pt_knight);
assert(squares);
do
{
const auto sq = pop_lsb(&squares);
score += distance_p_k[distance(sq, pos.king(me))];
auto attack = pos.attack_from<pt_knight>(sq);
if (attack & ai.k_zone[you])
ai.k_attack_score[me] += knight_attack_king;
ai.attack[me][pt_knight] |= attack;
ai.double_attack[me] |= ai.attack[me][pieces_without_king] & attack;
ai.attack[me][pieces_without_king] |= attack;
uint32_t mobility = 0;
if (ai.pinned[me] & sq)
mobility = 0;
else
{
attack &= ai.mobility_mask[me];
mobility = popcnt(attack) + popcnt(attack & bb_ranks_forward(me, sq));
}
score += mobility_p[(mobility * mob_mult_p[relative_square(me, sq)] + p_mobility_add) / p_mobility_div];
} while (squares);
return score;
}
template <side me>
inline int eval_passed_pawns(const position& pos, const attack_info& ai, uint64_t bb_passed_pawns)
{
assert(bb_passed_pawns != 0);
constexpr auto you = me == white ? black : white;
#ifndef TUNER
constexpr auto passed_pawn_mk_mult = 3;
constexpr auto passed_pawn_mk_div = 4;
constexpr auto passed_pawn_yk_mult = 3;
constexpr auto passed_pawn_yk_div = 4;
constexpr auto passed_pawn_mk_md_mul = 2;
constexpr auto passed_pawn_mk_md_div = 4;
constexpr auto passed_pawn_yk_md_mul = 2;
constexpr auto passed_pawn_yk_md_div = 4;
constexpr auto bb_behind_passed_pawn_bonus = 6488502;
#endif
auto score = 0;
while (bb_passed_pawns)
{
const auto passed_pawn = pop_lsb(&bb_passed_pawns);
const auto pawn_rank = relative_rank(me, passed_pawn) - 1;
if (pos.non_pawn_material(white) == mat_queen && pos.non_pawn_material(black) == mat_queen)
score += passed_pawn_dvd[pawn_rank];
else
score += passed_pawn_not_dvd[pawn_rank];
if (pawn_rank > 1)
{
const auto square_for_pawn = passed_pawn + pawn_ahead(me);
auto my_distance = distance(square_for_pawn, pos.king(me));
auto your_distance = distance(square_for_pawn, pos.king(you));
score += mul_div(passed_pawn_my_k[pawn_rank][my_distance], passed_pawn_mk_mult, passed_pawn_mk_div);
score += mul_div(passed_pawn_your_k[pawn_rank][your_distance], passed_pawn_yk_mult, passed_pawn_yk_div);
const auto promotion_square = make_square(file_of(passed_pawn), static_cast<rank>(7 * you));
my_distance = distance(promotion_square, pos.king(me));
your_distance = distance(promotion_square, pos.king(you));
score += mul_div(passed_pawn_my_k[pawn_rank][my_distance], passed_pawn_mk_md_mul, passed_pawn_mk_md_div);
score += mul_div(passed_pawn_your_k[pawn_rank][your_distance], passed_pawn_yk_md_mul, passed_pawn_yk_md_div);
if (pawn_rank > 2)
{
const auto bb_behind_passed_pawn = bb_forward(you, passed_pawn);
if (bb_behind_passed_pawn & pos.pieces(me, pt_rook))
score += bb_behind_passed_pawn_bonus;
if (bb_behind_passed_pawn & pos.pieces(you, pt_rook))
score -= bb_behind_passed_pawn_bonus;
}
if (!(pos.pieces() & square_for_pawn))
{
const auto passed_pawn_path = bb_forward(me, passed_pawn);
auto bb_advance_blocked = passed_pawn_path & (pos.pieces(you) | ai.attack[you][all_pieces]);
if (const auto attacked = pos.pieces(you, pt_rook, pt_queen) & bb_forward(you, passed_pawn); attacked)
{
if (const auto sq = front_square(me, attacked); !(pos.pieces() & bb_between(passed_pawn, sq)))
bb_advance_blocked = passed_pawn_path;
}
if (!bb_advance_blocked)
score += passed_pawn_free_passage[pawn_rank];
else if (bb_advance_blocked & ~ai.attack[me][all_pieces])
score += passed_pawn_advance_blocked[pawn_rank];
else
score += passed_pawn_advance_supported[pawn_rank];
}
}
}
return score;
}
template <side me>
inline int eval_queens(const position& pos, attack_info& ai)
{
constexpr auto you = me == white ? black : white;
auto score = 0;
#ifndef TUNER
constexpr auto queen_attack_king = 24;
constexpr auto queen_attack_king_zone = 8;
constexpr auto q_mobility_add = 32;
constexpr auto q_mobility_div = 64;
#endif
auto squares = pos.pieces(me, pt_queen);
assert(squares);
const auto mobility_mask_d = ~(ai.attack[you][pt_bishop] | ai.attack[you][pt_rook]
| pos.pieces(me, pt_king, pt_pawn)
| ai.attack[you][pt_pawn]
| ai.attack[you][pt_knight]);
ai.k_attack_score[me] += queen_attack_king;
do
{
const auto sq = pop_lsb(&squares);
auto attack = attack_bb_rook(sq, pos.pieces() ^ pos.pieces(me, pt_queen))
| attack_bb_bishop(sq, pos.pieces() ^ pos.pieces(me, pt_queen));
if (attack & ai.k_zone[you])
ai.k_attack_score[me] += queen_attack_king_zone;
ai.attack[me][pt_queen] |= attack;
ai.double_attack[me] |= ai.attack[me][pieces_without_king] & attack;
ai.attack[me][pieces_without_king] |= attack;
attack &= mobility_mask_d;
if (ai.pinned[me] & sq)
attack &= bb_between(pos.king(me), sq);
constexpr uint64_t center_square = 0x00003C3C3C3C0000;
const uint32_t mobility = popcnt(attack) + popcnt(attack & center_square);
score += mobility_q[(mobility * mob_mult_q[relative_square(me, sq)] + q_mobility_add) / q_mobility_div];
} while (squares);
return score;
}
template <side me>
inline int eval_rooks(const position& pos, attack_info& ai)
{
constexpr auto you = me == white ? black : white;
auto score = 0;
#ifndef TUNER
constexpr auto uncastled_penalty = 29556897;
constexpr auto rook_attacks_king = 8;
constexpr auto rook_traps_king_on_7th = 6684932;
constexpr auto no_pawn = 19398907;
constexpr auto pawn_attacks = 2097222;
constexpr auto pawn_no_attack = 11731094;
constexpr auto r_mobility_add = 16;
constexpr auto r_mobility_div = 32;
#endif
auto squares = pos.pieces(me, pt_rook);
assert(squares);
if (pos.pieces(me, pt_king) & (me == white ? bb2(f1, g1) : bb2(f8, g8)) && squares & (me == white ? 0xC0C0 : 0xC0C0000000000000))
score -= uncastled_penalty;
do
{
const auto sq = pop_lsb(&squares);
auto attack = attack_bb_rook(sq, pos.pieces() ^ pos.pieces(me, pt_rook, pt_queen));
if (attack & ai.k_zone[you])
ai.k_attack_score[me] += rook_attacks_king;
ai.attack[me][pt_rook] |= attack;
ai.double_attack[me] |= ai.attack[me][pieces_without_king] & attack;
ai.attack[me][pieces_without_king] |= attack;
attack &= ai.mobility_mask[me];
if (ai.pinned[me] & sq)
attack &= bb_between(pos.king(me), sq);
const uint32_t mobility = popcnt(attack);
score += mobility_r[(mobility * mob_mult_r[relative_square(me, sq)] + r_mobility_add) / r_mobility_div];
if (constexpr auto eighth_rank = me == white ? rank_8_bb : rank_1_bb; relative_rank(me, sq) == rank_7 && pos.pieces(you, pt_king) & eighth_rank)
score += rook_traps_king_on_7th;
if (file_of(sq) == file_of(pos.king(you)) && !(pos.pieces(me, pt_pawn) & bb_between(pos.king(you), sq)))
ai.k_attack_score[me] += 2 * 8;
if (!(bb_file(sq) & pos.pieces(me, pt_pawn)))
{
if (const auto pawn = pos.pieces(you, pt_pawn) & bb_file(sq); !pawn)
score += no_pawn;
else if (pawn & ai.attack[you][pt_pawn])
score += pawn_attacks;
else
score += pawn_no_attack;
}
} while (squares);
return score;
}
template <side me>
inline int eval_space(const position& pos, const attack_info& ai)
{
constexpr auto you = me == white ? black : white;
#ifndef TUNER
constexpr auto space_weight_mult = 3;
constexpr auto space_weight_div = 16;
#endif
constexpr uint64_t center_zone = me == white ? 0x000000003c3c3c00 : 0x003c3c3c00000000;
const auto safe = center_zone & ~(pos.pieces(me, pt_pawn) | ai.attack[you][pt_pawn] | (ai.attack[you][all_pieces] & ~ai.attack[me][all_pieces]));
auto shielded = pos.pieces(me, pt_pawn);
shielded |= me == white ? shielded >> 8 : shielded << 8;
shielded |= me == white ? shielded >> 16 : shielded << 16;
shielded &= safe;
const uint32_t control = popcnt(safe | (me == white ? shielded << 32 : shielded >> 32));
const uint32_t weight = popcnt(pos.pieces(me));
const auto escore = remake_score(weight * weight * space_weight_mult / space_weight_div, eval_0);
return escore * control;
}
template <side me>
inline int eval_strong_squares(const position& pos, const attack_info& ai, const pawn::pawn_hash_entry* pawn_entry)
{
constexpr auto you = me == white ? black : white;
constexpr uint64_t rank456 = me == white ? 0x3C3C3C000000 : 0x3C3C3C0000;
#ifndef TUNER
constexpr auto safety_for_pawn_rbp = 3670059;
constexpr auto strong_p_in_front_of_pawn = 1441846;
constexpr auto strong_square_pb = 6488176;
constexpr auto strong_square_pb_extra = 16318582;
constexpr auto pb_behind_pawn = 3342348;
constexpr auto protected_piece = 5767214;
#endif
auto score = 0;
score += safety_for_pawn_rbp * popcnt(pawn_entry->safe_for_pawn(you) & pos.pieces(me, pt_knight, pt_bishop, pt_rook));
score += strong_p_in_front_of_pawn * popcnt(
pawn_entry->safe_for_pawn(you) & pos.pieces(me, pt_knight) & shift_down<me>(pos.pieces(you, pt_pawn)));
auto strong_pb = pawn_entry->safe_for_pawn(you) & ai.attack[me][pt_pawn] & pos.pieces(me, pt_knight, pt_bishop) & rank456;
if (strong_pb)
{
score += strong_square_pb * popcnt(strong_pb);
if (!pos.pieces(you, pt_knight))
{
do
{
const auto sq = pop_lsb(&strong_pb);
if (const auto squares_same_color = dark_squares & sq ? dark_squares : ~dark_squares; !(pos.pieces(you, pt_bishop) & squares_same_color))
score += strong_square_pb_extra;
} while (strong_pb);
}
}
score += pb_behind_pawn * popcnt(pos.pieces(me, pt_knight) & pawn_attack<you>(pos.pieces(me, pt_pawn)));
score += pb_behind_pawn * popcnt(pos.pieces(me, pt_knight, pt_bishop) & shift_down<me>(pos.pieces(me, pt_pawn)));
score += protected_piece * popcnt(pos.pieces_excluded(me, pt_pawn) & ai.attack[me][pt_pawn]);
return score;
}
template <side me>
int eval_threats(const position& pos, attack_info& ai)
{
constexpr auto you = me == white ? black : white;
constexpr auto rank2 = me == white ? rank_2_bb : rank_7_bb;
constexpr auto rank7 = me == white ? rank_7_bb : rank_2_bb;
#ifndef TUNER
constexpr auto hanging_pawn_threat = 26083619;
constexpr auto hanging_pieces = 17498230;
constexpr auto king_threat_single = 2490697;
constexpr auto king_threat_multiple = 6488796;
constexpr auto pawn_advance = 11272272;
#endif
auto score = 0;
ai.strong_threat[me] = false;
if (auto pawn_threats = pos.pieces_excluded(you, pt_pawn) & ai.attack[me][pt_pawn]; pawn_threats)
{
const auto safe_pawns = pos.pieces(me, pt_pawn) & (~ai.attack[you][all_pieces] | ai.attack[me][all_pieces]);
uint64_t safe_threats = pawn_attack<me>(safe_pawns) & pawn_threats;
if (pawn_threats ^ safe_threats)
score += hanging_pawn_threat;
if (safe_threats)
{
ai.strong_threat[me] = true;
}
while (safe_threats)
score += pawn_threat[piece_type(pos.piece_on_square(pop_lsb(&safe_threats)))];
}
const auto supported_pieces = pos.pieces_excluded(you, pt_pawn) & ai.attack[you][pt_pawn] & ai.attack[me][all_pieces];
if (const auto weak_pieces = pos.pieces(you) & ~ai.attack[you][pt_pawn] & ai.attack[me][all_pieces]; supported_pieces | weak_pieces)
{
auto b = (supported_pieces | weak_pieces) & (ai.attack[me][pt_knight] | ai.attack[me][pt_bishop]);
if (b & pos.pieces(you, pt_rook, pt_queen))
ai.strong_threat[me] = true;
while (b)
score += piece_threat[piece_type(pos.piece_on_square(pop_lsb(&b)))];
b = (pos.pieces(you, pt_queen) | weak_pieces) & ai.attack[me][pt_rook];
if (b & pos.pieces(you, pt_queen))
ai.strong_threat[me] = true;
while (b)
score += rook_threat[piece_type(pos.piece_on_square(pop_lsb(&b)))];
b = weak_pieces & ~ai.attack[you][all_pieces];
if (b & pos.pieces_excluded(you, pt_pawn))
ai.strong_threat[me] = true;
score += hanging_pieces * popcnt(b);
b = weak_pieces & ai.attack[me][pt_king];
if (b)
score += more_than_one(b) ? king_threat_multiple : king_threat_single;
}
auto b = pos.pieces(me, pt_pawn) & ~rank7;
b = shift_up<me>(b | (shift_up<me>(b & rank2) & ~pos.pieces()));
b &= ~pos.pieces()
& ~ai.attack[you][pt_pawn]
& (ai.attack[me][all_pieces] | ~ai.attack[you][all_pieces]);
b = pawn_attack<me>(b)
& pos.pieces(you)
& ~ai.attack[me][pt_pawn];
score += pawn_advance * popcnt(b);
return score;
}
int eval(const position& pos, const int alpha, const int beta)
{
if (pos.is_in_check())
return score_0;
#ifndef TUNER
constexpr auto mg_mgvalue_mult = 106;
constexpr auto mg_egvalue_mult = 6;
constexpr auto eg_mgvalue_mult = 13;
constexpr auto eg_egvalue_mult = 87;
constexpr auto eval_mult = 35;
constexpr auto conversion_mult = 115;
constexpr auto conversion_div = 128;
constexpr auto eval_div = 32;
constexpr auto eval_value_div = 8;
constexpr auto blocked_pawns_mg = 43;
constexpr auto blocked_pawns_eg = 167;
constexpr auto flank_double_attack = 1835008;
constexpr auto pawn_contempt_mult = 2;
constexpr auto knight_contempt_mult = 2;
constexpr auto bishop_contempt_mult = 3;
constexpr auto rook_contempt_mult = 4;
constexpr auto queen_contempt_mult = 8;
constexpr auto contempt_mult = 4;
#endif
const auto blocked_pawns = mul_div(make_score(blocked_pawns_mg, blocked_pawns_eg), 128, 256);
auto* const material_entry = material::probe(pos);
auto* pi = pos.info();
pi->eval_is_exact = false;
if (material_entry->has_value_function())
{
pi->strong_threat = 0;
return material_entry->value_from_function(pos);
}
if (const auto do_lazy_eval = beta < win_score && (pi - 1)->eval_positional != no_eval && alpha > -win_score
&& pos.non_pawn_material(white) + pos.non_pawn_material(black) > 2 * mat_bishop
&& !(pos.pieces(white, pt_pawn) & rank_7_bb) && !(pos.pieces(black, pt_pawn) & rank_2_bb); do_lazy_eval)
{
auto val = (pi - 1)->eval_positional;
const int eval_factor = (pi - 1)->eval_factor;
val += material_entry->value * eval_factor / max_factor;
if (pos.on_move() == black)
val = -val;
if (const auto lazy_result = val / eval_value_div + value_tempo; lazy_result <= alpha || lazy_result >= beta)
{
pi->strong_threat = 0;
return lazy_result;
}
}
auto* pawn_entry = pawn::probe(pos);
auto king_safety = pawn_entry->king_safety<white>(pos);
king_safety -= pawn_entry->king_safety<black>(pos);
attack_info ai{};
eval_init<white>(pos, ai, pawn_entry);
eval_init<black>(pos, ai, pawn_entry);
ai.mobility_mask[white] = ~(ai.attack[black][pt_pawn] | (pos.pieces(white, pt_pawn) & shift_down<white>(pos.pieces())))
| pos.pieces_excluded(black, pt_pawn);
ai.mobility_mask[black] = ~(ai.attack[white][pt_pawn] | (pos.pieces(black, pt_pawn) & shift_down<black>(pos.pieces())))
| pos.pieces_excluded(white, pt_pawn);
auto eval_score = pos.psq_score();
if (pos.pieces(white, pt_knight))
eval_score += eval_knights<white>(pos, ai);
if (pos.pieces(black, pt_knight))
eval_score -= eval_knights<black>(pos, ai);
if (pos.pieces(white, pt_bishop))
eval_score += eval_bishops<white>(pos, ai, pawn_entry);
if (pos.pieces(black, pt_bishop))
eval_score -= eval_bishops<black>(pos, ai, pawn_entry);
if (pos.pieces(white, pt_rook))
eval_score += eval_rooks<white>(pos, ai);
if (pos.pieces(black, pt_rook))
eval_score -= eval_rooks<black>(pos, ai);
if (pos.pieces(white, pt_queen))
eval_score += eval_queens<white>(pos, ai);
if (pos.pieces(black, pt_queen))
eval_score -= eval_queens<black>(pos, ai);
ai.double_attack[white] |= ai.attack[white][pieces_without_king] & ai.attack[white][pt_king];
ai.attack[white][all_pieces] = ai.attack[white][pieces_without_king] | ai.attack[white][pt_king];
ai.double_attack[black] |= ai.attack[black][pieces_without_king] & ai.attack[black][pt_king];
ai.attack[black][all_pieces] = ai.attack[black][pieces_without_king] | ai.attack[black][pt_king];
eval_score += eval_king_attack<white>(pos, ai, ai.k_attack_score[white] - pawn_entry->safety[black]);
eval_score -= eval_king_attack<black>(pos, ai, ai.k_attack_score[black] - pawn_entry->safety[white]);
eval_score += eval_threats<white>(pos, ai);
eval_score -= eval_threats<black>(pos, ai);
pi->strong_threat = ai.strong_threat[white] + 2 * ai.strong_threat[black];
if (pawn_entry->passed_pawns(white))
eval_score += eval_passed_pawns<white>(pos, ai, pawn_entry->passed_pawns(white));
if (pawn_entry->passed_pawns(black))
eval_score -= eval_passed_pawns<black>(pos, ai, pawn_entry->passed_pawns(black));
eval_score += eval_strong_squares<white>(pos, ai, pawn_entry);
eval_score -= eval_strong_squares<black>(pos, ai, pawn_entry);
eval_score -= blocked_pawns * popcnt(pos.pieces(white, pt_pawn) & shift_down<white>(pos.pieces()));
eval_score += blocked_pawns * popcnt(pos.pieces(black, pt_pawn) & shift_down<black>(pos.pieces()));
if (pos.non_pawn_material(white) + pos.non_pawn_material(black) >= space_threshold)
{
eval_score += eval_space<white>(pos, ai);
eval_score -= eval_space<black>(pos, ai);
}
auto bb_flank = ai.attack[white][pieces_without_king] & bb_king_flank_attack[black][file_of(pos.king(black))]
& ~ai.attack[black][pt_king] & ~ai.attack[black][pt_pawn];
bb_flank = bb_flank >> 4 | (bb_flank & ai.double_attack[white]);
eval_score += popcnt(bb_flank) * flank_double_attack;
bb_flank = ai.attack[black][pieces_without_king] & bb_king_flank_attack[white][file_of(pos.king(white))]
& ~ai.attack[white][pt_king] & ~ai.attack[white][pt_pawn];
bb_flank = bb_flank << 4 | (bb_flank & ai.double_attack[black]);
eval_score -= popcnt(bb_flank) * flank_double_attack;
auto score = pawn_entry->pawns_score() + king_safety + mul_div(eval_score, eval_mult, eval_div);
if (pos.non_pawn_material(white) + pos.non_pawn_material(black) <= 4 * mat_bishop)
score += pawn_file_width[pawn_entry->pawn_range(white)] - pawn_file_width[pawn_entry->pawn_range(black)];
const auto mg = (mg_mgvalue_mult * mg_value(score) - mg_egvalue_mult * eg_value(score)) / 100;
auto eg = (eg_mgvalue_mult * mg_value(score) + eg_egvalue_mult * eg_value(score)) / 100;
const auto scale_factor = calculate_scale_factor(pos, material_entry, material_entry->value + eg);
auto conversion = material_entry->conversion;
eg += eval_initiative(pos, pawn_entry, material_entry->value + eg);
if (material_entry->conversion_is_estimated && !pos.pieces(pt_queen) && !(pawn_entry->passed_pawns(white) | pawn_entry->passed_pawns(black)))
conversion = static_cast<sfactor>(conversion * conversion_mult / conversion_div);
if (pawn_entry->conversion_difficult)
conversion = static_cast<sfactor>(conversion * conversion_mult / conversion_div);
const auto eval_factor = scale_factor == normal_factor
? conversion
: static_cast<sfactor>(std::min(static_cast<int>(conversion), 2 * scale_factor));
const auto phase = material_entry->get_game_phase();
auto val = (mg * conversion / max_factor * phase + eg * eval_factor / max_factor * (middlegame_phase - phase)) /
static_cast<int>(middlegame_phase);
pi->eval_positional = val;
pi->eval_factor = static_cast<uint8_t>(eval_factor);
val += material_entry->value * eval_factor / max_factor;
if (thread_pool.piece_contempt)
{
const auto contempt_number = pawn_contempt_mult * pos.number(thread_pool.contempt_color, pt_pawn)
+ knight_contempt_mult * pos.number(thread_pool.contempt_color, pt_knight) + bishop_contempt_mult * pos.number(thread_pool.contempt_color, pt_bishop)
+ rook_contempt_mult * pos.number(thread_pool.contempt_color, pt_rook) + queen_contempt_mult * pos.number(thread_pool.contempt_color, pt_queen);
if (const auto contempt_score = contempt_mult * thread_pool.piece_contempt * contempt_number * eval_factor / max_factor; thread_pool.contempt_color == white)