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