forked from TomHyer/Roc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roc.cpp
9427 lines (8926 loc) · 257 KB
/
Roc.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
// 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
typedef unsigned char uint8;
typedef char sint8;
typedef unsigned short uint16;
typedef short sint16;
typedef unsigned int uint32;
typedef int sint32;
typedef unsigned long long uint64;
typedef long long sint64;
#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 C> INLINE bool T(const C& x)
{
return x != 0;
}
template<class T> INLINE bool F(const T& x)
{
return x == 0;
}
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);
}
typedef sint64 packed_t;
constexpr packed_t Pack4(sint16 op, sint16 mid, sint16 eg, sint16 asym)
{
return op + (static_cast<sint64>(mid) << 16) + (static_cast<sint64>(eg) << 32) + (static_cast<sint64>(asym) << 48);
}
constexpr packed_t Pack2(sint16 x, sint16 y)
{
return Pack4(x, (x + y) / 2, y, 0);
}
INLINE sint16 Opening(packed_t x) { return static_cast<sint16>(x & 0xFFFF); }
INLINE sint16 Middle(packed_t x) { return static_cast<sint16>((x >> 16) + ((x >> 15) & 1)); }
INLINE sint16 Endgame(packed_t x) { return static_cast<sint16>((x >> 32) + ((x >> 31) & 1)); }
INLINE sint16 Closed(packed_t x) { return static_cast<sint16>((x >> 48) + ((x >> 47) & 1)); }
// unsigned for king_att
constexpr uint32 UPack(uint16 x, uint16 y)
{
return x + (static_cast<uint32>(y) << 16);
}
INLINE uint16 UUnpack1(uint32 x)
{
return static_cast<uint16>(x & 0xFFFF);
}
INLINE uint16 UUnpack2(uint32 x)
{
return static_cast<sint16>(x >> 16);
}
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 uint64 Bit(int loc)
{
return 1ull << loc;
}
#ifndef HNI
INLINE void Cut(uint64& x)
{
x &= x - 1;
}
#else
INLINE void Cut(uint64& x)
{
x = _blsr_u64(x);
}
#endif
INLINE bool Multiple(const uint64& b)
{
return T(b & (b - 1));
}
INLINE bool Single(const uint64& b)
{
return F(b & (b - 1));
}
INLINE bool HasBit(const uint64& bb, int loc)
{
return T(bb & Bit(loc));
}
INLINE int From(uint16 move)
{
return (move >> 6) & 0x3f;
}
INLINE int To(uint16 move)
{
return move & 0x3f;
}
inline void SetScore(int* move, uint16 score)
{
*move = (*move & 0xFFFF) | (score << 16);
} // notice this operates on long-form (int) moves
static const uint64 Filled = 0xFFFFFFFFFFFFFFFF;
static const uint64 Interior = 0x007E7E7E7E7E7E00;
static const uint64 Boundary = ~Interior;
static const uint64 LightArea = 0x55AA55AA55AA55AA;
static const uint64 DarkArea = ~LightArea;
INLINE uint32 High32(const uint64& x)
{
return (uint32)(x >> 32);
}
INLINE uint32 Low32(const uint64& x)
{
return (uint32)(x);
}
static const int N_KILLER = 2;
static const int MAX_PHASE = 128;
static const int MIDDLE_PHASE = 64;
static const int PHASE_M2M = MAX_PHASE - MIDDLE_PHASE;
static const int MAX_HEIGHT = 128;
typedef sint16 score_t;
static const score_t CP_EVAL = 4; // numeric value of 1 centipawn, in eval phase
static const 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
{
static const int shift = std::numeric_limits<int>::max() / (2 * DEN);
static const int shrink = (SINK - DEN) / 2;
const int y = x > 0 ? Max(0, x - shrink) : Min(0, x + shrink);
return (y + DEN * shift) / DEN - shift;
}
};
static const uint8 White = 0;
static const uint8 Black = 1;
static const uint8 WhitePawn = 2;
static const uint8 BlackPawn = 3;
static const uint8 IPawn[2] = { WhitePawn, BlackPawn };
static const uint8 WhiteKnight = 4;
static const uint8 BlackKnight = 5;
static const uint8 IKnight[2] = { WhiteKnight, BlackKnight };
static const uint8 WhiteLight = 6;
static const uint8 BlackLight = 7;
static const uint8 ILight[2] = { WhiteLight, BlackLight };
static const uint8 WhiteDark = 8;
static const uint8 BlackDark = 9;
static const uint8 IDark[2] = { WhiteDark, BlackDark };
static const uint8 WhiteRook = 10;
static const uint8 BlackRook = 11;
static const uint8 IRook[2] = { WhiteRook, BlackRook };
static const uint8 WhiteQueen = 12;
static const uint8 BlackQueen = 13;
static const uint8 IQueen[2] = { WhiteQueen, BlackQueen };
static const uint8 WhiteKing = 14;
static const uint8 BlackKing = 15;
static const uint8 IKing[2] = { WhiteKing, BlackKing };
INLINE bool IsBishop(uint8 piece)
{
return piece >= WhiteLight && piece < WhiteRook;
}
static const uint8 CanCastle_OO = 1;
static const uint8 CanCastle_oo = 2;
static const uint8 CanCastle_OOO = 4;
static const uint8 CanCastle_ooo = 8;
static const uint16 FlagCastling = 0x1000; // numerically equal to FlagPriority
static const uint16 FlagPriority = 0x1000; // flag desirable quiet moves
static const uint16 FlagEP = 0x2000;
static const uint16 FlagPKnight = 0x4000;
static const uint16 FlagPBishop = 0x6000;
static const uint16 FlagPRook = 0xA000;
static const uint16 FlagPQueen = 0xC000;
INLINE bool IsPromotion(uint16 move)
{
return T(move & 0xC000);
}
INLINE bool IsCastling(int piece, uint16 move)
{
return piece >= WhiteKing && abs(To(move) - From(move)) == 2;
}
INLINE bool IsEP(uint16 move)
{
return (move & 0xF000) == FlagEP;
}
template<bool me> INLINE uint8 Promotion(uint16 move)
{
uint8 retval = (move & 0xF000) >> 12;
// if (retval == WhiteLight && HasBit(DarkArea, To(move)))
// retval = WhiteDark;
return (me ? 1 : 0) + retval;
}
#ifndef W32_BUILD
INLINE int lsb(uint64 x)
{
register unsigned long y;
_BitScanForward64(&y, x);
return y;
}
INLINE int msb(uint64 x)
{
register unsigned long y;
_BitScanReverse64(&y, x);
return y;
}
INLINE int popcnt(uint64 x)
{
x = x - ((x >> 1) & 0x5555555555555555);
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
return (x * 0x0101010101010101) >> 56;
}
#else
INLINE int lsb(uint64 x) {
_asm {
mov eax, dword ptr x[0]
test eax, eax
jz l_high
bsf eax, eax
jmp l_ret
l_high : bsf eax, dword ptr x[4]
add eax, 20h
l_ret :
}
}
INLINE int msb(uint64 x) {
_asm {
mov eax, dword ptr x[4]
test eax, eax
jz l_low
bsr eax, eax
add eax, 20h
jmp l_ret
l_low : bsr eax, dword ptr x[0]
l_ret :
}
}
INLINE int popcnt(uint64 x) {
unsigned int x1, x2;
x1 = (unsigned int)(x & 0xFFFFFFFF);
x1 -= (x1 >> 1) & 0x55555555;
x1 = (x1 & 0x33333333) + ((x1 >> 2) & 0x33333333);
x1 = (x1 + (x1 >> 4)) & 0x0F0F0F0F;
x2 = (unsigned int)(x >> 32);
x2 -= (x2 >> 1) & 0x55555555;
x2 = (x2 & 0x33333333) + ((x2 >> 2) & 0x33333333);
x2 = (x2 + (x2 >> 4)) & 0x0F0F0F0F;
return ((x1 * 0x01010101) >> 24) + ((x2 * 0x01010101) >> 24);
}
#endif
typedef int(*pop_func_t)(const uint64&);
int pop0(const uint64& b) { return popcnt(b); }
struct pop0_
{
INLINE int operator()(const uint64& b) const { return pop0(b); }
INLINE pop_func_t Imp() const { return pop0; }
};
#ifdef W32_BUILD
#define pop1 pop0
#define pop1_ pop0_
#else
int pop1(const uint64& b) { return static_cast<int>(_mm_popcnt_u64(b)); }
struct pop1_
{
INLINE int operator()(const uint64& b) const { return pop1(b); }
INLINE pop_func_t Imp() const { return pop1; }
};
#endif
static const int MatWQ = 1;
static const int MatBQ = 3 * MatWQ;
static const int MatWR = 3 * MatBQ;
static const int MatBR = 3 * MatWR;
static const int MatWL = 3 * MatBR;
static const int MatBL = 2 * MatWL;
static const int MatWD = 2 * MatBL;
static const int MatBD = 2 * MatWD;
static const int MatWN = 2 * MatBD;
static const int MatBN = 3 * MatWN;
static const int MatWP = 3 * MatBN;
static const int MatBP = 9 * MatWP;
static const int TotalMat = 2 * (MatWQ + MatBQ) + MatWL + MatBL + MatWD + MatBD + 2 * (MatWR + MatBR + MatWN + MatBN) + 8 * (MatWP + MatBP) + 1;
static const int FlagUnusualMaterial = 1 << 30;
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;
};
static const 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<int, 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, 28>, 2> MobQueen;
array<uint8, 256> PieceFromChar;
array<int, 64> ROffset;
array<array<packed_t, 15>, 2> MobBishop, MobRook;
array<array<packed_t, 9>, 2> MobKnight;
array<short, 64> BShift, BOffset, RShift;
array<uint64, 16> CastleKey;
array<array<uint64, 8>, 2> Forward;
array<uint64, 8> EPKey;
array<uint64, 8> File, Line;
array<uint64, 8> West, East, PIsolated;
array<packed_t, 8> PasserGeneral, PasserBlocked, PasserFree, PasserSupported, PasserProtected, PasserConnected, PasserOutside, PasserCandidate, PasserClear;
array<uint8, 64> UpdateCastling;
array<int, 16> MatCode;
array<array<sint16, 8>, 3> Shelter;
array<uint16, 16> KingAttackScale;
array<int, 4> KingCenterScale;
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 & (~(RO->File[0] | RO->Line[7]))) << 7;
}
INLINE uint64 ShiftNE(const uint64& target)
{
return (target & (~(RO->File[7] | RO->Line[7]))) << 9;
}
INLINE uint64 ShiftSE(const uint64& target)
{
return (target & (~(RO->File[7] | RO->Line[0]))) >> 7;
}
INLINE uint64 ShiftSW(const uint64& target)
{
return (target & (~(RO->File[0] | RO->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 RO->Line[me ? 7 - n : n];
}
// Constants controlling play
static const int PliesToEvalCut = 50; // halfway to 50-move
static const int KingSafetyNoQueen = 8; // numerator; denominator is 16
static const int SeeThreshold = 40 * CP_EVAL;
static const int DrawCap = 100 * CP_EVAL;
static const int DeltaDecrement = (3 * CP_SEARCH) / 2; // 5 (+91/3) vs 3
static const int TBMinDepth = 7;
inline int MapPositive(int scale, int param)
{
return param > scale ? param : (scale * scale) / (2 * scale - param);
}
static const int FailLoInit = 22;
static const int FailHiInit = 31;
static const int FailLoGrowth = 37; // numerator; denominator is 64
static const int FailHiGrowth = 26; // numerator; denominator is 64
static const int FailLoDelta = 27;
static const int FailHiDelta = 24;
static const int AspirationEpsilon = 10;
static const int InitiativeConst = 3 * CP_SEARCH;
static const int InitiativePhase = 3 * CP_SEARCH;
static const int FutilityThreshold = 50 * CP_SEARCH;
#define IncV(var, x) (me ? (var -= (x)) : (var += (x)))
#define DecV(var, x) IncV(var, -(x))
static const sint16 KpkValue = 300 * CP_EVAL;
static const sint16 EvalValue = 30000;
static const sint16 MateValue = 32760 - 8 * (CP_SEARCH - 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
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
/*
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
*/
const int MvvLvaVictim[16] = { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3 };
const int MvvLvaAttacker[16] = { 0, 0, 5, 5, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 6, 6 };
const 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]; }
static const int MaxPawnCaptureMvvLva = MvvLvaAttacker[15]; // 6
INLINE int KnightCaptureMvvLva(int attacker) { return MaxPawnCaptureMvvLva + MvvLvaAttackerKB[attacker]; }
static const int MaxKnightCaptureMvvLva = MaxPawnCaptureMvvLva + MvvLvaAttackerKB[15]; // 17
INLINE int BishopCaptureMvvLva(int attacker) { return MaxPawnCaptureMvvLva + MvvLvaAttackerKB[attacker] + 1; }
static const int MaxBishopCaptureMvvLva = MaxPawnCaptureMvvLva + MvvLvaAttackerKB[15] + 1; // usually 18
INLINE int RookCaptureMvvLva(int attacker) { return MaxBishopCaptureMvvLva + MvvLvaAttacker[attacker]; }
static const 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]; }
static const int RefOneScore = (0xFF << 16) | (3 << 24);
static const 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;
}
static const int FlagHashCheck = 1 << 20; // first 20 bits are reserved for the hash killer and extension
static const int FlagHaltCheck = 1 << 21;
static const int FlagCallEvaluation = 1 << 22;
static const int FlagDisableNull = 1 << 23;
static const int FlagNeatSearch = FlagHashCheck | FlagHaltCheck | FlagCallEvaluation;
static const int FlagNoKillerUpdate = 1 << 24;
static const 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;
static const uint8 FlagSort = 1 << 0;
static const uint8 FlagNoBcSort = 1 << 1;
GData SaveData[1];
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
};
static const 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 };
static const 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;
};
static const GPawnEntry NullPawnEntry = { 0, 0, {0, 0}, {0, 0}, {0, 0} };
static const int PAWN_HASH_SIZE = 1 << 20;
__declspec(align(64)) array<GPawnEntry, PAWN_HASH_SIZE> PawnHash;
static const 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;
static const GPVEntry NullPVEntry = { 0, 0, 0, 1, 0, 0, 0, 0, 0 };
static const int pv_hash_size = 1 << 20;
static const int pv_cluster_size = 1 << 2;
static const 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;
static const sint64 CyclesPerMSec = 3400000;
#endif
int Aspiration = 1, LargePages = 1;
static const int TimeSingTwoMargin = 20;
static const int TimeSingOneMargin = 30;
static const int TimeNoPVSCOMargin = 60;
static const int TimeNoChangeMargin = 70;
static const int TimeRatio = 120;
static const int PonderRatio = 120;
static const int MovesTg = 30;
static const int InfoLag = 5000;
static const int InfoDelay = 1000;
sint64 StartTime, InfoTime, CurrTime;
uint16 SMoves[256];
jmp_buf Jump, ResetJump;