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
|
/**
Copyright (c) Scott Gasch
Module Name:
main.c
Abstract:
Program entry point and setup code.
Author:
Scott Gasch ([email protected]) 8 Apr 2004
Revision History:
$Id: main.c 355 2008-07-01 15:46:43Z scott $
**/
#include "chess.h"
#include <errno.h>
FILE *g_pfLogfile = NULL;
GAME_OPTIONS g_Options;
CHAR *g_szInitialCommand = NULL;
ULONG g_uInputThreadHandle = (ULONG)-1;
void
Banner(void)
/**
Routine description:
Describe the compile/build options and program version.
Parameters:
void
Return value:
void
**/
{
char *p;
Trace("Typhoon %s (built on %s %s, commit %s):\n", VERSION, __DATE__, __TIME__, GIT_COMMIT);
Trace(" Copyright (C) 2000-2026, Scott Gasch ([email protected])\n");
Trace(" " COMPILER_STRING);
Trace(" Make profile used: %s\n", PROFILE);
#ifdef MP
Trace(" Multiprocessor enabled; %u searcher thread%s\n",
g_Options.uNumProcessors, (g_Options.uNumProcessors > 1) ? "s" : "");
#else
Trace(" Single processor version\n");
#endif
#ifdef DEBUG
Trace(" DEBUG build\n");
#endif
#ifdef TEST
Trace(" Testcode compiled in\n");
#endif
#ifdef EVAL_DUMP
Trace(" Evaluation terms\n");
#endif
#ifdef EVAL_TIME
Trace(" Evaluation timing\n");
#endif
#ifdef PERF_COUNTERS
Trace(" Performance counters\n");
#ifdef TEST_NULL
Trace(" Testing nullmove prediction algorithms\n");
#endif
#endif
#ifdef TREEDUMP
Trace(" TREEDUMP search tree tracing enabled (treedump.log)\n");
#endif
Trace(" Hash sizes: %u Mb (main), %u Mb / thread (pawn)\n",
(g_uHashTableSizeEntries * sizeof(HASH_ENTRY)) / MB,
PAWN_HASH_TABLE_SIZE * sizeof(PAWN_HASH_ENTRY) / MB);
Trace(" QCheckPlies: %u\n", QPLIES_OF_NON_CAPTURE_CHECKS);
Trace(" FutilityBase: full=%u lazy=%u superlazy=%u\n",
FUTILITY_BASE_MARGIN_FULL, FUTILITY_BASE_MARGIN_LAZY,
FUTILITY_BASE_MARGIN_SUPERLAZY);
p = ExportEvalDNA();
Trace(" Logging Eval DNA.\n");
Log(p);
free(p);
#ifdef DO_IID
Trace(" IID R-factor: %u ply\n", IID_R_FACTOR / ONE_PLY);
#endif
}
void
EndLogging(void)
/**
Routine description:
Close the main logfile.
Parameters:
void
Return value:
void
**/
{
if (NULL != g_pfLogfile) {
fflush(g_pfLogfile);
fflush(stdout);
fclose(g_pfLogfile);
g_pfLogfile = NULL;
}
}
FLAG
InitializeLogging(void)
/**
Routine description:
Open the main logfile.
Parameters:
void
Return value:
FLAG
**/
{
if (NULL != g_pfLogfile) {
EndLogging();
}
if (!strlen(g_Options.szLogfile) ||
!strcmp(g_Options.szLogfile, "-")) {
return(FALSE);
}
// If the logfile we want to write already exists, append to it.
FLAG fReusingLogfile = SystemDoesFileExist(g_Options.szLogfile);
g_pfLogfile = fopen(g_Options.szLogfile, "ab+");
if (NULL == g_pfLogfile) {
Trace("WARNING: Can't open logfile %s for write (%s); "
"continuing without logging.\n",
g_Options.szLogfile, strerror(errno));
return(FALSE);
}
if (TRUE == fReusingLogfile) {
time_t tNow = time(NULL);
CHAR szTimestamp[64];
strftime(szTimestamp, sizeof(szTimestamp), "%Y-%m-%d %H:%M:%S",
localtime(&tNow));
fprintf(g_pfLogfile,
"----- New engine run re-using this logfile (%s) -----\n",
szTimestamp);
}
return(TRUE);
}
FLAG
PreGameReset(FLAG fResetBoard)
/**
Routine description:
Reset internal state between games.
Parameters:
void
Return value:
FLAG
**/
{
if (TRUE == fResetBoard)
{
SetRootToInitialPosition();
}
SetMyName();
ClearDynamicMoveOrdering();
ClearHashTable();
ResetOpeningBook();
return(TRUE);
}
static ULONG
_ParseHashOption(char *p,
ULONG uEntrySize)
{
ULONG u, v;
CHAR c;
if ((!STRCMPI(p, "none")) || (!STRCMPI(p, "no"))) return(0);
if (2 == sscanf(p, "%u%1c", &u, &c))
{
v = u;
switch(c)
{
case 'K':
case 'k':
u *= 1024;
break;
case 'M':
case 'm':
u *= 1024 * 1024;
break;
case 'G':
case 'g':
u *= 1024 * 1024 * 1024;
break;
default:
Trace("Error (unrecognized size): \"%s\"\n", p);
break;
}
if (u < v)
{
Trace("Error (size too large): \"%s\"\n", p);
u = v;
}
u /= uEntrySize;
}
else
{
u = (ULONG)atoi(p);
}
while(!IS_A_POWER_OF_2(u))
{
u &= (u - 1);
}
return(u);
}
void
InitializeOptions(int argc, char *argv[])
/**
Routine description:
Set global options to their initial state. This code also is
responsible for parsing the commandline in order to override the
default state of global options.
Parameters:
int argc,
char *argv[]
Return value:
void
**/
{
int i;
//
// Defaults
//
memset(&g_Options, 0, sizeof(g_Options));
g_szInitialCommand = NULL;
g_Options.uMyClock = g_Options.uOpponentsClock = 600;
g_Options.fGameIsRated = FALSE;
g_Options.fOpponentIsComputer = FALSE;
g_Options.uSecPerMove = 0;
g_Options.uMaxDepth = MAX_PLY_PER_SEARCH - 1;
g_Options.fShouldPonder = TRUE;
g_Options.fPondering = g_Options.fThinking = FALSE;
g_Options.mvPonder.uMove = 0;
g_Options.fShouldPost = TRUE;
g_Options.fForceDrawWorthZero = FALSE;
g_Options.uMyIncrement = 0;
g_Options.uMovesPerTimePeriod = 0;
g_Options.szAnalyzeProgressReport[0] = '\0';
g_Options.fShouldAnnounceOpening = TRUE;
g_Options.eClock = CLOCK_NONE;
g_Options.eGameType = GAME_UNKNOWN;
g_Options.ePlayMode = FORCE_MODE;
g_Options.fNoInputThread = FALSE;
g_Options.fVerbosePosting = TRUE;
strcpy(g_Options.szEGTBPath, "/zscratch/egtb");
strcpy(g_Options.szLogfile, "/usr/local/tmp/typhoon.log");
strcpy(g_Options.szBookName, "book.bin");
g_Options.uNumHashTableEntries = _ParseHashOption("256m", sizeof(HASH_ENTRY));
g_Options.uNumProcessors = 1;
g_Options.fStatusLine = TRUE;
g_Options.iResignThreshold = 0;
g_Options.dPostThrottleSec = 0.35;
g_Options.u64MaxNodeCount = 0ULL;
i = 1;
while(i < argc)
{
// Accept both "--opt value" (two argv tokens) and "--opt=value"
// (one token, '=' separated) -- a user typing --cpus=4 used to
// silently fail STRCMPI's exact-name match against "--cpus",
// fall through to the "unknown argument" branch, and leave
// uNumProcessors at its default of 1 with no indication why.
// Split any "--name=value" token into pszArg/pszInlineVal here
// so every branch below can keep comparing/consuming exactly
// as before; only the value-source (same token vs. next argv
// slot) changes.
char szArgNameBuf[SMALL_STRING_LEN_CHAR];
char *pszArg = argv[i];
char *pszInlineVal = NULL;
char *pEquals = strchr(argv[i], '=');
if ((NULL != pEquals) && ('-' == argv[i][0]))
{
size_t uNameLen = (size_t)(pEquals - argv[i]);
if (uNameLen < sizeof(szArgNameBuf))
{
strncpy(szArgNameBuf, argv[i], uNameLen);
szArgNameBuf[uNameLen] = '\0';
pszArg = szArgNameBuf;
pszInlineVal = pEquals + 1;
}
}
// The "value" argument for a flag that takes one: either the
// inline "=value" just split off above, or the next argv
// token -- and whether that next token is even present. Must
// check argc > i+1, not argc > i -- the while loop's own
// condition already guarantees i < argc, so "argc > i" was
// always true and let the two-token branches below read
// argv[i+1] one past the end of argv when a value-taking flag
// was the last argument on the command line.
FLAG fHaveVal = (NULL != pszInlineVal) || (argc > i+1);
char *pszVal = (NULL != pszInlineVal) ? pszInlineVal :
((argc > i+1) ? argv[i+1] : NULL);
// Only the two-token form consumes an extra argv slot.
FLAG fConsumedNextArg = (NULL == pszInlineVal);
#ifdef MP
if ((!STRCMPI(pszArg, "--cpus")) && fHaveVal)
{
g_Options.uNumProcessors = (ULONG)atoi(pszVal);
if ((g_Options.uNumProcessors == 0) ||
(g_Options.uNumProcessors > 64))
{
g_Options.uNumProcessors = 2;
}
if (fConsumedNextArg) i++;
}
else
#endif
if ((!STRCMPI(pszArg, "--command")) && fHaveVal)
{
ULONG uLen = (ULONG)strlen(pszVal);
g_szInitialCommand = (CHAR *)SystemAllocateMemory(uLen + 3);
strcpy(g_szInitialCommand, pszVal);
strcat(g_szInitialCommand, "\r\n");
if (fConsumedNextArg) i++;
}
else if ((!STRCMPI(pszArg, "--hash")) && fHaveVal)
{
g_Options.uNumHashTableEntries =
_ParseHashOption(pszVal,
sizeof(HASH_ENTRY));
if (fConsumedNextArg) i++;
}
else if ((!STRCMPI(pszArg, "--egtbpath")) && fHaveVal)
{
if (!strcmp(pszVal, "-")) {
g_Options.szEGTBPath[0] = '\0';
} else {
strncpy(g_Options.szEGTBPath, pszVal, SMALL_STRING_LEN_CHAR);
}
if (fConsumedNextArg) i++;
}
else if (!STRCMPI(pszArg, "--book") && fHaveVal)
{
if (!strcmp(pszVal, "-")) {
g_Options.szBookName[0] = '\0';
} else {
strncpy(g_Options.szBookName, pszVal, SMALL_STRING_LEN_CHAR);
}
if (fConsumedNextArg) i++;
}
else if (!STRCMPI(pszArg, "--batch"))
{
g_Options.fNoInputThread = TRUE;
}
else if ((!STRCMPI(pszArg, "--nnueweights")) && fHaveVal)
{
// Loads and enables NNUE eval at startup -- for scripted
// match-play (eval_tune/match_play.py et al) spawning a
// fresh engine process per game, where there's no
// reliable way to send an interactive "nnue load"/"nnue
// on" command pair before the first "go". Interactive use
// can still do the same thing via the 'nnue' command.
if (!NnueLoadWeights(pszVal))
{
UtilPanic(INITIALIZATION_FAILURE,
(void *)"Bad NNUE weights file",
(void *)&(pszVal),
(void *)"load",
NULL,
__FILE__, __LINE__);
}
else
{
g_fUseNnueEval = TRUE;
Trace("Loaded NNUE weights \"%s\", NNUE eval enabled.\n",
pszVal);
}
if (fConsumedNextArg) i++;
}
else if ((!STRCMPI(pszArg, "--nnueprobeweights")) && fHaveVal)
{
// Same reasoning as --nnueweights above -- loads and
// enables the lazy-eval accumulator probe gate at startup
// for scripted match-play, where there's no interactive
// "nnue probeload"/"nnue lazyprobeon" command pair
// available. Does NOT imply --nnueweights -- pass both to
// get the full NNUE+lazy-gate configuration; this alone
// without NNUE eval enabled would load the probe but never
// reach the code path that consults it.
if (!NnueProbeLoadWeights(pszVal))
{
UtilPanic(INITIALIZATION_FAILURE,
(void *)"Bad NNUE probe weights file",
(void *)&(pszVal),
(void *)"probeload",
NULL,
__FILE__, __LINE__);
}
else
{
g_fUseNnueLazyProbe = TRUE;
Trace("Loaded NNUE probe weights \"%s\", lazy probe gate enabled.\n",
pszVal);
}
if (fConsumedNextArg) i++;
}
else if ((!STRCMPI(pszArg, "--nnuelazymargin")) && fHaveVal)
{
// Scripted-match-play equivalent of "nnue lazymargin <cp>"
// -- flat override for every material bucket's lazy-probe
// margin, see nnueeval.c/eval.c. 2026-10-16 looser-margin
// experiment.
g_iNnueLazyMarginOverride = atoi(pszVal);
Trace("NNUE lazy margin override set to %d cp.\n",
g_iNnueLazyMarginOverride);
if (fConsumedNextArg) i++;
}
else if ((!STRCMPI(pszArg, "--nnuesuperlazymargin")) && fHaveVal)
{
// Scripted equivalent of "nnue superlazyon <cp>".
g_iNnueSuperLazyMargin = (SCORE)atoi(pszVal);
g_fUseNnueSuperLazy = TRUE;
Trace("NNUE super-lazy pre-filter enabled, margin=%d cp.\n",
g_iNnueSuperLazyMargin);
if (fConsumedNextArg) i++;
}
else if ((!STRCMPI(pszArg, "--dnafile")) && fHaveVal)
{
if (!ReadEvalDNA(pszVal))
{
UtilPanic(INITIALIZATION_FAILURE,
(void *)"Bad DNA file",
(void *)&(pszVal),
(void *)"read",
NULL,
__FILE__, __LINE__);
}
else
{
Trace("Loaded dna file \"%s\"\n", pszVal);
}
if (fConsumedNextArg) i++;
}
else if ((!STRCMPI(pszArg, "--logfile")) && fHaveVal)
{
strncpy(g_Options.szLogfile, pszVal,
SMALL_STRING_LEN_CHAR);
if (fConsumedNextArg) i++;
}
else if (!STRCMPI(pszArg, "--help")) {
Trace("Usage: %s [--batch] [--command arg] [--logfile arg] [--egtbpath arg]\n"
" [--dnafile arg] [--cpus arg] [--hash arg]\n\n"
" --batch : operate the engine without an input thread\n"
" --book : specify the opening book to use or '-' for none\n"
" --command : specify initial command(s) (requires arg)\n"
" --cpus : indicate the number of cpus to use (1..64)\n"
" --hash : indicate desired hash size (e.g. 16m, 1g)\n"
" --egtbpath : supplies the egtb path or '-' for none\n"
" --logfile : indicate desired output logfile name or '-' for none\n"
" --dnafile : indicate desired eval profile input (requres arg)\n\n"
"Example: %s --hash 256m --cpus 2\n"
" %s --logfile test.out --batch --command \"test\" --dnafile test.in\n", argv[0], argv[0], argv[0]);
exit(-1);
}
else
{
Trace("Error (unknown argument): \"%s\"; try --help\n", argv[i]);
}
//
// TODO: parse other commandline options and override above
//
i++;
}
// 2026-10-25: NNUE is now the default evaluator, same convention
// as szEGTBPath's hardcoded /zscratch/egtb default above -- try to
// load a weights file sitting next to the binary (matching
// typhoon_nnue's own NNUE_WEIGHTS default) if the user didn't
// already enable NNUE explicitly via --nnueweights or an
// interactive "nnue load"/"nnue on". Fails soft: a checkout
// without eval.nnue present (or a bad/missing file) just falls
// back to HCE, exactly like it always did -- this is a default,
// not a requirement, so there's no UtilPanic here the way
// --nnueweights's own explicit-request failure path has.
if ((FALSE == g_fUseNnueEval) &&
(TRUE == SystemDoesFileExist("eval.nnue")))
{
if (NnueLoadWeights("eval.nnue"))
{
g_fUseNnueEval = TRUE;
Trace("Loaded default NNUE weights \"eval.nnue\", "
"NNUE eval enabled.\n");
}
else
{
Trace("Found \"eval.nnue\" but failed to load it; "
"falling back to HCE eval.\n");
}
}
}
void
CleanupOptions(void)
/**
Routine description:
Cleanup options...
Parameters:
void
Return value:
void
**/
{
NOTHING;
}
FLAG
MainProgramInitialization(int argc, char *argv[])
/**
Routine description:
Perform main program initialization. This includes stuff like:
precomputing some data structures, resetting the global options,
parsing the commandline to override said options, and calling out
to other modules to tell them to initialize themselves.
Parameters:
int argc,
char *argv[]
Return value:
FLAG
**/
{
const char *pszForcedSeed = getenv("TYPHOON_SEED");
unsigned int uSeed = pszForcedSeed ?
(UINT)strtoul(pszForcedSeed, NULL, 10) :
(UINT)time(0);
Trace("srand seed: %u\n", uSeed);
srand(uSeed);
InitializeOptions(argc, argv);
TDOpen();
InitializeEGTB();
InitializeSigSystem();
InitializeInteriorNodeRecognizers();
InitializeWhiteSquaresTable();
InitializeVectorDeltaTable();
InitializeSwapTable();
InitializeDistanceTable();
InitializeRookRayTables();
InitializeBishopRayTables();
InitializeKnightAttackTables();
InitializeKingAttackTables();
InitializePawnAttackOriginTable();
InitializeBitboardMagic();
InitializeOpeningBook();
InitializeDynamicMoveOrdering();
InitializeLMRTable();
InitializeWilsonBoundTable();
InitializeHashSystem();
InitializeEval();
#ifdef MP
InitializeParallelSearch();
#endif
VERIFY(PreGameReset(TRUE));
InitializeLogging();
return TRUE;
}
FLAG
MainProgramCleanup(void)
/**
Routine description:
Perform main program cleanup. Tear down the stuff we set up in
MainProgramInitialization.
Parameters:
void
Return value:
FLAG
**/
{
CleanupOpeningBook();
CleanupEGTB();
CleanupDynamicMoveOrdering();
#ifdef MP
CleanupParallelSearch();
#endif
CleanupHashSystem();
#ifdef FH_REGRESSION_COLLECTION
// Flush every thread's buffered-but-unflushed collection records
// (fhcollect.c) before exiting -- otherwise up to
// FH_COLLECT_BUFFER_RECORDS - 1 records per thread would be
// silently lost on a clean shutdown.
FhCollectCloseAll();
#endif
TDClose();
CleanupOptions();
EndLogging();
return(TRUE);
}
#ifdef TEST
FLAG
RunStartupProgramTest(void)
/**
Routine description:
If we built this version with -DTEST then this code is the start
of the self-test that runs at program startup time.
Parameters:
void
Return value:
FLAG
**/
{
#if defined(_X86_) || defined(_X64_)
ULONG u, x, y;
Trace("Testing CPP macros...\n");
for (u = 0; u < 1000000; u++)
{
x = rand() * rand();
y = rand() * rand();
x = x & ~(0x80000000);
y = y & ~(0x80000000);
if (MAX(x, y) != MAXU(x, y))
{
UtilPanic(TESTCASE_FAILURE,
NULL, "max maxu", NULL, NULL, __FILE__, __LINE__);
}
if (MAXU(x, y) != MAXU(y, x))
{
UtilPanic(TESTCASE_FAILURE,
NULL, "maxu assoc", NULL, NULL, __FILE__, __LINE__);
}
if (MIN(x, y) != MINU(x, y))
{
UtilPanic(TESTCASE_FAILURE,
NULL, "min minu", NULL, NULL, __FILE__, __LINE__);
}
if (MINU(x, y) != MINU(y, x))
{
UtilPanic(TESTCASE_FAILURE,
NULL, "minu assoc", NULL, NULL, __FILE__, __LINE__);
}
if (abs(x - y) != ABS_DIFF(x, y))
{
UtilPanic(TESTCASE_FAILURE,
NULL, "abs_diff", NULL, NULL, __FILE__, __LINE__);
}
if (ABS_DIFF(x, y) != ABS_DIFF(y, x))
{
UtilPanic(TESTCASE_FAILURE,
NULL, "abs_diff assoc", NULL, NULL, __FILE__, __LINE__);
}
}
#endif
// TODO(temporary): TestRecogn hoisted to the front of the self-test
// sequence while iterating on recogn.c so a failure doesn't require
// waiting through the slow move-gen speed benchmarks first. Move
// back down next to TestMakeUnmakeMove (its natural home) once
// recogn.c work settles.
TestRecogn();
// Re-enabled at user's request to look at failing FENs; user will
// disable when done.
//TestEvalSamplePasserRacesKPPKPP();
//TestEvalSamplePasserRacesKPKPPP();
//TestEvalPasserRacesAgainstLoneKings();
TestDraw();
#ifdef EVAL_DUMP
TestEval();
#endif
TestBitboards();
TestSan();
TestIcs();
TestGetAttacks();
TestSeeFastSign();
TestSEENoDefenderFastPath();
TestWhoAttacksSquareMagic();
TestMoveGenerator();
TestLegalMoveGenerator();
TestNnueAccumulator();
TestGenerateAllMovesSpeed();
#if defined(CAPTURES_PROMS_BITBOARD_PATH)
TestCapturesAndPromotionsGenerator();
TestGenerateCapturesAndPromotionsSpeed();
#endif
TestFindUnblockedSquaresSpeed();
TestFenCode();
TestLiftPlaceSlidePiece();
TestExposesCheck();
TestIsAttacked();
TestMakeUnmakeMove();
TestSearch();
TestLocklessHashAlgebra();
TestQSearchSanity();
return(TRUE);
}
#endif // TEST
void DeclareTerminalStates(GAME_RESULT result) {
switch(result.eResult) {
case RESULT_IN_PROGRESS:
return;
case RESULT_BLACK_WON:
Trace("0-1 {%s}\n", result.szDescription);
SetGameResultAndDescription(result);
DumpPgn();
g_Options.ePlayMode = FORCE_MODE;
break;
case RESULT_WHITE_WON:
Trace("1-0 {%s}\n", result.szDescription);
SetGameResultAndDescription(result);
DumpPgn();
g_Options.ePlayMode = FORCE_MODE;
break;
case RESULT_DRAW:
Trace("1/2-1/2 {%s}\n", result.szDescription);
Trace("tellics draw\n");
SetGameResultAndDescription(result);
DumpPgn();
g_Options.ePlayMode = FORCE_MODE;
break;
case RESULT_ABANDONED:
case RESULT_UNKNOWN:
ASSERT(FALSE);
SetGameResultAndDescription(result);
DumpPgn();
g_Options.ePlayMode = FORCE_MODE;
break;
}
}
// Main work loop
void MainProgramLoop() {
POSITION pos;
GAME_RESULT result;
FLAG fThink;
FLAG fPonder;
while (!g_fExitProgram) {
// Prepare to search or ponder
memcpy(&pos, GetRootPosition(), sizeof(POSITION));
fThink = fPonder = FALSE;
result.eResult = RESULT_IN_PROGRESS;
result.szDescription[0] = '\0';
// What should we do?
switch(g_Options.ePlayMode) {
case I_PLAY_WHITE:
fThink = (pos.uToMove == WHITE);
fPonder = (pos.uToMove == BLACK);
break;
case I_PLAY_BLACK:
fThink = (pos.uToMove == BLACK);
fPonder = (pos.uToMove == WHITE);
break;
case FORCE_MODE:
default:
fThink = fPonder = FALSE;
break;
}
if (fThink) {
result = Think(&pos);
if ((NumberOfPendingInputEvents() != 0) &&
(FALSE == g_fExitProgram))
{
MakeStatusLine();
ParseUserInput(FALSE);
}
DeclareTerminalStates(result);
} else if (fPonder) {
if (g_Options.fShouldPonder) {
result = Ponder(&pos);
}
if (FALSE == g_fExitProgram)
{
MakeStatusLine();
ParseUserInput(FALSE);
}
DeclareTerminalStates(result);
} else {
MakeStatusLine();
ParseUserInput(FALSE);
}
}
}
int CDECL
main(int argc, char *argv[])
/**
Routine description:
Chess engine entry point and main loop.
Parameters:
int argc,
char *argv[]
Return value:
int CDECL
**/
{
// Initial setup work...
Trace("Setting up, please wait...\n");
if (FALSE == SystemDependentInitialization())
{
Trace("main: Operating system dependent init code failed, "
"aborting.\n");
exit(-1);
}
if (FALSE == MainProgramInitialization(argc, argv))
{
Trace("main: Main program init code failed, aborting.\n");
exit(-1);
}
Banner();
#ifdef TEST
(void)RunStartupProgramTest();
#endif
if (TRUE == g_Options.fNoInputThread)
{
InitInputSystemInBatchMode();
}
else
{
g_uInputThreadHandle = InitInputSystemWithDedicatedThread();
}
// Enter the main work loop code...
MainProgramLoop();
// If we get here g_fExitProgram is set -- clean up.
Trace("MAIN THREAD: thread terminating.\n");
if (FALSE == MainProgramCleanup())
{
Trace("main: Main program cleanup code failed, aborting.\n");
exit(-1);
}
exit(0);
}
|