| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| |
| 3 |
|
| #include "EXTERN.h" |
| 4 |
|
| #include "perl.h" |
| 5 |
|
| #include "XSUB.h" |
| 6 |
|
| |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include "pcg.h" |
| 10 |
|
| |
| 11 |
|
| MODULE = Math::Random::PCG32 PACKAGE = Math::Random::PCG32 |
| 12 |
|
| |
| 13 |
|
| PROTOTYPES: ENABLE |
| 14 |
|
| |
| 15 |
|
| pcg32_random_t * |
| 16 |
|
| new(CLASS, initstate, initseq) |
| 17 |
|
| char *CLASS |
| 18 |
|
| UV initstate |
| 19 |
|
| UV initseq |
| 20 |
|
| CODE: |
| 21 |
1 |
| Newxz(RETVAL, 1, pcg32_random_t); |
| 22 |
1 |
▶ | if (RETVAL == NULL) croak("Could not allocate state memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (RETVAL == NULL) croak("Could not allocate state memory"); |
0 |
1 |
1 |
|
| 23 |
1 |
| pcg32_srandom_r(RETVAL, initstate, initseq); |
| 24 |
|
| OUTPUT: |
| 25 |
|
| RETVAL |
| 26 |
|
| |
| 27 |
|
| UV |
| 28 |
|
| coinflip(pcg32_random_t *rng) |
| 29 |
|
| CODE: |
| 30 |
6 |
▶ | RETVAL = pcg32_random_r(rng) & 1; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = pcg32_random_r(rng) & 1; |
5 |
1 |
6 |
|
| 31 |
|
| OUTPUT: |
| 32 |
|
| RETVAL |
| 33 |
|
| |
| 34 |
|
| UV |
| 35 |
|
| decay(pcg32_random_t *rng, uint32_t odds, uint32_t min, uint32_t max) |
| 36 |
|
| PREINIT: |
| 37 |
|
| uint32_t count; |
| 38 |
|
| CODE: |
| 39 |
3 |
| count = min; |
| 40 |
14 |
▶ | while (count < max) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (count < max) { |
12 |
2 |
14 |
|
| 41 |
12 |
▶ | if (pcg32_random_r(rng) < odds) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcg32_random_r(rng) < odds) break; |
1 |
11 |
12 |
|
| 42 |
11 |
| count++; |
| 43 |
|
| } |
| 44 |
3 |
▶ | RETVAL = count; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = count; |
0 |
3 |
3 |
|
| 45 |
|
| OUTPUT: |
| 46 |
|
| RETVAL |
| 47 |
|
| |
| 48 |
|
| UV |
| 49 |
|
| irand(pcg32_random_t *rng) |
| 50 |
|
| CODE: |
| 51 |
6 |
▶ | RETVAL = pcg32_random_r(rng); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = pcg32_random_r(rng); |
5 |
1 |
6 |
|
| 52 |
|
| OUTPUT: |
| 53 |
|
| RETVAL |
| 54 |
|
| |
| 55 |
|
| UV |
| 56 |
|
| irand64(pcg32_random_t *rng) |
| 57 |
|
| CODE: |
| 58 |
1 |
▶ | RETVAL = ((uint64_t) pcg32_random_r(rng) << 32) | pcg32_random_r(rng); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ((uint64_t) pcg32_random_r(rng) << 32) | pcg32_random_r(rng); |
0 |
1 |
1 |
|
| 59 |
|
| OUTPUT: |
| 60 |
|
| RETVAL |
| 61 |
|
| |
| 62 |
|
| UV |
| 63 |
|
| irand_in(pcg32_random_t *rng, uint32_t min, uint32_t max) |
| 64 |
|
| CODE: |
| 65 |
7 |
▶ | if (max == min) RETVAL = min; |
Branch: 1 branch
| Branch | True | False | Total |
| if (max == min) RETVAL = min; |
0 |
7 |
7 |
|
| 66 |
7 |
▶ | else if (min > max) croak("max must be greater than min"); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (min > max) croak("max must be greater than min"); |
0 |
7 |
7 |
|
| 67 |
7 |
| else RETVAL = min + pcg32_random_r(rng) % (max - min + 1); |
| 68 |
|
| OUTPUT: |
| 69 |
|
| RETVAL |
| 70 |
|
| |
| 71 |
|
| void |
| 72 |
|
| irand_way(pcg32_random_t *rng, int32_t x1, int32_t y1, int32_t x2, int32_t y2) |
| 73 |
|
| PREINIT: |
| 74 |
|
| int32_t dx, dy, magx; |
| 75 |
|
| PPCODE: |
| 76 |
23 |
▶ | if (x1 == x2 && y1 == y2) XSRETURN_UNDEF; |
Branch: 2 branches
| Branch | True | False | Total |
| if (x1 == x2 && y1 == y2) XSRETURN_UNDEF; |
7 |
16 |
23 |
| if (x1 == x2 && y1 == y2) XSRETURN_UNDEF; |
3 |
4 |
7 |
|
| 77 |
20 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
20 |
20 |
|
| 78 |
20 |
| dx = x2 - x1; |
| 79 |
20 |
| dy = y2 - y1; |
| 80 |
20 |
▶ | if (dx == 0) goto MOVE_Y; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dx == 0) goto MOVE_Y; |
4 |
16 |
20 |
|
| 81 |
16 |
▶ | else if (dy == 0) goto MOVE_X; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (dy == 0) goto MOVE_X; |
2 |
14 |
16 |
|
| 82 |
14 |
| magx = abs(dx); |
| 83 |
14 |
▶ | if (pcg32_random_r(rng) % (magx + abs(dy)) < magx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcg32_random_r(rng) % (magx + abs(dy)) < magx) { |
8 |
6 |
14 |
|
| 84 |
8 |
| MOVE_X: |
| 85 |
10 |
▶ | mPUSHs(newSViv(x1 + (dx > 0 ? 1 : -1))); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHs(newSViv(x1 + (dx > 0 ? 1 : -1))); |
6 |
4 |
10 |
|
| 86 |
10 |
| mPUSHs(newSViv(y1)); |
| 87 |
|
| } else { |
| 88 |
6 |
| MOVE_Y: |
| 89 |
10 |
| mPUSHs(newSViv(x1)); |
| 90 |
10 |
▶ | mPUSHs(newSViv(y1 + (dy > 0 ? 1 : -1))); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHs(newSViv(y1 + (dy > 0 ? 1 : -1))); |
6 |
4 |
10 |
|
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| double |
| 94 |
|
| rand(pcg32_random_t *rng, ...) |
| 95 |
|
| PROTOTYPE: $;$ |
| 96 |
|
| PREINIT: |
| 97 |
|
| double factor; |
| 98 |
|
| CODE: |
| 99 |
2 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
1 |
1 |
2 |
|
| 100 |
1 |
▶ | if (!SvIOK(ST(1)) && !SvNOK(ST(1))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvIOK(ST(1)) && !SvNOK(ST(1))) |
0 |
1 |
1 |
| if (!SvIOK(ST(1)) && !SvNOK(ST(1))) |
0 |
0 |
0 |
|
| 101 |
0 |
| croak("factor must be a number"); |
| 102 |
1 |
| factor = SvNV(ST(1)); |
| 103 |
1 |
| } else factor = 1.0; |
| 104 |
|
| /* "binary floating point constant for 2^-32" from PCG blog |
| 105 |
|
| * post "Efficiently Generating a Number in a Range" (fast, |
| 106 |
|
| * but biased) */ |
| 107 |
2 |
▶ | RETVAL = 0x1.0p-32 * pcg32_random_r(rng) * factor; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 0x1.0p-32 * pcg32_random_r(rng) * factor; |
0 |
2 |
2 |
|
| 108 |
|
| OUTPUT: |
| 109 |
|
| RETVAL |
| 110 |
|
| |
| 111 |
|
| SV * |
| 112 |
|
| rand_elm(pcg32_random_t *rng, avref) |
| 113 |
|
| AV *avref; |
| 114 |
|
| PREINIT: |
| 115 |
|
| SSize_t len; |
| 116 |
|
| SV **svp; |
| 117 |
|
| CODE: |
| 118 |
1 |
| len = av_len(avref) + 1; |
| 119 |
1 |
▶ | if (len == 0) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) XSRETURN_UNDEF; |
0 |
1 |
1 |
|
| 120 |
1 |
| svp = av_fetch(avref, pcg32_random_r(rng) % len, FALSE); |
| 121 |
1 |
| SvREFCNT_inc(*svp); |
| 122 |
1 |
| RETVAL = *svp; |
| 123 |
|
| OUTPUT: |
| 124 |
|
| RETVAL |
| 125 |
|
| |
| 126 |
|
| SV * |
| 127 |
|
| rand_from(pcg32_random_t *rng, avref) |
| 128 |
|
| AV *avref; |
| 129 |
|
| PREINIT: |
| 130 |
|
| SSize_t i, len, rnd; |
| 131 |
|
| SV *dunno, **src, **dst; |
| 132 |
|
| CODE: |
| 133 |
6 |
| len = av_len(avref) + 1; |
| 134 |
6 |
▶ | if (len == 0) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) XSRETURN_UNDEF; |
1 |
5 |
6 |
|
| 135 |
5 |
| rnd = pcg32_random_r(rng) % len; |
| 136 |
5 |
| dunno = av_delete(avref, rnd, 0); |
| 137 |
5 |
▶ | if (rnd != len - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rnd != len - 1) { |
2 |
3 |
5 |
|
| 138 |
2 |
| dst = &AvARRAY(avref)[rnd]; |
| 139 |
2 |
| src = dst + 1; |
| 140 |
7 |
▶ | for (i = rnd; i < len - 1; i++) *dst++ = *src++; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = rnd; i < len - 1; i++) *dst++ = *src++; |
5 |
2 |
7 |
|
| 141 |
2 |
| AvFILLp(avref) -= 1; |
| 142 |
2 |
| AvMAX(avref) -= 1; |
| 143 |
|
| } |
| 144 |
5 |
| SvREFCNT_inc(dunno); |
| 145 |
5 |
| RETVAL = dunno; |
| 146 |
|
| OUTPUT: |
| 147 |
|
| RETVAL |
| 148 |
|
| |
| 149 |
|
| UV |
| 150 |
|
| rand_idx(pcg32_random_t *rng, avref) |
| 151 |
|
| AV *avref; |
| 152 |
|
| PREINIT: |
| 153 |
|
| SSize_t len; |
| 154 |
|
| CODE: |
| 155 |
1 |
| len = av_len(avref) + 1; |
| 156 |
1 |
▶ | if (len == 0) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) XSRETURN_UNDEF; |
0 |
1 |
1 |
|
| 157 |
1 |
▶ | else RETVAL = pcg32_random_r(rng) % len; |
Branch: 1 branch
| Branch | True | False | Total |
| else RETVAL = pcg32_random_r(rng) % len; |
0 |
1 |
1 |
|
| 158 |
|
| OUTPUT: |
| 159 |
|
| RETVAL |
| 160 |
|
| |
| 161 |
|
| UV |
| 162 |
|
| roll(pcg32_random_t *rng, uint32_t count, uint32_t sides) |
| 163 |
|
| PREINIT: |
| 164 |
|
| uint32_t sum; |
| 165 |
|
| CODE: |
| 166 |
6 |
▶ | if (count == 0) croak("count must be positive"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (count == 0) croak("count must be positive"); |
0 |
6 |
6 |
|
| 167 |
6 |
▶ | if (sides == 0) croak("sides must be positive"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (sides == 0) croak("sides must be positive"); |
0 |
6 |
6 |
|
| 168 |
6 |
| sum = count; |
| 169 |
24 |
▶ | while (count--) sum += pcg32_random_r(rng) % sides; |
Branch: 1 branch
| Branch | True | False | Total |
| while (count--) sum += pcg32_random_r(rng) % sides; |
18 |
6 |
24 |
|
| 170 |
6 |
▶ | RETVAL = sum; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = sum; |
5 |
1 |
6 |
|
| 171 |
|
| OUTPUT: |
| 172 |
|
| RETVAL |
| 173 |
|
| |
| 174 |
|
| void |
| 175 |
|
| sample(pcg32_random_t *rng, uint32_t count, avref) |
| 176 |
|
| AV *avref; |
| 177 |
|
| PREINIT: |
| 178 |
|
| AV* smpl; |
| 179 |
|
| SSize_t len, i; |
| 180 |
|
| uint32_t total; |
| 181 |
|
| PPCODE: |
| 182 |
4 |
| smpl = newAV(); |
| 183 |
4 |
| len = av_len(avref) + 1; |
| 184 |
4 |
▶ | if (len == 0 || count == 0) goto DONE; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 0 || count == 0) goto DONE; |
3 |
1 |
4 |
| if (len == 0 || count == 0) goto DONE; |
1 |
2 |
3 |
|
| 185 |
2 |
▶ | if (count >= len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count >= len) { |
1 |
1 |
2 |
|
| 186 |
1 |
| av_extend(smpl, len - 1); |
| 187 |
6 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
5 |
1 |
6 |
|
| 188 |
5 |
| SV *sv = *av_fetch(avref, i, FALSE); |
| 189 |
5 |
| av_push(smpl, sv); |
| 190 |
|
| } |
| 191 |
|
| } else { |
| 192 |
1 |
| total = len; |
| 193 |
1 |
| av_extend(smpl, count - 1); |
| 194 |
5 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
5 |
0 |
5 |
|
| 195 |
5 |
▶ | if (pcg32_random_r(rng) < ( count * ( UINT32_MAX / total ) )) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcg32_random_r(rng) < ( count * ( UINT32_MAX / total ) )) { |
3 |
2 |
5 |
|
| 196 |
3 |
| SV *sv = *av_fetch(avref, i, FALSE); |
| 197 |
3 |
| av_push(smpl, sv); |
| 198 |
3 |
▶ | if (--count == 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (--count == 0) break; |
1 |
2 |
3 |
|
| 199 |
|
| } |
| 200 |
4 |
| total--; |
| 201 |
|
| } |
| 202 |
|
| } |
| 203 |
0 |
| DONE: |
| 204 |
4 |
| ST(0) = sv_2mortal( newRV_inc((SV *) smpl) ); |
| 205 |
4 |
| XSRETURN(1); |
| 206 |
|
| |
| 207 |
|
| void |
| 208 |
|
| DESTROY(pcg32_random_t *rng) |
| 209 |
|
| PPCODE: |
| 210 |
1 |
| Safefree(rng); |