diff options
author | Yves Orton <demerphq@gmail.com> | 2020-01-24 16:08:35 +0100 |
---|---|---|
committer | James E Keenan <jkeenan@cpan.org> | 2020-01-24 12:45:46 -0500 |
commit | 9af31f94746ab52901fec0d3e10bc769da49c7e9 (patch) | |
tree | 5f808fa86696bb42285f1f15202a011fa35f7ee4 /hv_func.h | |
parent | fb953ad4077a4a0f7b8204f52f3ce71feca190d2 (diff) | |
download | perl-9af31f94746ab52901fec0d3e10bc769da49c7e9.tar.gz |
hv_func.h: tweak how we deal with 64 bit Siphash
This slightly modifies how we apply Siphash in our code
so that instead of taking the low 32 bits of the hash
we return the xor of the high and low 32 bits.
This also adds /* FALLTHROUGH */ statements to some of
the related code so that picky compilers don't get upset
as it is not a mistake.
Lastly this allows our test code to access both the
"real" 64 bit Siphash, and also the reduced 32 bit version.
Most of this is in preparation for adding tests for
some of our hash related code. See next commit.
Diffstat (limited to 'hv_func.h')
-rw-r--r-- | hv_func.h | 34 |
1 files changed, 24 insertions, 10 deletions
@@ -130,7 +130,7 @@ U32 S_perl_hash_with_seed(const U8 * const seed, const U8 * const str, const STR * including reading from the environment, or we randomly setup the * seed. The seed will be passed into the PERL_HASH_SEED_STATE() function * defined for the configuration defined for this perl, which will then - * initialze whatever state it might need later in hashing. */ + * initialize whatever state it might need later in hashing. */ #ifndef PERL_HASH_SEED # if defined(USE_HASH_SEED) @@ -197,8 +197,8 @@ void S_perl_siphash_seed_state(const unsigned char * const seed_buf, unsigned ch } #define PERL_SIPHASH_FNC(FNC,SIP_ROUNDS,SIP_FINAL_ROUNDS) \ -PERL_STATIC_INLINE U32 \ -FNC ## _with_state \ +PERL_STATIC_INLINE U64 \ +FNC ## _with_state_64 \ (const unsigned char * const state, const unsigned char *in, const STRLEN inlen) \ { \ const int left = inlen & 7; \ @@ -223,12 +223,12 @@ FNC ## _with_state \ \ switch( left ) \ { \ - case 7: b |= ( ( U64 )in[ 6] ) << 48; \ - case 6: b |= ( ( U64 )in[ 5] ) << 40; \ - case 5: b |= ( ( U64 )in[ 4] ) << 32; \ - case 4: b |= ( ( U64 )in[ 3] ) << 24; \ - case 3: b |= ( ( U64 )in[ 2] ) << 16; \ - case 2: b |= ( ( U64 )in[ 1] ) << 8; \ + case 7: b |= ( ( U64 )in[ 6] ) << 48; /*FALLTHROUGH*/ \ + case 6: b |= ( ( U64 )in[ 5] ) << 40; /*FALLTHROUGH*/ \ + case 5: b |= ( ( U64 )in[ 4] ) << 32; /*FALLTHROUGH*/ \ + case 4: b |= ( ( U64 )in[ 3] ) << 24; /*FALLTHROUGH*/ \ + case 3: b |= ( ( U64 )in[ 2] ) << 16; /*FALLTHROUGH*/ \ + case 2: b |= ( ( U64 )in[ 1] ) << 8; /*FALLTHROUGH*/ \ case 1: b |= ( ( U64 )in[ 0] ); break; \ case 0: break; \ } \ @@ -244,10 +244,23 @@ FNC ## _with_state \ SIP_FINAL_ROUNDS \ \ b = v0 ^ v1 ^ v2 ^ v3; \ - return (U32)(b & U32_MAX); \ + return b; \ } \ \ PERL_STATIC_INLINE U32 \ +FNC ## _with_state \ + (const unsigned char * const state, const unsigned char *in, const STRLEN inlen) \ +{ \ + union { \ + U64 h64; \ + U32 h32[2]; \ + } h; \ + h.h64= FNC ## _with_state_64(state,in,inlen); \ + return h.h32[0] ^ h.h32[1]; \ +} \ + \ + \ +PERL_STATIC_INLINE U32 \ FNC (const unsigned char * const seed, const unsigned char *in, const STRLEN inlen) \ { \ U64 state[4]; \ @@ -267,6 +280,7 @@ PERL_SIPHASH_FNC( ,SIPROUND;SIPROUND; ,SIPROUND;SIPROUND;SIPROUND;SIPROUND; ) + #endif /* defined(CAN64BITHASH) */ |