Util.xs

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 114 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Bit/Util.pm »
1 /* -*- Mode: C -*- */
2  
3 #define PERL_NO_GET_CONTEXT 1
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 #include "ppport.h"
10  
11 static const char bit_count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
12 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
13 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
15 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
16 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
17 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
18 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
19 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
20 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
21 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
22 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
23 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
24 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
25 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
26 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 };
27  
28 static IV
29 0 bu_first(pTHX_ SV *bv, SV *start_sv) {
30 STRLEN len;
31 0 const char *bytes = SvPV_const(bv, len);
32 char byte;
33 IV byte_ix;
34 IV bit_ix;
35 IV start;
36  
37 0 ▶ if (start_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (start_sv) { 0 0 0
38 0 start = SvIV(start_sv);
39 0 ▶ if (start < 0) start = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (start < 0) start = 0; 0 0 0
40 }
41 0 else start = 0;
42  
43 0 byte_ix = start / 8;
44 0 ▶ if (byte_ix >= len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (byte_ix >= len) return -1; 0 0 0
45 0 bit_ix = start & 7;
46  
47 0 ▶ while (byte_ix < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (byte_ix < len) { 0 0 0
48 0 ▶ if (byte = bytes[byte_ix]) {
Branch: 1 branch
BranchTrueFalseTotal
if (byte = bytes[byte_ix]) { 0 0 0
49 0 ▶ while (bit_ix < 8) {
Branch: 1 branch
BranchTrueFalseTotal
while (bit_ix < 8) { 0 0 0
50 0 ▶ if ((byte >> bit_ix) & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ((byte >> bit_ix) & 1) 0 0 0
51 0 return byte_ix * 8 + bit_ix;
52 0 bit_ix++;
53 }
54 }
55 0 bit_ix = 0;
56 0 byte_ix++;
57 }
58 0 return -1;
59 }
60  
61 static IV
62 0 bu_last(pTHX_ SV *bv, SV *end_sv) {
63 STRLEN len;
64 0 const char *bytes = SvPV_const(bv, len);
65 char byte;
66 IV byte_ix;
67 IV bit_ix;
68 IV end;
69  
70 0 ▶ if (end_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (end_sv) { 0 0 0
71 0 end = SvIV(end_sv);
72 0 ▶ if (end < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (end < 0) return -1; 0 0 0
73 0 byte_ix = end / 8;
74 }
75 0 else end = len * 8;
76  
77 0 byte_ix = end / 8;
78 0 ▶ if (byte_ix < len)
Branch: 1 branch
BranchTrueFalseTotal
if (byte_ix < len) 0 0 0
79 0 bit_ix = end & 7;
80 else {
81 0 byte_ix = len - 1;
82 0 bit_ix = 7;
83 }
84  
85 0 ▶ while (byte_ix >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (byte_ix >= 0) { 0 0 0
86 0 ▶ if (byte = bytes[byte_ix]) {
Branch: 1 branch
BranchTrueFalseTotal
if (byte = bytes[byte_ix]) { 0 0 0
87 0 ▶ while (bit_ix >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (bit_ix >= 0) { 0 0 0
88 0 ▶ if ((byte >> bit_ix) & 1)
Branch: 1 branch
BranchTrueFalseTotal
if ((byte >> bit_ix) & 1) 0 0 0
89 0 return byte_ix * 8 + bit_ix;
90 0 bit_ix--;
91 }
92 }
93 0 bit_ix = 7;
94 0 byte_ix--;
95 }
96 0 return -1;
97 }
98  
99 static IV
100 0 bu_count(pTHX_ SV *bv, SV *start_sv, SV *end_sv) {
101 STRLEN len;
102 0 const unsigned char *bytes = SvPV_const(bv, len);
103 IV start;
104 IV end;
105 IV byte_ix;
106 IV last_byte;
107 IV bit_ix;
108 0 IV count = 0;
109 IV mask;
110  
111 0 ▶ start = (start_sv ? SvIV(start_sv) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
start = (start_sv ? SvIV(start_sv) : 0 ); 0 0 0
112 0 ▶ if (start < 0) start = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (start < 0) start = 0; 0 0 0
113 0 ▶ if (start >= len * 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (start >= len * 8) return 0; 0 0 0
114  
115 0 ▶ end = (end_sv ? SvIV(end_sv) : len * 8);
Branch: 1 branch
BranchTrueFalseTotal
end = (end_sv ? SvIV(end_sv) : len * 8); 0 0 0
116 0 ▶ if (end > len * 8) end = len * 8;
Branch: 1 branch
BranchTrueFalseTotal
if (end > len * 8) end = len * 8; 0 0 0
117 0 ▶ if (end <= start) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (end <= start) return 0; 0 0 0
118  
119 0 byte_ix = start / 8;
120 0 bit_ix = start & 7;
121  
122 0 last_byte = (end + 7)/ 8;
123
124 0 mask = ~ ((1 << bit_ix) - 1);
125 0 count = bit_count[bytes[byte_ix] & mask];
126  
127 0 byte_ix ++;
128 0 ▶ while (byte_ix < last_byte) {
Branch: 1 branch
BranchTrueFalseTotal
while (byte_ix < last_byte) { 0 0 0
129 0 count += bit_count[bytes[byte_ix]];
130 0 byte_ix++;
131 }
132  
133 0 ▶ if (bit_ix = end & 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (bit_ix = end & 7) { 0 0 0
134 0 mask = ~ ((1 << bit_ix) - 1);
135 0 count -= bit_count[bytes[byte_ix -1] & mask];
136 }
137  
138 0 return count;
139 }
140  
141 MODULE = Bit::Util PACKAGE = Bit::Util
142  
143 SV *
144 bu_first(bv, start = NULL)
145 SV *bv
146 SV *start
147 PREINIT:
148 IV ix;
149 CODE:
150 0 ix = bu_first(aTHX_ bv, start);
151 0 ▶RETVAL = (ix < 0 ? &PL_sv_undef : newSViv(ix));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ix < 0 ? &PL_sv_undef : newSViv(ix)); 0 0 0
152 OUTPUT:
153 RETVAL
154  
155 SV *
156 bu_last(bv, end = NULL)
157 SV *bv
158 SV *end
159 PREINIT:
160 IV ix;
161 CODE:
162 0 ix = bu_last(aTHX_ bv, end);
163 0 ▶ RETVAL = (ix < 0 ? &PL_sv_undef : newSViv(ix));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ix < 0 ? &PL_sv_undef : newSViv(ix)); 0 0 0
164 OUTPUT:
165 RETVAL
166  
167 IV
168 bu_count(bv, start = NULL, end = NULL)
169 SV *bv
170 SV *start
171 SV *end
172 CODE:
173 0 RETVAL = bu_count(aTHX_ bv, start, end);
174 OUTPUT:
175 RETVAL
↑ Summary blib/lib/Bit/Util.pm »