#include "prandom.h" #ifndef PTSD_RNG_START_SEED #define PTSD_RNG_START_SEED (0X16A0CE8489509C5ULL) #endif static prandom_device_t device = { .seed = PTSD_RNG_START_SEED, .size = PTSD_RNG_SIZE, .state = { 101908133358930373ull, 1279206712819156980ull, 16519118440885668846ull, 1587614043125756814ull, 6809011425044097350ull, 12909525058246637862ull, 16952313123508511554ull, 11809275674093189406ull, 16604926886037155980ull, 3787968006240461474ull, 17569104002941337880ull, 8619602686182369848ull, 16204900816441354091ull, 236515697944171621ull, 5668857744650724449ull, 11779822010686561455ull, 16630420258829264091ull, 16139341452904273273ull, 12017992252712376456ull, 1850076972857787129ull, 17486109888956289203ull, 2492345506203110373ull, 14916433888947732713ull, 3820511635493092253ull, 1249599914758899827ull, 16930784432854743342ull, 13471944436842288229ull, 10637336150747750684ull, 13689884363429774670ull, 4139528179167278321ull, 985483960858434565ull, 15373808676098440146ull, 6242159211917357607ull, 10763521356545867531ull, 11675401284268988977ull, 2664092856055611768ull, 5328334615396470764ull, 12016537341191819184ull, 3359433700764873124ull, 9267042685203896035ull, 12579157914845316255ull, 16274557025767856004ull, 9778691008832483595ull, 17842167765772294202ull, 1697949259063052683ull, 16942858795882915946ull, 6007936429811739101ull, 15474130554251706867ull, 9669807547919878336ull, 4712277241206355487ull, 9373835514628778084ull, 7669156162099736285ull, 2343932951312561400ull, 10065713850025955453ull, 8429591195143904607ull, 17919115195182464809ull, 1844660470841680766ull, 16435083473959818251ull, 15097509775206006514ull, 6486024131972645410ull, 11685926839579735073ull, 10363742624166998562ull, 11503477548597020958ull, 6580889460225302723ull, 772000794353327662ull, 13019715310774429155ull, 5032041962839498169ull, 14431423565080193483ull, 7209785309605684028ull, 2434068425307062272ull, 10237125718625747014ull, 15284569583832960867ull, 2803291431141112296ull, 10220953819450187297ull, 467850002287638575ull, 18282447593311697476ull, 1549884280935399533ull, 17560471451419560792ull, 14877148883148613179ull, 12821576511436262743ull, 2761279275769929267ull, 17134133601156668838ull, 5860120099897641317ull, 15968179041833118031ull, 9461035184657641768ull, 1571274917902180763ull, 4908197189749913091ull, 4657702903308408581ull, 9636843265006897844ull, 10695859912062711859ull, 13962695921020696321ull, 3643311239376039433ull, 15699907558930453355ull, 3596806281458403509ull, 8734160505831001057ull, 150514622830135679ull, 14550512796106734729ull, 1661621602033696583ull, 16456648991530939267ull, 1998144815287943395ull, 4861703820965644169ull, 3972181521528815517ull, 4595403746626561217ull, 7122336112928357374ull, 6295697259249985041ull, 3704654907975929817ull, 2469371592457548073ull, 5647639102221378394ull, 13409600688487255641ull, 7914232214515985242ull, 10539667813991695963ull, 16009864558310160046ull, 7780803086195800891ull, 5635895958476991783ull, 17457295056651357276ull, 197474077055170236ull, 10149005757013140056ull, 13835896551140686091ull, 1627610115375272494ull, 14811361735047107097ull, 13352746572070892366ull, 2855425638704240461ull, 5763893903825162661ull, 1892645034206558007ull, 5108438424632088109ull, 10954639150128263537ull, 4428572663425001619ull, 7782324095054390388ull, 717850476531956291ull, 3256644307905840966ull, 13563019489399270732ull, 2508070374742395397ull, 18339703097752722999ull, 10948611624710069297ull, 13388906938010185691ull, 16754593763497496902ull, 10690899279850624251ull, 16289121661699960616ull, 16144783029029564967ull, 2791306603066688199ull, 2975898847065908269ull, 13905581915023421912ull, 8415962784503528443ull, 654993183308951173ull, 4187554619978614211ull, 4773425700299255766ull, 9602956233894548643ull, 17390612538200375626ull, 10974630798508318307ull, 12577465130955379084ull, 13577896479720365016ull, 11464777123980313517ull, 4474865735987307281ull, 1180219764175440224ull, 14795946472253812794ull, 17113193026548265466ull, 18329437419485465147ull, 4422811750221089189ull, 13881103794371870129ull, 10786803654537810973ull, 16060219129390848297ull, 1816609783926192615ull, 661353555799105571ull, 6528385464840955528ull, 12616931753772894003ull, 18079705321682440268ull, 17129505516399766911ull, 12536655220394655179ull, 808271890750238199ull, 16084815013318373786ull, 7195960586295929257ull, 13640121843692968131ull, 8419518948402079811ull, 14266271218858961691ull, 5713059945022158294ull, 14531120917258706624ull, 12072840919536643829ull, 1616667682464876706ull, 13468948679118482368ull, 7479153813335972769ull, 15155155690895713300ull, 13914760489063831174ull, 12065533884376620521ull, 16290258095120487828ull, 16177845077259577609ull, 5751843108461977375ull, 7110869302729857004ull, 15423129882524764230ull, 9258728975031682607ull, 11080003765098987528ull, 3965068799417528356ull, 16395835809598739707ull, 4580452205154545928ull, 10403546257202649721ull, 2132700181009320591ull, 3880725812505475420ull, 6854394608273590024ull, 6464617282133955284ull, 11292372507378154793ull, 9532738908011656292ull, 10577007320027448178ull, 13046462232816978905ull, 16076319398979342903ull, 14281814143899945079ull, 9023852915670693944ull, 6039209026195286572ull, 13531126811471445017ull, 9927314125538637820ull, 1655449637387832354ull, 8537187732773821535ull, 1149063713388570820ull, 12117932873375437679ull, 1124339588827463903ull, 8667313296164294302ull, 12645037818316815071ull, 15212624085260290034ull, 16129669904079180223ull, 12259542992044609469ull, 13964236130694290627ull, 3126808252226536987ull, 13356655894042910217ull, 17502131248914325658ull, 10435964740342932189ull, 2308725560737537192ull, 14346311813388725752ull, 8905024040965810990ull, 9820826743553134555ull, 1873397771125092770ull, 6623147353496345330ull, 281826768905094010ull, 6138229692602579804ull, 2629443434910544770ull, 9451003329394294294ull, 5749474164118090165ull, 5976595007259491606ull, 7684904922227791292ull, 4529549984860531239ull, 1567874679330670126ull, 5298816512834453136ull, 16554493894359212598ull, 9032589273455441651ull, 13551849166313081135ull, 11788263046431622461ull, 7113711552678972252ull, 13996451774975320975ull, 14014169765690998153ull, 716405728811259612ull, 6281764556252220859ull, 6168534469571589934ull, 16550454337657086962ull, 9850627662399968737ull, 11231113148028316448ull, 8645813163297842570ull, 5341893966886300986ull, 6349559103380318731ull, 4221845441261925989ull, 14748897105144296275ull, 5554320982311784753ull, 5084045416338831442ull, 17978950379355356984ull, 18276589965950502929ull, 4745226222775917347ull, 15469200773474891156ull, 5100509630310533464ull, 7163211062309944391ull, 17139059371070894291ull, 12482850372116051898ull, 2564551423968100499ull, 3381801501969157377ull, 2165377871479122532ull, 14582507687443407114ull, 8573310138757376286ull, 9930652967649590425ull, 570041046755582910ull, 13951508035093605028ull, 6200218144898052564ull, 15610746202299680375ull, 15350004480113937025ull, 5563545218799734596ull, 7261305680336250762ull, 8203123745914477397ull, 7766503052771189413ull, 15164314224060907222ull, 13958107475933196670ull, 1183209120415078551ull, 7148484773491748462ull, 261932016709642967ull, 17074438645673337392ull, 5925928925812161141ull, 12230720535555028621ull, 9603482508161494459ull, 16304424188300318978ull, 8078443723219205755ull, 1058909325274450075ull, 1647415960466749395ull, 12726419882774287324ull, 14516011994456180122ull, 13985423138863953448ull, 3942336098781090759ull, 946570366961822668ull, 8355859045160897408ull, 13160280818358282244ull, 11292314467103428408ull, 9689236616670476773ull, 146488478595965105ull, 5167957847301765976ull, 3206397696539769457ull, 16859478427316566938ull, 16972203774113370163ull, 17456896543063597445ull, 7766208013773052352ull, 6732072506767245262ull, 18259698028495213201ull, 16500086934380817623ull, 2917997925619701062ull, 2078370371322228741ull, 3566416607440242927ull, 10270392280833715318ull, 5144141297194464426ull, 3764650445331140956ull, 16790511065966161796ull, 16448174479857015330ull, 6267707721886244985ull, 1442563428367964683ull, 2435276993727464705ull, 1970996224696054428ull, 7377955693044841546ull, 18074323302867216580ull, 4772335248245231337ull, 15273422196499137057ull, 15244932131878090456ull, 13920435279566876600ull, 10295350540483953625ull, 14817497597995309754ull, 14103659193512797229ull, 12033223398623801585ull, 1172060364353322469ull, 3625666558904762404ull, 1506782018458797454ull, 11967350032592963733ull, 5480955456682819237ull, 10998552558469628689ull, 5382970836753082059ull, 16715306293569444861ull, 4810551287325477034ull, 17994258720367811958ull, 206905435062950429ull, 8403177623406847798ull, 14246241425090684685ull, 7444641662747257631ull, 6333473103207034000ull, 17381354019011331238ull, 10999586971146292083ull, 5923102720176964296ull, 4583606194298610097ull, 11522467097097212554ull, 14927936899798285724ull, 11056643330936271982ull, 11550549942923835930ull, 4089487537404874639ull, 5635378697670159617ull, 6785563013481532521ull, 10130734601340250306ull, 12045245622644218539ull, 9221223128420843227ull, 17109047477002529155ull, 13153788070103997422ull, 3971315021993928355ull, 12085044547664108757ull, 9504153104331186306ull, 10128664722281860338ull, 7252748051052922627ull, 8370655641737715490ull, 12940673986805387354ull, 10096583571072917342ull, 6445258490562245051ull, 5171693209265157038ull, 4190733487616947186ull, 17662493028375954616ull, 17350830905700378376ull, 16189689111465078841ull, 16212065641469505843ull, 2138860959450110158ull, 16859704570437030785ull, 3084455271440539310ull, 13757812588672209571ull, 2173927216603894841ull, 12199110035626897122ull, 7186096099668307876ull, 6705015887139121304ull, 11687508248721054597ull, 9268502105800789608ull, 9787903931355255694ull, 15867165814246231581ull, 2265255111597543868ull, 5224343500007198831ull, 9190074864044505838ull, 17116056182967642822ull, 17332233554327617665ull, 7006216226477894077ull, 2605549834931992692ull, 5451428457315862781ull, 11834527260125796150ull, 13215867077603556415ull, 17316058989003800655ull, 10971532703813632873ull, 3699963323019105523ull, 10008976741139315244ull, 12537323175983229754ull, 12906004931665052065ull, 8090686801770498559ull, 14211799045788740333ull, 5937388144450878590ull, 15633346246943429318ull, 8576031699171399825ull, 17362294633634429583ull, 3239931547069353780ull, 11855448300288164941ull, 1434226139059182203ull, 2913157883817069424ull, 2254119154719753396ull, 5871566286971318993ull, 18118292482050893398ull, 10368977385565789834ull, 5853810697312293344ull, 11859633629252670240ull, 10188001796389076536ull, 16065603928068485985ull, 4955226774400398330ull, 7445715973519259130ull, 18105670628100973563ull, 9579202598508153591ull, 925494967624192243ull, 6554158594539019846ull, 13886793540525080019ull, 96985608002069347ull, 12131988410176684633ull, 9842334647039203490ull, 4879920434004402198ull, 5989045598348671880ull, 8793175749900151623ull, 9018420823457933955ull, 5784412579687755752ull, 10526562699476826090ull, 4039763885403009428ull, 8699991691217400585ull, 17516839137652410742ull, 9535984839012945538ull, 10771083099225943872ull, 11638238124360584495ull, 14728262831933563405ull, 14750867935140444293ull, 271887338183060302ull, 15848639943633722695ull, 16507674012648057826ull, 13663689638937454910ull, 2166857898694364460ull, 5227436512244655037ull, 4319200726958993070ull, 2397800910742962925ull, 13017617110417581655ull, 16956999388184092336ull, 16475802754481244835ull, 4616636750962269743ull, 1873671862234376050ull, 13738842186989716879ull, 913544108677352125ull, 13243550124404092430ull, 7077775487096131560ull, 18097669378493274116ull, 2318928423494229559ull, 8461913334841743496ull, 8687969026846164583ull, 3380525911469253251ull, 8463856809802151519ull, 10259802007836508109ull, 10533539766507098933ull, 10281767202748439438ull, 9637730197383038578ull, 6180368031522034159ull, 15484761520957668802ull, 8690406752285080696ull, 319579655039737089ull, 18378689156243249978ull, 7221830107753271363ull, 6878882511509505107ull, 3981504813660804548ull, 5727300434788638595ull, 3009402910608782102ull, 10781399339573637731ull, 2331677748400709345ull, 17021879095309844066ull, 14154945888703820515ull, 11576849344006622221ull, 18346544511058558215ull, 4246472695339038283ull, 1914200706993037165ull, 1590332447688070652ull, 17442082978240332438ull, 3114319107933567126ull, 15661019185593667822ull, 8672872034673584256ull, 16556617141895775181ull, 4979273269836050681ull, 9566060364586939968ull, 15120909788192026215ull, 18293468635461877238ull, 815720754080228926ull, 17034196900141040910ull, 14759866008643624808ull, 11639166453002951003ull, 15280774597530840382ull, } }; u64 prd_random_u64(prandom_device_t *device) { if (device->seed == 0) { srand(device->state[rand() % device->size]); // NOLINT device->seed = rand(); // NOLINT } static const u64 a = 23LLU; static const u64 b = 0X9EB684818AE6BECFLLU; static const u64 c = 11LLU; static const u64 d = 0X220621236D8913FLLU; static const u64 e = 23LLU; static const u64 f = 0X9EB684818AE6BECFLLU; u64 index = (device->seed + (f >> e)); u64 num0 = device->state[(index >> 0) % device->size] + device->seed; num0 ^= ((num0 << a) & b) ^ ~(device->state[((index >> 0) + (b+a)) % device->size]); num0 ^= ((num0 >> c) & d) ^ ~(device->state[((index >> 0) - (c+d)) % device->size]); num0 ^= ((num0 << e) & f) ^ ~(device->state[((index >> 0) + (e+f)) % device->size]); // num0 = num0 >> 1; // num0 ^= seed; u64 num1 = device->state[(index >> 32) % device->size] + device->seed; num1 ^= ((num1 >> a) & b) ^ ~(device->state[((index >> 32) - (b+a)) % device->size]); num1 ^= ((num1 << c) & d) ^ ~(device->state[((index >> 32) + (c+d)) % device->size]); num1 ^= ((num1 >> e) & f) ^ ~(device->state[((index >> 32) - (e+f)) % device->size]); // num1 = num1 >> 3; //num1 ^= device->seed; u64 result = (num0 ^ num1) + device->seed; device->seed ^= result; device->state[(index >> 0 ) % device->size] = num1; device->state[(index >> 32 ) % device->size] = num0; return result; //% max; } u64 prandom_u64(void) { return prd_random_u64(&device); } u32 prandom_u32(void) { return (u32)prd_random_u64(&device); } u16 prandom_u16(void) { return (u16)prd_random_u64(&device); } u8 prandom_u8 (void) { return (u8) prd_random_u64(&device); } s64 prandom_s64(void) { return (s64)prd_random_u64(&device); } s32 prandom_s32(void) { return (s32)prd_random_u64(&device); } s16 prandom_s16(void) { return (s16)prd_random_u64(&device); } s8 prandom_s8 (void) { return (s8) prd_random_u64(&device); } u32 prd_random_u32(prandom_device_t *device) { return (u32)prd_random_u64(device); } u16 prd_random_u16(prandom_device_t *device) { return (u16)prd_random_u64(device); } u8 prd_random_u8 (prandom_device_t *device) { return (u8) prd_random_u64(device); } s64 prd_random_s64(prandom_device_t *device) { return (s64)prd_random_u64(device); } s32 prd_random_s32(prandom_device_t *device) { return (s32)prd_random_u64(device); } s16 prd_random_s16(prandom_device_t *device) { return (s16)prd_random_u64(device); } s8 prd_random_s8 (prandom_device_t *device) { return (s8) prd_random_u64(device); } #ifndef PTSD_STATE_INITIAL_VALUE #define PTSD_STATE_INITIAL_VALUE (101908133358930373ULL) #endif #ifndef PTSD_STATE_CONSTANT_VALUE #define PTSD_STATE_CONSTANT_VALUE (15632138375488585303ULL) #endif static u64 pget_state_value(u64 n) { if (n == 0) return PTSD_STATE_INITIAL_VALUE; return PTSD_STATE_CONSTANT_VALUE * (pget_state_value(n - 1) ^ (pget_state_value(n - 1) >> (64 - 2))) + n; } void pinitialize_state_array(usize count, u64 buffer[count]) { for (usize i = 0; i < count; i++) { buffer[i] = pget_state_value(count); } }