-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRoc.cpp
More file actions
9243 lines (8754 loc) · 253 KB
/
Roc.cpp
File metadata and controls
9243 lines (8754 loc) · 253 KB
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
// This code is public domain, as defined by the "CC0" Creative Commons license
//#define REGRESSION
//#define W32_BUILD
#define _CRT_SECURE_NO_WARNINGS
//#define CPU_TIMING
#define LARGE_PAGES
#define MP_NPS
//#define TIME_TO_DEPTH
#define TB 1
//#define HNI
#ifdef W32_BUILD
#define NTDDI_VERSION 0x05010200
#define _WIN32_WINNT 0x0501
#else
#define _WIN32_WINNT 0x0600
#endif
#include <iostream>
#include <fstream>
#include <array>
#include <numeric>
#include <string>
#include <thread>
#include <cmath>
#include <algorithm>
#include <bitset>
#include "setjmp.h"
#include <windows.h>
#undef min
#undef max
#include <intrin.h>
#include <assert.h>
//#include "TunerParams.inc"
using namespace std;
#define INLINE __forceinline
#include "Bit.h"
#include "Immutable.h"
#define TEMPLATE_ME(f) (me ? f<1> : f<0>)
template<class T> INLINE int Sgn(const T& x)
{
return x == 0 ? 0 : (x > 0 ? 1 : -1);
}
template<class T> INLINE const T& Min(const T& x, const T& y)
{
return x < y ? x : y;
}
template<class T> INLINE const T& Max(const T& x, const T& y)
{
return x < y ? y : x;
}
template<class T> INLINE T Square(const T& x)
{
return x * x;
}
template<class T> INLINE bool Even(const T& x)
{
return F(x & 1);
}
template<class C> INLINE bool Odd(const C& x)
{
return T(x & 1);
}
INLINE int FileOf(int loc)
{
return loc & 7;
}
INLINE int RankOf(int loc)
{
return loc >> 3;
}
template<bool me> INLINE int OwnRank(int loc)
{
return me ? (7 - RankOf(loc)) : RankOf(loc);
}
INLINE int OwnRank(bool me, int loc)
{
return me ? (7 - RankOf(loc)) : RankOf(loc);
}
INLINE int NDiagOf(int loc)
{
return 7 - FileOf(loc) + RankOf(loc);
}
INLINE int SDiagOf(int loc)
{
return FileOf(loc) + RankOf(loc);
}
INLINE int Dist(int x, int y)
{
return Max(abs(RankOf(x) - RankOf(y)), abs(FileOf(x) - FileOf(y)));
}
INLINE int From(uint16 move)
{
return (move >> 6) & 0x3f;
}
INLINE int To(uint16 move)
{
return move & 0x3f;
}
INLINE bool IsCastling(int piece, uint16 move)
{
return piece >= WhiteKing && abs(To(move) - From(move)) == 2;
}
inline void SetScore(int* move, uint16 score)
{
*move = (*move & 0xFFFF) | (score << 16);
} // notice this operates on long-form (int) moves
INLINE uint32 High32(const uint64& x)
{
return (uint32)(x >> 32);
}
INLINE uint32 Low32(const uint64& x)
{
return (uint32)(x);
}
constexpr int N_KILLER = 2;
constexpr int MAX_PHASE = 128;
constexpr int MIDDLE_PHASE = 64;
constexpr int PHASE_M2M = MAX_PHASE - MIDDLE_PHASE;
constexpr int MAX_HEIGHT = 128;
constexpr score_t CP_EVAL = 4; // numeric value of 1 centipawn, in eval phase
constexpr score_t CP_SEARCH = 4; // numeric value of 1 centipawn, in search phase (# of value equivalence classes in 1-cp interval)
// helper to divide intermediate quantities to form scores
// note that straight integer division (a la Gull) creates an attractor at 0
// we support this, especially for weights inherited from Gull which have not been tuned for Roc
template<int DEN, int SINK = DEN> struct Div_
{
int operator()(int x) const
{
constexpr int shift = std::numeric_limits<int>::max() / (2 * DEN);
constexpr int shrink = (SINK - DEN) / 2;
const int y = x > 0 ? Max(0, x - shrink) : Min(0, x + shrink);
return (y + DEN * shift) / DEN - shift;
}
};
struct GMaterial;
struct GPawnEntry;
struct GEvalInfo
{
uint64 occ, area[2], free[2], klocus[2];
GPawnEntry* PawnEntry;
const GMaterial* material;
packed_t score;
uint32 king_att[2];
packed_t king_att_val[2];
int king[2], mul;
};
// special calculators for specific material
// irritatingly, they need to be told what pop() to use
// setting any of these to call also triggers a call to eval_stalemate
// so they should never call eval_stalemate themselves
typedef void(*eval_special_t)(GEvalInfo& EI, pop_func_t pop);
// here is a function that exists only to trigger eval_stalemate
void eval_null(GEvalInfo&, pop_func_t) {}
void eval_unwinnable(GEvalInfo& EI, pop_func_t) { EI.mul = 0; }
struct GMaterial
{
sint16 score, closed;
array<eval_special_t, 2> eval;
array<uint8, 2> mul;
uint8 phase;
bitset<2> pawnsForPiece, contempt;
};
constexpr int MAGIC_SIZE = 107648;
struct CommonData_
{
array<GMaterial, TotalMat> Material;
array<uint64, MAGIC_SIZE> MagicAttacks;
array<array<array<uint64, 64>, 64>, 2> Kpk;
array<array<uint64, 64>, 64> Between, FullLine;
array<packed_t, 16 * 64> PstVals;
array<array<uint64, 64>, 16> PieceKey;
array<array<int, 16>, 16> MvvLva; // [piece][capture]
array<uint8, 256> SpanWidth, SpanGap;
array<array<uint64, 64>, 2> BishopForward, PAtt, PMove, PWay, PCone, PSupport;
array<uint64, 64> BMagic, BMagicMask, RMagic, RMagicMask;
array<uint64, 64> VLine, RMask, BMask, QMask, NAtt, KAtt, KAttAtt, NAttAtt, OneIn;
array<uint64, 64> KingFrontal, KingFlank;
array<array<packed_t, 32>, 2> MobQueen;
array<uint8, 256> PieceFromChar;
array<int, 64> ROffset;
array<array<packed_t, 18>, 2> MobBishop, MobRook;
array<array<packed_t, 12>, 2> MobKnight;
array<short, 64> BShift, BOffset, RShift;
array<uint64, 16> CastleKey;
array<array<uint64, 8>, 2> Forward;
array<uint64, 8> EPKey;
array<packed_t, 8> PasserGeneral, PasserBlocked, PasserFree, PasserSupported, PasserProtected, PasserConnected, PasserOutside, PasserCandidate, PasserClear;
array<uint8, 64> UpdateCastling;
array<array<sint16, 8>, 3> Shelter;
array<uint8, 16> LogDist;
array<sint16, 8> PasserAtt, PasserDef, PasserAttLog, PasserDefLog;
array<sint16, 4> StormBlocked, StormShelterAtt, StormConnected, StormOpen, StormFree;
uint64 TurnKey;
};
HANDLE DATA = NULL;
const CommonData_* RO = nullptr; // generally we access DATA through RO
#define opp (1 ^ (me))
#define MY_TURN (!Current->turn == opp)
INLINE uint64 ShiftNW(const uint64& target)
{
return (target & (~(File[0] | Line[7]))) << 7;
}
INLINE uint64 ShiftNE(const uint64& target)
{
return (target & (~(File[7] | Line[7]))) << 9;
}
INLINE uint64 ShiftSE(const uint64& target)
{
return (target & (~(File[7] | Line[0]))) >> 7;
}
INLINE uint64 ShiftSW(const uint64& target)
{
return (target & (~(File[0] | Line[0]))) >> 9;
}
template<bool me> INLINE uint64 ShiftW(const uint64& target)
{
return me ? ShiftSW(target) : ShiftNW(target);
}
template<bool me> INLINE uint64 ShiftE(const uint64& target)
{
return me ? ShiftSE(target) : ShiftNE(target);
}
template<bool me> INLINE uint64 Shift(const uint64& target)
{
return me ? target >> 8 : target << 8;
}
// THIS IS A NON-TEMPLATE FUNCTION BECAUSE MY COMPILER (Visual C++ 2015, Community Edition) CANNOT INLINE THE TEMPLATE VERSION CORRECTLY
INLINE const uint64& OwnLine(bool me, int n)
{
return Line[me ? 7 - n : n];
}
// Constants controlling play
constexpr int PliesToEvalCut = 50; // halfway to 50-move
constexpr int KingSafetyNoQueen = 8; // numerator; denominator is 16
constexpr int SeeThreshold = 40 * CP_EVAL;
constexpr int DrawCap = 100 * CP_EVAL;
constexpr int DeltaDecrement = (3 * CP_SEARCH) / 2; // 5 (+91/3) vs 3
constexpr int TBMinDepth = 7;
inline int MapPositive(int scale, int param)
{
return param > scale ? param : (scale * scale) / (2 * scale - param);
}
constexpr int FailLoInit = 22;
constexpr int FailHiInit = 31;
constexpr int FailLoGrowth = 37; // numerator; denominator is 64
constexpr int FailHiGrowth = 26; // numerator; denominator is 64
constexpr int FailLoDelta = 27;
constexpr int FailHiDelta = 24;
constexpr int AspirationEpsilon = 10;
constexpr int InitiativeConst = 3 * CP_SEARCH;
constexpr int InitiativePhase = 3 * CP_SEARCH;
constexpr int FutilityThreshold = 50 * CP_SEARCH;
#define IncV(var, x) (me ? (var -= (x)) : (var += (x)))
#define DecV(var, x) IncV(var, -(x))
constexpr sint16 KpkValue = 300 * CP_EVAL;
constexpr sint16 EvalValue = 30000;
constexpr sint16 MateValue = 32760 - 8 * (CP_SEARCH - 1);
/*
general move:
0 - 11: from & to
12 - 15: flags
16 - 23: history
24 - 25: spectial moves: killers, refutations...
26 - 30: MvvLva
delta move:
0 - 11: from & to
12 - 15: flags
16 - 31: sint16 delta + (sint16)0x4000
*/
constexpr int MvvLvaVictim[16] = { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3 };
constexpr int MvvLvaAttacker[16] = { 0, 0, 5, 5, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 6, 6 };
constexpr int MvvLvaAttackerKB[16] = { 0, 0, 9, 9, 7, 7, 5, 5, 5, 5, 3, 3, 1, 1, 11, 11 };
INLINE int PawnCaptureMvvLva(int attacker) { return MvvLvaAttacker[attacker]; }
constexpr int MaxPawnCaptureMvvLva = MvvLvaAttacker[15]; // 6
INLINE int KnightCaptureMvvLva(int attacker) { return MaxPawnCaptureMvvLva + MvvLvaAttackerKB[attacker]; }
constexpr int MaxKnightCaptureMvvLva = MaxPawnCaptureMvvLva + MvvLvaAttackerKB[15]; // 17
INLINE int BishopCaptureMvvLva(int attacker) { return MaxPawnCaptureMvvLva + MvvLvaAttackerKB[attacker] + 1; }
constexpr int MaxBishopCaptureMvvLva = MaxPawnCaptureMvvLva + MvvLvaAttackerKB[15] + 1; // usually 18
INLINE int RookCaptureMvvLva(int attacker) { return MaxBishopCaptureMvvLva + MvvLvaAttacker[attacker]; }
constexpr int MaxRookCaptureMvvLva = MaxBishopCaptureMvvLva + MvvLvaAttacker[15]; // usually 24
INLINE int QueenCaptureMvvLva(int attacker) { return MaxRookCaptureMvvLva + MvvLvaAttacker[attacker]; }
INLINE int MvvLvaPromotionCap(int capture) { return RO->MvvLva[((capture) < WhiteRook) ? WhiteRook : ((capture) >= WhiteQueen ? WhiteKing : WhiteKnight)][BlackQueen]; }
INLINE int MvvLvaPromotionKnightCap(int capture) { return RO->MvvLva[WhiteKing][capture]; }
INLINE int MvvLvaXrayCap(int capture) { return RO->MvvLva[WhiteKing][capture]; }
constexpr int RefOneScore = (0xFF << 16) | (3 << 24);
constexpr int RefTwoScore = (0xFF << 16) | (2 << 24);
#define HALT_CHECK \
if (Current->ply >= 100) return 0; \
for (int i = 4; i <= Current->ply; i += 2) if (Stack[sp - i] == Current->key) return 0; \
if ((Current - Data) >= 126) {evaluate(); return Current->score; }
INLINE int ExtToFlag(int ext)
{
return ext << 16;
}
INLINE int ExtFromFlag(int flags)
{
return (flags >> 16) & 0xF;
}
constexpr int FlagHashCheck = 1 << 20; // first 20 bits are reserved for the hash killer and extension
constexpr int FlagHaltCheck = 1 << 21;
constexpr int FlagCallEvaluation = 1 << 22;
constexpr int FlagDisableNull = 1 << 23;
constexpr int FlagNeatSearch = FlagHashCheck | FlagHaltCheck | FlagCallEvaluation;
constexpr int FlagNoKillerUpdate = 1 << 24;
constexpr int FlagReturnBestMove = 1 << 25;
typedef struct
{
array<uint64, 16> bb;
array<uint8, 64> square;
} GBoard;
__declspec(align(64)) GBoard Board[1];
array<uint64, 2048> Stack;
int sp, save_sp;
uint64 nodes, tb_hits, check_node, check_node_smp;
GBoard SaveBoard[1];
// macros using global "Board"
INLINE uint64& Piece(int i) { return Board->bb[i]; }
INLINE uint64& Pawn(int me) { return Piece(WhitePawn | me); }
INLINE uint64& Knight(int me) { return Piece(WhiteKnight | me); }
INLINE uint64 Bishop(int me) { return Piece(WhiteLight | me) | Piece(WhiteDark | me); }
INLINE uint64& Rook(int me) { return Piece(WhiteRook | me); }
INLINE uint64& Queen(int me) { return Piece(WhiteQueen | me); }
INLINE uint64& King(int me) { return Piece(WhiteKing | me); }
INLINE uint64 NonPawn(int me) { return Piece(me) ^ Pawn(me); }
INLINE uint64 NonPawnKing(int me) { return NonPawn(me) ^ King(me); }
INLINE uint64 Major(int me) { return Rook(me) | Queen(me); }
INLINE uint64 Minor(int me) { return Knight(me) | Bishop(me); }
INLINE uint64 PieceAll() { return Piece(White) | Piece(Black); }
INLINE uint64 PawnAll() { return Pawn(White) | Pawn(Black); }
INLINE uint64 NonPawnKingAll() { return NonPawnKing(White) | NonPawnKing(Black); }
INLINE uint8& PieceAt(int sq) { return Board->square[sq]; }
typedef struct
{
array<uint8, 64> square;
uint64 key, pawn_key;
packed_t pst;
int material;
uint16 move;
uint8 turn, castle_flags, ply, ep_square, piece, capture;
} GPosData;
typedef struct
{
array<uint64, 2> att, patt, xray;
uint64 key, pawn_key, eval_key, passer, threat, mask;
packed_t pst;
int material;
int *start, *current;
int best;
score_t score;
array<uint16, N_KILLER + 1> killer;
array<uint16, 2> ref;
uint16 move;
uint8 turn, castle_flags, ply, ep_square, capture, gen_flags, piece, stage;
sint32 moves[144];
} GData;
__declspec(align(64)) GData Data[MAX_HEIGHT];
GData* Current = Data;
constexpr uint8 FlagSort = 1 << 0;
constexpr uint8 FlagNoBcSort = 1 << 1;
GData SaveData[1];
#if TB
constexpr sint16 TBMateValue = 31380;
constexpr sint16 TBCursedMateValue = 13;
const int TbValues[5] = { -TBMateValue, -TBCursedMateValue, 0, TBCursedMateValue, TBMateValue };
constexpr int NominalTbDepth = 33;
inline int TbDepth(int depth) { return Min(depth + NominalTbDepth, 117); }
constexpr uint32 TB_RESULT_FAILED = 0xFFFFFFFF;
extern unsigned TB_LARGEST;
bool tb_init_fwd(const char*);
#define TB_CUSTOM_POP_COUNT pop1
#define TB_CUSTOM_LSB lsb
#define TB_CUSTOM_BSWAP32 _byteswap_ulong
extern "C" unsigned tb_probe_wdl(
uint64_t _white,
uint64_t _black,
uint64_t _kings,
uint64_t _queens,
uint64_t _rooks,
uint64_t _bishops,
uint64_t _knights,
uint64_t _pawns,
unsigned _rule50,
unsigned _castling,
unsigned _ep,
bool _turn);
template<class F_, typename... Args_> int TBProbe(F_ func, bool me, const Args_&&... args)
{
return func(Piece(White), Piece(Black),
King(White) | King(Black),
Queen(White) | Queen(Black),
Rook(White) | Rook(Black),
Bishop(White) | Bishop(Black),
Knight(White) | Knight(Black),
Pawn(White) | Pawn(Black),
Current->ply,
Current->castle_flags,
Current->ep_square,
(me == White), std::forward<Args_>(args)...);
}
unsigned tb_probe_root_fwd(
uint64_t _white,
uint64_t _black,
uint64_t _kings,
uint64_t _queens,
uint64_t _rooks,
uint64_t _bishops,
uint64_t _knights,
uint64_t _pawns,
unsigned _rule50,
unsigned _ep,
bool _turn);
static inline unsigned tb_probe_root_checked(
uint64_t _white,
uint64_t _black,
uint64_t _kings,
uint64_t _queens,
uint64_t _rooks,
uint64_t _bishops,
uint64_t _knights,
uint64_t _pawns,
unsigned _rule50,
unsigned _castling,
unsigned _ep,
bool _turn)
{
if (_castling != 0)
return TB_RESULT_FAILED;
return tb_probe_root_fwd(_white, _black, _kings, _queens, _rooks, _bishops, _knights, _pawns, _rule50, _ep, _turn);
}
int GetTBMove(unsigned res, int* best_score);
#endif
enum
{
stage_search,
s_hash_move,
s_good_cap,
s_special,
s_quiet,
s_bad_cap,
s_none,
stage_evasion,
e_hash_move,
e_ev,
e_none,
stage_razoring,
r_hash_move,
r_cap,
r_checks,
r_none
};
constexpr int StageNone = (1 << s_none) | (1 << e_none) | (1 << r_none);
typedef struct
{
uint32 key;
uint16 date;
uint16 move;
score_t low;
score_t high;
uint16 flags;
uint8 low_depth;
uint8 high_depth;
} GEntry;
static GEntry NullEntry = { 0, 1, 0, 0, 0, 0, 0, 0 };
constexpr int initial_hash_size = 1024 * 1024;
sint64 hash_size = initial_hash_size;
uint64 hash_mask = (initial_hash_size - 4);
GEntry* Hash;
struct GPawnEntry
{
uint64 key;
packed_t score;
array<sint16, 2> shelter;
array<uint8, 2> passer, draw;
};
constexpr GPawnEntry NullPawnEntry = { 0, 0, {0, 0}, {0, 0}, {0, 0} };
constexpr int PAWN_HASH_SIZE = 1 << 20;
__declspec(align(64)) array<GPawnEntry, PAWN_HASH_SIZE> PawnHash;
constexpr int PAWN_HASH_MASK = PAWN_HASH_SIZE - 1;
typedef struct
{
int knodes;
int ply;
uint32 key;
uint16 date;
uint16 move;
score_t value;
score_t exclusion;
uint8 depth;
uint8 ex_depth;
} GPVEntry;
constexpr GPVEntry NullPVEntry = { 0, 0, 0, 1, 0, 0, 0, 0, 0 };
constexpr int pv_hash_size = 1 << 20;
constexpr int pv_cluster_size = 1 << 2;
constexpr int pv_hash_mask = pv_hash_size - pv_cluster_size;
GPVEntry* PVHash = nullptr;
array<int, 256> RootList;
void Prefetch1(const char* p)
{
_mm_prefetch(p, _MM_HINT_T0);
}
template<int N> void Prefetch(const char* p)
{
//p -= reinterpret_cast<size_t>(p) & 63;
for (int ii = 0; ii < N; ++ii)
Prefetch1(p + 64 * ii);
}
INLINE void Prefetch(const GMaterial* pe)
{
Prefetch1(reinterpret_cast<const char*>(pe));
}
INLINE void Prefetch(const GEntry* pe)
{
Prefetch<2>(reinterpret_cast<const char*>(pe));
}
INLINE void Prefetch(const GPawnEntry* pe)
{
Prefetch<2>(reinterpret_cast<const char*>(pe));
}
#ifndef HNI
inline uint64 BishopAttacks(int sq, const uint64& occ)
{
return RO->MagicAttacks[RO->BOffset[sq] + (((RO->BMagicMask[sq] & occ) * RO->BMagic[sq]) >> RO->BShift[sq])];
}
inline uint64 RookAttacks(int sq, const uint64& occ)
{
return RO->MagicAttacks[RO->ROffset[sq] + (((RO->RMagicMask[sq] & occ) * RO->RMagic[sq]) >> RO->RShift[sq])];
}
#else
inline uint64 BishopAttacks(int sq, const uint64& occ)
{
return RO->MagicAttacks[RO->BOffset[sq] + _pext_u64(occ, RO->BMagicMask[sq])];
}
inline uint64 RookAttacks(int sq, const uint64& occ)
{
return RO->MagicAttacks[RO->ROffset[sq] + _pext_u64(occ, RO->RMagicMask[sq])];
}
#endif
INLINE uint64 QueenAttacks(int sq, const uint64& occ)
{
return BishopAttacks(sq, occ) | RookAttacks(sq, occ);
}
INLINE packed_t& XPst(CommonData_* data, int piece, int sq)
{
return data->PstVals[(piece << 6) | sq];
};
INLINE packed_t Pst(int piece, int sq)
{
return RO->PstVals[(piece << 6) | sq];
};
uint16 date;
array<array<array<uint16, 2>, 64>, 16> HistoryVals;
INLINE int* AddMove(int* list, int from, int to, int flags, int score)
{
*list = ((from) << 6) | (to) | (flags) | (score);
return ++list;
}
INLINE int* AddCapturePP(int* list, int att, int vic, int from, int to, int flags)
{
return AddMove(list, from, to, flags, RO->MvvLva[att][vic]);
}
INLINE int* AddCaptureP(int* list, int piece, int from, int to, int flags)
{
return AddCapturePP(list, piece, PieceAt(to), from, to, flags);
}
INLINE int* AddCaptureP(int* list, int piece, int from, int to, int flags, uint8 min_vic)
{
return AddCapturePP(list, piece, Max(min_vic, PieceAt(to)), from, to, flags);
}
INLINE int* AddCapture(int* list, int from, int to, int flags)
{
return AddCaptureP(list, PieceAt(from), from, to, flags);
}
INLINE uint16 JoinFlag(uint16 move)
{
return T(move & FlagPriority);
}
INLINE uint16& HistoryScore(int join, int piece, int from, int to)
{
return HistoryVals[piece][to][join];
}
INLINE int HistoryMerit(uint16 hs)
{
return hs / (hs & 0x00FF); // differs by 1 from Gull convention
}
INLINE int HistoryP(int join, int piece, int from, int to)
{
return HistoryMerit(HistoryScore(join, piece, from, to)) << 16;
}
INLINE int History(int join, int from, int to)
{
return HistoryP(join, PieceAt(from), from, to);
}
INLINE uint16& HistoryM(int move)
{
return HistoryScore(JoinFlag(move), PieceAt(From(move)), From(move), To(move));
}
INLINE int HistoryInc(int depth)
{
return Square(Min((depth) >> 1, 8));
}
INLINE void HistoryBad(uint16* hist, int inc)
{
if ((*hist & 0x00FF) >= 256 - inc)
*hist = ((*hist & 0xFEFE) >> 1);
*hist += inc;
}
INLINE void HistoryBad(int move, int depth)
{
HistoryBad(&HistoryM(move), HistoryInc(depth));
}
INLINE void HistoryGood(uint16* hist, int inc)
{
HistoryBad(hist, inc);
*hist += inc << 8;
}
INLINE void HistoryGood(int move, int depth)
{
HistoryGood(&HistoryM(move), HistoryInc(depth));
}
INLINE int* AddHistoryP(int* list, int piece, int from, int to, int flags)
{
return AddMove(list, from, to, flags, HistoryP(JoinFlag(flags), piece, from, to));
}
sint16 DeltaVals[16 * 4096];
INLINE sint16& DeltaScore(int piece, int from, int to)
{
return DeltaVals[(piece << 12) | (from << 6) | to];
}
INLINE sint16& Delta(int from, int to)
{
return DeltaScore(PieceAt(from), from, to);
}
INLINE sint16& DeltaM(int move)
{
return Delta(From(move), To(move));
}
INLINE int* AddCDeltaP(int* list, int margin, int piece, int from, int to, int flags)
{
return DeltaScore(piece, from, to) < margin
? list
: AddMove(list, from, to, flags, (DeltaScore(piece, from, to) + 0x4000) << 16);
}
typedef struct
{
uint16 ref[2];
uint16 check_ref[2];
} GRef;
GRef Ref[16 * 64];
INLINE GRef& RefPointer(int piece, int from, int to)
{
return Ref[((piece) << 6) | (to)];
}
INLINE GRef& RefM(int move)
{
return RefPointer(PieceAt(To(move)), From(move), To(move));
}
INLINE void UpdateRef(int ref_move)
{
auto& dst = RefM(Current->move).ref;
if (T(Current->move) && dst[0] != ref_move)
{
dst[1] = dst[0];
dst[0] = ref_move;
}
}
INLINE void UpdateCheckRef(int ref_move)
{
auto& dst = RefM(Current->move).check_ref;
if (T(Current->move) && dst[0] != ref_move)
{
dst[1] = dst[0];
dst[0] = ref_move;
}
}
uint16 PV[MAX_HEIGHT];
char info_string[1024];
char pv_string[1024];
char score_string[16];
char mstring[65536];
int MultiPV[256];
int pvp;
int pv_length;
int best_move, best_score;
int TimeLimit1, TimeLimit2, Console, HardwarePopCnt;
int DepthLimit, LastDepth, LastTime, LastValue, LastExactValue, PrevMove, InstCnt;
sint64 LastSpeed;
int PVN, Contempt, Wobble, Stop, Print, Input = 1, PVHashing = 1, Infinite, MoveTime, SearchMoves, SMPointer, Ponder, Searching, Previous;
typedef struct
{
int Bad, Change, Singular, Early, FailLow, FailHigh;
} GSearchInfo;
GSearchInfo CurrentSI[1], BaseSI[1];
#ifdef CPU_TIMING
int CpuTiming = 0, UciMaxDepth = 0, UciMaxKNodes = 0, UciBaseTime = 1000, UciIncTime = 5;
int GlobalTime[2] = { 0, 0 };
int GlobalInc[2] = { 0, 0 };
int GlobalTurn = 0;
constexpr sint64 CyclesPerMSec = 3400000;
#endif
int Aspiration = 1, LargePages = 1;
constexpr int TimeSingTwoMargin = 20;
constexpr int TimeSingOneMargin = 30;
constexpr int TimeNoPVSCOMargin = 60;
constexpr int TimeNoChangeMargin = 70;
constexpr int TimeRatio = 120;
constexpr int PonderRatio = 120;
constexpr int MovesTg = 30;
constexpr int InfoLag = 5000;
constexpr int InfoDelay = 1000;
sint64 StartTime, InfoTime, CurrTime;
uint16 SMoves[256];
jmp_buf Jump, ResetJump;
HANDLE StreamHandle;
INLINE int ExclSingle(int depth)
{
return 8 * CP_SEARCH;
}
INLINE int ExclDouble(int depth)
{
return 16 * CP_SEARCH;
}
// EVAL
const sint8 DistC[8] = { 3, 2, 1, 0, 0, 1, 2, 3 };
const sint8 RankR[8] = { -3, -2, -1, 0, 1, 2, 3, 4 };
constexpr uint16 SeeValue[16] = { 0, 0, 360, 360, 1300, 1300, 1300, 1300, 1300, 1300, 2040, 2040, 3900, 3900, 30000, 30000 };
constexpr int PieceType[16] = { 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5 };
constexpr array<int, 5> Phase = { 0, SeeValue[4], SeeValue[6], SeeValue[10], SeeValue[12] };
constexpr int PhaseMin = 2 * Phase[3] + Phase[1] + Phase[2];
constexpr int PhaseMax = 16 * Phase[0] + 3 * Phase[1] + 3 * Phase[2] + 4 * Phase[3] + 2 * Phase[4];
#define V(x) (x)
INLINE constexpr int ArrayIndex(int width, int row, int column)
{
return row * width + column;
}
template<class T_> constexpr auto Av(const T_& x, int width, int row, int column) -> decltype(x[0])
{
return x[ArrayIndex(width, row, column)];
}
template<class T_> constexpr auto TrAv(const T_& x, int w, int r, int c) -> decltype(x[0])
{
return Av(x, 0, 0, ((r * (2 * w - r + 1)) / 2) + c);
}
template<class T_> constexpr auto Sa(const T_& x, int y) -> decltype(x[0])
{
return Av(x, 0, 0, y);
}
template<class C_> INLINE constexpr packed_t Ca4(const C_& x, int y)
{
return Pack4(Av(x, 4, y, 0), Av(x, 4, y, 1), Av(x, 4, y, 2), Av(x, 4, y, 3));
}
// EVAL WEIGHTS
// tuner: start
enum
{ // tuner: enum
IMatLinear,
IMatQuadMe = IMatLinear + 5,
IMatQuadOpp = IMatQuadMe + 14,
IBishopPairQuad = IMatQuadOpp + 10,
iMatClosed = IBishopPairQuad + 9,
IMatSpecial = iMatClosed + 6,
IPstQuadWeights = IMatSpecial + 40,
IPstLinearWeights = IPstQuadWeights + 96,
IPstQuadMixedWeights = IPstLinearWeights + 96,
IMobilityLinear = IPstQuadMixedWeights + 48,
IMobilityLog = IMobilityLinear + 16,
IMobilityLocus = IMobilityLog + 16,
IShelterValue = IMobilityLocus + 16,
IStormQuad = IShelterValue + 15,
IStormLinear = IStormQuad + 5,
IStormHof = IStormLinear + 5,
IPasserQuad = IStormHof + 2,
IPasserLinear = IPasserQuad + 36,
IPasserConstant = IPasserLinear + 36,
IPasserAttDefQuad = IPasserConstant + 36,
IPasserAttDefLinear = IPasserAttDefQuad + 4,
IPasserAttDefConst = IPasserAttDefLinear + 4,
IPasserSpecial = IPasserAttDefConst + 4,
IIsolated = IPasserSpecial + 12,
IUnprotected = IIsolated + 10,
IBackward = IUnprotected + 6,
IDoubled = IBackward + 4,
IRookSpecial = IDoubled + 4,
ITactical = IRookSpecial + 20,
IKingDefence = ITactical + 12,
IPawnSpecial = IKingDefence + 8,
IBishopSpecial = IPawnSpecial + 8,
IKnightSpecial = IBishopSpecial + 4,
IPin = IKnightSpecial + 10,
IKingRay = IPin + 10,
IKingAttackWeight = IKingRay + 6
};
// pawn, knight, bishop, rook, queen, pair
constexpr array<int, 6> MatLinear = { 21, -20, -8, 76, -15, -1 };
// T(pawn), pawn, knight, bishop, rook, queen
constexpr array<int, 21> MatQuadMe = { // tuner: type=array, var=1000, active=0
NULL, 0, 0, 0, 0, 0,
-33, 117, -23, -155, -247,
15, 296, -105, -83,
-162, 327, 315,
-861, -1013,
NULL
};
constexpr array<int, 15> MatQuadOpp = { // tuner: type=array, var=1000, active=0
0, 0, 0, 0, 0,
-114, -96, -20, -278,
35, 39, 49,
9, -2,
75
};
constexpr array<int, 9> BishopPairQuad = { // tuner: type=array, var=1000, active=0
-38, 164, 99, 246, -84, -57, -184, 88, -186
};
constexpr array<int, 6> MatClosed = { -20, 22, -33, 18, -2, 26 };
namespace Params
{
enum
{
MatRB,
MatRN,
MatQRR,
MatQRB,
MatQRN,
MatQ3,
MatBBR,
MatBNR,
MatNNR,
MatM,
MatPawnOnly
};
constexpr array<int, 44> MatSpecial = { // tuner: type=array, var=120, active=0
52, 0, -52, -10,
40, 2, -36, -10,
32, 40, 48, 0,
16, 20, 24, 0,
20, 28, 36, 0,
-12, -22, -32, 0,
-10, 20, 64, 0,
6, 21, 21, 0,
0, -12, -24, 0,
4, 8, 12, 0,
0, 0, 0, -100 };
}
namespace Values
{
#define VALUE(name) constexpr packed_t name = Ca4(Params::MatSpecial, Params::name)
VALUE(MatRB);
VALUE(MatRN);
VALUE(MatQRR);
VALUE(MatQRB);
VALUE(MatQRN);
VALUE(MatQ3);
VALUE(MatBBR);
VALUE(MatBNR);
VALUE(MatNNR);
VALUE(MatM);
VALUE(MatPawnOnly);
#undef VALUE
}
namespace PstW
{
struct Weights_
{
struct Phase_
{
array<int, 4> quad_;
array<int, 4> linear_;
array<int, 2> quadMixed_;
} op_, md_, eg_, cl_;
};
constexpr Weights_ Pawn = {
{ { -48, -275, 165, 0 },{ -460, -357, -359, 437 },{ 69, -28 } },
{ {-85, -171, 27, 400},{ -160, -133, 93, 1079 },{ 13, -6 }},
{ {-80, -41, -85, 782},{ 336, 303, 295, 1667 },{ -35, 13 }},
{ {2, 13, 11, 23},{ 6, 14, 37, -88 },{ 14, -2 }} };
constexpr Weights_ Knight = {
{ { -134, 6, -12, -72 },{ -680, -343, -557, 1128 },{ -32, 14 } },
{ { -315, -123, -12, -90 },{ -449, -257, -390, 777 },{ -24, -3 } },
{ { -501, -246, -12, -107 },{ 61, -274, -357, 469 },{ -1, -16 } },
{ { -12, -5, -2, -22 },{ 96, 69, -64, -23 },{ -5, -8 } } };
constexpr Weights_ Bishop = {
{ { -123, -62, 54, -116 },{ 24, -486, -350, -510 },{ 8, -58 } },
{ { -168, -49, 24, -48 },{ -323, -289, -305, -254 },{ -7, -21 } },
{ { -249, -33, 4, -14 },{ -529, -232, -135, 31 },{ -32, 0 } },
{ { 4, -10, 9, -13 },{ 91, -43, -34, 29 },{ -13, -10 } } };
constexpr Weights_ Rook = {
{ { -260, 12, -49, 324 },{ -777, -223, 245, 670 },{ -7, -25 } },
{ { -148, -88, -9, 165 },{ -448, -278, -63, 580 },{ -7, 0 } },
{ { 13, -149, 14, 46 },{ -153, -225, -246, 578 },{ -6, 16 } },
{ { 0, 8, -15, 8 },{ -32, -29, 10, -51 },{ -6, -23 } } };
constexpr Weights_ Queen = {
{ { -270, -18, -19, -68 },{ -520, 444, 474, -186 },{ 18, -6 } },
{ { -114, -209, 21, -103 },{ -224, -300, 73, 529 },{ -13, 1 } },
{ { 2, -341, 58, -160 },{ 40, -943, -171, 1328 },{ -34, 27 } },
{ { -3, -26, 9, 5 },{ -43, -18, -107, 60 },{ 5, 12 } } };
constexpr Weights_ King = {
{ { -266, -694, -12, 170 },{ 1077, 3258, 20, -186 },{ -18, 3 } },
{ { -284, -451, -31, 43 },{ 230, 1219, -425, 577 },{ -1, 5 } },
{ { -334, -157, -67, -93 },{ -510, -701, -863, 1402 },{ 37, -8 } },
{ { 22, 14, -16, 0 },{ 7, 70, 40, 78 } ,{ 9, -3 } } };
}
// coefficient (Linear, Log, Locus) * phase (4)
constexpr array<int, 12> MobCoeffsKnight = { 86, 46, 19, -5, 57, 36, 24, -1, 1, 10, -5, 12 };
constexpr array<int, 12> MobCoeffsBishop = { 85, 60, 48, -15, 59, 32, 25, -2, 1, 5, -1, 40 };
constexpr array<int, 12> MobCoeffsRook = { 22, 31, 44, 2, 41, 33, 28, 2, -1, 1, -1, 7 };
constexpr array<int, 12> MobCoeffsQueen = { 53, 30, 17, 2, 24, 31, 45, 1, 1, 1, -1, 10 };
constexpr int N_LOCUS = 22;
// file type (3) * distance from 2d rank/open (5)
constexpr array<int, 15> ShelterValue = { // tuner: type=array, var=26, active=0
8, 36, 44, 0, 0, // h-pawns
48, 72, 44, 0, 8, // g
96, 28, 32, 0, 0 // f
};
enum
{
StormHofValue,
StormHofScale,
StormOfValue,
StormOfScale