summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--Makefile4
-rw-r--r--lib/.gitignore2
-rw-r--r--lib/Makefile23
-rw-r--r--lib/liblz4.pc.in2
-rw-r--r--lib/lz4.h8
-rw-r--r--lib/lz4hc.c161
-rw-r--r--programs/Makefile8
-rw-r--r--programs/bench.c6
-rw-r--r--programs/frametest.c79
-rw-r--r--programs/fullbench.c351
-rw-r--r--programs/fuzzer.c72
-rw-r--r--programs/lz4cli.c69
13 files changed, 238 insertions, 552 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/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/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 8bcebc8..68afa98 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);
@@ -325,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;
@@ -358,15 +353,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 +370,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 +384,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 +398,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 +411,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 +429,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 +439,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 +474,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 +503,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<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
else *op++ = (BYTE)(lastRun<<ML_BITS);
@@ -588,8 +564,7 @@ void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel)
int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize)
{
LZ4HC_Data_Structure* ctxPtr = (LZ4HC_Data_Structure*) LZ4_streamHCPtr;
- if (dictSize > 64 KB)
- {
+ if (dictSize > 64 KB) {
dictionary += dictSize - 64 KB;
dictSize = 64 KB;
}
@@ -604,8 +579,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 +594,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 +633,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;
diff --git a/programs/Makefile b/programs/Makefile
index 372d7b0..290361f 100644
--- a/programs/Makefile
+++ b/programs/Makefile
@@ -34,18 +34,16 @@
# 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
CFLAGS += $(MOREFLAGS)
-CPPFLAGS:= -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_ -DLZ4_VERSION=\"$(RELEASE)\"
+CPPFLAGS:= -I$(LZ4DIR) -DXXH_NAMESPACE=LZ4_
FLAGS := $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
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/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 <stdlib.h> /* malloc, free */
#include <stdio.h> /* fprintf */
#include <string.h> /* strcmp */
+#include <time.h> /* 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 <sys/timeb.h> /* timeb, ftime */
-#else
-# include <sys/time.h> /* 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);
diff --git a/programs/fullbench.c b/programs/fullbench.c
index 0d08a40..ab6bba1 100644
--- a/programs/fullbench.c
+++ b/programs/fullbench.c
@@ -52,13 +52,7 @@
#include <sys/types.h> /* stat64 */
#include <sys/stat.h> /* stat64 */
#include <string.h> /* strcmp */
-
-/* Use ftime() if gettimeofday() is not available on your target */
-#if defined(BMK_LEGACY_TIMER)
-# include <sys/timeb.h> /* timeb, ftime */
-#else
-# include <sys/time.h> /* gettimeofday */
-#endif
+#include <time.h> /* 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)
+static clock_t BMK_GetClockSpan( clock_t clockStart )
{
- /* 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 )
-{
- 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)
@@ -256,126 +212,6 @@ static U64 BMK_GetFileSize(char* infilename)
/*********************************************************
* Benchmark function
*********************************************************/
-#ifdef __SSSE3__
-
-#include <tmmintrin.h>
-
-/* 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)
{
@@ -565,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<nbFiles)
- {
- FILE* inFile;
+ /* Loop for each fileName */
+ while (fileIdx<nbFiles) {
char* orig_buff = NULL;
struct chunkParameters* chunkP = NULL;
char* compressed_buff=NULL;
- char* inFileName;
+ const char* const inFileName = fileNamesTable[fileIdx++];
+ FILE* const inFile = fopen( inFileName, "rb" );
U64 inFileSize;
size_t benchedSize;
int nbChunks;
@@ -589,16 +423,15 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
size_t readSize;
int compressedBuffSize;
U32 crcOriginal;
+ size_t errorCode;
/* Check file existence */
- inFileName = fileNamesTable[fileIdx++];
- inFile = fopen( inFileName, "rb" );
if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
/* Memory size adjustments */
inFileSize = BMK_GetFileSize(inFileName);
if (inFileSize==0) { DISPLAY( "file is empty\n"); fclose(inFile); return 11; }
- benchedSize = (size_t) BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */
+ benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */
if (benchedSize==0) { DISPLAY( "not enough memory\n"); fclose(inFile); return 11; }
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
if (benchedSize < inFileSize)
@@ -611,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);
@@ -626,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);
@@ -640,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.;
@@ -649,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;
@@ -660,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<nbChunks; i++)
- {
- chunkP[i].id = i;
- chunkP[i].origBuffer = in; in += g_chunkSize;
- if ((int)remaining > 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<nbChunks; i++) {
+ chunkP[i].id = i;
+ chunkP[i].origBuffer = in; in += g_chunkSize;
+ if ((int)remaining > 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)
@@ -715,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<benchedSize; i++) compressed_buff[i]=(char)i; } /* warming up memory */
nb_loops = 0;
- milliTime = BMK_GetMilliStart();
- while(BMK_GetMilliStart() == milliTime);
- milliTime = BMK_GetMilliStart();
- while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
- {
+ clockTime = clock();
+ while(clock() == clockTime);
+ clockTime = clock();
+ while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
if (initFunction!=NULL) initFunction();
- for (chunkNb=0; chunkNb<nbChunks; chunkNb++)
- {
+ for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
chunkP[chunkNb].compressedSize = compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
if (chunkP[chunkNb].compressedSize==0) DISPLAY("ERROR ! %s() = 0 !! \n", compressorName), exit(1);
}
nb_loops++;
}
- milliTime = BMK_GetMilliSpan(milliTime);
+ clockTime = BMK_GetClockSpan(clockTime);
nb_loops += !nb_loops; /* avoid division by zero */
- averageTime = (double)milliTime / nb_loops;
+ averageTime = ((double)clockTime) / nb_loops / CLOCKS_PER_SEC;
if (averageTime < bestTime) bestTime = averageTime;
cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += chunkP[chunkNb].compressedSize;
ratio = (double)cSize/(double)benchedSize*100.;
- PROGRESS("%1i- %-28.28s :%9i ->%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<nbChunks; i++)
- {
+ for (i=0; i<nbChunks; i++) {
chunkP[i].id = i;
chunkP[i].origBuffer = in; in += g_chunkSize;
if ((int)remaining > g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
@@ -771,15 +593,13 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
chunkP[i].compressedSize = 0;
}
}
- for (chunkNb=0; chunkNb<nbChunks; chunkNb++)
- {
+ for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
chunkP[chunkNb].compressedSize = LZ4_compress(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
if (chunkP[chunkNb].compressedSize==0) DISPLAY("ERROR ! %s() = 0 !! \n", "LZ4_compress"), exit(1);
}
/* Decompression Algorithms */
- for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && (g_decompressionTest); dAlgNb++)
- {
+ for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && (g_decompressionTest); dAlgNb++) {
const char* dName;
int (*decompressionFunction)(const char*, char*, int, int);
double bestTime = 100000000.;
@@ -797,8 +617,7 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break;
case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress";
errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL);
- if (LZ4F_isError(errorCode))
- {
+ if (LZ4F_isError(errorCode)) {
DISPLAY("Error while preparing compressed frame\n");
free(orig_buff);
free(compressed_buff);
@@ -815,56 +634,53 @@ int fullSpeedBench(char** fileNamesTable, int nbFiles)
{ size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; } /* zeroing source area, for CRC checking */
- for (loopNb = 1; loopNb <= g_nbIterations; loopNb++)
- {
+ for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
double averageTime;
- int milliTime;
+ clock_t clockTime;
U32 crcDecoded;
PROGRESS("%1i- %-29.29s :%10i ->\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<nbChunks; chunkNb++)
- {
+ clockTime = clock();
+ while(clock() == clockTime);
+ clockTime = clock();
+ while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
+ for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
int decodedSize = decompressionFunction(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedSize, chunkP[chunkNb].origSize);
if (chunkP[chunkNb].origSize != decodedSize) DISPLAY("ERROR ! %s() == %i != %i !! \n", dName, decodedSize, chunkP[chunkNb].origSize), exit(1);
}
nb_loops++;
}
- milliTime = BMK_GetMilliSpan(milliTime);
+ clockTime = BMK_GetClockSpan(clockTime);
nb_loops += !nb_loops; /* Avoid division by zero */
- averageTime = (double)milliTime / nb_loops;
+ averageTime = (double)clockTime / nb_loops / CLOCKS_PER_SEC;
if (averageTime < bestTime) bestTime = averageTime;
- PROGRESS("%1i- %-29.29s :%10i -> %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);
@@ -885,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<argc; i++)
- {
- char* argument = argv[i];
+ for(i=1; i<argc; i++) {
+ const char* argument = argv[i];
if(!argument) continue; // Protection if argument empty
- if (!strcmp(argument, "--no-prompt"))
- {
+ if (!strcmp(argument, "--no-prompt")) {
g_noPrompt = 1;
continue;
}
// Decode command (note : aggregated commands are allowed)
- if (argument[0]=='-')
- {
- while (argument[1]!=0)
- {
+ if (argument[0]=='-') {
+ while (argument[1]!=0) {
argument ++;
switch(argument[0])
@@ -927,8 +739,7 @@ int main(int argc, char** argv)
// Select compression algorithm only
case 'c':
g_decompressionTest = 0;
- while ((argument[1]>= '0') && (argument[1]<= '9'))
- {
+ while ((argument[1]>= '0') && (argument[1]<= '9')) {
g_compressionAlgo *= 10;
g_compressionAlgo += argument[1] - '0';
argument++;
@@ -938,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++;
@@ -959,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++;
@@ -974,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++;
@@ -1003,4 +811,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 <stdlib.h>
#include <stdio.h> /* fgets, sscanf */
#include <string.h> /* strcmp */
-#include "lz4.h"
+#include <time.h> /* 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 <sys/timeb.h> /* timeb, ftime */
-#else
-# include <sys/time.h> /* 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 3e952c6..961bea1 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
@@ -84,11 +86,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 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"
@@ -342,7 +341,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 */
@@ -411,8 +409,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])
{
@@ -448,13 +445,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;
@@ -479,8 +473,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;
@@ -498,31 +491,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);
@@ -533,8 +521,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);
@@ -560,21 +547,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: