blib/lib/Data/Integer.pm

filter: Statementstmt 99.8% 635 / 636 Branchbran 93.0% 188 / 202 Conditioncond 80.9% 34 / 42 MC/DCmcdc 50.0% 12 / 24 Subroutinesub 99.4% 188 / 189 Podpod 100% 74 / 74 totaltotal 96.9% 1131 / 1167 CC 116 SCAR 11.3
CC
116
Cov
97%
nint_sub
12.0
hex_natint
9.1
nint_add
8.0
CRAP 3.1
↑ Summary
1 =head1 NAME
2  
3 Data::Integer - details of the native integer data type
4  
5 =head1 SYNOPSIS
6  
7 use Data::Integer qw(natint_bits);
8  
9 $n = natint_bits;
10  
11 # and other constants; see text
12  
13 use Data::Integer qw(nint sint uint nint_is_sint nint_is_uint);
14  
15 $ni = nint($ni);
16 $si = sint($si);
17 $ui = uint($ui);
18 if(nint_is_sint($ni)) { ...
19 if(nint_is_uint($ni)) { ...
20  
21 use Data::Integer qw(
22 nint_sgn sint_sgn uint_sgn
23 nint_abs sint_abs uint_abs
24 nint_cmp sint_cmp uint_cmp
25 nint_min sint_min uint_min
26 nint_max sint_max uint_max
27 nint_neg sint_neg uint_neg
28 nint_add sint_add uint_add
29 nint_sub sint_sub uint_sub);
30  
31 $sn = nint_sgn($ni);
32 $sn = sint_sgn($si);
33 $sn = uint_sgn($ui);
34 $ni = nint_abs($ni);
35 $si = sint_abs($si);
36 $ui = uint_abs($ui);
37 @sorted_nints = sort { nint_cmp($a, $b) } @nints;
38 @sorted_sints = sort { sint_cmp($a, $b) } @sints;
39 @sorted_uints = sort { uint_cmp($a, $b) } @uints;
40 $ni = nint_min($na, $nb);
41 $si = sint_min($sa, $sb);
42 $ui = uint_min($ua, $ub);
43 $ni = nint_max($na, $nb);
44 $si = sint_max($sa, $sb);
45 $ui = uint_max($ua, $ub);
46 $ni = nint_neg($ni);
47 $si = sint_neg($si);
48 $ui = uint_neg($ui);
49 $ni = nint_add($na, $nb);
50 $si = sint_add($sa, $sb);
51 $ui = uint_add($ua, $ub);
52 $ni = nint_sub($na, $nb);
53 $si = sint_sub($sa, $sb);
54 $ui = uint_sub($ua, $ub);
55  
56 use Data::Integer qw(
57 sint_shl uint_shl
58 sint_shr uint_shr
59 sint_rol uint_rol
60 sint_ror uint_ror);
61  
62 $si = sint_shl($si, $dist);
63 $ui = uint_shl($ui, $dist);
64 $si = sint_shr($si, $dist);
65 $ui = uint_shr($ui, $dist);
66 $si = sint_rol($si, $dist);
67 $ui = uint_rol($ui, $dist);
68 $si = sint_ror($si, $dist);
69 $ui = uint_ror($ui, $dist);
70  
71 use Data::Integer qw(
72 nint_bits_as_sint nint_bits_as_uint
73 sint_bits_as_uint uint_bits_as_sint);
74  
75 $si = nint_bits_as_sint($ni);
76 $ui = nint_bits_as_uint($ni);
77 $ui = sint_bits_as_uint($si);
78 $si = uint_bits_as_sint($ui);
79  
80 use Data::Integer qw(
81 sint_not uint_not
82 sint_and uint_and
83 sint_nand uint_nand
84 sint_andn uint_andn
85 sint_or uint_or
86 sint_nor uint_nor
87 sint_orn uint_orn
88 sint_xor uint_xor
89 sint_nxor uint_nxor
90 sint_mux uint_mux);
91  
92 $si = sint_not($si);
93 $ui = uint_not($ui);
94 $si = sint_and($sa, $sb);
95 $ui = uint_and($ua, $ub);
96 $si = sint_nand($sa, $sb);
97 $ui = uint_nand($ua, $ub);
98 $si = sint_andn($sa, $sb);
99 $ui = uint_andn($ua, $ub);
100 $si = sint_or($sa, $sb);
101 $ui = uint_or($ua, $ub);
102 $si = sint_nor($sa, $sb);
103 $ui = uint_nor($ua, $ub);
104 $si = sint_orn($sa, $sb);
105 $ui = uint_orn($ua, $ub);
106 $si = sint_xor($sa, $sb);
107 $ui = uint_xor($ua, $ub);
108 $si = sint_nxor($sa, $sb);
109 $ui = uint_nxor($ua, $ub);
110 $si = sint_mux($sa, $sb, $sc);
111 $ui = uint_mux($ua, $ub, $uc);
112  
113 use Data::Integer qw(
114 sint_madd uint_madd
115 sint_msub uint_msub
116 sint_cadd uint_cadd
117 sint_csub uint_csub
118 sint_sadd uint_sadd
119 sint_ssub uint_ssub);
120  
121 $si = sint_madd($sa, $sb);
122 $ui = uint_madd($ua, $ub);
123 $si = sint_msub($sa, $sb);
124 $ui = uint_msub($ua, $ub);
125 ($carry, $si) = sint_cadd($sa, $sb, $carry);
126 ($carry, $ui) = uint_cadd($ua, $ub, $carry);
127 ($carry, $si) = sint_csub($sa, $sb, $carry);
128 ($carry, $ui) = uint_csub($ua, $ub, $carry);
129 $si = sint_sadd($sa, $sb);
130 $ui = uint_sadd($ua, $ub);
131 $si = sint_ssub($sa, $sb);
132 $ui = uint_ssub($ua, $ub);
133  
134 use Data::Integer qw(natint_hex hex_natint);
135  
136 print natint_hex($value);
137 $value = hex_natint($string);
138  
139 =head1 DESCRIPTION
140  
141 This module is about the native integer numerical data type. A native
142 integer is one of the types of datum that can appear in the numeric part
143 of a Perl scalar. This module supplies constants describing the native
144 integer type.
145  
146 There are actually two native integer representations: signed and
147 unsigned. Both are handled by this module.
148  
149 =head1 NATIVE INTEGERS
150  
151 Each native integer format represents a value using binary place
152 value, with some fixed number of bits. The number of bits is the
153 same for both signed and unsigned representations. In each case
154 the least-significant bit has the value 1, the next 2, the next 4,
155 and so on. In the unsigned representation, this pattern continues up
156 to and including the most-significant bit, which for a 32-bit machine
157 therefore has the value 2^31 (2147483648). The unsigned format cannot
158 represent any negative numbers.
159  
160 In the signed format, the most-significant bit is exceptional, having
161 the negation of the value that it does in the unsigned format. Thus on
162 a 32-bit machine this has the value -2^31 (-2147483648). Values with
163 this bit set are negative, and those with it clear are non-negative;
164 this bit is also known as the "sign bit".
165  
166 It is usual in machine arithmetic to use one of these formats at a
167 time, for example to add two signed numbers yielding a signed result.
168 However, Perl has a trick: a scalar with a native integer value contains
169 an additional flag bit which indicates whether the signed or unsigned
170 format is being used. It is therefore possible to mix signed and unsigned
171 numbers in arithmetic, at some extra expense.
172  
173 =cut
174  
175 package Data::Integer;
176  
177 9 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
178 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 9 our $VERSION = "0.007";
184  
185 9 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
186 9 our @EXPORT_OK = qw(
187 natint_bits
188 min_nint max_nint min_natint max_natint
189 min_sint max_sint min_signed_natint max_signed_natint
190 min_uint max_uint min_unsigned_natint max_unsigned_natint
191 nint sint uint
192 nint_is_sint nint_is_uint
193 nint_sgn sint_sgn uint_sgn
194 nint_abs sint_abs uint_abs
195 nint_cmp sint_cmp uint_cmp
196 nint_min sint_min uint_min
197 nint_max sint_max uint_max
198 nint_neg sint_neg uint_neg
199 nint_add sint_add uint_add
200 nint_sub sint_sub uint_sub
201 sint_shl uint_shl
202 sint_shr uint_shr
203 sint_rol uint_rol
204 sint_ror uint_ror
205 nint_bits_as_sint nint_bits_as_uint
206 sint_bits_as_uint uint_bits_as_sint
207 sint_not uint_not
208 sint_and uint_and
209 sint_nand uint_nand
210 sint_andn uint_andn
211 sint_or uint_or
212 sint_nor uint_nor
213 sint_orn uint_orn
214 sint_xor uint_xor
215 sint_nxor uint_nxor
216 sint_mux uint_mux
217 sint_madd uint_madd
218 sint_msub uint_msub
219 sint_cadd uint_cadd
220 sint_csub uint_csub
221 sint_sadd uint_sadd
222 sint_ssub uint_ssub
223 natint_hex hex_natint
224 );
225  
226 =head1 CONSTANTS
227  
228 Each of the extreme-value constants has two names, a short one and a
229 long one. The short names are more convenient to use, but the long
230 names are clearer in a context where other similar constants exist.
231  
232 Due to the risks of Perl changing the behaviour of a native integer value
233 that has been involved in floating point arithmetic (see L</BUGS>),
234 the extreme-value constants are actually non-constant functions that
235 always return a fresh copy of the appropriate value. The returned value
236 is always a pure native integer value, unsullied by floating point or
237 string operations.
238  
239 =over
240  
241 =item natint_bits
242  
243 The width, in bits, of the native integer data types.
244  
245 =cut
246  
247 # Count the number of bits in native integers by repeatedly shifting a bit
248 # left until it turns into the sign bit. "use integer" forces the use of a
249 # signed integer representation.
250 9 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
251 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
252 9 my $bit_count = 1;
253 9 my $test_bit = 1;
254 9 ▶ while($test_bit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_bit > 0) 567 9 576
255 567 $bit_count += 1;
256 567 $test_bit <<= 1;
257 }
258 9 my $natint_bits = $bit_count;
259 0 ▶ *natint_bits = sub () { $natint_bits };
Subroutine __ANON__: not called CC=1 SCAR=1.2
260 }
261  
262 =item min_nint
263  
264 =item min_natint
265  
266 The minimum representable value in either representation. This is
267 -2^(natint_bits - 1).
268  
269 =cut
270  
271 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
272 9 ▶ my $min_nint = do { use integer; 1 << (natint_bits - 1) };
Subroutine BEGIN: called CC=1 SCAR=0.0
273 9 ▶ *min_natint = *min_nint = sub() { my $ret = $min_nint };
Subroutine __ANON__: called CC=1 SCAR=0.0
274 }
275  
276 =item max_nint
277  
278 =item max_natint
279  
280 The maximum representable value in either representation. This is
281 2^natint_bits - 1.
282  
283 =cut
284  
285 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
286 9 my $max_nint = ~0;
287 9 ▶ *max_natint = *max_nint = sub() { my $ret = $max_nint };
Subroutine __ANON__: called CC=1 SCAR=0.0
288 }
289  
290 =item min_sint
291  
292 =item min_signed_natint
293  
294 The minimum representable value in the signed representation. This is
295 -2^(natint_bits - 1).
296  
297 =cut
298  
299 9 ▶BEGIN { *min_signed_natint = *min_sint = \&min_nint; }
Subroutine BEGIN: called CC=1 SCAR=0.0
300  
301 =item max_sint
302  
303 =item max_signed_natint
304  
305 The maximum representable value in the signed representation. This is
306 2^(natint_bits - 1) - 1.
307  
308 =cut
309  
310 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
311 9 my $max_sint = ~min_sint;
312 9 ▶ *max_signed_natint = *max_sint = sub() { my $ret = $max_sint };
Subroutine __ANON__: called CC=1 SCAR=0.0
313 }
314  
315 =item min_uint
316  
317 =item min_unsigned_natint
318  
319 The minimum representable value in the unsigned representation.
320 This is zero.
321  
322 =cut
323  
324 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
325 9 my $min_uint = 0;
326 9 ▶ *min_unsigned_natint = *min_uint = sub() { my $ret = $min_uint };
Subroutine __ANON__: called CC=1 SCAR=0.0
327 }
328  
329 =item max_uint
330  
331 =item max_unsigned_natint
332  
333 The maximum representable value in the unsigned representation. This is
334 2^natint_bits - 1.
335  
336 =cut
337  
338 9 ▶BEGIN { *max_unsigned_natint = *max_uint = \&max_nint; }
Subroutine BEGIN: called CC=1 SCAR=0.0
339  
340 =back
341  
342 =head1 FUNCTIONS
343  
344 Each "nint_", "sint_", or "uint_" function operates on one of the three
345 integer formats. "nint_" functions operate on Perl's union of signed
346 and unsigned; "sint_" functions operate on signed integers; and "uint_"
347 functions operate on unsigned integers. Except where indicated otherwise,
348 the function returns a value of its primary type.
349  
350 Parameters I<A>, I<B>, and I<C>, where present, must be numbers of
351 the appropriate type: specifically, with a numerical value that can be
352 represented in that type. If there are multiple flavours of zero, due
353 to floating point funkiness, all zeroes are treated the same. Parameters
354 with other names have other requirements, explained with each function.
355  
356 The functions attempt to detect unsuitable arguments, and C<die> if
357 an invalid argument is detected, but they can't notice some kinds of
358 incorrect argument. Generally, it is the caller's responsibility to
359 provide a sane numerical argument, and supplying an invalid argument will
360 cause mayhem. Only the numeric value of plain scalar arguments is used;
361 the string value is completely ignored, so dualvars are not a problem.
362  
363 =head2 Canonicalisation and classification
364  
365 These are basic glue functions.
366  
367 =over
368  
369 =item nint(A)
370  
371 =item sint(A)
372  
373 =item uint(A)
374  
375 These functions each take an argument in a specific integer format and
376 return its numerical value. This is the argument canonicalisation that is
377 performed by all of the functions in this module, presented in isolation.
378  
379 =cut
380  
381 4669 ▶sub nint($) {
Subroutine nint: called CC=5 SCAR=16.1
Pod documented
382 4669 my $tval = $_[0];
383 4669 ▶ croak "not a native integer"
Branch: 1 branch
BranchTrueFalseTotal
unless int $tval == $tval and $tval >= &min_nint() and $tval <= &max_nint() 3 4666 4669
Condition100%6/6
int $tval == $tval and $tval >= &min_nint() and $tval <= &max_nint()
!ll&&!rl&&r
214666
int $tval == $tval and $tval >= &min_nint()
!ll&&!rl&&r
114667
MC/DC100%3/3
int $tval == $tval and $tval >= &min_nint() and $tval <= &max_nint()
int $tval == $tval $tval >= &min_nint() $tval <= &max_nint()
Truth table100%4/4
A and B and C
A= int $tval == $tvalB= $tval >= &min_nint()C= $tval <= &max_nint()
ABCresult
0XX0
10X0
1100
1111
384 unless int($tval) == $tval && $tval >= min_nint &&
385 $tval <= max_nint;
386 9 ▶ return ($tval = $_[0]) < 0 ? do { use integer; 0 | $_[0] } : 0 | $_[0];
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
($tval = $_[0]) < 0 ? : 1587 3079 4666
387 }
388  
389 3930 ▶sub sint($) {
Subroutine sint: called CC=6 SCAR=17.9
Pod documented
390 3930 my $tval = $_[0];
391 3930 ▶ croak "not a signed native integer"
Branch: 1 branch
BranchTrueFalseTotal
unless int $tval == $tval and $tval >= &min_sint() and $tval <= &max_sint() 8 3922 3930
Condition100%6/6
int $tval == $tval and $tval >= &min_sint() and $tval <= &max_sint()
!ll&&!rl&&r
263922
int $tval == $tval and $tval >= &min_sint()
!ll&&!rl&&r
113928
MC/DC100%3/3
int $tval == $tval and $tval >= &min_sint() and $tval <= &max_sint()
int $tval == $tval $tval >= &min_sint() $tval <= &max_sint()
Truth table100%4/4
A and B and C
A= int $tval == $tvalB= $tval >= &min_sint()C= $tval <= &max_sint()
ABCresult
0XX0
10X0
1100
1111
392 unless int($tval) == $tval && $tval >= min_sint &&
393 $tval <= max_sint;
394 9 ▶ my $val = do { use integer; 0 | $_[0] };
Subroutine BEGIN: called CC=1 SCAR=0.0
395 croak "not a signed native integer"
396 9 ▶ if $tval >= 0 && do { use integer; $val < 0 };
Subroutine BEGIN: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
if $tval >= 0 and do { use integer; $val < 0 } 0 3922 3922
Condition66%2/3
$tval >= 0 and do { use integer; $val < 0 }
!ll&&!rl&&r
151624060
MC/DC0%0/2
$tval >= 0 and do { use integer; $val < 0 }
$tval >= 0 do { use integer; $val < 0 }
Truth table66%2/3
A and B
A= $tval >= 0B= do { use integer; $val < 0 }
ABresult
0X0
100
111
397 3922 return $val;
398 }
399  
400 4135 ▶sub uint($) {
Subroutine uint: called CC=4 SCAR=13.9
Pod documented
401 4135 my $tval = $_[0];
402 4135 ▶ croak "not an unsigned native integer"
Branch: 1 branch
BranchTrueFalseTotal
unless int $tval == $tval and $tval >= &min_uint() and $tval <= &max_uint() 7 4128 4135
Condition100%6/6
int $tval == $tval and $tval >= &min_uint() and $tval <= &max_uint()
!ll&&!rl&&r
614128
int $tval == $tval and $tval >= &min_uint()
!ll&&!rl&&r
154129
MC/DC100%3/3
int $tval == $tval and $tval >= &min_uint() and $tval <= &max_uint()
int $tval == $tval $tval >= &min_uint() $tval <= &max_uint()
Truth table100%4/4
A and B and C
A= int $tval == $tvalB= $tval >= &min_uint()C= $tval <= &max_uint()
ABCresult
0XX0
10X0
1100
1111
403 unless int($tval) == $tval && $tval >= min_uint &&
404 $tval <= max_uint;
405 4128 return 0 | $_[0];
406 }
407  
408 =item nint_is_sint(A)
409  
410 Takes a native integer of either type. Returns a truth value indicating
411 whether this value can be exactly represented as a signed native integer.
412  
413 =cut
414  
415 1314 ▶sub nint_is_sint($) {
Subroutine nint_is_sint: called CC=2 SCAR=6.9
Pod documented
416 1314 my $val = nint($_[0]);
417 return (my $tval = $val) < 0 ||
418 9 ▶ do { use integer; ($val & min_sint) == 0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition100%3/3
(my $tval = $val) < 0 || do { use integer; ($val & &min_sint()) == 0 }
l!l&&r!l&&!r
456587271
MC/DC100%2/2
(my $tval = $val) < 0 || do { use integer; ($val & &min_sint()) == 0 }
(my $tval = $val) < 0 do { use integer; ($val & &min_sint()) == 0 }
Truth table100%3/3
A || B
A= (my $tval = $val) < 0B= do { use integer; ($val & &min_sint()) == 0 }
ABresult
1X1
011
000
419 }
420  
421 =item nint_is_uint(A)
422  
423 Takes a native integer of either type. Returns a truth value indicating
424 whether this value can be exactly represented as an unsigned native
425 integer.
426  
427 =cut
428  
429 1099 ▶sub nint_is_uint($) { nint($_[0]) >= 0 }
Subroutine nint_is_uint: called CC=1 SCAR=0.0
Pod documented
430  
431 =back
432  
433 =head2 Arithmetic
434  
435 These functions operate on numerical values rather than just bit patterns.
436 They will all C<die> if the true numerical result doesn't fit into the
437 result format, rather than give a wrong answer.
438  
439 =over
440  
441 =item nint_sgn(A)
442  
443 =item sint_sgn(A)
444  
445 =item uint_sgn(A)
446  
447 Returns +1 if the argument is positive, 0 if the argument is zero,
448 or -1 if the argument is negative.
449  
450 =cut
451  
452 21 ▶sub nint_sgn($) { nint($_[0]) <=> 0 }
Subroutine nint_sgn: called CC=1 SCAR=0.0
Pod documented
453  
454 9 ▶sub sint_sgn($) { use integer; sint($_[0]) <=> 0 }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_sgn: called CC=1 SCAR=0.0
Pod documented
455  
456 9 ▶sub uint_sgn($) { use integer; uint($_[0]) == 0 ? 0 : +1 }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_sgn: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
&uint($_[0]) == 0 ? : 1 7 8
457  
458 =item nint_abs(A)
459  
460 =item sint_abs(A)
461  
462 =item uint_abs(A)
463  
464 Absolute value (magnitude, discarding sign).
465  
466 =cut
467  
468 21 ▶sub nint_abs($) {
Subroutine nint_abs: called CC=3 SCAR=11.0
Pod documented
469 21 my $a = nint($_[0]);
470 21 ▶ if((my $tval = $a) >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $tval = $a) >= 0) { } 14 7 21
elsif (do { use integer; $a == &min_sint() }) { } 1 6 7
471 14 return $a;
472 9 ▶ } elsif(do { use integer; $a == min_sint }) {
Subroutine BEGIN: called CC=1 SCAR=0.0
473 1 return 0 | min_sint;
474 } else {
475 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
476 6 return -$a;
477 }
478 }
479  
480 8 ▶sub sint_abs($) {
Subroutine sint_abs: called CC=3 SCAR=11.0
Pod documented
481 8 my $a = sint($_[0]);
482 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
483 8 ▶ croak "integer overflow" if $a == min_sint;
Branch: 1 branch
BranchTrueFalseTotal
if $a == &min_sint() 1 7 8
484 7 ▶ return $a < 0 ? -$a : $a;
Branch: 1 branch
BranchTrueFalseTotal
$a < 0 ? : 3 4 7
485 }
486  
487 9 *uint_abs = \&uint;
488  
489 =item nint_cmp(A, B)
490  
491 =item sint_cmp(A, B)
492  
493 =item uint_cmp(A, B)
494  
495 Arithmetic comparison. Returns -1, 0, or +1, indicating whether A is
496 less than, equal to, or greater than B.
497  
498 =cut
499  
500 196 ▶sub nint_cmp($$) {
Subroutine nint_cmp: called CC=4 SCAR=13.9
Pod documented
501 196 my($a, $b) = (nint($_[0]), nint($_[1]));
502 196 ▶ if((my $ta = $a) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ta = $a) < 0) { } 70 126 196
503 70 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 25 45 70
504 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
505 25 return $a <=> $b;
506 } else {
507 45 return -1;
508 }
509 } else {
510 126 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 45 81 126
511 45 return 1;
512 } else {
513 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
514 81 return ($a ^ min_sint) <=> ($b ^ min_sint);
515 }
516 }
517 }
518  
519 9 ▶sub sint_cmp($$) { use integer; sint($_[0]) <=> sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_cmp: called CC=1 SCAR=0.0
Pod documented
520  
521 81 ▶sub uint_cmp($$) {
Subroutine uint_cmp: called CC=1 SCAR=0.0
Pod documented
522 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
523 81 return (uint($_[0]) ^ min_sint) <=> (uint($_[1]) ^ min_sint);
524 }
525  
526 =item nint_min(A, B)
527  
528 =item sint_min(A, B)
529  
530 =item uint_min(A, B)
531  
532 Arithmetic minimum. Returns the arithmetically lesser of the two
533 arguments.
534  
535 =cut
536  
537 196 ▶sub nint_min($$) {
Subroutine nint_min: called CC=6 SCAR=17.9
Pod documented
538 196 my($a, $b) = (nint($_[0]), nint($_[1]));
539 196 ▶ if((my $ta = $a) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ta = $a) < 0) { } 70 126 196
540 70 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 25 45 70
541 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
542 25 ▶ return $a < $b ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 10 15 25
543 } else {
544 45 return $a;
545 }
546 } else {
547 126 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 45 81 126
548 45 return $b;
549 } else {
550 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
551 81 ▶ return ($a ^ min_sint) < ($b ^ min_sint) ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
($a ^ &min_sint()) < ($b ^ &min_sint()) ? : 36 45 81
552 }
553 }
554 }
555  
556 121 ▶sub sint_min($$) {
Subroutine sint_min: called CC=2 SCAR=6.9
Pod documented
557 121 my($a, $b) = (sint($_[0]), sint($_[1]));
558 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
559 121 ▶ return $a < $b ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 55 66 121
560 }
561  
562 81 ▶sub uint_min($$) {
Subroutine uint_min: called CC=2 SCAR=6.9
Pod documented
563 81 my($a, $b) = (uint($_[0]), uint($_[1]));
564 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
565 81 ▶ return ($a ^ min_sint) < ($b ^ min_sint) ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
($a ^ &min_sint()) < ($b ^ &min_sint()) ? : 36 45 81
566 }
567  
568 =item nint_max(A, B)
569  
570 =item sint_max(A, B)
571  
572 =item uint_max(A, B)
573  
574 Arithmetic maximum. Returns the arithmetically greater of the two
575 arguments.
576  
577 =cut
578  
579 196 ▶sub nint_max($$) {
Subroutine nint_max: called CC=6 SCAR=17.9
Pod documented
580 196 my($a, $b) = (nint($_[0]), nint($_[1]));
581 196 ▶ if((my $ta = $a) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ta = $a) < 0) { } 70 126 196
582 70 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 25 45 70
583 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
584 25 ▶ return $a < $b ? $b : $a;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 10 15 25
585 } else {
586 45 return $b;
587 }
588 } else {
589 126 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 45 81 126
590 45 return $a;
591 } else {
592 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
593 81 ▶ return ($a ^ min_sint) < ($b ^ min_sint) ? $b : $a;
Branch: 1 branch
BranchTrueFalseTotal
($a ^ &min_sint()) < ($b ^ &min_sint()) ? : 36 45 81
594 }
595 }
596 }
597  
598 121 ▶sub sint_max($$) {
Subroutine sint_max: called CC=2 SCAR=6.9
Pod documented
599 121 my($a, $b) = (sint($_[0]), sint($_[1]));
600 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
601 121 ▶ return $a < $b ? $b : $a;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 55 66 121
602 }
603  
604 81 ▶sub uint_max($$) {
Subroutine uint_max: called CC=2 SCAR=6.9
Pod documented
605 81 my($a, $b) = (uint($_[0]), uint($_[1]));
606 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
607 81 ▶ return ($a ^ min_sint) < ($b ^ min_sint) ? $b : $a;
Branch: 1 branch
BranchTrueFalseTotal
($a ^ &min_sint()) < ($b ^ &min_sint()) ? : 36 45 81
608 }
609  
610 =item nint_neg(A)
611  
612 =item sint_neg(A)
613  
614 =item uint_neg(A)
615  
616 Negation: returns -A.
617  
618 =cut
619  
620 12 ▶sub nint_neg($) {
Subroutine nint_neg: called CC=3 SCAR=11.0
Pod documented
621 12 my $a = nint($_[0]);
622 12 ▶ if((my $ta = $a) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ta = $a) <= 0) { } 5 7 12
623 9 ▶ return 0 | do { use integer; -$a };
Subroutine BEGIN: called CC=1 SCAR=0.0
624 } else {
625 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
626 7 my $neg = -$a;
627 7 ▶ croak "integer overflow" if $neg >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $neg >= 0 3 4 7
628 4 return $neg;
629 }
630 }
631  
632 8 ▶sub sint_neg($) {
Subroutine sint_neg: called CC=2 SCAR=6.9
Pod documented
633 8 my $a = sint($_[0]);
634 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
635 8 ▶ croak "integer overflow" if $a == min_sint;
Branch: 1 branch
BranchTrueFalseTotal
if $a == &min_sint() 1 7 8
636 7 return -$a;
637 }
638  
639 8 ▶sub uint_neg($) {
Subroutine uint_neg: called CC=2 SCAR=6.9
Pod documented
640 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
641 8 ▶ croak "integer overflow" unless uint($_[0]) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless &uint($_[0]) == 0 7 1 8
642 1 return my $zero = 0;
643 }
644  
645 =item nint_add(A, B)
646  
647 =item sint_add(A, B)
648  
649 =item uint_add(A, B)
650  
651 Addition: returns A + B.
652  
653 =cut
654  
655 252 ▶sub nint_add($$) {
Subroutine nint_add: called CC=8 SCAR=20.8
Pod documented
656 252 my($a, $b) = (nint($_[0]), nint($_[1]));
657 252 ▶ if((my $ta = $a) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $ta = $a) < 0) { } 89 163 252
658 89 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 34 55 89
659 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
660 34 my $r = $a + $b;
661 34 ▶ croak "integer overflow" if $r > $a;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $a 16 18 34
662 18 return $r;
663 } else {
664 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
665 55 my $r = $a + $b;
666 9 ▶ $r = do { no integer; 0 | $r } if $r < $a;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $r < $a 7 48 55
667 55 return $r;
668 }
669 } else {
670 163 ▶ if((my $tb = $b) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 55 108 163
671 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
672 55 my $r = $a + $b;
673 9 ▶ $r = do { no integer; 0 | $r } if $r < $b;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $r < $b 7 48 55
674 55 return $r;
675 } else {
676 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
677 108 my $r = $a + $b;
678 108 ▶ croak "integer overflow"
Branch: 1 branch
BranchTrueFalseTotal
if ($r ^ &min_sint()) < ($a ^ &min_sint()) 40 68 108
679 if ($r ^ min_sint) < ($a ^ min_sint);
680 9 ▶ return do { no integer; 0 | $r };
Subroutine BEGIN: called CC=1 SCAR=0.0
681 }
682 }
683 }
684  
685 148 ▶sub sint_add($$) {
Subroutine sint_add: called CC=3 SCAR=11.0
Pod documented
686 148 my($a, $b) = (sint($_[0]), sint($_[1]));
687 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
688 148 my $r = $a + $b;
689 148 ▶ croak "integer overflow" if $b < 0 ? $r > $a : $r < $a;
Branch: 2 branches
BranchTrueFalseTotal
if $b < 0 ? $r > $a : $r < $a 36 112 148
$b < 0 ? : 68 80 148
690 112 return $r;
691 }
692  
693 108 ▶sub uint_add($$) {
Subroutine uint_add: called CC=2 SCAR=6.9
Pod documented
694 108 my($a, $b) = (uint($_[0]), uint($_[1]));
695 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
696 108 my $r = $a + $b;
697 108 ▶ croak "integer overflow" if ($r ^ min_sint) < ($a ^ min_sint);
Branch: 1 branch
BranchTrueFalseTotal
if ($r ^ &min_sint()) < ($a ^ &min_sint()) 40 68 108
698 9 ▶ return do { no integer; 0 | $r };
Subroutine BEGIN: called CC=1 SCAR=0.0
699 }
700  
701 =item nint_sub(A, B)
702  
703 =item sint_sub(A, B)
704  
705 =item uint_sub(A, B)
706  
707 Subtraction: returns A - B.
708  
709 =cut
710  
711 234 ▶sub nint_sub($$) {
Subroutine nint_sub: called CC=12 SCAR=24.8
Pod documented
712 234 my($a, $b) = (nint($_[0]), nint($_[1]));
713 234 ▶ if((my $ta = $a) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $ta = $a) < 0) { } 63 171 234
elsif (not $a & &min_sint()) { } 106 65 171
714 63 ▶ if((my $tb = $b) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 31 32 63
elsif (not $b & &min_sint()) { } 22 10 32
715 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
716 31 return $a - $b;
717 } elsif(!($b & min_sint)) {
718 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
719 22 my $r = $a - $b;
720 22 ▶ croak "integer overflow" if $r >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r >= 0 9 13 22
721 13 return $r;
722 } else {
723 10 croak "integer overflow";
724 }
725 } elsif(!($a & min_sint)) {
726 106 ▶ if((my $tb = $b) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 35 71 106
elsif (not $b & &min_sint()) { } 47 24 71
727 9 ▶ return 0 | do { use integer; $a - $b };
Subroutine BEGIN: called CC=1 SCAR=0.0
728 } elsif(!($b & min_sint)) {
729 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
730 47 return $a - $b;
731 } else {
732 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
733 24 my $r = $a - $b;
734 24 ▶ croak "integer overflow" if $r >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r >= 0 10 14 24
735 14 return $r;
736 }
737 } else {
738 65 ▶ if((my $tb = $b) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((my $tb = $b) < 0) { } 16 49 65
elsif (not $b & &min_sint()) { } 31 18 49
739 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
740 16 my $r = $a - $b;
741 16 ▶ croak "integer overflow" if $r >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r >= 0 9 7 16
742 9 ▶ return do { no integer; 0 | $r };
Subroutine BEGIN: called CC=1 SCAR=0.0
743 } elsif(!($b & min_sint)) {
744 9 ▶ return 0 | do { use integer; $a - $b };
Subroutine BEGIN: called CC=1 SCAR=0.0
745 } else {
746 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
747 18 return $a - $b;
748 }
749 }
750 }
751  
752 135 ▶sub sint_sub($$) {
Subroutine sint_sub: called CC=3 SCAR=11.0
Pod documented
753 135 my($a, $b) = (sint($_[0]), sint($_[1]));
754 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
755 135 my $r = $a - $b;
756 135 ▶ croak "integer overflow" if $b > 0 ? $r > $a : $r < $a;
Branch: 2 branches
BranchTrueFalseTotal
if $b > 0 ? $r > $a : $r < $a 23 112 135
$b > 0 ? : 59 76 135
757 112 return $r;
758 }
759  
760 120 ▶sub uint_sub($$) {
Subroutine uint_sub: called CC=2 SCAR=6.9
Pod documented
761 120 my($a, $b) = (uint($_[0]), uint($_[1]));
762 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
763 120 my $r = $a - $b;
764 120 ▶ croak "integer overflow" if ($r ^ min_sint) > ($a ^ min_sint);
Branch: 1 branch
BranchTrueFalseTotal
if ($r ^ &min_sint()) > ($a ^ &min_sint()) 52 68 120
765 9 ▶ return do { no integer; 0 | $r };
Subroutine BEGIN: called CC=1 SCAR=0.0
766 }
767  
768 =back
769  
770 =head2 Bit shifting
771  
772 These functions all operate on the bit patterns representing integers,
773 mostly ignoring the numerical values represented. In most cases the
774 results for particular numerical arguments are influenced by the word
775 size, because that determines where a bit being left-shifted will drop
776 off the end of the word and where a bit will be shifted in during a
777 rightward shift.
778  
779 With the exception of rightward shifts (see below), each pair of
780 functions performs exactly the same operations on the bit sequences.
781 There inevitably can't be any functions here that operate on Perl's union
782 of signed and unsigned; you must choose, by which function you call,
783 which type the result is to be tagged as.
784  
785 =over
786  
787 =item sint_shl(A, DIST)
788  
789 =item uint_shl(A, DIST)
790  
791 Bitwise left shift (towards more-significant bits). I<DIST> is the
792 distance to shift, in bits, and must be an integer in the range [0,
793 natint_bits). Zeroes are shifted in from the right.
794  
795 =cut
796  
797 21 ▶sub sint_shl($$) {
Subroutine sint_shl: called CC=2 SCAR=7.0
Pod documented
798 21 my($val, $dist) = @_;
799 21 $dist = uint($dist);
800 21 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 21 21
801 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
802 21 return sint($val) << $dist;
803 }
804  
805 21 ▶sub uint_shl($$) {
Subroutine uint_shl: called CC=2 SCAR=7.0
Pod documented
806 21 my($val, $dist) = @_;
807 21 $dist = uint($dist);
808 21 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 21 21
809 9 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
810 21 return uint($val) << $dist;
811 }
812  
813 =item sint_shr(A, DIST)
814  
815 =item uint_shr(A, DIST)
816  
817 Bitwise right shift (towards less-significant bits). I<DIST> is the
818 distance to shift, in bits, and must be an integer in the range [0,
819 natint_bits).
820  
821 When performing an unsigned right shift, zeroes are shifted in from the
822 left. A signed right shift is different: the sign bit gets duplicated,
823 so right-shifting a negative number always gives a negative result.
824  
825 =cut
826  
827 17 ▶sub sint_shr($$) {
Subroutine sint_shr: called CC=2 SCAR=7.0
Pod documented
828 17 my($val, $dist) = @_;
829 17 $dist = uint($dist);
830 17 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 17 17
831 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
832 17 return sint($val) >> $dist;
833 }
834  
835 17 ▶sub uint_shr($$) {
Subroutine uint_shr: called CC=2 SCAR=7.0
Pod documented
836 17 my($val, $dist) = @_;
837 17 $dist = uint($dist);
838 17 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 17 17
839 9 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
840 17 return uint($val) >> $dist;
841 }
842  
843 =item sint_rol(A, DIST)
844  
845 =item uint_rol(A, DIST)
846  
847 Bitwise left rotation (towards more-significant bits, with the
848 most-significant bit wrapping round to the least-significant bit).
849 I<DIST> is the distance to rotate, in bits, and must be an integer in
850 the range [0, natint_bits).
851  
852 =cut
853  
854 21 ▶sub sint_rol($$) {
Subroutine sint_rol: called CC=3 SCAR=11.0
Pod documented
855 21 my($val, $dist) = @_;
856 21 $dist = uint($dist);
857 21 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 21 21
858 21 $val = sint($val);
859 21 ▶ return $val if $dist == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dist == 0 4 17 21
860 17 my $low_val = $val >> (natint_bits - $dist);
861 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
862 17 return $low_val | ($val << $dist);
863 }
864  
865 21 ▶sub uint_rol($$) {
Subroutine uint_rol: called CC=3 SCAR=11.0
Pod documented
866 21 my($val, $dist) = @_;
867 21 $dist = uint($dist);
868 21 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 21 21
869 21 $val = uint($val);
870 21 ▶ return $val if $dist == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dist == 0 4 17 21
871 17 return ($val >> (natint_bits - $dist)) | ($val << $dist);
872 }
873  
874 =item sint_ror(A, DIST)
875  
876 =item uint_ror(A, DIST)
877  
878 Bitwise right rotation (towards less-significant bits, with the
879 least-significant bit wrapping round to the most-significant bit).
880 I<DIST> is the distance to rotate, in bits, and must be an integer in
881 the range [0, natint_bits).
882  
883 =cut
884  
885 21 ▶sub sint_ror($$) {
Subroutine sint_ror: called CC=3 SCAR=11.0
Pod documented
886 21 my($val, $dist) = @_;
887 21 $dist = uint($dist);
888 21 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 21 21
889 21 $val = sint($val);
890 21 ▶ return $val if $dist == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dist == 0 4 17 21
891 17 my $low_val = $val >> $dist;
892 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
893 17 return $low_val | ($val << (natint_bits - $dist));
894 }
895  
896 21 ▶sub uint_ror($$) {
Subroutine uint_ror: called CC=3 SCAR=11.0
Pod documented
897 21 my($val, $dist) = @_;
898 21 $dist = uint($dist);
899 21 ▶ croak "shift distance exceeds word size" if $dist >= natint_bits;
Branch: 1 branch
BranchTrueFalseTotal
if $dist >= 64 0 21 21
900 21 $val = uint($val);
901 21 ▶ return $val if $dist == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dist == 0 4 17 21
902 17 return ($val >> $dist) | ($val << (natint_bits - $dist));
903 }
904  
905 =back
906  
907 =head2 Format conversion
908  
909 These functions convert between the various native integer formats
910 by reinterpreting the bit patterns used to represent the integers.
911 The bit pattern remains unchanged; its meaning changes, and so the
912 numerical value changes. Perl scalars preserve the numerical value,
913 rather than just the bit pattern, so from the Perl point of view these
914 are functions that change numbers into other numbers.
915  
916 =over
917  
918 =item nint_bits_as_sint(A)
919  
920 Converts a native integer of either type to a signed integer, by
921 reinterpreting the bits. The most-significant bit (whether a sign bit
922 or not) becomes a sign bit.
923  
924 =cut
925  
926 9 ▶sub nint_bits_as_sint($) { use integer; nint($_[0]) | 0 }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine nint_bits_as_sint: called CC=1 SCAR=0.0
Pod documented
927  
928 =item nint_bits_as_uint(A)
929  
930 Converts a native integer of either type to an unsigned integer, by
931 reinterpreting the bits. The most-significant bit (whether a sign bit
932 or not) becomes an ordinary most-significant bit.
933  
934 =cut
935  
936 9 ▶sub nint_bits_as_uint($) { no integer; nint($_[0]) | 0 }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine nint_bits_as_uint: called CC=1 SCAR=0.0
Pod documented
937  
938 =item sint_bits_as_uint(A)
939  
940 Converts a signed integer to an unsigned integer, by reinterpreting
941 the bits. The sign bit becomes an ordinary most-significant bit.
942  
943 =cut
944  
945 9 ▶sub sint_bits_as_uint($) { no integer; sint($_[0]) | 0 }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_bits_as_uint: called CC=1 SCAR=0.0
Pod documented
946  
947 =item uint_bits_as_sint(A)
948  
949 Converts an unsigned integer to a signed integer, by reinterpreting
950 the bits. The most-significant bit becomes a sign bit.
951  
952 =cut
953  
954 9 ▶sub uint_bits_as_sint($) { use integer; uint($_[0]) | 0 }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_bits_as_sint: called CC=1 SCAR=0.0
Pod documented
955  
956 =back
957  
958 =head2 Bitwise operations
959  
960 These functions all operate on the bit patterns representing integers,
961 completely ignoring the numerical values represented. They are mostly
962 not influenced by the word size, in the sense that they will produce
963 the same numerical result for the same numerical arguments regardless
964 of word size. However, a few are affected by the word size: those on
965 unsigned operands that return a non-zero result if given zero arguments.
966  
967 Each pair of functions performs exactly the same operations on the bit
968 sequences. There inevitably can't be any functions here that operate on
969 Perl's union of signed and unsigned; you must choose, by which function
970 you call, which type the result is to be tagged as.
971  
972 =over
973  
974 =item sint_not(A)
975  
976 =item uint_not(A)
977  
978 Bitwise complement (NOT).
979  
980 =cut
981  
982 9 ▶sub sint_not($) { use integer; ~sint($_[0]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_not: called CC=1 SCAR=0.0
Pod documented
983  
984 9 ▶sub uint_not($) { no integer; ~uint($_[0]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_not: called CC=1 SCAR=0.0
Pod documented
985  
986 =item sint_and(A, B)
987  
988 =item uint_and(A, B)
989  
990 Bitwise conjunction (AND).
991  
992 =cut
993  
994 9 ▶sub sint_and($$) { use integer; sint($_[0]) & sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_and: called CC=1 SCAR=0.0
Pod documented
995  
996 9 ▶sub uint_and($$) { no integer; uint($_[0]) & uint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_and: called CC=1 SCAR=0.0
Pod documented
997  
998 =item sint_nand(A, B)
999  
1000 =item uint_nand(A, B)
1001  
1002 Bitwise inverted conjunction (NAND).
1003  
1004 =cut
1005  
1006 9 ▶sub sint_nand($$) { use integer; ~(sint($_[0]) & sint($_[1])) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_nand: called CC=1 SCAR=0.0
Pod documented
1007  
1008 9 ▶sub uint_nand($$) { no integer; ~(uint($_[0]) & uint($_[1])) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_nand: called CC=1 SCAR=0.0
Pod documented
1009  
1010 =item sint_andn(A, B)
1011  
1012 =item uint_andn(A, B)
1013  
1014 Bitwise conjunction with inverted argument (A AND (NOT B)).
1015  
1016 =cut
1017  
1018 9 ▶sub sint_andn($$) { use integer; sint($_[0]) & ~sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_andn: called CC=1 SCAR=0.0
Pod documented
1019  
1020 9 ▶sub uint_andn($$) { no integer; uint($_[0]) & ~uint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_andn: called CC=1 SCAR=0.0
Pod documented
1021  
1022 =item sint_or(A, B)
1023  
1024 =item uint_or(A, B)
1025  
1026 Bitwise disjunction (OR).
1027  
1028 =cut
1029  
1030 9 ▶sub sint_or($$) { use integer; sint($_[0]) | sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_or: called CC=1 SCAR=0.0
Pod documented
1031  
1032 9 ▶sub uint_or($$) { no integer; uint($_[0]) | uint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_or: called CC=1 SCAR=0.0
Pod documented
1033  
1034 =item sint_nor(A, B)
1035  
1036 =item uint_nor(A, B)
1037  
1038 Bitwise inverted disjunction (NOR).
1039  
1040 =cut
1041  
1042 9 ▶sub sint_nor($$) { use integer; ~(sint($_[0]) | sint($_[1])) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_nor: called CC=1 SCAR=0.0
Pod documented
1043  
1044 9 ▶sub uint_nor($$) { no integer; ~(uint($_[0]) | uint($_[1])) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_nor: called CC=1 SCAR=0.0
Pod documented
1045  
1046 =item sint_orn(A, B)
1047  
1048 =item uint_orn(A, B)
1049  
1050 Bitwise disjunction with inverted argument (A OR (NOT B)).
1051  
1052 =cut
1053  
1054 9 ▶sub sint_orn($$) { use integer; sint($_[0]) | ~sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_orn: called CC=1 SCAR=0.0
Pod documented
1055  
1056 9 ▶sub uint_orn($$) { no integer; uint($_[0]) | ~uint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_orn: called CC=1 SCAR=0.0
Pod documented
1057  
1058 =item sint_xor(A, B)
1059  
1060 =item uint_xor(A, B)
1061  
1062 Bitwise symmetric difference (XOR).
1063  
1064 =cut
1065  
1066 9 ▶sub sint_xor($$) { use integer; sint($_[0]) ^ sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_xor: called CC=1 SCAR=0.0
Pod documented
1067  
1068 9 ▶sub uint_xor($$) { no integer; uint($_[0]) ^ uint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_xor: called CC=1 SCAR=0.0
Pod documented
1069  
1070 =item sint_nxor(A, B)
1071  
1072 =item uint_nxor(A, B)
1073  
1074 Bitwise symmetric similarity (NXOR).
1075  
1076 =cut
1077  
1078 9 ▶sub sint_nxor($$) { use integer; ~(sint($_[0]) ^ sint($_[1])) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_nxor: called CC=1 SCAR=0.0
Pod documented
1079  
1080 9 ▶sub uint_nxor($$) { no integer; ~(uint($_[0]) ^ uint($_[1])) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_nxor: called CC=1 SCAR=0.0
Pod documented
1081  
1082 =item sint_mux(A, B, C)
1083  
1084 =item uint_mux(A, B, C)
1085  
1086 Bitwise multiplex. The output has a bit from B wherever A has a 1 bit,
1087 and a bit from C wherever A has a 0 bit. That is, the result is (A AND B)
1088 OR ((NOT A) AND C).
1089  
1090 =cut
1091  
1092 10 ▶sub sint_mux($$$) {
Subroutine sint_mux: called CC=1 SCAR=0.0
Pod documented
1093 10 my $a = sint($_[0]);
1094 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1095 10 return ($a & sint($_[1])) | (~$a & sint($_[2]));
1096 }
1097  
1098 10 ▶sub uint_mux($$$) {
Subroutine uint_mux: called CC=1 SCAR=0.0
Pod documented
1099 10 my $a = uint($_[0]);
1100 9 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1101 10 return ($a & uint($_[1])) | (~$a & uint($_[2]));
1102 }
1103  
1104 =back
1105  
1106 =head2 Machine arithmetic
1107  
1108 These functions perform arithmetic operations that are inherently
1109 influenced by the word size. They always produce a well-defined output
1110 if given valid inputs. There inevitably can't be any functions here
1111 that operate on Perl's union of signed and unsigned; you must choose,
1112 by which function you call, which type the result is to be tagged as.
1113  
1114 =over
1115  
1116 =item sint_madd(A, B)
1117  
1118 =item uint_madd(A, B)
1119  
1120 Modular addition. The result for unsigned addition is (A + B)
1121 mod 2^natint_bits. The signed version behaves similarly, but with a
1122 different result range.
1123  
1124 =cut
1125  
1126 9 ▶sub sint_madd($$) { use integer; sint($_[0]) + sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_madd: called CC=1 SCAR=0.0
Pod documented
1127  
1128 9 ▶sub uint_madd($$) { 0 | do { use integer; uint($_[0]) + uint($_[1]) } }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_madd: called CC=1 SCAR=0.0
Pod documented
1129  
1130 =item sint_msub(A, B)
1131  
1132 =item uint_msub(A, B)
1133  
1134 Modular subtraction. The result for unsigned subtraction is (A - B)
1135 mod 2^natint_bits. The signed version behaves similarly, but with a
1136 different result range.
1137  
1138 =cut
1139  
1140 9 ▶sub sint_msub($$) { use integer; sint($_[0]) - sint($_[1]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine sint_msub: called CC=1 SCAR=0.0
Pod documented
1141  
1142 9 ▶sub uint_msub($$) { 0 | do { use integer; uint($_[0]) - uint($_[1]) } }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine uint_msub: called CC=1 SCAR=0.0
Pod documented
1143  
1144 =item sint_cadd(A, B, CARRY_IN)
1145  
1146 =item uint_cadd(A, B, CARRY_IN)
1147  
1148 Addition with carry. Two word arguments (A and B) and an input carry
1149 bit (CARRY_IN, which must have the value 0 or 1) are all added together.
1150 Returns a list of two items: an output carry and an output word (of the
1151 same signedness as the inputs). Precisely, the output list (CARRY_OUT,
1152 R) is such that CARRY_OUT*2^natint_bits + R = A + B + CARRY_IN.
1153  
1154 =cut
1155  
1156 196 ▶sub sint_cadd($$$) {
Subroutine sint_cadd: called CC=6 SCAR=18.0
Pod documented
1157 196 my($a, $b, $cin) = map { sint($_) } @_;
1158 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1159 196 ▶ croak "invalid carry" unless $cin == 0 || $cin == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $cin == 0 or $cin == 1 0 196 196
Condition66%2/3
$cin == 0 or $cin == 1
l!l&&r!l&&!r
98980
MC/DC0%0/2
$cin == 0 or $cin == 1
$cin == 0 $cin == 1
Truth table66%2/3
A or B
A= $cin == 0B= $cin == 1
ABresult
1X1
011
000
1160 196 my $r = $a + $b + $cin;
1161 196 ▶ my $cout = $b < 0 ? $r > $a ? -1 : 0 : $r < $a ? +1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
$b < 0 ? : 78 118 196
$r > $a ? : 20 58 78
$r < $a ? : 42 76 118
1162 196 return ($cout, $r);
1163 }
1164  
1165 172 ▶sub uint_cadd($$$) {
Subroutine uint_cadd: called CC=6 SCAR=17.9
Pod documented
1166 172 my($a, $b, $cin) = map { uint($_) } @_;
1167 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1168 172 ▶ croak "invalid carry" unless $cin == 0 || $cin == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $cin == 0 or $cin == 1 0 172 172
Condition66%2/3
$cin == 0 or $cin == 1
l!l&&r!l&&!r
86860
MC/DC0%0/2
$cin == 0 or $cin == 1
$cin == 0 $cin == 1
Truth table66%2/3
A or B
A= $cin == 0B= $cin == 1
ABresult
1X1
011
000
1169 172 my $r = $a + $b;
1170 172 ▶ my $cout = ($r ^ min_sint) < ($a ^ min_sint) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($r ^ &min_sint()) < ($a ^ &min_sint()) ? : 68 104 172
1171 172 ▶ if($cin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cin) 86 86 172
1172 86 $r += 1;
1173 86 ▶ $cout = 1 if $r == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r == 0 8 78 86
1174 }
1175 9 ▶ return ($cout, do { no integer; 0 | $r });
Subroutine BEGIN: called CC=1 SCAR=0.0
1176 }
1177  
1178 =item sint_csub(A, B, CARRY_IN)
1179  
1180 =item uint_csub(A, B, CARRY_IN)
1181  
1182 Subtraction with carry (borrow). The second word argument (B) and
1183 an input carry bit (CARRY_IN, which must have the value 0 or 1) are
1184 subtracted from the first word argument (A). Returns a list of two
1185 items: an output carry and an output word (of the same signedness as
1186 the inputs). Precisely, the output list (CARRY_OUT, R) is such that R -
1187 CARRY_OUT*2^natint_bits = A - B - CARRY_IN.
1188  
1189 =cut
1190  
1191 196 ▶sub sint_csub($$$) {
Subroutine sint_csub: called CC=6 SCAR=18.0
Pod documented
1192 196 my($a, $b, $cin) = map { sint($_) } @_;
1193 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1194 196 ▶ croak "invalid carry" unless $cin == 0 || $cin == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $cin == 0 or $cin == 1 0 196 196
Condition66%2/3
$cin == 0 or $cin == 1
l!l&&r!l&&!r
98980
MC/DC0%0/2
$cin == 0 or $cin == 1
$cin == 0 $cin == 1
Truth table66%2/3
A or B
A= $cin == 0B= $cin == 1
ABresult
1X1
011
000
1195 196 my $r = $a - $b - $cin;
1196 196 ▶ my $cout = $b < 0 ? $r < $a ? -1 : 0 : $r > $a ? +1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
$b < 0 ? : 78 118 196
$r < $a ? : 20 58 78
$r > $a ? : 42 76 118
1197 196 return ($cout, $r);
1198 }
1199  
1200 172 ▶sub uint_csub($$$) {
Subroutine uint_csub: called CC=6 SCAR=17.9
Pod documented
1201 172 my($a, $b, $cin) = map { uint($_) } @_;
1202 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1203 172 ▶ croak "invalid carry" unless $cin == 0 || $cin == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $cin == 0 or $cin == 1 0 172 172
Condition66%2/3
$cin == 0 or $cin == 1
l!l&&r!l&&!r
86860
MC/DC0%0/2
$cin == 0 or $cin == 1
$cin == 0 $cin == 1
Truth table66%2/3
A or B
A= $cin == 0B= $cin == 1
ABresult
1X1
011
000
1204 172 my $r = $a - $b;
1205 172 ▶ my $cout = ($r ^ min_sint) > ($a ^ min_sint) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($r ^ &min_sint()) > ($a ^ &min_sint()) ? : 66 106 172
1206 172 ▶ if($cin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cin) 86 86 172
1207 86 ▶ $cout = 1 if $r == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r == 0 10 76 86
1208 86 $r -= 1;
1209 }
1210 9 ▶ return ($cout, do { no integer; 0 | $r });
Subroutine BEGIN: called CC=1 SCAR=0.0
1211 }
1212  
1213 =item sint_sadd(A, B)
1214  
1215 =item uint_sadd(A, B)
1216  
1217 Saturating addition. The result is A + B if that will fit into the result
1218 format, otherwise the minimum or maximum value of the result format is
1219 returned depending on the direction in which the addition overflowed.
1220  
1221 =cut
1222  
1223 98 ▶sub sint_sadd($$) {
Subroutine sint_sadd: called CC=4 SCAR=13.9
Pod documented
1224 98 my($a, $b) = map { sint($_) } @_;
1225 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1226 98 my $r = $a + $b;
1227 98 ▶ if($b < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < 0) { } 39 59 98
1228 39 ▶ $r = min_sint if $r > $a;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $a 12 27 39
1229 } else {
1230 59 ▶ $r = max_sint if $r < $a;
Branch: 1 branch
BranchTrueFalseTotal
if $r < $a 18 41 59
1231 }
1232 98 return $r;
1233 }
1234  
1235 86 ▶sub uint_sadd($$) {
Subroutine uint_sadd: called CC=2 SCAR=6.9
Pod documented
1236 86 my($a, $b) = map { uint($_) } @_;
1237 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1238 86 my $r = $a + $b;
1239 86 ▶ $r = max_uint if ($r ^ min_sint) < ($a ^ min_sint);
Branch: 1 branch
BranchTrueFalseTotal
if ($r ^ &min_sint()) < ($a ^ &min_sint()) 34 52 86
1240 9 ▶ return do { no integer; 0 | $r };
Subroutine BEGIN: called CC=1 SCAR=0.0
1241 }
1242  
1243 =item sint_ssub(A, B)
1244  
1245 =item uint_ssub(A, B)
1246  
1247 Saturating subtraction. The result is A - B if that will fit into the
1248 result format, otherwise the minimum or maximum value of the result
1249 format is returned depending on the direction in which the subtraction
1250 overflowed.
1251  
1252 =cut
1253  
1254 92 ▶sub sint_ssub($$) {
Subroutine sint_ssub: called CC=4 SCAR=13.9
Pod documented
1255 92 my($a, $b) = map { sint($_) } @_;
1256 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1257 92 my $r = $a - $b;
1258 92 ▶ if($b >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b >= 0) { } 50 42 92
1259 50 ▶ $r = min_sint if $r > $a;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $a 9 41 50
1260 } else {
1261 42 ▶ $r = max_sint if $r < $a;
Branch: 1 branch
BranchTrueFalseTotal
if $r < $a 11 31 42
1262 }
1263 92 return $r;
1264 }
1265  
1266 89 ▶sub uint_ssub($$) {
Subroutine uint_ssub: called CC=2 SCAR=6.9
Pod documented
1267 89 my($a, $b) = map { uint($_) } @_;
1268 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1269 89 ▶ my $r = ($a ^ min_sint) <= ($b ^ min_sint) ? 0 : $a - $b;
Branch: 1 branch
BranchTrueFalseTotal
($a ^ &min_sint()) <= ($b ^ &min_sint()) ? : 46 43 89
1270 9 ▶ return do { no integer; 0 | $r };
Subroutine BEGIN: called CC=1 SCAR=0.0
1271 }
1272  
1273 =back
1274  
1275 =head2 String conversion
1276  
1277 =over
1278  
1279 =item natint_hex(VALUE)
1280  
1281 VALUE must be a native integer value. The function encodes VALUE in
1282 hexadecimal, returning that representation as a string. Specifically,
1283 the output is of the form "I<s>B<0x>I<dddd>", where "I<s>" is the sign
1284 and "I<dddd>" is a sequence of hexadecimal digits.
1285  
1286 =cut
1287  
1288 9 ▶sub natint_hex($) {
Subroutine natint_hex: called CC=4 SCAR=13.9
Pod documented
1289 9 my $val = nint($_[0]);
1290 9 my $sgn = nint_sgn($val);
1291 9 $val = nint_abs($val);
1292 9 my $digits = "";
1293 9 my $i = (natint_bits+3) >> 2;
1294 9 ▶ for(; $i >= 7; $i -= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 7) 18 9 27
1295 18 $digits = sprintf("%07x", $val & 0xfffffff).$digits;
1296 18 $val >>= 28;
1297 }
1298 9 ▶ for(; $i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 18 9 27
1299 18 $digits = sprintf("%01x", $val & 0xf).$digits;
1300 18 $val >>= 4;
1301 }
1302 9 ▶ return ($sgn == -1 ? "-" : "+")."0x".$digits;
Branch: 1 branch
BranchTrueFalseTotal
$sgn == -1 ? : 3 6 9
1303 }
1304  
1305 =item hex_natint(STRING)
1306  
1307 Generates and returns a native integer value from a string encoding it in
1308 hexadecimal. Specifically, the input format is "[I<s>][B<0x>]I<dddd>",
1309 where "I<s>" is the sign and "I<dddd>" is a sequence of one or more
1310 hexadecimal digits. The input is interpreted case insensitively.
1311 If the value given in the string cannot be exactly represented in the
1312 native integer type, the function C<die>s.
1313  
1314 The core Perl function C<hex> (see L<perlfunc/hex>) does a similar job
1315 to this function, but differs in several ways. Principally, C<hex>
1316 doesn't handle negative values, and it gives the wrong answer for values
1317 that don't fit into the native integer type. In Perl 5.6 it also gives
1318 the wrong answer for values that don't fit into the native floating
1319 point type. It also doesn't enforce strict syntax on the input string.
1320  
1321 =cut
1322  
1323 9 my %hexdigit_value;
1324 {
1325 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1326 9 $hexdigit_value{chr(ord("0") + $_)} = $_ foreach 0..9;
1327 9 $hexdigit_value{chr(ord("a") + $_)} = 10+$_ foreach 0..5;
1328 9 $hexdigit_value{chr(ord("A") + $_)} = 10+$_ foreach 0..5;
1329 }
1330  
1331 103 ▶sub hex_natint($) {
Subroutine hex_natint: called CC=9 SCAR=22.1
Pod documented
1332 103 my($str) = @_;
1333 103 ▶ $str =~ /\A([-+]?)(?:0x)?([0-9a-f]+)\z/i
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /\A([-+]?)(?:0x)?([0-9a-f]+)\z/i or croak("bad syntax for hexadecimal integer value") 103 0 103
1334 or croak "bad syntax for hexadecimal integer value";
1335 103 my($sign, $digits) = ($1, $2);
1336 9 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1337 103 $digits =~ /\A0*/g;
1338 103 ▶ return my $zero = 0 if $digits =~ /\G\z/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $digits =~ /\G\z/cg 3 100 103
1339 100 $digits =~ /\G(.)/g;
1340 100 my $value = $hexdigit_value{$1};
1341 100 my $bits_to_go = (length($digits)-pos($digits)) << 2;
1342 100 ▶ croak "integer value too large"
Branch: 1 branch
BranchTrueFalseTotal
if $bits_to_go >= 64 or $bits_to_go + 4 > 64 and &max_uint() >> $bits_to_go < $value 69 31 100
Condition50%3/6
$bits_to_go >= 64 or $bits_to_go + 4 > 64 and &max_uint() >> $bits_to_go < $value
l!l&&r!l&&!r
69031
$bits_to_go + 4 > 64 and &max_uint() >> $bits_to_go < $value
!ll&&!rl&&r
3100
MC/DC33%1/3
$bits_to_go >= 64 or $bits_to_go + 4 > 64 and &max_uint() >> $bits_to_go < $value
$bits_to_go >= 64 $bits_to_go + 4 > 64 &max_uint() >> $bits_to_go < $value
Truth table50%2/4
A or B and C
A= $bits_to_go >= 64B= $bits_to_go + 4 > 64C= &max_uint() >> $bits_to_go < $value
ABCresult
1XX1
0111
00X0
0100
1343 if $bits_to_go >= natint_bits ||
1344 ($bits_to_go + 4 > natint_bits &&
1345 (max_uint >> $bits_to_go) < $value);
1346 31 ▶ while($digits =~ /\G(.)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits =~ /\G(.)/g) 252 31 283
1347 252 $value = ($value << 4) | $hexdigit_value{$1};
1348 }
1349 31 ▶ if($sign eq "-") {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq '-') { } 15 16 31
1350 15 $value = -$value;
1351 15 ▶ croak "integer value too large" if $value >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value >= 0 8 7 15
1352 7 return $value;
1353 } else {
1354 9 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1355 16 return 0 | $value;
1356 }
1357 }
1358  
1359 =back
1360  
1361 =head1 BUGS
1362  
1363 In Perl 5.6, when a native integer scalar is used in any arithmetic other
1364 than specifically integer arithmetic, it gets partially transformed into
1365 a floating point scalar. Even if its numerical value can be represented
1366 exactly in floating point, so that floating point arithmetic uses the
1367 correct numerical value, some operations are affected by the floatness.
1368 In particular, the stringification of the scalar doesn't necessarily
1369 represent its exact value if it is tagged as floating point.
1370  
1371 Because of this transforming behaviour, if you need to stringify a native
1372 integer it is best to ensure that it doesn't get used in any non-integer
1373 arithmetic first. If an integer scalar must be used in standard Perl
1374 arithmetic, it may be copied first and the copy operated upon to avoid
1375 causing side effects on the original. If an integer scalar might have
1376 already been transformed, it can be cleaned by passing it through the
1377 canonicalisation function C<nint>. The functions in this module all
1378 avoid modifying their arguments, and always return pristine integers.
1379  
1380 Perl 5.8+ still internally modifies integer scalars in the same
1381 circumstances, but seems to have corrected all the misbehaviour that
1382 resulted from it.
1383  
1384 Also in Perl 5.6, default Perl arithmetic doesn't necessarily work
1385 correctly on native integers. (This is part of the motivation for
1386 the myriad arithmetic functions in this module.) Default arithmetic
1387 here is strictly floating point, so if there are native integers that
1388 cannot be exactly represented in floating point then the arithmetic will
1389 approximate the values before operating on them. Perl 5.8+ attempts to
1390 use native integer operations where possible in its default arithmetic,
1391 but as of Perl 5.8.8 it doesn't always succeed. For reliable integer
1392 arithmetic, integer operations must still be requested explicitly.
1393  
1394 =head1 SEE ALSO
1395  
1396 L<Data::Float>,
1397 L<Scalar::Number>,
1398 L<perlnumber(1)>
1399  
1400 =head1 AUTHOR
1401  
1402 Andrew Main (Zefram) <zefram@fysh.org>
1403  
1404 Currently maintained by Robert Rothenberg <rrwo@cpan.org>
1405  
1406 =head1 COPYRIGHT
1407  
1408 Copyright (C) 2007, 2010, 2015, 2017, 2025
1409 Andrew Main (Zefram) <zefram@fysh.org>
1410  
1411 =head1 LICENSE
1412  
1413 This module is free software; you can redistribute it and/or modify it
1414 under the same terms as Perl itself.
1415  
1416 =cut
1417  
1418 9 1;
↑ Summary