Skip to content

Commit

Permalink
Add xoroshiro128** as an option for PRNG
Browse files Browse the repository at this point in the history
  • Loading branch information
scottchiefbaker committed Feb 24, 2025
1 parent 8e92324 commit 5ae64c3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Configure
Original file line number Diff line number Diff line change
Expand Up @@ -21349,11 +21349,18 @@ esac
#randbits=48
#randseedtype=U32

randfunc=Perl_pcg32_random_double
drand01="Perl_pcg32_random_double()"
seedfunc="Perl_pcg32_seed"
# Use xoroshiro128** as the PRNG for rand()
randfunc=Perl_xoroshiro128starstar_random_double
drand01="Perl_xoroshiro128starstar_random_double()"
seedfunc="Perl_xoroshiro128starstar_seed"
randseedtype=U64

# Use PCG32 as the PRNG for rand()
#randfunc=Perl_pcg32_random_double
#drand01="Perl_pcg32_random_double()"
#seedfunc="Perl_pcg32_seed"
#randseedtype=U64

: Probe whether dtrace builds an object, as newer Illumos requires an input
: object file that uses at least one of the probes defined in the .d file
case "$usedtrace" in
Expand Down
4 changes: 4 additions & 0 deletions embed.fnc
Original file line number Diff line number Diff line change
Expand Up @@ -3935,6 +3935,10 @@ Adp |void |wrap_op_checker|Optype opcode \
|NN Perl_check_t *old_checker_p
: Used in pp_ctl.c
p |void |write_to_stderr|NN SV *msv

TXop |double |xoroshiro128starstar_random_double
TXop |void |xoroshiro128starstar_seed \
|U64 seed1
Xp |void |xs_boot_epilog |const SSize_t ax

FTXopv |Stack_off_t|xs_handshake \
Expand Down
45 changes: 45 additions & 0 deletions prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,48 @@ Perl_pcg32_random_double()

return ret;
}

//////////////////////////////////////////////////////////////
// xoroshiro128** functions
//////////////////////////////////////////////////////////////

static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}

static uint64_t XOSSS_SEED[2];

// Perl can only send one seed, so we have to deterministically
// create the other seeds needed for our PRNG
void
Perl_xoroshiro128starstar_seed(U64 seed) {
XOSSS_SEED[0] = hash_msh(seed);
XOSSS_SEED[1] = hash_msh(XOSSS_SEED[0]);

DEBUG_U(PerlIO_printf(Perl_error_log, "Xoroshiro128** INIT: %lu => %lu / %lu\n", seed, XOSSS_SEED[0], XOSSS_SEED[1]));
}

U64
xoroshiro128starstar_rand64()
{
const uint64_t s0 = XOSSS_SEED[0];
uint64_t s1 = XOSSS_SEED[1];
const uint64_t result = rotl(s0 * 5, 7) * 9;

s1 ^= s0;
XOSSS_SEED[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b
XOSSS_SEED[1] = rotl(s1, 37); // c

return result;
}

double
Perl_xoroshiro128starstar_random_double()
{
U64 num = xoroshiro128starstar_rand64();
double ret = uint64_to_double(num);

/*DEBUG_U(PerlIO_printf(Perl_error_log, "Xoroshiro128** Double: %lu => %0.15f\n", num, ret));*/

return ret;
}
8 changes: 8 additions & 0 deletions proto.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ae64c3

Please sign in to comment.