rshogi-core 0.1.6

A high-performance shogi engine core library with NNUE evaluation
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
//! Alpha-Beta探索の実装
//!
//! YaneuraOu準拠のAlpha-Beta探索。
//! - Principal Variation Search (PVS)
//! - 静止探索 (Quiescence Search)
//! - 各種枝刈り: NMP, LMR, Futility, Razoring, SEE, Singular Extension

use std::ptr::NonNull;
use std::sync::Arc;

use crate::eval::{evaluate_pass_rights, get_scaled_pass_move_bonus, EvalHash};
use crate::nnue::{evaluate_dispatch, get_network, AccumulatorStackVariant, DirtyPiece};
use crate::position::Position;
use crate::search::PieceToHistory;
use crate::tt::{ProbeResult, TTData, TranspositionTable};
use crate::types::{
    Bound, Color, Depth, Move, Piece, PieceType, Square, Value, DEPTH_QS, DEPTH_UNSEARCHED, MAX_PLY,
};

use super::history::{
    capture_malus, continuation_history_bonus_with_offset, low_ply_history_bonus,
    pawn_history_bonus, quiet_malus, stat_bonus, HistoryTables,
    CONTINUATION_HISTORY_NEAR_PLY_OFFSET, CONTINUATION_HISTORY_WEIGHTS, CORRECTION_HISTORY_LIMIT,
    CORRECTION_HISTORY_SIZE, LOW_PLY_HISTORY_SIZE, PRIOR_CAPTURE_COUNTERMOVE_BONUS,
    TT_MOVE_HISTORY_BONUS, TT_MOVE_HISTORY_MALUS,
};
use super::movepicker::piece_value;
use super::types::{
    draw_value, init_stack_array, value_from_tt, value_to_tt, ContHistKey, NodeType,
    OrderedMovesBuffer, RootMoves, SearchedMoveList, StackArray, STACK_SIZE,
};
use super::{LimitsType, MovePicker, TimeManagement};

// =============================================================================
// 定数
// =============================================================================

/// Futility margin(depth × 係数)
///
/// YaneuraOu準拠での基準係数。枝刈りでtt未ヒットのカットノードは係数を少し下げる。
const FUTILITY_MARGIN_BASE: i32 = 90;

/// IIR関連のしきい値(yaneuraou-search.cpp:2769-2774 由来)
const IIR_PRIOR_REDUCTION_THRESHOLD_SHALLOW: i32 = 1;
const IIR_PRIOR_REDUCTION_THRESHOLD_DEEP: i32 = 3;
const IIR_DEPTH_BOUNDARY: Depth = 10;
const IIR_EVAL_SUM_THRESHOLD: i32 = 177;

use std::sync::LazyLock;

/// 引き分けスコアに揺らぎを与える(YaneuraOu準拠)
const DRAW_JITTER_MASK: u64 = 0x2;
const DRAW_JITTER_OFFSET: i32 = -1; // VALUE_DRAW(0) 周辺に ±1 の揺らぎを入れる

#[inline]
fn draw_jitter(nodes: u64) -> i32 {
    // YaneuraOu: value_draw(nodes) = VALUE_DRAW - 1 + (nodes & 0x2)
    // 千日手盲点を避けるため、VALUE_DRAW(0) を ±1 にばらつかせる。
    ((nodes & DRAW_JITTER_MASK) as i32) + DRAW_JITTER_OFFSET
}

/// depthに対するmsb(YaneuraOuのlm r補正用)
#[inline]
fn msb(x: i32) -> i32 {
    if x <= 0 {
        return 0;
    }
    31 - x.leading_zeros() as i32
}

/// 補正履歴を適用した静的評価に変換(詰みスコア領域に入り込まないようにクリップ)
#[inline]
fn to_corrected_static_eval(unadjusted: Value, correction_value: i32) -> Value {
    let corrected = unadjusted.raw() + correction_value / 131_072;
    Value::new(corrected.clamp(Value::MATED_IN_MAX_PLY.raw() + 1, Value::MATE_IN_MAX_PLY.raw() - 1))
}

/// LMR用のreduction配列(YaneuraOu準拠の1次元テーブル)
type Reductions = [i32; 64];

// YaneuraOuのreduction式で用いる定数(yaneuraou-search.cpp:3163-3170,4759)
const REDUCTION_DELTA_SCALE: i32 = 731;
const REDUCTION_NON_IMPROVING_MULT: i32 = 216;
const REDUCTION_NON_IMPROVING_DIV: i32 = 512;
const REDUCTION_BASE_OFFSET: i32 = 1089;

/// Reduction配列(LazyLockによる遅延初期化)
/// 初回アクセス時に自動初期化されるため、get()呼び出しが不要
static REDUCTIONS: LazyLock<Reductions> = LazyLock::new(|| {
    let mut table: Reductions = [0; 64];
    // YaneuraOu: reductions[i] = int(2782 / 128.0 * log(i)) (yaneuraou-search.cpp:1818)
    for (i, value) in table.iter_mut().enumerate().skip(1) {
        *value = (2782.0 / 128.0 * (i as f64).ln()) as i32;
    }
    table
});

/// Reductionを取得
///
/// LazyLockにより初回アクセス時に自動初期化されるため、panicしない。
#[inline]
fn reduction(imp: bool, depth: i32, move_count: i32, delta: i32, root_delta: i32) -> i32 {
    if depth <= 0 || move_count <= 0 {
        return 0;
    }

    let d = depth.clamp(1, 63) as usize;
    let mc = move_count.clamp(1, 63) as usize;
    // LazyLockにより直接アクセス可能(get()不要)
    let reduction_scale = REDUCTIONS[d] * REDUCTIONS[mc];
    let root_delta = root_delta.max(1);
    let delta = delta.max(0);

    // YaneuraOuのreduction式(yaneuraou-search.cpp:3163-3170,4759)
    // 1024倍スケールで返す。ttPv加算は呼び出し側で行う。
    reduction_scale - delta * REDUCTION_DELTA_SCALE / root_delta
        + (!imp as i32) * reduction_scale * REDUCTION_NON_IMPROVING_MULT
            / REDUCTION_NON_IMPROVING_DIV
        + REDUCTION_BASE_OFFSET
}

/// 置換表プローブの結果をまとめたコンテキスト
///
/// TTプローブ後の即時カットオフ判定や、後続の枝刈りロジックで使用される。
struct TTContext {
    key: u64,
    result: ProbeResult,
    data: TTData,
    hit: bool,
    mv: Move,
    value: Value,
    capture: bool,
}

/// 置換表プローブの結果(続行 or カットオフ)
enum ProbeOutcome {
    /// 探索続行(TTContext付き)
    Continue(TTContext),
    /// 即時カットオフ値
    Cutoff(Value),
}

/// 静的評価まわりの情報をまとめたコンテキスト
struct EvalContext {
    static_eval: Value,
    unadjusted_static_eval: Value,
    correction_value: i32,
    /// 2手前と比較して局面が改善しているか
    improving: bool,
    /// 相手側の局面が悪化しているか
    opponent_worsening: bool,
}

/// Step14の枝刈り判定結果
enum Step14Outcome {
    /// 枝刈りする(best_value を更新する場合のみ付随)
    Skip { best_value: Option<Value> },
    /// 続行し、lmr_depth を返す
    Continue,
}

/// Futility判定に必要な情報をまとめたパラメータ
#[derive(Clone, Copy)]
struct FutilityParams {
    depth: Depth,
    beta: Value,
    static_eval: Value,
    correction_value: i32,
    improving: bool,
    opponent_worsening: bool,
    cut_node: bool,
    tt_hit: bool,
    pv_node: bool,
    in_check: bool,
}

/// Step14 の枝刈りに必要な文脈
struct Step14Context<'a> {
    pos: &'a Position,
    mv: Move,
    depth: Depth,
    ply: i32,
    improving: bool,
    best_move: Move,
    best_value: Value,
    alpha: Value,
    in_check: bool,
    gives_check: bool,
    is_capture: bool,
    lmr_depth: i32,
    mover: Color,
    move_count: i32,
    cont_history_1: &'a PieceToHistory,
    cont_history_2: &'a PieceToHistory,
}

// =============================================================================
// SearchWorker
// =============================================================================

/// 探索用のワーカー状態
///
/// YaneuraOu準拠: Workerはゲーム全体で再利用される。
/// 履歴統計は直接メンバとして保持し、usinewgameでクリア、goでは保持。
pub struct SearchWorker {
    /// 置換表への共有参照(Arc)
    pub tt: Arc<TranspositionTable>,

    /// 評価ハッシュへの共有参照(Arc)
    pub eval_hash: Arc<EvalHash>,

    /// スレッドID(0=main)
    pub thread_id: usize,

    // =========================================================================
    // 履歴統計(YaneuraOu準拠: 直接メンバとして保持)
    // =========================================================================
    // HistoryTablesを単一のヒープ領域として確保し、履歴テーブルを連続配置する。
    // `Box::new_zeroed` で一括確保することで、巨大配列のスタック確保を避ける。
    // =========================================================================
    /// 履歴/統計テーブル群
    pub history: Box<HistoryTables>,

    /// ContinuationHistoryのsentinel(YaneuraOu方式)
    pub cont_history_sentinel: NonNull<PieceToHistory>,

    // =========================================================================
    // 探索状態(毎回リセット)
    // =========================================================================
    /// ルート手
    pub root_moves: RootMoves,

    /// 探索スタック
    pub stack: StackArray,

    /// 探索ノード数
    pub nodes: u64,

    /// 選択的深さ
    pub sel_depth: i32,

    /// ルート深さ
    pub root_depth: Depth,

    /// ルートでのウィンドウ幅(beta - alpha)。YaneuraOuのLMRスケール用。
    pub root_delta: i32,

    /// 探索完了済み深さ
    pub completed_depth: Depth,

    /// 全合法手生成フラグ(YaneuraOu互換)
    pub generate_all_legal_moves: bool,

    /// 最善手
    pub best_move: Move,

    /// 中断フラグ
    pub abort: bool,

    /// 最善手変更カウンター(PV安定性判断用)
    ///
    /// YaneuraOu準拠: move_count > 1 && !pvIdx の時にインクリメント
    /// 反復深化の各世代で /= 2 して減衰させる
    pub best_move_changes: f64,

    /// 引き分けまでの最大手数(YaneuraOu準拠)
    pub max_moves_to_draw: i32,

    /// Null Move Pruning の Verification Search 用フラグ
    ///
    /// Stockfish/YaneuraOu準拠: Verification Search中は、
    /// `ply >= nmp_min_ply` の条件でNMPを制限する。
    /// 深い探索(depth >= 16)でZugzwangを検出して再探索する仕組み。
    pub nmp_min_ply: i32,

    /// NNUE Accumulator スタック(統合型)
    ///
    /// 探索時のNNUE差分更新用。Position.do_move/undo_moveと同期してpush/popする。
    /// アーキテクチャに応じて適切なスタックを1つだけ保持し、メモリ効率を向上。
    pub nnue_stack: AccumulatorStackVariant,

    // =========================================================================
    // 頻度制御(YaneuraOu準拠)
    // =========================================================================
    /// check_abort呼び出しカウンター(512回に1回チェック)
    calls_cnt: i32,
}

impl SearchWorker {
    /// 新しいSearchWorkerを作成(YaneuraOu準拠: isreadyまたは最初のgo時)
    ///
    /// Box化してヒープに配置し、スタックオーバーフローを防ぐ。
    pub fn new(
        tt: Arc<TranspositionTable>,
        eval_hash: Arc<EvalHash>,
        max_moves_to_draw: i32,
        thread_id: usize,
    ) -> Box<Self> {
        let history = HistoryTables::new_boxed();
        let cont_history_sentinel =
            NonNull::from(history.continuation_history[0][0].get_table(Piece::NONE, Square::SQ_11));

        let mut worker = Box::new(Self {
            tt,
            eval_hash,
            thread_id,
            // 履歴統計の初期化
            history,
            cont_history_sentinel,
            // 探索状態の初期化
            root_moves: RootMoves::new(),
            stack: init_stack_array(),
            nodes: 0,
            sel_depth: 0,
            root_depth: 0,
            root_delta: 1,
            completed_depth: 0,
            generate_all_legal_moves: false,
            best_move: Move::NONE,
            abort: false,
            best_move_changes: 0.0,
            max_moves_to_draw,
            nmp_min_ply: 0,
            nnue_stack: AccumulatorStackVariant::new_default(), // prepare_search() で適切なバリアントに更新
            // 頻度制御
            calls_cnt: 0,
        });
        worker.reset_cont_history_ptrs();
        worker
    }

    #[inline]
    fn cont_history_ptr(&self, ply: i32, back: i32) -> NonNull<PieceToHistory> {
        debug_assert!(ply >= 0 && (ply as usize) < STACK_SIZE, "ply out of bounds: {ply}");
        debug_assert!(back >= 0, "back must be non-negative: {back}");
        if ply >= back {
            self.stack[(ply - back) as usize].cont_history_ptr
        } else {
            self.cont_history_sentinel
        }
    }

    #[inline]
    fn cont_history_ref(&self, ply: i32, back: i32) -> &PieceToHistory {
        let ptr = self.cont_history_ptr(ply, back);
        unsafe { ptr.as_ref() }
    }

    #[inline]
    fn cont_history_tables(&self, ply: i32) -> [&PieceToHistory; 6] {
        [
            self.cont_history_ref(ply, 1),
            self.cont_history_ref(ply, 2),
            self.cont_history_ref(ply, 3),
            self.cont_history_ref(ply, 4),
            self.cont_history_ref(ply, 5),
            self.cont_history_ref(ply, 6),
        ]
    }

    fn reset_cont_history_ptrs(&mut self) {
        let sentinel = self.cont_history_sentinel;
        for stack in self.stack.iter_mut() {
            stack.cont_history_ptr = sentinel;
        }
    }

    #[inline]
    fn set_cont_history_for_move(
        &mut self,
        ply: i32,
        in_check: bool,
        capture: bool,
        piece: Piece,
        to: Square,
    ) {
        debug_assert!(ply >= 0 && (ply as usize) < STACK_SIZE, "ply out of bounds: {ply}");
        let in_check_idx = in_check as usize;
        let capture_idx = capture as usize;
        let table =
            self.history.continuation_history[in_check_idx][capture_idx].get_table(piece, to);
        self.stack[ply as usize].cont_history_ptr = NonNull::from(table);
        self.stack[ply as usize].cont_hist_key =
            Some(ContHistKey::new(in_check, capture, piece, to));
    }

    #[inline]
    fn clear_cont_history_for_null(&mut self, ply: i32) {
        self.stack[ply as usize].cont_history_ptr = self.cont_history_sentinel;
        self.stack[ply as usize].cont_hist_key = None;
    }

    /// usinewgameで呼び出し:全履歴をクリア(YaneuraOu Worker::clear()相当)
    pub fn clear(&mut self) {
        self.history.clear();
    }

    /// goで呼び出し:探索状態のリセット(履歴はクリアしない、YaneuraOu準拠)
    pub fn prepare_search(&mut self) {
        self.nodes = 0;
        self.sel_depth = 0;
        self.root_depth = 0;
        self.root_delta = 1;
        self.completed_depth = 0;
        self.best_move = Move::NONE;
        self.abort = false;
        self.best_move_changes = 0.0;
        self.nmp_min_ply = 0;
        self.root_moves.clear();
        // YaneuraOu準拠: low_ply_historyのみクリア
        self.history.low_ply_history.clear();
        // NNUE AccumulatorStack: ネットワークに応じたバリアントに更新・リセット
        if let Some(network) = get_network() {
            // バリアントがネットワークと一致しない場合は再作成
            if !self.nnue_stack.matches_network(network) {
                self.nnue_stack = AccumulatorStackVariant::from_network(network);
            } else {
                self.nnue_stack.reset();
            }
        } else {
            // NNUE未初期化の場合はデフォルト(HalfKP)でリセット
            self.nnue_stack.reset();
        }
        // check_abort頻度制御カウンターをリセット
        // これにより新しい探索開始時に即座に停止チェックが行われる
        self.calls_cnt = 0;
    }

    /// best_move_changes を半減(世代減衰)
    ///
    /// YaneuraOu準拠: 反復深化の各世代終了時に呼び出して、
    /// 古い情報の重みを低くする
    pub fn decay_best_move_changes(&mut self) {
        self.best_move_changes /= 2.0;
    }

    /// 全合法手生成モードの設定(YaneuraOu互換)
    pub fn set_generate_all_legal_moves(&mut self, flag: bool) {
        self.generate_all_legal_moves = flag;
    }

    // =========================================================================
    // NNUE ヘルパーメソッド(LayerStacks / HalfKP・HalfKA_hm の分岐を隠蔽)
    // =========================================================================

    /// NNUE 評価値を計算(パス権なし)
    ///
    /// ロードされた NNUE のアーキテクチャに応じて適切なアキュムレータと評価関数を使用。
    /// パス権評価は手数依存のためここでは加算せず、静的評価補正後に毎回再計算して加える。
    /// これにより TT/EvalHash(局面キーのみ)と整合し、再訪局面で古い手数のパス権評価を再利用しない。
    #[inline]
    fn nnue_evaluate(&mut self, pos: &Position) -> Value {
        evaluate_dispatch(pos, &mut self.nnue_stack)
    }

    /// NNUE アキュムレータスタックを push
    #[inline]
    fn nnue_push(&mut self, dirty_piece: DirtyPiece) {
        self.nnue_stack.push(dirty_piece);
    }

    /// NNUE アキュムレータスタックを pop
    #[inline]
    fn nnue_pop(&mut self) {
        self.nnue_stack.pop();
    }

    /// 中断チェック
    /// YaneuraOu準拠: 512回に1回だけ実際のチェックを行う
    #[inline]
    fn check_abort(&mut self, limits: &LimitsType, time_manager: &mut TimeManagement) -> bool {
        // すでにabortフラグが立っている場合は即座に返す
        if self.abort {
            #[cfg(debug_assertions)]
            eprintln!("check_abort: abort flag already set");
            return true;
        }

        // 頻度制御:512回に1回だけ実際のチェックを行う(YaneuraOu準拠)
        self.calls_cnt -= 1;
        if self.calls_cnt > 0 {
            return false;
        }
        // カウンターをリセット
        self.calls_cnt = if limits.nodes > 0 {
            std::cmp::min(512, (limits.nodes / 1024) as i32).max(1)
        } else {
            512
        };

        // 外部からの停止要求
        if time_manager.stop_requested() {
            #[cfg(debug_assertions)]
            eprintln!("check_abort: stop requested");
            self.abort = true;
            return true;
        }

        // ノード数制限チェック
        if limits.nodes > 0 && self.nodes >= limits.nodes {
            #[cfg(debug_assertions)]
            eprintln!(
                "check_abort: node limit reached nodes={} limit={}",
                self.nodes, limits.nodes
            );
            self.abort = true;
            return true;
        }

        // 時間制限チェック(main threadのみ)
        // YaneuraOu準拠の2フェーズロジック
        if self.thread_id == 0 {
            // ponderhit フラグをポーリングし、検知したら通常探索へ切り替える
            if time_manager.take_ponderhit() {
                time_manager.on_ponderhit();
            }

            let elapsed = time_manager.elapsed();
            let elapsed_effective = time_manager.elapsed_from_ponderhit();

            // フェーズ1: search_end 設定済み → 即座に停止
            if time_manager.search_end() > 0 && elapsed >= time_manager.search_end() {
                #[cfg(debug_assertions)]
                eprintln!(
                    "check_abort: search_end reached elapsed={} search_end={}",
                    elapsed,
                    time_manager.search_end()
                );
                self.abort = true;
                return true;
            }

            // フェーズ2: search_end 未設定 → maximum超過 or stop_on_ponderhit で設定
            // ただし ponder 中は停止判定を行わない(YO準拠)
            if !time_manager.is_pondering()
                && time_manager.search_end() == 0
                && limits.use_time_management()
                && (elapsed_effective > time_manager.maximum() || time_manager.stop_on_ponderhit())
            {
                time_manager.set_search_end(elapsed);
                // 注: ここでは停止せず、次のチェックで秒境界で停止
            }
        }

        false
    }

    /// 補正履歴から静的評価の補正値を算出(YaneuraOu準拠)
    #[inline]
    fn correction_value(&self, pos: &Position, ply: i32) -> i32 {
        let us = pos.side_to_move();
        let pawn_idx = (pos.pawn_key() as usize) & (CORRECTION_HISTORY_SIZE - 1);
        let minor_idx = (pos.minor_piece_key() as usize) & (CORRECTION_HISTORY_SIZE - 1);
        let non_pawn_idx_w =
            (pos.non_pawn_key(Color::White) as usize) & (CORRECTION_HISTORY_SIZE - 1);
        let non_pawn_idx_b =
            (pos.non_pawn_key(Color::Black) as usize) & (CORRECTION_HISTORY_SIZE - 1);

        let pcv = self.history.correction_history.pawn_value(pawn_idx, us) as i32;
        let micv = self.history.correction_history.minor_value(minor_idx, us) as i32;
        let wnpcv =
            self.history.correction_history.non_pawn_value(non_pawn_idx_w, Color::White, us) as i32;
        let bnpcv =
            self.history.correction_history.non_pawn_value(non_pawn_idx_b, Color::Black, us) as i32;

        let mut cntcv = 0;
        if ply >= 2 {
            let prev_move = self.stack[(ply - 1) as usize].current_move;
            if prev_move.is_normal() {
                if let Some(prev2_key) = self.stack[(ply - 2) as usize].cont_hist_key {
                    let pc = pos.piece_on(prev_move.to());
                    cntcv = self.history.correction_history.continuation_value(
                        prev2_key.piece,
                        prev2_key.to,
                        pc,
                        prev_move.to(),
                    ) as i32;
                }
            }
        }

        8867 * pcv + 8136 * micv + 10_757 * (wnpcv + bnpcv) + 7232 * cntcv
    }

    /// 補正履歴の更新(YaneuraOu準拠)
    #[inline]
    fn update_correction_history(&mut self, pos: &Position, ply: i32, bonus: i32) {
        let us = pos.side_to_move();
        let pawn_idx = (pos.pawn_key() as usize) & (CORRECTION_HISTORY_SIZE - 1);
        let minor_idx = (pos.minor_piece_key() as usize) & (CORRECTION_HISTORY_SIZE - 1);
        let non_pawn_idx_w =
            (pos.non_pawn_key(Color::White) as usize) & (CORRECTION_HISTORY_SIZE - 1);
        let non_pawn_idx_b =
            (pos.non_pawn_key(Color::Black) as usize) & (CORRECTION_HISTORY_SIZE - 1);

        const NON_PAWN_WEIGHT: i32 = 165;

        self.history.correction_history.update_pawn(pawn_idx, us, bonus);
        self.history.correction_history.update_minor(minor_idx, us, bonus * 153 / 128);
        self.history.correction_history.update_non_pawn(
            non_pawn_idx_w,
            Color::White,
            us,
            bonus * NON_PAWN_WEIGHT / 128,
        );
        self.history.correction_history.update_non_pawn(
            non_pawn_idx_b,
            Color::Black,
            us,
            bonus * NON_PAWN_WEIGHT / 128,
        );

        if ply >= 2 {
            let prev_move = self.stack[(ply - 1) as usize].current_move;
            if prev_move.is_normal() {
                if let Some(prev2_key) = self.stack[(ply - 2) as usize].cont_hist_key {
                    let pc = pos.piece_on(prev_move.to());
                    self.history.correction_history.update_continuation(
                        prev2_key.piece,
                        prev2_key.to,
                        pc,
                        prev_move.to(),
                        bonus * 153 / 128,
                    );
                }
            }
        }
    }

    /// 親ノードのreductionを取得してクリア
    #[inline]
    fn take_prior_reduction(&mut self, ply: i32) -> i32 {
        if ply >= 1 {
            let parent_idx = (ply - 1) as usize;
            let pr = self.stack[parent_idx].reduction;
            self.stack[parent_idx].reduction = 0;
            pr
        } else {
            0
        }
    }

    /// 置換表プローブと即時カットオフ(root以外のmate_1ply含む)
    ///
    /// `excluded_move`がある場合(Singular Extension中)は置換表カットオフを回避する。
    fn probe_transposition<const NT: u8>(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        beta: Value,
        ply: i32,
        pv_node: bool,
        in_check: bool,
        excluded_move: Move,
    ) -> ProbeOutcome {
        let key = pos.key();
        let tt_result = self.tt.probe(key, pos);
        let tt_hit = tt_result.found;
        let tt_data = tt_result.data;

        self.stack[ply as usize].tt_hit = tt_hit;
        // excludedMoveがある場合は前回のttPvを維持(YaneuraOu準拠)
        self.stack[ply as usize].tt_pv = if excluded_move.is_some() {
            self.stack[ply as usize].tt_pv
        } else {
            pv_node || (tt_hit && tt_data.is_pv)
        };

        let tt_move = if tt_hit { tt_data.mv } else { Move::NONE };
        let tt_value = if tt_hit {
            value_from_tt(tt_data.value, ply)
        } else {
            Value::NONE
        };
        let tt_capture = tt_move.is_some() && pos.is_capture(tt_move);

        // excludedMoveがある場合はカットオフしない(YaneuraOu準拠)
        if !pv_node
            && excluded_move.is_none()
            && tt_hit
            && tt_data.depth >= depth
            && tt_value != Value::NONE
            && tt_data.bound.can_cutoff(tt_value, beta)
        {
            return ProbeOutcome::Cutoff(tt_value);
        }

        // 1手詰め判定(置換表未ヒット時のみ、Rootでは実施しない)
        // excludedMoveがある場合も実施しない(詰みがあればsingular前にbeta cutするため)
        if NT != NodeType::Root as u8 && !in_check && !tt_hit && excluded_move.is_none() {
            let mate_move = pos.mate_1ply();
            if mate_move.is_some() {
                let value = Value::mate_in(ply + 1);
                let stored_depth = (depth + 6).min(MAX_PLY - 1);
                tt_result.write(
                    key,
                    value,
                    self.stack[ply as usize].tt_pv,
                    Bound::Exact,
                    stored_depth,
                    mate_move,
                    Value::NONE,
                    self.tt.generation(),
                );
                return ProbeOutcome::Cutoff(value);
            }
        }

        ProbeOutcome::Continue(TTContext {
            key,
            result: tt_result,
            data: tt_data,
            hit: tt_hit,
            mv: tt_move,
            value: tt_value,
            capture: tt_capture,
        })
    }

    /// 静的評価と補正値の計算
    ///
    /// `excluded_move`がある場合(Singular Extension中)は既存のstatic_evalを維持する。
    fn compute_eval_context(
        &mut self,
        pos: &mut Position,
        ply: i32,
        in_check: bool,
        tt_ctx: &TTContext,
        excluded_move: Move,
    ) -> EvalContext {
        let correction_value = self.correction_value(pos, ply);

        // excludedMoveがある場合は、前回のstatic_evalをそのまま使用(YaneuraOu準拠)
        if excluded_move.is_some() {
            let static_eval = self.stack[ply as usize].static_eval;
            let improving = if ply >= 2 && !in_check && static_eval != Value::NONE {
                static_eval > self.stack[(ply - 2) as usize].static_eval
            } else {
                false
            };
            let opponent_worsening = if ply >= 1 && static_eval != Value::NONE {
                let prev_eval = self.stack[(ply - 1) as usize].static_eval;
                prev_eval != Value::NONE && static_eval > -prev_eval
            } else {
                false
            };
            return EvalContext {
                static_eval,
                unadjusted_static_eval: static_eval, // excludedMove時は未補正値も同じ
                correction_value,
                improving,
                opponent_worsening,
            };
        }

        let mut unadjusted_static_eval = Value::NONE;
        let mut static_eval = if in_check {
            Value::NONE
        } else if tt_ctx.hit && tt_ctx.data.eval != Value::NONE {
            unadjusted_static_eval = tt_ctx.data.eval;
            unadjusted_static_eval
        } else {
            unadjusted_static_eval = self.nnue_evaluate(pos);
            unadjusted_static_eval
        };

        if !in_check && unadjusted_static_eval != Value::NONE {
            static_eval = to_corrected_static_eval(unadjusted_static_eval, correction_value);
            // パス権評価を動的に追加(TTには保存されないので手数依存でもOK)
            static_eval += evaluate_pass_rights(pos, pos.game_ply() as u16);
        }

        if !in_check
            && tt_ctx.hit
            && tt_ctx.value != Value::NONE
            && !tt_ctx.value.is_mate_score()
            && ((tt_ctx.value > static_eval && tt_ctx.data.bound == Bound::Lower)
                || (tt_ctx.value < static_eval && tt_ctx.data.bound == Bound::Upper))
        {
            static_eval = tt_ctx.value;
        }

        self.stack[ply as usize].static_eval = static_eval;

        let improving = if ply >= 2 && !in_check {
            static_eval > self.stack[(ply - 2) as usize].static_eval
        } else {
            false
        };
        let opponent_worsening = if ply >= 1 && static_eval != Value::NONE {
            let prev_eval = self.stack[(ply - 1) as usize].static_eval;
            prev_eval != Value::NONE && static_eval > -prev_eval
        } else {
            false
        };

        EvalContext {
            static_eval,
            unadjusted_static_eval,
            correction_value,
            improving,
            opponent_worsening,
        }
    }

    /// Razoring
    #[allow(clippy::too_many_arguments)]
    fn try_razoring<const NT: u8>(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        alpha: Value,
        beta: Value,
        ply: i32,
        pv_node: bool,
        in_check: bool,
        static_eval: Value,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) -> Option<Value> {
        if !pv_node && !in_check && depth <= 3 {
            let razoring_threshold = alpha - Value::new(200 * depth);
            if static_eval < razoring_threshold {
                let value = self.qsearch::<{ NodeType::NonPV as u8 }>(
                    pos,
                    DEPTH_QS,
                    alpha,
                    beta,
                    ply,
                    limits,
                    time_manager,
                );
                if value <= alpha {
                    return Some(value);
                }
            }
        }
        None
    }

    /// Futility pruning
    fn try_futility_pruning(&self, params: FutilityParams) -> Option<Value> {
        if !params.pv_node
            && !params.in_check
            && params.depth <= 8
            && params.static_eval != Value::NONE
        {
            let futility_mult =
                FUTILITY_MARGIN_BASE - 20 * (params.cut_node && !params.tt_hit) as i32;
            let futility_margin = Value::new(
                futility_mult * params.depth
                    - (params.improving as i32) * futility_mult * 2
                    - (params.opponent_worsening as i32) * futility_mult / 3
                    + (params.correction_value.abs() / 171_290),
            );

            if params.static_eval - futility_margin >= params.beta {
                return Some(params.static_eval);
            }
        }
        None
    }

    /// Null move pruning with Verification Search(Stockfish/YaneuraOu準拠)
    ///
    /// NMPは、自分の手番で「何もしない(パス)」という仮想的な手を打ち、
    /// それでも優勢なら探索を打ち切る枝刈り技術。
    ///
    /// Verification Search(深度 >= 16 の場合):
    /// - NMPの結果が正しいかを検証するため、NMPを無効化して再探索
    /// - Zugzwang(動かなければならないこと自体が不利な局面)対策
    #[allow(clippy::too_many_arguments)]
    fn try_null_move_pruning<const NT: u8>(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        beta: Value,
        ply: i32,
        cut_node: bool,
        in_check: bool,
        static_eval: Value,
        mut improving: bool,
        excluded_move: Move,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) -> (Option<Value>, bool) {
        // Stockfish/YaneuraOu準拠のNMP条件:
        // - ply >= 1(スタックアクセスの安全性)
        // - Singular Extension中でない(excludedMoveが設定されていない)
        // - cutNodeである(!pv_nodeではなく、より厳密な条件)
        // - 王手されていない
        // - 評価値がマージン付きでbetaを超える(beta - 18 * depth + 390)
        // - ply >= nmp_min_ply(Verification Search中のNMP制限)
        // - betaが負けスコアでない
        // - 前回の手が有効な手(NONE/NULLでない)(連続NullMove禁止)
        //
        // 注: Stockfishでは non_pawn_material(us) チェックがあるが、
        //     YaneuraOuでは将棋で使用していない(#if STOCKFISHで囲まれている)。
        //     チェスでは Pawn endgame の Zugzwang 対策だが、
        //     将棋は持ち駒があるため Pawn だけの終盤は存在しない。
        //     将来検証する場合: && pos.non_pawn_material(pos.side_to_move())

        // ply >= 1 のガード(防御的プログラミング)
        // 実際には search_root から search_node を呼ぶ際に常に ply=1 から開始するため、
        // ply=0 でこの関数が呼ばれることはないが、将来の変更に備えて明示的にガードする。
        if ply < 1 {
            return (None, improving);
        }

        let margin = 18 * depth - 390;
        let prev_move = self.stack[(ply - 1) as usize].current_move;
        // YaneuraOu準拠: prev_move != Move::null() のみをチェック
        // Move::NONEのチェックは不要(root直下でNMPを無効化してしまう)
        // YaneuraOuではASSERT_LV3で検証しているのみ
        // NMP条件に加えて、連続パスの禁止(prev_move が PASS でないこと)
        let prev_is_pass = prev_move.is_pass();
        if excluded_move.is_none()
            && cut_node
            && !in_check
            && static_eval >= beta - Value::new(margin)
            && ply >= self.nmp_min_ply
            && !beta.is_loss()
            && !prev_move.is_null()
            && !prev_is_pass
        // 連続パスを禁止
        {
            // Null move dynamic reduction based on depth(YaneuraOu準拠)
            let r = 7 + depth / 3;

            // パス権ルールが有効かつパス可能な場合は、実際のパス手を使用
            // これにより、パス権の消費が正しく反映され、評価もより正確になる
            let use_pass = pos.is_pass_rights_enabled() && pos.can_pass();

            // YaneuraOu準拠: NullMove探索前にcurrent_moveとcont_hist_keyを設定
            // これにより再帰呼び出し先で連続NullMoveが禁止され、
            // continuation historyの不正参照を防ぐ
            if use_pass {
                self.stack[ply as usize].current_move = Move::PASS;
            } else {
                self.stack[ply as usize].current_move = Move::NULL;
            }
            self.clear_cont_history_for_null(ply);

            // パス手または NullMove を実行
            if use_pass {
                pos.do_pass_move();
            } else {
                pos.do_null_move_with_prefetch(self.tt.as_ref());
            }
            self.nnue_push(DirtyPiece::new()); // パス/null moveでは駒の移動なし
            let null_value = -self.search_node::<{ NodeType::NonPV as u8 }>(
                pos,
                depth - r,
                -beta,
                -beta + Value::new(1),
                ply + 1,
                false, // cutNode = false(NullMove後は相手番なので)
                limits,
                time_manager,
            );
            self.nnue_pop();
            if use_pass {
                pos.undo_pass_move();
            } else {
                pos.undo_null_move();
            }

            // Do not return unproven mate scores(勝ちスコアは信頼しない)
            if null_value >= beta && !null_value.is_win() {
                // 浅い探索 or 既にVerification Search中 → そのままreturn
                if self.nmp_min_ply != 0 || depth < 16 {
                    return (Some(null_value), improving);
                }

                // Verification Search: 深い探索でZugzwangを検出
                // NMPを無効化(nmp_min_plyを設定)して再探索
                self.nmp_min_ply = ply + 3 * (depth - r) / 4;

                let v = self.search_node::<{ NodeType::NonPV as u8 }>(
                    pos,
                    depth - r,
                    beta - Value::new(1),
                    beta,
                    ply,
                    false, // cutNode = false(Verification SearchはcutNodeにしない)
                    limits,
                    time_manager,
                );

                self.nmp_min_ply = 0;

                if v >= beta {
                    return (Some(null_value), improving);
                }
            }
        }

        // improving の更新(Stockfish/YaneuraOu: NMPの後で更新)
        if !in_check && static_eval != Value::NONE {
            improving |= static_eval >= beta;
        }

        (None, improving)
    }

    /// ProbCut(YaneuraOu準拠)
    #[allow(clippy::too_many_arguments)]
    fn try_probcut(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        beta: Value,
        improving: bool,
        tt_ctx: &TTContext,
        ply: i32,
        static_eval: Value,
        unadjusted_static_eval: Value,
        in_check: bool,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) -> Option<Value> {
        if in_check || depth < 3 || static_eval == Value::NONE {
            return None;
        }

        let prob_beta = beta + Value::new(215 - 60 * improving as i32);
        if beta.is_mate_score()
            || (tt_ctx.hit
                && tt_ctx.value != Value::NONE
                && tt_ctx.value < prob_beta
                && !tt_ctx.value.is_mate_score())
        {
            return None;
        }

        let threshold = prob_beta - static_eval;
        if threshold <= Value::ZERO {
            return None;
        }

        let dynamic_reduction = (static_eval - beta).raw() / 300;
        let probcut_depth = (depth - 5 - dynamic_reduction).max(0);

        // 固定長バッファに収集(借用チェッカーの制約回避)
        let probcut_moves = {
            let cont_tables = self.cont_history_tables(ply);
            let mp = MovePicker::new_probcut(
                pos,
                tt_ctx.mv,
                threshold,
                &self.history.main_history,
                &self.history.low_ply_history,
                &self.history.capture_history,
                cont_tables,
                &self.history.pawn_history,
                ply,
                self.generate_all_legal_moves,
            );

            let mut buf = [Move::NONE; crate::movegen::MAX_MOVES];
            let mut len = 0;
            for mv in mp {
                buf[len] = mv;
                len += 1;
            }
            (buf, len)
        };
        let (buf, len) = probcut_moves;

        // YaneuraOu準拠: 全捕獲手を試す
        for &mv in buf[..len].iter() {
            if !pos.is_legal(mv) {
                continue;
            }

            let gives_check = pos.gives_check(mv);
            let is_capture = pos.is_capture(mv);
            let cont_hist_piece = mv.moved_piece_after();
            let cont_hist_to = mv.to();

            self.stack[ply as usize].current_move = mv;
            let dirty_piece = pos.do_move_with_prefetch(mv, gives_check, self.tt.as_ref());
            self.nnue_push(dirty_piece);
            self.nodes += 1;
            self.set_cont_history_for_move(
                ply,
                in_check,
                is_capture,
                cont_hist_piece,
                cont_hist_to,
            );
            let mut value = -self.qsearch::<{ NodeType::NonPV as u8 }>(
                pos,
                DEPTH_QS,
                -prob_beta,
                -prob_beta + Value::new(1),
                ply + 1,
                limits,
                time_manager,
            );

            if value >= prob_beta && probcut_depth > 0 {
                value = -self.search_node::<{ NodeType::NonPV as u8 }>(
                    pos,
                    probcut_depth,
                    -prob_beta,
                    -prob_beta + Value::new(1),
                    ply + 1,
                    true,
                    limits,
                    time_manager,
                );
            }
            self.nnue_pop();
            pos.undo_move(mv);

            if value >= prob_beta {
                let stored_depth = (probcut_depth + 1).max(1);
                tt_ctx.result.write(
                    tt_ctx.key,
                    value_to_tt(value, ply),
                    self.stack[ply as usize].tt_pv,
                    Bound::Lower,
                    stored_depth,
                    mv,
                    unadjusted_static_eval,
                    self.tt.generation(),
                );

                if value.raw().abs() < Value::INFINITE.raw() {
                    return Some(value - (prob_beta - beta));
                }
                return Some(value);
            }
        }

        None
    }

    /// Small ProbCut
    #[inline]
    fn try_small_probcut(&self, depth: Depth, beta: Value, tt_ctx: &TTContext) -> Option<Value> {
        if depth >= 1 {
            let sp_beta = beta + Value::new(417);
            if tt_ctx.hit
                && tt_ctx.data.bound == Bound::Lower
                && tt_ctx.data.depth >= depth - 4
                && tt_ctx.value != Value::NONE
                && tt_ctx.value >= sp_beta
                && !tt_ctx.value.is_mate_score()
                && !beta.is_mate_score()
            {
                return Some(sp_beta);
            }
        }
        None
    }

    /// 指し手生成(TT手優先、qsearch用チェック生成など含む)
    ///
    /// 固定長バッファを使用してヒープ割り当てを回避する。
    fn generate_ordered_moves(
        &self,
        pos: &mut Position,
        tt_move: Move,
        depth: Depth,
        in_check: bool,
        ply: i32,
    ) -> (OrderedMovesBuffer, Move) {
        let mut ordered_moves = OrderedMovesBuffer::new();
        let mut tt_move = tt_move;

        // qsearch/ProbCut互換: 捕獲フェーズではTT手もcapture_stageで制約
        if depth <= DEPTH_QS
            && tt_move.is_some()
            && (!pos.capture_stage(tt_move) && !pos.gives_check(tt_move) || depth < -16)
        {
            tt_move = Move::NONE;
        }

        let cont_tables = self.cont_history_tables(ply);
        let mp = MovePicker::new(
            pos,
            tt_move,
            depth,
            &self.history.main_history,
            &self.history.low_ply_history,
            &self.history.capture_history,
            cont_tables,
            &self.history.pawn_history,
            ply,
            self.generate_all_legal_moves,
        );

        for mv in mp {
            if mv.is_some() {
                ordered_moves.push(mv);
            }
        }

        // qsearchでは捕獲以外のチェックも生成(YaneuraOu準拠)
        if !in_check && depth == DEPTH_QS {
            let mut buf = crate::movegen::ExtMoveBuffer::new();
            let gen_type = if self.generate_all_legal_moves {
                crate::movegen::GenType::QuietChecksAll
            } else {
                crate::movegen::GenType::QuietChecks
            };
            crate::movegen::generate_with_type(pos, gen_type, &mut buf, None);
            for ext in buf.iter() {
                if ordered_moves.contains(&ext.mv) {
                    continue;
                }
                ordered_moves.push(ext.mv);
            }
            // PASSが王手になる場合は quiet check として追加
            if pos.can_pass() && pos.gives_check(Move::PASS) && !ordered_moves.contains(&Move::PASS)
            {
                ordered_moves.push(Move::PASS);
            }
        }

        // depth <= -5 なら recaptures のみに絞る
        // ただし前手がPASSの場合は to() が未定義なのでスキップ
        let prev_move = self.stack[(ply - 1) as usize].current_move;
        if depth <= -5 && ply >= 1 && prev_move.is_normal() {
            let mut buf = crate::movegen::ExtMoveBuffer::new();
            let rec_sq = prev_move.to();
            let gen_type = if self.generate_all_legal_moves {
                crate::movegen::GenType::RecapturesAll
            } else {
                crate::movegen::GenType::Recaptures
            };
            crate::movegen::generate_with_type(pos, gen_type, &mut buf, Some(rec_sq));
            ordered_moves.clear();
            for ext in buf.iter() {
                ordered_moves.push(ext.mv);
            }
        }

        // PASS は最低優先度(探索最後に試す)
        // 静止探索 (depth <= DEPTH_QS) では PASS を追加しない
        // TT手としてPASSが既に追加されている場合は重複を避ける
        if depth > DEPTH_QS && pos.can_pass() && !ordered_moves.contains(&Move::PASS) {
            ordered_moves.push(Move::PASS);
        }

        (ordered_moves, tt_move)
    }

    /// Step14 の枝刈り(進行可否を返す)
    fn step14_pruning(&self, ctx: Step14Context<'_>) -> Step14Outcome {
        // パス手は枝刈りの対象外(ボーナス評価のため探索する必要がある)
        if ctx.mv.is_pass() {
            return Step14Outcome::Continue;
        }

        let mut lmr_depth = ctx.lmr_depth;

        if ctx.ply != 0 && !ctx.best_value.is_loss() {
            let lmp_denominator = 2 - ctx.improving as i32;
            debug_assert!(lmp_denominator > 0, "LMP denominator must be positive");
            let lmp_limit = (3 + ctx.depth * ctx.depth) / lmp_denominator;
            if ctx.move_count >= lmp_limit && !ctx.is_capture && !ctx.gives_check {
                return Step14Outcome::Skip { best_value: None };
            }

            if ctx.is_capture || ctx.gives_check {
                let captured = ctx.pos.piece_on(ctx.mv.to());
                let capt_hist = self.history.capture_history.get_with_captured_piece(
                    ctx.mv.moved_piece_after(),
                    ctx.mv.to(),
                    captured,
                ) as i32;

                if !ctx.gives_check && lmr_depth < 7 && !ctx.in_check {
                    let futility_value = self.stack[ctx.ply as usize].static_eval
                        + Value::new(232 + 224 * lmr_depth)
                        + Value::new(piece_value(captured))
                        + Value::new(131 * capt_hist / 1024);
                    if futility_value <= ctx.alpha {
                        return Step14Outcome::Skip { best_value: None };
                    }
                }

                let margin = (158 * ctx.depth + capt_hist / 31).clamp(0, 283 * ctx.depth);
                if !ctx.pos.see_ge(ctx.mv, Value::new(-margin)) {
                    return Step14Outcome::Skip { best_value: None };
                }
            } else {
                let mut history = 0;
                history += ctx.cont_history_1.get(ctx.mv.moved_piece_after(), ctx.mv.to()) as i32;
                history += ctx.cont_history_2.get(ctx.mv.moved_piece_after(), ctx.mv.to()) as i32;
                history += self.history.pawn_history.get(
                    ctx.pos.pawn_history_index(),
                    ctx.mv.moved_piece_after(),
                    ctx.mv.to(),
                ) as i32;

                if history < -4361 * ctx.depth {
                    return Step14Outcome::Skip { best_value: None };
                }

                history += 71 * self.history.main_history.get(ctx.mover, ctx.mv) as i32 / 32;
                lmr_depth += history / 3233;

                let base_futility = if ctx.best_move.is_some() { 46 } else { 230 };
                let futility_value = self.stack[ctx.ply as usize].static_eval
                    + Value::new(base_futility + 131 * lmr_depth)
                    + Value::new(
                        91 * (self.stack[ctx.ply as usize].static_eval > ctx.alpha) as i32,
                    );

                if !ctx.in_check && lmr_depth < 11 && futility_value <= ctx.alpha {
                    if ctx.best_value <= futility_value
                        && !ctx.best_value.is_mate_score()
                        && !futility_value.is_win()
                    {
                        return Step14Outcome::Skip {
                            best_value: Some(futility_value),
                        };
                    }
                    return Step14Outcome::Skip { best_value: None };
                }

                lmr_depth = lmr_depth.max(0);
                // lmr_depth は探索深さ由来で十数〜数十程度に収まるため i32 乗算でオーバーフローしない
                if !ctx.pos.see_ge(ctx.mv, Value::new(-26 * lmr_depth * lmr_depth)) {
                    return Step14Outcome::Skip { best_value: None };
                }
            }
        }

        Step14Outcome::Continue
    }

    /// 探索のメインエントリーポイント
    ///
    /// 反復深化で指定された深さまで探索する。
    pub fn search(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) {
        // ルート手を初期化
        self.root_moves = RootMoves::from_legal_moves(pos, &limits.search_moves);

        if self.root_moves.is_empty() {
            // 合法手がない場合
            self.best_move = Move::NONE;
            return;
        }

        // 反復深化
        for d in 1..=depth {
            if self.abort {
                break;
            }

            // イテレーション開始時にeffortをリセット
            for rm in self.root_moves.iter_mut() {
                rm.effort = 0.0;
            }

            self.root_depth = d;
            self.sel_depth = 0;

            // Aspiration Window
            let prev_score = if d > 1 {
                self.root_moves[0].score
            } else {
                Value::new(-32001)
            };

            let mut delta = Value::new(10);
            let mut alpha = if d >= 4 {
                Value::new(prev_score.raw().saturating_sub(delta.raw()).max(-32001))
            } else {
                Value::new(-32001)
            };
            let mut beta = if d >= 4 {
                Value::new(prev_score.raw().saturating_add(delta.raw()).min(32001))
            } else {
                Value::new(32001)
            };

            loop {
                let score = self.search_root(pos, d, alpha, beta, limits, time_manager);

                if self.abort {
                    break;
                }

                // Window調整
                if score <= alpha {
                    beta = Value::new((alpha.raw() + beta.raw()) / 2);
                    alpha = Value::new(score.raw().saturating_sub(delta.raw()).max(-32001));
                } else if score >= beta {
                    beta = Value::new(score.raw().saturating_add(delta.raw()).min(32001));
                } else {
                    break;
                }

                delta = Value::new(
                    delta.raw().saturating_add(delta.raw() / 3).min(Value::INFINITE.raw()),
                );
            }

            if !self.abort {
                self.completed_depth = d;
                self.best_move = self.root_moves[0].mv();
            }
        }
    }

    /// ルート探索
    pub(crate) fn search_root(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        alpha: Value,
        beta: Value,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) -> Value {
        self.root_delta = (beta.raw() - alpha.raw()).abs().max(1);

        let mut alpha = alpha;
        let mut best_value = Value::new(-32001);
        let mut pv_idx = 0;
        let root_in_check = pos.in_check();

        self.stack[0].in_check = root_in_check;
        self.stack[0].cont_history_ptr = self.cont_history_sentinel;
        self.stack[0].cont_hist_key = None;

        // PVをクリアして前回探索の残留を防ぐ
        // NOTE: YaneuraOuでは (ss+1)->pv = pv でポインタを新配列に向け、ss->pv[0] = Move::none() でクリア
        //       Vecベースの実装では明示的なclear()で同等の効果を得る
        self.stack[0].pv.clear();
        self.stack[1].pv.clear();

        for rm_idx in 0..self.root_moves.len() {
            if self.check_abort(limits, time_manager) {
                return Value::ZERO;
            }

            // 各手ごとにsel_depthをリセット(YaneuraOu準拠)
            self.sel_depth = 0;

            let mv = self.root_moves[rm_idx].mv();
            let gives_check = pos.gives_check(mv);
            let is_capture = pos.is_capture(mv);

            let nodes_before = self.nodes;

            // 探索
            let dirty_piece = pos.do_move_with_prefetch(mv, gives_check, self.tt.as_ref());
            self.nnue_push(dirty_piece);
            self.nodes += 1;
            self.stack[0].current_move = mv;

            // PASS は to()/moved_piece_after() が未定義のため、null move と同様に扱う
            if mv.is_pass() {
                self.clear_cont_history_for_null(0);
            } else {
                let cont_hist_piece = mv.moved_piece_after();
                let cont_hist_to = mv.to();
                self.set_cont_history_for_move(
                    0,
                    root_in_check,
                    is_capture,
                    cont_hist_piece,
                    cont_hist_to,
                );
            }

            // PVS
            let value = if rm_idx == 0 {
                -self.search_node::<{ NodeType::PV as u8 }>(
                    pos,
                    depth - 1,
                    -beta,
                    -alpha,
                    1,
                    false,
                    limits,
                    time_manager,
                )
            } else {
                // Zero Window Search
                let mut value = -self.search_node::<{ NodeType::NonPV as u8 }>(
                    pos,
                    depth - 1,
                    -alpha - Value::new(1),
                    -alpha,
                    1,
                    true,
                    limits,
                    time_manager,
                );

                // Re-search if needed
                if value > alpha && value < beta {
                    value = -self.search_node::<{ NodeType::PV as u8 }>(
                        pos,
                        depth - 1,
                        -beta,
                        -alpha,
                        1,
                        false,
                        limits,
                        time_manager,
                    );
                }

                value
            };

            self.nnue_pop();
            pos.undo_move(mv);

            // この手に費やしたノード数をeffortに積算
            let nodes_delta = self.nodes.saturating_sub(nodes_before);
            self.root_moves[rm_idx].effort += nodes_delta as f64;

            if self.abort {
                return Value::ZERO;
            }

            // スコア更新(この手の探索で到達したsel_depthを記録)
            let mut updated_alpha = rm_idx == 0; // 先頭手は維持(YO準拠)
            {
                let rm = &mut self.root_moves[rm_idx];
                rm.score = value;
                rm.sel_depth = self.sel_depth;
                rm.accumulate_score_stats(value);
            }

            if value > best_value {
                best_value = value;

                if value > alpha {
                    // YaneuraOu準拠: 2番目以降の手がalphaを更新した場合にカウント
                    // moveCount > 1 && !pvIdx の条件
                    // (Multi-PV未実装なので pvIdx は常に0)
                    if rm_idx > 0 {
                        self.best_move_changes += 1.0;
                    }

                    alpha = value;
                    pv_idx = rm_idx;
                    updated_alpha = true;

                    // PVを更新
                    self.root_moves[rm_idx].pv.truncate(1);
                    self.root_moves[rm_idx].pv.extend_from_slice(&self.stack[1].pv);

                    if value >= beta {
                        break;
                    }
                }
            }

            // α未更新の手はスコアを -INFINITE に落として順序維持(YO準拠)
            if !updated_alpha {
                self.root_moves[rm_idx].score = Value::new(-Value::INFINITE.raw());
            }
        }

        // 最善手を先頭に移動
        self.root_moves.move_to_front(pv_idx);
        self.root_moves.sort();

        best_value
    }

    /// 特定のPVライン(pv_idx)のみを探索
    ///
    /// YaneuraOuのMultiPVループに相当。
    /// pv_idx以降の手のみを探索対象とし、0..pv_idxの手は固定とみなす。
    ///
    /// # Arguments
    /// * `pos` - 現在の局面
    /// * `depth` - 探索深さ
    /// * `alpha` - アルファ値
    /// * `beta` - ベータ値
    /// * `pv_idx` - 探索対象のPVインデックス(0-indexed)
    /// * `limits` - 探索制限
    /// * `time_manager` - 時間管理
    pub(crate) fn search_root_for_pv(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        alpha: Value,
        beta: Value,
        pv_idx: usize,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) -> Value {
        self.root_delta = (beta.raw() - alpha.raw()).abs().max(1);

        let mut alpha = alpha;
        let mut best_value = Value::new(-32001);
        let mut best_rm_idx = pv_idx;
        let root_in_check = pos.in_check();

        self.stack[0].in_check = root_in_check;
        self.stack[0].cont_history_ptr = self.cont_history_sentinel;
        self.stack[0].cont_hist_key = None;

        // PVをクリアして前回探索の残留を防ぐ
        // NOTE: YaneuraOuでは (ss+1)->pv = pv でポインタを新配列に向け、ss->pv[0] = Move::none() でクリア
        //       Vecベースの実装では明示的なclear()で同等の効果を得る
        self.stack[0].pv.clear();
        self.stack[1].pv.clear();

        // pv_idx以降の手のみを探索
        for rm_idx in pv_idx..self.root_moves.len() {
            if self.check_abort(limits, time_manager) {
                return Value::ZERO;
            }

            // 各手ごとにsel_depthをリセット
            self.sel_depth = 0;

            let mv = self.root_moves[rm_idx].mv();
            let gives_check = pos.gives_check(mv);
            let is_capture = pos.is_capture(mv);

            let nodes_before = self.nodes;

            // 探索
            let dirty_piece = pos.do_move_with_prefetch(mv, gives_check, self.tt.as_ref());
            self.nnue_push(dirty_piece);
            self.nodes += 1;
            self.stack[0].current_move = mv;

            // PASS は to()/moved_piece_after() が未定義のため、null move と同様に扱う
            if mv.is_pass() {
                self.clear_cont_history_for_null(0);
            } else {
                let cont_hist_piece = mv.moved_piece_after();
                let cont_hist_to = mv.to();
                self.set_cont_history_for_move(
                    0,
                    root_in_check,
                    is_capture,
                    cont_hist_piece,
                    cont_hist_to,
                );
            }

            // PVS: 最初の手(このPVラインの候補)はPV探索
            let value = if rm_idx == pv_idx {
                -self.search_node::<{ NodeType::PV as u8 }>(
                    pos,
                    depth - 1,
                    -beta,
                    -alpha,
                    1,
                    false,
                    limits,
                    time_manager,
                )
            } else {
                // それ以降はZero Window Search
                let mut value = -self.search_node::<{ NodeType::NonPV as u8 }>(
                    pos,
                    depth - 1,
                    -alpha - Value::new(1),
                    -alpha,
                    1,
                    true,
                    limits,
                    time_manager,
                );

                // Re-search if needed
                if value > alpha && value < beta {
                    value = -self.search_node::<{ NodeType::PV as u8 }>(
                        pos,
                        depth - 1,
                        -beta,
                        -alpha,
                        1,
                        false,
                        limits,
                        time_manager,
                    );
                }

                value
            };

            self.nnue_pop();
            pos.undo_move(mv);

            // この手に費やしたノード数をeffortに積算
            let nodes_delta = self.nodes.saturating_sub(nodes_before);
            self.root_moves[rm_idx].effort += nodes_delta as f64;

            if self.abort {
                return Value::ZERO;
            }

            // スコア更新
            let mut updated_alpha = rm_idx == pv_idx; // PVラインの先頭は維持
            {
                let rm = &mut self.root_moves[rm_idx];
                rm.score = value;
                rm.sel_depth = self.sel_depth;
                rm.accumulate_score_stats(value);
            }

            if value > best_value {
                best_value = value;

                if value > alpha {
                    // best_move_changesのカウント(2番目以降の手で更新された場合)
                    // MultiPVでは pv_idx == 0(第1PVライン)のみカウントする
                    if pv_idx == 0 && rm_idx > pv_idx {
                        self.best_move_changes += 1.0;
                    }

                    alpha = value;
                    best_rm_idx = rm_idx;
                    updated_alpha = true;

                    // PVを更新
                    self.root_moves[rm_idx].pv.truncate(1);
                    self.root_moves[rm_idx].pv.extend_from_slice(&self.stack[1].pv);

                    if value >= beta {
                        break;
                    }
                }
            }

            // α未更新の手は -INFINITE で前回順序を保持(YO準拠)
            if !updated_alpha {
                self.root_moves[rm_idx].score = Value::new(-Value::INFINITE.raw());
            }
        }

        // 最善手をpv_idxの位置に移動
        self.root_moves.move_to_index(best_rm_idx, pv_idx);

        best_value
    }

    /// 通常探索ノード
    ///
    /// NTは NodeType を const genericで受け取る(コンパイル時最適化)
    /// cut_node は「βカットが期待される(ゼロウィンドウの非PVなど)」ときに true を渡す。
    /// 再探索やPV探索では all_node 扱いにするため false を渡す(YaneuraOuのcutNode引き渡しと対応)。
    fn search_node<const NT: u8>(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        alpha: Value,
        beta: Value,
        ply: i32,
        cut_node: bool,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) -> Value {
        let pv_node = NT == NodeType::PV as u8 || NT == NodeType::Root as u8;
        let mut depth = depth;
        let in_check = pos.in_check();
        // YaneuraOuのallNode定義: !(PvNode || cutNode)(yaneuraou-search.cpp:1854付近)
        let all_node = !(pv_node || cut_node);
        let mut alpha = alpha;

        // 深さが0以下なら静止探索へ
        if depth <= DEPTH_QS {
            return self.qsearch::<NT>(pos, depth, alpha, beta, ply, limits, time_manager);
        }

        // 最大深さチェック
        if ply >= MAX_PLY {
            return if in_check {
                Value::ZERO
            } else {
                self.nnue_evaluate(pos)
            };
        }

        // 選択的深さを更新
        if pv_node && self.sel_depth < ply + 1 {
            self.sel_depth = ply + 1;
        }

        // 中断チェック
        if self.check_abort(limits, time_manager) {
            return Value::ZERO;
        }

        // スタック設定
        self.stack[ply as usize].in_check = in_check;
        self.stack[ply as usize].move_count = 0;
        self.stack[(ply + 1) as usize].cutoff_cnt = 0;

        // PVノードの場合、PVをクリアして前回探索の残留を防ぐ
        // NOTE: YaneuraOuでは (ss+1)->pv = pv でポインタを新配列に向け、ss->pv[0] = Move::none() でクリア
        //       Vecベースの実装では明示的なclear()で同等の効果を得る
        if pv_node {
            self.stack[ply as usize].pv.clear();
            self.stack[(ply + 1) as usize].pv.clear();
        }

        let prior_reduction = self.take_prior_reduction(ply);
        self.stack[ply as usize].reduction = 0;

        // Singular Extension用の除外手を取得
        let excluded_move = self.stack[ply as usize].excluded_move;

        // 置換表プローブ(即時カットオフ含む)
        let tt_ctx = match self.probe_transposition::<NT>(
            pos,
            depth,
            beta,
            ply,
            pv_node,
            in_check,
            excluded_move,
        ) {
            ProbeOutcome::Continue(ctx) => ctx,
            ProbeOutcome::Cutoff(value) => return value,
        };
        let tt_move = tt_ctx.mv;
        let tt_value = tt_ctx.value;
        let tt_hit = tt_ctx.hit;
        let tt_data = tt_ctx.data;
        let tt_capture = tt_ctx.capture;

        // 静的評価
        let eval_ctx = self.compute_eval_context(pos, ply, in_check, &tt_ctx, excluded_move);
        let mut improving = eval_ctx.improving;
        let opponent_worsening = eval_ctx.opponent_worsening;

        // priorReduction に応じた深さ調整(yaneuraou-search.cpp:2769-2774)
        if prior_reduction
            >= if depth < IIR_DEPTH_BOUNDARY {
                IIR_PRIOR_REDUCTION_THRESHOLD_SHALLOW
            } else {
                IIR_PRIOR_REDUCTION_THRESHOLD_DEEP
            }
            && !opponent_worsening
        {
            depth += 1;
        }
        if prior_reduction >= 2
            && depth >= 2
            && ply >= 1
            && eval_ctx.static_eval != Value::NONE
            && self.stack[(ply - 1) as usize].static_eval != Value::NONE
            // Value は ±32002 程度なので i32 加算でオーバーフローしない
            && eval_ctx.static_eval + self.stack[(ply - 1) as usize].static_eval
                > Value::new(IIR_EVAL_SUM_THRESHOLD)
        {
            depth -= 1;
        }

        if let Some(v) = self.try_razoring::<NT>(
            pos,
            depth,
            alpha,
            beta,
            ply,
            pv_node,
            in_check,
            eval_ctx.static_eval,
            limits,
            time_manager,
        ) {
            return v;
        }

        if let Some(v) = self.try_futility_pruning(FutilityParams {
            depth,
            beta,
            static_eval: eval_ctx.static_eval,
            correction_value: eval_ctx.correction_value,
            improving,
            opponent_worsening,
            cut_node,
            tt_hit,
            pv_node,
            in_check,
        }) {
            return v;
        }

        let (null_value, improving_after_null) = self.try_null_move_pruning::<NT>(
            pos,
            depth,
            beta,
            ply,
            cut_node,
            in_check,
            eval_ctx.static_eval,
            improving,
            excluded_move,
            limits,
            time_manager,
        );
        if let Some(v) = null_value {
            return v;
        }
        improving = improving_after_null;

        // Internal Iterative Reductions(improving再計算後に実施)
        // YaneuraOu準拠: !allNode && depth>=6 && !ttMove && priorReduction<=3
        // (yaneuraou-search.cpp:2912-2919)
        if !all_node && depth >= 6 && tt_move.is_none() && prior_reduction <= 3 {
            depth -= 1;
        }

        if let Some(v) = self.try_probcut(
            pos,
            depth,
            beta,
            improving,
            &tt_ctx,
            ply,
            eval_ctx.static_eval,
            eval_ctx.unadjusted_static_eval,
            in_check,
            limits,
            time_manager,
        ) {
            return v;
        }

        if let Some(v) = self.try_small_probcut(depth, beta, &tt_ctx) {
            return v;
        }

        // =================================================================
        // 指し手ループ
        // =================================================================
        let mut best_value = Value::new(-32001);
        let mut best_move = Move::NONE;
        let mut move_count = 0;
        let mut quiets_tried = SearchedMoveList::new();
        let mut captures_tried = SearchedMoveList::new();
        let mover = pos.side_to_move();

        let (ordered_moves, tt_move) =
            self.generate_ordered_moves(pos, tt_move, depth, in_check, ply);

        // Singular Extension用の変数
        let tt_pv = self.stack[ply as usize].tt_pv;
        let root_node = NT == NodeType::Root as u8;

        for mv in ordered_moves.iter() {
            // Singular Extension用の除外手をスキップ(YaneuraOu準拠)
            if mv == excluded_move {
                continue;
            }
            if !pos.pseudo_legal(mv) {
                continue;
            }
            if !pos.is_legal(mv) {
                continue;
            }
            if self.check_abort(limits, time_manager) {
                return Value::ZERO;
            }

            move_count += 1;
            self.stack[ply as usize].move_count = move_count;

            let is_capture = pos.is_capture(mv);
            let gives_check = pos.gives_check(mv);

            // quietの指し手の連続回数(YaneuraOu: quietMoveStreak)
            self.stack[(ply + 1) as usize].quiet_move_streak = if !is_capture && !gives_check {
                self.stack[ply as usize].quiet_move_streak + 1
            } else {
                0
            };

            let mut new_depth = depth - 1;
            let mut extension = 0i32;

            // =============================================================
            // Singular Extension(YaneuraOu準拠)
            // =============================================================
            // singular延長をするnodeであるか判定
            // 条件: !rootNode && move == ttMove && !excludedMove && depth >= 6 + ttPv
            //       && is_valid(ttValue) && !is_decisive(ttValue) && (ttBound & BOUND_LOWER)
            //       && ttDepth >= depth - 3
            if !root_node
                && mv == tt_move
                && excluded_move.is_none()
                && depth >= 6 + tt_pv as i32
                && tt_value != Value::NONE
                && !tt_value.is_mate_score()
                && tt_data.bound.is_lower_or_exact()
                && tt_data.depth >= depth - 3
            {
                // singularBeta = ttValue - (56 + 79 * (ttPv && !PvNode)) * depth / 58
                let singular_beta_margin = (56 + 79 * (tt_pv && !pv_node) as i32) * depth / 58;
                let singular_beta = tt_value - Value::new(singular_beta_margin);
                let singular_depth = new_depth / 2;

                // ttMoveを除外して浅い探索を実行
                // 注: YaneuraOu準拠で同じplyで再帰呼び出しを行う(do_moveせず同一局面で探索)
                // これによりstack[ply]の一部フィールド(tt_hit, move_count等)が上書きされるが:
                // - tt_pv: excludedMoveがある場合は保持される(probe_transposition内)
                // - tt_hit: 同じ局面なので同じ値になる
                // - move_count: ローカル変数で管理しているため影響なし
                // - その他: ヒューリスティック用途のため多少の誤差は許容される
                self.stack[ply as usize].excluded_move = mv;
                let singular_value = self.search_node::<{ NodeType::NonPV as u8 }>(
                    pos,
                    singular_depth,
                    singular_beta - Value::new(1),
                    singular_beta,
                    ply,
                    cut_node,
                    limits,
                    time_manager,
                );
                self.stack[ply as usize].excluded_move = Move::NONE;

                if singular_value < singular_beta {
                    // Singular確定 → 延長量を計算
                    // 補正履歴の寄与(abs(correctionValue)/249096)を margin に加算
                    let corr_val_adj = eval_ctx.correction_value.abs() / 249_096;
                    let double_margin =
                        4 + 205 * pv_node as i32 - 223 * !tt_capture as i32 - corr_val_adj;
                    let triple_margin = 80 + 276 * pv_node as i32 - 249 * !tt_capture as i32
                        + 86 * tt_pv as i32
                        - corr_val_adj;

                    extension = 1
                        + (singular_value < singular_beta - Value::new(double_margin)) as i32
                        + (singular_value < singular_beta - Value::new(triple_margin)) as i32;

                    // YaneuraOu準拠: singular確定時にdepthを+1(yaneuraou-search.cpp:3401)
                    depth += 1;
                } else if singular_value >= beta && !singular_value.is_mate_score() {
                    // Multi-Cut: 他の手もfail highする場合は枝刈り
                    return singular_value;
                } else if tt_value >= beta {
                    // Negative Extension: ttMoveが特別でない場合
                    extension = -3;
                } else if cut_node {
                    extension = -2;
                }
            }

            // 注: YaneuraOu準拠で、new_depth += extension はdo_moveの後で行う(3482行目)
            // ここではextensionを保持しておき、枝刈りはextension反映前のnew_depthで判定

            // =============================================================
            // Reduction計算とStep14の枝刈り
            // =============================================================
            let delta = (beta.raw() - alpha.raw()).max(0);
            let mut r = reduction(improving, depth, move_count, delta, self.root_delta.max(1));

            // YaneuraOu: ttPvなら reduction を少し増やす(yaneuraou-search.cpp:3168-3170)
            if self.stack[ply as usize].tt_pv {
                r += 931;
            }

            let lmr_depth = new_depth - r / 1024;

            let step14_ctx = Step14Context {
                pos,
                mv,
                depth,
                ply,
                improving,
                best_move,
                best_value,
                alpha,
                in_check,
                gives_check,
                is_capture,
                lmr_depth,
                mover,
                move_count,
                cont_history_1: self.cont_history_ref(ply, 1),
                cont_history_2: self.cont_history_ref(ply, 2),
            };

            match self.step14_pruning(step14_ctx) {
                Step14Outcome::Skip {
                    best_value: updated,
                } => {
                    if let Some(v) = updated {
                        best_value = v;
                    }
                    continue;
                }
                Step14Outcome::Continue => {}
            }

            // =============================================================
            // Late Move Pruning
            // =============================================================
            // パス手はLMPの対象外(ボーナス評価のため探索する必要がある)
            if !pv_node && !in_check && !is_capture && !mv.is_pass() {
                let lmp_limit = (3 + depth * depth) / (2 - improving as i32);
                if move_count >= lmp_limit {
                    continue;
                }
            }

            // =============================================================
            // SEE Pruning
            // =============================================================
            // パス手はSEE Pruningの対象外(SEEは駒交換の評価でありパスには適用不可)
            if !pv_node && depth <= 8 && !in_check && !mv.is_pass() {
                let see_threshold = if is_capture {
                    Value::new(-20 * depth * depth)
                } else {
                    Value::new(-50 * depth)
                };
                if !pos.see_ge(mv, see_threshold) {
                    continue;
                }
            }

            // 指し手を実行
            self.stack[ply as usize].current_move = mv;

            let dirty_piece = pos.do_move_with_prefetch(mv, gives_check, self.tt.as_ref());
            self.nnue_push(dirty_piece);
            self.nodes += 1;

            // YaneuraOu方式: ContHistKey/ContinuationHistoryを設定
            // ⚠ in_checkは親ノードの王手状態を使用(gives_checkではない)
            // PASS は to()/moved_piece_after() が未定義のため、null move と同様に扱う
            if mv.is_pass() {
                self.clear_cont_history_for_null(ply);
            } else {
                let cont_hist_piece = mv.moved_piece_after();
                let cont_hist_to = mv.to();
                self.set_cont_history_for_move(
                    ply,
                    in_check,
                    is_capture,
                    cont_hist_piece,
                    cont_hist_to,
                );
            }

            // 手の記録(YaneuraOu準拠: quietsSearched, capturesSearched)
            // PASS は history_index() が未定義のため記録しない
            if !mv.is_pass() {
                if is_capture {
                    captures_tried.push(mv);
                } else {
                    quiets_tried.push(mv);
                }
            }

            // 延長量をnew_depthに加算(YaneuraOu準拠: do_moveの後、yaneuraou-search.cpp:3482)
            new_depth += extension;

            // =============================================================
            // Late Move Reduction (LMR)
            // =============================================================
            let msb_depth = msb(depth);
            let tt_value_higher = tt_hit && tt_value != Value::NONE && tt_value > alpha;
            let tt_depth_ge = tt_hit && tt_data.depth >= depth;

            if self.stack[ply as usize].tt_pv {
                r -= 2510
                    + (pv_node as i32) * 963
                    + (tt_value_higher as i32) * 916
                    + (tt_depth_ge as i32) * (943 + (cut_node as i32) * 1180);
            }

            r += 679 - 6 * msb_depth;
            r -= move_count * (67 - 2 * msb_depth);
            r -= eval_ctx.correction_value.abs() / 27_160;

            if cut_node {
                let no_tt_move = !tt_hit || tt_move.is_none();
                r += 2998 + 2 * msb_depth + (948 + 14 * msb_depth) * (no_tt_move as i32);
            }

            if tt_capture {
                r += 1402 - 39 * msb_depth;
            }

            if self.stack[(ply + 1) as usize].cutoff_cnt > 2 {
                r += 925 + 33 * msb_depth + (all_node as i32) * (701 + 224 * msb_depth);
            }

            r += self.stack[(ply + 1) as usize].quiet_move_streak * 51;

            if mv == tt_move {
                r -= 2121 + 28 * msb_depth;
            }

            // statScoreによる補正
            // PASS は history が未定義のため stat_score = 0 とし、還元を控えめに
            let stat_score = if mv.is_pass() {
                0 // PASS は history がないので還元補正なし
            } else if is_capture {
                let captured = pos.captured_piece();
                let captured_pt = captured.piece_type();
                let moved_piece = mv.moved_piece_after();
                let hist =
                    self.history.capture_history.get(moved_piece, mv.to(), captured_pt) as i32;
                782 * piece_value(captured) / 128 + hist
            } else {
                let moved_piece = mv.moved_piece_after();
                let main_hist = self.history.main_history.get(mover, mv) as i32;
                let cont0 = self.cont_history_ref(ply, 1).get(moved_piece, mv.to()) as i32;
                let cont1 = self.cont_history_ref(ply, 2).get(moved_piece, mv.to()) as i32;
                2 * main_hist + cont0 + cont1
            };
            self.stack[ply as usize].stat_score = stat_score;
            r -= stat_score * (729 - 12 * msb_depth) / 8192;

            // =============================================================
            // 探索
            // =============================================================
            let mut value = if depth >= 2 && move_count > 1 {
                let d = (std::cmp::max(
                    1,
                    std::cmp::min(new_depth - r / 1024, new_depth + 1 + pv_node as i32),
                ) + pv_node as i32)
                    .max(1);

                let reduction_from_parent = (depth - 1) - d;
                self.stack[ply as usize].reduction = reduction_from_parent;
                let mut value = -self.search_node::<{ NodeType::NonPV as u8 }>(
                    pos,
                    d,
                    -alpha - Value::new(1),
                    -alpha,
                    ply + 1,
                    true,
                    limits,
                    time_manager,
                );
                self.stack[ply as usize].reduction = 0;

                if value > alpha {
                    let do_deeper =
                        d < new_depth && value > (best_value + Value::new(43 + 2 * new_depth));
                    let do_shallower = value < best_value + Value::new(9);
                    new_depth += do_deeper as i32 - do_shallower as i32;

                    if new_depth > d {
                        value = -self.search_node::<{ NodeType::NonPV as u8 }>(
                            pos,
                            new_depth,
                            -alpha - Value::new(1),
                            -alpha,
                            ply + 1,
                            !cut_node,
                            limits,
                            time_manager,
                        );
                    }

                    // YaneuraOu: fail high後にcontHistを更新 (yaneuraou-search.cpp:3614-3618)
                    // PASS は履歴の対象外なのでスキップ
                    if !mv.is_pass() {
                        let moved_piece = mv.moved_piece_after();
                        let to_sq = mv.to();
                        const CONTHIST_BONUSES: &[(i32, i32)] =
                            &[(1, 1108), (2, 652), (3, 273), (4, 572), (5, 126), (6, 449)];
                        for &(offset, weight) in CONTHIST_BONUSES {
                            if self.stack[ply as usize].in_check && offset > 2 {
                                break;
                            }
                            let idx = ply - offset;
                            if idx < 0 {
                                break;
                            }
                            if let Some(key) = self.stack[idx as usize].cont_hist_key {
                                let in_check_idx = key.in_check as usize;
                                let capture_idx = key.capture as usize;
                                let bonus = 1412 * weight / 1024 + if offset < 2 { 80 } else { 0 };
                                self.history.continuation_history[in_check_idx][capture_idx]
                                    .update(key.piece, key.to, moved_piece, to_sq, bonus);
                            }
                        }
                    }
                    // cutoffCntインクリメント条件 (extension<2 || PvNode) をベータカット時に加算で近似。
                    // ※ Extension導入後は extension<2 を実際の延長量で判定する形に差し替えること。
                } else if value > alpha && value < best_value + Value::new(9) {
                    #[allow(unused_assignments)]
                    {
                        new_depth -= 1;
                    }
                }

                if pv_node && (move_count == 1 || value > alpha) {
                    self.stack[ply as usize].reduction = 0;
                    -self.search_node::<{ NodeType::PV as u8 }>(
                        pos,
                        depth - 1,
                        -beta,
                        -alpha,
                        ply + 1,
                        false,
                        limits,
                        time_manager,
                    )
                } else {
                    value
                }
            } else if !pv_node || move_count > 1 {
                // Zero window search
                self.stack[ply as usize].reduction = 0;
                let mut value = -self.search_node::<{ NodeType::NonPV as u8 }>(
                    pos,
                    depth - 1,
                    -alpha - Value::new(1),
                    -alpha,
                    ply + 1,
                    !cut_node,
                    limits,
                    time_manager,
                );
                self.stack[ply as usize].reduction = 0;

                if pv_node && value > alpha && value < beta {
                    self.stack[ply as usize].reduction = 0;
                    value = -self.search_node::<{ NodeType::PV as u8 }>(
                        pos,
                        depth - 1,
                        -beta,
                        -alpha,
                        ply + 1,
                        false,
                        limits,
                        time_manager,
                    );
                    self.stack[ply as usize].reduction = 0;
                }

                value
            } else {
                // Full window search
                self.stack[ply as usize].reduction = 0;
                -self.search_node::<{ NodeType::PV as u8 }>(
                    pos,
                    depth - 1,
                    -beta,
                    -alpha,
                    ply + 1,
                    false,
                    limits,
                    time_manager,
                )
            };
            self.nnue_pop();
            pos.undo_move(mv);

            // パス手評価ボーナス: パス手を実行した場合、評価値にボーナスを加算
            // スケーリングなし(常に設定値の100%を適用)
            // 負のボーナスも適用(パス抑制用途)
            // 注意: 詰みスコアには加算しない(mate距離が壊れるため)
            if mv.is_pass() && !value.is_mate_score() {
                let bonus = get_scaled_pass_move_bonus(pos.game_ply());
                if bonus != 0 {
                    value += Value::new(bonus);
                }
            }

            if self.abort {
                return Value::ZERO;
            }

            // =============================================================
            // スコア更新
            // =============================================================
            if value > best_value {
                best_value = value;

                if value > alpha {
                    best_move = mv;
                    alpha = value;

                    // PV更新
                    if pv_node {
                        // 借用チェッカーの制約を避けるためクローン
                        let child_pv = self.stack[(ply + 1) as usize].pv.clone();
                        self.stack[ply as usize].update_pv(mv, &child_pv);
                    }

                    if value >= beta {
                        // cutoffCntインクリメント条件 (extension<2 || PvNode) をベータカット時に加算で近似。
                        self.stack[ply as usize].cutoff_cnt += 1;
                        break;
                    }
                }
            }
        }

        // =================================================================
        // 詰み/ステイルメイト判定
        // =================================================================
        // YaneuraOu準拠: excludedMoveがある場合は、ttMoveが除外されているので
        // 単にalphaを返す(詰みとは判定しない)
        if move_count == 0 {
            if excluded_move.is_some() {
                return alpha;
            }
            // 合法手なし
            if in_check {
                // 詰み
                return Value::mated_in(ply);
            } else {
                // ステイルメイト(将棋では通常発生しないがパスがない場合)
                return Value::ZERO;
            }
        }

        // =================================================================
        // History更新(YaneuraOu準拠: update_all_stats)
        // =================================================================
        // YaneuraOu: bestMoveがある場合は常にupdate_all_statsを呼ぶ
        // PASS は history_index() が未定義のためスキップ
        if best_move.is_some() && !best_move.is_pass() {
            let is_best_capture = pos.is_capture(best_move);
            let is_tt_move = best_move == tt_move;
            // YaneuraOu準拠: bonus = min(170*depth-87, 1598) + 332*(bestMove==ttMove)
            let bonus = stat_bonus(depth, is_tt_move);
            // YaneuraOu準拠: quietMalus = min(743*depth-180, 2287) - 33*quietsSearched.size()
            let malus = quiet_malus(depth, quiets_tried.len());
            let us = pos.side_to_move();
            let pawn_key_idx = pos.pawn_history_index();

            // best_moveの駒情報を取得
            let best_moved_pc = pos.moved_piece(best_move);
            let best_cont_pc = if best_move.is_promotion() {
                best_moved_pc.promote().unwrap_or(best_moved_pc)
            } else {
                best_moved_pc
            };
            let best_to = best_move.to();

            // 王手中は1,2手前のみ
            let max_ply_back = if in_check { 2 } else { 6 };

            if !is_best_capture {
                // Quiet手がbest: update_quiet_histories(bestMove, bonus * 978 / 1024)相当
                // YaneuraOu準拠: bonus * 978 / 1024をベースに各historyを更新
                let scaled_bonus = bonus * 978 / 1024;

                // MainHistory: そのまま渡す
                self.history.main_history.update(us, best_move, scaled_bonus);

                // LowPlyHistory: bonus * 771 / 1024 + 40
                if ply < LOW_PLY_HISTORY_SIZE as i32 {
                    let low_ply_bonus = low_ply_history_bonus(scaled_bonus);
                    self.history.low_ply_history.update(ply as usize, best_move, low_ply_bonus);
                }

                // ContinuationHistory: bonus * (bonus > 0 ? 979 : 842) / 1024 + weight + 80*(i<2)
                for &(ply_back, weight) in CONTINUATION_HISTORY_WEIGHTS.iter() {
                    if ply_back > max_ply_back {
                        continue;
                    }
                    if ply >= ply_back as i32 {
                        let prev_ply = (ply - ply_back as i32) as usize;
                        if let Some(key) = self.stack[prev_ply].cont_hist_key {
                            let in_check_idx = key.in_check as usize;
                            let capture_idx = key.capture as usize;
                            let weighted_bonus = continuation_history_bonus_with_offset(
                                scaled_bonus * weight / 1024,
                                ply_back,
                            );
                            self.history.continuation_history[in_check_idx][capture_idx].update(
                                key.piece,
                                key.to,
                                best_cont_pc,
                                best_to,
                                weighted_bonus,
                            );
                        }
                    }
                }

                // PawnHistory: bonus * 704 / 1024 + 70
                let pawn_bonus = pawn_history_bonus(scaled_bonus);
                self.history
                    .pawn_history
                    .update(pawn_key_idx, best_cont_pc, best_to, pawn_bonus);

                // 他のquiet手にはペナルティ
                // YaneuraOu: update_quiet_histories(move, -quietMalus * 1115 / 1024)
                let scaled_malus = malus * 1115 / 1024;
                for &m in quiets_tried.iter() {
                    if m != best_move {
                        // MainHistory
                        self.history.main_history.update(us, m, -scaled_malus);

                        // LowPlyHistory(現行欠落していたので追加)
                        if ply < LOW_PLY_HISTORY_SIZE as i32 {
                            let low_ply_malus = low_ply_history_bonus(-scaled_malus);
                            self.history.low_ply_history.update(ply as usize, m, low_ply_malus);
                        }

                        // ContinuationHistory/PawnHistoryへのペナルティで必要な情報
                        let moved_pc = pos.moved_piece(m);
                        let cont_pc = if m.is_promotion() {
                            moved_pc.promote().unwrap_or(moved_pc)
                        } else {
                            moved_pc
                        };
                        let to = m.to();

                        // ContinuationHistoryへのペナルティ
                        for &(ply_back, weight) in CONTINUATION_HISTORY_WEIGHTS.iter() {
                            if ply_back > max_ply_back {
                                continue;
                            }
                            if ply >= ply_back as i32 {
                                let prev_ply = (ply - ply_back as i32) as usize;
                                if let Some(key) = self.stack[prev_ply].cont_hist_key {
                                    let in_check_idx = key.in_check as usize;
                                    let capture_idx = key.capture as usize;
                                    let weighted_malus = continuation_history_bonus_with_offset(
                                        -scaled_malus * weight / 1024,
                                        ply_back,
                                    );
                                    self.history.continuation_history[in_check_idx][capture_idx]
                                        .update(key.piece, key.to, cont_pc, to, weighted_malus);
                                }
                            }
                        }

                        // PawnHistoryへのペナルティ
                        let pawn_malus = pawn_history_bonus(-scaled_malus);
                        self.history.pawn_history.update(pawn_key_idx, cont_pc, to, pawn_malus);
                    }
                }
            } else {
                // 捕獲手がbest: captureHistoryを更新
                let captured_pt = pos.piece_on(best_to).piece_type();
                self.history.capture_history.update(best_cont_pc, best_to, captured_pt, bonus);
            }

            // YaneuraOu: 他の捕獲手へのペナルティ(capture best以外の全捕獲手)
            // captureMalus = min(708*depth-148, 2287) - 29*capturesSearched.size()
            let cap_malus = capture_malus(depth, captures_tried.len());
            for &m in captures_tried.iter() {
                if m != best_move {
                    let moved_pc = pos.moved_piece(m);
                    let cont_pc = if m.is_promotion() {
                        moved_pc.promote().unwrap_or(moved_pc)
                    } else {
                        moved_pc
                    };
                    let to = m.to();
                    let captured_pt = pos.piece_on(to).piece_type();
                    // YaneuraOu: captureHistory << -captureMalus * 1431 / 1024
                    self.history.capture_history.update(
                        cont_pc,
                        to,
                        captured_pt,
                        -cap_malus * 1431 / 1024,
                    );
                }
            }

            // YaneuraOu: quiet early refutationペナルティ
            // 条件: prevSq != SQ_NONE && (ss-1)->moveCount == 1 + (ss-1)->ttHit && !pos.captured_piece()
            // 処理: update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq, -captureMalus * 622 / 1024)
            if ply >= 1 {
                let prev_ply = (ply - 1) as usize;
                let prev_move_count = self.stack[prev_ply].move_count;
                let prev_tt_hit = self.stack[prev_ply].tt_hit;
                // YaneuraOu: !pos.captured_piece() = 現在の局面で駒が取られていない
                if prev_move_count == 1 + (prev_tt_hit as i32)
                    && pos.captured_piece() == Piece::NONE
                {
                    if let Some(key) = self.stack[prev_ply].cont_hist_key {
                        let prev_sq = key.to;
                        let prev_piece = pos.piece_on(prev_sq);
                        // YaneuraOu: update_continuation_histories(ss - 1, ...)を呼ぶ
                        // = 過去1-6手分全てに weight と +80 オフセット付きで更新
                        let penalty_base = -cap_malus * 622 / 1024;
                        // YaneuraOu: update_continuation_histories(ss - 1, ...) で (ss - 1)->inCheck を参照
                        let prev_in_check = self.stack[prev_ply].in_check;
                        let prev_max_ply_back = if prev_in_check { 2 } else { 6 };

                        for &(ply_back, weight) in CONTINUATION_HISTORY_WEIGHTS.iter() {
                            if ply_back > prev_max_ply_back {
                                continue;
                            }
                            // ss - 1 からさらに ply_back 手前 = ply - 1 - ply_back
                            let target_ply = ply - 1 - ply_back as i32;
                            if target_ply >= 0 {
                                if let Some(target_key) =
                                    self.stack[target_ply as usize].cont_hist_key
                                {
                                    let in_check_idx = target_key.in_check as usize;
                                    let capture_idx = target_key.capture as usize;
                                    let weighted_penalty = penalty_base * weight / 1024
                                        + if ply_back <= 2 {
                                            CONTINUATION_HISTORY_NEAR_PLY_OFFSET
                                        } else {
                                            0
                                        };
                                    self.history.continuation_history[in_check_idx][capture_idx]
                                        .update(
                                            target_key.piece,
                                            target_key.to,
                                            prev_piece,
                                            prev_sq,
                                            weighted_penalty,
                                        );
                                }
                            }
                        }
                    }
                }
            }

            // TTMoveHistory更新(非PVノードのみ)
            if !pv_node && tt_move.is_some() {
                if best_move == tt_move {
                    self.history.tt_move_history.update(ply as usize, TT_MOVE_HISTORY_BONUS);
                } else {
                    self.history.tt_move_history.update(ply as usize, TT_MOVE_HISTORY_MALUS);
                }
            }
        }
        // =================================================================
        // Prior Countermove Bonus(fail low時の前の手にボーナス)
        // YaneuraOu準拠: yaneuraou-search.cpp:3936-3977
        // =================================================================
        else if ply >= 1 {
            let prev_ply = (ply - 1) as usize;
            if let Some(prev_key) = self.stack[prev_ply].cont_hist_key {
                let prior_capture = prev_key.capture;
                let prev_sq = prev_key.to;

                if !prior_capture {
                    // Prior quiet countermove bonus
                    // YaneuraOu: yaneuraou-search.cpp:3945-3966
                    let parent_stat_score = self.stack[prev_ply].stat_score;
                    let parent_move_count = self.stack[prev_ply].move_count;
                    let parent_in_check = self.stack[prev_ply].in_check;
                    let parent_static_eval = self.stack[prev_ply].static_eval;
                    let static_eval = self.stack[ply as usize].static_eval;

                    // bonusScale計算(YaneuraOu準拠)
                    let mut bonus_scale: i32 = -228;
                    bonus_scale -= parent_stat_score / 104;
                    bonus_scale += (63 * depth).min(508);
                    bonus_scale += 184 * (parent_move_count > 8) as i32;
                    bonus_scale += 143
                        * (!in_check
                            && static_eval != Value::NONE
                            && best_value <= static_eval - Value::new(92))
                            as i32;
                    bonus_scale += 149
                        * (!parent_in_check
                            && parent_static_eval != Value::NONE
                            && best_value <= -parent_static_eval - Value::new(70))
                            as i32;
                    bonus_scale = bonus_scale.max(0);

                    // 値域: bonus_scale ∈ [0, ~913], min(...) ∈ [52, 1365] (depth>=1)
                    // 最大値 1365 * 913 ≈ 1.2M << i32::MAX なのでオーバーフローなし
                    let scaled_bonus = (144 * depth - 92).min(1365) * bonus_scale;

                    // continuation history更新
                    // YaneuraOu: update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq, scaledBonus * 400 / 32768)
                    // 注: prev_sq は cont_hist_key.to(do_move後に設定)なので、
                    //     この時点で prev_piece != NONE が保証される
                    let prev_piece = pos.piece_on(prev_sq);
                    let prev_max_ply_back = if parent_in_check { 2 } else { 6 };
                    let cont_bonus = scaled_bonus * 400 / 32768;

                    for &(ply_back, weight) in CONTINUATION_HISTORY_WEIGHTS.iter() {
                        if ply_back > prev_max_ply_back {
                            continue;
                        }
                        // ss - 1 からさらに ply_back 手前 = ply - 1 - ply_back
                        let target_ply = ply - 1 - ply_back as i32;
                        if target_ply >= 0 {
                            if let Some(target_key) = self.stack[target_ply as usize].cont_hist_key
                            {
                                let in_check_idx = target_key.in_check as usize;
                                let capture_idx = target_key.capture as usize;
                                let weighted_bonus = cont_bonus * weight / 1024
                                    + if ply_back <= 2 {
                                        CONTINUATION_HISTORY_NEAR_PLY_OFFSET
                                    } else {
                                        0
                                    };
                                self.history.continuation_history[in_check_idx][capture_idx]
                                    .update(
                                        target_key.piece,
                                        target_key.to,
                                        prev_piece,
                                        prev_sq,
                                        weighted_bonus,
                                    );
                            }
                        }
                    }

                    // main history更新
                    // YaneuraOu: mainHistory[~us][((ss - 1)->currentMove).raw()] << scaledBonus * 220 / 32768
                    let prev_move = self.stack[prev_ply].current_move;
                    let main_bonus = scaled_bonus * 220 / 32768;
                    // 注: 前の手なので手番は!pos.side_to_move()
                    let opponent = !pos.side_to_move();
                    self.history.main_history.update(opponent, prev_move, main_bonus);

                    // pawn history更新(歩以外かつ成りでない場合)
                    // YaneuraOu: if (type_of(pos.piece_on(prevSq)) != PAWN && ((ss - 1)->currentMove).type_of() != PROMOTION)
                    if prev_piece.piece_type() != PieceType::Pawn && !prev_move.is_promotion() {
                        let pawn_key_idx = pos.pawn_history_index();
                        let pawn_bonus = scaled_bonus * 1164 / 32768;
                        self.history.pawn_history.update(
                            pawn_key_idx,
                            prev_piece,
                            prev_sq,
                            pawn_bonus,
                        );
                    }
                } else {
                    // Prior capture countermove bonus
                    // YaneuraOu: yaneuraou-search.cpp:3972-3977
                    // 注: prev_sq は cont_hist_key.to(do_move後に設定)なので prev_piece は有効
                    let prev_piece = pos.piece_on(prev_sq);
                    let captured_piece = pos.captured_piece();
                    // YaneuraOu: assert(capturedPiece != NO_PIECE)
                    debug_assert!(
                        captured_piece != Piece::NONE,
                        "prior_capture is true but captured_piece is NONE"
                    );
                    if captured_piece != Piece::NONE {
                        self.history.capture_history.update(
                            prev_piece,
                            prev_sq,
                            captured_piece.piece_type(),
                            PRIOR_CAPTURE_COUNTERMOVE_BONUS,
                        );
                    }
                }
            }
        }

        // CorrectionHistoryの更新(YaneuraOu準拠)
        if !in_check && best_move.is_some() && !pos.is_capture(best_move) {
            let static_eval = self.stack[ply as usize].static_eval;
            if static_eval != Value::NONE
                && ((best_value < static_eval && best_value < beta) || best_value > static_eval)
            {
                let bonus = ((best_value.raw() - static_eval.raw()) * depth / 8)
                    .clamp(-CORRECTION_HISTORY_LIMIT / 4, CORRECTION_HISTORY_LIMIT / 4);
                self.update_correction_history(pos, ply, bonus);
            }
        }

        // =================================================================
        // 置換表更新
        // =================================================================
        // excludedMoveがある場合は置換表に書き込まない(YaneuraOu準拠)
        // 同一局面で異なるexcludedMoveを持つ局面が同じhashkeyを持つため
        if excluded_move.is_none() {
            let bound = if best_value >= beta {
                Bound::Lower
            } else if pv_node && best_move.is_some() {
                Bound::Exact
            } else {
                Bound::Upper
            };

            tt_ctx.result.write(
                tt_ctx.key,
                value_to_tt(best_value, ply),
                pv_node,
                bound,
                depth,
                best_move,
                eval_ctx.unadjusted_static_eval,
                self.tt.generation(),
            );
        }

        best_value
    }

    /// 静止探索
    fn qsearch<const NT: u8>(
        &mut self,
        pos: &mut Position,
        depth: Depth,
        alpha: Value,
        beta: Value,
        ply: i32,
        limits: &LimitsType,
        time_manager: &mut TimeManagement,
    ) -> Value {
        let pv_node = NT == NodeType::PV as u8;
        let in_check = pos.in_check();

        if ply >= MAX_PLY {
            return if in_check {
                Value::ZERO
            } else {
                self.nnue_evaluate(pos)
            };
        }

        if pv_node && self.sel_depth < ply + 1 {
            self.sel_depth = ply + 1;
        }

        if self.check_abort(limits, time_manager) {
            return Value::ZERO;
        }

        let rep_state = pos.repetition_state(ply);
        if rep_state.is_repetition() || rep_state.is_superior_inferior() {
            let v = draw_value(rep_state, pos.side_to_move());
            if v != Value::NONE {
                if v == Value::DRAW {
                    let jittered = Value::new(v.raw() + draw_jitter(self.nodes));
                    return value_from_tt(jittered, ply);
                }
                return value_from_tt(v, ply);
            }
        }

        // 引き分け手数ルール(YaneuraOu準拠、MaxMovesToDrawオプション)
        if self.max_moves_to_draw > 0 && pos.game_ply() > self.max_moves_to_draw {
            return Value::new(Value::DRAW.raw() + draw_jitter(self.nodes));
        }

        let key = pos.key();
        let tt_result = self.tt.probe(key, pos);
        let tt_hit = tt_result.found;
        let tt_data = tt_result.data;
        let pv_hit = tt_hit && tt_data.is_pv;
        self.stack[ply as usize].tt_hit = tt_hit;
        self.stack[ply as usize].tt_pv = pv_hit;
        let mut tt_move = if tt_hit { tt_data.mv } else { Move::NONE };
        let tt_value = if tt_hit {
            value_from_tt(tt_data.value, ply)
        } else {
            Value::NONE
        };

        if !pv_node
            && tt_hit
            && tt_data.depth >= DEPTH_QS
            && tt_value != Value::NONE
            && tt_data.bound.can_cutoff(tt_value, beta)
        {
            return tt_value;
        }

        let mut best_move = Move::NONE;

        let correction_value = self.correction_value(pos, ply);
        let mut unadjusted_static_eval = Value::NONE;
        let mut static_eval = if in_check {
            Value::NONE
        } else if tt_hit && tt_data.eval != Value::NONE {
            unadjusted_static_eval = tt_data.eval;
            unadjusted_static_eval
        } else {
            // 置換表に無いときだけ簡易1手詰め判定を行う
            if !tt_hit {
                let mate_move = pos.mate_1ply();
                if mate_move.is_some() {
                    return Value::mate_in(ply + 1);
                }
            }
            unadjusted_static_eval = self.nnue_evaluate(pos);
            unadjusted_static_eval
        };

        if !in_check && unadjusted_static_eval != Value::NONE {
            static_eval = to_corrected_static_eval(unadjusted_static_eval, correction_value);
            // パス権評価を動的に追加(TTには保存されないので手数依存でもOK)
            static_eval += evaluate_pass_rights(pos, pos.game_ply() as u16);
        }

        self.stack[ply as usize].static_eval = static_eval;

        let mut alpha = alpha;
        let mut best_value = if in_check {
            Value::mated_in(ply)
        } else {
            static_eval
        };

        if !in_check && tt_hit && tt_value != Value::NONE && !tt_value.is_mate_score() {
            let improves = (tt_value > best_value && tt_data.bound == Bound::Lower)
                || (tt_value < best_value && tt_data.bound == Bound::Upper);
            if improves {
                best_value = tt_value;
                static_eval = tt_value;
                self.stack[ply as usize].static_eval = static_eval;
            }
        }

        if !in_check && best_value >= beta {
            let mut v = best_value;
            if !v.is_mate_score() {
                v = Value::new((v.raw() + beta.raw()) / 2);
            }
            if !tt_hit {
                // YaneuraOu: pvHitを使用
                tt_result.write(
                    key,
                    value_to_tt(v, ply),
                    pv_hit,
                    Bound::Lower,
                    DEPTH_UNSEARCHED,
                    Move::NONE,
                    unadjusted_static_eval,
                    self.tt.generation(),
                );
            }
            return v;
        }

        if !in_check && best_value > alpha {
            alpha = best_value;
        }

        let futility_base = if in_check {
            Value::NONE
        } else {
            static_eval + Value::new(352)
        };

        if depth <= DEPTH_QS
            && tt_move.is_some()
            && ((!pos.capture_stage(tt_move) && !pos.gives_check(tt_move)) || depth < -16)
        {
            tt_move = Move::NONE;
        }

        let prev_move = if ply >= 1 {
            self.stack[(ply - 1) as usize].current_move
        } else {
            Move::NONE
        };

        let ordered_moves = {
            let cont_tables = self.cont_history_tables(ply);
            let mut buf_moves = OrderedMovesBuffer::new();

            {
                let mp = if in_check {
                    MovePicker::new_evasions(
                        pos,
                        tt_move,
                        &self.history.main_history,
                        &self.history.low_ply_history,
                        &self.history.capture_history,
                        cont_tables,
                        &self.history.pawn_history,
                        ply,
                        self.generate_all_legal_moves,
                    )
                } else {
                    MovePicker::new(
                        pos,
                        tt_move,
                        DEPTH_QS,
                        &self.history.main_history,
                        &self.history.low_ply_history,
                        &self.history.capture_history,
                        cont_tables,
                        &self.history.pawn_history,
                        ply,
                        self.generate_all_legal_moves,
                    )
                };

                for mv in mp {
                    buf_moves.push(mv);
                }
            }

            if !in_check && depth == DEPTH_QS {
                let mut buf = crate::movegen::ExtMoveBuffer::new();
                let gen_type = if self.generate_all_legal_moves {
                    crate::movegen::GenType::QuietChecksAll
                } else {
                    crate::movegen::GenType::QuietChecks
                };
                crate::movegen::generate_with_type(pos, gen_type, &mut buf, None);
                for ext in buf.iter() {
                    if buf_moves.contains(&ext.mv) {
                        continue;
                    }
                    buf_moves.push(ext.mv);
                }
            }

            if !in_check && depth <= -5 && ply >= 1 && prev_move.is_normal() {
                let mut buf = crate::movegen::ExtMoveBuffer::new();
                let rec_sq = prev_move.to();
                let gen_type = if self.generate_all_legal_moves {
                    crate::movegen::GenType::RecapturesAll
                } else {
                    crate::movegen::GenType::Recaptures
                };
                crate::movegen::generate_with_type(pos, gen_type, &mut buf, Some(rec_sq));
                buf_moves.clear();
                for ext in buf.iter() {
                    buf_moves.push(ext.mv);
                }
            }

            buf_moves
        };

        let mut move_count = 0;

        for mv in ordered_moves.iter() {
            // 静止探索では PASS は対象外(TT手として来る可能性があるため明示的にスキップ)
            if mv.is_pass() {
                continue;
            }

            if !pos.is_legal(mv) {
                continue;
            }

            let gives_check = pos.gives_check(mv);
            let capture = pos.capture_stage(mv);

            if !in_check && depth <= DEPTH_QS && !capture && !gives_check {
                continue;
            }

            if !in_check && capture && !pos.see_ge(mv, Value::ZERO) {
                continue;
            }

            move_count += 1;

            if !best_value.is_loss() {
                if !gives_check
                    && (!prev_move.is_normal() || mv.to() != prev_move.to())
                    && futility_base != Value::NONE
                {
                    if move_count > 2 {
                        continue;
                    }

                    let futility_value =
                        futility_base + Value::new(piece_value(pos.piece_on(mv.to())));

                    if futility_value <= alpha {
                        best_value = best_value.max(futility_value);
                        continue;
                    }

                    if !pos.see_ge(mv, alpha - futility_base) {
                        best_value = best_value.min(alpha.min(futility_base));
                        continue;
                    }
                }
                if !capture {
                    let mut cont_score = 0;

                    // ss-1の参照(ContinuationHistory直結)
                    cont_score +=
                        self.cont_history_ref(ply, 1).get(mv.moved_piece_after(), mv.to()) as i32;

                    let pawn_idx = pos.pawn_history_index();
                    cont_score +=
                        self.history.pawn_history.get(pawn_idx, pos.moved_piece(mv), mv.to())
                            as i32;
                    if cont_score <= 5868 {
                        continue;
                    }
                }

                if !pos.see_ge(mv, Value::new(-74)) {
                    continue;
                }
            }

            self.stack[ply as usize].current_move = mv;

            let dirty_piece = pos.do_move_with_prefetch(mv, gives_check, self.tt.as_ref());
            self.nnue_push(dirty_piece);
            self.nodes += 1;

            // PASS は to()/moved_piece_after() が未定義のため、null move と同様に扱う
            if mv.is_pass() {
                self.clear_cont_history_for_null(ply);
            } else {
                let cont_hist_pc = mv.moved_piece_after();
                let cont_hist_to = mv.to();
                self.set_cont_history_for_move(ply, in_check, capture, cont_hist_pc, cont_hist_to);
            }

            let value =
                -self.qsearch::<NT>(pos, depth - 1, -beta, -alpha, ply + 1, limits, time_manager);

            self.nnue_pop();
            pos.undo_move(mv);

            if self.abort {
                return Value::ZERO;
            }

            if value > best_value {
                best_value = value;
                best_move = mv;

                if value > alpha {
                    alpha = value;

                    if value >= beta {
                        break;
                    }
                }
            }
        }

        if in_check && move_count == 0 {
            return Value::mated_in(ply);
        }

        if !best_value.is_mate_score() && best_value > beta {
            best_value = Value::new((best_value.raw() + beta.raw()) / 2);
        }

        let bound = if best_value >= beta {
            Bound::Lower
        } else if pv_node && best_move.is_some() {
            Bound::Exact
        } else {
            Bound::Upper
        };

        // YaneuraOu: pvHitを使用
        tt_result.write(
            key,
            value_to_tt(best_value, ply),
            pv_hit,
            bound,
            DEPTH_QS,
            best_move,
            unadjusted_static_eval,
            self.tt.generation(),
        );

        best_value
    }
}

// SAFETY: SearchWorkerは単一スレッドで使用される前提。
// StackArray内の各Stackが持つ `cont_history_ptr: NonNull<PieceToHistory>` は
// `self.history.continuation_history` 内のテーブルへの参照である。
// SearchWorkerがスレッド間でmoveされても、history フィールドも一緒にmoveされるため、
// ポインタの参照先は常に有効であり、データ競合も発生しない。
unsafe impl Send for SearchWorker {}

// =============================================================================
// テスト
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_reduction_values() {
        // reduction(true, 10, 5) などが正の値を返すことを確認
        // LazyLockにより初回アクセス時に自動初期化される
        let root_delta = 64;
        let delta = 32;
        assert!(reduction(true, 10, 5, delta, root_delta) / 1024 >= 0);
        assert!(
            reduction(false, 10, 5, delta, root_delta) / 1024
                >= reduction(true, 10, 5, delta, root_delta) / 1024
        );
    }

    #[test]
    fn test_reduction_bounds() {
        // 境界値テスト
        let root_delta = 64;
        let delta = 32;
        assert_eq!(reduction(true, 0, 0, delta, root_delta), 0); // depth=0, mc=0 は計算外
        assert!(reduction(true, 63, 63, delta, root_delta) / 1024 < 64);
        assert!(reduction(false, 63, 63, delta, root_delta) / 1024 < 64);
    }

    /// depth/move_countが大きい場合にreductionが正の値を返すことを確認
    #[test]
    fn test_reduction_returns_nonzero_for_large_values() {
        let root_delta = 64;
        let delta = 32;
        // 深い探索で多くの手を試した場合、reductionは正の値であるべき
        let r = reduction(false, 10, 10, delta, root_delta) / 1024;
        assert!(
            r > 0,
            "reduction should return positive value for depth=10, move_count=10, got {r}"
        );

        // improving=trueの場合は若干小さい値になる
        let r_imp = reduction(true, 10, 10, delta, root_delta) / 1024;
        assert!(r >= r_imp, "non-improving should have >= reduction than improving");
    }

    /// 境界ケース: depth=1, move_count=1でもreduction関数が動作することを確認
    #[test]
    fn test_reduction_small_values() {
        let root_delta = 64;
        let delta = 32;
        // 小さな値でもpanicしないことを確認
        let r = reduction(true, 1, 1, delta, root_delta) / 1024;
        assert!(r >= 0, "reduction should not be negative");
    }

    #[test]
    fn test_reduction_extremes_no_overflow() {
        // 最大depth/mcでもオーバーフローせずに値が得られることを確認
        let delta = 0;
        let root_delta = 1;
        let r = reduction(false, 63, 63, delta, root_delta);
        assert!(
            (0..i32::MAX / 2).contains(&r),
            "reduction extreme should be in safe range, got {r}"
        );
    }

    #[test]
    fn test_reduction_zero_root_delta_clamped() {
        // root_delta=0 を渡しても内部で1にクランプされることを確認
        let r = reduction(false, 10, 10, 0, 0) / 1024;
        assert!(r >= 0, "reduction should clamp root_delta to >=1 even when 0 is passed");
    }

    #[test]
    fn test_sentinel_initialization() {
        use std::sync::Arc;

        // SearchWorker作成時にsentinelが正しく初期化されることを確認
        let tt = Arc::new(TranspositionTable::new(16));
        let eval_hash = Arc::new(EvalHash::new(1));
        let worker = SearchWorker::new(tt, eval_hash, 0, 0);

        // sentinelポインタがdanglingではなく、実際のテーブルを指していることを確認
        let sentinel = worker.cont_history_sentinel;
        // NonNullはnullにならないことが保証されているので、
        // 代わりにsafeにderefできることを確認(ポインタが有効なメモリを指していること)
        let sentinel_ref = unsafe { sentinel.as_ref() };
        // PieceToHistoryテーブルはゼロ初期化されているはず
        assert_eq!(
            sentinel_ref.get(crate::types::Piece::B_PAWN, crate::types::Square::SQ_11),
            0,
            "sentinel table should be zero-initialized"
        );

        // 全てのスタックエントリがsentinelで初期化されていることを確認
        for (i, stack) in worker.stack.iter().enumerate() {
            assert_eq!(
                stack.cont_history_ptr, sentinel,
                "stack[{i}].cont_history_ptr should be initialized to sentinel"
            );
        }
    }

    #[test]
    fn test_cont_history_ptr_returns_sentinel_for_negative_offset() {
        use std::sync::Arc;

        let tt = Arc::new(TranspositionTable::new(16));
        let eval_hash = Arc::new(EvalHash::new(1));
        let worker = SearchWorker::new(tt, eval_hash, 0, 0);

        // ply < back の場合はsentinelを返すことを確認
        let ptr = worker.cont_history_ptr(0, 1);
        assert_eq!(ptr, worker.cont_history_sentinel);

        let ptr = worker.cont_history_ptr(3, 5);
        assert_eq!(ptr, worker.cont_history_sentinel);
    }
}