2014-08-15 09:27:04 +00:00
|
|
|
/*
|
2015-05-04 21:56:53 +00:00
|
|
|
xxhsum - Command line interface for xxhash algorithms
|
2014-12-09 18:16:15 +00:00
|
|
|
Copyright (C) Yann Collet 2012-2015
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
GPL v2 License
|
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
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 :
|
2015-05-04 21:56:53 +00:00
|
|
|
- xxHash source repository : https://github.com/Cyan4973/xxHash
|
2014-08-15 09:27:04 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*************************************
|
|
|
|
* Compiler Options
|
|
|
|
*************************************/
|
2014-10-29 01:42:26 +00:00
|
|
|
/* MS Visual */
|
|
|
|
#if defined(_MSC_VER) || defined(_WIN32)
|
|
|
|
# define _CRT_SECURE_NO_WARNINGS /* removes visual warnings */
|
|
|
|
# define BMK_LEGACY_TIMER 1 /* gettimeofday() not supported by MSVC */
|
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 01:42:26 +00:00
|
|
|
/* Under Linux at least, pull in the *64 commands */
|
2014-08-15 09:27:04 +00:00
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
|
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*************************************
|
|
|
|
* Includes
|
|
|
|
*************************************/
|
2014-12-17 11:15:03 +00:00
|
|
|
#include <stdlib.h> /* malloc */
|
|
|
|
#include <stdio.h> /* fprintf, fopen, ftello64, fread, stdin, stdout; when present : _fileno */
|
|
|
|
#include <string.h> /* strcmp */
|
|
|
|
#include <sys/types.h> /* stat64 */
|
|
|
|
#include <sys/stat.h> /* stat64 */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
#include "xxhash.h"
|
|
|
|
|
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*************************************
|
|
|
|
* OS-Specific Includes
|
|
|
|
*************************************/
|
2015-05-07 14:27:27 +00:00
|
|
|
/* Use ftime() if gettimeofday() is not available on your target */
|
2014-10-29 01:42:26 +00:00
|
|
|
#if defined(BMK_LEGACY_TIMER)
|
2015-05-07 14:27:27 +00:00
|
|
|
# include <sys/timeb.h> /* timeb, ftime */
|
2014-10-29 01:42:26 +00:00
|
|
|
#else
|
2015-05-07 14:27:27 +00:00
|
|
|
# include <sys/time.h> /* gettimeofday */
|
2014-10-29 01:42:26 +00:00
|
|
|
#endif
|
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
2015-05-07 14:27:27 +00:00
|
|
|
# include <fcntl.h> /* _O_BINARY */
|
|
|
|
# include <io.h> /* _setmode, _isatty */
|
2014-10-29 12:55:58 +00:00
|
|
|
# ifdef __MINGW32__
|
2015-05-07 14:27:27 +00:00
|
|
|
int _fileno(FILE *stream); /* MINGW somehow forgets to include this windows declaration into <stdio.h> */
|
2014-10-29 12:55:58 +00:00
|
|
|
# endif
|
|
|
|
# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)
|
|
|
|
# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
|
|
|
|
#else
|
2015-05-07 14:27:27 +00:00
|
|
|
# include <unistd.h> /* isatty, STDIN_FILENO */
|
2014-10-29 12:55:58 +00:00
|
|
|
# define SET_BINARY_MODE(file)
|
|
|
|
# define IS_CONSOLE(stdStream) isatty(STDIN_FILENO)
|
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
#if !defined(S_ISREG)
|
|
|
|
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*************************************
|
|
|
|
* Basic Types
|
|
|
|
*************************************/
|
|
|
|
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
|
2014-08-15 09:27:04 +00:00
|
|
|
# include <stdint.h>
|
|
|
|
typedef uint8_t BYTE;
|
|
|
|
typedef uint16_t U16;
|
|
|
|
typedef uint32_t U32;
|
|
|
|
typedef int32_t S32;
|
|
|
|
typedef uint64_t U64;
|
|
|
|
#else
|
|
|
|
typedef unsigned char BYTE;
|
|
|
|
typedef unsigned short U16;
|
|
|
|
typedef unsigned int U32;
|
|
|
|
typedef signed int S32;
|
|
|
|
typedef unsigned long long U64;
|
|
|
|
#endif
|
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
static unsigned BMK_isLittleEndian(void)
|
|
|
|
{
|
|
|
|
const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
|
|
|
|
return one.c[0];
|
|
|
|
}
|
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
/**************************************
|
2015-05-07 14:27:27 +00:00
|
|
|
* Constants
|
|
|
|
**************************************/
|
2014-10-18 11:03:38 +00:00
|
|
|
#define PROGRAM_NAME exename
|
2014-08-15 09:27:04 +00:00
|
|
|
#define PROGRAM_VERSION ""
|
|
|
|
#define COMPILED __DATE__
|
2015-05-07 14:27:27 +00:00
|
|
|
static const char author[] = "Yann Collet";
|
|
|
|
#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s (%s) ***\n", PROGRAM_NAME, (int)(sizeof(void*)*8), PROGRAM_VERSION, author, COMPILED
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
#define NBLOOPS 3 /* Default number of benchmark iterations */
|
|
|
|
#define TIMELOOP 2500 /* Minimum timing per iteration */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
#define KB *( 1<<10)
|
|
|
|
#define MB *( 1<<20)
|
2014-08-15 09:27:04 +00:00
|
|
|
#define GB *(1U<<30)
|
|
|
|
|
|
|
|
#define MAX_MEM (2 GB - 64 MB)
|
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
static const char stdinName[] = "-";
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*************************************
|
|
|
|
* Display macros
|
|
|
|
*************************************/
|
2014-10-23 20:36:23 +00:00
|
|
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#define DISPLAYRESULT(...) fprintf(stdout, __VA_ARGS__)
|
2014-10-18 11:03:38 +00:00
|
|
|
#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) DISPLAY(__VA_ARGS__);
|
|
|
|
static unsigned g_displayLevel = 1;
|
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*************************************
|
|
|
|
* Local variables
|
|
|
|
*************************************/
|
2014-10-18 11:03:38 +00:00
|
|
|
static int g_nbIterations = NBLOOPS;
|
2015-01-01 16:10:04 +00:00
|
|
|
static int g_fn_selection = 1; /* required within main() & usage() */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*************************************
|
|
|
|
* Benchmark Functions
|
|
|
|
*************************************/
|
2014-10-29 01:42:26 +00:00
|
|
|
#if defined(BMK_LEGACY_TIMER)
|
|
|
|
|
|
|
|
static int BMK_GetMilliStart(void)
|
|
|
|
{
|
2015-01-01 16:10:04 +00:00
|
|
|
/* Based on Legacy ftime()
|
|
|
|
* Rolls over every ~ 12.1 days (0x100000/24/60/60)
|
|
|
|
* Use GetMilliSpan to correct for rollover */
|
2014-10-29 01:42:26 +00:00
|
|
|
struct timeb tb;
|
|
|
|
int nCount;
|
|
|
|
ftime( &tb );
|
|
|
|
nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000);
|
|
|
|
return nCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
static int BMK_GetMilliStart(void)
|
|
|
|
{
|
2015-01-01 16:10:04 +00:00
|
|
|
/* Based on newer gettimeofday()
|
|
|
|
* Use GetMilliSpan to correct for rollover */
|
2014-10-29 01:42:26 +00:00
|
|
|
struct timeval tv;
|
|
|
|
int nCount;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000);
|
|
|
|
return nCount;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 01:42:26 +00:00
|
|
|
#endif
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
static int BMK_GetMilliSpan( int nTimeStart )
|
|
|
|
{
|
|
|
|
int nSpan = BMK_GetMilliStart() - nTimeStart;
|
|
|
|
if ( nSpan < 0 )
|
|
|
|
nSpan += 0x100000 * 1000;
|
|
|
|
return nSpan;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-28 11:20:23 +00:00
|
|
|
static size_t BMK_findMaxMem(U64 requiredMem)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2015-06-28 11:20:23 +00:00
|
|
|
size_t step = 64 MB;
|
2014-08-15 09:27:04 +00:00
|
|
|
BYTE* testmem=NULL;
|
|
|
|
|
2015-06-28 11:20:23 +00:00
|
|
|
requiredMem = (((requiredMem >> 26) + 1) << 26);
|
|
|
|
requiredMem += 2*step;
|
|
|
|
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
while (!testmem)
|
|
|
|
{
|
2015-06-28 11:20:23 +00:00
|
|
|
if (requiredMem > step) requiredMem -= step;
|
|
|
|
else requiredMem >>= 1;
|
|
|
|
testmem = (BYTE*) malloc ((size_t)requiredMem);
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
free (testmem);
|
|
|
|
|
2015-06-28 11:20:23 +00:00
|
|
|
/* keep some space available */
|
|
|
|
if (requiredMem > step) requiredMem -= step;
|
|
|
|
else requiredMem >>= 1;
|
|
|
|
|
|
|
|
return (size_t)requiredMem;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
static U64 BMK_GetFileSize(const char* infilename)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
struct _stat64 statbuf;
|
|
|
|
r = _stat64(infilename, &statbuf);
|
|
|
|
#else
|
|
|
|
struct stat statbuf;
|
|
|
|
r = stat(infilename, &statbuf);
|
|
|
|
#endif
|
2015-01-01 16:10:04 +00:00
|
|
|
if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
|
2014-08-15 09:27:04 +00:00
|
|
|
return (U64)statbuf.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Note : buffer is supposed malloc'ed, hence aligned */
|
|
|
|
static void BMK_benchMem(const void* buffer, size_t bufferSize)
|
|
|
|
{
|
|
|
|
static const int nbh_perloop = 100;
|
|
|
|
|
|
|
|
/* XXH32 bench */
|
|
|
|
{
|
|
|
|
int iterationNb;
|
|
|
|
double fastestH = 100000000.;
|
2015-06-28 11:20:23 +00:00
|
|
|
U32 hashResult = 0;
|
2015-05-07 12:30:27 +00:00
|
|
|
|
|
|
|
DISPLAY("\r%79s\r", ""); /* Clean display line */
|
2015-06-28 11:20:23 +00:00
|
|
|
if (g_nbIterations<1) g_nbIterations=1;
|
2015-05-07 12:30:27 +00:00
|
|
|
for (iterationNb = 1; iterationNb <= g_nbIterations; iterationNb++)
|
|
|
|
{
|
|
|
|
int nbHashes = 0;
|
|
|
|
int milliTime;
|
|
|
|
|
|
|
|
DISPLAY("%1i-%-14.14s : %10i ->\r", iterationNb, "XXH32", (int)bufferSize);
|
|
|
|
|
|
|
|
/* Timing loop */
|
|
|
|
milliTime = BMK_GetMilliStart();
|
|
|
|
while(BMK_GetMilliStart() == milliTime);
|
|
|
|
milliTime = BMK_GetMilliStart();
|
|
|
|
while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<nbh_perloop; i++)
|
|
|
|
{
|
|
|
|
hashResult = XXH32(buffer, bufferSize, 0);
|
|
|
|
}
|
|
|
|
nbHashes += nbh_perloop;
|
|
|
|
}
|
|
|
|
milliTime = BMK_GetMilliSpan(milliTime);
|
|
|
|
if ((double)milliTime < fastestH*nbHashes) fastestH = (double)milliTime/nbHashes;
|
|
|
|
DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", iterationNb, "XXH32", (int)bufferSize, (double)bufferSize / fastestH / 1000.);
|
|
|
|
}
|
|
|
|
DISPLAY("%-16.16s : %10i -> %7.1f MB/s 0x%08X\n", "XXH32", (int)bufferSize, (double)bufferSize / fastestH / 1000., hashResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bench XXH32 on Unaligned input */
|
|
|
|
{
|
|
|
|
int iterationNb;
|
|
|
|
double fastestH = 100000000.;
|
|
|
|
|
|
|
|
DISPLAY("\r%79s\r", ""); /* Clean display line */
|
|
|
|
for (iterationNb = 1; (iterationNb <= g_nbIterations) && ((bufferSize>1)); iterationNb++)
|
|
|
|
{
|
|
|
|
int nbHashes = 0;
|
|
|
|
int milliTime;
|
|
|
|
const char* charPtr = (const char*)buffer;
|
|
|
|
|
|
|
|
DISPLAY("%1i-%-14.14s : %10i ->\r", iterationNb, "(unaligned)", (int)(bufferSize-1));
|
|
|
|
/* timing loop */
|
|
|
|
milliTime = BMK_GetMilliStart();
|
|
|
|
while(BMK_GetMilliStart() == milliTime);
|
|
|
|
milliTime = BMK_GetMilliStart();
|
|
|
|
while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<nbh_perloop; i++)
|
|
|
|
{
|
|
|
|
XXH32(charPtr+1, bufferSize-1, 0);
|
|
|
|
}
|
|
|
|
nbHashes += nbh_perloop;
|
|
|
|
}
|
|
|
|
milliTime = BMK_GetMilliSpan(milliTime);
|
|
|
|
if ((double)milliTime < fastestH*nbHashes) fastestH = (double)milliTime/nbHashes;
|
|
|
|
DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", iterationNb, "XXH32 (unaligned)", (int)(bufferSize-1), (double)(bufferSize-1) / fastestH / 1000.);
|
|
|
|
}
|
|
|
|
DISPLAY("%-16.16s : %10i -> %7.1f MB/s \n", "XXH32 (unaligned)", (int)(bufferSize-1), (double)(bufferSize-1) / fastestH / 1000.);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bench XXH64 */
|
|
|
|
{
|
|
|
|
int iterationNb;
|
|
|
|
double fastestH = 100000000.;
|
|
|
|
unsigned long long h64 = 0;
|
|
|
|
|
|
|
|
DISPLAY("\r%79s\r", ""); /* Clean display line */
|
|
|
|
for (iterationNb = 1; iterationNb <= g_nbIterations; iterationNb++)
|
|
|
|
{
|
|
|
|
int nbHashes = 0;
|
|
|
|
int milliTime;
|
|
|
|
|
|
|
|
DISPLAY("%1i-%-14.14s : %10i ->\r", iterationNb, "XXH64", (int)bufferSize);
|
|
|
|
|
|
|
|
/* Timing loop */
|
|
|
|
milliTime = BMK_GetMilliStart();
|
|
|
|
while(BMK_GetMilliStart() == milliTime);
|
|
|
|
milliTime = BMK_GetMilliStart();
|
|
|
|
while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<nbh_perloop; i++)
|
|
|
|
{
|
|
|
|
h64 = XXH64(buffer, bufferSize, 0);
|
|
|
|
}
|
|
|
|
nbHashes+=nbh_perloop;
|
|
|
|
}
|
|
|
|
milliTime = BMK_GetMilliSpan(milliTime);
|
|
|
|
if ((double)milliTime < fastestH*nbHashes) fastestH = (double)milliTime/nbHashes;
|
|
|
|
DISPLAY("%1i-%-14.14s : %10i -> %7.1f MB/s\r", iterationNb, "XXH64", (int)bufferSize, (double)bufferSize / fastestH / 1000.);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
DISPLAY("%-16.16s : %10i -> %7.1f MB/s 0x", "XXH64", (int)bufferSize, (double)bufferSize / fastestH / 1000.);
|
2015-05-07 14:27:27 +00:00
|
|
|
DISPLAY("%08X%08X", (U32)(h64 >> 32), (U32)h64);
|
2015-05-07 12:30:27 +00:00
|
|
|
DISPLAY("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
static int BMK_benchFiles(const char** fileNamesTable, int nbFiles)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
int fileIdx=0;
|
|
|
|
|
|
|
|
while (fileIdx<nbFiles)
|
|
|
|
{
|
|
|
|
FILE* inFile;
|
2015-08-07 23:22:56 +00:00
|
|
|
const char* inFileName;
|
2014-08-15 09:27:04 +00:00
|
|
|
U64 inFileSize;
|
|
|
|
size_t benchedSize;
|
|
|
|
size_t readSize;
|
|
|
|
char* buffer;
|
|
|
|
char* alignedBuffer;
|
|
|
|
|
2015-01-01 16:10:04 +00:00
|
|
|
/* Check file existence */
|
2014-08-15 09:27:04 +00:00
|
|
|
inFileName = fileNamesTable[fileIdx++];
|
|
|
|
inFile = fopen( inFileName, "rb" );
|
2015-06-28 11:20:23 +00:00
|
|
|
if ((inFile==NULL) || (inFileName==NULL))
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
DISPLAY( "Pb opening %s\n", inFileName);
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
|
2015-01-01 16:10:04 +00:00
|
|
|
/* Memory allocation & restrictions */
|
2014-08-15 09:27:04 +00:00
|
|
|
inFileSize = BMK_GetFileSize(inFileName);
|
|
|
|
benchedSize = (size_t) BMK_findMaxMem(inFileSize);
|
|
|
|
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
|
|
|
|
if (benchedSize < inFileSize)
|
|
|
|
{
|
|
|
|
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20));
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (char*)malloc((size_t )benchedSize+16);
|
|
|
|
if(!buffer)
|
|
|
|
{
|
|
|
|
DISPLAY("\nError: not enough memory!\n");
|
|
|
|
fclose(inFile);
|
|
|
|
return 12;
|
|
|
|
}
|
2015-01-01 16:10:04 +00:00
|
|
|
alignedBuffer = (buffer+15) - (((size_t)(buffer+15)) & 0xF); /* align on next 16 bytes boundaries */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-01-01 16:10:04 +00:00
|
|
|
/* Fill input buffer */
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY("\rLoading %s... \n", inFileName);
|
|
|
|
readSize = fread(alignedBuffer, 1, benchedSize, inFile);
|
|
|
|
fclose(inFile);
|
|
|
|
|
|
|
|
if(readSize != benchedSize)
|
|
|
|
{
|
|
|
|
DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
|
|
|
|
free(buffer);
|
|
|
|
return 13;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* bench */
|
|
|
|
BMK_benchMem(alignedBuffer, benchedSize);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
free(buffer);
|
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
static int BMK_benchInternal(void)
|
|
|
|
{
|
|
|
|
static const size_t benchedSize = 100 KB;
|
|
|
|
void* buffer;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
buffer = malloc(benchedSize);
|
|
|
|
if(!buffer)
|
|
|
|
{
|
|
|
|
DISPLAY("\nError: not enough memory!\n");
|
|
|
|
return 12;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* bench */
|
|
|
|
DISPLAY("\rSample of %u KB... \n", (U32)(benchedSize >> 10));
|
|
|
|
BMK_benchMem(buffer, benchedSize);
|
|
|
|
|
|
|
|
free(buffer);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void BMK_checkResult(U32 r1, U32 r2)
|
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
|
|
|
|
|
|
|
if (r1==r2) DISPLAY("\rTest%3i : %08X == %08X ok ", nbTests, r1, r2);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DISPLAY("\rERROR : Test%3i : %08X <> %08X !!!!! \n", nbTests, r1, r2);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void BMK_checkResult64(U64 r1, U64 r2)
|
|
|
|
{
|
|
|
|
static int nbTests = 1;
|
|
|
|
|
|
|
|
if (r1!=r2)
|
|
|
|
{
|
|
|
|
DISPLAY("\rERROR : Test%3i : 64-bits values non equals !!!!! \n", nbTests);
|
2015-05-07 14:27:27 +00:00
|
|
|
DISPLAY("\r %08X%08X != %08X%08X \n", (U32)(r1>>32), (U32)r1, (U32)(r2>>32), (U32)r2);
|
2014-08-15 09:27:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nbTests++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void BMK_testSequence64(void* sentence, int len, U64 seed, U64 Nresult)
|
|
|
|
{
|
|
|
|
U64 Dresult;
|
2014-09-25 16:30:15 +00:00
|
|
|
XXH64_state_t state;
|
2014-08-15 09:27:04 +00:00
|
|
|
int index;
|
|
|
|
|
|
|
|
Dresult = XXH64(sentence, len, seed);
|
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
|
2014-09-25 16:30:15 +00:00
|
|
|
XXH64_reset(&state, seed);
|
|
|
|
XXH64_update(&state, sentence, len);
|
|
|
|
Dresult = XXH64_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
|
2014-09-25 16:30:15 +00:00
|
|
|
XXH64_reset(&state, seed);
|
|
|
|
for (index=0; index<len; index++) XXH64_update(&state, ((char*)sentence)+index, 1);
|
|
|
|
Dresult = XXH64_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult64(Dresult, Nresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
static void BMK_testSequence(const void* sequence, size_t len, U32 seed, U32 Nresult)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
U32 Dresult;
|
2014-09-25 16:30:15 +00:00
|
|
|
XXH32_state_t state;
|
2015-05-07 14:27:27 +00:00
|
|
|
size_t index;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
Dresult = XXH32(sequence, len, seed);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult(Dresult, Nresult);
|
|
|
|
|
2014-09-25 16:30:15 +00:00
|
|
|
XXH32_reset(&state, seed);
|
2015-05-07 14:27:27 +00:00
|
|
|
XXH32_update(&state, sequence, len);
|
2014-09-25 16:30:15 +00:00
|
|
|
Dresult = XXH32_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult(Dresult, Nresult);
|
|
|
|
|
2014-09-25 16:30:15 +00:00
|
|
|
XXH32_reset(&state, seed);
|
2015-05-07 14:27:27 +00:00
|
|
|
for (index=0; index<len; index++) XXH32_update(&state, ((const char*)sequence)+index, 1);
|
2014-09-25 16:30:15 +00:00
|
|
|
Dresult = XXH32_digest(&state);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_checkResult(Dresult, Nresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define SANITY_BUFFER_SIZE 101
|
|
|
|
static void BMK_sanityCheck(void)
|
|
|
|
{
|
|
|
|
BYTE sanityBuffer[SANITY_BUFFER_SIZE];
|
|
|
|
int i;
|
2015-05-07 14:27:27 +00:00
|
|
|
static const U32 prime = 2654435761U;
|
|
|
|
U32 byteGen = prime;
|
|
|
|
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
for (i=0; i<SANITY_BUFFER_SIZE; i++)
|
|
|
|
{
|
2015-05-07 14:27:27 +00:00
|
|
|
sanityBuffer[i] = (BYTE)(byteGen>>24);
|
|
|
|
byteGen *= byteGen;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BMK_testSequence(NULL, 0, 0, 0x02CC5D05);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(NULL, 0, prime, 0x36B78AE7);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 1, 0, 0xB85CBEE5);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 1, prime, 0xD5845D64);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 14, 0, 0xE5AA0AB4);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(sanityBuffer, 14, prime, 0x4481951D);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence(sanityBuffer, SANITY_BUFFER_SIZE, 0, 0x1F1AA412);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence(sanityBuffer, SANITY_BUFFER_SIZE, prime, 0x498EC8E2);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
BMK_testSequence64(NULL , 0, 0, 0xEF46DB3751D8E999ULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(NULL , 0, prime, 0xAC75FDA2929B17EFULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 1, 0, 0x4FCE394CC88952D8ULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 1, prime, 0x739840CB819FA723ULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 14, 0, 0xCFFA8DB881BC3A3DULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, 14, prime, 0x5B9611585EFCC9CBULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, SANITY_BUFFER_SIZE, 0, 0x0EAB543384F878ADULL);
|
2015-05-07 14:27:27 +00:00
|
|
|
BMK_testSequence64(sanityBuffer, SANITY_BUFFER_SIZE, prime, 0xCAA65939306F1E21ULL);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
DISPLAY("\r%79s\r", ""); /* Clean display line */
|
2014-09-25 20:22:59 +00:00
|
|
|
DISPLAYLEVEL(2, "Sanity check -- all tests ok\n");
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2014-12-09 17:42:33 +00:00
|
|
|
static void BMK_display_BigEndian(const void* ptr, size_t length)
|
|
|
|
{
|
2015-05-05 00:01:10 +00:00
|
|
|
const BYTE* p = (const BYTE*)ptr;
|
2015-05-07 14:27:27 +00:00
|
|
|
size_t index = BMK_isLittleEndian() ? length-1 : 0;
|
|
|
|
int incr = BMK_isLittleEndian() ? -1 : 1;
|
|
|
|
while (index<length) { DISPLAYRESULT("%02x", p[index]); index += incr; } /* intentional underflow to negative to detect end */
|
2014-12-09 17:42:33 +00:00
|
|
|
}
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
static int BMK_hash(const char* fileName, U32 hashNb)
|
2014-09-25 20:22:59 +00:00
|
|
|
{
|
|
|
|
FILE* inFile;
|
|
|
|
size_t const blockSize = 64 KB;
|
|
|
|
size_t readSize;
|
2015-08-07 23:22:56 +00:00
|
|
|
void* buffer;
|
|
|
|
XXH64_state_t state; /* sizeof >= XXH32_state_t */
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Check file existence */
|
2014-10-29 12:55:58 +00:00
|
|
|
if (fileName == stdinName)
|
|
|
|
{
|
|
|
|
inFile = stdin;
|
|
|
|
SET_BINARY_MODE(stdin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
inFile = fopen( fileName, "rb" );
|
2014-09-25 20:22:59 +00:00
|
|
|
if (inFile==NULL)
|
|
|
|
{
|
|
|
|
DISPLAY( "Pb opening %s\n", fileName);
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Memory allocation & restrictions */
|
2015-08-07 23:22:56 +00:00
|
|
|
buffer = malloc(blockSize);
|
2014-09-25 20:22:59 +00:00
|
|
|
if(!buffer)
|
|
|
|
{
|
|
|
|
DISPLAY("\nError: not enough memory!\n");
|
|
|
|
fclose(inFile);
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Init */
|
2014-09-25 20:22:59 +00:00
|
|
|
switch(hashNb)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
XXH32_reset((XXH32_state_t*)&state, 0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
XXH64_reset(&state, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DISPLAY("Error : bad hash algorithm ID\n");
|
|
|
|
fclose(inFile);
|
|
|
|
free(buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Load file & update hash */
|
2014-09-25 20:22:59 +00:00
|
|
|
DISPLAY("\rLoading %s... \r", fileName);
|
|
|
|
readSize = 1;
|
|
|
|
while (readSize)
|
|
|
|
{
|
|
|
|
readSize = fread(buffer, 1, blockSize, inFile);
|
|
|
|
switch(hashNb)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
XXH32_update((XXH32_state_t*)&state, buffer, readSize);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
XXH64_update(&state, buffer, readSize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(inFile);
|
|
|
|
free(buffer);
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* display Hash */
|
2014-09-25 20:22:59 +00:00
|
|
|
switch(hashNb)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
U32 h32 = XXH32_digest((XXH32_state_t*)&state);
|
2014-12-09 17:42:33 +00:00
|
|
|
BMK_display_BigEndian(&h32, 4);
|
2015-08-07 23:22:56 +00:00
|
|
|
DISPLAYRESULT(" %s \n", fileName);
|
2014-09-25 20:22:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
U64 h64 = XXH64_digest(&state);
|
2014-12-09 17:42:33 +00:00
|
|
|
BMK_display_BigEndian(&h64, 8);
|
2015-08-07 23:22:56 +00:00
|
|
|
DISPLAYRESULT(" %s \n", fileName);
|
2014-09-25 20:22:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
static int BMK_hashFiles(const char** fnList, int fnTotal, U32 hashNb)
|
|
|
|
{
|
|
|
|
int fnNb;
|
|
|
|
int result = 0;
|
|
|
|
if (fnTotal==0)
|
|
|
|
{
|
|
|
|
result = BMK_hash(stdinName, hashNb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (fnNb=0; fnNb<fnTotal; fnNb++)
|
|
|
|
result |= BMK_hash(fnList[fnNb], hashNb);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/*********************************************************
|
|
|
|
* Main
|
|
|
|
*********************************************************/
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
static int usage(const char* exename)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2014-10-18 11:03:38 +00:00
|
|
|
DISPLAY( WELCOME_MESSAGE );
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY( "Usage :\n");
|
2014-10-29 12:55:58 +00:00
|
|
|
DISPLAY( " %s [arg] [filename]\n", exename);
|
|
|
|
DISPLAY( "When no filename provided, or - provided : use stdin as input\n");
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY( "Arguments :\n");
|
2014-10-18 11:03:38 +00:00
|
|
|
DISPLAY( " -H# : hash selection : 0=32bits, 1=64bits (default %i)\n", g_fn_selection);
|
2014-09-25 20:22:59 +00:00
|
|
|
DISPLAY( " -b : benchmark mode \n");
|
2014-10-18 11:03:38 +00:00
|
|
|
DISPLAY( " -i# : number of iterations (benchmark mode; default %i)\n", g_nbIterations);
|
2014-08-15 09:27:04 +00:00
|
|
|
DISPLAY( " -h : help (this text)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
static int badusage(const char* exename)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
|
|
|
DISPLAY("Wrong parameters\n");
|
|
|
|
usage(exename);
|
2014-09-25 20:22:59 +00:00
|
|
|
return 1;
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
int main(int argc, const char** argv)
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2014-10-29 12:55:58 +00:00
|
|
|
int i, filenamesStart=0;
|
|
|
|
const char* exename = argv[0];
|
2014-09-25 20:22:59 +00:00
|
|
|
U32 benchmarkMode = 0;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-04 21:56:53 +00:00
|
|
|
/* xxh32sum default to 32 bits checksum */
|
2014-10-29 12:55:58 +00:00
|
|
|
if (strstr(exename, "xxh32sum")!=NULL) g_fn_selection=0;
|
2014-08-15 09:27:04 +00:00
|
|
|
|
|
|
|
for(i=1; i<argc; i++)
|
|
|
|
{
|
2015-08-07 23:22:56 +00:00
|
|
|
const char* argument = argv[i];
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
if(!argument) continue; /* Protection, if argument empty */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
if (*argument!='-')
|
2014-08-15 09:27:04 +00:00
|
|
|
{
|
2015-08-07 23:22:56 +00:00
|
|
|
if (filenamesStart==0) filenamesStart=i; /* only supports a continuous list of filenames */
|
2014-10-29 12:55:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* command selection */
|
|
|
|
argument++; /* note : *argument=='-' */
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
while (*argument!=0)
|
|
|
|
{
|
|
|
|
switch(*argument)
|
2014-09-25 20:22:59 +00:00
|
|
|
{
|
2015-05-07 14:27:27 +00:00
|
|
|
/* Display help on usage */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'h':
|
|
|
|
return usage(exename);
|
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
/* select hash algorithm */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'H':
|
|
|
|
g_fn_selection = argument[1] - '0';
|
|
|
|
argument+=2;
|
|
|
|
break;
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Trigger benchmark mode */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'b':
|
|
|
|
argument++;
|
|
|
|
benchmarkMode=1;
|
|
|
|
break;
|
|
|
|
|
2015-05-07 14:27:27 +00:00
|
|
|
/* Modify Nb Iterations (benchmark only) */
|
2014-10-29 12:55:58 +00:00
|
|
|
case 'i':
|
|
|
|
g_nbIterations = argument[1] - '0';
|
|
|
|
argument+=2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return badusage(exename);
|
2014-09-25 20:22:59 +00:00
|
|
|
}
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Check benchmark mode */
|
2014-10-18 11:10:27 +00:00
|
|
|
if (benchmarkMode)
|
|
|
|
{
|
|
|
|
DISPLAY( WELCOME_MESSAGE );
|
2014-10-29 12:55:58 +00:00
|
|
|
BMK_sanityCheck();
|
2015-05-07 12:30:27 +00:00
|
|
|
if (filenamesStart==0) return BMK_benchInternal();
|
2015-08-07 23:22:56 +00:00
|
|
|
return BMK_benchFiles(argv+filenamesStart, argc-filenamesStart);
|
2014-10-18 11:10:27 +00:00
|
|
|
}
|
2014-09-25 20:22:59 +00:00
|
|
|
|
2015-05-07 12:30:27 +00:00
|
|
|
/* Check if input is defined as console; trigger an error in this case */
|
2015-08-07 23:22:56 +00:00
|
|
|
if ( (filenamesStart==0) && IS_CONSOLE(stdin) ) return badusage(exename);
|
2015-05-07 12:30:27 +00:00
|
|
|
|
2014-10-29 12:55:58 +00:00
|
|
|
if(g_fn_selection < 0 || g_fn_selection > 1) return badusage(exename);
|
2014-08-15 09:27:04 +00:00
|
|
|
|
2015-08-07 23:22:56 +00:00
|
|
|
if (filenamesStart==0) filenamesStart = argc;
|
|
|
|
return BMK_hashFiles(argv+filenamesStart, argc-filenamesStart, g_fn_selection);
|
2014-08-15 09:27:04 +00:00
|
|
|
}
|