X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/3f023586c2fbf826d45cf78795361337eca3daa1..fb81daf0179f156be3f2a95cd5cf1d27e9f7ebbb:/hv_func.h diff --git a/hv_func.h b/hv_func.h index b05eb87..a519839 100644 --- a/hv_func.h +++ b/hv_func.h @@ -4,92 +4,11 @@ * to avoid "algorithmic complexity attacks". * * If USE_HASH_SEED is defined, hash randomisation is done by default - * If USE_HASH_SEED_EXPLICIT is defined, hash randomisation is done - * only if the environment variable PERL_HASH_SEED is set. * (see also perl.c:perl_parse() and S_init_tls_and_interp() and util.c:get_hash_seed()) */ - #ifndef PERL_SEEN_HV_FUNC_H /* compile once */ #define PERL_SEEN_HV_FUNC_H - -#if IVSIZE == 8 -#define CAN64BITHASH -#endif - -/*----------------------------------------------------------------------------- - * Endianess, misalignment capabilities and util macros - * - * The following 3 macros are defined in this section. The other macros defined - * are only needed to help derive these 3. - * - * U8TO16_LE(x) Read a little endian unsigned 32-bit int - * U8TO32_LE(x) Read a little endian unsigned 32-bit int - * U8TO28_LE(x) Read a little endian unsigned 32-bit int - * ROTL32(x,r) Rotate x left by r bits - * ROTL64(x,r) Rotate x left by r bits - * ROTR32(x,r) Rotate x right by r bits - * ROTR64(x,r) Rotate x right by r bits - */ - -#ifndef U32_ALIGNMENT_REQUIRED - #if (BYTEORDER == 0x1234 || BYTEORDER == 0x12345678) - #define U8TO16_LE(ptr) (*((const U16*)(ptr))) - #define U8TO32_LE(ptr) (*((const U32*)(ptr))) - #define U8TO64_LE(ptr) (*((const U64*)(ptr))) - #elif (BYTEORDER == 0x4321 || BYTEORDER == 0x87654321) - #if defined(__GNUC__) && (__GNUC__>4 || (__GNUC__==4 && __GNUC_MINOR__>=3)) - #define U8TO16_LE(ptr) (__builtin_bswap16(*((U16*)(ptr)))) - #define U8TO32_LE(ptr) (__builtin_bswap32(*((U32*)(ptr)))) - #define U8TO64_LE(ptr) (__builtin_bswap64(*((U64*)(ptr)))) - #endif - #endif -#endif - -#ifndef U8TO16_LE - /* Without a known fast bswap32 we're just as well off doing this */ - #define U8TO16_LE(ptr) ((U32)(ptr)[0]|(U32)(ptr)[1]<<8) - #define U8TO32_LE(ptr) ((U32)(ptr)[0]|(U32)(ptr)[1]<<8|(U32)(ptr)[2]<<16|(U32)(ptr)[3]<<24) - #define U8TO64_LE(ptr) ((U64)(ptr)[0]|(U64)(ptr)[1]<<8|(U64)(ptr)[2]<<16|(U64)(ptr)[3]<<24|\ - (U64)(ptr)[4]<<32|(U64)(ptr)[5]<<40|\ - (U64)(ptr)[6]<<48|(U64)(ptr)[7]<<56) -#endif - -#ifdef CAN64BITHASH - #ifndef U64TYPE - /* This probably isn't going to work, but failing with a compiler error due to - lack of uint64_t is no worse than failing right now with an #error. */ - #define U64 uint64_t - #endif -#endif - -/* Find best way to ROTL32/ROTL64 */ -#if defined(_MSC_VER) - #include /* Microsoft put _rotl declaration in here */ - #define ROTL32(x,r) _rotl(x,r) - #define ROTR32(x,r) _rotr(x,r) - #define ROTL64(x,r) _rotl64(x,r) - #define ROTR64(x,r) _rotr64(x,r) -#else - /* gcc recognises this code and generates a rotate instruction for CPUs with one */ - #define ROTL32(x,r) (((U32)(x) << (r)) | ((U32)(x) >> (32 - (r)))) - #define ROTR32(x,r) (((U32)(x) << (32 - (r))) | ((U32)(x) >> (r))) - #define ROTL64(x,r) ( ( (U64)(x) << (r) ) | ( (U64)(x) >> ( 64 - (r) ) ) ) - #define ROTR64(x,r) ( ( (U64)(x) << ( 64 - (r) ) ) | ( (U64)(x) >> (r) ) ) -#endif - - -#ifdef UV_IS_QUAD -#define ROTL_UV(x,r) ROTL64(x,r) -#define ROTR_UV(x,r) ROTL64(x,r) -#else -#define ROTL_UV(x,r) ROTL32(x,r) -#define ROTR_UV(x,r) ROTR32(x,r) -#endif - -/*-----------------------------------------------------------------------------* - * And now set up the actual hashing macros - *-----------------------------------------------------------------------------*/ -#define PERL_HASH_FUNC_ZAPHOD32 +#include "hv_macro.h" #if !( 0 \ || defined(PERL_HASH_FUNC_SIPHASH) \ @@ -128,7 +47,7 @@ # define __PERL_HASH_SEED_STATE(seed,state) S_perl_siphash_seed_state(seed,state) # define __PERL_HASH_WITH_STATE(state,str,len) S_perl_hash_siphash_1_3_with_state((state),(U8*)(str),(len)) #elif defined(PERL_HASH_FUNC_STADTX) -# define __PERL_HASH_FUNC "STATDX" +# define __PERL_HASH_FUNC "STADTX" # define __PERL_HASH_SEED_BYTES 16 # define __PERL_HASH_STATE_BYTES 32 # define __PERL_HASH_SEED_STATE(seed,state) stadtx_seed_state(seed,state) @@ -154,7 +73,7 @@ #endif -#if PERL_HASH_USE_SBOX32_ALSO == 1 +#if PERL_HASH_USE_SBOX32_ALSO != 1 # define _PERL_HASH_FUNC __PERL_HASH_FUNC # define _PERL_HASH_SEED_BYTES __PERL_HASH_SEED_BYTES # define _PERL_HASH_STATE_BYTES __PERL_HASH_STATE_BYTES @@ -164,19 +83,19 @@ #define _PERL_HASH_FUNC "SBOX32_WITH_" __PERL_HASH_FUNC -#define _PERL_HASH_SEED_BYTES ( __PERL_HASH_SEED_BYTES + ( 3 * sizeof(U32) ) ) +#define _PERL_HASH_SEED_BYTES ( __PERL_HASH_SEED_BYTES + (int)( 3 * sizeof(U32) ) ) #define _PERL_HASH_STATE_BYTES \ - ( __PERL_HASH_SEED_BYTES + ( ( 1 + ( 256 * SBOX32_MAX_LEN ) ) * sizeof(U32) ) ) + ( __PERL_HASH_STATE_BYTES + ( ( 1 + ( 256 * SBOX32_MAX_LEN ) ) * sizeof(U32) ) ) #define _PERL_HASH_SEED_STATE(seed,state) STMT_START { \ __PERL_HASH_SEED_STATE(seed,state); \ - sbox32_seed_state96(seed + __PERL_HASH_SEED_BYTES , state + __PERL_HASH_STATE_BYTES); \ + sbox32_seed_state96(seed + __PERL_HASH_SEED_BYTES, state + __PERL_HASH_STATE_BYTES); \ } STMT_END -#define _PERL_HASH_WITH_STATE(state,str,len) \ - ((len <= SBOX32_MAX_LEN) \ - ? sbox32_hash_with_state((state + __PERL_HASH_STATE_BYTES),(U8*)(str),(len)) \ +#define _PERL_HASH_WITH_STATE(state,str,len) \ + (LIKELY(len <= SBOX32_MAX_LEN) \ + ? sbox32_hash_with_state((state + __PERL_HASH_STATE_BYTES),(U8*)(str),(len)) \ : __PERL_HASH_WITH_STATE((state),(str),(len))) #endif @@ -190,7 +109,7 @@ U32 S_perl_hash_with_seed(const U8 * const seed, const U8 * const str, const STR } #define PERL_HASH_WITH_SEED(seed,hash,str,len) \ - (hash) = S_perl_hash_with_seed(seed,str,len) + (hash) = S_perl_hash_with_seed((const U8 *) seed, (const U8 *) str,len) #define PERL_HASH_WITH_STATE(state,hash,str,len) \ (hash) = _PERL_HASH_WITH_STATE((state),(U8*)(str),(len)) #define PERL_HASH_SEED_STATE(seed,state) _PERL_HASH_SEED_STATE(seed,state) @@ -214,7 +133,7 @@ U32 S_perl_hash_with_seed(const U8 * const seed, const U8 * const str, const STR * initialze whatever state it might need later in hashing. */ #ifndef PERL_HASH_SEED -# if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT) +# if defined(USE_HASH_SEED) # define PERL_HASH_SEED PL_hash_seed # else /* this is a 512 bit seed, which should be more than enough for the @@ -265,10 +184,10 @@ do { \ v0 = v2 = U8TO64_LE(key + 0); \ v1 = v3 = U8TO64_LE(key + 8); \ /* "somepseudorandomlygeneratedbytes" */ \ - v0 ^= 0x736f6d6570736575ull; \ - v1 ^= 0x646f72616e646f6dull; \ - v2 ^= 0x6c7967656e657261ull; \ - v3 ^= 0x7465646279746573ull; \ + v0 ^= UINT64_C(0x736f6d6570736575); \ + v1 ^= UINT64_C(0x646f72616e646f6d); \ + v2 ^= UINT64_C(0x6c7967656e657261); \ + v3 ^= UINT64_C(0x7465646279746573); \ } while (0) PERL_STATIC_INLINE @@ -351,9 +270,6 @@ PERL_SIPHASH_FNC( #endif /* defined(CAN64BITHASH) */ - - - #endif /*compile once*/ /*