| 1 |
/* ISAAC.xs: Perl interface to the ISAAC Pseudo-Random Number Generator |
| 2 |
* |
| 3 |
* This is a Perl XS interface to the original ISAAC reference implementation, |
| 4 |
* written by Bob Jenkins and released into the public domain circa 1996. |
| 5 |
* See `LICENSE' for details. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "EXTERN.h" |
| 9 |
#include "perl.h" |
| 10 |
#include "XSUB.h" |
| 11 |
#include "ppport.h" |
| 12 |
|
| 13 |
#include "rand.h" |
| 14 |
#include "standard.h" |
| 15 |
|
| 16 |
typedef randctx * Math__Random__ISAAC__XS; |
| 17 |
|
| 18 |
MODULE = Math::Random::ISAAC::XS PACKAGE = Math::Random::ISAAC::XS |
| 19 |
|
| 20 |
PROTOTYPES: DISABLE |
| 21 |
|
| 22 |
Math::Random::ISAAC::XS |
| 23 |
new(...) |
| 24 |
PREINIT: |
| 25 |
int idx; |
| 26 |
randctx *self; |
| 27 |
INIT: |
| 28 |
Newx(self, 1, randctx); /* allocate 1 randctx instance */ |
| 29 |
self->randa = self->randb = self->randc = (ub4)0; |
| 30 |
CODE: |
| 31 |
/* Loop through each argument and copy it into randrsl. Copy items from |
| 32 |
* our parameter list first, and then zero-pad thereafter. |
| 33 |
*/ |
| 34 |
for (idx = 0; idx < RANDSIZ; idx++) |
| 35 |
{ |
| 36 |
/* items must be at least 2, or our parameter list is empty */ |
| 37 |
if (!(items > 1)) |
| 38 |
break; |
| 39 |
|
| 40 |
/* note: the list begins at ST(1) */ |
| 41 |
self->randrsl[idx] = (ub4)SvUV(ST(idx+1)); |
| 42 |
items--; |
| 43 |
} |
| 44 |
|
| 45 |
/* Zero-pad the array, if necessary */ |
| 46 |
for (; idx < RANDSIZ; idx++) |
| 47 |
{ |
| 48 |
self->randrsl[idx] = (ub4)0; |
| 49 |
} |
| 50 |
|
| 51 |
randinit(self); /* Initialize using our seed */ |
| 52 |
RETVAL = self; |
| 53 |
OUTPUT: |
| 54 |
RETVAL |
| 55 |
|
| 56 |
UV |
| 57 |
irand(self) |
| 58 |
Math::Random::ISAAC::XS self |
| 59 |
CODE: |
| 60 |
RETVAL = (UV)randInt(self); |
| 61 |
OUTPUT: |
| 62 |
RETVAL |
| 63 |
|
| 64 |
double |
| 65 |
rand(self) |
| 66 |
Math::Random::ISAAC::XS self |
| 67 |
CODE: |
| 68 |
RETVAL = (double)randInt(self) / UINT32_MAX; |
| 69 |
OUTPUT: |
| 70 |
RETVAL |
| 71 |
|
| 72 |
void |
| 73 |
DESTROY(self) |
| 74 |
Math::Random::ISAAC::XS self |
| 75 |
CODE: |
| 76 |
Safefree(self); |