From 56cbabb752599584611fbd7587118cee3ecedc05 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 Oct 2014 02:05:59 +0100 Subject: [PATCH 1/4] Makefile : C compiler selection is OS driven --- Makefile | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index c579ea6c..21c0ed23 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,9 @@ # xxHash.exe : benchmark program, to demonstrate xxHash speed # ################################################################ -CC=gcc -CFLAGS+= -I. -std=c99 -O3 -Wall -Wextra -Wundef -Wshadow -Wstrict-prototypes +CC := $(CC) +CFLAGS ?= -O3 +CFLAGS += -I. -std=c99 -Wall -Wextra -Wundef -Wshadow -Wstrict-prototypes # Define *.exe as extension for Windows systems @@ -34,6 +35,7 @@ else EXT = endif + default: xxhsum all: xxhsum xxhsum32 @@ -50,9 +52,9 @@ test: $(TEST_TARGETS) test: xxhsum ./xxhsum -b xxhash.c - valgrind ./xxhsum -bi1 xxhash.c - valgrind ./xxhsum -H0 xxhash.c - valgrind ./xxhsum -H1 xxhash.c + valgrind --leak-check=yes ./xxhsum -bi1 xxhash.c + valgrind --leak-check=yes ./xxhsum -H0 xxhash.c + valgrind --leak-check=yes ./xxhsum -H1 xxhash.c test-all: test xxhsum32 ./xxhsum32 -b xxhash.c From ef3cc0c80a386682ce45c795e74dbd679fdeee1a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 Oct 2014 02:42:26 +0100 Subject: [PATCH 2/4] xxhsum : introduces gettimeofday() as a potential replacement for ftime() --- xxhsum.c | 65 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/xxhsum.c b/xxhsum.c index 7cfdb1ed..39347681 100644 --- a/xxhsum.c +++ b/xxhsum.c @@ -21,26 +21,35 @@ You can contact the author at : - Discussion group : https://groups.google.com/forum/?fromgroups#!forum/lz4c */ -//************************************** -// Compiler Options -//************************************** -// Visual warning messages (must be first line) -#define _CRT_SECURE_NO_WARNINGS +/************************************** + * Compiler Options + *************************************/ +/* 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 -// Under Linux at least, pull in the *64 commands +/* Under Linux at least, pull in the *64 commands */ #define _LARGEFILE64_SOURCE -//************************************** -// Includes -//************************************** +/************************************** + * Includes + *************************************/ #include // malloc #include // fprintf, fopen, ftello64 #include // strcmp -#include // timeb #include // stat64 #include // stat64 +// Use ftime() if gettimeofday() is not available on your target +#if defined(BMK_LEGACY_TIMER) +# include // timeb, ftime +#else +# include // gettimeofday +#endif + #include "xxhash.h" @@ -84,8 +93,8 @@ You can contact the author at : #define TIMELOOP 2500 // Minimum timing per iteration #define PRIME 2654435761U -#define KB *(1U<<10) -#define MB *(1U<<20) +#define KB *(1<<10) +#define MB *(1<<20) #define GB *(1U<<30) #define MAX_MEM (2 GB - 64 MB) @@ -112,18 +121,34 @@ static int g_fn_selection = 1; // Benchmark Functions //********************************************************* +#if defined(BMK_LEGACY_TIMER) + +static int BMK_GetMilliStart(void) +{ + // Based on Legacy ftime() + // Rolls over every ~ 12.1 days (0x100000/24/60/60) + // Use GetMilliSpan to correct for rollover + struct timeb tb; + int nCount; + ftime( &tb ); + nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000); + return nCount; +} + +#else + static int BMK_GetMilliStart(void) { - // 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; + // Based on newer gettimeofday() + // Use GetMilliSpan to correct for rollover + struct timeval tv; + int nCount; + gettimeofday(&tv, NULL); + nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); + return nCount; } +#endif static int BMK_GetMilliSpan( int nTimeStart ) { From f7f77986f19a94a573590a0e0ec8c3caf48fe5c8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 Oct 2014 13:55:58 +0100 Subject: [PATCH 3/4] xxhsum : can use stdin as input (default) --- xxhsum.c | 161 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 90 insertions(+), 71 deletions(-) diff --git a/xxhsum.c b/xxhsum.c index 39347681..e090e511 100644 --- a/xxhsum.c +++ b/xxhsum.c @@ -38,11 +38,17 @@ You can contact the author at : * Includes *************************************/ #include // malloc -#include // fprintf, fopen, ftello64 +#include // fprintf, fopen, ftello64, fread, stdin, stdout; when present : _fileno #include // strcmp #include // stat64 #include // stat64 +#include "xxhash.h" + + +/************************************** + * OS-Specific Includes + *************************************/ // Use ftime() if gettimeofday() is not available on your target #if defined(BMK_LEGACY_TIMER) # include // timeb, ftime @@ -50,20 +56,28 @@ You can contact the author at : # include // gettimeofday #endif -#include "xxhash.h" - +#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) +# include // _O_BINARY +# include // _setmode, _isatty +# ifdef __MINGW32__ + int _fileno(FILE *stream); // MINGW somehow forgets to include this windows declaration into +# endif +# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY) +# define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) +#else +# include // isatty, STDIN_FILENO +# define SET_BINARY_MODE(file) +# define IS_CONSOLE(stdStream) isatty(STDIN_FILENO) +#endif -//************************************** -// Compiler specifics -//************************************** #if !defined(S_ISREG) # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) #endif -//************************************** -// Basic Types -//************************************** +/************************************** + * Basic Types + *************************************/ #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 # include typedef uint8_t BYTE; @@ -80,9 +94,9 @@ You can contact the author at : #endif -//************************************** -// Constants -//************************************** +/************************************** + * Constants + *************************************/ #define PROGRAM_NAME exename #define PROGRAM_VERSION "" #define COMPILED __DATE__ @@ -99,6 +113,7 @@ You can contact the author at : #define MAX_MEM (2 GB - 64 MB) +static const char stdinName[] = "-"; //************************************** @@ -111,10 +126,10 @@ static unsigned g_displayLevel = 1; //************************************** -// Global variables +// Unit variables //************************************** static int g_nbIterations = NBLOOPS; -static int g_fn_selection = 1; +static int g_fn_selection = 1; // required within main() & usage() //********************************************************* @@ -196,7 +211,7 @@ static U64 BMK_GetFileSize(char* infilename) } -int BMK_benchFile(char** fileNamesTable, int nbFiles) +static int BMK_benchFile(char** fileNamesTable, int nbFiles) { int fileIdx=0; U32 hashResult=0; @@ -478,7 +493,7 @@ static void BMK_sanityCheck(void) } -int BMK_hash(char* fileName, U32 hashNb) +static int BMK_hash(const char* fileName, U32 hashNb) { FILE* inFile; size_t const blockSize = 64 KB; @@ -487,7 +502,13 @@ int BMK_hash(char* fileName, U32 hashNb) XXH64_state_t state; // Check file existence - inFile = fopen( fileName, "rb" ); + if (fileName == stdinName) + { + inFile = stdin; + SET_BINARY_MODE(stdin); + } + else + inFile = fopen( fileName, "rb" ); if (inFile==NULL) { DISPLAY( "Pb opening %s\n", fileName); @@ -568,11 +589,12 @@ int BMK_hash(char* fileName, U32 hashNb) // Main //********************************************************* -int usage(char* exename) +static int usage(const char* exename) { DISPLAY( WELCOME_MESSAGE ); DISPLAY( "Usage :\n"); - DISPLAY( " %s [arg] filename\n", exename); + DISPLAY( " %s [arg] [filename]\n", exename); + DISPLAY( "When no filename provided, or - provided : use stdin as input\n"); DISPLAY( "Arguments :\n"); DISPLAY( " -H# : hash selection : 0=32bits, 1=64bits (default %i)\n", g_fn_selection); DISPLAY( " -b : benchmark mode \n"); @@ -582,7 +604,7 @@ int usage(char* exename) } -int badusage(char* exename) +static int badusage(const char* exename) { DISPLAY("Wrong parameters\n"); usage(exename); @@ -592,16 +614,13 @@ int badusage(char* exename) int main(int argc, char** argv) { - int i, - filenamesStart=0; - char* input_filename=0; - char* exename = argv[0]; + int i, filenamesStart=0; + const char* input_filename = (char*)stdinName; + const char* exename = argv[0]; U32 benchmarkMode = 0; - // lz4cat behavior - if (strstr(argv[0], "xxh32sum")!=NULL) g_fn_selection=0; - - if (argc<2) return badusage(exename); + // xxh32sum default to 32 bits checksum + if (strstr(exename, "xxh32sum")!=NULL) g_fn_selection=0; for(i=1; i Error - if(!input_filename) { badusage(exename); return 1; } - - if(g_fn_selection < 0 || g_fn_selection > 1) { badusage(exename); return 1; } + if(g_fn_selection < 0 || g_fn_selection > 1) return badusage(exename); - return BMK_hash(argv[filenamesStart], g_fn_selection); + return BMK_hash(input_filename, g_fn_selection); } From f452e84660042202e1447af7da4d40f5591a3d8c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 Oct 2014 14:02:50 +0100 Subject: [PATCH 4/4] Added : stdin test --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 21c0ed23..9843f5e4 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ xxhsum32: xxhash.c xxhsum.c test: $(TEST_TARGETS) test: xxhsum + ./xxhsum < xxhash.c ./xxhsum -b xxhash.c valgrind --leak-check=yes ./xxhsum -bi1 xxhash.c valgrind --leak-check=yes ./xxhsum -H0 xxhash.c