summaryrefslogtreecommitdiff
path: root/src/testsee.c
blob: 4afe60fe17127a2348df04cea95b8c5e84eee179 (plain)
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
/**

Copyright (c) Scott Gasch

Module Name:

    testsee.c

Abstract:

    This code is meant to sanity check the SEE routine.  It works by
    using the generator and MakeMove/UnmakeMove to play out a sequence
    of moves on a single square.  It's somewhat useful code but right
    now I don't really use it because it is too good: it detects
    things like pieces that are pinned against the king whereas the
    SEE itself does not.  Still, I'd want to run it and verify the
    discrepencies if I was doing much work in the SEE code.

Author:

    Scott Gasch ([email protected]) 21 Oct 2004

Revision History:

**/

#include "chess.h"

#ifdef TEST_BROKEN
ULONG g_uRootOnMove;

SCORE
DebugSEERecursive(SEARCHER_THREAD_CONTEXT *ctx,
                  MOVE mv)
{
    SCORE i, iMinMax;
    ULONG x;
    MOVE mvReply;
    ULONG uToMove = ctx->sPosition.uToMove;

    //
    // Make the move, this is not optional
    //
    ASSERT(GET_COLOR(mv.pMoved) == uToMove);
    if (FALSE == MakeMove(ctx, mv))
    {
        return(INVALID_SCORE);
    }

    //
    // Initialize MinMax to the stand pat score here so that the reply
    // can be "something else".
    //
    iMinMax = ((PawnMaterial(&ctx->sPosition, g_uRootOnMove) +
                ctx->sPosition.uNonPawnMaterial[g_uRootOnMove]) -
               (PawnMaterial(&ctx->sPosition, FLIP(g_uRootOnMove)) +
                ctx->sPosition.uNonPawnMaterial[FLIP(g_uRootOnMove)]));

    //
    // Generate the reply moves.
    //
    GenerateMoves(ctx, (MOVE){0}, GENERATE_DONT_SCORE);
    for (x = ctx->sMoveStack.uBegin[ctx->uPly];
         x < ctx->sMoveStack.uEnd[ctx->uPly];
         x++)
    {
        //
        // Only consider replies that end up on the same sq as the
        // move.
        //
        mvReply = ctx->sMoveStack.mvf[x].mv;
        ASSERT(SanityCheckMove(&ctx->sPosition, mvReply));
        if (mvReply.cTo != mv.cTo)
        {
            continue;
        }
        
        i = DebugSEERecursive(ctx, mvReply);
        if (INVALID_SCORE != i)
        {
            if ((ctx->uPly % 2) == 0)
            {
                if (i > iMinMax) iMinMax = i;
            }
            else
            {
                if (i < iMinMax) iMinMax = i;
            }
        }
    }

    //
    // Unmake the original move
    //
    UnmakeMove(ctx, mv);
    return(iMinMax);
}

    
SCORE
DebugSEE(POSITION *pos,
         MOVE mv)
{
    SCORE iRootBalance = ((PawnMaterial(pos, pos->uToMove) +
                           pos->uNonPawnMaterial[pos->uToMove]) -
                          (PawnMaterial(pos, FLIP(pos->uToMove)) +
                           pos->uNonPawnMaterial[FLIP(pos->uToMove)]));
    SCORE iAfterExchange = iRootBalance;
    SEARCHER_THREAD_CONTEXT *ctx = 
        malloc(sizeof(SEARCHER_THREAD_CONTEXT));

    g_uRootOnMove = pos->uToMove;
    if (NULL != ctx)
    {
        pos->uDangerCount[BLACK] = pos->uDangerCount[WHITE] = 0;
        InitializeSearcherContext(pos, ctx);
        
        GenerateMoves(ctx, (MOVE){0}, GENERATE_DONT_SCORE);
        mv.bvFlags |= WouldGiveCheck(ctx, mv);
        if (InCheck(pos, pos->uToMove))
        {
            mv.bvFlags |= MOVE_FLAG_ESCAPING_CHECK;
        }

        iAfterExchange = DebugSEERecursive(ctx, mv);
        if (iAfterExchange != INVALID_SCORE)
        {
            iAfterExchange -= iRootBalance;
        }
        free(ctx);
    }
    return(iAfterExchange);
}
#endif

#ifdef TEST
static int
_SeeListEntryCompare(const void *pA, const void *pB)
{
    const SEE_THREESOME *a = (const SEE_THREESOME *)pA;
    const SEE_THREESOME *b = (const SEE_THREESOME *)pB;
    if (a->cLoc != b->cLoc) return ((int)a->cLoc - (int)b->cLoc);
    return ((int)a->pPiece - (int)b->pPiece);
}

FLAG
SeeListsAreEqual(SEE_LIST *pA, SEE_LIST *pB)
{
    // Order-independent: GetAttacks's caller (SEE()) sorts/heaps the
    // list immediately after it's populated, so a bitboard-based
    // GetAttacks returning the same *set* of attackers in a different
    // order is a correct match, not a bug (board_representation/
    // MIGRATION.md section 4). Sort a scratch copy of each by
    // (cLoc, pPiece) before comparing field-by-field.
    SEE_LIST sA = *pA;
    SEE_LIST sB = *pB;
    ULONG u;

    if (sA.uCount != sB.uCount) return FALSE;
    qsort(sA.data, sA.uCount, sizeof(sA.data[0]), _SeeListEntryCompare);
    qsort(sB.data, sB.uCount, sizeof(sB.data[0]), _SeeListEntryCompare);
    for (u = 0; u < sA.uCount; u++)
    {
        if ((sA.data[u].pPiece != sB.data[u].pPiece) ||
            (sA.data[u].cLoc != sB.data[u].cLoc) ||
            (sA.data[u].uVal != sB.data[u].uVal))
        {
            return FALSE;
        }
    }
    return TRUE;
}

// This used to diff _GetAttacksBB against the mailbox asm GetAttacks and
// the C SlowGetAttacks reference implementation; both were retired
// 2026-09-06 (board_representation/MIGRATION.md section 7) once that
// three-way comparison had run clean for long enough. With no second
// implementation left to diff against, this is now just a crash/
// no-degenerate-output smoke test plus a standalone cycles/call number.
void
TestGetAttacks(void)
{
    POSITION pos;
    ULONG u;
    COOR c;
    SEE_LIST rgBBList;
    ULONG color;

    Trace("Testing GetAttacks...\n");
    for (u = 0; u < 20000; u++)
    {
        GenerateRandomLegalPosition(&pos);
        FOREACH_SQUARE(c)
        {
            if (!IS_ON_BOARD(c)) continue;
            for (color = BLACK; color <= WHITE; color++)
            {
                _GetAttacksBB(&rgBBList, &pos, c, color);
                ASSERT(rgBBList.uCount <= ARRAY_LENGTH(rgBBList.data));
            }
        }
    }

    //
    // Speed: standalone cycles/call number, three positions spanning
    // piece density (opening/middlegame/endgame). No longer a
    // comparison (nothing left to compare against), just a number to
    // watch for regressions over time.
    {
        static const char *rgszFen[3] =
        {
            "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
            "r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P4/2NBPN2/PP3PPP/R1BQ1RK1 w - - 0 1",
            "8/5k2/8/3K4/8/8/8/4R3 w - - 0 1",
        };
        static const char *rgszLabel[3] =
        {
            "opening   ", "middlegame", "endgame   ",
        };
        POSITION posBench;
        SEE_LIST rgList;
        UINT64 u64BBTotal, u64Start;
        ULONG uIter;
        ULONG uSq;
        COOR cBench;
        ULONG uSide;
        const ULONG uCallsPerPosition = 200000;

        Trace("Benchmarking _GetAttacksBB (%lu calls/position)...\n",
              uCallsPerPosition);
        for (u = 0; u < 3; u++)
        {
            FenToPosition(&posBench, (char *)rgszFen[u]);
            u64BBTotal = 0;
            for (uIter = 0; uIter < uCallsPerPosition; uIter++)
            {
                uSq = uIter % 64;
                cBench = BIT_NUMBER_TO_COOR(uSq);
                uSide = uIter & 1;
                if (!IS_ON_BOARD(cBench)) continue;

                u64Start = SystemReadTimeStampCounter();
                _GetAttacksBB(&rgList, &posBench, cBench, uSide);
                u64BBTotal += (SystemReadTimeStampCounter() - u64Start);
            }
            printf(" %s: _GetAttacksBB %" COMPILER_LONGLONG_UNSIGNED_FORMAT
                   " cycles/call\n",
                   rgszLabel[u],
                   u64BBTotal / uCallsPerPosition);
        }
    }
}

// SeeFastSign (see.c) is a sign-only shortcut for SEE(pos, mv) >= 0 --
// GetMoveSee's only consumer (ComputeMoveExtension, searchsup.c) never
// reads the magnitude, only the sign, so this skips SEE()'s list-
// build/heap/xray work whenever the defending side has zero attackers
// on the target square (a case where SEE()'s own exchange loop would
// terminate on its first iteration anyway). Correctness requirement,
// per Scott: assert the sign always matches what real SEE() returns --
// this isn't meant to be an approximation, so any mismatch is a real
// bug in the fast path, not tolerable noise.
void
TestSeeFastSign(void)
{
    SEARCHER_THREAD_CONTEXT *ctx;
    POSITION posBench;
    MOVE rgmv[MAX_MOVE_STACK];
    ULONG uMoveCount;
    ULONG u, x;
    FLAG fFast, fReal;
    const ULONG uPositions = 3000;

    Trace("Testing SeeFastSign (sign vs real SEE)...\n");
    ctx = (SEARCHER_THREAD_CONTEXT *)malloc(sizeof(SEARCHER_THREAD_CONTEXT));
    if (NULL == ctx)
    {
        return;
    }

    for (u = 0; u < uPositions; u++)
    {
        GenerateRandomLegalPosition(&posBench);
        InitializeSearcherContext(&posBench, ctx);
        GenerateMoves(ctx, (MOVE){0}, GENERATE_DONT_SCORE);

        uMoveCount = 0;
        for (x = ctx->sMoveStack.uBegin[ctx->uPly];
             x < ctx->sMoveStack.uEnd[ctx->uPly];
             x++)
        {
            rgmv[uMoveCount++] = ctx->sMoveStack.mvf[x].mv;
        }

        for (x = 0; x < uMoveCount; x++)
        {
            fFast = SeeFastSign(&(ctx->sPosition), rgmv[x]);
            fReal = (SEE(&(ctx->sPosition), rgmv[x]) >= 0);
            if (fFast != fReal)
            {
                Trace("move=0x%x from=%d to=%d moved=%d captured=%d "
                      "promoted=%d fFast=%d fReal=%d real_see=%d\n",
                      rgmv[x].uMove, rgmv[x].cFrom, rgmv[x].cTo,
                      rgmv[x].pMoved, rgmv[x].pCaptured, rgmv[x].pPromoted,
                      fFast, fReal, SEE(&(ctx->sPosition), rgmv[x]));
                DumpPosition(&(ctx->sPosition));
                UtilPanic(TESTCASE_FAILURE,
                          &(ctx->sPosition),
                          "SeeFastSign/SEE sign mismatch",
                          NULL, NULL,
                          __FILE__, __LINE__);
            }
        }
    }
    free(ctx);

    //
    // Speed: same isolated, interleaved cycles/call methodology as the
    // other benchmarks above, real generated moves at each benchmark
    // position (same setup as TestSEESpeed).
    //
    {
        static const char *rgszFen[3] =
        {
            "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
            "r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P4/2NBPN2/PP3PPP/R1BQ1RK1 w - - 0 1",
            "8/5k2/8/3K4/8/8/8/4R3 w - - 0 1",
        };
        static const char *rgszLabel[3] =
        {
            "opening   ", "middlegame", "endgame   ",
        };
        SEARCHER_THREAD_CONTEXT *ctx2;
        POSITION posBench2;
        MOVE rgmv2[MAX_MOVE_STACK];
        ULONG uMoveCount2;
        UINT64 u64RealTotal, u64FastTotal, u64Start;
        ULONG uIter;
        FLAG fSink;
        const ULONG uCallsPerPosition = 200000;

        Trace("Benchmarking SEE sign: real SEE()>=0 vs SeeFastSign "
              "(interleaved, %lu calls/position)...\n",
              uCallsPerPosition);
        ctx2 = (SEARCHER_THREAD_CONTEXT *)malloc(sizeof(SEARCHER_THREAD_CONTEXT));
        if (NULL == ctx2)
        {
            return;
        }

        for (u = 0; u < 3; u++)
        {
            FenToPosition(&posBench2, (char *)rgszFen[u]);
            InitializeSearcherContext(&posBench2, ctx2);
            GenerateMoves(ctx2, (MOVE){0}, GENERATE_DONT_SCORE);

            uMoveCount2 = 0;
            for (x = ctx2->sMoveStack.uBegin[ctx2->uPly];
                 x < ctx2->sMoveStack.uEnd[ctx2->uPly];
                 x++)
            {
                rgmv2[uMoveCount2++] = ctx2->sMoveStack.mvf[x].mv;
            }
            if (uMoveCount2 == 0)
            {
                continue;
            }

            u64RealTotal = 0;
            u64FastTotal = 0;
            for (uIter = 0; uIter < uCallsPerPosition; uIter++)
            {
                ULONG uPair = uIter % uMoveCount2;

                u64Start = SystemReadTimeStampCounter();
                fSink = (SEE(&(ctx2->sPosition), rgmv2[uPair]) >= 0);
                u64RealTotal += (SystemReadTimeStampCounter() - u64Start);

                u64Start = SystemReadTimeStampCounter();
                fSink = SeeFastSign(&(ctx2->sPosition), rgmv2[uPair]);
                u64FastTotal += (SystemReadTimeStampCounter() - u64Start);
                (void)fSink;
            }
            printf(" %s: real-SEE %" COMPILER_LONGLONG_UNSIGNED_FORMAT
                   " cycles/call, SeeFastSign %"
                   COMPILER_LONGLONG_UNSIGNED_FORMAT " cycles/call "
                   "(fast is %.2fx real, %u distinct moves)\n",
                   rgszLabel[u],
                   u64RealTotal / uCallsPerPosition,
                   u64FastTotal / uCallsPerPosition,
                   (double)u64FastTotal / (double)u64RealTotal,
                   uMoveCount2);
        }
        free(ctx2);
    }
}

// _WhoAttacksSquareMagicBB (see.c) replaces _WhoAttacksSquareBB's
// manual per-direction ray-walk with the same O(1) magic-bitboard
// lookups (_RookAttacksBB/_BishopAttacksBB, generate.c) already used
// everywhere else in this codebase. Same correctness+speed pattern as
// the other Test* functions here.
void
TestWhoAttacksSquareMagic(void)
{
    POSITION pos;
    ULONG u;
    COOR c;
    ULONG uSide;
    BITBOARD bbRay;
    BITBOARD bbMagic;
    const ULONG uPositions = 20000;

    Trace("Testing _WhoAttacksSquareMagicBB (magic vs ray-walk)...\n");
    for (u = 0; u < uPositions; u++)
    {
        GenerateRandomLegalPosition(&pos);
        FOREACH_SQUARE(c)
        {
            if (!IS_ON_BOARD(c)) continue;
            for (uSide = BLACK; uSide <= WHITE; uSide++)
            {
                bbRay = _WhoAttacksSquareRayBB(&pos, c, uSide, pos.bbOccupied);
                bbMagic = _WhoAttacksSquareBB(&pos, c, uSide,
                                               pos.bbOccupied);
                if (bbRay != bbMagic)
                {
                    Trace("cSquare=%d uSide=%lu bbRay=0x%llx bbMagic=0x%llx\n",
                          c, uSide, bbRay, bbMagic);
                    UtilPanic(TESTCASE_FAILURE,
                              &pos,
                              "_WhoAttacksSquareBB/ray-walk mismatch",
                              NULL, NULL,
                              __FILE__, __LINE__);
                }
            }
        }
    }

    //
    // Speed: same isolated, interleaved cycles/call methodology as the
    // other benchmarks above.
    //
    {
        static const char *rgszFen[3] =
        {
            "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
            "r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P4/2NBPN2/PP3PPP/R1BQ1RK1 w - - 0 1",
            "8/5k2/8/3K4/8/8/8/4R3 w - - 0 1",
        };
        static const char *rgszLabel[3] =
        {
            "opening   ", "middlegame", "endgame   ",
        };
        POSITION posBench;
        BITBOARD bbSink;
        UINT64 u64RayTotal, u64MagicTotal, u64Start;
        ULONG uIter;
        ULONG uSq;
        COOR cBench;
        ULONG uSideBench;
        const ULONG uCallsPerPosition = 200000;

        Trace("Benchmarking _WhoAttacksSquareBB: ray-walk vs magic "
              "(interleaved, %lu calls/position)...\n",
              uCallsPerPosition);
        for (u = 0; u < 3; u++)
        {
            FenToPosition(&posBench, (char *)rgszFen[u]);
            u64RayTotal = 0;
            u64MagicTotal = 0;
            for (uIter = 0; uIter < uCallsPerPosition; uIter++)
            {
                uSq = uIter % 64;
                cBench = BIT_NUMBER_TO_COOR(uSq);
                uSideBench = uIter & 1;
                if (!IS_ON_BOARD(cBench)) continue;

                u64Start = SystemReadTimeStampCounter();
                bbSink = _WhoAttacksSquareRayBB(&posBench, cBench, uSideBench,
                                                 posBench.bbOccupied);
                u64RayTotal += (SystemReadTimeStampCounter() - u64Start);

                u64Start = SystemReadTimeStampCounter();
                bbSink = _WhoAttacksSquareBB(&posBench, cBench,
                                              uSideBench,
                                              posBench.bbOccupied);
                u64MagicTotal += (SystemReadTimeStampCounter() - u64Start);
                (void)bbSink;
            }
            printf(" %s: ray-walk %" COMPILER_LONGLONG_UNSIGNED_FORMAT
                   " cycles/call, magic %"
                   COMPILER_LONGLONG_UNSIGNED_FORMAT " cycles/call "
                   "(magic is %.2fx ray-walk)\n",
                   rgszLabel[u],
                   u64RayTotal / uCallsPerPosition,
                   u64MagicTotal / uCallsPerPosition,
                   (double)u64MagicTotal / (double)u64RayTotal);
        }
    }
}

//
// Correctness + speed gate for SEE()'s no-defender early exit (see.c):
// SEE() must return the EXACT same value as _SEEFull() (not just the
// same sign -- TestSeeFastSign above already covers sign-only) on
// every move, since real callers (_ScoreAllMoves/_ScoreQSearchMoves*)
// trust the magnitude, not just the sign, for a winning capture.
//
// Speed deliberately uses four benchmark positions spanning the
// defended/undefended spectrum, not just a "best case": a dense
// tactical middlegame where nearly every capture has a defender (the
// new check's overhead pays for nothing -- this is the case to watch
// for a net LOSS, since every call still pays for the early check
// before falling through to the exact same _SEEFull work SEE() used
// to do directly), a bare-kings-plus-one-piece endgame where almost
// nothing is defended (the case the early exit is for), plus the
// standard opening/middlegame pair for a realistic middle ground.
//
void
TestSEENoDefenderFastPath(void)
{
    SEARCHER_THREAD_CONTEXT *ctx;
    POSITION posBench;
    MOVE rgmv[MAX_MOVE_STACK];
    ULONG uMoveCount;
    ULONG u, x;
    SCORE iFull, iFast;
    const ULONG uPositions = 3000;

    Trace("Testing SEE() no-defender fast path against _SEEFull "
          "(exact value, not just sign)...\n");
    ctx = (SEARCHER_THREAD_CONTEXT *)malloc(sizeof(SEARCHER_THREAD_CONTEXT));
    if (NULL == ctx)
    {
        return;
    }

    for (u = 0; u < uPositions; u++)
    {
        GenerateRandomLegalPosition(&posBench);
        InitializeSearcherContext(&posBench, ctx);
        GenerateMoves(ctx, (MOVE){0}, GENERATE_DONT_SCORE);

        uMoveCount = 0;
        for (x = ctx->sMoveStack.uBegin[ctx->uPly];
             x < ctx->sMoveStack.uEnd[ctx->uPly];
             x++)
        {
            rgmv[uMoveCount++] = ctx->sMoveStack.mvf[x].mv;
        }

        for (x = 0; x < uMoveCount; x++)
        {
            iFull = _SEEFull(&(ctx->sPosition), rgmv[x]);
            iFast = SEE(&(ctx->sPosition), rgmv[x]);
            if (iFull != iFast)
            {
                Trace("move=0x%x from=%d to=%d moved=%d captured=%d "
                      "promoted=%d _SEEFull=%d SEE=%d\n",
                      rgmv[x].uMove, rgmv[x].cFrom, rgmv[x].cTo,
                      rgmv[x].pMoved, rgmv[x].pCaptured, rgmv[x].pPromoted,
                      iFull, iFast);
                DumpPosition(&(ctx->sPosition));
                UtilPanic(TESTCASE_FAILURE,
                          &(ctx->sPosition),
                          "SEE/_SEEFull exact-value mismatch",
                          NULL, NULL,
                          __FILE__, __LINE__);
            }
        }
    }
    free(ctx);

    //
    // Speed: same isolated, interleaved cycles/call methodology as the
    // other benchmarks above.
    //
    {
        static const char *rgszFen[4] =
        {
            "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
            "r1bq1rk1/pp2bppp/2n1pn2/2pp4/3P4/2NBPN2/PP3PPP/R1BQ1RK1 w - - 0 1",
            "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1",
            "8/5k2/8/3K4/8/8/8/4R3 w - - 0 1",
        };
        static const char *rgszLabel[4] =
        {
            "opening        ", "middlegame     ",
            "tactical/dense  ", "sparse endgame ",
        };
        SEARCHER_THREAD_CONTEXT *ctx2;
        POSITION posBench2;
        MOVE rgmv2[MAX_MOVE_STACK];
        ULONG uMoveCount2;
        UINT64 u64FullTotal, u64FastTotal, u64Start;
        ULONG uIter;
        SCORE iSink;
        const ULONG uCallsPerPosition = 200000;

        Trace("Benchmarking SEE(): _SEEFull (no early exit) vs SEE "
              "(no-defender fast path), interleaved, %lu calls/"
              "position...\n",
              uCallsPerPosition);
        ctx2 = (SEARCHER_THREAD_CONTEXT *)malloc(sizeof(SEARCHER_THREAD_CONTEXT));
        if (NULL == ctx2)
        {
            return;
        }

        for (u = 0; u < 4; u++)
        {
            FenToPosition(&posBench2, (char *)rgszFen[u]);
            InitializeSearcherContext(&posBench2, ctx2);
            GenerateMoves(ctx2, (MOVE){0}, GENERATE_DONT_SCORE);

            uMoveCount2 = 0;
            for (x = ctx2->sMoveStack.uBegin[ctx2->uPly];
                 x < ctx2->sMoveStack.uEnd[ctx2->uPly];
                 x++)
            {
                rgmv2[uMoveCount2++] = ctx2->sMoveStack.mvf[x].mv;
            }
            if (uMoveCount2 == 0)
            {
                continue;
            }

            u64FullTotal = 0;
            u64FastTotal = 0;
            for (uIter = 0; uIter < uCallsPerPosition; uIter++)
            {
                ULONG uPair = uIter % uMoveCount2;

                u64Start = SystemReadTimeStampCounter();
                iSink = _SEEFull(&(ctx2->sPosition), rgmv2[uPair]);
                u64FullTotal += (SystemReadTimeStampCounter() - u64Start);

                u64Start = SystemReadTimeStampCounter();
                iSink = SEE(&(ctx2->sPosition), rgmv2[uPair]);
                u64FastTotal += (SystemReadTimeStampCounter() - u64Start);
                (void)iSink;
            }
            printf(" %s: _SEEFull %" COMPILER_LONGLONG_UNSIGNED_FORMAT
                   " cycles/call, SEE %"
                   COMPILER_LONGLONG_UNSIGNED_FORMAT " cycles/call "
                   "(SEE is %.2fx _SEEFull, %u distinct moves)\n",
                   rgszLabel[u],
                   u64FullTotal / uCallsPerPosition,
                   u64FastTotal / uCallsPerPosition,
                   (double)u64FastTotal / (double)u64FullTotal,
                   uMoveCount2);
        }
        free(ctx2);
    }
}
#endif // TEST