Runtime endianess detection

removed typecast warnings for 64-bits CPU with aligned-only memory accesses
made bench.c compatible with tcc
small bench.c optimisation, reducing impact of timer calls 


git-svn-id: https://xxhash.googlecode.com/svn/trunk@31 a90a0800-428d-89a3-ac20-94765a7798ee
This commit is contained in:
yann.collet.73@gmail.com 2013-07-25 08:40:23 +00:00
parent 681ea2aad6
commit 99a867a14a
2 changed files with 323 additions and 320 deletions

397
bench.c
View File

@ -1,41 +1,35 @@
/*
bench.c - Demo program to benchmark open-source algorithm
Copyright (C) Yann Collet 2012-2013
bench.c - Demo program to benchmark open-source algorithm
Copyright (C) Yann Collet 2012-2013
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- Blog homepage : http://fastcompression.blogspot.com/
- Discussion group : https://groups.google.com/forum/?fromgroups#!forum/lz4c
You can contact the author at :
- Blog homepage : http://fastcompression.blogspot.com/
- Discussion group : https://groups.google.com/forum/?fromgroups#!forum/lz4c
*/
//**************************************
// Compiler Options
//**************************************
// Visual warning messages (must be first line)
// Visual warning messages (must be first line)
#define _CRT_SECURE_NO_WARNINGS
// Under Linux at least, pull in the *64 commands
#define _LARGEFILE64_SOURCE
// MSVC does not support S_ISREG
#ifndef S_ISREG
#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif
//**************************************
// Includes
@ -47,6 +41,14 @@
#include <sys/stat.h> // stat64
//**************************************
// Compiler specifics
//**************************************
#if !defined(S_ISREG)
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif
//**************************************
// Hash Functions to test
//**************************************
@ -55,23 +57,22 @@
#define HASH0 XXH32
//**************************************
// Basic Types
//**************************************
#if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
#define BYTE unsigned __int8
#define U16 unsigned __int16
#define U32 unsigned __int32
#define S32 __int32
#define U64 unsigned __int64
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
# include <stdint.h>
typedef uint8_t BYTE;
typedef uint16_t U16;
typedef uint32_t U32;
typedef int32_t S32;
typedef uint64_t U64;
#else
#include <stdint.h>
#define BYTE uint8_t
#define U16 uint16_t
#define U32 uint32_t
#define S32 int32_t
#define U64 uint64_t
typedef unsigned char BYTE;
typedef unsigned short U16;
typedef unsigned int U32;
typedef signed int S32;
typedef unsigned long long U64;
#endif
@ -84,10 +85,10 @@
#define AUTHOR "Yann Collet"
#define WELCOME_MESSAGE "*** %s %s, by %s (%s) ***\n", PROGRAM_NAME, PROGRAM_VERSION, AUTHOR, COMPILED
#define NBLOOPS 3 // Default number of benchmark iterations
#define TIMELOOP 2000 // Minimum timing per iteration
#define NBLOOPS 3 // Default number of benchmark iterations
#define TIMELOOP 2000 // Minimum timing per iteration
#define MAX_MEM (1984<<20)
#define MAX_MEM (1984<<20)
//**************************************
@ -96,7 +97,7 @@
struct hashFunctionPrototype
{
unsigned int (*hashFunction)(const void*, int, unsigned int);
unsigned int (*hashFunction)(const void*, int, unsigned int);
};
@ -114,8 +115,8 @@ static int nbIterations = NBLOOPS;
void BMK_SetNbIterations(int nbLoops)
{
nbIterations = nbLoops;
DISPLAY("- %i iterations-", nbIterations);
nbIterations = nbLoops;
DISPLAY("- %i iterations-", nbIterations);
}
@ -126,178 +127,182 @@ void BMK_SetNbIterations(int nbLoops)
static int BMK_GetMilliStart()
{
// Supposed to be portable
// Rolls over every ~ 12.1 days (0x100000/24/60/60)
// Use GetMilliSpan to correct for rollover
struct timeb tb;
int nCount;
ftime( &tb );
nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
return nCount;
// Supposed to be portable
// Rolls over every ~ 12.1 days (0x100000/24/60/60)
// Use GetMilliSpan to correct for rollover
struct timeb tb;
int nCount;
ftime( &tb );
nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
return nCount;
}
static int BMK_GetMilliSpan( int nTimeStart )
{
int nSpan = BMK_GetMilliStart() - nTimeStart;
if ( nSpan < 0 )
nSpan += 0x100000 * 1000;
return nSpan;
int nSpan = BMK_GetMilliStart() - nTimeStart;
if ( nSpan < 0 )
nSpan += 0x100000 * 1000;
return nSpan;
}
static size_t BMK_findMaxMem(U64 requiredMem)
{
size_t step = (64U<<20); // 64 MB
BYTE* testmem=NULL;
size_t step = (64U<<20); // 64 MB
BYTE* testmem=NULL;
requiredMem = (((requiredMem >> 25) + 1) << 26);
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
requiredMem = (((requiredMem >> 25) + 1) << 26);
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
requiredMem += 2*step;
while (!testmem)
{
requiredMem -= step;
testmem = malloc ((size_t)requiredMem);
}
requiredMem += 2*step;
while (!testmem)
{
requiredMem -= step;
testmem = malloc ((size_t)requiredMem);
}
free (testmem);
return (size_t) (requiredMem - step);
free (testmem);
return (size_t) (requiredMem - step);
}
static U64 BMK_GetFileSize(char* infilename)
{
int r;
int r;
#if defined(_MSC_VER)
struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
struct _stat64 statbuf;
r = _stat64(infilename, &statbuf);
#else
struct stat statbuf;
r = stat(infilename, &statbuf);
struct stat statbuf;
r = stat(infilename, &statbuf);
#endif
if (r || !S_ISREG(statbuf.st_mode)) return 0; // No good...
return (U64)statbuf.st_size;
if (r || !S_ISREG(statbuf.st_mode)) return 0; // No good...
return (U64)statbuf.st_size;
}
int BMK_benchFile(char** fileNamesTable, int nbFiles, int selection)
{
int fileIdx=0;
FILE* fileIn;
char* infilename;
U64 largefilesize;
size_t benchedsize;
size_t readSize;
char* in_buff;
struct hashFunctionPrototype hashP;
unsigned int hashResult=0;
int fileIdx=0;
FILE* fileIn;
char* infilename;
U64 largefilesize;
size_t benchedsize;
size_t readSize;
char* buffer;
char* in_buff;
struct hashFunctionPrototype hashP;
unsigned int hashResult=0;
U64 totals = 0;
double totalc = 0.;
U64 totals = 0;
double totalc = 0.;
// Init
switch (selection)
{
// Init
switch (selection)
{
#ifdef HASH0
case 0 : hashP.hashFunction = HASH0; break;
case 0 : hashP.hashFunction = HASH0; break;
#endif
#ifdef HASH1
case 1 : hashP.hashFunction = HASH1; break;
case 1 : hashP.hashFunction = HASH1; break;
#endif
#ifdef HASH2
case 2 : hashP.hashFunction = HASH2; break;
case 2 : hashP.hashFunction = HASH2; break;
#endif
default: hashP.hashFunction = DEFAULTHASH;
}
default: hashP.hashFunction = DEFAULTHASH;
}
// Loop for each file
while (fileIdx<nbFiles)
{
// Check file existence
infilename = fileNamesTable[fileIdx++];
fileIn = fopen( infilename, "rb" );
if (fileIn==NULL)
{
DISPLAY( "Pb opening %s\n", infilename);
return 11;
}
// Loop for each file
while (fileIdx<nbFiles)
{
// Check file existence
infilename = fileNamesTable[fileIdx++];
fileIn = fopen( infilename, "rb" );
if (fileIn==NULL)
{
DISPLAY( "Pb opening %s\n", infilename);
return 11;
}
// Memory allocation & restrictions
largefilesize = BMK_GetFileSize(infilename);
benchedsize = (size_t) BMK_findMaxMem(largefilesize);
if ((U64)benchedsize > largefilesize) benchedsize = (size_t)largefilesize;
if (benchedsize < largefilesize)
{
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", infilename, (int)(benchedsize>>20));
}
// Memory allocation & restrictions
largefilesize = BMK_GetFileSize(infilename);
benchedsize = (size_t) BMK_findMaxMem(largefilesize);
if ((U64)benchedsize > largefilesize) benchedsize = (size_t)largefilesize;
if (benchedsize < largefilesize)
{
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", infilename, (int)(benchedsize>>20));
}
in_buff = malloc((size_t )benchedsize);
buffer = (char*)malloc((size_t )benchedsize+16);
if(!buffer)
{
DISPLAY("\nError: not enough memory!\n");
fclose(fileIn);
return 12;
}
in_buff = (buffer+15) - (((size_t)(buffer+15)) & 0xF); // align buffer on next 16 bytes boundaries
if(!in_buff)
{
DISPLAY("\nError: not enough memory!\n");
free(in_buff);
fclose(fileIn);
return 12;
}
// Fill input buffer
DISPLAY("Loading %s... \r", infilename);
readSize = fread(in_buff, 1, benchedsize, fileIn);
fclose(fileIn);
// Fill input buffer
DISPLAY("Loading %s... \r", infilename);
readSize = fread(in_buff, 1, benchedsize, fileIn);
fclose(fileIn);
if(readSize != benchedsize)
{
DISPLAY("\nError: problem reading file '%s' !! \n", infilename);
free(in_buff);
return 13;
}
if(readSize != benchedsize)
{
DISPLAY("\nError: problem reading file '%s' !! \n", infilename);
free(buffer);
return 13;
}
// Bench
{
int loopNb, nb_loops;
int milliTime;
double fastestC = 100000000.;
// Bench
{
int loopNb, nb_loops;
int milliTime;
double fastestC = 100000000.;
DISPLAY("\r%79s\r", ""); // Clean display line
for (loopNb = 1; loopNb <= nbIterations; loopNb++)
{
// Hash
DISPLAY("%1i-%-14.14s : %10i ->\r", loopNb, infilename, (int)benchedsize);
DISPLAY("\r%79s\r", ""); // Clean display line
for (loopNb = 1; loopNb <= nbIterations; loopNb++)
{
// Hash
DISPLAY("%1i-%-14.14s : %10i ->\r", loopNb, infilename, (int)benchedsize);
nb_loops = 0;
milliTime = BMK_GetMilliStart();
while(BMK_GetMilliStart() == milliTime);
milliTime = BMK_GetMilliStart();
while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
{
hashResult = hashP.hashFunction(in_buff, (int)benchedsize, 0);
nb_loops++;
}
milliTime = BMK_GetMilliSpan(milliTime);
nb_loops = 0;
milliTime = BMK_GetMilliStart();
while(BMK_GetMilliStart() == milliTime);
milliTime = BMK_GetMilliStart();
while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
{
int i;
for (i=0; i<100; i++)
{
hashResult = hashP.hashFunction(in_buff, (int)benchedsize, 0);
nb_loops++;
}
}
milliTime = BMK_GetMilliSpan(milliTime);
if ((double)milliTime < fastestC*nb_loops) fastestC = (double)milliTime/nb_loops;
if ((double)milliTime < fastestC*nb_loops) fastestC = (double)milliTime/nb_loops;
DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", loopNb, infilename, (int)benchedsize, (double)benchedsize / fastestC / 1000.);
DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", loopNb, infilename, (int)benchedsize, (double)benchedsize / fastestC / 1000.);
}
}
DISPLAY("%-16.16s : %10i -> %7.1f MB/s 0x%08X\n", infilename, (int)benchedsize, (double)benchedsize / fastestC / 1000., hashResult);
DISPLAY("%-16.16s : %10i -> %7.1f MB/s 0x%08X\n", infilename, (int)benchedsize, (double)benchedsize / fastestC / 1000., hashResult);
totals += benchedsize;
totalc += fastestC;
}
totals += benchedsize;
totalc += fastestC;
}
free(in_buff);
}
free(buffer);
}
if (nbFiles > 1)
printf("%-16.16s :%11llu -> %7.1f MB/s\n", " TOTAL", (long long unsigned int)totals, (double)totals/totalc/1000.);
if (nbFiles > 1)
printf("%-16.16s :%11llu -> %7.1f MB/s\n", " TOTAL", (long long unsigned int)totals, (double)totals/totalc/1000.);
return 0;
return 0;
}
@ -307,62 +312,62 @@ int BMK_benchFile(char** fileNamesTable, int nbFiles, int selection)
int usage(char* exename)
{
DISPLAY( "Usage :\n");
DISPLAY( " %s [arg] filename\n", exename);
DISPLAY( "Arguments :\n");
DISPLAY( " -i# : number of iterations \n");
DISPLAY( " -h : help (this text)\n");
return 0;
DISPLAY( "Usage :\n");
DISPLAY( " %s [arg] filename\n", exename);
DISPLAY( "Arguments :\n");
DISPLAY( " -i# : number of iterations \n");
DISPLAY( " -h : help (this text)\n");
return 0;
}
int badusage(char* exename)
{
DISPLAY("Wrong parameters\n");
usage(exename);
return 0;
DISPLAY("Wrong parameters\n");
usage(exename);
return 0;
}
int main(int argc, char** argv)
{
int i,
filenamesStart=2;
char* input_filename=0;
int i,
filenamesStart=2;
char* input_filename=0;
// Welcome message
DISPLAY( WELCOME_MESSAGE );
// Welcome message
DISPLAY( WELCOME_MESSAGE );
if (argc<2) { badusage(argv[0]); return 1; }
if (argc<2) { badusage(argv[0]); return 1; }
for(i=1; i<argc; i++)
{
char* argument = argv[i];
for(i=1; i<argc; i++)
{
char* argument = argv[i];
if(!argument) continue; // Protection if argument empty
if(!argument) continue; // Protection if argument empty
// Select command
if (argument[0]=='-')
{
argument ++;
// Select command
if (argument[0]=='-')
{
argument ++;
// Display help on usage
if ( argument[0] =='h' ) { usage(argv[0]); return 0; }
// Display help on usage
if ( argument[0] =='h' ) { usage(argv[0]); return 0; }
// Modify Nb Iterations (benchmark only)
if ( argument[0] =='i' ) { int iters = argument[1] - '0'; BMK_SetNbIterations(iters); continue; }
// Modify Nb Iterations (benchmark only)
if ( argument[0] =='i' ) { int iters = argument[1] - '0'; BMK_SetNbIterations(iters); continue; }
}
}
// first provided filename is input
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
// first provided filename is input
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
}
}
// No input filename ==> Error
if(!input_filename) { badusage(argv[0]); return 1; }
// No input filename ==> Error
if(!input_filename) { badusage(argv[0]); return 1; }
return BMK_benchFile(argv+filenamesStart, argc-filenamesStart, 0);
return BMK_benchFile(argv+filenamesStart, argc-filenamesStart, 0);
}

246
xxhash.c
View File

@ -31,7 +31,6 @@ You can contact the author at :
*/
//**************************************
// Tuning parameters
//**************************************
@ -44,8 +43,8 @@ You can contact the author at :
#endif
// XXH_ACCEPT_NULL_INPUT_POINTER :
// If the input pointer is a null pointer, xxHash default behavior is to crash, since it is a bad input.
// If this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
// If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
// When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
// This option has a very small performance cost (only measurable on small inputs).
// By default, this option is disabled. To enable it, uncomment below define :
//#define XXH_ACCEPT_NULL_INPUT_POINTER 1
@ -54,17 +53,28 @@ You can contact the author at :
// By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
// Results are therefore identical for little-endian and big-endian CPU.
// This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
// Should endian-independance be of no importance for your application, you may uncomment the #define below.
// Should endian-independance be of no importance for your application, you may set the #define below to 1.
// It will improve speed for Big-endian CPU.
// This option has no impact on Little_Endian CPU.
//#define XXH_FORCE_NATIVE_FORMAT 1
#define XXH_FORCE_NATIVE_FORMAT 0
//**************************************
// Compiler Options
// Compiler Specific Options
//**************************************
#if defined(_MSC_VER) && !defined(__cplusplus) // Visual Studio
# define inline __inline // Visual C is not C99, but supports some kind of inline
// Disable some Visual warning messages
#ifdef _MSC_VER // Visual Studio
# pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
#endif
#ifdef _MSC_VER // Visual Studio
# define forceinline static __forceinline
#else
# ifdef __GNUC__
# define forceinline static inline __attribute__((always_inline))
# else
# define forceinline static inline
# endif
#endif
@ -75,39 +85,11 @@ You can contact the author at :
// Modify the local functions below should you wish to use some other memory related routines
// for malloc(), free()
#include <stdlib.h>
static inline void* XXH_malloc(size_t s) { return malloc(s); }
static inline void XXH_free (void* p) { free(p); }
forceinline void* XXH_malloc(size_t s) { return malloc(s); }
forceinline void XXH_free (void* p) { free(p); }
// for memcpy()
#include <string.h>
static inline void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
//**************************************
// CPU Feature Detection
//**************************************
// Little Endian or Big Endian ?
// You can overwrite the #define below if you know your architecture endianess
#if defined(XXH_FORCE_NATIVE_FORMAT) && (XXH_FORCE_NATIVE_FORMAT==1)
// Force native format. The result will be endian dependant.
# define XXH_BIG_ENDIAN 0
#elif defined (__GLIBC__)
# include <endian.h>
# if (__BYTE_ORDER == __BIG_ENDIAN)
# define XXH_BIG_ENDIAN 1
# endif
#elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
# define XXH_BIG_ENDIAN 1
#elif defined(__sparc) || defined(__sparc__) \
|| defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) \
|| defined(__hpux) || defined(__hppa) \
|| defined(_MIPSEB) || defined(__s390__)
# define XXH_BIG_ENDIAN 1
#endif
#if !defined(XXH_BIG_ENDIAN)
// Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
# define XXH_BIG_ENDIAN 0
#endif
forceinline void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
//**************************************
@ -135,7 +117,11 @@ static inline void* XXH_memcpy(void* dest, const void* src, size_t size) { retur
#endif
#if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
# pragma pack(push, 1)
# ifdef __IBMC__
# pragma pack(1)
# else
# pragma pack(push, 1)
# endif
#endif
typedef struct _U32_S { U32 v; } _PACKED U32_S;
@ -182,90 +168,54 @@ static inline U32 XXH_swap32 (U32 x) {
#define PRIME32_5 374761393U
//**************************************
// Architecture Macros
//**************************************
typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
#ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch
static const int one = 1;
# define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one))
#endif
//**************************************
// Macros
//**************************************
#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations
#define XXH_LE32(p) (XXH_BIG_ENDIAN ? XXH_swap32(A32(p)) : A32(p))
#define XXH_alignedLE32(p) (XXH_BIG_ENDIAN ? XXH_swap32(*(U32*)(p)) : *(U32*)(p))
#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations
//****************************
// Memory reads
//****************************
typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
forceinline U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align)
{
if (align==XXH_unaligned)
return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr));
else
return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr);
}
forceinline U32 XXH_readLE32(const U32* ptr, XXH_endianess endian) { return XXH_readLE32_align(ptr, endian, XXH_unaligned); }
//****************************
// Simple Hash Functions
//****************************
#if !defined(XXH_USE_UNALIGNED_ACCESS)
// Specific version, for aligned 32-bits input. Useless for CPU supporting unaligned access.
static U32 XXH32_alignedInput(const void* input, int len, U32 seed)
forceinline U32 XXH32_endian_align(const void* input, int len, U32 seed, XXH_endianess endian, XXH_alignment align)
{
const BYTE* p = (const BYTE*)input;
const BYTE* const bEnd = p + len;
U32 h32;
if (len>=16)
{
const BYTE* const limit = bEnd - 16;
U32 v1 = seed + PRIME32_1 + PRIME32_2;
U32 v2 = seed + PRIME32_2;
U32 v3 = seed + 0;
U32 v4 = seed - PRIME32_1;
do
{
v1 += XXH_alignedLE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
v2 += XXH_alignedLE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
v3 += XXH_alignedLE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
v4 += XXH_alignedLE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
} while (p<=limit);
h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
}
else { h32 = seed + PRIME32_5; }
h32 += (U32) len;
while (p<=bEnd-4)
{
h32 += XXH_alignedLE32(p) * PRIME32_3;
h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
p+=4;
}
while (p<bEnd)
{
h32 += (*p) * PRIME32_5;
h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
p++;
}
h32 ^= h32 >> 15;
h32 *= PRIME32_2;
h32 ^= h32 >> 13;
h32 *= PRIME32_3;
h32 ^= h32 >> 16;
return h32;
}
#endif
U32 XXH32(const void* input, int len, U32 seed)
{
#if 0
// Simple version, good for code maintenance, but unfortunately slow for small inputs
void* state = XXH32_init(seed);
XXH32_update(state, input, len);
return XXH32_digest(state);
#else
const BYTE* p = (const BYTE*)input;
const BYTE* const bEnd = p + len;
U32 h32;
#ifdef XXH_ACCEPT_NULL_INPUT_POINTER
if (p==NULL) { len=0; p=(const BYTE*)16; }
#endif
#if !defined(XXH_USE_UNALIGNED_ACCESS)
if ((((U32)p) & 3) == 0) return XXH32_alignedInput(input, len, seed); // Input is aligned, let's leverage the speed advantage
if (p==NULL) { len=0; p=(const BYTE*)(size_t)16; }
#endif
if (len>=16)
{
const BYTE* const limit = bEnd - 16;
const BYTE* const limit = bEnd - 32;
U32 v1 = seed + PRIME32_1 + PRIME32_2;
U32 v2 = seed + PRIME32_2;
U32 v3 = seed + 0;
@ -273,10 +223,10 @@ U32 XXH32(const void* input, int len, U32 seed)
do
{
v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
v1 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
v2 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
v3 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
v4 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
} while (p<=limit);
h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
@ -290,8 +240,8 @@ U32 XXH32(const void* input, int len, U32 seed)
while (p<=bEnd-4)
{
h32 += XXH_LE32(p) * PRIME32_3;
h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
h32 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_3;
h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
p+=4;
}
@ -309,7 +259,33 @@ U32 XXH32(const void* input, int len, U32 seed)
h32 ^= h32 >> 16;
return h32;
}
U32 XXH32(const void* input, int len, U32 seed)
{
#if 0
// Simple version, good for code maintenance, but unfortunately slow for small inputs
void* state = XXH32_init(seed);
XXH32_update(state, input, len);
return XXH32_digest(state);
#else
XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
# if !defined(XXH_USE_UNALIGNED_ACCESS)
if ((((size_t)input) & 3)) // Input is aligned, let's leverage the speed advantage
{
if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
else
return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
}
# endif
if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
else
return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
#endif
}
@ -360,7 +336,7 @@ void* XXH32_init (U32 seed)
}
XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
forceinline XXH_errorcode XXH32_update_endian (void* state_in, const void* input, int len, XXH_endianess endian)
{
struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
const BYTE* p = (const BYTE*)input;
@ -384,10 +360,10 @@ XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize);
{
const U32* p32 = (const U32*)state->memory;
state->v1 += XXH_LE32(p32) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++;
state->v2 += XXH_LE32(p32) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++;
state->v3 += XXH_LE32(p32) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++;
state->v4 += XXH_LE32(p32) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++;
state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++;
state->v2 += XXH_readLE32(p32, endian) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++;
state->v3 += XXH_readLE32(p32, endian) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++;
state->v4 += XXH_readLE32(p32, endian) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++;
}
p += 16-state->memsize;
state->memsize = 0;
@ -403,10 +379,10 @@ XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
do
{
v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
} while (p<=limit);
state->v1 = v1;
@ -424,11 +400,22 @@ XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
return XXH_OK;
}
XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
{
XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
else
return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
}
U32 XXH32_intermediateDigest (void* state_in)
forceinline U32 XXH32_intermediateDigest_endian (void* state_in, XXH_endianess endian)
{
struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
BYTE * p = (BYTE*)state->memory;
const BYTE * p = (const BYTE*)state->memory;
BYTE* bEnd = (BYTE*)state->memory + state->memsize;
U32 h32;
@ -445,8 +432,8 @@ U32 XXH32_intermediateDigest (void* state_in)
while (p<=bEnd-4)
{
h32 += XXH_LE32(p) * PRIME32_3;
h32 = XXH_rotl32(h32, 17) * PRIME32_4;
h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3;
h32 = XXH_rotl32(h32, 17) * PRIME32_4;
p+=4;
}
@ -467,6 +454,17 @@ U32 XXH32_intermediateDigest (void* state_in)
}
U32 XXH32_intermediateDigest (void* state_in)
{
XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
return XXH32_intermediateDigest_endian(state_in, XXH_littleEndian);
else
return XXH32_intermediateDigest_endian(state_in, XXH_bigEndian);
}
U32 XXH32_digest (void* state_in)
{
U32 h32 = XXH32_intermediateDigest(state_in);