JHash.xs

filter: Statementstmt 100% 35 / 35 Branchbran 43.7% 7 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.3% 42 / 51 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Digest/JHash.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 /* Jenkins Hash http://burtleburtle.net/bob/hash/doobs.html */
6  
7 const int DEBUG = 0;
8  
9 /* Need to constrain U32 to only 32 bits on 64 bit systems
10 * For efficiency we only use the & 0xffffffff if required
11 */
12 #if BYTEORDER > 0x4321 || defined(TRUNCATE_U32)
13 #define MIX(a,b,c) \
14 { \
15 a &= 0xffffffff; b &= 0xffffffff; c &= 0xffffffff; \
16 a -= b; a -= c; a ^= (c>>13); a &= 0xffffffff; \
17 b -= c; b -= a; b ^= (a<<8); b &= 0xffffffff; \
18 c -= a; c -= b; c ^= (b>>13); c &= 0xffffffff; \
19 a -= b; a -= c; a ^= (c>>12); a &= 0xffffffff; \
20 b -= c; b -= a; b ^= (a<<16); b &= 0xffffffff; \
21 c -= a; c -= b; c ^= (b>>5); c &= 0xffffffff; \
22 a -= b; a -= c; a ^= (c>>3); a &= 0xffffffff; \
23 b -= c; b -= a; b ^= (a<<10); b &= 0xffffffff; \
24 c -= a; c -= b; c ^= (b>>15); c &= 0xffffffff; \
25 }
26 #else
27 #define MIX(a,b,c) \
28 { \
29 a -= b; a -= c; a ^= (c>>13); \
30 b -= c; b -= a; b ^= (a<<8); \
31 c -= a; c -= b; c ^= (b>>13); \
32 a -= b; a -= c; a ^= (c>>12); \
33 b -= c; b -= a; b ^= (a<<16); \
34 c -= a; c -= b; c ^= (b>>5); \
35 a -= b; a -= c; a ^= (c>>3); \
36 b -= c; b -= a; b ^= (a<<10); \
37 c -= a; c -= b; c ^= (b>>15); \
38 }
39 #endif
40  
41 4 U32 jhash( SV* str )
42 {
43 STRLEN rawlen;
44 char* p;
45 U32 a, b, c, len, length;
46  
47 /* extract the string data and string length from the perl scalar */
48 4 p = (char*)SvPV(str, rawlen);
49 4 length = len = (U32)rawlen;
50  
51 /* Test for undef or null string case and return 0 */
52 4 ▶ if ( length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( length == 0 ) { 2 2 4
53 2 ▶ DEBUG && printf( "Recieved a null or undef string!\n" );
Branch: 2 branches
BranchTrueFalseTotal
DEBUG && printf( "Recieved a null or undef string!\n" ); 0 2 2
DEBUG && printf( "Recieved a null or undef string!\n" ); 0 0 0
54 2 return 0;
55 }
56  
57 2 ▶ DEBUG && printf( "Received string '%.*s'.\n", (int)len, p );
Branch: 2 branches
BranchTrueFalseTotal
DEBUG && printf( "Received string '%.*s'.\n", (int)len, p ); 0 2 2
DEBUG && printf( "Received string '%.*s'.\n", (int)len, p ); 0 0 0
58  
59 2 a = b = 0x9e3779b9; /* golden ratio suggested by Jenkins */
60 2 c = 0;
61 3 ▶ while (len >= 12)
Branch: 1 branch
BranchTrueFalseTotal
while (len >= 12) 1 2 3
62 {
63 1 a += (p[0] + (((U32)p[1])<<8) + (((U32)p[2])<<16) +
64 1 (((U32)p[3])<<24));
65 1 b += (p[4] + (((U32)p[5])<<8) + (((U32)p[6])<<16) +
66 1 (((U32)p[7])<<24));
67 1 c += (p[8] + (((U32)p[9])<<8) + (((U32)p[10])<<16) +
68 1 (((U32)p[11])<<24));
69 1 MIX(a, b, c);
70 1 p += 12;
71 1 len -= 12;
72 }
73 2 c += length;
74 2 switch(len) {
75 1 case 11: c+=((U32)p[10]<<24);
76 1 case 10: c+=((U32)p[9]<<16);
77 1 case 9: c+=((U32)p[8]<<8);
78 1 case 8: b+=((U32)p[7]<<24);
79 2 case 7: b+=((U32)p[6]<<16);
80 2 case 6: b+=((U32)p[5]<<8);
81 2 case 5: b+=((U32)p[4]);
82 2 case 4: a+=((U32)p[3]<<24);
83 2 case 3: a+=((U32)p[2]<<16);
84 2 case 2: a+=((U32)p[1]<<8);
85 2 case 1: a+=((U32)p[0]);
86 }
87 2 MIX(a, b, c);
88 2 ▶ DEBUG && printf( "Hash value is %d.\n", (int)(c) );
Branch: 2 branches
BranchTrueFalseTotal
DEBUG && printf( "Hash value is %d.\n", (int)(c) ); 0 2 2
DEBUG && printf( "Hash value is %d.\n", (int)(c) ); 0 0 0
89  
90 2 return(c);
91 }
92  
93 MODULE = Digest::JHash PACKAGE = Digest::JHash
94  
95 PROTOTYPES: ENABLE
96  
97 U32
98 jhash(str)
99 SV* str
↑ Summary blib/lib/Digest/JHash.pm »