line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bit::Manip::PP; |
2
|
|
|
|
|
|
|
|
3
|
12
|
|
|
12
|
|
36608
|
use warnings; |
|
12
|
|
|
|
|
13
|
|
|
12
|
|
|
|
|
326
|
|
4
|
12
|
|
|
12
|
|
38
|
use strict; |
|
12
|
|
|
|
|
27
|
|
|
12
|
|
|
|
|
359
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.05'; |
7
|
|
|
|
|
|
|
|
8
|
12
|
|
|
12
|
|
42
|
use Exporter; |
|
12
|
|
|
|
|
18
|
|
|
12
|
|
|
|
|
10223
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
12
|
|
|
|
|
|
|
bit_get |
13
|
|
|
|
|
|
|
bit_set |
14
|
|
|
|
|
|
|
bit_clr |
15
|
|
|
|
|
|
|
bit_toggle |
16
|
|
|
|
|
|
|
bit_on |
17
|
|
|
|
|
|
|
bit_off |
18
|
|
|
|
|
|
|
bit_bin |
19
|
|
|
|
|
|
|
bit_count |
20
|
|
|
|
|
|
|
bit_mask |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our %EXPORT_TAGS; |
24
|
|
|
|
|
|
|
$EXPORT_TAGS{all} = [@EXPORT_OK]; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _ref { |
27
|
298
|
100
|
|
298
|
|
2308
|
shift if @_ == 2; |
28
|
298
|
100
|
100
|
|
|
1499
|
if ($_[0] !~ /^\d+$/ && ref $_[0] ne 'SCALAR'){ |
29
|
3
|
|
|
|
|
13
|
die "your data must either be an integer or a SCALAR reference\n"; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
295
|
100
|
|
|
|
400
|
if (ref $_[0]){ |
33
|
138
|
100
|
|
|
|
94
|
if (${ $_[0] } !~ /^\d+/){ |
|
138
|
|
|
|
|
383
|
|
34
|
1
|
|
|
|
|
5
|
die "data reference must contain only an integer\n"; |
35
|
|
|
|
|
|
|
} |
36
|
137
|
|
|
|
|
238
|
return 1; |
37
|
|
|
|
|
|
|
} |
38
|
157
|
|
|
|
|
235
|
return 0; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
sub bit_bin { |
41
|
1030
|
|
|
1030
|
1
|
227401
|
my ($data) = @_; |
42
|
1030
|
|
|
|
|
4624
|
return sprintf("%b", $data); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
sub bit_count { |
45
|
145
|
|
|
145
|
1
|
528
|
my ($n, $set) = @_; |
46
|
|
|
|
|
|
|
|
47
|
145
|
100
|
66
|
|
|
644
|
if (! defined $n || $n !~ /^\d+/){ |
48
|
2
|
|
|
|
|
12
|
die "bit_count() requires an integer param\n"; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
143
|
|
|
|
|
369
|
my $bits = sprintf("%b", $n); |
52
|
143
|
|
|
|
|
139
|
my $bit_count; |
53
|
|
|
|
|
|
|
|
54
|
143
|
100
|
|
|
|
160
|
if ($set){ |
55
|
15
|
|
|
|
|
18
|
$bit_count = $bits =~ tr/1/1/; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
128
|
|
|
|
|
103
|
$bit_count = length($bits); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
143
|
|
|
|
|
230
|
return $bit_count; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
sub bit_mask { |
64
|
112
|
|
|
112
|
1
|
143
|
my ($bits, $lsb) = @_; |
65
|
112
|
|
|
|
|
232
|
return (2 ** $bits - 1) << $lsb; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
sub bit_get { |
68
|
20
|
|
|
20
|
1
|
5614
|
my ($data, $msb, $lsb) = @_; |
69
|
|
|
|
|
|
|
|
70
|
20
|
100
|
|
|
|
38
|
$lsb = 0 if ! defined $lsb; |
71
|
|
|
|
|
|
|
|
72
|
20
|
|
|
|
|
26
|
_check_msb($msb); |
73
|
19
|
|
|
|
|
14
|
$msb++; # need to start from 1 here |
74
|
|
|
|
|
|
|
|
75
|
19
|
|
|
|
|
24
|
_check_lsb($lsb, $msb); |
76
|
|
|
|
|
|
|
|
77
|
16
|
|
|
|
|
27
|
return ($data & (2**$msb-1)) >> $lsb; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
sub bit_set { |
80
|
98
|
|
|
98
|
1
|
8981
|
my ($data, $lsb, $bits, $value) = @_; |
81
|
|
|
|
|
|
|
|
82
|
98
|
100
|
|
|
|
177
|
if (@_ != 4){ |
83
|
2
|
|
|
|
|
11
|
die "bit_set() requires four params\n"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
96
|
|
|
|
|
133
|
_check_value($value); |
87
|
|
|
|
|
|
|
|
88
|
96
|
|
|
|
|
104
|
my $value_bits = bit_count($value, 0); |
89
|
96
|
100
|
|
|
|
138
|
if ($value_bits != $bits){ |
90
|
14
|
|
|
|
|
14
|
$value_bits = $bits; |
91
|
|
|
|
|
|
|
} |
92
|
96
|
|
|
|
|
100
|
my $mask = bit_mask($value_bits, $lsb); |
93
|
|
|
|
|
|
|
|
94
|
96
|
100
|
|
|
|
105
|
if (_ref($data)){ |
95
|
38
|
|
|
|
|
60
|
$$data = ($$data & ~($mask)) | ($value << $lsb); |
96
|
38
|
|
|
|
|
42
|
return 0; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
else { |
99
|
58
|
|
|
|
|
78
|
$data = ($data & ~($mask)) | ($value << $lsb); |
100
|
58
|
|
|
|
|
187
|
return $data; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
sub bit_clr { |
104
|
44
|
|
|
44
|
1
|
5772
|
my ($data, $lsb, $nbits) = @_; |
105
|
44
|
|
|
|
|
60
|
return bit_set($data, $lsb, $nbits, 0); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
sub bit_toggle { |
108
|
96
|
|
|
96
|
1
|
48387
|
my ($data, $bit) = @_; |
109
|
|
|
|
|
|
|
|
110
|
96
|
100
|
|
|
|
138
|
if (_ref($data)){ |
111
|
48
|
|
|
|
|
56
|
$$data ^= 1 << $bit; |
112
|
48
|
|
|
|
|
65
|
return 0; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
else { |
115
|
48
|
|
|
|
|
109
|
return $data ^= 1 << $bit; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
sub bit_on { |
119
|
48
|
|
|
48
|
1
|
17417
|
my ($data, $bit) = @_; |
120
|
|
|
|
|
|
|
|
121
|
48
|
100
|
|
|
|
83
|
if (_ref($data)){ |
122
|
24
|
|
|
|
|
29
|
$$data |= 1 << $bit; |
123
|
24
|
|
|
|
|
37
|
return 0; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
else { |
126
|
24
|
|
|
|
|
50
|
return $data |= 1 << $bit; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
sub bit_off { |
130
|
48
|
|
|
48
|
1
|
16350
|
my ($data, $bit) = @_; |
131
|
|
|
|
|
|
|
|
132
|
48
|
100
|
|
|
|
76
|
if (_ref($data)){ |
133
|
24
|
|
|
|
|
37
|
$$data &= ~(1 << $bit); |
134
|
24
|
|
|
|
|
30
|
return 0; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { |
137
|
24
|
|
|
|
|
55
|
return $data &= ~(1 << $bit); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
sub _check_msb { |
141
|
20
|
|
|
20
|
|
17
|
my ($msb) = @_; |
142
|
20
|
100
|
|
|
|
33
|
if ($msb < 0){ |
143
|
1
|
|
|
|
|
8
|
die("\$msb param can not be negative\n"); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
sub _check_lsb { |
147
|
19
|
|
|
19
|
|
19
|
my ($lsb, $msb) = @_; |
148
|
|
|
|
|
|
|
|
149
|
19
|
100
|
|
|
|
28
|
if ($lsb < 0){ |
150
|
1
|
|
|
|
|
5
|
die "\$lsb param can't be negative\n"; |
151
|
|
|
|
|
|
|
} |
152
|
18
|
100
|
|
|
|
28
|
if (($lsb + 1) >= $msb){ |
153
|
2
|
|
|
|
|
9
|
die "\$lsb param must be less than \$msb\n"; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
sub _check_value { |
157
|
97
|
100
|
|
97
|
|
154
|
shift if @_ > 1; |
158
|
97
|
|
|
|
|
69
|
my ($val) = @_; |
159
|
97
|
100
|
|
|
|
149
|
if ($val < 0){ |
160
|
1
|
|
|
|
|
6
|
die "\$value param must be zero or greater\n"; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
0
|
|
|
sub _vim{}; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |
166
|
|
|
|
|
|
|
__END__ |