src/calculate_bucket_values.c

filter: Statementstmt 100% 51 / 51 Branchbran 100% 22 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 73 / 73 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Sort/Bucket.pm ↑ Summary src/mergesort_algo.c »
1 /*
2 =head1 NAME
3  
4 calculate_bucket_values.c - compute bucket values for SVs
5  
6 =head1 SYNOPSIS
7  
8 #include "calculate_bucket_values.c"
9  
10 ...
11  
12 calculate_bucket_values(...);
13  
14 =head1 DESCRIPTION
15  
16 Provides a function to compute vectors of major and minor numerical bucket
17 values from a vector of SVs:
18  
19 static void calculate_bucket_values(
20 int svcount, SV** src,
21 U32* b_major, U32* b_minor, int major_bits);
22  
23 The parameters are:
24  
25 =over
26  
27 =item int B<svcount>
28  
29 The number of SVs in the vector.
30  
31 =item SV** B<src>
32  
33 A pointer to the start of the SV vector.
34  
35 =item U32 *B<b_major>
36  
37 A pointer to a vector of B<svcount> U32s. The major bucket values are to be
38 stored here.
39  
40 =item U32 *B<b_minor>
41  
42 A pointer to a vector of B<svcount> U32s. The minor bucket values are to be
43 stored here.
44  
45 =item int B<major_bits>
46  
47 The number of bits to use in the major bucket values. Must not be less than
48 1 or more than 31.
49  
50 The major bucket values will range from 0 to (2^B<major_bits>)-1.
51  
52 =back
53  
54 =cut
55 */
56  
57 static void cbv__1to7( int svcount, SV** src,
58 U32* b_major, U32* b_minor, int major_bits);
59 static void cbv__8to15( int svcount, SV** src,
60 U32* b_major, U32* b_minor, int major_bits);
61 static void cbv__16to23(int svcount, SV** src,
62 U32* b_major, U32* b_minor, int major_bits);
63 static void cbv__24to31(int svcount, SV** src,
64 U32* b_major, U32* b_minor, int major_bits);
65  
66 static void
67 25149 calculate_bucket_values(int svcount, SV** src,
68 U32* b_major, U32* b_minor, int major_bits)
69 {
70 25149 ▶ if (major_bits < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (major_bits < 8) { 15127 10022 25149
71 15127 cbv__1to7( svcount, src, b_major, b_minor, major_bits);
72 10022 ▶ } else if (major_bits < 16) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (major_bits < 16) { 9185 837 10022
73 9185 cbv__8to15( svcount, src, b_major, b_minor, major_bits);
74 837 ▶ } else if (major_bits < 24) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (major_bits < 24) { 533 304 837
75 533 cbv__16to23(svcount, src, b_major, b_minor, major_bits);
76 } else {
77 304 cbv__24to31(svcount, src, b_major, b_minor, major_bits);
78 }
79 25149 }
80  
81 /*
82 =head1 PRIVATE FUNCTIONS
83  
84 Functions with names starting in C<cbv__> are not intended for use outside
85 this file.
86  
87 =over
88  
89 =item cbv__sv_to_null_padded_str(B<sv>, B<want_len>, B<buf>)
90  
91 Converts B<sv> to a string, padding with NULLs if necessary to ensure
92 that its length is at least B<want_len> bytes.
93  
94 B<buf> is storage into which this function may choose to copy the string.
95 It must have capacity for B<want_len> bytes.
96  
97 Returns a pointer to the string.
98  
99 =cut
100 */
101  
102 static unsigned char *
103 64397596 cbv__sv_to_null_padded_str(SV *sv, int want_len, unsigned char *buf)
104 {
105 unsigned char *pv;
106 STRLEN cur;
107  
108 64397596 ▶ if (sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv) { 64397494 102 64397596
109 64397494 pv = SvPV(sv, cur);
110 } else {
111 102 cur = 0;
112 }
113  
114 64397596 ▶ if (cur < want_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur < want_len) { 45054373 19343223 64397596
115 int i;
116 175102959 ▶ for ( i=0 ; i<cur ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i=0 ; i<cur ; i++ ) { 130048586 45054373 175102959
117 130048586 buf[i] = pv[i];
118 }
119 173377881 ▶ for ( ; i<want_len ; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; i<want_len ; i++ ) { 128323508 45054373 173377881
120 128323508 buf[i] = '\0';
121 }
122 45054373 return buf;
123 } else {
124 19343223 return pv;
125 }
126 }
127  
128 /*
129 =item cbv__1to7(B<svcount>, B<src>, B<b_major>, B<b_minor>, B<major_bits>)
130  
131 An implementation of calculate_bucket_values() for B<major_bits> in the range
132 1 to 7.
133  
134 The most significant B<major_bits> bits of byte 0 are taken as the
135 major bucket, and bytes 0,1,2,3 are packed into the minor bucket value.
136  
137 =cut
138 */
139  
140 static void
141 15127 cbv__1to7(int svcount, SV** src,
142 U32* b_major, U32* b_minor, int major_bits)
143 {
144 unsigned char *pv, buf[4];
145 15127 int major_rightshift = 8 - major_bits;
146  
147 14852439 ▶ while (svcount--) {
Branch: 1 branch
BranchTrueFalseTotal
while (svcount--) { 14837312 15127 14852439
148 14837312 pv = cbv__sv_to_null_padded_str(*src++, sizeof(buf), buf);
149 14837312 *b_major++ = pv[0] >> major_rightshift;
150 14837312 *b_minor++ = (pv[0] << 24) + (pv[1] << 16) + (pv[2] << 8) + pv[3];
151 }
152 15127 }
153  
154 /*
155 =item cbv__8to15(B<svcount>, B<src>, B<b_major>, B<b_minor>, B<major_bits>)
156  
157 An implementation of calculate_bucket_values() for B<major_bits> in the range
158 8 to 15.
159  
160 The most significant B<major_bits> bits of bytes 0,1 are taken as the
161 major bucket, and bytes 1,2,3,4 are packed into the minor bucket value.
162  
163 =cut
164 */
165  
166 static void
167 9185 cbv__8to15(int svcount, SV** src,
168 U32* b_major, U32* b_minor, int major_bits)
169 {
170 unsigned char *pv, buf[4+1];
171 9185 int major_rightshift = 32 - major_bits;
172 U32 bucket;
173  
174 18973903 ▶ while (svcount--) {
Branch: 1 branch
BranchTrueFalseTotal
while (svcount--) { 18964718 9185 18973903
175 18964718 pv = cbv__sv_to_null_padded_str(*src++, sizeof(buf), buf);
176 18964718 bucket = (pv[0] << 24) + (pv[1] << 16) + (pv[2] << 8) + pv[3];
177 18964718 *b_major++ = bucket >> major_rightshift;
178 18964718 *b_minor++ = (bucket << 8) + pv[4];
179 }
180 9185 }
181  
182 /*
183 =item cbv__16to23(B<svcount>, B<src>, B<b_major>, B<b_minor>, B<major_bits>)
184  
185 An implementation of calculate_bucket_values() for B<major_bits> in the range
186 16 to 23.
187  
188 The most significant B<major_bits> bits of bytes 0,1,2 are
189 taken as the major bucket, and bytes 2,3,4,5 are packed into the minor
190 bucket value.
191  
192 =cut
193 */
194  
195 static void
196 533 cbv__16to23(int svcount, SV** src,
197 U32* b_major, U32* b_minor, int major_bits)
198 {
199 unsigned char *pv, buf[4+2];
200 533 int major_rightshift = 32 - major_bits;
201 U32 bucket;
202  
203 16904403 ▶ while (svcount--) {
Branch: 1 branch
BranchTrueFalseTotal
while (svcount--) { 16903870 533 16904403
204 16903870 pv = cbv__sv_to_null_padded_str(*src++, sizeof(buf), buf);
205 16903870 bucket = (pv[0] << 24) + (pv[1] << 16) + (pv[2] << 8) + pv[3];
206 16903870 *b_major++ = bucket >> major_rightshift;
207 16903870 *b_minor++ = (bucket << 16) + (pv[4] << 8) + pv[5];
208 }
209 533 }
210  
211 /*
212 =item cbv__24to31(B<svcount>, B<src>, B<b_major>, B<b_minor>, B<major_bits>)
213  
214 An implementation of calculate_bucket_values() for B<major_bits> in the range
215 24 to 31.
216  
217 The most significant B<major_bits> bits of bytes 0,1,2,3 are
218 taken as the major bucket, and bytes 3,4,5,6 are packed into the minor
219 bucket value.
220  
221 =cut
222 */
223  
224 static void
225 304 cbv__24to31(int svcount, SV** src,
226 U32* b_major, U32* b_minor, int major_bits)
227 {
228 unsigned char *pv, buf[4+3];
229 304 int major_rightshift = 32 - major_bits;
230 U32 bucket;
231  
232 13692000 ▶ while (svcount--) {
Branch: 1 branch
BranchTrueFalseTotal
while (svcount--) { 13691696 304 13692000
233 13691696 pv = cbv__sv_to_null_padded_str(*src++, sizeof(buf), buf);
234 13691696 bucket = (pv[0] << 24) + (pv[1] << 16) + (pv[2] << 8) + pv[3];
235 13691696 *b_major++ = bucket >> major_rightshift;
236 13691696 *b_minor++ = (bucket << 24) + (pv[4] << 16) + (pv[5] << 8) + pv[6];
237 }
238 304 }
239  
240 /*
241 =back
242  
243 =head1 AUTHOR
244  
245 Nick Cleaton, E<lt>nick@cleaton.netE<gt>
246  
247 =head1 COPYRIGHT AND LICENSE
248  
249 Copyright (C) 2010 by Nick Cleaton
250  
251 This library is free software; you can redistribute it and/or modify
252 it under the same terms as Perl itself, either Perl version 5.10.0 or,
253 at your option, any later version of Perl 5 you may have available.
254  
255 =cut
256 */
« blib/lib/Sort/Bucket.pm ↑ Summary src/mergesort_algo.c »