Biski64.pm

filter: Statementstmt 95.1% 157 / 165 Branchbran 58.8% 20 / 34 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 32 / 32 Podpod 66.6% 8 / 12 totaltotal 82.2% 223 / 271 CC 29 SCAR 18.0
CC
29
Cov
84%
os_random_bytes
22.5
for_stream
7.1
_mul64
5.0
CRAP 6.0
↑ Summary example.pl »
1 package Math::Random::Biski64;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = 'v0.2.0';
9  
10 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant MASK64 => 0xFFFFFFFFFFFFFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 my $URANDOM_FH = undef;
13  
14 1342 ▶sub _rotl64 {
Subroutine _rotl64: called CC=1 SCAR=0.0
15 1342 my ($x, $k) = @_;
16 1342 (($x << $k) | ($x >> (64 - $k))) & MASK64;
17 }
18  
19 4 ▶sub _add64 {
Subroutine _add64: called CC=1 SCAR=0.0
20 4 my ($a, $b) = @_;
21 4 my $lo = ($a & 0xFFFFFFFF) + ($b & 0xFFFFFFFF);
22 4 my $hi = (($a >> 32) & 0xFFFFFFFF) + (($b >> 32) & 0xFFFFFFFF) + ($lo >> 32);
23 4 (($hi & 0xFFFFFFFF) << 32) | ($lo & 0xFFFFFFFF);
24 }
25  
26 8 ▶sub _mul64 {
Subroutine _mul64: called CC=5 SCAR=16.1
27 8 my ($a, $b) = @_;
28 8 my @al = map { ($a >> $_) & 0xFFFF } (0, 16, 32, 48);
29 8 my @bl = map { ($b >> $_) & 0xFFFF } (0, 16, 32, 48);
30 8 my @p;
31 8 for my $i (0 .. 3) {
32 32 for my $j (0 .. 3) {
33 128 ▶ next if $i + $j >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $i + $j >= 4 48 80 128
34 80 $p[$i + $j] += $al[$i] * $bl[$j];
35 }
36 }
37 8 for my $i (0 .. 2) {
38 24 my $c = $p[$i] >> 16;
39 24 $p[$i] &= 0xFFFF;
40 24 $p[$i + 1] += $c;
41 }
42 8 $p[0] | ($p[1] << 16) | ($p[2] << 32) | ($p[3] << 48);
43 }
44  
45 18 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
46 18 my ($class, $seed) = @_;
47 18 ▶ my $self = bless {}, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
48 18 ▶ if (defined $seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seed) { } 16 2 18
49 16 $self->seed($seed);
50 } else {
51 2 $self->seed(os_random_u64());
52 }
53 18 $self;
54 }
55  
56 3 ▶sub os_random_u64 {
Subroutine os_random_u64: called CC=1 SCAR=0.0
Pod undocumented
57 3 my $bytes = os_random_bytes(8);
58 3 return unpack('Q<', $bytes);
59 }
60  
61 ################################################################################
62  
63 #my $seed = [10293820198];
64 #my $num = splitmix_64_perl_single($seed);
65 72 ▶sub splitmix64_perl {
Subroutine splitmix64_perl: called CC=1 SCAR=0.0
Pod undocumented
66 # Seed must be passed as a array reference so we can update it
67 72 my $seed = $_[0];
68  
69 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 # We bitwise or with zero to convert a signed int (IV) to an unsigned int (UV)
71 # This is a weird hack that mauke taught me. It works so *shrug*
72 72 my $z = ($seed->[0] += 11400714819323198485) | 0;
73 72 $seed->[0] |= 0;
74 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 72 $z = ($z ^ ($z >> 30));
77 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 72 $z = ($z * 13787848793156543929) | 0;
79 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 72 $z = ($z ^ ($z >> 27));
82 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 72 $z = ($z * 10723151780598845931) | 0;
84 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 72 $z = ($z ^ ($z >> 31));
87  
88 72 return $z;
89 }
90  
91 ################################################################################
92  
93 20 ▶sub seed {
Subroutine seed: called CC=4 SCAR=14.6
Pod documented
94 20 my ($self, $seed) = @_;
95 20 my $sm = [$seed];
96 20 my ($s0, $s1, $s2);
97 20 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$s0 == 0 and $s1 == 0 and $s2 == 0 and do { do { $s0 = splitmix64_perl($sm); $s1 = splitmix64_perl($sm); $s2 = splitmix64_perl($sm) } } 0 20 20
Condition33%2/6
$s0 == 0 and $s1 == 0 and $s2 == 0
!ll&&!rl&&r
2000
$s0 == 0 and $s1 == 0
!ll&&!rl&&r
2000
MC/DC0%0/3
$s0 == 0 and $s1 == 0 and $s2 == 0
$s0 == 0 $s1 == 0 $s2 == 0
Truth table25%1/4
A and B and C
A= $s0 == 0B= $s1 == 0C= $s2 == 0
ABCresult
0XX0
10X0
1100
1111
98 20 $s0 = splitmix64_perl($sm);
99 20 $s1 = splitmix64_perl($sm);
100 20 $s2 = splitmix64_perl($sm);
101 } while ($s0 == 0 && $s1 == 0 && $s2 == 0);
102 20 @{$self}{qw(fast_loop mix loop_mix)} = ($s0, $s1, $s2);
103 20 $self->_warmup;
104 20 $self;
105 }
106  
107 671 ▶sub next_u64 {
Subroutine next_u64: called CC=1 SCAR=0.0
Pod documented
108 671 my ($self) = @_;
109 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 671 my $output = $self->{mix} + $self->{loop_mix};
111 671 my $old_lm = $self->{loop_mix};
112 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 671 $self->{loop_mix} = $self->{fast_loop} ^ $self->{mix};
115  
116 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 671 $self->{mix} = _rotl64($self->{mix}, 16) + _rotl64($old_lm, 40);
118 671 $self->{fast_loop} += 11068046444225730969;
119 1 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 671 return $output | 0;
122 }
123  
124 1 ▶sub next_u32 {
Subroutine next_u32: called CC=1 SCAR=0.0
Pod documented
125 1 my ($self) = @_;
126 1 $self->next_u64 >> 32;
127 }
128  
129 2 ▶sub next_double {
Subroutine next_double: called CC=1 SCAR=0.0
Pod documented
130 2 my ($self) = @_;
131 2 ($self->next_u64 >> 11) / 9007199254740992.0;
132 }
133  
134 266 ▶sub rand_integer {
Subroutine rand_integer: called CC=3 SCAR=11.0
Pod documented
135 266 my ($self, $min, $max) = @_;
136 266 my $range = $max - $min + 1;
137 266 ▶ return $min if $range <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $range <= 1 3 263 266
138 263 my $limit = MASK64 - (MASK64 % $range + 1) % $range;
139 263 my $val;
140 263 ▶ do { $val = $self->next_u64 } while $val > $limit;
Branch: 1 branch
BranchTrueFalseTotal
$val > $limit and do { do { $val = $self->next_u64 } } 0 263 263
141 263 return $min + ($val % $range);
142 }
143  
144 5 ▶sub shuffle_array {
Subroutine shuffle_array: called CC=2 SCAR=6.9
Pod documented
145 5 my $self = shift;
146 5 my @copy = @_;
147 5 for my $i (reverse 1 .. $#copy) {
148 47 my $j = $self->rand_integer(0, $i);
149 47 @copy[$i, $j] = @copy[$j, $i];
150 }
151 5 return @copy;
152 }
153  
154 214 ▶sub random_elem {
Subroutine random_elem: called CC=2 SCAR=6.9
Pod documented
155 214 my $self = shift;
156  
157 214 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 213 214
158 1 return undef;
159 }
160  
161 213 my $size = scalar(@_) - 1;
162 213 my $id = $self->rand_integer(0, $size);
163 213 my $ret = $_[$id];
164  
165 213 return $ret;
166 }
167  
168 24 ▶sub _warmup {
Subroutine _warmup: called CC=2 SCAR=6.9
169 24 my ($self) = @_;
170 24 $self->next_u64 for 1 .. 16;
171 }
172  
173 4 ▶sub for_stream {
Subroutine for_stream: called CC=6 SCAR=19.6
Pod undocumented
174 4 my ($class, $seed, $stream_index, $total_streams) = @_;
175 4 my $sm = [$seed];
176 4 my ($s0, $s1, $s2);
177 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$s0 == 0 and $s1 == 0 and $s2 == 0 and do { do { $s0 = splitmix64_perl($sm); $s1 = splitmix64_perl($sm); $s2 = splitmix64_perl($sm) } } 0 4 4
Condition33%2/6
$s0 == 0 and $s1 == 0 and $s2 == 0
!ll&&!rl&&r
400
$s0 == 0 and $s1 == 0
!ll&&!rl&&r
400
MC/DC0%0/3
$s0 == 0 and $s1 == 0 and $s2 == 0
$s0 == 0 $s1 == 0 $s2 == 0
Truth table25%1/4
A and B and C
A= $s0 == 0B= $s1 == 0C= $s2 == 0
ABCresult
0XX0
10X0
1100
1111
178 4 $s0 = splitmix64_perl($sm);
179 4 $s1 = splitmix64_perl($sm);
180 4 $s2 = splitmix64_perl($sm);
181 } while ($s0 == 0 && $s1 == 0 && $s2 == 0);
182 4 my $fast_loop;
183 4 ▶ if ($total_streams > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_streams > 1) { } 4 0 4
184 4 my $cycles_per_stream = int(MASK64 / $total_streams);
185 4 my $step = _mul64($stream_index & MASK64, $cycles_per_stream);
186 4 $fast_loop = _add64($s0, _mul64($step, 0x9999999999999999));
187 } else {
188 0 $fast_loop = $s0;
189 }
190 4 ▶ my $self = bless { fast_loop => $fast_loop, mix => $s1, loop_mix => $s2 }, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
191 4 $self->_warmup;
192 4 $self;
193 }
194  
195 # Fetch random bytes from the OS supplied method
196 # /dev/urandom = Linux, Unix, FreeBSD, Mac, Android
197 # Windows requires the Win32::API call to call RtlGenRandom()
198 3 ▶sub os_random_bytes {
Subroutine os_random_bytes: called CC=10 SCAR=31.1
Pod undocumented
199 3 my $count = shift();
200 3 my $ret = "";
201  
202 3 ▶ if ($count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count <= 0) 0 3 3
203 0 croak("$count is not a valid amount of bytes");
204 }
205  
206 3 ▶ if ($^O eq 'MSWin32') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 3 3
elsif (-r '/dev/urandom') { } 3 0 3
207 0 require Win32::API;
208  
209 0 ▶ state $rand = Win32::API->new(
Branch: 1 branch
BranchTrueFalseTotal
state $rand = "Win32::API"->new("advapi32", "INT SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength)") or croak("Could not import SystemFunction036: $^E") 0 0 0
210 'advapi32',
211 'INT SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength)'
212 ) or croak("Could not import SystemFunction036: $^E");
213  
214 0 $ret = chr(0) x $count;
215 0 ▶ $rand->Call($ret, $count) or croak("Could not read from csprng: $^E");
Branch: 1 branch
BranchTrueFalseTotal
$rand->Call($ret, $count) or croak("Could not read from csprng: $^E") 0 0 0
216 } elsif (-r "/dev/urandom") {
217 3 ▶ if (!$URANDOM_FH) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($URANDOM_FH) 1 2 3
218 1 ▶ open($URANDOM_FH, '<:raw', '/dev/urandom') or croak("Couldn't open /dev/urandom: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $URANDOM_FH, "<:raw", "/dev/urandom" or croak("Couldn't open /dev/urandom: $!") 1 0 1
219 }
220  
221 3 ▶ sysread($URANDOM_FH, $ret, $count) or croak("Couldn't read from csprng: $!");
Branch: 1 branch
BranchTrueFalseTotal
sysread $URANDOM_FH, $ret, $count or croak("Couldn't read from csprng: $!") 3 0 3
222 } else {
223 0 croak("Unknown operating system $^O");
224 };
225  
226 3 ▶ if (length($ret) != $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ret != $count) 0 3 3
227 0 croak("Unable to read $count bytes from OS");
228 }
229  
230 3 return $ret;
231 }
232  
233 1 1;
234  
235 __END__
↑ Summary example.pl »