From 5871585b6a28c6582d1626b8d07bb1a53a4cbd9d Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Sep 2016 19:06:01 -0700 Subject: coding style change --- lib/lz4hc.c | 150 ++++++++++++++++++++++-------------------------------------- 1 file changed, 55 insertions(+), 95 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 8bcebc8..c6f67cb 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -134,19 +134,18 @@ static void LZ4HC_init (LZ4HC_Data_Structure* hc4, const BYTE* start) /* Update chains up to ip (excluded) */ FORCE_INLINE void LZ4HC_Insert (LZ4HC_Data_Structure* hc4, const BYTE* ip) { - U16* chainTable = hc4->chainTable; - U32* HashTable = hc4->hashTable; + U16* const chainTable = hc4->chainTable; + U32* const hashTable = hc4->hashTable; const BYTE* const base = hc4->base; - const U32 target = (U32)(ip - base); + U32 const target = (U32)(ip - base); U32 idx = hc4->nextToUpdate; - while(idx < target) - { - U32 h = LZ4HC_hashPtr(base+idx); - size_t delta = idx - HashTable[h]; + while (idx < target) { + U32 const h = LZ4HC_hashPtr(base+idx); + size_t delta = idx - hashTable[h]; if (delta>MAX_DISTANCE) delta = MAX_DISTANCE; DELTANEXTU16(idx) = (U16)delta; - HashTable[h] = idx; + hashTable[h] = idx; idx++; } @@ -174,24 +173,19 @@ FORCE_INLINE int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, /* I LZ4HC_Insert(hc4, ip); matchIndex = HashTable[LZ4HC_hashPtr(ip)]; - while ((matchIndex>=lowLimit) && (nbAttempts)) - { + while ((matchIndex>=lowLimit) && (nbAttempts)) { nbAttempts--; - if (matchIndex >= dictLimit) - { + if (matchIndex >= dictLimit) { match = base + matchIndex; if (*(match+ml) == *(ip+ml) && (LZ4_read32(match) == LZ4_read32(ip))) { - size_t mlt = LZ4_count(ip+MINMATCH, match+MINMATCH, iLimit) + MINMATCH; + size_t const mlt = LZ4_count(ip+MINMATCH, match+MINMATCH, iLimit) + MINMATCH; if (mlt > ml) { ml = mlt; *matchpos = match; } } - } - else - { + } else { match = dictBase + matchIndex; - if (LZ4_read32(match) == LZ4_read32(ip)) - { + if (LZ4_read32(match) == LZ4_read32(ip)) { size_t mlt; const BYTE* vLimit = ip + (dictLimit - matchIndex); if (vLimit > iLimit) vLimit = iLimit; @@ -234,38 +228,32 @@ FORCE_INLINE int LZ4HC_InsertAndGetWiderMatch ( LZ4HC_Insert(hc4, ip); matchIndex = HashTable[LZ4HC_hashPtr(ip)]; - while ((matchIndex>=lowLimit) && (nbAttempts)) - { + while ((matchIndex>=lowLimit) && (nbAttempts)) { nbAttempts--; - if (matchIndex >= dictLimit) - { + if (matchIndex >= dictLimit) { const BYTE* matchPtr = base + matchIndex; - if (*(iLowLimit + longest) == *(matchPtr - delta + longest)) - if (LZ4_read32(matchPtr) == LZ4_read32(ip)) - { + if (*(iLowLimit + longest) == *(matchPtr - delta + longest)) { + if (LZ4_read32(matchPtr) == LZ4_read32(ip)) { int mlt = MINMATCH + LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, iHighLimit); int back = 0; - while ((ip+back>iLowLimit) + while ((ip+back > iLowLimit) && (matchPtr+back > lowPrefixPtr) && (ip[back-1] == matchPtr[back-1])) back--; mlt -= back; - if (mlt > longest) - { + if (mlt > longest) { longest = (int)mlt; *matchpos = matchPtr+back; *startpos = ip+back; } } - } - else - { + } + } else { const BYTE* matchPtr = dictBase + matchIndex; - if (LZ4_read32(matchPtr) == LZ4_read32(ip)) - { + if (LZ4_read32(matchPtr) == LZ4_read32(ip)) { size_t mlt; int back=0; const BYTE* vLimit = ip + (dictLimit - matchIndex); @@ -358,15 +346,14 @@ static int LZ4HC_compress_generic ( unsigned maxNbAttempts; int ml, ml2, ml3, ml0; - const BYTE* ref=NULL; - const BYTE* start2=NULL; - const BYTE* ref2=NULL; - const BYTE* start3=NULL; - const BYTE* ref3=NULL; + const BYTE* ref = NULL; + const BYTE* start2 = NULL; + const BYTE* ref2 = NULL; + const BYTE* start3 = NULL; + const BYTE* ref3 = NULL; const BYTE* start0; const BYTE* ref0; - /* init */ if (compressionLevel > LZ4HC_MAX_CLEVEL) compressionLevel = LZ4HC_MAX_CLEVEL; if (compressionLevel < 1) compressionLevel = LZ4HC_DEFAULT_CLEVEL; @@ -376,8 +363,7 @@ static int LZ4HC_compress_generic ( ip++; /* Main Loop */ - while (ip < mflimit) - { + while (ip < mflimit) { ml = LZ4HC_InsertAndFindBestMatch (ctx, ip, matchlimit, (&ref), maxNbAttempts); if (!ml) { ip++; continue; } @@ -391,16 +377,13 @@ _Search2: ml2 = LZ4HC_InsertAndGetWiderMatch(ctx, ip + ml - 2, ip + 1, matchlimit, ml, &ref2, &start2, maxNbAttempts); else ml2 = ml; - if (ml2 == ml) /* No better match */ - { + if (ml2 == ml) { /* No better match */ if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; continue; } - if (start0 < ip) - { - if (start2 < ip + ml0) /* empirical */ - { + if (start0 < ip) { + if (start2 < ip + ml0) { /* empirical */ ip = start0; ref = ref0; ml = ml0; @@ -408,8 +391,7 @@ _Search2: } /* Here, start0==ip */ - if ((start2 - ip) < 3) /* First Match too small : removed */ - { + if ((start2 - ip) < 3) { /* First Match too small : removed */ ml = ml2; ip = start2; ref =ref2; @@ -422,15 +404,13 @@ _Search3: * ml2 > ml1, and * ip1+3 <= ip2 (usually < ip1+ml1) */ - if ((start2 - ip) < OPTIMAL_ML) - { + if ((start2 - ip) < OPTIMAL_ML) { int correction; int new_ml = ml; if (new_ml > OPTIMAL_ML) new_ml = OPTIMAL_ML; if (ip+new_ml > start2 + ml2 - MINMATCH) new_ml = (int)(start2 - ip) + ml2 - MINMATCH; correction = new_ml - (int)(start2 - ip); - if (correction > 0) - { + if (correction > 0) { start2 += correction; ref2 += correction; ml2 -= correction; @@ -442,8 +422,7 @@ _Search3: ml3 = LZ4HC_InsertAndGetWiderMatch(ctx, start2 + ml2 - 3, start2, matchlimit, ml2, &ref3, &start3, maxNbAttempts); else ml3 = ml2; - if (ml3 == ml2) /* No better match : 2 sequences to encode */ - { + if (ml3 == ml2) { /* No better match : 2 sequences to encode */ /* ip & ref are known; Now for ml */ if (start2 < ip+ml) ml = (int)(start2 - ip); /* Now, encode 2 sequences */ @@ -453,18 +432,14 @@ _Search3: continue; } - if (start3 < ip+ml+3) /* Not enough space for match 2 : remove it */ - { - if (start3 >= (ip+ml)) /* can write Seq1 immediately ==> Seq2 is removed, so Seq3 becomes Seq1 */ - { - if (start2 < ip+ml) - { + if (start3 < ip+ml+3) { /* Not enough space for match 2 : remove it */ + if (start3 >= (ip+ml)) { /* can write Seq1 immediately ==> Seq2 is removed, so Seq3 becomes Seq1 */ + if (start2 < ip+ml) { int correction = (int)(ip+ml - start2); start2 += correction; ref2 += correction; ml2 -= correction; - if (ml2 < MINMATCH) - { + if (ml2 < MINMATCH) { start2 = start3; ref2 = ref3; ml2 = ml3; @@ -492,23 +467,18 @@ _Search3: * OK, now we have 3 ascending matches; let's write at least the first one * ip & ref are known; Now for ml */ - if (start2 < ip+ml) - { - if ((start2 - ip) < (int)ML_MASK) - { + if (start2 < ip+ml) { + if ((start2 - ip) < (int)ML_MASK) { int correction; if (ml > OPTIMAL_ML) ml = OPTIMAL_ML; if (ip + ml > start2 + ml2 - MINMATCH) ml = (int)(start2 - ip) + ml2 - MINMATCH; correction = ml - (int)(start2 - ip); - if (correction > 0) - { + if (correction > 0) { start2 += correction; ref2 += correction; ml2 -= correction; } - } - else - { + } else { ml = (int)(start2 - ip); } } @@ -526,8 +496,7 @@ _Search3: } /* Encode Last Literals */ - { - int lastRun = (int)(iend - anchor); + { int lastRun = (int)(iend - anchor); if ((limit) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) return 0; /* Check output limit */ if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } else *op++ = (BYTE)(lastRun< 64 KB) - { + if (dictSize > 64 KB) { dictionary += dictSize - 64 KB; dictSize = 64 KB; } @@ -604,8 +572,7 @@ int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int static void LZ4HC_setExternalDict(LZ4HC_Data_Structure* ctxPtr, const BYTE* newBlock) { - if (ctxPtr->end >= ctxPtr->base + 4) - LZ4HC_Insert (ctxPtr, ctxPtr->end-3); /* Referencing remaining dictionary content */ + if (ctxPtr->end >= ctxPtr->base + 4) LZ4HC_Insert (ctxPtr, ctxPtr->end-3); /* Referencing remaining dictionary content */ /* Only one memory segment for extDict, so any previous extDict is lost at this stage */ ctxPtr->lowLimit = ctxPtr->dictLimit; ctxPtr->dictLimit = (U32)(ctxPtr->end - ctxPtr->base); @@ -620,29 +587,23 @@ static int LZ4_compressHC_continue_generic (LZ4HC_Data_Structure* ctxPtr, int inputSize, int maxOutputSize, limitedOutput_directive limit) { /* auto-init if forgotten */ - if (ctxPtr->base == NULL) - LZ4HC_init (ctxPtr, (const BYTE*) source); + if (ctxPtr->base == NULL) LZ4HC_init (ctxPtr, (const BYTE*) source); /* Check overflow */ - if ((size_t)(ctxPtr->end - ctxPtr->base) > 2 GB) - { + if ((size_t)(ctxPtr->end - ctxPtr->base) > 2 GB) { size_t dictSize = (size_t)(ctxPtr->end - ctxPtr->base) - ctxPtr->dictLimit; if (dictSize > 64 KB) dictSize = 64 KB; - LZ4_loadDictHC((LZ4_streamHC_t*)ctxPtr, (const char*)(ctxPtr->end) - dictSize, (int)dictSize); } /* Check if blocks follow each other */ - if ((const BYTE*)source != ctxPtr->end) - LZ4HC_setExternalDict(ctxPtr, (const BYTE*)source); + if ((const BYTE*)source != ctxPtr->end) LZ4HC_setExternalDict(ctxPtr, (const BYTE*)source); /* Check overlapping input/dictionary space */ - { - const BYTE* sourceEnd = (const BYTE*) source + inputSize; - const BYTE* dictBegin = ctxPtr->dictBase + ctxPtr->lowLimit; - const BYTE* dictEnd = ctxPtr->dictBase + ctxPtr->dictLimit; - if ((sourceEnd > dictBegin) && ((const BYTE*)source < dictEnd)) - { + { const BYTE* sourceEnd = (const BYTE*) source + inputSize; + const BYTE* const dictBegin = ctxPtr->dictBase + ctxPtr->lowLimit; + const BYTE* const dictEnd = ctxPtr->dictBase + ctxPtr->dictLimit; + if ((sourceEnd > dictBegin) && ((const BYTE*)source < dictEnd)) { if (sourceEnd > dictEnd) sourceEnd = dictEnd; ctxPtr->lowLimit = (U32)(sourceEnd - ctxPtr->dictBase); if (ctxPtr->dictLimit - ctxPtr->lowLimit < 4) ctxPtr->lowLimit = ctxPtr->dictLimit; @@ -665,14 +626,13 @@ int LZ4_compress_HC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* sourc int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictSize) { - LZ4HC_Data_Structure* streamPtr = (LZ4HC_Data_Structure*)LZ4_streamHCPtr; - int prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); + LZ4HC_Data_Structure* const streamPtr = (LZ4HC_Data_Structure*)LZ4_streamHCPtr; + int const prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); if (dictSize > 64 KB) dictSize = 64 KB; if (dictSize < 4) dictSize = 0; if (dictSize > prefixSize) dictSize = prefixSize; memmove(safeBuffer, streamPtr->end - dictSize, dictSize); - { - U32 endIndex = (U32)(streamPtr->end - streamPtr->base); + { U32 const endIndex = (U32)(streamPtr->end - streamPtr->base); streamPtr->end = (const BYTE*)safeBuffer + dictSize; streamPtr->base = streamPtr->end - endIndex; streamPtr->dictLimit = endIndex - dictSize; -- cgit v0.12 From 64f556e610b68e6781e05d2d6470ebf8b8dff45c Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Sep 2016 19:32:06 -0700 Subject: lz4cli version number from lz4.h --- .gitignore | 5 ++-- lib/lz4.h | 8 +++---- lib/lz4hc.c | 11 +++++++-- programs/bench.c | 6 ++--- programs/lz4cli.c | 69 ++++++++++++++++++++++--------------------------------- 5 files changed, 46 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index d1ce131..54cd8fa 100644 --- a/.gitignore +++ b/.gitignore @@ -18,9 +18,8 @@ *.app lz4 -# Visual solution files +# IDE / editors files *.suo *.user - -# Directories +.clang_complete _codelite/ diff --git a/lib/lz4.h b/lib/lz4.h index b8f9aeb..ba6d70b 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -58,10 +58,10 @@ extern "C" { #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) int LZ4_versionNumber (void); -#define LZ4_STR(str) #str -#define LZ4_XSTR(str) LZ4_STR(str) -#define LZ4_VERSION_STRING LZ4_XSTR(LZ4_VERSION_MAJOR) "." \ - LZ4_XSTR(LZ4_VERSION_MINOR) "." LZ4_XSTR(LZ4_VERSION_RELEASE) +#define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE +#define LZ4_QUOTE(str) #str +#define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str) +#define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) const char* LZ4_versionString (void); diff --git a/lib/lz4hc.c b/lib/lz4hc.c index c6f67cb..68afa98 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -313,8 +313,15 @@ FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode MatchLength */ length = (int)(matchLength-MINMATCH); if ((limitedOutputBuffer) && (*op + (length>>8) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ - if (length>=(int)ML_MASK) { *token+=ML_MASK; length-=ML_MASK; for(; length > 509 ; length-=510) { *(*op)++ = 255; *(*op)++ = 255; } if (length > 254) { length-=255; *(*op)++ = 255; } *(*op)++ = (BYTE)length; } - else *token += (BYTE)(length); + if (length>=(int)ML_MASK) { + *token += ML_MASK; + length -= ML_MASK; + for(; length > 509 ; length-=510) { *(*op)++ = 255; *(*op)++ = 255; } + if (length > 254) { length-=255; *(*op)++ = 255; } + *(*op)++ = (BYTE)length; + } else { + *token += (BYTE)(length); + } /* Prepare next loop */ *ip += matchLength; diff --git a/programs/bench.c b/programs/bench.c index 81118c0..5952f47 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -282,7 +282,7 @@ int BMK_benchLevel(const char** fileNamesTable, int nbFiles, int cLevel) /* Fill input buffer */ DISPLAY("Loading %s... \r", inFileName); - if (strlen(inFileName)>16) inFileName += strlen(inFileName)-16; /* can only display 16 characters */ + if (strlen(inFileName)>16) inFileName += strlen(inFileName)-16; /* can only display 16 characters */ readSize = fread(orig_buff, 1, benchedSize, inFile); fclose(inFile); @@ -398,11 +398,11 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel, int cLe if (cLevelLast > LZ4HC_MAX_CLEVEL) cLevelLast = LZ4HC_MAX_CLEVEL; if (cLevelLast < cLevel) cLevelLast = cLevel; - DISPLAY("Benchmarking levels from %d to %d\n", cLevel, cLevelLast); + if (cLevelLast > cLevel) DISPLAY("Benchmarking levels from %d to %d\n", cLevel, cLevelLast); for (i=cLevel; i<=cLevelLast; i++) { res = BMK_benchLevel(fileNamesTable, nbFiles, i); if (res != 0) break; } - + return res; } diff --git a/programs/lz4cli.c b/programs/lz4cli.c index a583481..3e09073 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -62,6 +62,8 @@ #include "bench.h" /* BMK_benchFile, BMK_SetNbIterations, BMK_SetBlocksize, BMK_SetPause */ #include "lz4io.h" /* LZ4IO_compressFilename, LZ4IO_decompressFilename, LZ4IO_compressMultipleFilenames */ #include "lz4hc.h" /* LZ4HC_DEFAULT_CLEVEL */ +#include "lz4.h" /* LZ4_VERSION_STRING */ + /**************************** * OS-specific Includes @@ -87,11 +89,8 @@ * Constants ******************************/ #define COMPRESSOR_NAME "LZ4 command line interface" -#ifndef LZ4_VERSION -# define LZ4_VERSION "r132" -#endif #define AUTHOR "Yann Collet" -#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s (%s) ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION, AUTHOR, __DATE__ +#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION_STRING, AUTHOR #define LZ4_EXTENSION ".lz4" #define LZ4CAT "lz4cat" #define UNLZ4 "unlz4" @@ -345,7 +344,6 @@ int main(int argc, const char** argv) if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE); return 0; } if (!strcmp(argument, "--keep")) { continue; } /* keep source file (default anyway; just for xz/lzma compatibility) */ - /* Short commands (note : aggregated short commands are allowed) */ if (argument[0]=='-') { /* '-' means stdin/stdout */ @@ -414,8 +412,7 @@ int main(int argc, const char** argv) /* Modify Block Properties */ case 'B': - while (argument[1]!=0) - { + while (argument[1]!=0) { int exitBlockProperties=0; switch(argument[1]) { @@ -451,13 +448,10 @@ int main(int argc, const char** argv) /* Modify Nb Iterations (benchmark only) */ case 'i': - { unsigned iters = 0; - while ((argument[1] >='0') && (argument[1] <='9')) - { - iters *= 10; - iters += argument[1] - '0'; - argument++; - } + { unsigned iters; + argument++; + iters = readU32FromChar(&argument); + argument--; BMK_setNbIterations(iters); } break; @@ -482,8 +476,7 @@ int main(int argc, const char** argv) if (!input_filename) { input_filename=argument; continue; } /* Second non-option arg in output_filename to preserve original cli logic. */ - if (!output_filename) - { + if (!output_filename) { output_filename=argument; if (!strcmp (output_filename, nullOutput)) output_filename = nulmark; continue; @@ -501,31 +494,26 @@ int main(int argc, const char** argv) if(!input_filename) { input_filename=stdinmark; } /* Check if input is defined as console; trigger an error in this case */ - if (!strcmp(input_filename, stdinmark) && IS_CONSOLE(stdin) ) - { + if (!strcmp(input_filename, stdinmark) && IS_CONSOLE(stdin) ) { DISPLAYLEVEL(1, "refusing to read from a console\n"); exit(1); } /* Check if benchmark is selected */ - if (bench) - { + if (bench) { int bmkResult = BMK_benchFiles(inFileNames, ifnIdx, cLevel, cLevelLast); free((void*)inFileNames); return bmkResult; } /* No output filename ==> try to select one automatically (when possible) */ - while (!output_filename) - { + while (!output_filename) { if (!IS_CONSOLE(stdout)) { output_filename=stdoutmark; break; } /* Default to stdout whenever possible (i.e. not a console) */ - if ((!decode) && !(forceCompress)) /* auto-determine compression or decompression, based on file extension */ - { + if ((!decode) && !(forceCompress)) { /* auto-determine compression or decompression, based on file extension */ size_t const l = strlen(input_filename); if (!strcmp(input_filename+(l-4), LZ4_EXTENSION)) decode=1; } - if (!decode) /* compression to file */ - { + if (!decode) { /* compression to file */ size_t const l = strlen(input_filename); dynNameSpace = (char*)calloc(1,l+5); if (dynNameSpace==NULL) exit(1); @@ -536,8 +524,7 @@ int main(int argc, const char** argv) break; } /* decompression to file (automatic name will work only if input filename has correct format extension) */ - { - size_t outl; + { size_t outl; size_t inl = strlen(input_filename); dynNameSpace = (char*)calloc(1,inl+1); strcpy(dynNameSpace, input_filename); @@ -563,21 +550,21 @@ int main(int argc, const char** argv) /* IO Stream/File */ LZ4IO_setNotificationLevel(displayLevel); if (decode) { - if (multiple_inputs) - operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION); - else - DEFAULT_DECOMPRESSOR(input_filename, output_filename); - } else { - /* compression is default action */ - if (legacy_format) { - DISPLAYLEVEL(3, "! Generating compressed LZ4 using Legacy format (deprecated) ! \n"); - LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel); - } else { if (multiple_inputs) - operationResult = LZ4IO_compressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION, cLevel); + operationResult = LZ4IO_decompressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION); else - DEFAULT_COMPRESSOR(input_filename, output_filename, cLevel); - } + DEFAULT_DECOMPRESSOR(input_filename, output_filename); + } else { + /* compression is default action */ + if (legacy_format) { + DISPLAYLEVEL(3, "! Generating compressed LZ4 using Legacy format (deprecated) ! \n"); + LZ4IO_compressFilename_Legacy(input_filename, output_filename, cLevel); + } else { + if (multiple_inputs) + operationResult = LZ4IO_compressMultipleFilenames(inFileNames, ifnIdx, LZ4_EXTENSION, cLevel); + else + DEFAULT_COMPRESSOR(input_filename, output_filename, cLevel); + } } _cleanup: -- cgit v0.12 From 32a31663c3352884c9569438646a4fdd4141e975 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Sep 2016 19:48:24 -0700 Subject: fuzzer depends on standard C time.h only --- programs/fullbench.c | 121 --------------------------------------------------- programs/fuzzer.c | 72 ++++++++---------------------- programs/lz4cli.c | 2 +- 3 files changed, 20 insertions(+), 175 deletions(-) diff --git a/programs/fullbench.c b/programs/fullbench.c index 0d08a40..f1152a0 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -256,126 +256,6 @@ static U64 BMK_GetFileSize(char* infilename) /********************************************************* * Benchmark function *********************************************************/ -#ifdef __SSSE3__ - -#include - -/* Idea proposed by Terje Mathisen */ -static BYTE stepSize16[17] = {16,16,16,15,16,15,12,14,16,9,10,11,12,13,14,15,16}; -static __m128i replicateTable[17] = { - {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}, - {0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0}, - {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3}, - {0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0}, - {0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3}, - {0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1}, - {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7}, - {0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6}, - {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5}, - {0,1,2,3,4,5,6,7,8,9,10,0,1,2,3,4}, - {0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3}, - {0,1,2,3,4,5,6,7,8,9,10,11,12,0,1,2}, - {0,1,2,3,4,5,6,7,8,9,10,11,12,13,0,1}, - {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0}, - {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}}; -static BYTE stepSize32[17] = {32,32,32,30,32,30,30,28,32,27,30,22,24,26,28,30,16}; -static __m128i replicateTable2[17] = { - {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}, - {1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1}, - {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3}, - {1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1}, - {4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1}, - {2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3}, - {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7}, - {7,8,0,1,2,3,4,5,6,7,8,0,1,2,3,4}, - {6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1}, - {5,6,7,8,9,10,0,1,2,3,4,5,6,7,8,9}, - {4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7}, - {3,4,5,6,7,8,9,10,11,12,0,1,2,3,4,5}, - {2,3,4,5,6,7,8,9,10,11,12,13,0,1,2,3}, - {1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,1}, - {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}}; - -U32 lz4_decode_sse(BYTE* dest, BYTE* src, U32 srcLength) -{ - BYTE* d = dest, *e = src+srcLength; - unsigned token, lit_len, mat_len; - __m128i a; - BYTE* dstore, *msrc; - - if (!srcLength) return 0; - goto start; - - do { - U32 step; - unsigned mat_offset = src[0] + (src[1] << 8); - src += 2; - msrc = d - mat_offset; - if (mat_len == 15) { - do { - token = *src++; - mat_len += token; - } while (token == 255); - } - mat_len += 4; - - dstore = d; - d += mat_len; - - if (mat_offset <= 16) - { // Bulk store only! - __m128i a2; - a = _mm_loadu_si128((const __m128i *)msrc); - a2 = _mm_shuffle_epi8(a, replicateTable2[mat_offset]); - a = _mm_shuffle_epi8(a, replicateTable[mat_offset]); - step = stepSize32[mat_offset]; - do { - _mm_storeu_si128((__m128i *)dstore, a); - _mm_storeu_si128((__m128i *)(dstore+16), a2); - dstore += step; - } while (dstore < d); - } - else - { - do - { - a = _mm_loadu_si128((const __m128i *)msrc); - _mm_storeu_si128((__m128i *)dstore, a); - msrc += sizeof(a); - dstore += sizeof(a); - } while (dstore < d); - } -start: - token = *src++; - lit_len = token >> 4; - mat_len = token & 15; - if (token >= 0xf0) { // lit_len == 15 - do { - token = *src++; - lit_len += token; - } while (token == 255); - } - dstore = d; - msrc = src; - d += lit_len; - src += lit_len; - do { - a = _mm_loadu_si128((const __m128i *)msrc); - _mm_storeu_si128((__m128i *)dstore, a); - msrc += sizeof(a); - dstore += sizeof(a); - } while (dstore < d); - } while (src < e); - - return (U32)(d-dest); -} -#endif // __SSSE3__ - - static LZ4_stream_t LZ4_stream; static void local_LZ4_resetDictT(void) { @@ -1003,4 +883,3 @@ _exit_blockProperties: return fullSpeedBench(argv+filenamesStart, argc-filenamesStart); } - diff --git a/programs/fuzzer.c b/programs/fuzzer.c index 822e2ec..db7ad07 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -45,18 +45,12 @@ #include #include /* fgets, sscanf */ #include /* strcmp */ -#include "lz4.h" +#include /* clock_t, clock, CLOCKS_PER_SEC */ +#include "lz4.h" /* LZ4_VERSION_STRING */ #include "lz4hc.h" #define XXH_STATIC_LINKING_ONLY #include "xxhash.h" -/* Use ftime() if gettimeofday() is not available on your target */ -#if defined(FUZ_LEGACY_TIMER) -# include /* timeb, ftime */ -#else -# include /* gettimeofday */ -#endif - /*-************************************ * Basic Types @@ -80,10 +74,6 @@ typedef unsigned long long U64; /*-************************************ * Constants **************************************/ -#ifndef LZ4_VERSION -# define LZ4_VERSION "" -#endif - #define NB_ATTEMPTS (1<<16) #define COMPRESSIBLE_NOISE_LENGTH (1 << 21) #define FUZ_MAX_BLOCK_SIZE (1 << 17) @@ -104,45 +94,16 @@ typedef unsigned long long U64; #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } static int g_displayLevel = 2; -static const U32 g_refreshRate = 250; -static U32 g_time = 0; +static const clock_t g_refreshRate = CLOCKS_PER_SEC * 25 / 100; +static clock_t g_time = 0; /*-******************************************************* * Fuzzer functions *********************************************************/ -#if defined(FUZ_LEGACY_TIMER) - -static U32 FUZ_GetMilliStart(void) +static clock_t FUZ_GetClockSpan(clock_t clockStart) { - struct timeb tb; - U32 nCount; - ftime( &tb ); - nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); - return nCount; -} - -#else - -static U32 FUZ_GetMilliStart(void) -{ - struct timeval tv; - U32 nCount; - gettimeofday(&tv, NULL); - nCount = (U32) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); - return nCount; -} - -#endif - - -static U32 FUZ_GetMilliSpan(U32 nTimeStart) -{ - U32 nCurrent = FUZ_GetMilliStart(); - U32 nSpan = nCurrent - nTimeStart; - if (nTimeStart > nCurrent) - nSpan += 0x100000 * 1000; - return nSpan; + return clock() - clockStart; /* works even if overflow; max span ~ 30mn */ } static U32 FUZ_rotl32(U32 u32, U32 nbBits) @@ -291,8 +252,8 @@ _overflowError: static void FUZ_displayUpdate(unsigned testNb) { - if ((FUZ_GetMilliSpan(g_time) > g_refreshRate) | (g_displayLevel>=3)) { - g_time = FUZ_GetMilliStart(); + if ((FUZ_GetClockSpan(g_time) > g_refreshRate) | (g_displayLevel>=3)) { + g_time = clock(); DISPLAY("\r%5u ", testNb); if (g_displayLevel>=3) fflush(stdout); } @@ -315,7 +276,7 @@ static void FUZ_findDiff(const void* buff1, const void* buff2) } -static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double compressibility, U32 duration) +static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double compressibility, U32 duration_s) { unsigned long long bytes = 0; unsigned long long cbytes = 0; @@ -338,12 +299,12 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c U32 coreRandState = seed; U32 randState = coreRandState ^ PRIME3; int result = 0; - const U32 startTime = FUZ_GetMilliStart(); + clock_t const clockStart = clock(); + clock_t const clockDuration = (clock_t)duration_s * CLOCKS_PER_SEC; /* init */ memset(&LZ4dict, 0, sizeof(LZ4dict)); - duration *= 1000; /* Create compressible test buffer */ CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); @@ -376,7 +337,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c } } /* Main test loop */ - for (cycleNb = startCycle; (cycleNb < nbCycles) || (FUZ_GetMilliSpan(startTime) < duration) ; cycleNb++) { + for (cycleNb = startCycle; (cycleNb < nbCycles) || (FUZ_GetClockSpan(clockStart) < clockDuration) ; cycleNb++) { U32 testNb = 0; char* dict; char* block; @@ -1183,10 +1144,15 @@ int main(int argc, char** argv) } } - printf("Starting LZ4 fuzzer (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION); + printf("Starting LZ4 fuzzer (%i-bits, v%s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING); - if (!seedset) seed = FUZ_GetMilliStart() % 10000; + if (!seedset) { + time_t const t = time(NULL); + U32 const h = XXH32(&t, sizeof(t), 1); + seed = h % 10000; + } printf("Seed = %u\n", seed); + if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); if ((seedset==0) && (testNb==0)) FUZ_unitTests(); diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 3e09073..8b391f9 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -90,7 +90,7 @@ ******************************/ #define COMPRESSOR_NAME "LZ4 command line interface" #define AUTHOR "Yann Collet" -#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION_STRING, AUTHOR +#define WELCOME_MESSAGE "*** %s %i-bits v%s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), LZ4_VERSION_STRING, AUTHOR #define LZ4_EXTENSION ".lz4" #define LZ4CAT "lz4cat" #define UNLZ4 "unlz4" -- cgit v0.12 From f327a8bddd06b815073cf029d5407114ae1cba7b Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Sep 2016 20:17:19 -0700 Subject: fullbench depends on standard C time.h only --- programs/fullbench.c | 230 ++++++++++++++++++--------------------------------- 1 file changed, 79 insertions(+), 151 deletions(-) diff --git a/programs/fullbench.c b/programs/fullbench.c index f1152a0..ab6bba1 100644 --- a/programs/fullbench.c +++ b/programs/fullbench.c @@ -52,13 +52,7 @@ #include /* stat64 */ #include /* stat64 */ #include /* strcmp */ - -/* Use ftime() if gettimeofday() is not available on your target */ -#if defined(BMK_LEGACY_TIMER) -# include /* timeb, ftime */ -#else -# include /* gettimeofday */ -#endif +#include /* clock_t, clock(), CLOCKS_PER_SEC */ #include "lz4.h" #include "lz4hc.h" @@ -99,14 +93,11 @@ * Constants **************************************/ #define PROGRAM_DESCRIPTION "LZ4 speed analyzer" -#ifndef LZ4_VERSION -# define LZ4_VERSION "" -#endif #define AUTHOR "Yann Collet" -#define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, LZ4_VERSION, (int)(sizeof(void*)*8), AUTHOR, __DATE__ +#define WELCOME_MESSAGE "*** %s v%s %i-bits, by %s ***\n", PROGRAM_DESCRIPTION, LZ4_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR #define NBLOOPS 6 -#define TIMELOOP 2500 +#define TIMELOOP (CLOCKS_PER_SEC * 25 / 10) #define KB *(1 <<10) #define MB *(1 <<20) @@ -173,43 +164,9 @@ static void BMK_setPause(void) /********************************************************* * Private 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) -{ - /* 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 ) +static clock_t BMK_GetClockSpan( clock_t clockStart ) { - int nSpan = BMK_GetMilliStart() - nTimeStart; - if ( nSpan < 0 ) - nSpan += 0x100000 * 1000; - return nSpan; + return clock() - clockStart; /* works even if overflow; max span ~30 mn */ } @@ -222,8 +179,7 @@ static size_t BMK_findMaxMem(U64 requiredMem) requiredMem += 2*step; if (requiredMem > MAX_MEM) requiredMem = MAX_MEM; - while (!testmem) - { + while (!testmem) { if (requiredMem > step) requiredMem -= step; else requiredMem >>= 1; testmem = (BYTE*) malloc ((size_t)requiredMem); @@ -238,7 +194,7 @@ static size_t BMK_findMaxMem(U64 requiredMem) } -static U64 BMK_GetFileSize(char* infilename) +static U64 BMK_GetFileSize(const char* infilename) { int r; #if defined(_MSC_VER) @@ -445,23 +401,21 @@ static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outS #define NB_COMPRESSION_ALGORITHMS 100 #define NB_DECOMPRESSION_ALGORITHMS 100 -int fullSpeedBench(char** fileNamesTable, int nbFiles) +int fullSpeedBench(const char** fileNamesTable, int nbFiles) { - int fileIdx=0; - size_t errorCode; + int fileIdx=0; - /* Init */ - errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; } + /* Init */ + { size_t const errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; } } - /* Loop for each fileName */ - while (fileIdx inFileSize) benchedSize = (size_t)inFileSize; if (benchedSize < inFileSize) @@ -491,8 +444,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) maxCompressedChunkSize = LZ4_compressBound(g_chunkSize); compressedBuffSize = nbChunks * maxCompressedChunkSize; compressed_buff = (char*)malloc((size_t)compressedBuffSize); - if(!chunkP || !orig_buff || !compressed_buff) - { + if(!chunkP || !orig_buff || !compressed_buff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); free(orig_buff); @@ -506,8 +458,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) readSize = fread(orig_buff, 1, benchedSize, inFile); fclose(inFile); - if(readSize != benchedSize) - { + if (readSize != benchedSize) { DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); free(orig_buff); free(compressed_buff); @@ -520,8 +471,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) /* Bench */ - { - int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb; + { int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb; size_t cSize=0; double ratio=0.; @@ -529,8 +479,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) DISPLAY(" %s : \n", inFileName); /* Bench Compression Algorithms */ - for (cAlgNb=0; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++) - { + for (cAlgNb=0; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++) { const char* compressorName; int (*compressionFunction)(const char*, char*, int); void (*initFunction)(void) = NULL; @@ -540,20 +489,18 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) if ((g_compressionAlgo != ALL_COMPRESSORS) && (g_compressionAlgo != cAlgNb)) continue; /* Init data chunks */ - { - int i; - size_t remaining = benchedSize; - char* in = orig_buff; - char* out = compressed_buff; + { int i; + size_t remaining = benchedSize; + char* in = orig_buff; + char* out = compressed_buff; nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize); - for (i=0; i g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } - chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; - chunkP[i].compressedSize = 0; - } + for (i=0; i g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } + chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize; + chunkP[i].compressedSize = 0; + } } switch(cAlgNb) @@ -595,55 +542,50 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) continue; /* unknown ID : just skip */ } - for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) - { + for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) { double averageTime; - int milliTime; + clock_t clockTime; PROGRESS("%1i- %-28.28s :%9i ->\r", loopNb, compressorName, (int)benchedSize); { size_t i; for (i=0; i%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.); + PROGRESS("%1i- %-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000); } if (ratio<100.) - DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.); + DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000); else - DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.); + DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 100000); } /* Prepare layout for decompression */ /* Init data chunks */ - { - int i; + { int i; size_t remaining = benchedSize; char* in = orig_buff; char* out = compressed_buff; nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize); - for (i=0; i g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; } @@ -651,15 +593,13 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles) chunkP[i].compressedSize = 0; } } - for (chunkNb=0; chunkNb\r", loopNb, dName, (int)benchedSize); nb_loops = 0; - milliTime = BMK_GetMilliStart(); - while(BMK_GetMilliStart() == milliTime); - milliTime = BMK_GetMilliStart(); - while(BMK_GetMilliSpan(milliTime) < TIMELOOP) - { - for (chunkNb=0; chunkNb %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000.); + PROGRESS("%1i- %-29.29s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000); /* CRC Checking */ crcDecoded = XXH32(orig_buff, (int)benchedSize, 0); if (crcOriginal!=crcDecoded) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded); exit(1); } } - DISPLAY("%2i-%-29.29s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000.); + DISPLAY("%2i-%-29.29s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000); } } free(orig_buff); free(compressed_buff); free(chunkP); - } + } - LZ4F_freeDecompressionContext(g_dCtx); - if (g_pause) { printf("press enter...\n"); (void)getchar(); } + LZ4F_freeDecompressionContext(g_dCtx); + if (g_pause) { printf("press enter...\n"); (void)getchar(); } - return 0; + return 0; } -static int usage(char* exename) +static int usage(const char* exename) { DISPLAY( "Usage :\n"); DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename); @@ -765,41 +701,37 @@ static int usage_advanced(void) return 0; } -static int badusage(char* exename) +static int badusage(const char* exename) { DISPLAY("Wrong parameters\n"); usage(exename); return 0; } -int main(int argc, char** argv) +int main(int argc, const char** argv) { int i, filenamesStart=2; - char* exename=argv[0]; - char* input_filename=0; + const char* exename = argv[0]; + const char* input_filename=0; // Welcome message DISPLAY(WELCOME_MESSAGE); if (argc<2) { badusage(exename); return 1; } - for(i=1; i= '0') && (argument[1]<= '9')) - { + while ((argument[1]>= '0') && (argument[1]<= '9')) { g_compressionAlgo *= 10; g_compressionAlgo += argument[1] - '0'; argument++; @@ -818,8 +749,7 @@ int main(int argc, char** argv) // Select decompression algorithm only case 'd': g_compressionTest = 0; - while ((argument[1]>= '0') && (argument[1]<= '9')) - { + while ((argument[1]>= '0') && (argument[1]<= '9')) { g_decompressionAlgo *= 10; g_decompressionAlgo += argument[1] - '0'; argument++; @@ -839,8 +769,7 @@ int main(int argc, char** argv) case '5': case '6': case '7': - { - int B = argument[1] - '0'; + { int B = argument[1] - '0'; int S = 1 << (8 + 2*B); BMK_setBlocksize(S); argument++; @@ -854,8 +783,7 @@ _exit_blockProperties: // Modify Nb Iterations case 'i': - if ((argument[1] >='0') && (argument[1] <='9')) - { + if ((argument[1] >='0') && (argument[1] <='9')) { int iters = argument[1] - '0'; BMK_setNbIterations(iters); argument++; -- cgit v0.12 From dd51ca63004b4d373f90071d12fbe8c06ef0ae1a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Sep 2016 21:46:02 -0700 Subject: frametest depends on standard C time.h only --- programs/frametest.c | 79 +++++++++++++--------------------------------------- 1 file changed, 19 insertions(+), 60 deletions(-) diff --git a/programs/frametest.c b/programs/frametest.c index 1aa6bc7..d9f2566 100644 --- a/programs/frametest.c +++ b/programs/frametest.c @@ -31,11 +31,6 @@ # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ #endif -/* S_ISREG & gettimeofday() are not supported by MSVC */ -#if defined(_MSC_VER) || defined(_WIN32) -# define FUZ_LEGACY_TIMER 1 -#endif - /*-************************************ * Includes @@ -43,17 +38,12 @@ #include /* malloc, free */ #include /* fprintf */ #include /* strcmp */ +#include /* clock_t, clock(), CLOCKS_PER_SEC */ #include "lz4frame_static.h" +#include "lz4.h" /* LZ4_VERSION_STRING */ #define XXH_STATIC_LINKING_ONLY #include "xxhash.h" /* XXH64 */ -/* Use ftime() if gettimeofday() is not available on your target */ -#if defined(FUZ_LEGACY_TIMER) -# include /* timeb, ftime */ -#else -# include /* gettimeofday */ -#endif - /*-************************************ * Basic Types @@ -88,10 +78,6 @@ static void FUZ_writeLE32 (void* dstVoidPtr, U32 value32) /*-************************************ * Constants **************************************/ -#ifndef LZ4_VERSION -# define LZ4_VERSION "" -#endif - #define LZ4F_MAGIC_SKIPPABLE_START 0x184D2A50U #define KB *(1U<<10) @@ -112,11 +98,11 @@ static const U32 prime2 = 2246822519U; #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } #define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { \ - if ((FUZ_GetMilliSpan(g_time) > refreshRate) || (displayLevel>=4)) \ - { g_time = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ + if ((FUZ_GetClockSpan(g_clockTime) > refreshRate) || (displayLevel>=4)) \ + { g_clockTime = clock(); DISPLAY(__VA_ARGS__); \ if (displayLevel>=4) fflush(stdout); } } -static const U32 refreshRate = 150; -static U32 g_time = 0; +static const clock_t refreshRate = CLOCKS_PER_SEC / 6; +static clock_t g_clockTime = 0; /*-*************************************** @@ -131,42 +117,13 @@ static U32 pause = 0; /*-******************************************************* * Fuzzer functions *********************************************************/ -#if defined(FUZ_LEGACY_TIMER) - -static U32 FUZ_GetMilliStart(void) +static clock_t FUZ_GetClockSpan(clock_t clockStart) { - struct timeb tb; - U32 nCount; - ftime( &tb ); - nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); - return nCount; + return clock() - clockStart; /* works even if overflow; max span ~ 30 mn */ } -#else -static U32 FUZ_GetMilliStart(void) -{ - struct timeval tv; - U32 nCount; - gettimeofday(&tv, NULL); - nCount = (U32) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); - return nCount; -} - -#endif - - -static U32 FUZ_GetMilliSpan(U32 nTimeStart) -{ - U32 nCurrent = FUZ_GetMilliStart(); - U32 nSpan = nCurrent - nTimeStart; - if (nTimeStart > nCurrent) - nSpan += 0x100000 * 1000; - return nSpan; -} - - -# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) +#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) unsigned int FUZ_rand(unsigned int* src) { U32 rand32 = *src; @@ -586,7 +543,7 @@ static void locateBuffDiff(const void* buff1, const void* buff2, size_t size, un static const U32 srcDataLength = 9 MB; /* needs to be > 2x4MB to test large blocks */ -int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility, U32 duration) +int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility, U32 duration_s) { unsigned testResult = 0; unsigned testNb = 0; @@ -597,14 +554,12 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi LZ4F_decompressionContext_t dCtx = NULL; LZ4F_compressionContext_t cCtx = NULL; size_t result; - const U32 startTime = FUZ_GetMilliStart(); + clock_t const startClock = clock(); + clock_t const clockDuration = duration_s * CLOCKS_PER_SEC; XXH64_state_t xxh64; # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } - /* Init */ - duration *= 1000; - /* Create buffers */ result = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); CHECK(LZ4F_isError(result), "Allocation failed (error %i)", (int)result); @@ -622,7 +577,7 @@ int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressi for (testNb =0; (testNb < startTest); testNb++) (void)FUZ_rand(&coreRand); // sync randomizer /* main fuzzer test loop */ - for ( ; (testNb < nbTests) || (duration > FUZ_GetMilliSpan(startTime)) ; testNb++) { + for ( ; (testNb < nbTests) || (clockDuration > FUZ_GetClockSpan(startClock)) ; testNb++) { U32 randState = coreRand ^ prime1; unsigned BSId = 4 + (FUZ_rand(&randState) & 3); unsigned BMId = FUZ_rand(&randState) & 1; @@ -893,9 +848,13 @@ int main(int argc, char** argv) } /* Get Seed */ - printf("Starting lz4frame tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION); + printf("Starting lz4frame tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), LZ4_VERSION_STRING); - if (!seedset) seed = FUZ_GetMilliStart() % 10000; + if (!seedset) { + time_t const t = time(NULL); + U32 const h = XXH32(&t, sizeof(t), 1); + seed = h % 10000; + } printf("Seed = %u\n", seed); if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) printf("Compressibility : %i%%\n", proba); -- cgit v0.12 From 28f98647e3349a4a0a9c8975199aa89f2a02f6b9 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 2 Sep 2016 22:04:16 -0700 Subject: lz4 version source from lz4.h --- Makefile | 4 ---- lib/.gitignore | 2 ++ lib/Makefile | 23 ++++++++++++++--------- lib/liblz4.pc.in | 2 +- programs/Makefile | 8 +++----- 5 files changed, 20 insertions(+), 19 deletions(-) create mode 100644 lib/.gitignore diff --git a/Makefile b/Makefile index 079be73..d9fbb8c 100644 --- a/Makefile +++ b/Makefile @@ -30,10 +30,6 @@ # - LZ4 forum froup : https://groups.google.com/forum/#!forum/lz4c # ################################################################ -# Version number -export VERSION=132 -export RELEASE=r$(VERSION) - DESTDIR?= PREFIX ?= /usr/local diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..5d6f134 --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1,2 @@ +# make install artefact +liblz4.pc diff --git a/lib/Makefile b/lib/Makefile index 52e0f95..141da01 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -31,17 +31,21 @@ # ################################################################ # Version numbers -VERSION?= 132 -LIBVER_MAJOR:=`sed -n '/define LZ4_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lz4.h` -LIBVER_MINOR:=`sed -n '/define LZ4_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lz4.h` -LIBVER_PATCH:=`sed -n '/define LZ4_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lz4.h` -LIBVER := $(LIBVER_MAJOR).$(LIBVER_MINOR).$(LIBVER_PATCH) +LIBVER_MAJOR_SCRIPT:=`sed -n '/define LZ4_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h` +LIBVER_MINOR_SCRIPT:=`sed -n '/define LZ4_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h` +LIBVER_PATCH_SCRIPT:=`sed -n '/define LZ4_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./lz4.h` +LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT) +LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT)) +LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT)) +LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT)) +LIBVER := $(shell echo $(LIBVER_SCRIPT)) + DESTDIR?= PREFIX ?= /usr/local CPPFLAGS= -DXXH_NAMESPACE=LZ4_ CFLAGS ?= -O3 -CFLAGS += -std=gnu99 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Wcast-qual -Wstrict-prototypes -pedantic +CFLAGS += -Wall -Wextra -Wundef -Wshadow -Wcast-align -Wcast-qual -Wstrict-prototypes FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) LIBDIR?= $(PREFIX)/lib @@ -66,9 +70,10 @@ default: liblz4 all: liblz4 -liblz4: lz4.c lz4hc.c lz4frame.c xxhash.c # need to compile once with -fPIC, and once without -fPIC +liblz4: *.c @echo compiling static library - @$(AR) rcs $@.a $^ + @$(CC) $(FLAGS) -c $^ + @$(AR) rcs $@.a *.o @echo compiling dynamic library $(LIBVER) @$(CC) $(FLAGS) -shared $^ -fPIC $(SONAME_FLAGS) -o $@.$(SHARED_EXT_VER) @echo creating versioned links @@ -90,7 +95,7 @@ liblz4.pc: liblz4.pc.in Makefile @sed -e 's|@PREFIX@|$(PREFIX)|' \ -e 's|@LIBDIR@|$(LIBDIR)|' \ -e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \ - -e 's|@VERSION@|$(VERSION)|' \ + -e 's|@VERSION@|$(LIBVER)|' \ $< >$@ install: liblz4 liblz4.pc diff --git a/lib/liblz4.pc.in b/lib/liblz4.pc.in index 1b29879..cb31cd7 100644 --- a/lib/liblz4.pc.in +++ b/lib/liblz4.pc.in @@ -7,7 +7,7 @@ libdir=@LIBDIR@ includedir=@INCLUDEDIR@ Name: lz4 -Description: fast lossless compression algorithm library +Description: extremely fast lossless compression algorithm library URL: http://www.lz4.org/ Version: @VERSION@ Libs: -L@LIBDIR@ -llz4 diff --git a/programs/Makefile b/programs/Makefile index bab49f7..4d8b391 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -34,17 +34,15 @@ # datagen : generates synthetic data samples for tests & benchmarks # ########################################################################## -RELEASE ?= r132 - +DESTDIR ?= +PREFIX ?= /usr/local BINDIR := $(PREFIX)/bin MANDIR := $(PREFIX)/share/man/man1 LZ4DIR := ../lib -DESTDIR ?= -PREFIX ?= /usr/local CFLAGS ?= -O3 # can select custom flags. For example : CFLAGS="-O2 -g" make CFLAGS += -Wall -Wextra -Wundef -Wcast-qual -Wcast-align -Wshadow -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -CPPFLAGS:= -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_ -DLZ4_VERSION=\"$(RELEASE)\" +CPPFLAGS:= -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_ FLAGS := $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -- cgit v0.12