[libcxx-commits] [libcxx] 66d049c - [libc++] Fix std::mt19937 seeding with std::seed_seq under vectorized ABI (#209860)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jul 30 08:08:28 PDT 2026
Author: Kyungtak Woo
Date: 2026-07-30T09:08:22-06:00
New Revision: 66d049cdad21a7ad387f358f7593bee91221bd39
URL: https://github.com/llvm/llvm-project/commit/66d049cdad21a7ad387f358f7593bee91221bd39
DIFF: https://github.com/llvm/llvm-project/commit/66d049cdad21a7ad387f358f7593bee91221bd39.diff
LOG: [libc++] Fix std::mt19937 seeding with std::seed_seq under vectorized ABI (#209860)
This PR fixes a bug in `std::mersenne_twister_engine::seed(_Sseq&)`
where the initialization twist is bypassed when the vectorized engine
optimization (`_LIBCPP_ABI_VECTORIZED_MERSENNE_TWISTER_ENGINE`) is
enabled.
In the vectorized implementation introduced in #206423, the engine state
must be pre-twisted during initialization by calling
`__update_all_states()` at the end of the `seed` functions.
However, in `seed(_Sseq& __q)`, the all-zero state check contains an
early `return;` when a non-zero state is verified. This early return
bypasses the trailing `__update_all_states()` call. Consequently, the
generator is initialized with raw, untwisted seed values (tempered)
rather than the standard twisted values.
This only manifests when compiled with unstable ABI settings
(`_LIBCPP_ABI_VERSION >= 2`) which enables the vectorized
implementation.
I replaced the early `return;` inside the all-zero state verification
block with a loop break, and conditionalized the zero-state fix using
the loop index `__i`, ensuring `__update_all_states()` is always
executed at the end of the function. Added regression tests to
`seed_sseq.pass.cpp` asserting the exact deterministic outputs for both
32-bit and 64-bit engines.
Added:
Modified:
libcxx/include/__random/mersenne_twister_engine.h
libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/ctor_sseq.pass.cpp
libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/seed_sseq.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/__random/mersenne_twister_engine.h b/libcxx/include/__random/mersenne_twister_engine.h
index 8e49b3b267aa8..4219ab8701c32 100644
--- a/libcxx/include/__random/mersenne_twister_engine.h
+++ b/libcxx/include/__random/mersenne_twister_engine.h
@@ -201,10 +201,12 @@ class mersenne_twister_engine {
const result_type __mask = __r == _Dt ? result_type(~0) : (result_type(1) << __r) - result_type(1);
__i_ = 0;
if ((__x_[0] & ~__mask) == 0) {
- for (size_t __i = 1; __i < __n; ++__i)
+ size_t __i = 1;
+ for (; __i < __n; ++__i)
if (__x_[__i] != 0)
- return;
- __x_[0] = result_type(1) << (__w - 1);
+ break;
+ if (__i == __n)
+ __x_[0] = result_type(1) << (__w - 1);
}
#ifdef _LIBCPP_ABI_VECTORIZED_MERSENNE_TWISTER_ENGINE
__update_all_states();
diff --git a/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/ctor_sseq.pass.cpp b/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/ctor_sseq.pass.cpp
index 24832330720e4..f49942dc9dcde 100644
--- a/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/ctor_sseq.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/ctor_sseq.pass.cpp
@@ -184,69 +184,69 @@ void test1() {
"2447397608 712235937 3332826819 2676980703 "
"4019468871 1952389952 1202638254 3625447051"
#else
- "358595400 4166558815 2016177162 3414480257 4027494649 3722317195 1190532340 3212207148 3537847251 389019999 "
- "1098708832 3277907415 1946784350 3608286140 2091419822 2227407035 2229110723 1825348377 1276269279 314337202 "
- "3182935337 1313150029 3118776508 3707918501 1900972958 4054820954 3973178248 906260237 1403942218 3139931556 "
- "2807126524 3940936448 1316345796 631296613 2268418920 2914000794 3760588399 3226216036 880155129 4183611084 "
- "211541083 3755352858 1331383234 3036493096 937478630 2092170412 777784402 93392729 3644029210 1681392086 "
- "2427001226 3143870332 3703581502 2017505388 1706274541 1049329728 2452031492 3437261233 2581204087 1700889875 "
- "1652573881 2127047692 3778506964 1960741508 2739602360 3395905609 2123355622 3041272975 784200748 3558951522 "
- "1002787860 4063320888 1587315560 4042698976 659183308 3082256417 2808969567 2361418535 3468698782 750700970 "
- "2991209851 3581521382 962022878 2518967363 1476525873 3865977235 2128790058 2380326689 1396773405 312559410 "
- "1370621899 1154499924 2963101919 2182689761 2071851902 1661288848 2411351341 1362764020 1289894483 1951662807 "
- "701821506 552267185 2356648449 3949188503 1748307081 87795201 3718396254 4112205936 2819888864 73923733 "
- "2800033151 839258139 3801779069 3105962436 2111266436 1772784466 3692264298 4148810953 3147390749 3537518553 "
- "1695044978 1430225842 1252346204 3465285434 3970017763 2920658411 2805151132 290569815 3802301355 1493420394 "
- "1943029276 1667143611 1049665988 1710824905 220168517 3997946231 1014582791 4244598752 1147604069 2533886627 "
- "598679964 761521020 431779255 3745982038 768658283 3598262505 1765664789 279538641 715144305 2371628432 "
- "2655860083 1759010423 3568452003 1910107098 2801429529 3924547532 3862672436 3933725740 1764550618 130893617 "
- "1460692387 4135312761 2075529299 2880227367 944557368 4166665482 2627749235 3732013815 1595900818 1553312393 "
- "3529311831 3531462424 2431328342 4075369692 1609967709 3704537555 2067297464 397140475 920618678 2840795964 "
- "4202512837 1286017648 7035910 1057207826 2325188262 191593698 3697383848 3029712831 2073681914 163454681 "
- "1329637200 290077398 287239431 4205081522 1233889024 167173087 3267660257 3406068803 2382354609 1680046927 "
- "125183503 3559536309 3208900974 2912148541 2882879316 1937001086 2919729069 892928802 4141691387 2507406586 "
- "855548593 3418647837 4035646154 2410275591 248715645 3180757482 1880770722 362912336 2964920095 2319904154 "
- "1493655850 4240733030 1834485846 1696040454 3329457927 1865824694 847759208 1587231623 3757294772 1161601118 "
- "3630323833 3007722125 3726418007 2124238171 1205345 172659797 3040354211 885213338 1857049013 447922412 "
- "719906299 1370059380 1922204800 3960090489 1658822644 1529626863 1565927273 3537718771 2733237258 2180221377 "
- "921910745 2144937687 1727603895 1315635304 4023867791 2401834107 808854185 2408824497 343935326 185237544 "
- "746732759 2641236122 4283215329 743609415 1134726665 3892851319 1302851263 3473445597 1326817414 2702766508 "
- "1943179285 4025685468 932896770 199392138 2787362875 3450501893 3351567147 2461286528 2227605848 2993751114 "
- "3988215720 1320573368 2866560199 4153194990 3007120042 3260751955 3171763740 2111121243 3962825228 102681859 "
- "3368179132 802089147 4029273561 424939445 4178414761 2592125109 1960801088 2967746406 310701990 2364200202 "
- "1320507009 3474372480 784693947 2952246664 1891935330 2048385105 3530082191 3238151038 3293189141 1316053288 "
- "2087004409 740799958 1187748554 3607767334 1190185990 1408429481 657134359 221834425 3907725865 1068016389 "
- "1402423875 2598612116 2046886300 2345022518 1196081924 357783981 4013683598 463491626 3269206482 3332444286 "
- "886491955 2257342866 475911113 833576299 2893727564 2866985145 1413365115 2995166393 1486060436 161205225 "
- "3181728373 3056027137 2040371876 2182305146 3028448628 2214316977 1266227021 876938740 276477469 752158077 "
- "2182179045 1381698878 3424557652 666674427 968327842 2534296575 265105940 961112540 2641188117 2319139814 "
- "1750453329 3450138343 678025317 1477566458 3773796420 2933993832 3326042905 4084805260 444182455 255333481 "
- "785163068 2321290820 2893603234 3005520266 541104079 1383277090 2770755666 3764627833 583371929 2864949033 "
- "1487681116 1811788361 240329486 3094213377 958509875 2564379085 1636995945 2070894127 2139004232 1747850055 "
- "3841512327 3325011872 1161622604 639182193 3533652535 4022667522 761048999 3337743670 254221568 2784956233 "
- "2990252814 4207922787 275707208 261819597 2071467265 4034945770 1999813410 3038921100 2200194573 1328217451 "
- "2440612380 3862293692 2733976812 2750523058 2920082515 3809044908 4285231753 3131963297 3481602724 1396460739 "
- "2011767965 2481047764 2958452120 3044188618 2217236658 3448605566 757716104 1818279145 2641228144 1312649639 "
- "1194087684 3845229729 1747658356 874418803 1956637337 268670179 2083040240 2577671381 3375334655 2587828868 "
- "1383012799 3583445685 2594576715 3282337104 4257972751 3440488071 3129180313 1830891395 1594931092 2680778339 "
- "3984026324 1102770400 2315820258 1263467048 1133254110 2400676748 2251795328 1036154092 3313541051 2277356560 "
- "1477696003 1417117088 3968537402 1404882202 2011058180 4114080985 1727459502 4100235708 2334509310 2829432554 "
- "377936301 1519324520 3252826644 1193335837 1929125820 2165344238 4160556243 223340988 670907625 1485396519 "
- "936389509 3813712964 2706450987 3132506320 875886515 557088991 2854916639 2955496008 2881696287 265169077 "
- "3239923698 3649366121 4072165960 1233904959 225406526 1767368993 1894882500 2296582180 339255168 83200939 "
- "2958376148 4100205346 1991250823 3806183082 2691709980 2642354997 3024056146 1681065839 3438299684 1638853652 "
- "362567001 2307868899 988801086 1342833399 2303298376 1500039911 765489391 4080464497 4155444368 980472018 "
- "2026981853 3460406995 391970367 667377014 4177754853 2657468948 3560690175 3030464357 2948380657 1208800977 "
- "2316451404 4001932203 1977856863 4265848271 3116200050 3037586215 1335232764 930230766 1026089249 2482219415 "
- "2613853154 1854543497 2909555107 3862874043 2609355500 907364682 383900687 358164223 232347546 2536276737 "
- "3118482806 1254103998 2357334077 1204777304 1996643329 4046232717 2570520290 3173323380 1201411457 2361883023 "
- "806087062 2984143714 2355127569 864220085 1787696713 1182652984 4200065581 100722519 2380458669 2429592313 "
- "2618338302 1236529564 1747130196 3711661325 1114068102 510380433 93703089 2277774664 3220741441 1577998569 "
- "2816701900 4206763045 2495239107 4080390459 1307072677 20360728 1468385549 96049834 3630657447 2809517346 "
- "3396111678 3043831060 673178359 4256729562 1755211210 1969834535 498315110 3717726302 1544859987 2239930949 "
- "1595372585 294525219 3961637067 3591840665 3324896933 2300077772 721255886 4197934760 1468866696 2184812884 "
- "628246683 3385113037 3041166140 3948531843 1176600829 228286131 2447397608 712235937 3332826819 2676980703 "
- "4019468871 1952389952 1202638254 3625447051"
+ "1674907590 1932602117 422958422 1395936173 220756089 1930821426 270548699 2334065719 572468890 1468842130 "
+ "228230072 4179658530 1688466613 4012674635 2103220286 2039701458 1285512161 286094819 3931593333 2025331906 "
+ "1149750616 3547818746 2763378766 256794962 2872450297 3325989980 2004362899 2919195159 3491646892 2122414198 "
+ "420628841 4200012434 3320162378 1152842928 3018425196 2167444892 1412989249 3081524922 749600645 2741892354 "
+ "3058644312 2927763742 2151790608 161447244 3956208867 2374508386 1482967587 3511828098 3205344876 3000437852 "
+ "814808125 829465099 3823711829 1185287549 1593782464 2199591136 3023467055 2523505466 1689197018 780089347 "
+ "44458730 4112197585 4258459986 1236687839 2823518555 2479684041 3491196802 545303058 3743891944 991189668 "
+ "3447458848 3832252746 2430529178 1159983819 3629940135 190594660 2554879313 362548723 3612336955 2010161841 "
+ "1743994703 2069273094 3398686937 3268961474 1232012370 3726658366 625902495 3298610786 677651270 469442853 "
+ "2453760226 713371311 3620340122 3164845645 3892725921 1819132929 555152709 2987836360 3396688021 1679549925 "
+ "27186723 2332811871 3897348543 1564936783 1878083837 1482755880 2555386514 4101650284 1178889632 1044730162 "
+ "2755182525 1689632581 1027502326 1803024923 3176795466 350639499 2997246028 1301124121 2836227540 1596217942 "
+ "3651744680 3539839038 490133327 2542349811 4453625 1153787121 4133958274 1364888559 4075038369 3990090481 "
+ "470207591 2968839955 3824740586 362106417 1084619727 3000476803 3225164351 1116096159 662787753 507615282 "
+ "566476880 2835244837 3159374524 1412230792 3703453218 1090129270 2002961036 400503819 817586050 3234854505 "
+ "4164794651 3196635028 4022367980 865452965 1043722328 4288662055 854885198 60043244 1807912906 733447404 "
+ "521161751 2739907959 1682755 1814925066 2381531623 1530460023 3316859409 84006784 4047486883 1248975321 "
+ "1866337499 3315038854 3919853719 1793265423 4275839402 364850440 1340486959 1493735389 180503567 3785612172 "
+ "3784809584 3217896258 1099924448 2728964767 3205210708 3129646871 813153851 807712600 2631995819 1881512613 "
+ "219973569 1239460347 2586307835 2930335422 681504804 2423592664 20055844 3077126477 2658158431 2264927422 "
+ "1854254043 62212580 1245969540 184413059 1748253724 1646967746 2357961224 1682689671 3282147505 3934952521 "
+ "1188946169 801679853 1558566920 4212356346 3608148280 3444156269 3783859627 3346381741 1220028542 4251181693 "
+ "336716306 2953503464 2902767836 1436890395 63178651 1592602138 1850917783 1278012828 296713002 2886486453 "
+ "2476728884 4214134925 2505304384 2300689460 389217986 2710278548 233661663 2747931581 4101629780 3906942615 "
+ "3354933105 1142282878 3600742577 1039176731 2777933131 1576335646 3369994980 368263005 176561861 1724186237 "
+ "2846507750 21118589 3775599720 3650828939 1935890677 286125095 3762296817 1397902790 4291975814 1252787966 "
+ "1241642237 1430742339 1309765472 3974198938 446758750 3547772933 501288115 3519407387 3188611528 1621115077 "
+ "1046935458 2953020642 2289640103 3531415265 781169631 1172796198 4220333084 1928574097 1749557294 2503122305 "
+ "570237345 3533282940 4175250455 3987272570 1850508236 975813332 3382516593 884971500 790425999 3652443020 "
+ "2270797661 156705640 1181634252 1972967689 4106764951 2773022792 593903895 2218687475 3793675215 1854731525 "
+ "1655220491 372666405 480765088 3760852832 2980007172 1735602211 391217934 2631828890 476981906 1839856762 "
+ "3570122281 1779091767 4121255853 1178002844 1953302633 2734234130 2221926310 1060916521 3965308914 1728648292 "
+ "2983354375 2500249711 1770970650 1119065287 565698497 4259881298 701222128 552650442 3898305120 724501753 "
+ "1577132678 3101102534 3653112130 2389539124 939777007 3042165698 3188633521 3362849399 4184926830 3135531283 "
+ "2842487236 3247751192 368049974 3907478262 2507718942 1438211757 1158076588 1218238585 1570740831 2234962914 "
+ "4272204652 642562515 2383644243 3938251731 3285524175 3036856235 4051125791 2344146482 4125705952 249216541 "
+ "2857339293 2375657250 2665433982 4031500829 368500267 3717617658 1744637812 4126648148 912973262 2872470306 "
+ "1359144362 2323793651 426262963 3192535476 1055302938 573075371 704025098 811061128 853648607 2207673794 "
+ "2674109022 958161375 2057405304 4151110152 2532325333 2181265057 2399706916 548340403 240129035 2876077151 "
+ "255483098 4016911297 3510424848 1978946517 1927500545 1050237103 1762209602 682459739 1314934269 2964973840 "
+ "2391562465 3068877582 2340035505 3948056432 1632151208 1901795742 4214422346 957156265 1095826728 1228138222 "
+ "3518121153 4000396322 3950136398 4269373645 1101005710 2052855856 3028013482 1792934547 3111534405 953929322 "
+ "2807675604 1890960104 3402384957 1126574973 3517971698 3370429723 674816533 1613976419 4255334888 1775621638 "
+ "2708315404 471969574 3777839652 801957787 4114789417 4256550217 2135965436 3141702233 1658271919 2900026376 "
+ "1221607807 55854402 2895878597 107244659 955636785 2364627181 318489955 999930328 652666118 3686731190 "
+ "897076587 113265130 994726670 1335150968 1269258330 1387572970 4075647939 676797750 3102753413 1616032926 "
+ "2736390884 561477471 3361077017 906120413 5855502 2265095939 3538773577 2188038926 273088779 81077432 "
+ "280433327 2237856833 2808883151 4053796154 360414378 3899664499 543361699 593259597 3020825521 1676390773 "
+ "3895683241 3133373759 1280756815 592633079 3670064502 1942987606 314090458 2263407917 2343092678 1855965337 "
+ "422294129 1677514362 784071237 458668972 2087100756 2255919018 1691470039 1954199650 728887784 2421406925 "
+ "2823752560 3437169347 1955179635 2880778770 2772735444 844601223 2116267753 126158003 2723080310 1796988516 "
+ "1885857748 859535315 2334395248 3443121877 3238216256 2697230631 2032145003 4237170965 1416579841 3809368829 "
+ "3417401023 644779351 3064682227 2263829690 3406600953 285981679 2916408936 2893955628 3946359698 430898054 "
+ "241626730 1382425716 3243922971 1267678007 1599128790 2074423020 1980818573 1398242918 4210797859 3091415644 "
+ "2826657287 4224949351 1223608592 3383503568 1681320332 2116129066 4104992708 3074345930 127416624 897514657 "
+ "668178712 3204343552 2651789822 2336379439 2171484243 2764106001 1975356867 2764544153 666639838 2176574857 "
+ "2525121669 3879426488 1973029575 306254922 3947884395 1565765543 1626122855 915745869 284434875 1541909193 "
+ "2352912952 2708032180 3794556148 607482607 3735129198 3328036696 2262096198 944788755 3494061432 2648849589 "
+ "250301726 276858865 1435559913 244046482 2334359870 1193480889 201749320 527732369 3726855603 4226354657 "
+ "2868724887 3639713679 3813282583 330448273 1447801496 951257725 2247781133 2785021404 290170638 4025174904 "
+ "797279738 2449404320 3492499128 3555250567 1960574190 1738480184 3196968598 4104469018 939568501 2970621930 "
+ "1250297393 3427956299 1856450111 1964044165 3211402549 111164799 1734093136 3043073892 966934257 1073641708 "
+ "266067573 1362376150 2342133557 417573025"
#endif // _LIBCPP_ABI_VECTORIZED_MERSENNE_TWISTER_ENGINE
;
unsigned as[] = {3, 5, 7};
diff --git a/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/seed_sseq.pass.cpp b/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/seed_sseq.pass.cpp
index 78285ac6baa7f..923e9c3761d84 100644
--- a/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/seed_sseq.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.eng/rand.eng.mers/seed_sseq.pass.cpp
@@ -20,6 +20,7 @@
#include "test_macros.h"
+// Make sure constructing with a std::seed_seq matches calling seed().
void
test1()
{
@@ -44,10 +45,31 @@ test2()
assert(e1 == e2);
}
+// Make sure that seeding with a std::seed_seq produces the standard-mandated deterministic
+// output values. test1() and test2() only check that constructor initialization matches
+// seed() initialization, but would pass if both left the engine in an un-twisted state.
+void test3() {
+ unsigned a[] = {15, 22, 288, 37};
+ std::seed_seq sseq(a, a + 4);
+ std::mt19937 e;
+ e.seed(sseq);
+ assert(e() == 192527404u);
+}
+
+void test4() {
+ unsigned a[] = {15, 22, 288, 37};
+ std::seed_seq sseq(a, a + 4);
+ std::mt19937_64 e;
+ e.seed(sseq);
+ assert(e() == 11567978440329390872ull);
+}
+
int main(int, char**)
{
test1();
test2();
+ test3();
+ test4();
- return 0;
+ return 0;
}
More information about the libcxx-commits
mailing list