Skip to content

Commit eb5a17c

Browse files
committed
perl_siphash.h - add a 64 bit variant for testing, cleanup line endings
Scott Baker wanted a 64 bit general purpose hash function. Currently our Siphash implementation is optimized for use in the internal hash tables by having its state initialization separated from the main hash function. We have a 32 bit variant we use for testing that takes a seed and not a state, but our only 64 bit support has the seeding step split out. This patch exposes a seedable 64 bit function. Note, that Siphash *is* a 64 bit hash function, however historically we only use 32 bit hashes in our hash tables. The new variant we expose here basically just exposes the normal Siphash that most people would use. A follow up patch will add documentation.
1 parent 82a05dc commit eb5a17c

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

perl_siphash.h

+62-55
Original file line numberDiff line numberDiff line change
@@ -43,75 +43,82 @@ void S_perl_siphash_seed_state(const unsigned char * const seed_buf, unsigned ch
4343
PERL_STATIC_INLINE U64 \
4444
FNC ## _with_state_64 \
4545
(const unsigned char * const state, const unsigned char *in, const STRLEN inlen) \
46-
{ \
47-
const int left = inlen & 7; \
48-
const U8 *end = in + inlen - left; \
49-
\
50-
U64 b = ( ( U64 )(inlen) ) << 56; \
51-
U64 m; \
52-
U64 v0 = U8TO64_LE(state); \
53-
U64 v1 = U8TO64_LE(state+8); \
54-
U64 v2 = U8TO64_LE(state+16); \
55-
U64 v3 = U8TO64_LE(state+24); \
56-
\
57-
for ( ; in != end; in += 8 ) \
58-
{ \
59-
m = U8TO64_LE( in ); \
60-
v3 ^= m; \
61-
\
62-
SIP_ROUNDS; \
63-
\
64-
v0 ^= m; \
65-
} \
66-
\
67-
switch( left ) \
68-
{ \
46+
{ \
47+
const int left = inlen & 7; \
48+
const U8 *end = in + inlen - left;\
49+
\
50+
U64 b = ( ( U64 )(inlen) ) << 56; \
51+
U64 m; \
52+
U64 v0 = U8TO64_LE(state); \
53+
U64 v1 = U8TO64_LE(state+8); \
54+
U64 v2 = U8TO64_LE(state+16); \
55+
U64 v3 = U8TO64_LE(state+24); \
56+
\
57+
for ( ; in != end; in += 8 ) \
58+
{ \
59+
m = U8TO64_LE( in ); \
60+
v3 ^= m; \
61+
\
62+
SIP_ROUNDS; \
63+
\
64+
v0 ^= m; \
65+
} \
66+
\
67+
switch( left ) \
68+
{ \
6969
case 7: b |= ( ( U64 )in[ 6] ) << 48; /*FALLTHROUGH*/ \
7070
case 6: b |= ( ( U64 )in[ 5] ) << 40; /*FALLTHROUGH*/ \
7171
case 5: b |= ( ( U64 )in[ 4] ) << 32; /*FALLTHROUGH*/ \
7272
case 4: b |= ( ( U64 )in[ 3] ) << 24; /*FALLTHROUGH*/ \
7373
case 3: b |= ( ( U64 )in[ 2] ) << 16; /*FALLTHROUGH*/ \
7474
case 2: b |= ( ( U64 )in[ 1] ) << 8; /*FALLTHROUGH*/ \
7575
case 1: b |= ( ( U64 )in[ 0] ); break; \
76-
case 0: break; \
77-
} \
78-
\
79-
v3 ^= b; \
80-
\
81-
SIP_ROUNDS; \
82-
\
83-
v0 ^= b; \
84-
\
85-
v2 ^= 0xff; \
86-
\
87-
SIP_FINAL_ROUNDS \
88-
\
89-
b = v0 ^ v1 ^ v2 ^ v3; \
90-
return b; \
91-
} \
92-
\
93-
PERL_STATIC_INLINE U32 \
94-
FNC ## _with_state \
76+
case 0: break; \
77+
} \
78+
\
79+
v3 ^= b; \
80+
\
81+
SIP_ROUNDS; \
82+
\
83+
v0 ^= b; \
84+
\
85+
v2 ^= 0xff; \
86+
\
87+
SIP_FINAL_ROUNDS \
88+
\
89+
b = v0 ^ v1 ^ v2 ^ v3; \
90+
return b; \
91+
} \
92+
\
93+
PERL_STATIC_INLINE U32 \
94+
FNC ## _with_state \
9595
(const unsigned char * const state, const unsigned char *in, const STRLEN inlen) \
96-
{ \
97-
union { \
98-
U64 h64; \
99-
U32 h32[2]; \
100-
} h; \
96+
{ \
97+
union { \
98+
U64 h64; \
99+
U32 h32[2]; \
100+
} h; \
101101
h.h64= FNC ## _with_state_64(state,in,inlen); \
102-
return h.h32[0] ^ h.h32[1]; \
103-
} \
104-
\
105-
\
106-
PERL_STATIC_INLINE U32 \
102+
return h.h32[0] ^ h.h32[1]; \
103+
} \
104+
\
105+
\
106+
PERL_STATIC_INLINE U32 \
107107
FNC (const unsigned char * const seed, const unsigned char *in, const STRLEN inlen) \
108-
{ \
109-
U64 state[4]; \
108+
{ \
109+
U64 state[4]; \
110110
SIPHASH_SEED_STATE(seed,state[0],state[1],state[2],state[3]); \
111111
return FNC ## _with_state((U8*)state,in,inlen); \
112+
} \
113+
\
114+
PERL_STATIC_INLINE U64 \
115+
FNC ## _64 (const unsigned char * const seed, const unsigned char *in, const STRLEN inlen) \
116+
{ \
117+
U64 state[4]; \
118+
SIPHASH_SEED_STATE(seed,state[0],state[1],state[2],state[3]); \
119+
return FNC ## _with_state_64((U8*)state,in,inlen); \
112120
}
113121

114-
115122
PERL_SIPHASH_FNC(
116123
S_perl_hash_siphash_1_3
117124
,SIPROUND;

0 commit comments

Comments
 (0)