kusano 7d535a
/*
kusano 7d535a
    bench.c - Demo program to benchmark open-source compression algorithm
kusano 7d535a
    Copyright (C) Yann Collet 2012-2015
kusano 7d535a
kusano 7d535a
    GPL v2 License
kusano 7d535a
kusano 7d535a
    This program is free software; you can redistribute it and/or modify
kusano 7d535a
    it under the terms of the GNU General Public License as published by
kusano 7d535a
    the Free Software Foundation; either version 2 of the License, or
kusano 7d535a
    (at your option) any later version.
kusano 7d535a
kusano 7d535a
    This program is distributed in the hope that it will be useful,
kusano 7d535a
    but WITHOUT ANY WARRANTY; without even the implied warranty of
kusano 7d535a
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
kusano 7d535a
    GNU General Public License for more details.
kusano 7d535a
kusano 7d535a
    You should have received a copy of the GNU General Public License along
kusano 7d535a
    with this program; if not, write to the Free Software Foundation, Inc.,
kusano 7d535a
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
kusano 7d535a
kusano 7d535a
    You can contact the author at :
kusano 7d535a
    - LZ4 source repository : https://github.com/Cyan4973/lz4
kusano 7d535a
    - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
kusano 7d535a
*/
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Compiler Options
kusano 7d535a
**************************************/
kusano 7d535a
/* Disable some Visual warning messages */
kusano 7d535a
#define _CRT_SECURE_NO_WARNINGS
kusano 7d535a
#define _CRT_SECURE_NO_DEPRECATE     /* VS2005 */
kusano 7d535a
kusano 7d535a
/* Unix Large Files support (>4GB) */
kusano 7d535a
#if (defined(__sun__) && (!defined(__LP64__)))   // Sun Solaris 32-bits requires specific definitions
kusano 7d535a
#  define _LARGEFILE_SOURCE
kusano 7d535a
#  define _FILE_OFFSET_BITS 64
kusano 7d535a
#elif ! defined(__LP64__)                        // No point defining Large file for 64 bit
kusano 7d535a
#  define _LARGEFILE64_SOURCE
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
// S_ISREG & gettimeofday() are not supported by MSVC
kusano 7d535a
#if defined(_MSC_VER) || defined(_WIN32)
kusano 7d535a
#  define BMK_LEGACY_TIMER 1
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Includes
kusano 7d535a
**************************************/
kusano 7d535a
#include <stdlib.h>      /* malloc, free */</stdlib.h>
kusano 7d535a
#include <stdio.h>       /* fprintf, fopen, ftello64 */</stdio.h>
kusano 7d535a
#include <sys types.h="">   /* stat64 */</sys>
kusano 7d535a
#include <sys stat.h="">    /* stat64 */</sys>
kusano 7d535a
#include <string.h>      /* strcmp */</string.h>
kusano 7d535a
kusano 7d535a
/* Use ftime() if gettimeofday() is not available on your target */
kusano 7d535a
#if defined(BMK_LEGACY_TIMER)
kusano 7d535a
#  include <sys timeb.h="">   /* timeb, ftime */</sys>
kusano 7d535a
#else
kusano 7d535a
#  include <sys time.h="">    /* gettimeofday */</sys>
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include "lz4.h"
kusano 7d535a
#include "lz4hc.h"
kusano 7d535a
#include "lz4frame.h"
kusano 7d535a
kusano 7d535a
#include "xxhash.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Compiler Options
kusano 7d535a
**************************************/
kusano 7d535a
/* S_ISREG & gettimeofday() are not supported by MSVC */
kusano 7d535a
#if !defined(S_ISREG)
kusano 7d535a
#  define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Basic Types
kusano 7d535a
**************************************/
kusano 7d535a
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
kusano 7d535a
# include <stdint.h></stdint.h>
kusano 7d535a
  typedef uint8_t  BYTE;
kusano 7d535a
  typedef uint16_t U16;
kusano 7d535a
  typedef uint32_t U32;
kusano 7d535a
  typedef  int32_t S32;
kusano 7d535a
  typedef uint64_t U64;
kusano 7d535a
#else
kusano 7d535a
  typedef unsigned char       BYTE;
kusano 7d535a
  typedef unsigned short      U16;
kusano 7d535a
  typedef unsigned int        U32;
kusano 7d535a
  typedef   signed int        S32;
kusano 7d535a
  typedef unsigned long long  U64;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Constants
kusano 7d535a
**************************************/
kusano 7d535a
#define PROGRAM_DESCRIPTION "LZ4 speed analyzer"
kusano 7d535a
#ifndef LZ4_VERSION
kusano 7d535a
#  define LZ4_VERSION ""
kusano 7d535a
#endif
kusano 7d535a
#define AUTHOR "Yann Collet"
kusano 7d535a
#define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, LZ4_VERSION, (int)(sizeof(void*)*8), AUTHOR, __DATE__
kusano 7d535a
kusano 7d535a
#define NBLOOPS    6
kusano 7d535a
#define TIMELOOP   2500
kusano 7d535a
kusano 7d535a
#define KB *(1 <<10)
kusano 7d535a
#define MB *(1 <<20)
kusano 7d535a
#define GB *(1U<<30)
kusano 7d535a
kusano 7d535a
#define KNUTH      2654435761U
kusano 7d535a
#define MAX_MEM    (1920 MB)
kusano 7d535a
#define DEFAULT_CHUNKSIZE   (4 MB)
kusano 7d535a
kusano 7d535a
#define ALL_COMPRESSORS 0
kusano 7d535a
#define ALL_DECOMPRESSORS 0
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Local structures
kusano 7d535a
**************************************/
kusano 7d535a
struct chunkParameters
kusano 7d535a
{
kusano 7d535a
    U32   id;
kusano 7d535a
    char* origBuffer;
kusano 7d535a
    char* compressedBuffer;
kusano 7d535a
    int   origSize;
kusano 7d535a
    int   compressedSize;
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Macros
kusano 7d535a
**************************************/
kusano 7d535a
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
kusano 7d535a
#define PROGRESS(...) g_noPrompt ? 0 : DISPLAY(__VA_ARGS__)
kusano 7d535a
kusano 7d535a
kusano 7d535a
/**************************************
kusano 7d535a
*  Benchmark Parameters
kusano 7d535a
**************************************/
kusano 7d535a
static int g_chunkSize = DEFAULT_CHUNKSIZE;
kusano 7d535a
static int g_nbIterations = NBLOOPS;
kusano 7d535a
static int g_pause = 0;
kusano 7d535a
static int g_compressionTest = 1;
kusano 7d535a
static int g_compressionAlgo = ALL_COMPRESSORS;
kusano 7d535a
static int g_decompressionTest = 1;
kusano 7d535a
static int g_decompressionAlgo = ALL_DECOMPRESSORS;
kusano 7d535a
static int g_noPrompt = 0;
kusano 7d535a
kusano 7d535a
static void BMK_setBlocksize(int bsize)
kusano 7d535a
{
kusano 7d535a
    g_chunkSize = bsize;
kusano 7d535a
    DISPLAY("-Using Block Size of %i KB-\n", g_chunkSize>>10);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void BMK_setNbIterations(int nbLoops)
kusano 7d535a
{
kusano 7d535a
    g_nbIterations = nbLoops;
kusano 7d535a
    DISPLAY("- %i iterations -\n", g_nbIterations);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void BMK_setPause(void)
kusano 7d535a
{
kusano 7d535a
    g_pause = 1;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*********************************************************
kusano 7d535a
*  Private functions
kusano 7d535a
*********************************************************/
kusano 7d535a
kusano 7d535a
#if defined(BMK_LEGACY_TIMER)
kusano 7d535a
kusano 7d535a
static int BMK_GetMilliStart(void)
kusano 7d535a
{
kusano 7d535a
  /* Based on Legacy ftime()
kusano 7d535a
   * Rolls over every ~ 12.1 days (0x100000/24/60/60)
kusano 7d535a
   * Use GetMilliSpan to correct for rollover */
kusano 7d535a
  struct timeb tb;
kusano 7d535a
  int nCount;
kusano 7d535a
  ftime( &tb );
kusano 7d535a
  nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000);
kusano 7d535a
  return nCount;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#else
kusano 7d535a
kusano 7d535a
static int BMK_GetMilliStart(void)
kusano 7d535a
{
kusano 7d535a
  /* Based on newer gettimeofday()
kusano 7d535a
   * Use GetMilliSpan to correct for rollover */
kusano 7d535a
  struct timeval tv;
kusano 7d535a
  int nCount;
kusano 7d535a
  gettimeofday(&tv, NULL);
kusano 7d535a
  nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000);
kusano 7d535a
  return nCount;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
static int BMK_GetMilliSpan( int nTimeStart )
kusano 7d535a
{
kusano 7d535a
  int nSpan = BMK_GetMilliStart() - nTimeStart;
kusano 7d535a
  if ( nSpan < 0 )
kusano 7d535a
    nSpan += 0x100000 * 1000;
kusano 7d535a
  return nSpan;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static size_t BMK_findMaxMem(U64 requiredMem)
kusano 7d535a
{
kusano 7d535a
    size_t step = 64 MB;
kusano 7d535a
    BYTE* testmem=NULL;
kusano 7d535a
kusano 7d535a
    requiredMem = (((requiredMem >> 26) + 1) << 26);
kusano 7d535a
    requiredMem += 2*step;
kusano 7d535a
    if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
kusano 7d535a
kusano 7d535a
    while (!testmem)
kusano 7d535a
    {
kusano 7d535a
        if (requiredMem > step) requiredMem -= step;
kusano 7d535a
        else requiredMem >>= 1;
kusano 7d535a
        testmem = (BYTE*) malloc ((size_t)requiredMem);
kusano 7d535a
    }
kusano 7d535a
    free (testmem);
kusano 7d535a
kusano 7d535a
    /* keep some space available */
kusano 7d535a
    if (requiredMem > step) requiredMem -= step;
kusano 7d535a
    else requiredMem >>= 1;
kusano 7d535a
kusano 7d535a
    return (size_t)requiredMem;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static U64 BMK_GetFileSize(char* infilename)
kusano 7d535a
{
kusano 7d535a
    int r;
kusano 7d535a
#if defined(_MSC_VER)
kusano 7d535a
    struct _stat64 statbuf;
kusano 7d535a
    r = _stat64(infilename, &statbuf);
kusano 7d535a
#else
kusano 7d535a
    struct stat statbuf;
kusano 7d535a
    r = stat(infilename, &statbuf);
kusano 7d535a
#endif
kusano 7d535a
    if (r || !S_ISREG(statbuf.st_mode)) return 0;   /* No good... */
kusano 7d535a
    return (U64)statbuf.st_size;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*********************************************************
kusano 7d535a
*  Benchmark function
kusano 7d535a
*********************************************************/
kusano 7d535a
#ifdef __SSSE3__
kusano 7d535a
kusano 7d535a
#include <tmmintrin.h></tmmintrin.h>
kusano 7d535a
kusano 7d535a
/* Idea proposed by Terje Mathisen */
kusano 7d535a
static BYTE stepSize16[17] = {16,16,16,15,16,15,12,14,16,9,10,11,12,13,14,15,16};
kusano 7d535a
static __m128i replicateTable[17] = {
kusano 7d535a
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
kusano 7d535a
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
kusano 7d535a
    {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
kusano 7d535a
    {0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0},
kusano 7d535a
    {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3},
kusano 7d535a
    {0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0},
kusano 7d535a
    {0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3},
kusano 7d535a
    {0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1},
kusano 7d535a
    {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,10,0,1,2,3,4},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,10,11,12,0,1,2},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,0,1},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}};
kusano 7d535a
static BYTE stepSize32[17] = {32,32,32,30,32,30,30,28,32,27,30,22,24,26,28,30,16};
kusano 7d535a
static __m128i replicateTable2[17] = {
kusano 7d535a
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
kusano 7d535a
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
kusano 7d535a
    {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
kusano 7d535a
    {1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1},
kusano 7d535a
    {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3},
kusano 7d535a
    {1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1},
kusano 7d535a
    {4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1},
kusano 7d535a
    {2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3},
kusano 7d535a
    {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7},
kusano 7d535a
    {7,8,0,1,2,3,4,5,6,7,8,0,1,2,3,4},
kusano 7d535a
    {6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1},
kusano 7d535a
    {5,6,7,8,9,10,0,1,2,3,4,5,6,7,8,9},
kusano 7d535a
    {4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7},
kusano 7d535a
    {3,4,5,6,7,8,9,10,11,12,0,1,2,3,4,5},
kusano 7d535a
    {2,3,4,5,6,7,8,9,10,11,12,13,0,1,2,3},
kusano 7d535a
    {1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,1},
kusano 7d535a
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}};
kusano 7d535a
kusano 7d535a
U32 lz4_decode_sse(BYTE* dest, BYTE* src, U32 srcLength)
kusano 7d535a
{
kusano 7d535a
    BYTE* d = dest, *e = src+srcLength;
kusano 7d535a
    unsigned token, lit_len, mat_len;
kusano 7d535a
    __m128i a;
kusano 7d535a
    BYTE* dstore, *msrc;
kusano 7d535a
kusano 7d535a
    if (!srcLength) return 0;
kusano 7d535a
    goto start;
kusano 7d535a
kusano 7d535a
    do {
kusano 7d535a
        U32 step;
kusano 7d535a
        unsigned mat_offset = src[0] + (src[1] << 8);
kusano 7d535a
        src += 2;
kusano 7d535a
        msrc = d - mat_offset;
kusano 7d535a
        if (mat_len == 15) {
kusano 7d535a
            do {
kusano 7d535a
                token = *src++;
kusano 7d535a
                mat_len += token;
kusano 7d535a
            } while (token == 255);
kusano 7d535a
        }
kusano 7d535a
        mat_len += 4;
kusano 7d535a
kusano 7d535a
        dstore = d;
kusano 7d535a
        d += mat_len;
kusano 7d535a
kusano 7d535a
        if (mat_offset <= 16)
kusano 7d535a
        { // Bulk store only!
kusano 7d535a
            __m128i a2;
kusano 7d535a
            a = _mm_loadu_si128((const __m128i *)msrc);
kusano 7d535a
            a2 = _mm_shuffle_epi8(a, replicateTable2[mat_offset]);
kusano 7d535a
            a = _mm_shuffle_epi8(a, replicateTable[mat_offset]);
kusano 7d535a
            step = stepSize32[mat_offset];
kusano 7d535a
            do {
kusano 7d535a
                _mm_storeu_si128((__m128i *)dstore, a);
kusano 7d535a
                _mm_storeu_si128((__m128i *)(dstore+16), a2);
kusano 7d535a
                dstore += step;
kusano 7d535a
            } while (dstore < d);
kusano 7d535a
        }
kusano 7d535a
        else
kusano 7d535a
        {
kusano 7d535a
            do
kusano 7d535a
            {
kusano 7d535a
                a = _mm_loadu_si128((const __m128i *)msrc);
kusano 7d535a
                _mm_storeu_si128((__m128i *)dstore, a);
kusano 7d535a
                msrc += sizeof(a);
kusano 7d535a
                dstore += sizeof(a);
kusano 7d535a
            } while (dstore < d);
kusano 7d535a
        }
kusano 7d535a
start:
kusano 7d535a
        token = *src++;
kusano 7d535a
        lit_len = token >> 4;
kusano 7d535a
        mat_len = token & 15;
kusano 7d535a
        if (token >= 0xf0) { // lit_len == 15
kusano 7d535a
            do {
kusano 7d535a
                token = *src++;
kusano 7d535a
                lit_len += token;
kusano 7d535a
            } while (token == 255);
kusano 7d535a
        }
kusano 7d535a
        dstore = d;
kusano 7d535a
        msrc = src;
kusano 7d535a
        d += lit_len;
kusano 7d535a
        src += lit_len;
kusano 7d535a
        do {
kusano 7d535a
            a = _mm_loadu_si128((const __m128i *)msrc);
kusano 7d535a
            _mm_storeu_si128((__m128i *)dstore, a);
kusano 7d535a
            msrc += sizeof(a);
kusano 7d535a
            dstore += sizeof(a);
kusano 7d535a
        } while (dstore < d);
kusano 7d535a
    } while (src < e);
kusano 7d535a
kusano 7d535a
    return (U32)(d-dest);
kusano 7d535a
}
kusano 7d535a
#endif // __SSSE3__
kusano 7d535a
kusano 7d535a
kusano 7d535a
static LZ4_stream_t LZ4_stream;
kusano 7d535a
static void local_LZ4_resetDictT(void)
kusano 7d535a
{
kusano 7d535a
    LZ4_resetStream(&LZ4_stream);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void local_LZ4_createStream(void)
kusano 7d535a
{
kusano 7d535a
    LZ4_resetStream(&LZ4_stream);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_saveDict(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    (void)in;
kusano 7d535a
    return LZ4_saveDict(&LZ4_stream, out, inSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_limitedOutput(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_limitedOutput(in, out, inSize, LZ4_compressBound(inSize)-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_default_large(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize));
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_default_small(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize)-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_fast0(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_fast1(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_fast2(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 2);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_fast17(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 17);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_fast_extState0(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_fast_extState(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_fast_continue0(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_fast_continue(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_withState(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_withState(&LZ4_stream, in, out, inSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_limitedOutput_withState(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_limitedOutput_withState(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize)-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_continue(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_continue(&LZ4_stream, in, out, inSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_limitedOutput_continue(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_limitedOutput_continue(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize)-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/* declare hidden function */
kusano 7d535a
int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize);
kusano 7d535a
kusano 7d535a
static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compress_forceExtDict(&LZ4_stream, in, out, inSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* HC compression functions */
kusano 7d535a
LZ4_streamHC_t LZ4_streamHC;
kusano 7d535a
static void local_LZ4_resetStreamHC(void)
kusano 7d535a
{
kusano 7d535a
    LZ4_resetStreamHC(&LZ4_streamHC, 0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_saveDictHC(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    (void)in;
kusano 7d535a
    return LZ4_saveDictHC(&LZ4_streamHC, out, inSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compressHC_withStateHC(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compressHC_withStateHC(&LZ4_streamHC, in, out, inSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compressHC_limitedOutput_withStateHC(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compressHC_limitedOutput_withStateHC(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize)-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compressHC_limitedOutput(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compressHC_limitedOutput(in, out, inSize, LZ4_compressBound(inSize)-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compressHC_continue(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compressHC_continue(&LZ4_streamHC, in, out, inSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_compressHC_limitedOutput_continue(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_compressHC_limitedOutput_continue(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize)-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* decompression functions */
kusano 7d535a
static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize)
kusano 7d535a
{
kusano 7d535a
    (void)inSize;
kusano 7d535a
    LZ4_decompress_fast(in, out, outSize);
kusano 7d535a
    return outSize;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_decompress_fast_usingDict(const char* in, char* out, int inSize, int outSize)
kusano 7d535a
{
kusano 7d535a
    (void)inSize;
kusano 7d535a
    LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65536);
kusano 7d535a
    return outSize;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize)
kusano 7d535a
{
kusano 7d535a
    (void)inSize;
kusano 7d535a
    LZ4_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536);
kusano 7d535a
    return outSize;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const char* dict, int dictSize);
kusano 7d535a
kusano 7d535a
static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize)
kusano 7d535a
{
kusano 7d535a
    (void)inSize;
kusano 7d535a
    LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536);
kusano 7d535a
    return outSize;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSize, int outSize)
kusano 7d535a
{
kusano 7d535a
    return LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* frame functions */
kusano 7d535a
static int local_LZ4F_compressFrame(const char* in, char* out, int inSize)
kusano 7d535a
{
kusano 7d535a
    return (int)LZ4F_compressFrame(out, 2*inSize + 16, in, inSize, NULL);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static LZ4F_decompressionContext_t g_dCtx;
kusano 7d535a
kusano 7d535a
static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outSize)
kusano 7d535a
{
kusano 7d535a
    size_t srcSize = inSize;
kusano 7d535a
    size_t dstSize = outSize;
kusano 7d535a
    size_t result;
kusano 7d535a
    result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL);
kusano 7d535a
    if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame\n"); exit(8); }
kusano 7d535a
    if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect\n"); exit(9); }
kusano 7d535a
    return (int)dstSize;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
#define NB_COMPRESSION_ALGORITHMS 100
kusano 7d535a
#define NB_DECOMPRESSION_ALGORITHMS 100
kusano 7d535a
int fullSpeedBench(char** fileNamesTable, int nbFiles)
kusano 7d535a
{
kusano 7d535a
  int fileIdx=0;
kusano 7d535a
  size_t errorCode;
kusano 7d535a
kusano 7d535a
  /* Init */
kusano 7d535a
  errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION);
kusano 7d535a
  if (LZ4F_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; }
kusano 7d535a
kusano 7d535a
  /* Loop for each fileName */
kusano 7d535a
  while (fileIdx
kusano 7d535a
  {
kusano 7d535a
      FILE* inFile;
kusano 7d535a
      char* orig_buff = NULL;
kusano 7d535a
      struct chunkParameters* chunkP = NULL;
kusano 7d535a
      char* compressed_buff=NULL;
kusano 7d535a
      char* inFileName;
kusano 7d535a
      U64   inFileSize;
kusano 7d535a
      size_t benchedSize;
kusano 7d535a
      int nbChunks;
kusano 7d535a
      int maxCompressedChunkSize;
kusano 7d535a
      size_t readSize;
kusano 7d535a
      int compressedBuffSize;
kusano 7d535a
      U32 crcOriginal;
kusano 7d535a
kusano 7d535a
      /* Check file existence */
kusano 7d535a
      inFileName = fileNamesTable[fileIdx++];
kusano 7d535a
      inFile = fopen( inFileName, "rb" );
kusano 7d535a
      if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
kusano 7d535a
kusano 7d535a
      /* Memory size adjustments */
kusano 7d535a
      inFileSize = BMK_GetFileSize(inFileName);
kusano 7d535a
      if (inFileSize==0) { DISPLAY( "file is empty\n"); fclose(inFile); return 11; }
kusano 7d535a
      benchedSize = (size_t) BMK_findMaxMem(inFileSize*2) / 2;   /* because 2 buffers */
kusano 7d535a
      if (benchedSize==0) { DISPLAY( "not enough memory\n"); fclose(inFile); return 11; }
kusano 7d535a
      if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
kusano 7d535a
      if (benchedSize < inFileSize)
kusano 7d535a
          DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20));
kusano 7d535a
kusano 7d535a
      /* Allocation */
kusano 7d535a
      chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)g_chunkSize)+1) * sizeof(struct chunkParameters));
kusano 7d535a
      orig_buff = (char*) malloc(benchedSize);
kusano 7d535a
      nbChunks = (int) ((benchedSize + (g_chunkSize-1)) / g_chunkSize);
kusano 7d535a
      maxCompressedChunkSize = LZ4_compressBound(g_chunkSize);
kusano 7d535a
      compressedBuffSize = nbChunks * maxCompressedChunkSize;
kusano 7d535a
      compressed_buff = (char*)malloc((size_t)compressedBuffSize);
kusano 7d535a
      if(!chunkP || !orig_buff || !compressed_buff)
kusano 7d535a
      {
kusano 7d535a
          DISPLAY("\nError: not enough memory!\n");
kusano 7d535a
          fclose(inFile);
kusano 7d535a
          free(orig_buff);
kusano 7d535a
          free(compressed_buff);
kusano 7d535a
          free(chunkP);
kusano 7d535a
          return(12);
kusano 7d535a
      }
kusano 7d535a
kusano 7d535a
      /* Fill in src buffer */
kusano 7d535a
      DISPLAY("Loading %s...       \r", inFileName);
kusano 7d535a
      readSize = fread(orig_buff, 1, benchedSize, inFile);
kusano 7d535a
      fclose(inFile);
kusano 7d535a
kusano 7d535a
      if(readSize != benchedSize)
kusano 7d535a
      {
kusano 7d535a
        DISPLAY("\nError: problem reading file '%s' !!    \n", inFileName);
kusano 7d535a
        free(orig_buff);
kusano 7d535a
        free(compressed_buff);
kusano 7d535a
        free(chunkP);
kusano 7d535a
        return 13;
kusano 7d535a
      }
kusano 7d535a
kusano 7d535a
      /* Calculating input Checksum */
kusano 7d535a
      crcOriginal = XXH32(orig_buff, benchedSize,0);
kusano 7d535a
kusano 7d535a
kusano 7d535a
      /* Bench */
kusano 7d535a
      {
kusano 7d535a
        int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb;
kusano 7d535a
        size_t cSize=0;
kusano 7d535a
        double ratio=0.;
kusano 7d535a
kusano 7d535a
        DISPLAY("\r%79s\r", "");
kusano 7d535a
        DISPLAY(" %s : \n", inFileName);
kusano 7d535a
kusano 7d535a
        /* Bench Compression Algorithms */
kusano 7d535a
        for (cAlgNb=0; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++)
kusano 7d535a
        {
kusano 7d535a
            const char* compressorName;
kusano 7d535a
            int (*compressionFunction)(const char*, char*, int);
kusano 7d535a
            void (*initFunction)(void) = NULL;
kusano 7d535a
            double bestTime = 100000000.;
kusano 7d535a
kusano 7d535a
            /* filter compressionAlgo only */
kusano 7d535a
            if ((g_compressionAlgo != ALL_COMPRESSORS) && (g_compressionAlgo != cAlgNb)) continue;
kusano 7d535a
kusano 7d535a
            /* Init data chunks */
kusano 7d535a
            {
kusano 7d535a
              int i;
kusano 7d535a
              size_t remaining = benchedSize;
kusano 7d535a
              char* in = orig_buff;
kusano 7d535a
              char* out = compressed_buff;
kusano 7d535a
                nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
kusano 7d535a
              for (i=0; i
kusano 7d535a
              {
kusano 7d535a
                  chunkP[i].id = i;
kusano 7d535a
                  chunkP[i].origBuffer = in; in += g_chunkSize;
kusano 7d535a
                  if ((int)remaining > g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
kusano 7d535a
                  chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
kusano 7d535a
                  chunkP[i].compressedSize = 0;
kusano 7d535a
              }
kusano 7d535a
            }
kusano 7d535a
kusano 7d535a
            switch(cAlgNb)
kusano 7d535a
            {
kusano 7d535a
            case 0 : DISPLAY("Compression functions : \n"); continue;
kusano 7d535a
            case 1 : compressionFunction = local_LZ4_compress_default_large; compressorName = "LZ4_compress_default"; break;
kusano 7d535a
            case 2 : compressionFunction = local_LZ4_compress_default_small; compressorName = "LZ4_compress_default(small dst)"; break;
kusano 7d535a
            case 3 : compressionFunction = local_LZ4_compress_fast0; compressorName = "LZ4_compress_fast(0)"; break;
kusano 7d535a
            case 4 : compressionFunction = local_LZ4_compress_fast1; compressorName = "LZ4_compress_fast(1)"; break;
kusano 7d535a
            case 5 : compressionFunction = local_LZ4_compress_fast2; compressorName = "LZ4_compress_fast(2)"; break;
kusano 7d535a
            case 6 : compressionFunction = local_LZ4_compress_fast17; compressorName = "LZ4_compress_fast(17)"; break;
kusano 7d535a
            case 7 : compressionFunction = local_LZ4_compress_fast_extState0; compressorName = "LZ4_compress_fast_extState(0)"; break;
kusano 7d535a
            case 8 : compressionFunction = local_LZ4_compress_fast_continue0; initFunction = local_LZ4_createStream; compressorName = "LZ4_compress_fast_continue(0)"; break;
kusano 7d535a
kusano 7d535a
            case 10: compressionFunction = LZ4_compressHC; compressorName = "LZ4_compressHC"; break;
kusano 7d535a
            case 11: compressionFunction = local_LZ4_compressHC_limitedOutput; compressorName = "LZ4_compressHC_limitedOutput"; break;
kusano 7d535a
            case 12 : compressionFunction = local_LZ4_compressHC_withStateHC; compressorName = "LZ4_compressHC_withStateHC"; break;
kusano 7d535a
            case 13: compressionFunction = local_LZ4_compressHC_limitedOutput_withStateHC; compressorName = "LZ4_compressHC_limitedOutput_withStateHC"; break;
kusano 7d535a
            case 14: compressionFunction = local_LZ4_compressHC_continue; initFunction = local_LZ4_resetStreamHC; compressorName = "LZ4_compressHC_continue"; break;
kusano 7d535a
            case 15: compressionFunction = local_LZ4_compressHC_limitedOutput_continue; initFunction = local_LZ4_resetStreamHC; compressorName = "LZ4_compressHC_limitedOutput_continue"; break;
kusano 7d535a
            case 20: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break;
kusano 7d535a
            case 30: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame";
kusano 7d535a
                        chunkP[0].origSize = (int)benchedSize; nbChunks=1;
kusano 7d535a
                        break;
kusano 7d535a
            case 40: compressionFunction = local_LZ4_saveDict; compressorName = "LZ4_saveDict";
kusano 7d535a
                        LZ4_loadDict(&LZ4_stream, chunkP[0].origBuffer, chunkP[0].origSize);
kusano 7d535a
                        break;
kusano 7d535a
            case 41: compressionFunction = local_LZ4_saveDictHC; compressorName = "LZ4_saveDictHC";
kusano 7d535a
                        LZ4_loadDictHC(&LZ4_streamHC, chunkP[0].origBuffer, chunkP[0].origSize);
kusano 7d535a
                        break;
kusano 7d535a
            case 60: DISPLAY("Obsolete compression functions : \n"); continue;
kusano 7d535a
            case 61: compressionFunction = LZ4_compress; compressorName = "LZ4_compress"; break;
kusano 7d535a
            case 62: compressionFunction = local_LZ4_compress_limitedOutput; compressorName = "LZ4_compress_limitedOutput"; break;
kusano 7d535a
            case 63: compressionFunction = local_LZ4_compress_withState; compressorName = "LZ4_compress_withState"; break;
kusano 7d535a
            case 64: compressionFunction = local_LZ4_compress_limitedOutput_withState; compressorName = "LZ4_compress_limitedOutput_withState"; break;
kusano 7d535a
            case 65: compressionFunction = local_LZ4_compress_continue; initFunction = local_LZ4_createStream; compressorName = "LZ4_compress_continue"; break;
kusano 7d535a
            case 66: compressionFunction = local_LZ4_compress_limitedOutput_continue; initFunction = local_LZ4_createStream; compressorName = "LZ4_compress_limitedOutput_continue"; break;
kusano 7d535a
            default :
kusano 7d535a
                continue;   /* unknown ID : just skip */
kusano 7d535a
            }
kusano 7d535a
kusano 7d535a
            for (loopNb = 1; loopNb <= g_nbIterations; loopNb++)
kusano 7d535a
            {
kusano 7d535a
                double averageTime;
kusano 7d535a
                int milliTime;
kusano 7d535a
kusano 7d535a
                PROGRESS("%1i- %-28.28s :%9i ->\r", loopNb, compressorName, (int)benchedSize);
kusano 7d535a
                { size_t i; for (i=0; i
kusano 7d535a
kusano 7d535a
                nb_loops = 0;
kusano 7d535a
                milliTime = BMK_GetMilliStart();
kusano 7d535a
                while(BMK_GetMilliStart() == milliTime);
kusano 7d535a
                milliTime = BMK_GetMilliStart();
kusano 7d535a
                while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
kusano 7d535a
                {
kusano 7d535a
                    if (initFunction!=NULL) initFunction();
kusano 7d535a
                    for (chunkNb=0; chunkNb
kusano 7d535a
                    {
kusano 7d535a
                        chunkP[chunkNb].compressedSize = compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
kusano 7d535a
                        if (chunkP[chunkNb].compressedSize==0) DISPLAY("ERROR ! %s() = 0 !! \n", compressorName), exit(1);
kusano 7d535a
                    }
kusano 7d535a
                    nb_loops++;
kusano 7d535a
                }
kusano 7d535a
                milliTime = BMK_GetMilliSpan(milliTime);
kusano 7d535a
kusano 7d535a
                nb_loops += !nb_loops;   /* avoid division by zero */
kusano 7d535a
                averageTime = (double)milliTime / nb_loops;
kusano 7d535a
                if (averageTime < bestTime) bestTime = averageTime;
kusano 7d535a
                cSize=0; for (chunkNb=0; chunkNb
kusano 7d535a
                ratio = (double)cSize/(double)benchedSize*100.;
kusano 7d535a
                PROGRESS("%1i- %-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.);
kusano 7d535a
            }
kusano 7d535a
kusano 7d535a
            if (ratio<100.)
kusano 7d535a
                DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.);
kusano 7d535a
            else
kusano 7d535a
                DISPLAY("%2i-%-28.28s :%9i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000.);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        /* Prepare layout for decompression */
kusano 7d535a
        /* Init data chunks */
kusano 7d535a
        {
kusano 7d535a
          int i;
kusano 7d535a
          size_t remaining = benchedSize;
kusano 7d535a
          char* in = orig_buff;
kusano 7d535a
          char* out = compressed_buff;
kusano 7d535a
kusano 7d535a
          nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
kusano 7d535a
          for (i=0; i
kusano 7d535a
          {
kusano 7d535a
              chunkP[i].id = i;
kusano 7d535a
              chunkP[i].origBuffer = in; in += g_chunkSize;
kusano 7d535a
              if ((int)remaining > g_chunkSize) { chunkP[i].origSize = g_chunkSize; remaining -= g_chunkSize; } else { chunkP[i].origSize = (int)remaining; remaining = 0; }
kusano 7d535a
              chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
kusano 7d535a
              chunkP[i].compressedSize = 0;
kusano 7d535a
          }
kusano 7d535a
        }
kusano 7d535a
        for (chunkNb=0; chunkNb
kusano 7d535a
        {
kusano 7d535a
            chunkP[chunkNb].compressedSize = LZ4_compress(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
kusano 7d535a
            if (chunkP[chunkNb].compressedSize==0) DISPLAY("ERROR ! %s() = 0 !! \n", "LZ4_compress"), exit(1);
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        /* Decompression Algorithms */
kusano 7d535a
        for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && (g_decompressionTest); dAlgNb++)
kusano 7d535a
        {
kusano 7d535a
            const char* dName;
kusano 7d535a
            int (*decompressionFunction)(const char*, char*, int, int);
kusano 7d535a
            double bestTime = 100000000.;
kusano 7d535a
kusano 7d535a
            if ((g_decompressionAlgo != ALL_DECOMPRESSORS) && (g_decompressionAlgo != dAlgNb)) continue;
kusano 7d535a
kusano 7d535a
            switch(dAlgNb)
kusano 7d535a
            {
kusano 7d535a
            case 0: DISPLAY("Decompression functions : \n"); continue;
kusano 7d535a
            case 1: decompressionFunction = local_LZ4_decompress_fast; dName = "LZ4_decompress_fast"; break;
kusano 7d535a
            case 3: decompressionFunction = local_LZ4_decompress_fast_usingDict; dName = "LZ4_decompress_fast_usingDict"; break;
kusano 7d535a
            case 4: decompressionFunction = LZ4_decompress_safe; dName = "LZ4_decompress_safe"; break;
kusano 7d535a
            case 6: decompressionFunction = local_LZ4_decompress_safe_usingDict; dName = "LZ4_decompress_safe_usingDict"; break;
kusano 7d535a
            case 7: decompressionFunction = local_LZ4_decompress_safe_partial; dName = "LZ4_decompress_safe_partial"; break;
kusano 7d535a
            case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break;
kusano 7d535a
            case 9: decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress";
kusano 7d535a
                    errorCode = LZ4F_compressFrame(compressed_buff, compressedBuffSize, orig_buff, benchedSize, NULL);
kusano 7d535a
                    if (LZ4F_isError(errorCode))
kusano 7d535a
                    {
kusano 7d535a
                        DISPLAY("Error while preparing compressed frame\n");
kusano 7d535a
                        free(orig_buff);
kusano 7d535a
                        free(compressed_buff);
kusano 7d535a
                        free(chunkP);
kusano 7d535a
                        return 1;
kusano 7d535a
                    }
kusano 7d535a
                    chunkP[0].origSize = (int)benchedSize;
kusano 7d535a
                    chunkP[0].compressedSize = (int)errorCode;
kusano 7d535a
                    nbChunks = 1;
kusano 7d535a
                    break;
kusano 7d535a
            default :
kusano 7d535a
                continue;   /* skip if unknown ID */
kusano 7d535a
            }
kusano 7d535a
kusano 7d535a
            { size_t i; for (i=0; i
kusano 7d535a
kusano 7d535a
            for (loopNb = 1; loopNb <= g_nbIterations; loopNb++)
kusano 7d535a
            {
kusano 7d535a
                double averageTime;
kusano 7d535a
                int milliTime;
kusano 7d535a
                U32 crcDecoded;
kusano 7d535a
kusano 7d535a
                PROGRESS("%1i- %-29.29s :%10i ->\r", loopNb, dName, (int)benchedSize);
kusano 7d535a
kusano 7d535a
                nb_loops = 0;
kusano 7d535a
                milliTime = BMK_GetMilliStart();
kusano 7d535a
                while(BMK_GetMilliStart() == milliTime);
kusano 7d535a
                milliTime = BMK_GetMilliStart();
kusano 7d535a
                while(BMK_GetMilliSpan(milliTime) < TIMELOOP)
kusano 7d535a
                {
kusano 7d535a
                    for (chunkNb=0; chunkNb
kusano 7d535a
                    {
kusano 7d535a
                        int decodedSize = decompressionFunction(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedSize, chunkP[chunkNb].origSize);
kusano 7d535a
                        if (chunkP[chunkNb].origSize != decodedSize) DISPLAY("ERROR ! %s() == %i != %i !! \n", dName, decodedSize, chunkP[chunkNb].origSize), exit(1);
kusano 7d535a
                    }
kusano 7d535a
                    nb_loops++;
kusano 7d535a
                }
kusano 7d535a
                milliTime = BMK_GetMilliSpan(milliTime);
kusano 7d535a
kusano 7d535a
                nb_loops += !nb_loops;   /* Avoid division by zero */
kusano 7d535a
                averageTime = (double)milliTime / nb_loops;
kusano 7d535a
                if (averageTime < bestTime) bestTime = averageTime;
kusano 7d535a
kusano 7d535a
                PROGRESS("%1i- %-29.29s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000.);
kusano 7d535a
kusano 7d535a
                /* CRC Checking */
kusano 7d535a
                crcDecoded = XXH32(orig_buff, (int)benchedSize, 0);
kusano 7d535a
                if (crcOriginal!=crcDecoded) { DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded); exit(1); }
kusano 7d535a
            }
kusano 7d535a
kusano 7d535a
            DISPLAY("%2i-%-29.29s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000.);
kusano 7d535a
        }
kusano 7d535a
      }
kusano 7d535a
      free(orig_buff);
kusano 7d535a
      free(compressed_buff);
kusano 7d535a
      free(chunkP);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  LZ4F_freeDecompressionContext(g_dCtx);
kusano 7d535a
  if (g_pause) { printf("press enter...\n"); (void)getchar(); }
kusano 7d535a
kusano 7d535a
  return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static int usage(char* exename)
kusano 7d535a
{
kusano 7d535a
    DISPLAY( "Usage :\n");
kusano 7d535a
    DISPLAY( "      %s [arg] file1 file2 ... fileX\n", exename);
kusano 7d535a
    DISPLAY( "Arguments :\n");
kusano 7d535a
    DISPLAY( " -c     : compression tests only\n");
kusano 7d535a
    DISPLAY( " -d     : decompression tests only\n");
kusano 7d535a
    DISPLAY( " -H/-h  : Help (this text + advanced options)\n");
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int usage_advanced(void)
kusano 7d535a
{
kusano 7d535a
    DISPLAY( "\nAdvanced options :\n");
kusano 7d535a
    DISPLAY( " -c#    : test only compression function # [1-%i]\n", NB_COMPRESSION_ALGORITHMS);
kusano 7d535a
    DISPLAY( " -d#    : test only decompression function # [1-%i]\n", NB_DECOMPRESSION_ALGORITHMS);
kusano 7d535a
    DISPLAY( " -i#    : iteration loops [1-9](default : %i)\n", NBLOOPS);
kusano 7d535a
    DISPLAY( " -B#    : Block size [4-7](default : 7)\n");
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int badusage(char* exename)
kusano 7d535a
{
kusano 7d535a
    DISPLAY("Wrong parameters\n");
kusano 7d535a
    usage(exename);
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int main(int argc, char** argv)
kusano 7d535a
{
kusano 7d535a
    int i,
kusano 7d535a
        filenamesStart=2;
kusano 7d535a
    char* exename=argv[0];
kusano 7d535a
    char* input_filename=0;
kusano 7d535a
kusano 7d535a
    // Welcome message
kusano 7d535a
    DISPLAY(WELCOME_MESSAGE);
kusano 7d535a
kusano 7d535a
    if (argc<2) { badusage(exename); return 1; }
kusano 7d535a
kusano 7d535a
    for(i=1; i
kusano 7d535a
    {
kusano 7d535a
        char* argument = argv[i];
kusano 7d535a
kusano 7d535a
        if(!argument) continue;   // Protection if argument empty
kusano 7d535a
        if (!strcmp(argument, "--no-prompt"))
kusano 7d535a
        {
kusano 7d535a
            g_noPrompt = 1;
kusano 7d535a
            continue;
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        // Decode command (note : aggregated commands are allowed)
kusano 7d535a
        if (argument[0]=='-')
kusano 7d535a
        {
kusano 7d535a
            while (argument[1]!=0)
kusano 7d535a
            {
kusano 7d535a
                argument ++;
kusano 7d535a
kusano 7d535a
                switch(argument[0])
kusano 7d535a
                {
kusano 7d535a
                    // Select compression algorithm only
kusano 7d535a
                case 'c':
kusano 7d535a
                    g_decompressionTest = 0;
kusano 7d535a
                    while ((argument[1]>= '0') && (argument[1]<= '9'))
kusano 7d535a
                    {
kusano 7d535a
                        g_compressionAlgo *= 10;
kusano 7d535a
                        g_compressionAlgo += argument[1] - '0';
kusano 7d535a
                        argument++;
kusano 7d535a
                    }
kusano 7d535a
                    break;
kusano 7d535a
kusano 7d535a
                    // Select decompression algorithm only
kusano 7d535a
                case 'd':
kusano 7d535a
                    g_compressionTest = 0;
kusano 7d535a
                    while ((argument[1]>= '0') && (argument[1]<= '9'))
kusano 7d535a
                    {
kusano 7d535a
                        g_decompressionAlgo *= 10;
kusano 7d535a
                        g_decompressionAlgo += argument[1] - '0';
kusano 7d535a
                        argument++;
kusano 7d535a
                    }
kusano 7d535a
                    break;
kusano 7d535a
kusano 7d535a
                    // Display help on usage
kusano 7d535a
                case 'h' :
kusano 7d535a
                case 'H': usage(exename); usage_advanced(); return 0;
kusano 7d535a
kusano 7d535a
                    // Modify Block Properties
kusano 7d535a
                case 'B':
kusano 7d535a
                    while (argument[1]!=0)
kusano 7d535a
                    switch(argument[1])
kusano 7d535a
                    {
kusano 7d535a
                    case '4':
kusano 7d535a
                    case '5':
kusano 7d535a
                    case '6':
kusano 7d535a
                    case '7':
kusano 7d535a
                    {
kusano 7d535a
                        int B = argument[1] - '0';
kusano 7d535a
                        int S = 1 << (8 + 2*B);
kusano 7d535a
                        BMK_setBlocksize(S);
kusano 7d535a
                        argument++;
kusano 7d535a
                        break;
kusano 7d535a
                    }
kusano 7d535a
                    case 'D': argument++; break;
kusano 7d535a
                    default : goto _exit_blockProperties;
kusano 7d535a
                    }
kusano 7d535a
_exit_blockProperties:
kusano 7d535a
                    break;
kusano 7d535a
kusano 7d535a
                    // Modify Nb Iterations
kusano 7d535a
                case 'i':
kusano 7d535a
                    if ((argument[1] >='0') && (argument[1] <='9'))
kusano 7d535a
                    {
kusano 7d535a
                        int iters = argument[1] - '0';
kusano 7d535a
                        BMK_setNbIterations(iters);
kusano 7d535a
                        argument++;
kusano 7d535a
                    }
kusano 7d535a
                    break;
kusano 7d535a
kusano 7d535a
                    // Pause at the end (hidden option)
kusano 7d535a
                case 'p': BMK_setPause(); break;
kusano 7d535a
kusano 7d535a
                    // Unknown command
kusano 7d535a
                default : badusage(exename); return 1;
kusano 7d535a
                }
kusano 7d535a
            }
kusano 7d535a
            continue;
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        // first provided filename is input
kusano 7d535a
        if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
kusano 7d535a
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    // No input filename ==> Error
kusano 7d535a
    if(!input_filename) { badusage(exename); return 1; }
kusano 7d535a
kusano 7d535a
    return fullSpeedBench(argv+filenamesStart, argc-filenamesStart);
kusano 7d535a
kusano 7d535a
}
kusano 7d535a