Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file sfc.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657(* Copyright (c) 2018 Melissa E. O'Neill
Copyright (c) 2024, Zolisa Bleki
SPDX-License-Identifier: BSD-3-Clause *)openStdintmoduleSFC64:sig(** SFC64 is a 256-bit implementation of Chris Doty-Humphrey's Small Fast
Chaotic PRNG. It has a few different cycles that one might be on, depending
on the seed; the expected period will be about {m 2^{255}}.
It incorporates a 64-bit counter which means that the absolute minimum cycle
length is {m 2^{64}} and that distinct seeds will not run into each other
for at least {m 2^{64}} iterations. The SFC64 state vector consists of 4
unsigned 64-bit values. The last is a 64-bit counter that increments by 1
each iteration. The input seed is processed by {!SeedSequence} to generate
the first 3 values, then the algorithm is iterated a small number of times to mix.*)includeCommon.BITGENend=struct(* last uint64 value is the counter *)typet={s:state;ustore:uint32option}andstate=uint64*uint64*uint64*uint64letnext(w,x,y,z)=matchUint64.(w+x+z)with|u->u,Uint64.(shift_rightx11|>logxorx,y+shift_lefty3,logor(shift_lefty24)(shift_righty40)+u,z+one)letnext_uint64t=matchnextt.swith|u,s->u,{twiths}letnext_uint32t=matchCommon.next_uint32~next:nextt.st.ustorewith|u,s,ustore->u,{s;ustore}letnext_doublet=Common.next_double~nextu64:next_uint64tletnext_bounded_uint64boundt=Common.next_bounded_uint64bound~nextu64:next_uint64tletset_seed(w,x,y)=letrecloops=function|0->s|i->loop(nexts|>snd)(i-1)inloop(w,x,y,Uint64.one)12letinitializeseed=letistate=Seed.SeedSequence.generate_64bit_state3seedin{s=set_seed(istate.(0),istate.(1),istate.(2));ustore=None}end