Skip to content

Instantly share code, notes, and snippets.

@run4flat
Last active December 21, 2016 19:26
Show Gist options
  • Save run4flat/480909b214b5aed3d0ce37629a4aac9b to your computer and use it in GitHub Desktop.
Save run4flat/480909b214b5aed3d0ce37629a4aac9b to your computer and use it in GitHub Desktop.
KISS RNG implementations using different Perl tcc wrappers
use strict;
use warnings;
use C::Blocks;
use C::Blocks::Types qw(uint);
my $N = $ARGV[0] || 100;
sub KISS_rand {
my uint $to_return = 0;
cblock {
static unsigned int x = 123456789,y = 362436000,
z = 521288629, c = 7654321; /* State variables */
unsigned long long t, a = 698769069ULL;
x = 69069*x+12345;
y ^= (y<<13); y ^= (y>>17); y ^= (y<<5);
t = a*z+c; c = (t>>32);
$to_return = (x+y+(z=t));
}
return $to_return;
}
# Get a random number from the generator
KISS_rand for 1 .. $N-1;
my $random = KISS_rand;
print "Random number #$N is $random\n";
use strict;
use warnings;
use C::Blocks;
use C::Blocks::Types qw(uint);
my $N = $ARGV[0] || 100;
# Get a random number from the generator
clex {
unsigned int x = 123456789,y = 362436000,
z = 521288629, c = 7654321; /* State variables */
unsigned int KISS_rng () {
unsigned long long t, a = 698769069ULL;
// if (1) z += 0; /* uncomment for performance drop on my machine */
x = 69069*x+12345;
y ^= (y<<13); y ^= (y>>17); y ^= (y<<5);
t = a*z+c; c = (t>>32);
return (x+y+(z=t));
}
}
# Get a random number from the generator
for (1 .. $N-1 ) { cblock { KISS_rng(); } }
my uint $random = 0;
cblock { $random = KISS_rng(); }
print "Random number #$N is $random\n";
use strict;
use warnings;
use C::Blocks;
my $N = $ARGV[0] || 100;
csub KISS_rand {
static unsigned int x = 123456789,y = 362436000,
z = 521288629, c = 7654321; /* State variables */
unsigned long long t, a = 698769069ULL;
x = 69069*x+12345;
y ^= (y<<13); y ^= (y>>17); y ^= (y<<5);
t = a*z+c; c = (t>>32);
z=t;
dXSARGS; /* setup the stack variables */
mXPUSHi(x+y+z); /* push number to stack */
XSRETURN(1); /* indicate single return value */
}
# Get a random number from the generator
KISS_rand for 1 .. $N-1;
my $random = KISS_rand;
print "Random number #$N is $random\n";
use strict;
use warnings;
use FFI::TinyCC::Inline qw(tcc_inline);
my $N = $ARGV[0] || 100;
tcc_inline q{
unsigned int KISS_rand() {
static unsigned int x = 123456789,y = 362436000,
z = 521288629, c = 7654321; /* State variables */
unsigned long long t, a = 698769069ULL;
x = 69069*x+12345;
y ^= (y<<13); y ^= (y>>17); y ^= (y<<5);
t = a*z+c; c = (t>>32);
return (x+y+(z=t));
}
};
# Get a random number from the generator
KISS_rand() for 1 .. $N-1;
my $random = KISS_rand();
print "Random number #$N is $random\n";
use strict;
use warnings;
use Inline 'C';
my $N = $ARGV[0] || 100;
# Get a random number from the generator
KISS_rand() for 1 .. $N-1;
my $random = KISS_rand();
print "Random number #$N is $random\n";
__END__
__C__
unsigned int KISS_rand() {
static unsigned int x = 123456789,y = 362436000,
z = 521288629, c = 7654321; /* State variables */
unsigned long long t, a = 698769069ULL;
x = 69069*x+12345;
y ^= (y<<13); y ^= (y>>17); y ^= (y<<5);
t = a*z+c; c = (t>>32);
return (x+y+(z=t));
}
use strict;
use warnings;
my $N = $ARGV[0] || 100;
my ($a, $x, $y, $z, $c) = (698769069, 123456789, 362436000, 521288629, 7654321);
sub KISS_rand {
my $t;
$x = (69069*$x+12345) & 0xffffffff;
$y ^= (($y<<13) & 0xffffffff); $y ^= ($y>>17); $y ^= (($y<<5) & 0xffffffff);
$t = $a*$z+$c; $c = ($t>>32);
$z = $t & 0xffffffff;
return (($x+$y+$z) & 0xffffffff);
}
# Get a random number from the generator
KISS_rand() for 1 .. $N-1;
my $random = KISS_rand();
print "Random number #$N is $random\n";
use strict;
use warnings;
use XS::TCC qw(tcc_inline);
my $N = $ARGV[0] || 100;
tcc_inline q{
unsigned int KISS_rand() {
static unsigned int x = 123456789,y = 362436000,
z = 521288629, c = 7654321; /* State variables */
unsigned long long t, a = 698769069ULL;
x = 69069*x+12345;
y ^= (y<<13); y ^= (y>>17); y ^= (y<<5);
t = a*z+c; c = (t>>32);
return (x+y+(z=t));
}
};
# Get a random number from the generator
KISS_rand() for 1 .. $N-1;
my $random = KISS_rand();
print "Random number #$N is $random\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment