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-01-22: 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 regular 64bit values. When
  18. # overflow occurs Perl likes to convert those values to negative 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,
  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 = perl_rand64();
  49. }
  50.  
  51. print color(123, "Using seed: $seed\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 reference so we can update it
  65.     my ($seed) = @_;
  66.  
  67.     use integer;
  68.     my $z  = iv_2_uv($$seed += 11400714819323198485);
  69.     $$seed = iv_2_uv($$seed);
  70.     no integer;
  71.  
  72.     $z = shift_xor_multiply($z, 30, 13787848793156543929);
  73.     $z = shift_xor_multiply($z, 27, 10723151780598845931);
  74.     $z = ($z ^ ($z >> 31));
  75.  
  76.     return $z;
  77. }
  78.  
  79. # Splitmix does a lot of bitshifting, xoring, and multiplying so we
  80. # create one function to simplify that. We utilize `use integer` to
  81. # make sure all math is done using integers and preserve the rollover
  82. sub shift_xor_multiply {
  83.     my ($x, $shift, $mult) = @_;
  84.  
  85.     # This needs to be done with `no integer`
  86.     $x = ($x ^ ($x >> $shift));
  87.  
  88.     # Use integer math for the overflow
  89.     use integer;
  90.     $x = iv_2_uv($x * $mult);
  91.     no integer;
  92.  
  93.     return $x;
  94. }
  95.  
  96. # During large integer math when a UV overflows and wraps back around
  97. # Perl casts it as a IV value. For the purposes of a PRNG we need that
  98. # wraparound math to stay in place. We need uint64_t all the time.
  99. sub iv_2_uv {
  100.     my $x = $_[0];
  101.  
  102.     # Flip it from a IV (signed) to a UV (unsigned)
  103.     # use Devel::Peek; Dump($var) # See the internal Perl type
  104.     if ($x < 0) {
  105.         no integer;
  106.         $x += 18446744073709551615;
  107.         $x += 1;
  108.     }
  109.  
  110.     return $x;
  111. }
  112.  
  113. ###############################################################################
  114. ###############################################################################
  115.  
  116. sub trim {
  117.     my ($s) = (@_, $_); # Passed in var, or default to $_
  118.     if (!defined($s) || length($s) == 0) { return ""; }
  119.     $s =~ s/^\s*//;
  120.     $s =~ s/\s*$//;
  121.  
  122.     return $s;
  123. }
  124.  
  125. # String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
  126. sub color {
  127.     my ($str, $txt) = @_;
  128.  
  129.     # If we're NOT connected to a an interactive terminal don't do color
  130.     if (-t STDOUT == 0) { return $txt || ""; }
  131.  
  132.     # No string sent in, so we just reset
  133.     if (!length($str) || $str eq 'reset') { return "\e[0m"; }
  134.  
  135.     # Some predefined colors
  136.     my %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
  137.     $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg;
  138.  
  139.     # Get foreground/background and any commands
  140.     my ($fc,$cmd) = $str =~ /^(\d{1,3})?_?(\w+)?$/g;
  141.     my ($bc)      = $str =~ /on_(\d{1,3})$/g;
  142.  
  143.     if (defined($fc) && int($fc) > 255) { $fc = undef; } # above 255 is invalid
  144.  
  145.     # Some predefined commands
  146.     my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7);
  147.     my $cmd_num = $cmd_map{$cmd // 0};
  148.  
  149.     my $ret = '';
  150.     if ($cmd_num)      { $ret .= "\e[${cmd_num}m"; }
  151.     if (defined($fc))  { $ret .= "\e[38;5;${fc}m"; }
  152.     if (defined($bc))  { $ret .= "\e[48;5;${bc}m"; }
  153.     if (defined($txt)) { $ret .= $txt . "\e[0m";   }
  154.  
  155.     return $ret;
  156. }
  157.  
  158. # Creates methods k() and kd() to print, and print & die respectively
  159. BEGIN {
  160.     if (eval { require Data::Dump::Color }) {
  161.         *k = sub { Data::Dump::Color::dd(@_) };
  162.     } else {
  163.         require Data::Dumper;
  164.         *k = sub { print Data::Dumper::Dumper(\@_) };
  165.     }
  166.  
  167.     sub kd {
  168.         k(@_);
  169.  
  170.         printf("Died at %2\$s line #%3\$s\n",caller());
  171.         exit(15);
  172.     }
  173. }
  174.  
  175. # Run a test with a given seed and return a string of the results
  176. sub quick_test {
  177.     my $seed = $_[0];
  178.  
  179.     my @data = ();
  180.     for (my $i = 0; $i < 4; $i++) {
  181.         my $num = splitmix64_perl(\$seed);
  182.         push(@data, $num);
  183.     }
  184.  
  185.     my $ret = join(", ", @data);
  186.     return $ret;
  187. }
  188.  
  189. sub run_unit_tests {
  190.     # Seeds < 2**32
  191.     cmp_ok(quick_test(11)       , 'eq', '5833679380957638813, 4839782808629744545, 11769803791402734189, 9308485889748266480');
  192.     cmp_ok(quick_test(22)       , 'eq', '14415425345905102346, 17264975761475716686, 1412077619021228083, 12404402112097020482');
  193.     cmp_ok(quick_test(100)      , 'eq', '2532601429470541124, 269152572843532260, 4491231873834608077, 4673566422923057776');
  194.     cmp_ok(quick_test(123456789), 'eq', '2466975172287755897, 8832083440362974766, 3534771765162737125, 9592110948284743397');
  195.     cmp_ok(quick_test(9999)     , 'eq', '6117204470161645077, 15966700211956150513, 15034308290212886683, 7774926710803868520');
  196.  
  197.     # Seeds > 2**32
  198.     cmp_ok(quick_test(7774926710803868520)  , 'eq', '9605346004387840742, 17435495358832388828, 12684084655726398219, 9795402745067826113');
  199.     cmp_ok(quick_test(9795402745067826113)  , 'eq', '13110559830617540027, 13626988459271143897, 846014752197971904, 13956522239222304255');
  200.     cmp_ok(quick_test(846014752197971904)   , 'eq', '17051223190671778754, 12943043929365758946, 17796463379074244041, 16028253299916138813');
  201.     cmp_ok(quick_test(12943043929365758946) , 'eq', '13152169664619309884, 10188724118650338133, 13259243310153093243, 12185650234802439251');
  202.     cmp_ok(quick_test(16028253299916138813) , 'eq', '17201533047954400773, 3347092783829409799, 2118253649191891459, 15494166571380546778');
  203.  
  204.     done_testing();
  205.     exit(0);
  206. }
  207.  
  208. sub perl_rand64 {
  209.     my $low  = int(rand() * (2**32-1));
  210.     my $high = int(rand() * (2**32-1));
  211.  
  212.     my $ret = ($high << 32) | $low;
  213.  
  214.     return $ret;
  215. }
  216.  
  217. # vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4