Showing splitmix64.pl (raw)


  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use v5.16;
  6.  
  7. use Test::More;
  8.  
  9. ################################################################################
  10. # 2025-10-23: Pure Perl implementation of PRNG splitmix64 PRNG
  11. # Scott Baker / https://www.perturb.org/
  12. ################################################################################
  13. # A lot of work was done here to mimic how C handles overflow multiplication
  14. # on large uint64_t numbers. Perl converts scalars that are larger than 2^64 - 1
  15. # to floating point on the backend. We do *NOT* want that, because splitmix
  16. # (and most PRNGs) rely on overflow math to do their magic. We utilize
  17. # 'use integer' to force Perl to do all math with integer 64bit values. When
  18. # overflow occurs Perl likes to convert those values to signed numbers. In
  19. # the original C all math is done with uint64_t, so we have to convert the
  20. # IV/signed numbers back into UV/unsigned (positive) values.
  21. ################################################################################
  22. #uint64_t splitmix64::rand64() {
  23. #    uint64_t z;
  24. #
  25. #    z = (x += 0x9e3779b97f4a7c15);
  26. #    z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
  27. #    z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
  28. #
  29. #    return z ^ (z >> 31);
  30. #}
  31. ################################################################################
  32.  
  33. use Getopt::Long;
  34.  
  35. my $seed        = [11]; # Default 64bit seed
  36. my $random_seed = 0;
  37.  
  38. GetOptions(
  39.     'seed=i'       => \$seed->[0],
  40.     'random_seed'  => \$random_seed,
  41.     'unit-tests'   => \&run_unit_tests,
  42. );
  43.  
  44. my $iters = int($ARGV[0] || 8);
  45.  
  46. if ($random_seed) {
  47.     print color('yellow', "Using random seed\n");
  48.     $seed->[0] = perl_rand64();
  49. }
  50.  
  51. print color(123, "Using seed: $seed->[0]\n\n");
  52.  
  53. for my $x (1 .. $iters) {
  54.     my $num1  = splitmix64_perl($seed);
  55.  
  56.     printf("%2d: %20u\n", $x, $num1);
  57. }
  58.  
  59. ###############################################################################
  60.  
  61. #my $seed = [10293820198];
  62. #my $num  = splitmix_64_perl($seed);
  63. sub splitmix64_perl {
  64.     # Seed must be passed as a array reference so we can update it
  65.     my $seed = $_[0];
  66.  
  67.     use integer;
  68.     # We bitwise or with zero to convert a signed int (IV) to an unsigned int (UV)
  69.     # This is a weird hack that mauke taught me. It works so *shrug*
  70.     my $z       = ($seed->[0] += 11400714819323198485) | 0;
  71.     $seed->[0] |= 0;
  72.     no integer;
  73.  
  74.     $z = shift_xor_multiply($z, 30, 13787848793156543929);
  75.     $z = shift_xor_multiply($z, 27, 10723151780598845931);
  76.     $z = ($z ^ ($z >> 31));
  77.  
  78.     return $z;
  79. }
  80.  
  81. # Splitmix does a lot of bitshifting, xoring, and multiplying so we
  82. # create one function to simplify that. We utilize `use integer` to
  83. # make sure all math is done using integers and preserve the rollover
  84. sub shift_xor_multiply {
  85.     my ($x, $shift, $mult) = @_;
  86.  
  87.     # This needs to be done with `no integer`
  88.     $x = ($x ^ ($x >> $shift));
  89.  
  90.     # Use integer math for the overflow
  91.     use integer;
  92.     $x = ($x * $mult) | 0;
  93.     no integer;
  94.  
  95.     return $x;
  96. }
  97.  
  98. #################################################################################
  99. ## Alternate single function for copy and paste (no other function dependencies #
  100. #################################################################################
  101.  
  102. #my $seed = [10293820198];
  103. #my $num  = splitmix_64_perl_single($seed);
  104. sub splitmix64_perl_single {
  105.     # Seed must be passed as a array reference so we can update it
  106.     my $seed = $_[0];
  107.  
  108.     use integer;
  109.     # We bitwise or with zero to convert a signed int (IV) to an unsigned int (UV)
  110.     # This is a weird hack that mauke taught me. It works so *shrug*
  111.     my $z       = ($seed->[0] += 11400714819323198485) | 0;
  112.     $seed->[0] |= 0;
  113.     no integer;
  114.  
  115.     $z = ($z ^ ($z >> 30));
  116.     use integer;
  117.     $z = ($z * 13787848793156543929) | 0;
  118.     no integer;
  119.  
  120.     $z = ($z ^ ($z >> 27));
  121.     use integer;
  122.     $z = ($z * 10723151780598845931) | 0;
  123.     no integer;
  124.  
  125.     $z = ($z ^ ($z >> 31));
  126.  
  127.     return $z;
  128. }
  129.  
  130. ###############################################################################
  131. ###############################################################################
  132.  
  133. # String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
  134. sub color {
  135.     my ($str, $txt) = @_;
  136.  
  137.     # If we're NOT connected to a an interactive terminal don't do color
  138.     if (-t STDOUT == 0) { return $txt || ""; }
  139.  
  140.     # No string sent in, so we just reset
  141.     if (!length($str) || $str eq 'reset') { return "\e[0m"; }
  142.  
  143.     # Some predefined colors
  144.     my %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
  145.     $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg;
  146.  
  147.     # Get foreground/background and any commands
  148.     my ($fc,$cmd) = $str =~ /^(\d{1,3})?_?(\w+)?$/g;
  149.     my ($bc)      = $str =~ /on_(\d{1,3})$/g;
  150.  
  151.     if (defined($fc) && int($fc) > 255) { $fc = undef; } # above 255 is invalid
  152.  
  153.     # Some predefined commands
  154.     my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7);
  155.     my $cmd_num = $cmd_map{$cmd // 0};
  156.  
  157.     my $ret = '';
  158.     if ($cmd_num)      { $ret .= "\e[${cmd_num}m"; }
  159.     if (defined($fc))  { $ret .= "\e[38;5;${fc}m"; }
  160.     if (defined($bc))  { $ret .= "\e[48;5;${bc}m"; }
  161.     if (defined($txt)) { $ret .= $txt . "\e[0m";   }
  162.  
  163.     return $ret;
  164. }
  165.  
  166. # Run a test with a given seed and return a string of the results
  167. sub quick_test {
  168.     my $seed = [$_[0]];
  169.  
  170.     my @data = ();
  171.     for (my $i = 0; $i < 4; $i++) {
  172.         my $num = splitmix64_perl($seed);
  173.         push(@data, $num);
  174.     }
  175.  
  176.     my $ret = join(", ", @data);
  177.     return $ret;
  178. }
  179.  
  180. sub run_unit_tests {
  181.     # Seeds < 2**32
  182.     cmp_ok(quick_test(11)       , 'eq', '5833679380957638813, 4839782808629744545, 11769803791402734189, 9308485889748266480');
  183.     cmp_ok(quick_test(22)       , 'eq', '14415425345905102346, 17264975761475716686, 1412077619021228083, 12404402112097020482');
  184.     cmp_ok(quick_test(100)      , 'eq', '2532601429470541124, 269152572843532260, 4491231873834608077, 4673566422923057776');
  185.     cmp_ok(quick_test(123456789), 'eq', '2466975172287755897, 8832083440362974766, 3534771765162737125, 9592110948284743397');
  186.     cmp_ok(quick_test(9999)     , 'eq', '6117204470161645077, 15966700211956150513, 15034308290212886683, 7774926710803868520');
  187.  
  188.     # Seeds > 2**32
  189.     cmp_ok(quick_test(7774926710803868520)  , 'eq', '9605346004387840742, 17435495358832388828, 12684084655726398219, 9795402745067826113');
  190.     cmp_ok(quick_test(9795402745067826113)  , 'eq', '13110559830617540027, 13626988459271143897, 846014752197971904, 13956522239222304255');
  191.     cmp_ok(quick_test(846014752197971904)   , 'eq', '17051223190671778754, 12943043929365758946, 17796463379074244041, 16028253299916138813');
  192.     cmp_ok(quick_test(12943043929365758946) , 'eq', '13152169664619309884, 10188724118650338133, 13259243310153093243, 12185650234802439251');
  193.     cmp_ok(quick_test(16028253299916138813) , 'eq', '17201533047954400773, 3347092783829409799, 2118253649191891459, 15494166571380546778');
  194.  
  195.     done_testing();
  196.     exit(0);
  197. }
  198.  
  199. sub perl_rand64 {
  200.     my $low  = int(rand() * (2**32-1));
  201.     my $high = int(rand() * (2**32-1));
  202.  
  203.     my $ret = ($high << 32) | $low;
  204.  
  205.     return $ret;
  206. }
  207.  
  208. sub get_64bit_seed {
  209.     open my $urandom, '<:raw', '/dev/urandom' or croak("Couldn't open /dev/urandom: $!");
  210.     sysread($urandom, my $buf, 8) or croak("Couldn't read from csprng: $!");
  211.  
  212.     return unpack("Q*", $buf);
  213. }
  214.  
  215. # Creates methods k() and kd() to print, and print & die respectively
  216. BEGIN {
  217.     if (eval { require Data::Dump::Color }) {
  218.         *k = sub { Data::Dump::Color::dd(@_) };
  219.     } else {
  220.         require Data::Dumper;
  221.         *k = sub { print Data::Dumper::Dumper(\@_) };
  222.     }
  223.  
  224.     sub kd {
  225.         k(@_);
  226.  
  227.         printf("Died at %2\$s line #%3\$s\n",caller());
  228.         exit(15);
  229.     }
  230. }
  231.  
  232. # vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4