rshogi-core 0.2.3

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
//! NNUEネットワーク全体の構造と評価関数
//!
//! 以下のアーキテクチャをサポート:
//! - **HalfKP**: classic NNUE(水匠/tanuki互換)
//! - **HalfKA**: nnue-pytorch互換(Non-mirror)
//! - **HalfKA_hm^**: nnue-pytorch互換(Half-Mirror + Factorization)
//!
//! # 階層構造(4バリアント)
//!
//! ```text
//! NNUENetwork
//! ├── HalfKA(HalfKANetwork)   // L256/L512/L1024 を内包
//! ├── HalfKA_hm(HalfKA_hmNetwork)   // L256/L512/L1024 を内包
//! ├── HalfKP(HalfKPNetwork)   // L256/L512 を内包
//! └── LayerStacks(Box<NetworkLayerStacks>)
//! ```
//!
//! **「Accumulator は L1 だけで決まる」** を活用し、L2/L3/活性化の追加時に
//! このファイルの変更は最小限で済む。

use super::accumulator_layer_stacks::{AccumulatorCacheLayerStacks, AccumulatorStackLayerStacks};
use super::accumulator_stack_variant::AccumulatorStackVariant;
use super::activation::detect_activation_from_arch;
use super::bona_piece::BonaPiece;
use super::bona_piece_halfka_hm::FE_OLD_END;
use super::constants::{MAX_ARCH_LEN, NNUE_VERSION, NNUE_VERSION_HALFKA};
use super::halfka::{HalfKANetwork, HalfKAStack};
use super::halfka_hm::{HalfKA_hmNetwork, HalfKA_hmStack};
use super::halfkp::{HalfKPNetwork, HalfKPStack};
use super::network_layer_stacks::NetworkLayerStacks;
use super::spec::{Activation, FeatureSet};
use super::stats::{count_already_computed, count_refresh, count_update};
use crate::eval::material;
use crate::position::Position;
use crate::types::{Color, PieceType, Value};
use std::cell::Cell;
use std::fs::File;
use std::io::{self, BufReader, Cursor, Read, Seek, SeekFrom};
use std::path::Path;
use std::sync::atomic::{AtomicI32, AtomicPtr, Ordering};
use std::sync::{Arc, LazyLock, RwLock};

/// グローバルなNNUEネットワーク(HalfKP/HalfKA/HalfKA_hm^)
static NETWORK: LazyLock<RwLock<Option<Arc<NNUENetwork>>>> = LazyLock::new(|| RwLock::new(None));

/// FV_SCALE のグローバルオーバーライド設定
///
/// 0 = 自動判定(Network 構造体の fv_scale を使用)
/// 1以上 = 指定値でオーバーライド
///
/// YaneuraOuと同様にエンジンオプションで設定可能。
/// 評価関数によって異なる値が必要な場合に使用。
static FV_SCALE_OVERRIDE: AtomicI32 = AtomicI32::new(0);

/// LayerStacks の bucket 選択モード
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LayerStackBucketMode {
    /// 従来方式: 両玉の相対段で 9 バケットを選択
    KingRank9 = 0,
    /// 手数方式: game_ply を固定境界で 9 バケットに分割
    Ply9 = 1,
    /// 進行度方式: logistic regression で 8 バケットへ分割(bucket8は未使用)
    Progress8 = 2,
    /// 進行度方式(gikou-lite): logistic regression(34特徴) で 8 バケットへ分割(bucket8は未使用)
    Progress8Gikou = 3,
    /// 進行度方式(KP-absolute): YaneuraOu 互換 progress.bin で 8 バケットへ分割(bucket8は未使用)
    Progress8KPAbs = 4,
}

impl LayerStackBucketMode {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::KingRank9 => "kingrank9",
            Self::Ply9 => "ply9",
            Self::Progress8 => "progress8",
            Self::Progress8Gikou => "progress8gikou",
            Self::Progress8KPAbs => "progress8kpabs",
        }
    }
}

/// LayerStacks の ply9 バケットの既定境界。
///
/// bucket0: <=30, bucket1: <=44, ..., bucket7: <=138, bucket8: >=139
pub const LAYER_STACK_PLY9_DEFAULT_BOUNDS: [u16; 8] = [30, 44, 58, 72, 86, 100, 116, 138];

/// progress8 で使用する特徴量数
pub const SHOGI_PROGRESS8_NUM_FEATURES: usize = 6;

/// progress8 で使用するバケット数
pub const SHOGI_PROGRESS8_NUM_BUCKETS: usize = 8;

/// progress8 coeff_v1 の特徴量順序
pub const SHOGI_PROGRESS8_FEATURE_ORDER: [&str; SHOGI_PROGRESS8_NUM_FEATURES] = [
    "x_board_non_king",
    "x_hand_total",
    "x_major_board",
    "x_promoted_board",
    "x_stm_king_rank_rel",
    "x_ntm_king_rank_rel",
];

/// progress8gikou で使用する特徴量数
pub const SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES: usize = 34;

/// progress8kpabs で使用する重み数(81 king squares x FE_OLD_END BonaPiece)
pub const SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS: usize = 81 * FE_OLD_END;

/// sigmoid(x)*8 = k となる x の閾値 (k=1..7)。
/// x = ln(k / (8-k)) で事前計算。
/// sum との比較のみで bucket index を決定でき、exp() が不要になる。
const PROGRESS_BUCKET_THRESHOLDS: [f32; 7] = [
    -1.945_910_1, // k=1: ln(1/7)
    -1.098_612_3, // k=2: ln(1/3)
    -0.510_825_6, // k=3: ln(3/5)
    0.0,          // k=4: ln(1)
    0.510_825_6,  // k=5: ln(5/3)
    1.098_612_3,  // k=6: ln(3)
    1.945_910_1,  // k=7: ln(7)
];

// progress8kpabs の差分計算済み bucket index キャッシュ(スレッドローカル)
//
// `update_and_evaluate_layer_stacks` で差分計算した結果を格納し、
// `compute_layer_stack_progress8kpabs_bucket_index` 内で消費する。
// 一度消費されると None にリセットされる(1回限り)。
thread_local! {
    static CACHED_PROGRESS_BUCKET: Cell<Option<usize>> = const { Cell::new(None) };
}

/// progress8gikou coeff_v2 の特徴量順序
pub const SHOGI_PROGRESS_GIKOU_LITE_FEATURE_ORDER: [&str; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES] = [
    "x_board_non_king",
    "x_hand_total",
    "x_major_board",
    "x_promoted_board",
    "x_stm_king_rank_rel",
    "x_ntm_king_rank_rel",
    "x_stm_all_to_own_king_d1",
    "x_stm_all_to_own_king_d2",
    "x_stm_all_to_own_king_d3p",
    "x_stm_all_to_opp_king_d1",
    "x_stm_all_to_opp_king_d2",
    "x_stm_all_to_opp_king_d3p",
    "x_ntm_all_to_own_king_d1",
    "x_ntm_all_to_own_king_d2",
    "x_ntm_all_to_own_king_d3p",
    "x_ntm_all_to_opp_king_d1",
    "x_ntm_all_to_opp_king_d2",
    "x_ntm_all_to_opp_king_d3p",
    "x_stm_major_to_own_king_d1",
    "x_stm_major_to_own_king_d2",
    "x_stm_major_to_own_king_d3p",
    "x_stm_major_to_opp_king_d1",
    "x_stm_major_to_opp_king_d2",
    "x_stm_major_to_opp_king_d3p",
    "x_ntm_major_to_own_king_d1",
    "x_ntm_major_to_own_king_d2",
    "x_ntm_major_to_own_king_d3p",
    "x_ntm_major_to_opp_king_d1",
    "x_ntm_major_to_opp_king_d2",
    "x_ntm_major_to_opp_king_d3p",
    "x_stm_hand_total",
    "x_ntm_hand_total",
    "x_stm_hand_major",
    "x_ntm_hand_major",
];

/// progress8 (coeff_v1) の係数。
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LayerStackProgressCoeff {
    pub mean: [f32; SHOGI_PROGRESS8_NUM_FEATURES],
    pub std: [f32; SHOGI_PROGRESS8_NUM_FEATURES],
    pub weights: [f32; SHOGI_PROGRESS8_NUM_FEATURES],
    pub bias: f32,
    pub z_clip: [f32; 2],
}

/// progress8gikou (coeff_v2) の係数。
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LayerStackProgressCoeffGikouLite {
    pub mean: [f32; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
    pub std: [f32; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
    pub weights: [f32; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
    pub bias: f32,
    pub z_clip: [f32; 2],
}

impl LayerStackProgressCoeffGikouLite {
    pub const fn new(
        mean: [f32; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
        std: [f32; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
        weights: [f32; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
        bias: f32,
        z_clip: [f32; 2],
    ) -> Self {
        Self {
            mean,
            std,
            weights,
            bias,
            z_clip,
        }
    }
}

impl LayerStackProgressCoeff {
    pub const fn new(
        mean: [f32; SHOGI_PROGRESS8_NUM_FEATURES],
        std: [f32; SHOGI_PROGRESS8_NUM_FEATURES],
        weights: [f32; SHOGI_PROGRESS8_NUM_FEATURES],
        bias: f32,
        z_clip: [f32; 2],
    ) -> Self {
        Self {
            mean,
            std,
            weights,
            bias,
            z_clip,
        }
    }
}

impl Default for LayerStackProgressCoeff {
    fn default() -> Self {
        // docs/coeff/progress_coeff_v1.default.json と同一の既定値。
        Self {
            mean: [30.12, 8.45, 2.18, 1.63, 6.71, 6.24],
            std: [3.77, 4.02, 0.66, 1.40, 1.31, 1.27],
            weights: [-0.81, 0.56, -0.32, 0.48, 0.11, -0.09],
            bias: -0.15,
            z_clip: [-8.0, 8.0],
        }
    }
}

impl Default for LayerStackProgressCoeffGikouLite {
    fn default() -> Self {
        Self {
            mean: [0.0; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
            std: [1.0; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
            weights: [0.0; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES],
            bias: 0.0,
            z_clip: [-8.0, 8.0],
        }
    }
}

/// LayerStacks bucket mode のグローバル設定
static LAYER_STACK_BUCKET_MODE: AtomicI32 = AtomicI32::new(LayerStackBucketMode::KingRank9 as i32);

/// LayerStacks ply9 境界のグローバル設定
static LAYER_STACK_PLY_BOUNDS: [AtomicI32; 8] = [
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[0] as i32),
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[1] as i32),
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[2] as i32),
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[3] as i32),
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[4] as i32),
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[5] as i32),
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[6] as i32),
    AtomicI32::new(LAYER_STACK_PLY9_DEFAULT_BOUNDS[7] as i32),
];

/// LayerStacks progress8 係数のグローバル設定
static LAYER_STACK_PROGRESS_COEFF: LazyLock<RwLock<LayerStackProgressCoeff>> =
    LazyLock::new(|| RwLock::new(LayerStackProgressCoeff::default()));

/// LayerStacks progress8gikou 係数のグローバル設定
static LAYER_STACK_PROGRESS_COEFF_GIKOU_LITE: LazyLock<RwLock<LayerStackProgressCoeffGikouLite>> =
    LazyLock::new(|| RwLock::new(LayerStackProgressCoeffGikouLite::default()));

/// progress8kpabs 重みのデフォルト(未設定時は全ゼロ)
static LAYER_STACK_PROGRESS_KP_ABS_ZERO_WEIGHTS: [f32; SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS] =
    [0.0; SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS];

/// progress8kpabs 重みのグローバル設定
///
/// `progress.bin` 読み込み時に Box を leak してポインタだけ差し替える。
/// 設定は起動時の一度を想定し、評価ホットパスでは lock を取らない。
static LAYER_STACK_PROGRESS_KP_ABS_PTR: AtomicPtr<f32> = AtomicPtr::new(std::ptr::null_mut());

/// FV_SCALE オーバーライドを取得
///
/// 戻り値:
/// - `Some(value)`: オーバーライド値が設定されている
/// - `None`: 自動判定を使用(Network の fv_scale を使用)
pub fn get_fv_scale_override() -> Option<i32> {
    let value = FV_SCALE_OVERRIDE.load(Ordering::Relaxed);
    if value > 0 { Some(value) } else { None }
}

/// FV_SCALE オーバーライドを設定
///
/// 引数:
/// - `value`: 設定値(0 = 自動判定、1以上 = オーバーライド)
pub fn set_fv_scale_override(value: i32) {
    FV_SCALE_OVERRIDE.store(value.max(0), Ordering::Relaxed);
}

/// LayerStacks bucket mode を取得
pub fn get_layer_stack_bucket_mode() -> LayerStackBucketMode {
    match LAYER_STACK_BUCKET_MODE.load(Ordering::Relaxed) {
        1 => LayerStackBucketMode::Ply9,
        2 => LayerStackBucketMode::Progress8,
        3 => LayerStackBucketMode::Progress8Gikou,
        4 => LayerStackBucketMode::Progress8KPAbs,
        _ => LayerStackBucketMode::KingRank9,
    }
}

/// LayerStacks bucket mode を設定
pub fn set_layer_stack_bucket_mode(mode: LayerStackBucketMode) {
    LAYER_STACK_BUCKET_MODE.store(mode as i32, Ordering::Relaxed);
}

/// LayerStacks ply9 境界を取得
pub fn get_layer_stack_ply_bounds() -> [u16; 8] {
    std::array::from_fn(|i| {
        let value = LAYER_STACK_PLY_BOUNDS[i].load(Ordering::Relaxed);
        if value < 0 { 0 } else { value as u16 }
    })
}

/// LayerStacks ply9 境界を設定
pub fn set_layer_stack_ply_bounds(bounds: [u16; 8]) {
    for (slot, &value) in LAYER_STACK_PLY_BOUNDS.iter().zip(bounds.iter()) {
        slot.store(i32::from(value), Ordering::Relaxed);
    }
}

/// LayerStacks progress8 係数を取得
pub fn get_layer_stack_progress_coeff() -> LayerStackProgressCoeff {
    match LAYER_STACK_PROGRESS_COEFF.read() {
        Ok(guard) => *guard,
        Err(poisoned) => *poisoned.into_inner(),
    }
}

/// LayerStacks progress8 係数を設定
pub fn set_layer_stack_progress_coeff(coeff: LayerStackProgressCoeff) {
    match LAYER_STACK_PROGRESS_COEFF.write() {
        Ok(mut guard) => *guard = coeff,
        Err(poisoned) => *poisoned.into_inner() = coeff,
    }
}

/// LayerStacks progress8gikou 係数を取得
pub fn get_layer_stack_progress_coeff_gikou_lite() -> LayerStackProgressCoeffGikouLite {
    match LAYER_STACK_PROGRESS_COEFF_GIKOU_LITE.read() {
        Ok(guard) => *guard,
        Err(poisoned) => *poisoned.into_inner(),
    }
}

/// LayerStacks progress8gikou 係数を設定
pub fn set_layer_stack_progress_coeff_gikou_lite(coeff: LayerStackProgressCoeffGikouLite) {
    match LAYER_STACK_PROGRESS_COEFF_GIKOU_LITE.write() {
        Ok(mut guard) => *guard = coeff,
        Err(poisoned) => *poisoned.into_inner() = coeff,
    }
}

/// LayerStacks progress8kpabs 重みを取得
pub fn get_layer_stack_progress_kpabs_weights() -> &'static [f32] {
    let ptr = LAYER_STACK_PROGRESS_KP_ABS_PTR.load(Ordering::Relaxed);
    if ptr.is_null() {
        &LAYER_STACK_PROGRESS_KP_ABS_ZERO_WEIGHTS
    } else {
        // SAFETY: `set_layer_stack_progress_kpabs_weights()` で leaked Box の先頭ポインタを保存している。
        unsafe { std::slice::from_raw_parts(ptr.cast_const(), SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS) }
    }
}

/// LayerStacks progress8kpabs 重みを設定
pub fn set_layer_stack_progress_kpabs_weights(weights: Box<[f32]>) -> Result<(), String> {
    if weights.len() != SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS {
        return Err(format!(
            "progress8kpabs weights length mismatch: got {}, expected {}",
            weights.len(),
            SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS
        ));
    }

    let leaked = Box::leak(weights);
    let old_ptr = LAYER_STACK_PROGRESS_KP_ABS_PTR.swap(leaked.as_mut_ptr(), Ordering::Relaxed);
    // SAFETY: old_ptr は過去の同関数で Box::leak したスライスの先頭ポインタ(または null)。
    // USI プロトコルにより設定変更中は評価パスが実行されないため、参照者は存在しない。
    if !old_ptr.is_null() {
        unsafe {
            drop(Box::from_raw(std::ptr::slice_from_raw_parts_mut(
                old_ptr,
                SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS,
            )));
        }
    }
    Ok(())
}

/// LayerStacks progress8kpabs 重みを既定値(全ゼロ)へ戻す
pub fn reset_layer_stack_progress_kpabs_weights() {
    let old_ptr = LAYER_STACK_PROGRESS_KP_ABS_PTR.swap(std::ptr::null_mut(), Ordering::Relaxed);
    // SAFETY: 同上。old_ptr は Box::leak 由来のポインタ(または null)。
    if !old_ptr.is_null() {
        unsafe {
            drop(Box::from_raw(std::ptr::slice_from_raw_parts_mut(
                old_ptr,
                SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS,
            )));
        }
    }
}

// =============================================================================
// NNUENetwork - アーキテクチャを抽象化するenum
// =============================================================================

/// NNUEネットワーク(4バリアント階層構造)
///
/// **「Accumulator は L1 だけで決まる」** を活用した設計:
/// - HalfKA(HalfKANetwork): L256/L512/L1024 を内包
/// - HalfKA_hm(HalfKA_hmNetwork): L256/L512/L1024 を内包
/// - HalfKP(HalfKPNetwork): L256/L512 を内包
/// - LayerStacks: 1536次元 + 9バケット
///
/// L2/L3/活性化の追加時、このenumの変更は不要。
/// 詳細は `halfka/` や `halfkp/` のモジュールで管理される。
pub enum NNUENetwork {
    /// HalfKA 特徴量セット(L256/L512/L1024)
    HalfKA(HalfKANetwork),
    /// HalfKA_hm 特徴量セット(L256/L512/L1024)
    #[allow(non_camel_case_types)]
    HalfKA_hm(HalfKA_hmNetwork),
    /// HalfKP 特徴量セット(L256/L512)
    HalfKP(HalfKPNetwork),
    /// LayerStacks(1536次元 + 9バケット)
    LayerStacks(Box<NetworkLayerStacks>),
}

impl NNUENetwork {
    /// HalfKP でサポートされているアーキテクチャ一覧
    pub fn supported_halfkp_specs() -> Vec<super::spec::ArchitectureSpec> {
        HalfKPNetwork::supported_specs()
    }

    /// HalfKA_hm でサポートされているアーキテクチャ一覧
    pub fn supported_halfka_hm_specs() -> Vec<super::spec::ArchitectureSpec> {
        HalfKA_hmNetwork::supported_specs()
    }

    /// HalfKA でサポートされているアーキテクチャ一覧
    pub fn supported_halfka_specs() -> Vec<super::spec::ArchitectureSpec> {
        HalfKANetwork::supported_specs()
    }

    /// ファイルから読み込み(バージョン自動判別)
    pub fn load<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        let file = File::open(path)?;
        let mut reader = BufReader::new(file);
        Self::read(&mut reader)
    }

    /// リーダーから読み込み(ファイルサイズ優先の自動判別)
    ///
    /// ファイルサイズからアーキテクチャを一意に検出し、適切なバリアントに委譲する。
    /// ヘッダーの description 文字列は活性化関数の検出にのみ使用する。
    pub fn read<R: Read + Seek>(reader: &mut R) -> io::Result<Self> {
        // 1. ファイルサイズを取得
        let file_size = reader.seek(SeekFrom::End(0))?;
        reader.seek(SeekFrom::Start(0))?;

        // 2. VERSION を読む
        let mut buf4 = [0u8; 4];
        reader.read_exact(&mut buf4)?;
        let version = u32::from_le_bytes(buf4);

        match version {
            NNUE_VERSION | NNUE_VERSION_HALFKA => {
                // 3. hash と arch_len を読む
                reader.read_exact(&mut buf4)?; // ネットワークハッシュ
                reader.read_exact(&mut buf4)?; // arch_len
                let arch_len = u32::from_le_bytes(buf4) as usize;
                if arch_len == 0 || arch_len > MAX_ARCH_LEN {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!("Invalid arch string length: {arch_len}"),
                    ));
                }

                // アーキテクチャ文字列を読む(活性化関数・FeatureSet 検出用)
                let mut arch = vec![0u8; arch_len];
                reader.read_exact(&mut arch)?;
                let arch_str = String::from_utf8_lossy(&arch);

                // 活性化関数を検出
                let activation_str = detect_activation_from_arch(&arch_str);
                let activation = match activation_str {
                    "SCReLU" => Activation::SCReLU,
                    "PairwiseCReLU" => Activation::PairwiseCReLU,
                    _ => Activation::CReLU,
                };

                // ヘッダーから FeatureSet を取得(検出のヒントに使用)
                let parsed = super::spec::parse_architecture(&arch_str)
                    .map_err(|msg| io::Error::new(io::ErrorKind::InvalidData, msg))?;

                // LayerStacks は特殊処理(ファイルサイズ検出の対象外)
                if parsed.feature_set == FeatureSet::LayerStacks {
                    reader.seek(SeekFrom::Start(0))?;
                    let network = NetworkLayerStacks::read(reader)?;
                    return Ok(Self::LayerStacks(Box::new(network)));
                }

                // 4. ファイルサイズからアーキテクチャを検出
                let detection = super::spec::detect_architecture_from_size(
                    file_size,
                    arch_len,
                    Some(parsed.feature_set),
                )
                .ok_or_else(|| {
                    // 検出失敗時は候補を表示
                    let candidates = super::spec::list_candidate_architectures(file_size, arch_len);
                    let candidates_str: Vec<String> = candidates
                        .iter()
                        .take(5)
                        .map(|(spec, diff)| format!("{} (diff: {:+})", spec.name(), diff))
                        .collect();

                    io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!(
                            "Unknown architecture: file_size={}, arch_len={}, feature_set={}. \
                             Closest candidates: [{}]",
                            file_size,
                            arch_len,
                            parsed.feature_set,
                            candidates_str.join(", ")
                        ),
                    )
                })?;

                // 位置を戻して読み込み
                reader.seek(SeekFrom::Start(0))?;

                // 5. 検出したアーキテクチャで読み込み
                let l1 = detection.spec.l1;
                let l2 = detection.spec.l2;
                let l3 = detection.spec.l3;

                match detection.spec.feature_set {
                    FeatureSet::HalfKA_hm => {
                        let network = HalfKA_hmNetwork::read(reader, l1, l2, l3, activation)?;
                        Ok(Self::HalfKA_hm(network))
                    }
                    FeatureSet::HalfKA => {
                        let network = HalfKANetwork::read(reader, l1, l2, l3, activation)?;
                        Ok(Self::HalfKA(network))
                    }
                    FeatureSet::HalfKP => {
                        let network = HalfKPNetwork::read(reader, l1, l2, l3, activation)?;
                        Ok(Self::HalfKP(network))
                    }
                    FeatureSet::LayerStacks => {
                        // 上で処理済みなのでここには来ない
                        unreachable!()
                    }
                }
            }
            _ => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!(
                    "Unknown NNUE version: {version:#x}. Expected {NNUE_VERSION:#x} (HalfKP) or {NNUE_VERSION_HALFKA:#x} (HalfKA_hm^)"
                ),
            )),
        }
    }

    /// バイト列から読み込み(バージョン自動判別)
    pub fn from_bytes(bytes: &[u8]) -> io::Result<Self> {
        let mut cursor = Cursor::new(bytes);
        Self::read(&mut cursor)
    }

    /// LayerStacks アーキテクチャかどうか
    pub fn is_layer_stacks(&self) -> bool {
        matches!(self, Self::LayerStacks(_))
    }

    /// HalfKA アーキテクチャかどうか
    pub fn is_halfka(&self) -> bool {
        matches!(self, Self::HalfKA(_))
    }

    /// HalfKA_hm アーキテクチャかどうか
    pub fn is_halfka_hm(&self) -> bool {
        matches!(self, Self::HalfKA_hm(_))
    }

    /// HalfKP アーキテクチャかどうか
    pub fn is_halfkp(&self) -> bool {
        matches!(self, Self::HalfKP(_))
    }

    /// L1 サイズを取得
    pub fn l1_size(&self) -> usize {
        match self {
            Self::HalfKA(net) => net.l1_size(),
            Self::HalfKA_hm(net) => net.l1_size(),
            Self::HalfKP(net) => net.l1_size(),
            Self::LayerStacks(_) => 1536,
        }
    }

    /// アーキテクチャ名を取得
    pub fn architecture_name(&self) -> &'static str {
        match self {
            Self::HalfKA(net) => net.architecture_name(),
            Self::HalfKA_hm(net) => net.architecture_name(),
            Self::HalfKP(net) => net.architecture_name(),
            Self::LayerStacks(_) => "LayerStacks",
        }
    }

    /// アーキテクチャ仕様を取得
    pub fn architecture_spec(&self) -> super::spec::ArchitectureSpec {
        match self {
            Self::HalfKA(net) => net.architecture_spec(),
            Self::HalfKA_hm(net) => net.architecture_spec(),
            Self::HalfKP(net) => net.architecture_spec(),
            Self::LayerStacks(_) => super::spec::ArchitectureSpec::new(
                super::spec::FeatureSet::LayerStacks,
                1536,
                0,
                0,
                Activation::CReLU,
            ),
        }
    }

    // LayerStacks 用のメソッド(LayerStacks のみ維持)

    /// 差分計算を使わずにAccumulatorを計算(LayerStacks用)
    pub fn refresh_accumulator_layer_stacks(
        &self,
        pos: &Position,
        acc: &mut super::accumulator_layer_stacks::AccumulatorLayerStacks,
    ) {
        match self {
            Self::LayerStacks(net) => net.refresh_accumulator(pos, acc),
            _ => panic!("This method is only for LayerStacks architecture."),
        }
    }

    /// 差分計算でAccumulatorを更新(LayerStacks用)
    pub fn update_accumulator_layer_stacks(
        &self,
        pos: &Position,
        dirty_piece: &super::accumulator::DirtyPiece,
        acc: &mut super::accumulator_layer_stacks::AccumulatorLayerStacks,
        prev_acc: &super::accumulator_layer_stacks::AccumulatorLayerStacks,
    ) {
        match self {
            Self::LayerStacks(net) => net.update_accumulator(pos, dirty_piece, acc, prev_acc),
            _ => panic!("This method is only for LayerStacks architecture."),
        }
    }

    /// 差分計算を使わずにAccumulatorを計算(LayerStacks用、キャッシュ対応)
    pub fn refresh_accumulator_layer_stacks_with_cache(
        &self,
        pos: &Position,
        acc: &mut super::accumulator_layer_stacks::AccumulatorLayerStacks,
        cache: &mut AccumulatorCacheLayerStacks,
    ) {
        match self {
            Self::LayerStacks(net) => net.refresh_accumulator_with_cache(pos, acc, cache),
            _ => panic!("This method is only for LayerStacks architecture."),
        }
    }

    /// 差分計算でAccumulatorを更新(LayerStacks用、キャッシュ対応)
    pub fn update_accumulator_layer_stacks_with_cache(
        &self,
        pos: &Position,
        dirty_piece: &super::accumulator::DirtyPiece,
        acc: &mut super::accumulator_layer_stacks::AccumulatorLayerStacks,
        prev_acc: &super::accumulator_layer_stacks::AccumulatorLayerStacks,
        cache: &mut AccumulatorCacheLayerStacks,
    ) {
        match self {
            Self::LayerStacks(net) => {
                net.update_accumulator_with_cache(pos, dirty_piece, acc, prev_acc, cache)
            }
            _ => panic!("This method is only for LayerStacks architecture."),
        }
    }

    /// 複数手分の差分を適用してアキュムレータを更新(LayerStacks用)
    pub fn forward_update_incremental_layer_stacks(
        &self,
        pos: &Position,
        stack: &mut AccumulatorStackLayerStacks,
        source_idx: usize,
    ) -> bool {
        match self {
            Self::LayerStacks(net) => net.forward_update_incremental(pos, stack, source_idx),
            _ => panic!("This method is only for LayerStacks architecture."),
        }
    }

    /// 評価値を計算(LayerStacks用)
    pub fn evaluate_layer_stacks(
        &self,
        pos: &Position,
        acc: &super::accumulator_layer_stacks::AccumulatorLayerStacks,
    ) -> Value {
        match self {
            Self::LayerStacks(net) => net.evaluate(pos, acc),
            _ => panic!("This method is only for LayerStacks architecture."),
        }
    }

    /// 評価値を計算(LayerStacks用、事前計算済み bucket index を使用)
    pub fn evaluate_layer_stacks_with_bucket(
        &self,
        pos: &Position,
        acc: &super::accumulator_layer_stacks::AccumulatorLayerStacks,
        bucket_index: usize,
    ) -> Value {
        match self {
            Self::LayerStacks(net) => net.evaluate_with_bucket(pos, acc, bucket_index),
            _ => panic!("This method is only for LayerStacks architecture."),
        }
    }

    /// HalfKA_hm アキュムレータをフル再計算
    pub fn refresh_accumulator_halfka_hm(&self, pos: &Position, stack: &mut HalfKA_hmStack) {
        match self {
            Self::HalfKA_hm(net) => net.refresh_accumulator(pos, stack),
            _ => panic!("This method is only for HalfKA_hm architecture."),
        }
    }

    /// HalfKA アキュムレータをフル再計算
    pub fn refresh_accumulator_halfka(&self, pos: &Position, stack: &mut HalfKAStack) {
        match self {
            Self::HalfKA(net) => net.refresh_accumulator(pos, stack),
            _ => panic!("This method is only for HalfKA architecture."),
        }
    }

    /// HalfKA_hm 差分更新
    pub fn update_accumulator_halfka_hm(
        &self,
        pos: &Position,
        dirty: &super::accumulator::DirtyPiece,
        stack: &mut HalfKA_hmStack,
        source_idx: usize,
    ) {
        match self {
            Self::HalfKA_hm(net) => net.update_accumulator(pos, dirty, stack, source_idx),
            _ => panic!("This method is only for HalfKA_hm architecture."),
        }
    }

    /// HalfKA 差分更新
    pub fn update_accumulator_halfka(
        &self,
        pos: &Position,
        dirty: &super::accumulator::DirtyPiece,
        stack: &mut HalfKAStack,
        source_idx: usize,
    ) {
        match self {
            Self::HalfKA(net) => net.update_accumulator(pos, dirty, stack, source_idx),
            _ => panic!("This method is only for HalfKA architecture."),
        }
    }

    /// HalfKA_hm 前方差分更新
    pub fn forward_update_incremental_halfka_hm(
        &self,
        pos: &Position,
        stack: &mut HalfKA_hmStack,
        source_idx: usize,
    ) -> bool {
        match self {
            Self::HalfKA_hm(net) => net.forward_update_incremental(pos, stack, source_idx),
            _ => panic!("This method is only for HalfKA_hm architecture."),
        }
    }

    /// HalfKA 前方差分更新
    pub fn forward_update_incremental_halfka(
        &self,
        pos: &Position,
        stack: &mut HalfKAStack,
        source_idx: usize,
    ) -> bool {
        match self {
            Self::HalfKA(net) => net.forward_update_incremental(pos, stack, source_idx),
            _ => panic!("This method is only for HalfKA architecture."),
        }
    }

    /// HalfKA_hm 評価
    pub fn evaluate_halfka_hm(&self, pos: &Position, stack: &HalfKA_hmStack) -> Value {
        match self {
            Self::HalfKA_hm(net) => net.evaluate(pos, stack),
            _ => panic!("This method is only for HalfKA_hm architecture."),
        }
    }

    /// HalfKA 評価
    pub fn evaluate_halfka(&self, pos: &Position, stack: &HalfKAStack) -> Value {
        match self {
            Self::HalfKA(net) => net.evaluate(pos, stack),
            _ => panic!("This method is only for HalfKA architecture."),
        }
    }

    /// HalfKP アキュムレータをフル再計算
    pub fn refresh_accumulator_halfkp(&self, pos: &Position, stack: &mut HalfKPStack) {
        match self {
            Self::HalfKP(net) => net.refresh_accumulator(pos, stack),
            _ => panic!("This method is only for HalfKP architecture."),
        }
    }

    /// HalfKP 差分更新
    pub fn update_accumulator_halfkp(
        &self,
        pos: &Position,
        dirty: &super::accumulator::DirtyPiece,
        stack: &mut HalfKPStack,
        source_idx: usize,
    ) {
        match self {
            Self::HalfKP(net) => net.update_accumulator(pos, dirty, stack, source_idx),
            _ => panic!("This method is only for HalfKP architecture."),
        }
    }

    /// HalfKP 前方差分更新
    pub fn forward_update_incremental_halfkp(
        &self,
        pos: &Position,
        stack: &mut HalfKPStack,
        source_idx: usize,
    ) -> bool {
        match self {
            Self::HalfKP(net) => net.forward_update_incremental(pos, stack, source_idx),
            _ => panic!("This method is only for HalfKP architecture."),
        }
    }

    /// HalfKP 評価
    pub fn evaluate_halfkp(&self, pos: &Position, stack: &HalfKPStack) -> Value {
        match self {
            Self::HalfKP(net) => net.evaluate(pos, stack),
            _ => panic!("This method is only for HalfKP architecture."),
        }
    }
}

// =============================================================================
// arch_str メタデータパース
// =============================================================================

/// arch_str から fv_scale を抽出
///
/// bullet-shogi で学習したモデルは arch_str に "fv_scale=N" を含む。
/// 例: "Features=HalfKA_hm^[73305->256x2]-SCReLU,fv_scale=13,qa=127,qb=64,scale=600"
///
/// 戻り値:
/// - `Some(N)`: fv_scale=N が見つかり、妥当な範囲(1〜128)内の場合
/// - `None`: fv_scale が見つからない、またはパース失敗、または範囲外
///
/// 範囲外の値(0, 負数, 128超)は None を返し、フォールバック値が使用される。
/// これによりゼロ除算や不正な評価値スケーリングを防止する。
pub fn parse_fv_scale_from_arch(arch_str: &str) -> Option<i32> {
    /// fv_scale の許容最小値(ゼロ除算防止)
    const FV_SCALE_MIN: i32 = 1;
    /// fv_scale の許容最大値(実用的な上限)
    const FV_SCALE_MAX: i32 = 128;

    for part in arch_str.split(',') {
        if let Some(value) = part.strip_prefix("fv_scale=") {
            if let Ok(scale) = value.parse::<i32>() {
                // 妥当な範囲内のみ受け入れる
                if (FV_SCALE_MIN..=FV_SCALE_MAX).contains(&scale) {
                    return Some(scale);
                }
            }
            // fv_scale= が見つかったがパース失敗または範囲外の場合は None
            return None;
        }
    }
    None
}

/// LayerStacks bucket mode をパース
pub fn parse_layer_stack_bucket_mode(value: &str) -> Option<LayerStackBucketMode> {
    match value.trim().to_ascii_lowercase().as_str() {
        "kingrank9" => Some(LayerStackBucketMode::KingRank9),
        "ply9" => Some(LayerStackBucketMode::Ply9),
        "progress8" => Some(LayerStackBucketMode::Progress8),
        "progress8gikou" => Some(LayerStackBucketMode::Progress8Gikou),
        "progress8kpabs" => Some(LayerStackBucketMode::Progress8KPAbs),
        _ => None,
    }
}

/// `LS_PLY_BOUNDS` 文字列をパースする。
///
/// 形式: `30,44,58,72,86,100,116,138` (8要素)
pub fn parse_layer_stack_ply_bounds_csv(text: &str) -> Result<[u16; 8], String> {
    let mut values = Vec::new();
    for token in text.split(',') {
        let t = token.trim();
        if t.is_empty() {
            continue;
        }
        let value: u16 =
            t.parse().map_err(|e| format!("invalid LS_PLY_BOUNDS value '{t}': {e}"))?;
        values.push(value);
    }

    if values.len() != 8 {
        return Err(format!(
            "LS_PLY_BOUNDS requires exactly 8 comma-separated values (got {})",
            values.len()
        ));
    }

    Ok([
        values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7],
    ])
}

/// LayerStacks ply9 境界を CSV 文字列へ変換
pub fn format_layer_stack_ply_bounds(bounds: [u16; 8]) -> String {
    bounds.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(",")
}

/// game_ply と境界から LayerStacks ply9 の bucket index (0..=8) を計算
pub fn compute_layer_stack_ply9_bucket_index(game_ply: i32, bounds: [u16; 8]) -> usize {
    let ply = if game_ply < 0 {
        0
    } else {
        u16::try_from(game_ply).unwrap_or(u16::MAX)
    };

    for (i, &bound) in bounds.iter().enumerate() {
        if ply <= bound {
            return i;
        }
    }

    8
}

/// progress8 係数に基づいて LayerStacks bucket index (0..=7) を計算
pub fn compute_layer_stack_progress8_bucket_index(
    pos: &Position,
    side_to_move: Color,
    coeff: LayerStackProgressCoeff,
) -> usize {
    let board_non_king = (pos.occupied().count() - pos.pieces_pt(PieceType::King).count()) as f32;

    let hand_black = pos.hand(Color::Black);
    let hand_white = pos.hand(Color::White);
    let hand_total = PieceType::HAND_PIECES
        .iter()
        .map(|&pt| hand_black.count(pt) + hand_white.count(pt))
        .sum::<u32>() as f32;

    let major_board = (pos.pieces_pt(PieceType::Bishop).count()
        + pos.pieces_pt(PieceType::Rook).count()
        + pos.pieces_pt(PieceType::Horse).count()
        + pos.pieces_pt(PieceType::Dragon).count()) as f32;

    let promoted_board = (pos.pieces_pt(PieceType::ProPawn).count()
        + pos.pieces_pt(PieceType::ProLance).count()
        + pos.pieces_pt(PieceType::ProKnight).count()
        + pos.pieces_pt(PieceType::ProSilver).count()
        + pos.pieces_pt(PieceType::Horse).count()
        + pos.pieces_pt(PieceType::Dragon).count()) as f32;

    let f_king_rank = pos.king_square(side_to_move).rank().index() as f32;
    let e_king_rank = pos.king_square(!side_to_move).rank().index() as f32;
    let (stm_king_rank_rel, ntm_king_rank_rel) = match side_to_move {
        Color::Black => (f_king_rank, 8.0 - e_king_rank),
        Color::White => (8.0 - f_king_rank, e_king_rank),
    };

    let x = [
        board_non_king,
        hand_total,
        major_board,
        promoted_board,
        stm_king_rank_rel,
        ntm_king_rank_rel,
    ];

    let mut z = coeff.bias;
    for (i, &feature) in x.iter().enumerate() {
        let std = if coeff.std[i] > 0.0 {
            coeff.std[i]
        } else {
            1.0
        };
        let x_norm = (feature - coeff.mean[i]) / std;
        z += coeff.weights[i] * x_norm;
    }

    let z_min = coeff.z_clip[0].min(coeff.z_clip[1]);
    let z_max = coeff.z_clip[0].max(coeff.z_clip[1]);
    let z_clamped = z.clamp(z_min, z_max);
    let p = (1.0 / (1.0 + (-z_clamped).exp())).clamp(0.0, 1.0);
    let raw = (p * SHOGI_PROGRESS8_NUM_BUCKETS as f32).floor() as i32;

    raw.clamp(0, (SHOGI_PROGRESS8_NUM_BUCKETS - 1) as i32) as usize
}

#[inline]
fn chebyshev_distance(a: crate::types::Square, b: crate::types::Square) -> u8 {
    let df = a.file().index().abs_diff(b.file().index());
    let dr = a.rank().index().abs_diff(b.rank().index());
    df.max(dr) as u8
}

#[inline]
fn distance_bin(d: u8) -> usize {
    if d <= 1 {
        0
    } else if d == 2 {
        1
    } else {
        2
    }
}

#[inline]
fn is_major_piece(pt: PieceType) -> bool {
    matches!(pt, PieceType::Bishop | PieceType::Rook | PieceType::Horse | PieceType::Dragon)
}

/// progress8gikou 係数に基づいて LayerStacks bucket index (0..=7) を計算
pub fn compute_layer_stack_progress8gikou_bucket_index(
    pos: &Position,
    side_to_move: Color,
    coeff: LayerStackProgressCoeffGikouLite,
) -> usize {
    let mut x = [0.0f32; SHOGI_PROGRESS_GIKOU_LITE_NUM_FEATURES];

    // v1 の6特徴を prefix として共有する。
    let board_non_king = (pos.occupied().count() - pos.pieces_pt(PieceType::King).count()) as f32;
    let hand_black = pos.hand(Color::Black);
    let hand_white = pos.hand(Color::White);
    let hand_total = PieceType::HAND_PIECES
        .iter()
        .map(|&pt| hand_black.count(pt) + hand_white.count(pt))
        .sum::<u32>() as f32;
    let major_board = (pos.pieces_pt(PieceType::Bishop).count()
        + pos.pieces_pt(PieceType::Rook).count()
        + pos.pieces_pt(PieceType::Horse).count()
        + pos.pieces_pt(PieceType::Dragon).count()) as f32;
    let promoted_board = (pos.pieces_pt(PieceType::ProPawn).count()
        + pos.pieces_pt(PieceType::ProLance).count()
        + pos.pieces_pt(PieceType::ProKnight).count()
        + pos.pieces_pt(PieceType::ProSilver).count()
        + pos.pieces_pt(PieceType::Horse).count()
        + pos.pieces_pt(PieceType::Dragon).count()) as f32;
    let f_king_rank = pos.king_square(side_to_move).rank().index() as f32;
    let e_king_rank = pos.king_square(!side_to_move).rank().index() as f32;
    let (stm_king_rank_rel, ntm_king_rank_rel) = match side_to_move {
        Color::Black => (f_king_rank, 8.0 - e_king_rank),
        Color::White => (8.0 - f_king_rank, e_king_rank),
    };
    x[0] = board_non_king;
    x[1] = hand_total;
    x[2] = major_board;
    x[3] = promoted_board;
    x[4] = stm_king_rank_rel;
    x[5] = ntm_king_rank_rel;

    let stm_king = pos.king_square(side_to_move);
    let ntm_king = pos.king_square(!side_to_move);
    for sq in pos.occupied().iter() {
        let pc = pos.piece_on(sq);
        if pc.is_none() {
            continue;
        }
        let pt = pc.piece_type();
        if pt == PieceType::King {
            continue;
        }

        let is_stm_piece = pc.color() == side_to_move;
        let side_offset = if is_stm_piece { 6usize } else { 12usize };
        let major_offset = if is_stm_piece { 18usize } else { 24usize };
        let own_king = if is_stm_piece { stm_king } else { ntm_king };
        let opp_king = if is_stm_piece { ntm_king } else { stm_king };

        let own_bin = distance_bin(chebyshev_distance(sq, own_king));
        let opp_bin = distance_bin(chebyshev_distance(sq, opp_king));
        x[side_offset + own_bin] += 1.0;
        x[side_offset + 3 + opp_bin] += 1.0;

        if is_major_piece(pt) {
            x[major_offset + own_bin] += 1.0;
            x[major_offset + 3 + opp_bin] += 1.0;
        }
    }

    let stm_hand = pos.hand(side_to_move);
    let ntm_hand = pos.hand(!side_to_move);
    x[30] = PieceType::HAND_PIECES.iter().map(|&pt| stm_hand.count(pt)).sum::<u32>() as f32;
    x[31] = PieceType::HAND_PIECES.iter().map(|&pt| ntm_hand.count(pt)).sum::<u32>() as f32;
    x[32] = (stm_hand.count(PieceType::Bishop) + stm_hand.count(PieceType::Rook)) as f32;
    x[33] = (ntm_hand.count(PieceType::Bishop) + ntm_hand.count(PieceType::Rook)) as f32;

    let mut z = coeff.bias;
    for (i, &feature) in x.iter().enumerate() {
        let std = if coeff.std[i] > 0.0 {
            coeff.std[i]
        } else {
            1.0
        };
        let x_norm = (feature - coeff.mean[i]) / std;
        z += coeff.weights[i] * x_norm;
    }

    let z_min = coeff.z_clip[0].min(coeff.z_clip[1]);
    let z_max = coeff.z_clip[0].max(coeff.z_clip[1]);
    let z_clamped = z.clamp(z_min, z_max);
    let p = (1.0 / (1.0 + (-z_clamped).exp())).clamp(0.0, 1.0);
    let raw = (p * SHOGI_PROGRESS8_NUM_BUCKETS as f32).floor() as i32;
    raw.clamp(0, (SHOGI_PROGRESS8_NUM_BUCKETS - 1) as i32) as usize
}

/// progress8kpabs 重みに基づいて LayerStacks bucket index (0..=7) を計算
///
/// `CACHED_PROGRESS_BUCKET` にキャッシュされた値がある場合はそちらを消費する。
pub fn compute_layer_stack_progress8kpabs_bucket_index(
    pos: &Position,
    _side_to_move: Color,
    weights: &[f32],
) -> usize {
    // 差分計算済みキャッシュがあれば消費して返す
    let cached = CACHED_PROGRESS_BUCKET.with(|c| c.replace(None));
    if let Some(bucket) = cached {
        return bucket;
    }
    // フォールバック: 全駒スキャン
    let sum = compute_progress8kpabs_sum(pos, weights);
    progress_sum_to_bucket(sum)
}

/// progress8kpabs の重み付き和を全駒スキャンで計算(refresh 用)
pub fn compute_progress8kpabs_sum(pos: &Position, weights: &[f32]) -> f32 {
    debug_assert_eq!(
        weights.len(),
        SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS,
        "progress8kpabs weights length mismatch"
    );

    let sq_bk = pos.king_square(Color::Black).index();
    let sq_wk = pos.king_square(Color::White).inverse().index();
    let weights_b = &weights[sq_bk * FE_OLD_END..(sq_bk + 1) * FE_OLD_END];
    let weights_w = &weights[sq_wk * FE_OLD_END..(sq_wk + 1) * FE_OLD_END];

    let mut sum = 0.0f32;

    for sq in pos.occupied().iter() {
        let pc = pos.piece_on(sq);
        if pc.is_none() || pc.piece_type() == PieceType::King {
            continue;
        }

        let bp_b = BonaPiece::from_piece_square(pc, sq, Color::Black);
        if bp_b != BonaPiece::ZERO {
            sum += weights_b[bp_b.value() as usize];
        }

        let bp_w = BonaPiece::from_piece_square(pc, sq, Color::White);
        if bp_w != BonaPiece::ZERO {
            sum += weights_w[bp_w.value() as usize];
        }
    }

    for owner in [Color::Black, Color::White] {
        let hand = pos.hand(owner);
        for &pt in &PieceType::HAND_PIECES {
            let count = hand.count(pt);
            for c in 1..=count {
                let c_u8 = u8::try_from(c).expect("hand count fits in u8");

                let bp_b = BonaPiece::from_hand_piece(Color::Black, owner, pt, c_u8);
                if bp_b != BonaPiece::ZERO {
                    sum += weights_b[bp_b.value() as usize];
                }

                let bp_w = BonaPiece::from_hand_piece(Color::White, owner, pt, c_u8);
                if bp_w != BonaPiece::ZERO {
                    sum += weights_w[bp_w.value() as usize];
                }
            }
        }
    }

    sum
}

/// progress_sum から DirtyPiece の変化分を差分更新
///
/// 玉が動いていない場合にのみ使用可能。
/// DirtyPiece の ExtBonaPiece.fb/fw は progress8kpabs と同じ BonaPiece 体系。
#[inline]
pub fn update_progress8kpabs_sum_diff(
    prev_sum: f32,
    dirty_piece: &super::accumulator::DirtyPiece,
    sq_bk: usize,
    sq_wk: usize,
    weights: &[f32],
) -> f32 {
    let weights_b = &weights[sq_bk * FE_OLD_END..(sq_bk + 1) * FE_OLD_END];
    let weights_w = &weights[sq_wk * FE_OLD_END..(sq_wk + 1) * FE_OLD_END];
    let mut sum = prev_sum;
    for i in 0..dirty_piece.dirty_num as usize {
        let changed = &dirty_piece.changed_piece[i];

        // old の寄与を引く
        let old_fb = changed.old_piece.fb;
        if old_fb != BonaPiece::ZERO {
            sum -= weights_b[old_fb.value() as usize];
        }
        let old_fw = changed.old_piece.fw;
        if old_fw != BonaPiece::ZERO {
            sum -= weights_w[old_fw.value() as usize];
        }

        // new の寄与を足す
        let new_fb = changed.new_piece.fb;
        if new_fb != BonaPiece::ZERO {
            sum += weights_b[new_fb.value() as usize];
        }
        let new_fw = changed.new_piece.fw;
        if new_fw != BonaPiece::ZERO {
            sum += weights_w[new_fw.value() as usize];
        }
    }
    sum
}

/// progress_sum から bucket index を計算(閾値比較のみ)
#[inline]
pub fn progress_sum_to_bucket(sum: f32) -> usize {
    PROGRESS_BUCKET_THRESHOLDS.partition_point(|&threshold| sum >= threshold)
}

/// NNUEを初期化(バージョン自動判別)
pub fn init_nnue<P: AsRef<Path>>(path: P) -> io::Result<()> {
    let network = Arc::new(NNUENetwork::load(path)?);
    *NETWORK.write().expect("NNUE lock poisoned") = Some(network);
    Ok(())
}

/// バイト列からNNUEを初期化(バージョン自動判別)
pub fn init_nnue_from_bytes(bytes: &[u8]) -> io::Result<()> {
    let network = Arc::new(NNUENetwork::from_bytes(bytes)?);
    *NETWORK.write().expect("NNUE lock poisoned") = Some(network);
    Ok(())
}

/// グローバル NNUE をクリアする
pub fn clear_nnue() {
    *NETWORK.write().expect("NNUE lock poisoned") = None;
}

/// NNUEが初期化済みかどうか
pub fn is_nnue_initialized() -> bool {
    NETWORK.read().expect("NNUE lock poisoned").is_some()
}

// =============================================================================
// フォーマット検出
// =============================================================================

/// NNUE フォーマット情報
#[derive(Debug, Clone)]
pub struct NnueFormatInfo {
    /// アーキテクチャ名(例: "HalfKA1024", "HalfKA_hm1024", "LayerStacks", "HalfKP256")
    pub architecture: String,

    /// L1 次元(例: 256, 512, 1024, 1536)
    pub l1_dimension: u32,

    /// L2 次元(例: 8, 32)
    pub l2_dimension: u32,

    /// L3 次元(例: 32, 96)
    pub l3_dimension: u32,

    /// 活性化関数("CReLU" or "SCReLU")
    pub activation: String,

    /// バージョンヘッダ(生の u32 値)
    pub version: u32,

    /// アーキテクチャ文字列(生の文字列)
    pub arch_string: String,
}

/// NNUE ファイルのフォーマット情報を検出(ファイルサイズベースの自動判定)
///
/// nnue-pytorch が生成するファイルはヘッダーに不正確なアーキテクチャ情報を
/// 含むことがあるため、ファイルサイズから正確なアーキテクチャを検出する。
///
/// # 検出ロジック
/// 1. ヘッダーから FeatureSet と活性化関数を取得(ヒントとして使用)
/// 2. ファイルサイズから L1/L2/L3 を一意に検出(優先)
/// 3. 検出失敗時はヘッダーのパース結果にフォールバック(精度低下の可能性あり)
///
/// # Arguments
/// * `bytes` - NNUE ファイルの先頭バイト列(ヘッダー + アーキテクチャ文字列を含む)
/// * `file_size` - ファイル全体のサイズ(バイト単位)
///
/// # Returns
/// * `Ok(NnueFormatInfo)` - 検出されたフォーマット情報
/// * `Err(io::Error)` - ヘッダー解析失敗または不正なフォーマット
///
/// # Errors
/// - `InvalidData`: ファイルサイズ不足、不正なヘッダー、またはアーキテクチャ文字列長
///
/// # Examples
/// ```ignore
/// let bytes = std::fs::read("model.bin")?;
/// let file_size = bytes.len() as u64;
/// let info = detect_format(&bytes, file_size)?;
/// println!("Detected: {} (L1={}, L2={}, L3={})",
///          info.architecture, info.l1_dimension, info.l2_dimension, info.l3_dimension);
/// ```
pub fn detect_format(bytes: &[u8], file_size: u64) -> io::Result<NnueFormatInfo> {
    // 最小ヘッダーサイズ: version(4) + hash(4) + arch_len(4)
    const MIN_HEADER_SIZE: usize = 12;

    if bytes.len() < MIN_HEADER_SIZE {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "NNUE file too small: {} bytes (need at least {} for header)",
                bytes.len(),
                MIN_HEADER_SIZE
            ),
        ));
    }

    // バージョンを読み取り
    let version = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);

    match version {
        NNUE_VERSION | NNUE_VERSION_HALFKA => {
            // アーキテクチャ文字列長を読み取り
            let arch_len = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]) as usize;

            // arch_len の妥当性をチェック(バッファオーバーリード防止)
            if arch_len == 0 || arch_len > MAX_ARCH_LEN {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Invalid arch string length: {} (max: {})", arch_len, MAX_ARCH_LEN),
                ));
            }

            // 必要なバイト数をチェック
            let required_size = MIN_HEADER_SIZE + arch_len;
            if bytes.len() < required_size {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "NNUE file too small: {} bytes (need {} for arch string)",
                        bytes.len(),
                        required_size
                    ),
                ));
            }

            // アーキテクチャ文字列を読み取り
            let arch_str = String::from_utf8_lossy(&bytes[12..12 + arch_len]).to_string();

            // 活性化関数を検出(ヘッダーから)
            let activation = detect_activation_from_arch(&arch_str).to_string();

            // ヘッダーから FeatureSet を取得(検出のヒントに使用)
            let parsed = super::spec::parse_architecture(&arch_str)
                .map_err(|msg| io::Error::new(io::ErrorKind::InvalidData, msg))?;

            // ファイルサイズからアーキテクチャを検出(L1/L2/L3 の正確な値を取得)
            let (l1, l2, l3, feature_set, used_file_size_detection) = if let Some(detection) =
                super::spec::detect_architecture_from_size(
                    file_size,
                    arch_len,
                    Some(parsed.feature_set),
                ) {
                // ファイルサイズベースの検出成功
                (
                    detection.spec.l1,
                    detection.spec.l2,
                    detection.spec.l3,
                    detection.spec.feature_set,
                    true,
                )
            } else {
                // フォールバック: ヘッダーのパース結果を使用
                // 注意: ヘッダーが不正確な場合、誤った結果になる可能性がある
                (parsed.l1, parsed.l2, parsed.l3, parsed.feature_set, false)
            };

            // フォールバック時は警告情報をログ出力(デバッグビルド時のみ)
            #[cfg(debug_assertions)]
            if !used_file_size_detection {
                eprintln!(
                    "Warning: File size detection failed for size={}. \
                     Falling back to header parsing (may be inaccurate).",
                    file_size
                );
            }
            // used_file_size_detection を使用済みとしてマーク(リリースビルドでの警告抑制)
            let _ = used_file_size_detection;

            // アーキテクチャ名を決定
            let architecture = match feature_set {
                FeatureSet::LayerStacks => "LayerStacks".to_string(),
                FeatureSet::HalfKA_hm => format!("HalfKA_hm{}", l1),
                FeatureSet::HalfKA => format!("HalfKA{}", l1),
                FeatureSet::HalfKP => format!("HalfKP{}", l1),
            };

            Ok(NnueFormatInfo {
                architecture,
                l1_dimension: l1 as u32,
                l2_dimension: l2 as u32,
                l3_dimension: l3 as u32,
                activation,
                version,
                arch_string: arch_str,
            })
        }
        _ => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("Unknown NNUE version: 0x{version:08X}"),
        )),
    }
}

/// NNUEネットワークへの参照を取得(初期化されていない場合はNone)
///
/// AccumulatorStackVariant の初期化・更新に使用。
pub fn get_network() -> Option<Arc<NNUENetwork>> {
    NETWORK.read().expect("NNUE lock poisoned").clone()
}

// =============================================================================
// 内部ヘルパー関数(ロジック集約用)
// =============================================================================

/// LayerStacks アキュムレータを更新して評価(内部実装)
///
/// `evaluate_layer_stacks` と `evaluate_dispatch` から呼び出される共通ロジック。
/// network は既に取得済みで、アーキテクチャチェックも完了していることが前提。
#[inline]
fn update_and_evaluate_layer_stacks(
    network: &NNUENetwork,
    pos: &Position,
    stack: &mut AccumulatorStackLayerStacks,
) -> Value {
    // アキュムレータの更新
    let current_entry = stack.current();
    if !current_entry.accumulator.computed_accumulation {
        let mut updated = false;

        // 1. 直前局面で差分更新を試行
        if let Some(prev_idx) = current_entry.previous {
            let prev_computed = stack.entry_at(prev_idx).accumulator.computed_accumulation;
            if prev_computed {
                let dirty_piece = stack.current().dirty_piece;
                let (prev_acc, current_acc) = stack.get_prev_and_current_accumulators(prev_idx);
                network.update_accumulator_layer_stacks(pos, &dirty_piece, current_acc, prev_acc);
                updated = true;
            }
        }

        // 2. 失敗なら祖先探索 + 複数手差分更新を試行
        if !updated && let Some((source_idx, _depth)) = stack.find_usable_accumulator() {
            updated = network.forward_update_incremental_layer_stacks(pos, stack, source_idx);
        }

        // 3. それでも失敗なら全計算
        if !updated {
            let acc = &mut stack.current_mut().accumulator;
            network.refresh_accumulator_layer_stacks(pos, acc);
        }
    }

    // progress8kpabs: 差分更新を試み、結果を CACHED_PROGRESS_BUCKET に格納
    if get_layer_stack_bucket_mode() == LayerStackBucketMode::Progress8KPAbs {
        let bucket = ensure_progress_bucket(pos, stack);
        CACHED_PROGRESS_BUCKET.with(|c| c.set(Some(bucket)));
    }

    // 評価
    let acc_ref = &stack.current().accumulator;
    network.evaluate_layer_stacks(pos, acc_ref)
}

/// LayerStacks アキュムレータを更新して評価(キャッシュ対応版)
///
/// `update_and_evaluate_layer_stacks` と同じロジックだが、
/// AccumulatorCaches(Finny Tables)を使用して refresh を高速化する。
#[inline]
fn update_and_evaluate_layer_stacks_cached(
    network: &NNUENetwork,
    pos: &Position,
    stack: &mut AccumulatorStackLayerStacks,
    acc_cache: &mut Option<AccumulatorCacheLayerStacks>,
) -> Value {
    // アキュムレータの更新
    let current_entry = stack.current();
    if !current_entry.accumulator.computed_accumulation {
        let mut updated = false;

        // 1. 直前局面で差分更新を試行
        if let Some(prev_idx) = current_entry.previous {
            let prev_computed = stack.entry_at(prev_idx).accumulator.computed_accumulation;
            if prev_computed {
                let dirty_piece = stack.current().dirty_piece;
                let (prev_acc, current_acc) = stack.get_prev_and_current_accumulators(prev_idx);
                if let Some(cache) = acc_cache {
                    network.update_accumulator_layer_stacks_with_cache(
                        pos,
                        &dirty_piece,
                        current_acc,
                        prev_acc,
                        cache,
                    );
                } else {
                    network.update_accumulator_layer_stacks(
                        pos,
                        &dirty_piece,
                        current_acc,
                        prev_acc,
                    );
                }
                updated = true;
            }
        }

        // 2. 失敗なら祖先探索 + 複数手差分更新を試行
        if !updated && let Some((source_idx, _depth)) = stack.find_usable_accumulator() {
            updated = network.forward_update_incremental_layer_stacks(pos, stack, source_idx);
        }

        // 3. それでも失敗なら全計算(キャッシュ経由)
        if !updated {
            let acc = &mut stack.current_mut().accumulator;
            if let Some(cache) = acc_cache {
                network.refresh_accumulator_layer_stacks_with_cache(pos, acc, cache);
            } else {
                network.refresh_accumulator_layer_stacks(pos, acc);
            }
        }
    }

    // progress8kpabs: 差分更新を試み、結果を CACHED_PROGRESS_BUCKET に格納
    if get_layer_stack_bucket_mode() == LayerStackBucketMode::Progress8KPAbs {
        let bucket = ensure_progress_bucket(pos, stack);
        CACHED_PROGRESS_BUCKET.with(|c| c.set(Some(bucket)));
    }

    // 評価
    let acc_ref = &stack.current().accumulator;
    network.evaluate_layer_stacks(pos, acc_ref)
}

/// progress8kpabs の progress_sum を計算済みにして bucket index を返す
///
/// 差分更新が可能な場合(前局面が計算済み、玉移動なし)は DirtyPiece の差分で O(1) 更新。
/// それ以外は全駒スキャンにフォールバック。
#[inline]
fn ensure_progress_bucket(pos: &Position, stack: &mut AccumulatorStackLayerStacks) -> usize {
    if !stack.current().computed_progress {
        let weights = get_layer_stack_progress_kpabs_weights();
        let current_entry = stack.current();
        let dirty = &current_entry.dirty_piece;
        let king_moved = dirty.king_moved[0] || dirty.king_moved[1];

        if !king_moved
            && let Some(prev_idx) = current_entry.previous
            && stack.entry_at(prev_idx).computed_progress
        {
            let prev_sum = stack.entry_at(prev_idx).progress_sum;
            let sq_bk = pos.king_square(Color::Black).index();
            let sq_wk = pos.king_square(Color::White).inverse().index();
            let new_sum = update_progress8kpabs_sum_diff(prev_sum, dirty, sq_bk, sq_wk, weights);
            let entry = stack.current_mut();
            entry.progress_sum = new_sum;
            entry.computed_progress = true;
        }

        if !stack.current().computed_progress {
            let sum = compute_progress8kpabs_sum(pos, weights);
            let entry = stack.current_mut();
            entry.progress_sum = sum;
            entry.computed_progress = true;
        }
    }
    progress_sum_to_bucket(stack.current().progress_sum)
}

/// HalfKA_hm アキュムレータを更新して評価(内部実装)
#[inline]
fn update_and_evaluate_halfka_hm(
    network: &NNUENetwork,
    pos: &Position,
    stack: &mut HalfKA_hmStack,
) -> Value {
    // アキュムレータの更新
    if !stack.is_current_computed() {
        let mut updated = false;

        // 1. 直前局面で差分更新を試行
        if let Some(prev_idx) = stack.current_previous()
            && stack.is_entry_computed(prev_idx)
        {
            let dirty = stack.current_dirty_piece();
            network.update_accumulator_halfka_hm(pos, &dirty, stack, prev_idx);
            updated = true;
        }

        // 2. 失敗なら祖先探索 + 複数手差分更新を試行
        if !updated && let Some((source_idx, _depth)) = stack.find_usable_accumulator() {
            updated = network.forward_update_incremental_halfka_hm(pos, stack, source_idx);
        }

        // 3. それでも失敗なら全計算
        if !updated {
            network.refresh_accumulator_halfka_hm(pos, stack);
        }
    }

    // 評価
    network.evaluate_halfka_hm(pos, stack)
}

/// HalfKA アキュムレータを更新して評価(内部実装)
#[inline]
fn update_and_evaluate_halfka(
    network: &NNUENetwork,
    pos: &Position,
    stack: &mut HalfKAStack,
) -> Value {
    // アキュムレータの更新
    if !stack.is_current_computed() {
        let mut updated = false;

        // 1. 直前局面で差分更新を試行
        if let Some(prev_idx) = stack.current_previous()
            && stack.is_entry_computed(prev_idx)
        {
            let dirty = stack.current_dirty_piece();
            network.update_accumulator_halfka(pos, &dirty, stack, prev_idx);
            updated = true;
        }

        // 2. 失敗なら祖先探索 + 複数手差分更新を試行
        if !updated && let Some((source_idx, _depth)) = stack.find_usable_accumulator() {
            updated = network.forward_update_incremental_halfka(pos, stack, source_idx);
        }

        // 3. それでも失敗なら全計算
        if !updated {
            network.refresh_accumulator_halfka(pos, stack);
        }
    }

    // 評価
    network.evaluate_halfka(pos, stack)
}

/// HalfKP アキュムレータを更新して評価(内部実装)
#[inline]
fn update_and_evaluate_halfkp(
    network: &NNUENetwork,
    pos: &Position,
    stack: &mut HalfKPStack,
) -> Value {
    // アキュムレータの更新
    if !stack.is_current_computed() {
        let mut updated = false;

        // 1. 直前局面で差分更新を試行
        if let Some(prev_idx) = stack.current_previous()
            && stack.is_entry_computed(prev_idx)
        {
            let dirty = stack.current_dirty_piece();
            network.update_accumulator_halfkp(pos, &dirty, stack, prev_idx);
            updated = true;
        }

        // 2. 失敗なら祖先探索 + 複数手差分更新を試行
        if !updated && let Some((source_idx, _depth)) = stack.find_usable_accumulator() {
            updated = network.forward_update_incremental_halfkp(pos, stack, source_idx);
        }

        // 3. それでも失敗なら全計算
        if !updated {
            network.refresh_accumulator_halfkp(pos, stack);
        }
    }

    // 評価
    network.evaluate_halfkp(pos, stack)
}

/// ロードされたNNUEがLayerStacksアーキテクチャかどうか
pub fn is_layer_stacks_loaded() -> bool {
    get_network().is_some_and(|n| n.is_layer_stacks())
}

/// ロードされたNNUEがHalfKA_hm256アーキテクチャかどうか
pub fn is_halfka_hm_256_loaded() -> bool {
    get_network().is_some_and(|n| n.is_halfka_hm() && n.l1_size() == 256)
}

/// ロードされたNNUEがHalfKA256アーキテクチャかどうか
pub fn is_halfka_256_loaded() -> bool {
    get_network().is_some_and(|n| n.is_halfka() && n.l1_size() == 256)
}

/// ロードされたNNUEがHalfKA_hm512アーキテクチャかどうか
pub fn is_halfka_hm_512_loaded() -> bool {
    get_network().is_some_and(|n| n.is_halfka_hm() && n.l1_size() == 512)
}

/// ロードされたNNUEがHalfKA512アーキテクチャかどうか
pub fn is_halfka_512_loaded() -> bool {
    get_network().is_some_and(|n| n.is_halfka() && n.l1_size() == 512)
}

/// ロードされたNNUEがHalfKA_hm1024アーキテクチャかどうか
pub fn is_halfka_hm_1024_loaded() -> bool {
    get_network().is_some_and(|n| n.is_halfka_hm() && n.l1_size() == 1024)
}

/// ロードされたNNUEがHalfKA1024アーキテクチャかどうか
pub fn is_halfka_1024_loaded() -> bool {
    get_network().is_some_and(|n| n.is_halfka() && n.l1_size() == 1024)
}

/// 局面を評価(LayerStacks用)
///
/// AccumulatorStackLayerStacks を使って差分更新し、計算済みなら再利用する。
///
/// # Panics
/// NNUEが未ロードかつMaterial評価も無効の場合はパニックする。
pub fn evaluate_layer_stacks(pos: &Position, stack: &mut AccumulatorStackLayerStacks) -> Value {
    if material::is_material_enabled() {
        return material::evaluate_material(pos);
    }

    let Some(network) = get_network() else {
        panic!(
            "NNUE network not loaded and MaterialLevel not set. \
             Use 'setoption name EvalFile' or 'setoption name MaterialLevel'."
        );
    };

    // LayerStacks 以外はエラー
    if !network.is_layer_stacks() {
        panic!("Non-LayerStacks architecture detected. Use evaluate() with AccumulatorStack.");
    }

    // 内部ヘルパー関数を呼び出し
    update_and_evaluate_layer_stacks(&network, pos, stack)
}

/// アーキテクチャに応じて適切な評価関数を呼び出す
///
/// AccumulatorStackVariant を受け取り、内部のバリアントに応じて
/// 適切な評価関数を呼び出す。
///
/// `acc_cache` は LayerStacks 用 AccumulatorCaches(Finny Tables)。
/// LayerStacks 以外のアーキテクチャでは無視される。
///
/// # Panics
/// NNUEが未ロードかつMaterial評価も無効の場合はパニックする。
pub fn evaluate_dispatch(
    pos: &Position,
    stack: &mut AccumulatorStackVariant,
    acc_cache: &mut Option<AccumulatorCacheLayerStacks>,
) -> Value {
    if material::is_material_enabled() {
        return material::evaluate_material(pos);
    }

    let Some(network) = get_network() else {
        panic!(
            "NNUE network not loaded and MaterialLevel not set. \
             Use 'setoption name EvalFile' or 'setoption name MaterialLevel'."
        );
    };

    // バリアントに応じて適切な評価関数を呼び出し(4バリアント)
    match stack {
        AccumulatorStackVariant::LayerStacks(s) => {
            update_and_evaluate_layer_stacks_cached(&network, pos, s, acc_cache)
        }
        AccumulatorStackVariant::HalfKA(s) => update_and_evaluate_halfka(&network, pos, s),
        AccumulatorStackVariant::HalfKA_hm(s) => update_and_evaluate_halfka_hm(&network, pos, s),
        AccumulatorStackVariant::HalfKP(s) => update_and_evaluate_halfkp(&network, pos, s),
    }
}

/// アキュムレータを計算済みにする(評価値の計算はしない)
///
/// TTヒット時など、評価値はTTから取得するが、
/// 次のノードの差分更新のためにアキュムレータだけは計算しておく必要がある場合に使用。
/// YaneuraOu/Stockfish互換の動作を実現する。
///
/// `acc_cache` は LayerStacks 用 AccumulatorCaches(Finny Tables)。
pub fn ensure_accumulator_computed(
    pos: &Position,
    stack: &mut AccumulatorStackVariant,
    acc_cache: &mut Option<AccumulatorCacheLayerStacks>,
) {
    // NNUEがなければ何もしない
    let Some(network) = get_network() else {
        return;
    };

    // バリアントに応じてアキュムレータを更新(評価はしない)
    match stack {
        AccumulatorStackVariant::LayerStacks(s) => {
            update_accumulator_only_layer_stacks_cached(&network, pos, s, acc_cache);
        }
        AccumulatorStackVariant::HalfKA(s) => {
            update_accumulator_only_halfka(&network, pos, s);
        }
        AccumulatorStackVariant::HalfKA_hm(s) => {
            update_accumulator_only_halfka_hm(&network, pos, s);
        }
        AccumulatorStackVariant::HalfKP(s) => {
            update_accumulator_only_halfkp(&network, pos, s);
        }
    }
}

/// LayerStacks アキュムレータを更新のみ(キャッシュ対応版、評価なし)
#[inline]
fn update_accumulator_only_layer_stacks_cached(
    network: &NNUENetwork,
    pos: &Position,
    stack: &mut AccumulatorStackLayerStacks,
    acc_cache: &mut Option<AccumulatorCacheLayerStacks>,
) {
    let current_entry = stack.current();
    if current_entry.accumulator.computed_accumulation {
        count_already_computed!();
        return;
    }

    let mut updated = false;

    // 直前局面で差分更新を試行
    if let Some(prev_idx) = current_entry.previous {
        let prev_computed = stack.entry_at(prev_idx).accumulator.computed_accumulation;
        if prev_computed {
            let dirty_piece = stack.current().dirty_piece;
            let (prev_acc, current_acc) = stack.get_prev_and_current_accumulators(prev_idx);
            if let Some(cache) = acc_cache {
                network.update_accumulator_layer_stacks_with_cache(
                    pos,
                    &dirty_piece,
                    current_acc,
                    prev_acc,
                    cache,
                );
            } else {
                network.update_accumulator_layer_stacks(pos, &dirty_piece, current_acc, prev_acc);
            }
            count_update!();
            updated = true;
        }
    }

    // 失敗なら全計算(キャッシュ経由)
    if !updated {
        let acc = &mut stack.current_mut().accumulator;
        if let Some(cache) = acc_cache {
            network.refresh_accumulator_layer_stacks_with_cache(pos, acc, cache);
        } else {
            network.refresh_accumulator_layer_stacks(pos, acc);
        }
        count_refresh!();
    }
}

/// HalfKA_hm アキュムレータを更新のみ(評価なし)
#[inline]
fn update_accumulator_only_halfka_hm(
    network: &NNUENetwork,
    pos: &Position,
    stack: &mut HalfKA_hmStack,
) {
    if stack.is_current_computed() {
        count_already_computed!();
        return;
    }

    let mut updated = false;

    // 直前局面で差分更新を試行
    if let Some(prev_idx) = stack.current_previous()
        && stack.is_entry_computed(prev_idx)
    {
        let dirty = stack.current_dirty_piece();
        network.update_accumulator_halfka_hm(pos, &dirty, stack, prev_idx);
        count_update!();
        updated = true;
    }

    // 失敗なら全計算
    if !updated {
        network.refresh_accumulator_halfka_hm(pos, stack);
        count_refresh!();
    }
}

/// HalfKA アキュムレータを更新のみ(評価なし)
#[inline]
fn update_accumulator_only_halfka(network: &NNUENetwork, pos: &Position, stack: &mut HalfKAStack) {
    if stack.is_current_computed() {
        count_already_computed!();
        return;
    }

    let mut updated = false;

    // 直前局面で差分更新を試行
    if let Some(prev_idx) = stack.current_previous()
        && stack.is_entry_computed(prev_idx)
    {
        let dirty = stack.current_dirty_piece();
        network.update_accumulator_halfka(pos, &dirty, stack, prev_idx);
        count_update!();
        updated = true;
    }

    // 失敗なら全計算
    if !updated {
        network.refresh_accumulator_halfka(pos, stack);
        count_refresh!();
    }
}

/// HalfKP アキュムレータを更新のみ(評価なし)
#[inline]
fn update_accumulator_only_halfkp(network: &NNUENetwork, pos: &Position, stack: &mut HalfKPStack) {
    if stack.is_current_computed() {
        count_already_computed!();
        return;
    }

    let mut updated = false;

    // 直前局面で差分更新を試行
    if let Some(prev_idx) = stack.current_previous()
        && stack.is_entry_computed(prev_idx)
    {
        let dirty = stack.current_dirty_piece();
        network.update_accumulator_halfkp(pos, &dirty, stack, prev_idx);
        count_update!();
        updated = true;
    }

    // 失敗なら全計算
    if !updated {
        network.refresh_accumulator_halfkp(pos, stack);
        count_refresh!();
    }
}

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

    /// NNUEが初期化されていない場合のフォールバック動作をテスト
    #[test]
    fn test_evaluate_fallback() {
        let mut pos = Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();
        let mut stack = AccumulatorStackVariant::new_default();

        // NNUEが初期化されていない場合はフォールバック
        let value = evaluate_dispatch(&pos, &mut stack, &mut None);

        // フォールバック評価が動作することを確認
        assert!(value.raw().abs() < 1000);
    }

    /// AccumulatorStackVariant を使った評価のテスト
    /// NNUEが未初期化でもフォールバックで評価が動作することを確認
    #[test]
    fn test_accumulator_stack_variant_fallback() {
        let mut pos = Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();
        let mut stack = AccumulatorStackVariant::new_default();

        // 1回目の evaluate: NNUEが未初期化なのでフォールバック評価
        let value1 = evaluate_dispatch(&pos, &mut stack, &mut None);

        // 2回目も動作することを確認
        let value2 = evaluate_dispatch(&pos, &mut stack, &mut None);

        // フォールバックの駒得評価は手番に依存して符号が変わる可能性があるが、
        // ここでは「評価が成功した」ことのみ検証する。
        let _ = (value1, value2);
    }

    /// NNUENetwork のアーキテクチャ自動検出テスト
    ///
    /// 外部NNUEファイルが必要なため通常はスキップ。
    /// 実行方法: `NNUE_TEST_FILE=/path/to/file.nnue cargo test test_nnue_network_auto_detect_layer_stacks -- --ignored`
    ///
    /// テスト結果 (epoch82.nnue):
    /// - LayerStacks として正しく認識される
    /// - 評価値: 0 (学習初期のモデル)
    #[test]
    #[ignore]
    fn test_nnue_network_auto_detect_layer_stacks() {
        let path = std::env::var("NNUE_TEST_FILE")
            .unwrap_or_else(|_| "/path/to/your/layer_stacks.nnue".to_string());
        let network = match NNUENetwork::load(path) {
            Ok(n) => n,
            Err(e) => {
                eprintln!("Skipping test: {e}");
                return;
            }
        };

        // LayerStacks として認識されることを確認
        assert!(network.is_layer_stacks(), "epoch82.nnue should be detected as LayerStacks");
        assert_eq!(network.architecture_name(), "LayerStacks");

        // LayerStacks 用の評価が動作することを確認
        let mut pos = crate::position::Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        let mut acc = crate::nnue::AccumulatorLayerStacks::new();
        network.refresh_accumulator_layer_stacks(&pos, &mut acc);

        let value = network.evaluate_layer_stacks(&pos, &acc);
        eprintln!("LayerStacks evaluate: {}", value.raw());

        // 評価値が妥当な範囲内
        assert!(value.raw().abs() < 1000);
    }

    /// detect_format のファイルサイズベース検出テスト
    ///
    /// AobaNNUE.bin のようにヘッダーが不正確なファイルでも
    /// ファイルサイズから正確なアーキテクチャを検出できることを確認する。
    ///
    /// 実行方法:
    /// ```bash
    /// NNUE_AOBA_FILE=/path/to/AobaNNUE.bin cargo test test_detect_format_aoba -- --ignored --nocapture
    /// ```
    #[test]
    #[ignore]
    fn test_detect_format_aoba() {
        let path = std::env::var("NNUE_AOBA_FILE").unwrap_or_else(|_| "AobaNNUE.bin".to_string());
        let bytes = match std::fs::read(&path) {
            Ok(b) => b,
            Err(e) => {
                eprintln!("Skipping test: {e}");
                return;
            }
        };

        let file_size = bytes.len() as u64;
        let info = detect_format(&bytes, file_size).expect("Failed to detect format");

        eprintln!("File: {path}");
        eprintln!("Architecture: {}", info.architecture);
        eprintln!(
            "L1: {}, L2: {}, L3: {}",
            info.l1_dimension, info.l2_dimension, info.l3_dimension
        );
        eprintln!("Activation: {}", info.activation);
        eprintln!("Arch string (header): {}", info.arch_string);

        // AobaNNUE.bin はヘッダーで 256 を主張するが、実際は 768-16-64
        assert_eq!(
            info.architecture, "HalfKP768",
            "Should detect HalfKP768 from file size, not HalfKP256 from header"
        );
        assert_eq!(info.l1_dimension, 768, "L1 should be 768, not 256 from header");
        assert_eq!(info.l2_dimension, 16, "L2 should be 16");
        assert_eq!(info.l3_dimension, 64, "L3 should be 64");
        // ヘッダーが不正確であることを確認(256 を主張している)
        assert!(
            info.arch_string.contains("256"),
            "Header should claim 256, but file size detection should override it"
        );
    }

    /// detect_format のフォールバックテスト
    ///
    /// ファイルサイズベースの検出が失敗した場合に、
    /// ヘッダーのパース結果にフォールバックすることを確認する。
    #[test]
    fn test_detect_format_fallback_to_header() {
        // 架空のファイルサイズ(既知のアーキテクチャと一致しない)
        let unknown_file_size = 12345678u64;

        // 有効なヘッダーを持つバイト列を作成
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&NNUE_VERSION_HALFKA.to_le_bytes()); // version
        bytes.extend_from_slice(&0u32.to_le_bytes()); // hash

        let arch_str = "Features=HalfKA_hm[73305->512x2],l2=8,l3=96";
        let arch_len = arch_str.len() as u32;
        bytes.extend_from_slice(&arch_len.to_le_bytes());
        bytes.extend_from_slice(arch_str.as_bytes());

        let info =
            detect_format(&bytes, unknown_file_size).expect("Should fallback to header parsing");

        // ヘッダーからパースした値が使われることを確認
        assert_eq!(info.architecture, "HalfKA_hm512");
        assert_eq!(info.l1_dimension, 512);
        assert_eq!(info.l2_dimension, 8);
        assert_eq!(info.l3_dimension, 96);
    }

    /// detect_format のエラーハンドリングテスト
    #[test]
    fn test_detect_format_error_cases() {
        // ケース1: ファイルサイズが小さすぎる
        let bytes = vec![0u8; 5];
        let result = detect_format(&bytes, 5);
        assert!(result.is_err(), "Should fail for too small file");
        assert!(
            result.unwrap_err().to_string().contains("too small"),
            "Error message should mention 'too small'"
        );

        // ケース2: arch_len = 0(不正)
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&NNUE_VERSION.to_le_bytes());
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&0u32.to_le_bytes()); // arch_len = 0
        let result = detect_format(&bytes, 100);
        assert!(result.is_err(), "Should fail for arch_len = 0");
        assert!(
            result.unwrap_err().to_string().contains("Invalid arch string length"),
            "Error message should mention invalid arch string length"
        );

        // ケース3: arch_len が MAX_ARCH_LEN を超える
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&NNUE_VERSION.to_le_bytes());
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&(MAX_ARCH_LEN as u32 + 1).to_le_bytes());
        let result = detect_format(&bytes, 100);
        assert!(result.is_err(), "Should fail for arch_len > MAX_ARCH_LEN");

        // ケース4: バッファが arch_len 分のデータを含まない
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&NNUE_VERSION.to_le_bytes());
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&100u32.to_le_bytes()); // arch_len = 100
        // bytes は 12 バイトのみ、arch_str 用のデータがない
        let result = detect_format(&bytes, 1000);
        assert!(result.is_err(), "Should fail when buffer is too small for arch_str");

        // ケース5: 不正なバージョン
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&0xDEADBEEFu32.to_le_bytes());
        bytes.extend_from_slice(&[0u8; 100]);
        let result = detect_format(&bytes, 112);
        assert!(result.is_err(), "Should fail for unknown version");
        assert!(
            result.unwrap_err().to_string().contains("Unknown NNUE version"),
            "Error message should mention unknown version"
        );
    }

    /// parse_fv_scale_from_arch のユニットテスト
    #[test]
    fn test_parse_fv_scale_from_arch() {
        // bullet-shogi 形式の arch_str
        assert_eq!(
            parse_fv_scale_from_arch(
                "Features=HalfKA_hm^[73305->256x2]-SCReLU,fv_scale=13,qa=127,qb=64,scale=600"
            ),
            Some(13)
        );
        assert_eq!(
            parse_fv_scale_from_arch(
                "Features=HalfKA_hm^[73305->512x2]-SCReLU,fv_scale=20,qa=127,qb=64,scale=400"
            ),
            Some(20)
        );
        assert_eq!(
            parse_fv_scale_from_arch(
                "Features=HalfKA_hm^[73305->1024x2]-SCReLU,fv_scale=16,qa=127,qb=64,scale=508"
            ),
            Some(16)
        );

        // fv_scale が含まれていない従来形式
        assert_eq!(parse_fv_scale_from_arch("Features=HalfKP[125388->256x2]"), None);
        assert_eq!(parse_fv_scale_from_arch("Features=HalfKA_hm^[73305->512x2]"), None);

        // 空文字列
        assert_eq!(parse_fv_scale_from_arch(""), None);

        // 不正な fv_scale 値(文字列)
        assert_eq!(
            parse_fv_scale_from_arch("Features=HalfKA_hm^[73305->256x2],fv_scale=abc"),
            None
        );
    }

    /// parse_fv_scale_from_arch の境界値・エラーケーステスト
    #[test]
    fn test_parse_fv_scale_edge_cases() {
        // 境界値(許容範囲内)
        assert_eq!(parse_fv_scale_from_arch("fv_scale=1"), Some(1));
        assert_eq!(parse_fv_scale_from_arch("fv_scale=128"), Some(128));
        assert_eq!(parse_fv_scale_from_arch("fv_scale=64"), Some(64));

        // 境界値(範囲外 - ゼロ除算防止)
        assert_eq!(parse_fv_scale_from_arch("fv_scale=0"), None);
        assert_eq!(parse_fv_scale_from_arch("fv_scale=129"), None);

        // 不正な値(負数)
        assert_eq!(parse_fv_scale_from_arch("fv_scale=-1"), None);
        assert_eq!(parse_fv_scale_from_arch("fv_scale=-100"), None);

        // 不正な値(極端に大きい値)
        assert_eq!(parse_fv_scale_from_arch("fv_scale=99999"), None);
        assert_eq!(parse_fv_scale_from_arch("fv_scale=2147483647"), None);

        // ホワイトスペースを含む(パース失敗を期待)
        assert_eq!(parse_fv_scale_from_arch("fv_scale= 16"), None);
        assert_eq!(parse_fv_scale_from_arch("fv_scale=16 "), None);

        // 複数の fv_scale がある場合(最初のものが使用される)
        assert_eq!(parse_fv_scale_from_arch("fv_scale=10,fv_scale=20"), Some(10));

        // fv_scale= の後に何もない
        assert_eq!(parse_fv_scale_from_arch("fv_scale="), None);

        // 小数点を含む(パース失敗を期待)
        assert_eq!(parse_fv_scale_from_arch("fv_scale=16.5"), None);

        // プレフィックスが部分一致する場合(マッチしない)
        assert_eq!(parse_fv_scale_from_arch("my_fv_scale=16"), None);
        assert_eq!(parse_fv_scale_from_arch("fv_scale_v2=16"), None);
    }

    #[test]
    fn test_parse_layer_stack_bucket_mode() {
        assert_eq!(
            parse_layer_stack_bucket_mode("kingrank9"),
            Some(LayerStackBucketMode::KingRank9)
        );
        assert_eq!(parse_layer_stack_bucket_mode("ply9"), Some(LayerStackBucketMode::Ply9));
        assert_eq!(parse_layer_stack_bucket_mode("PLY9"), Some(LayerStackBucketMode::Ply9));
        assert_eq!(
            parse_layer_stack_bucket_mode("progress8"),
            Some(LayerStackBucketMode::Progress8)
        );
        assert_eq!(
            parse_layer_stack_bucket_mode("progress8gikou"),
            Some(LayerStackBucketMode::Progress8Gikou)
        );
        assert_eq!(
            parse_layer_stack_bucket_mode("progress8kpabs"),
            Some(LayerStackBucketMode::Progress8KPAbs)
        );
        assert_eq!(
            parse_layer_stack_bucket_mode(" kingrank9 "),
            Some(LayerStackBucketMode::KingRank9)
        );
        assert_eq!(parse_layer_stack_bucket_mode("unknown"), None);
    }

    #[test]
    fn test_parse_layer_stack_ply_bounds_csv() {
        assert_eq!(
            parse_layer_stack_ply_bounds_csv("30,44,58,72,86,100,116,138").unwrap(),
            [30, 44, 58, 72, 86, 100, 116, 138]
        );
        assert_eq!(
            parse_layer_stack_ply_bounds_csv(" 30, 44, 58, 72, 86, 100, 116, 138 ").unwrap(),
            [30, 44, 58, 72, 86, 100, 116, 138]
        );

        assert!(parse_layer_stack_ply_bounds_csv("30,44,58").is_err());
        assert!(parse_layer_stack_ply_bounds_csv("30,44,58,72,86,100,116,abc").is_err());
    }

    #[test]
    fn test_compute_layer_stack_ply9_bucket_index() {
        let bounds = LAYER_STACK_PLY9_DEFAULT_BOUNDS;
        assert_eq!(compute_layer_stack_ply9_bucket_index(0, bounds), 0);
        assert_eq!(compute_layer_stack_ply9_bucket_index(30, bounds), 0);
        assert_eq!(compute_layer_stack_ply9_bucket_index(31, bounds), 1);
        assert_eq!(compute_layer_stack_ply9_bucket_index(138, bounds), 7);
        assert_eq!(compute_layer_stack_ply9_bucket_index(139, bounds), 8);
        assert_eq!(compute_layer_stack_ply9_bucket_index(400, bounds), 8);
        assert_eq!(compute_layer_stack_ply9_bucket_index(-5, bounds), 0);
    }

    #[test]
    fn test_compute_layer_stack_progress8_bucket_index_range() {
        let mut pos = Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        let coeff = LayerStackProgressCoeff::default();
        let b = compute_layer_stack_progress8_bucket_index(&pos, pos.side_to_move(), coeff);
        assert!(b <= 7, "progress8 bucket must be in 0..=7, got {b}");
    }

    #[test]
    fn test_compute_layer_stack_progress8gikou_bucket_index_range() {
        let mut pos = Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        let coeff = LayerStackProgressCoeffGikouLite::default();
        let b = compute_layer_stack_progress8gikou_bucket_index(&pos, pos.side_to_move(), coeff);
        assert!(b <= 7, "progress8gikou bucket must be in 0..=7, got {b}");
    }

    #[test]
    fn test_compute_layer_stack_progress8kpabs_bucket_index_range() {
        let mut pos = Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        let weights = vec![0.0f32; SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS];
        let b = compute_layer_stack_progress8kpabs_bucket_index(&pos, pos.side_to_move(), &weights);
        assert_eq!(b, 4, "zero-weight progress8kpabs should map to the middle bucket");
    }

    #[test]
    fn test_progress_bucket_thresholds_match_sigmoid() {
        // テーブル引きが元の sigmoid 方式と一致することを確認
        let sigmoid_bucket = |sum: f32| -> usize {
            let p = (1.0 / (1.0 + (-sum).exp())).clamp(0.0, 1.0);
            let raw = (p * SHOGI_PROGRESS8_NUM_BUCKETS as f32).floor() as i32;
            raw.clamp(0, (SHOGI_PROGRESS8_NUM_BUCKETS - 1) as i32) as usize
        };
        let threshold_bucket = |sum: f32| -> usize {
            PROGRESS_BUCKET_THRESHOLDS
                .iter()
                .filter(|&&t| sum >= t)
                .count()
                .min(SHOGI_PROGRESS8_NUM_BUCKETS - 1)
        };

        // 閾値から離れた値では完全一致すべき
        for &sum in &[
            -10.0, -5.0, -3.0, -2.5, -1.5, -0.8, -0.3, 0.0, 0.3, 0.8, 1.5, 2.5, 3.0, 5.0, 10.0,
        ] {
            assert_eq!(sigmoid_bucket(sum), threshold_bucket(sum), "mismatch at sum={sum}");
        }
    }

    #[test]
    fn test_progress8kpabs_diff_update() {
        use crate::types::Move;

        // ランダムな重みを生成(固定シード)
        let mut weights = vec![0.0f32; SHOGI_PROGRESS_KP_ABS_NUM_WEIGHTS];
        let mut rng: u64 = 12345;
        for w in weights.iter_mut() {
            // 簡易 xorshift
            rng ^= rng << 13;
            rng ^= rng >> 7;
            rng ^= rng << 17;
            *w = ((rng as i64 % 1000) as f32) / 1000.0;
        }

        let mut pos = Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        // 初期局面での全駒スキャン sum
        let sum0 = compute_progress8kpabs_sum(&pos, &weights);

        // いくつかの手を実行して差分更新と全計算を比較
        let moves_usi = [
            "7g7f", "3c3d", "2g2f", "8c8d", "2f2e", "8d8e", "6i7h", "4a3b",
        ];
        let mut prev_sum = sum0;

        for &mv_str in &moves_usi {
            let mv = Move::from_usi(mv_str).expect("valid move");
            let gives_check = pos.gives_check(mv);
            let dirty = pos.do_move(mv, gives_check);

            // 全駒スキャンによる正解値
            let expected_sum = compute_progress8kpabs_sum(&pos, &weights);
            let expected_bucket = progress_sum_to_bucket(expected_sum);

            if dirty.king_moved[0] || dirty.king_moved[1] {
                // 玉が動いた場合は差分更新不可(全計算にフォールバック)
                prev_sum = expected_sum;
            } else {
                // 差分更新
                let sq_bk = pos.king_square(Color::Black).index();
                let sq_wk = pos.king_square(Color::White).inverse().index();
                let diff_sum =
                    update_progress8kpabs_sum_diff(prev_sum, &dirty, sq_bk, sq_wk, &weights);
                let diff_bucket = progress_sum_to_bucket(diff_sum);

                assert!(
                    (diff_sum - expected_sum).abs() < 1e-5,
                    "sum mismatch after {mv_str}: diff={diff_sum}, expected={expected_sum}"
                );
                assert_eq!(diff_bucket, expected_bucket, "bucket mismatch after {mv_str}");

                prev_sum = diff_sum;
            }
        }
    }

    /// HalfKP 768x2-16-64 ファイルの読み込みテスト
    ///
    /// nnue-pytorch がハードコードした不正確なヘッダーを持つファイルを
    /// ファイルサイズベースの自動検出で正しく読み込めることを確認する。
    ///
    /// 実行方法:
    /// ```bash
    /// cargo test test_nnue_halfkp_768_auto_detect -- --ignored
    /// ```
    #[test]
    #[ignore]
    fn test_nnue_halfkp_768_auto_detect() {
        // ワークスペースルートからの相対パス
        let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(|p| p.parent())
            .expect("Failed to find workspace root");
        let default_path = workspace_root
            .join("eval/halfkp_768x2-16-64_crelu/AobaNNUE_HalfKP_768x2_16_64_FV_SCALE_40.bin");
        let path = std::env::var("NNUE_HALFKP_768_FILE")
            .unwrap_or_else(|_| default_path.display().to_string());

        let network = match NNUENetwork::load(&path) {
            Ok(n) => n,
            Err(e) => {
                eprintln!("Skipping test: {e}");
                return;
            }
        };

        // HalfKP として認識されることを確認
        assert!(network.is_halfkp(), "File should be detected as HalfKP");

        // L1=768 が検出されることを確認
        assert_eq!(network.l1_size(), 768, "L1 should be 768");

        // アーキテクチャ仕様を確認
        let spec = network.architecture_spec();
        assert_eq!(spec.l1, 768, "spec.l1 should be 768");
        assert_eq!(spec.l2, 16, "spec.l2 should be 16");
        assert_eq!(spec.l3, 64, "spec.l3 should be 64");

        eprintln!("Successfully loaded HalfKP 768x2-16-64 network");
        eprintln!("Architecture name: {}", network.architecture_name());

        // HalfKP 用の評価が動作することを確認
        let mut pos = crate::position::Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        // HalfKPStack を作成して評価
        use crate::nnue::halfkp::HalfKPStack;
        let mut stack = HalfKPStack::from_network(match &network {
            NNUENetwork::HalfKP(net) => net,
            _ => unreachable!(),
        });

        network.refresh_accumulator_halfkp(&pos, &mut stack);
        let value = network.evaluate_halfkp(&pos, &stack);

        eprintln!("HalfKP 768 evaluate: {}", value.raw());

        // 評価値が妥当な範囲内
        assert!(value.raw().abs() < 10000, "Evaluation {} is out of expected range", value.raw());
    }

    /// HalfKA_hm 256x2-32-32 ファイルの読み込みテスト
    ///
    /// nnue-pytorch 形式のファイルを FT hash を使って正しく読み込めることを確認する。
    ///
    /// 実行方法:
    /// ```bash
    /// cargo test test_nnue_halfka_hm_256_auto_detect -- --ignored
    /// ```
    #[test]
    #[ignore]
    fn test_nnue_halfka_hm_256_auto_detect() {
        // ワークスペースルートからの相対パス
        let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(|p| p.parent())
            .expect("Failed to find workspace root");
        let default_path = workspace_root.join("eval/halfka_hm_256x2-32-32_crelu/v28_epoch65.nnue");
        let path = std::env::var("NNUE_HALFKA_HM_256_FILE")
            .unwrap_or_else(|_| default_path.display().to_string());

        let network = match NNUENetwork::load(&path) {
            Ok(n) => n,
            Err(e) => {
                eprintln!("Skipping test: {e}");
                return;
            }
        };

        // HalfKA_hm として認識されることを確認
        assert!(network.is_halfka_hm(), "File should be detected as HalfKA_hm");

        // L1=256 が検出されることを確認
        assert_eq!(network.l1_size(), 256, "L1 should be 256");

        // アーキテクチャ仕様を確認
        let spec = network.architecture_spec();
        assert_eq!(spec.l1, 256, "spec.l1 should be 256");
        assert_eq!(spec.l2, 32, "spec.l2 should be 32");
        assert_eq!(spec.l3, 32, "spec.l3 should be 32");

        eprintln!("Successfully loaded HalfKA_hm 256x2-32-32 network");
        eprintln!("Architecture name: {}", network.architecture_name());

        // HalfKA_hm 用の評価が動作することを確認
        let mut pos = crate::position::Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        // HalfKA_hmStack を作成して評価
        use crate::nnue::halfka_hm::HalfKA_hmStack;
        let mut stack = HalfKA_hmStack::from_network(match &network {
            NNUENetwork::HalfKA_hm(net) => net,
            _ => unreachable!(),
        });

        network.refresh_accumulator_halfka_hm(&pos, &mut stack);
        let value = network.evaluate_halfka_hm(&pos, &stack);

        eprintln!("HalfKA_hm 256 evaluate: {}", value.raw());

        // 評価値が妥当な範囲内
        assert!(value.raw().abs() < 10000, "Evaluation {} is out of expected range", value.raw());
    }

    /// HalfKA_hm 1024x2-8-96 ファイルの読み込みテスト
    ///
    /// 実行方法:
    /// ```bash
    /// cargo test test_nnue_halfka_hm_1024_auto_detect -- --ignored
    /// ```
    #[test]
    #[ignore]
    fn test_nnue_halfka_hm_1024_auto_detect() {
        // ワークスペースルートからの相対パス
        let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(|p| p.parent())
            .expect("Failed to find workspace root");
        let default_path = workspace_root.join("eval/halfka_hm_1024x2-8-96_crelu/epoch20_v2.nnue");
        let path = std::env::var("NNUE_HALFKA_HM_1024_FILE")
            .unwrap_or_else(|_| default_path.display().to_string());

        let network = match NNUENetwork::load(&path) {
            Ok(n) => n,
            Err(e) => {
                eprintln!("Skipping test: {e}");
                return;
            }
        };

        // HalfKA_hm として認識されることを確認
        assert!(network.is_halfka_hm(), "File should be detected as HalfKA_hm");

        // L1=1024 が検出されることを確認
        assert_eq!(network.l1_size(), 1024, "L1 should be 1024");

        // アーキテクチャ仕様を確認
        let spec = network.architecture_spec();
        assert_eq!(spec.l1, 1024, "spec.l1 should be 1024");
        assert_eq!(spec.l2, 8, "spec.l2 should be 8");
        assert_eq!(spec.l3, 96, "spec.l3 should be 96");

        eprintln!("Successfully loaded HalfKA_hm 1024x2-8-96 network");
        eprintln!("Architecture name: {}", network.architecture_name());

        // HalfKA_hm 用の評価が動作することを確認
        let mut pos = crate::position::Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        // HalfKA_hmStack を作成して評価
        use crate::nnue::halfka_hm::HalfKA_hmStack;
        let mut stack = HalfKA_hmStack::from_network(match &network {
            NNUENetwork::HalfKA_hm(net) => net,
            _ => unreachable!(),
        });

        network.refresh_accumulator_halfka_hm(&pos, &mut stack);
        let value = network.evaluate_halfka_hm(&pos, &stack);

        eprintln!("HalfKA_hm 1024 evaluate: {}", value.raw());

        // 評価値が妥当な範囲内
        assert!(value.raw().abs() < 10000, "Evaluation {} is out of expected range", value.raw());
    }

    /// HalfKP 256x2-32-32 ファイル (suisho5.bin) の読み込みテスト
    ///
    /// ファイルサイズベースの検出で正しく読み込めることを確認する。
    ///
    /// 実行方法:
    /// ```bash
    /// cargo test test_nnue_halfkp_256_suisho5 -- --ignored
    /// ```
    #[test]
    #[ignore]
    fn test_nnue_halfkp_256_suisho5() {
        // ワークスペースルートからの相対パス
        let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(|p| p.parent())
            .expect("Failed to find workspace root");
        let default_path = workspace_root.join("eval/halfkp_256x2-32-32_crelu/suisho5.bin");
        let path = std::env::var("NNUE_HALFKP_256_FILE")
            .unwrap_or_else(|_| default_path.display().to_string());

        let network = match NNUENetwork::load(&path) {
            Ok(n) => n,
            Err(e) => {
                eprintln!("Skipping test: {e}");
                return;
            }
        };

        // HalfKP として認識されることを確認
        assert!(network.is_halfkp(), "File should be detected as HalfKP");

        // L1=256 が検出されることを確認
        assert_eq!(network.l1_size(), 256, "L1 should be 256");

        // アーキテクチャ仕様を確認
        let spec = network.architecture_spec();
        assert_eq!(spec.l1, 256, "spec.l1 should be 256");
        assert_eq!(spec.l2, 32, "spec.l2 should be 32");
        assert_eq!(spec.l3, 32, "spec.l3 should be 32");

        eprintln!("Successfully loaded HalfKP 256x2-32-32 network (suisho5)");
        eprintln!("Architecture name: {}", network.architecture_name());

        // HalfKP 用の評価が動作することを確認
        let mut pos = crate::position::Position::new();
        pos.set_sfen(SFEN_HIRATE).unwrap();

        // HalfKPStack を作成して評価
        use crate::nnue::halfkp::HalfKPStack;
        let mut stack = HalfKPStack::from_network(match &network {
            NNUENetwork::HalfKP(net) => net,
            _ => unreachable!(),
        });

        network.refresh_accumulator_halfkp(&pos, &mut stack);
        let value = network.evaluate_halfkp(&pos, &stack);

        eprintln!("HalfKP 256 evaluate: {}", value.raw());

        // 評価値が妥当な範囲内
        assert!(value.raw().abs() < 10000, "Evaluation {} is out of expected range", value.raw());
    }
}