lz4c : automatic output name when decoding a *.lz4 file, as requested by Peter Humphreys

Cleaner C99 detection, thanks to Elad
Modified Basic Types, as recommended by Wayne Scott
Simplified compression function
Corrected : warning on missing #undef in lz4hc, thanks Arseny Kapoulkine
Modified length loops as suggested by Wouter Vermaelen
Modified pragma pack directive for older versions of GCC, as reported by Peter Costello
Updated xxHash to r29
Indent lz4hc.c with spaces 

git-svn-id: https://lz4.googlecode.com/svn/trunk@95 650e7d94-2a16-8b24-b05c-7c0b3f6821cd
This commit is contained in:
yann.collet.73@gmail.com 2013-05-17 18:41:32 +00:00
parent 40ae7043df
commit e185b252f0
8 changed files with 550 additions and 491 deletions

24
bench.c
View File

@ -77,19 +77,19 @@
//************************************** //**************************************
// Basic Types // Basic Types
//************************************** //**************************************
#if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
#define BYTE unsigned __int8 # include <stdint.h>
#define U16 unsigned __int16 typedef uint8_t BYTE;
#define U32 unsigned __int32 typedef uint16_t U16;
#define S32 __int32 typedef uint32_t U32;
#define U64 unsigned __int64 typedef int32_t S32;
typedef uint64_t U64;
#else #else
#include <stdint.h> typedef unsigned char BYTE;
#define BYTE uint8_t typedef unsigned short U16;
#define U16 uint16_t typedef unsigned int U32;
#define U32 uint32_t typedef signed int S32;
#define S32 int32_t typedef unsigned long long U64;
#define U64 uint64_t
#endif #endif

51
lz4.c
View File

@ -46,9 +46,10 @@ Note : this source file requires "lz4_encoder.h" and "lz4_decoder.h"
#define MEMORY_USAGE 14 #define MEMORY_USAGE 14
// HEAPMODE : // HEAPMODE :
// Select if compression algorithm will allocate space for its tables // Select how default compression function will allocate memory for its hash table,
// in memory stack (0:default, fastest), or in memory heap (1:requires memory allocation (malloc)). // in memory stack (0:default, fastest), or in memory heap (1:requires memory allocation (malloc)).
// Default allocation strategy is to use stack (HEAPMODE 0) // Default allocation strategy is to use stack (HEAPMODE 0)
// Note : explicit functions *_stack* and *_heap* are unaffected by this setting
#define HEAPMODE 0 #define HEAPMODE 0
// BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE : // BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE :
@ -63,7 +64,9 @@ Note : this source file requires "lz4_encoder.h" and "lz4_decoder.h"
// CPU Feature Detection // CPU Feature Detection
//************************************** //**************************************
// 32 or 64 bits ? // 32 or 64 bits ?
#if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) \
|| defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) \
|| defined(__ia64__) ) // Detects 64 bits mode
# define LZ4_ARCH64 1 # define LZ4_ARCH64 1
#else #else
# define LZ4_ARCH64 0 # define LZ4_ARCH64 0
@ -103,7 +106,7 @@ Note : this source file requires "lz4_encoder.h" and "lz4_decoder.h"
//************************************** //**************************************
// Compiler Options // Compiler Options
//************************************** //**************************************
#if __STDC_VERSION__ >= 199901L // C99 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
/* "restrict" is a known keyword */ /* "restrict" is a known keyword */
#else #else
# define restrict // Disable restrict # define restrict // Disable restrict
@ -150,30 +153,36 @@ Note : this source file requires "lz4_encoder.h" and "lz4_decoder.h"
//************************************** //**************************************
// Basic Types // Basic Types
//************************************** //**************************************
#if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
# define BYTE unsigned __int8
# define U16 unsigned __int16
# define U32 unsigned __int32
# define S32 __int32
# define U64 unsigned __int64
#else
# include <stdint.h> # include <stdint.h>
# define BYTE uint8_t typedef uint8_t BYTE;
# define U16 uint16_t typedef uint16_t U16;
# define U32 uint32_t typedef uint32_t U32;
# define S32 int32_t typedef int32_t S32;
# define U64 uint64_t typedef uint64_t U64;
#else
typedef unsigned char BYTE;
typedef unsigned short U16;
typedef unsigned int U32;
typedef signed int S32;
typedef unsigned long long U64;
#endif #endif
#ifndef LZ4_FORCE_UNALIGNED_ACCESS #if defined(__GNUC__) && !defined(LZ4_FORCE_UNALIGNED_ACCESS)
# define _PACKED __attribute__ ((packed))
#else
# define _PACKED
#endif
#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)
# pragma pack(push, 1) # pragma pack(push, 1)
#endif #endif
typedef struct _U16_S { U16 v; } U16_S; typedef struct _U16_S { U16 v; } _PACKED U16_S;
typedef struct _U32_S { U32 v; } U32_S; typedef struct _U32_S { U32 v; } _PACKED U32_S;
typedef struct _U64_S { U64 v; } U64_S; typedef struct _U64_S { U64 v; } _PACKED U64_S;
#ifndef LZ4_FORCE_UNALIGNED_ACCESS #if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)
# pragma pack(pop) # pragma pack(pop)
#endif #endif
@ -242,7 +251,7 @@ typedef struct _U64_S { U64 v; } U64_S;
// Macros // Macros
//************************************** //**************************************
#define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e); #define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
#define LZ4_BLINDCOPY(s,d,l) { BYTE* e=(d)+l; LZ4_WILDCOPY(s,d,e); d=e; } #define LZ4_BLINDCOPY(s,d,l) { BYTE* e=(d)+(l); LZ4_WILDCOPY(s,d,e); d=e; }
//**************************** //****************************

View File

@ -152,34 +152,14 @@ int FUNCTION_NAME(
#ifdef LIMITED_OUTPUT #ifdef LIMITED_OUTPUT
if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
#endif #endif
#ifdef _MSC_VER
if (length>=(int)RUN_MASK) if (length>=(int)RUN_MASK)
{ {
int len = length-RUN_MASK; int len = length-RUN_MASK;
*token=(RUN_MASK<<ML_BITS); *token=(RUN_MASK<<ML_BITS);
if (len>254) for(; len >= 255 ; len-=255) *op++ = 255;
{
do { *op++ = 255; len -= 255; } while (len>254);
*op++ = (BYTE)len;
memcpy(op, anchor, length);
op += length;
goto _next_match;
}
else
*op++ = (BYTE)len; *op++ = (BYTE)len;
} }
else *token = (BYTE)(length<<ML_BITS); else *token = (BYTE)(length<<ML_BITS);
#else
if (length>=(int)RUN_MASK)
{
int len;
*token=(RUN_MASK<<ML_BITS);
len = length-RUN_MASK;
for(; len > 254 ; len-=255) *op++ = 255;
*op++ = (BYTE)len;
}
else *token = (length<<ML_BITS);
#endif
// Copy Literals // Copy Literals
LZ4_BLINDCOPY(anchor, op, length); LZ4_BLINDCOPY(anchor, op, length);
@ -213,7 +193,7 @@ _endCount:
*token += ML_MASK; *token += ML_MASK;
length -= ML_MASK; length -= ML_MASK;
for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; } for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; }
if (length > 254) { length-=255; *op++ = 255; } if (length >= 255) { length-=255; *op++ = 255; }
*op++ = (BYTE)length; *op++ = (BYTE)length;
} }
else *token += (BYTE)length; else *token += (BYTE)length;
@ -241,7 +221,7 @@ _last_literals:
#ifdef LIMITED_OUTPUT #ifdef LIMITED_OUTPUT
if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0; // Check output limit if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0; // Check output limit
#endif #endif
if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun >= 255 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
else *op++ = (BYTE)(lastRun<<ML_BITS); else *op++ = (BYTE)(lastRun<<ML_BITS);
memcpy(op, anchor, iend - anchor); memcpy(op, anchor, iend - anchor);
op += iend-anchor; op += iend-anchor;

20
lz4c.c
View File

@ -24,9 +24,9 @@
/* /*
Note : this is stand-alone program. Note : this is stand-alone program.
It is not part of LZ4 compression library, it is a user program of LZ4 library. It is not part of LZ4 compression library, it is a user program of LZ4 library.
The license of LZ4 is BSD. The license of LZ4 library is BSD.
The license of xxHash is BSD. The license of xxHash library is BSD.
The license of the compression program is GPLv2. The license of this compression CLI program is GPLv2.
*/ */
//************************************** //**************************************
@ -162,7 +162,7 @@ static int streamChecksum = 1;
int usage(char* exename) int usage(char* exename)
{ {
DISPLAY( "Usage :\n"); DISPLAY( "Usage :\n");
DISPLAY( " %s [arg] input output\n", exename); DISPLAY( " %s [arg] input [output]\n", exename);
DISPLAY( "Arguments :\n"); DISPLAY( "Arguments :\n");
DISPLAY( " -c0/-c : Fast compression (default) \n"); DISPLAY( " -c0/-c : Fast compression (default) \n");
DISPLAY( " -c1/-hc: High compression \n"); DISPLAY( " -c1/-hc: High compression \n");
@ -819,7 +819,7 @@ int main(int argc, char** argv)
// No output filename ==> build one automatically (for compression only) // No output filename ==> build one automatically (for compression only)
if (!output_filename) if (!output_filename)
{ {
if (!decode) if (!decode) // compression
{ {
int i=0, l=0; int i=0, l=0;
while (input_filename[l]!=0) l++; while (input_filename[l]!=0) l++;
@ -829,9 +829,15 @@ int main(int argc, char** argv)
} }
else else
{ {
badusage(exename); int inl=0,outl;
return 1; while (input_filename[inl]!=0) inl++;
output_filename = (char*)calloc(1,inl+1);
for (outl=0;outl<inl;outl++) output_filename[outl] = input_filename[outl];
if (inl>4)
while ((outl >= inl-4) && (input_filename[outl] == extension[outl-inl+4])) output_filename[outl--]=0;
if (outl != inl-5) output_filename = NULL;
} }
if (!output_filename) { badusage(exename); return 1; }
} }
if (decode) return decodeFile(input_filename, output_filename); if (decode) return decodeFile(input_filename, output_filename);

134
lz4hc.c
View File

@ -36,11 +36,23 @@ Note : this source file requires "lz4hc_encoder.h"
*/ */
//**************************************
// Memory routines
//**************************************
#include <stdlib.h> // calloc, free
#define ALLOCATOR(s) calloc(1,s)
#define FREEMEM free
#include <string.h> // memset, memcpy
#define MEM_INIT memset
//************************************** //**************************************
// CPU Feature Detection // CPU Feature Detection
//************************************** //**************************************
// 32 or 64 bits ? // 32 or 64 bits ?
#if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) \
|| defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) \
|| defined(__ia64__) ) // Detects 64 bits mode
# define LZ4_ARCH64 1 # define LZ4_ARCH64 1
#else #else
# define LZ4_ARCH64 0 # define LZ4_ARCH64 0
@ -80,7 +92,7 @@ Note : this source file requires "lz4hc_encoder.h"
//************************************** //**************************************
// Compiler Options // Compiler Options
//************************************** //**************************************
#if __STDC_VERSION__ >= 199901L // C99 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
/* "restrict" is a known keyword */ /* "restrict" is a known keyword */
#else #else
# define restrict // Disable restrict # define restrict // Disable restrict
@ -117,44 +129,44 @@ Note : this source file requires "lz4hc_encoder.h"
//************************************** //**************************************
// Includes // Includes
//************************************** //**************************************
#include <stdlib.h> // calloc, free
#include <string.h> // memset, memcpy
#include "lz4hc.h" #include "lz4hc.h"
#include "lz4.h" #include "lz4.h"
#define ALLOCATOR(s) calloc(1,s)
#define FREEMEM free
#define MEM_INIT memset
//************************************** //**************************************
// Basic Types // Basic Types
//************************************** //**************************************
#if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
#define BYTE unsigned __int8 # include <stdint.h>
#define U16 unsigned __int16 typedef uint8_t BYTE;
#define U32 unsigned __int32 typedef uint16_t U16;
#define S32 __int32 typedef uint32_t U32;
#define U64 unsigned __int64 typedef int32_t S32;
typedef uint64_t U64;
#else #else
#include <stdint.h> typedef unsigned char BYTE;
#define BYTE uint8_t typedef unsigned short U16;
#define U16 uint16_t typedef unsigned int U32;
#define U32 uint32_t typedef signed int S32;
#define S32 int32_t typedef unsigned long long U64;
#define U64 uint64_t
#endif #endif
#ifndef LZ4_FORCE_UNALIGNED_ACCESS #if defined(__GNUC__) && !defined(LZ4_FORCE_UNALIGNED_ACCESS)
#pragma pack(push, 1) # define _PACKED __attribute__ ((packed))
#else
# define _PACKED
#endif #endif
typedef struct _U16_S { U16 v; } U16_S; #if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)
typedef struct _U32_S { U32 v; } U32_S; # pragma pack(push, 1)
typedef struct _U64_S { U64 v; } U64_S; #endif
#ifndef LZ4_FORCE_UNALIGNED_ACCESS typedef struct _U16_S { U16 v; } _PACKED U16_S;
#pragma pack(pop) typedef struct _U32_S { U32 v; } _PACKED U32_S;
typedef struct _U64_S { U64 v; } _PACKED U64_S;
#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)
# pragma pack(pop)
#endif #endif
#define A64(x) (((U64_S *)(x))->v) #define A64(x) (((U64_S *)(x))->v)
@ -194,29 +206,29 @@ typedef struct _U64_S { U64 v; } U64_S;
// Architecture-specific macros // Architecture-specific macros
//************************************** //**************************************
#if LZ4_ARCH64 // 64-bit #if LZ4_ARCH64 // 64-bit
#define STEPSIZE 8 # define STEPSIZE 8
#define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8; # define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
#define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d) # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
#define UARCH U64 # define UARCH U64
#define AARCH A64 # define AARCH A64
#define HTYPE U32 # define HTYPE U32
#define INITBASE(b,s) const BYTE* const b = s # define INITBASE(b,s) const BYTE* const b = s
#else // 32-bit #else // 32-bit
#define STEPSIZE 4 # define STEPSIZE 4
#define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4; # define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
#define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d); # define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
#define UARCH U32 # define UARCH U32
#define AARCH A32 # define AARCH A32
#define HTYPE const BYTE* # define HTYPE const BYTE*
#define INITBASE(b,s) const int b = 0 # define INITBASE(b,s) const int b = 0
#endif #endif
#if defined(LZ4_BIG_ENDIAN) #if defined(LZ4_BIG_ENDIAN)
#define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; } # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
#define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; } # define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
#else // Little Endian #else // Little Endian
#define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); } # define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
#define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; } # define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
#endif #endif
@ -252,30 +264,30 @@ typedef struct
inline static int LZ4_NbCommonBytes (register U64 val) inline static int LZ4_NbCommonBytes (register U64 val)
{ {
#if defined(LZ4_BIG_ENDIAN) #if defined(LZ4_BIG_ENDIAN)
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0; unsigned long r = 0;
_BitScanReverse64( &r, val ); _BitScanReverse64( &r, val );
return (int)(r>>3); return (int)(r>>3);
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) # elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (__builtin_clzll(val) >> 3); return (__builtin_clzll(val) >> 3);
#else # else
int r; int r;
if (!(val>>32)) { r=4; } else { r=0; val>>=32; } if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
r += (!val); r += (!val);
return r; return r;
#endif # endif
#else #else
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r = 0; unsigned long r = 0;
_BitScanForward64( &r, val ); _BitScanForward64( &r, val );
return (int)(r>>3); return (int)(r>>3);
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) # elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (__builtin_ctzll(val) >> 3); return (__builtin_ctzll(val) >> 3);
#else # else
static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58]; return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
#endif # endif
#endif #endif
} }
@ -284,29 +296,29 @@ inline static int LZ4_NbCommonBytes (register U64 val)
inline static int LZ4_NbCommonBytes (register U32 val) inline static int LZ4_NbCommonBytes (register U32 val)
{ {
#if defined(LZ4_BIG_ENDIAN) #if defined(LZ4_BIG_ENDIAN)
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r; unsigned long r;
_BitScanReverse( &r, val ); _BitScanReverse( &r, val );
return (int)(r>>3); return (int)(r>>3);
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) # elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (__builtin_clz(val) >> 3); return (__builtin_clz(val) >> 3);
#else # else
int r; int r;
if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
r += (!val); r += (!val);
return r; return r;
#endif # endif
#else #else
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
unsigned long r; unsigned long r;
_BitScanForward( &r, val ); _BitScanForward( &r, val );
return (int)(r>>3); return (int)(r>>3);
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) # elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
return (__builtin_ctz(val) >> 3); return (__builtin_ctz(val) >> 3);
#else # else
static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };
return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
#endif # endif
#endif #endif
} }

View File

@ -332,6 +332,7 @@ int FUNCTION_NAME (const char* source,
// Locally Generated // Locally Generated
#undef ENCODE_SEQUENCE #undef ENCODE_SEQUENCE
#undef ENCODE_SEQUENCE_NAME
// Optional defines // Optional defines
#ifdef LIMITED_OUTPUT #ifdef LIMITED_OUTPUT

187
xxhash.c
View File

@ -1,33 +1,33 @@
/* /*
xxHash - Fast Hash algorithm xxHash - Fast Hash algorithm
Copyright (C) 2012-2013, Yann Collet. Copyright (C) 2012-2013, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are modification, are permitted provided that the following conditions are
met: met:
* Redistributions of source code must retain the above copyright * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer. notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the in the documentation and/or other materials provided with the
distribution. distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at : You can contact the author at :
- xxHash source repository : http://code.google.com/p/xxhash/ - xxHash source repository : http://code.google.com/p/xxhash/
*/ */
@ -35,14 +35,21 @@
//************************************** //**************************************
// Tuning parameters // Tuning parameters
//************************************** //**************************************
// FORCE_NATIVE_FORMAT : // XXH_ACCEPT_NULL_INPUT_POINTER :
// By default, xxHash library provides endian-independant Hash values. // If the input pointer is a null pointer, xxHash default behavior is to crash, since it is a bad input.
// Results are therefore identical for big-endian and little-endian CPU. // If this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
// This option has a very small performance cost (only measurable on small inputs).
// By default, this option is disabled. To enable it, uncomment below define :
//#define XXH_ACCEPT_NULL_INPUT_POINTER 1
// XXH_FORCE_NATIVE_FORMAT :
// By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
// Results are therefore identical for little-endian and big-endian CPU.
// This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format. // This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
// Should endian-independance be of no importance to your application, you may uncomment the #define below // Should endian-independance be of no importance to your application, you may uncomment the #define below
// It will improve speed for Big-endian CPU. // It will improve speed for Big-endian CPU.
// This option has no impact on Little_Endian CPU. // This option has no impact on Little_Endian CPU.
//#define FORCE_NATIVE_FORMAT 1 //#define XXH_FORCE_NATIVE_FORMAT 1
@ -60,7 +67,7 @@
//************************************** //**************************************
// Little Endian or Big Endian ? // Little Endian or Big Endian ?
// You can overwrite the #define below if you know your architecture endianess // You can overwrite the #define below if you know your architecture endianess
#if defined(FORCE_NATIVE_FORMAT) && (FORCE_NATIVE_FORMAT==1) #if defined(XXH_FORCE_NATIVE_FORMAT) && (XXH_FORCE_NATIVE_FORMAT==1)
// Force native format. The result will be endian dependant. // Force native format. The result will be endian dependant.
# define XXH_BIG_ENDIAN 0 # define XXH_BIG_ENDIAN 0
#elif defined (__GLIBC__) #elif defined (__GLIBC__)
@ -83,6 +90,24 @@
#endif #endif
//**************************************
// Basic Types
//**************************************
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
# include <stdint.h>
typedef uint8_t BYTE;
typedef uint16_t U16;
typedef uint32_t U32;
typedef int32_t S32;
typedef uint64_t U64;
#else
typedef unsigned char BYTE;
typedef unsigned short U16;
typedef unsigned int U32;
typedef signed int S32;
typedef unsigned long long U64;
#endif
//************************************** //**************************************
// Compiler-specific Options & Functions // Compiler-specific Options & Functions
@ -101,16 +126,14 @@
#elif GCC_VERSION >= 403 #elif GCC_VERSION >= 403
# define XXH_swap32 __builtin_bswap32 # define XXH_swap32 __builtin_bswap32
#else #else
static inline unsigned int XXH_swap32 (unsigned int x) { static inline U32 XXH_swap32 (U32 x) {
return ((x << 24) & 0xff000000 ) | return ((x << 24) & 0xff000000 ) |
((x << 8) & 0x00ff0000 ) | ((x << 8) & 0x00ff0000 ) |
((x >> 8) & 0x0000ff00 ) | ((x >> 8) & 0x0000ff00 ) |
((x >> 24) & 0x000000ff ); ((x >> 24) & 0x000000ff );}
}
#endif #endif
//************************************** //**************************************
// Constants // Constants
//************************************** //**************************************
@ -121,11 +144,10 @@ static inline unsigned int XXH_swap32 (unsigned int x) {
#define PRIME32_5 374761393U #define PRIME32_5 374761393U
//************************************** //**************************************
// Macros // Macros
//************************************** //**************************************
#define XXH_LE32(p) (XXH_BIG_ENDIAN ? XXH_swap32(*(unsigned int*)(p)) : *(unsigned int*)(p)) #define XXH_LE32(p) (XXH_BIG_ENDIAN ? XXH_swap32(*(U32*)(p)) : *(U32*)(p))
@ -133,7 +155,7 @@ static inline unsigned int XXH_swap32 (unsigned int x) {
// Simple Hash Functions // Simple Hash Functions
//**************************** //****************************
unsigned int XXH32(const void* input, int len, unsigned int seed) U32 XXH32(const void* input, int len, U32 seed)
{ {
#if 0 #if 0
// Simple version, good for code maintenance, but unfortunately slow for small inputs // Simple version, good for code maintenance, but unfortunately slow for small inputs
@ -142,17 +164,21 @@ unsigned int XXH32(const void* input, int len, unsigned int seed)
return XXH32_digest(state); return XXH32_digest(state);
#else #else
const unsigned char* p = (const unsigned char*)input; const BYTE* p = (const BYTE*)input;
const unsigned char* const bEnd = p + len; const BYTE* const bEnd = p + len;
unsigned int h32; U32 h32;
#ifdef XXH_ACCEPT_NULL_INPUT_POINTER
if (p==NULL) { len=0; p=(const BYTE*)16; }
#endif
if (len>=16) if (len>=16)
{ {
const unsigned char* const limit = bEnd - 16; const BYTE* const limit = bEnd - 16;
unsigned int v1 = seed + PRIME32_1 + PRIME32_2; U32 v1 = seed + PRIME32_1 + PRIME32_2;
unsigned int v2 = seed + PRIME32_2; U32 v2 = seed + PRIME32_2;
unsigned int v3 = seed + 0; U32 v3 = seed + 0;
unsigned int v4 = seed - PRIME32_1; U32 v4 = seed - PRIME32_1;
do do
{ {
@ -160,7 +186,7 @@ unsigned int XXH32(const void* input, int len, unsigned int seed)
v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
} while (p<=limit) ; } while (p<=limit);
h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
} }
@ -169,7 +195,7 @@ unsigned int XXH32(const void* input, int len, unsigned int seed)
h32 = seed + PRIME32_5; h32 = seed + PRIME32_5;
} }
h32 += (unsigned int) len; h32 += (U32) len;
while (p<=bEnd-4) while (p<=bEnd-4)
{ {
@ -203,20 +229,23 @@ unsigned int XXH32(const void* input, int len, unsigned int seed)
struct XXH_state32_t struct XXH_state32_t
{ {
unsigned int seed; U32 seed;
unsigned int v1; U32 v1;
unsigned int v2; U32 v2;
unsigned int v3; U32 v3;
unsigned int v4; U32 v4;
unsigned long long total_len; U64 total_len;
char memory[16]; char memory[16];
int memsize; int memsize;
}; };
void* XXH32_init (unsigned int seed) int XXH32_sizeofState() { return sizeof(struct XXH_state32_t); }
XXH_errorcode XXH32_resetState(void* state_in, unsigned int seed)
{ {
struct XXH_state32_t * state = (struct XXH_state32_t *) malloc ( sizeof(struct XXH_state32_t)); struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
state->seed = seed; state->seed = seed;
state->v1 = seed + PRIME32_1 + PRIME32_2; state->v1 = seed + PRIME32_1 + PRIME32_2;
state->v2 = seed + PRIME32_2; state->v2 = seed + PRIME32_2;
@ -224,7 +253,14 @@ void* XXH32_init (unsigned int seed)
state->v4 = seed - PRIME32_1; state->v4 = seed - PRIME32_1;
state->total_len = 0; state->total_len = 0;
state->memsize = 0; state->memsize = 0;
return OK;
}
void* XXH32_init (U32 seed)
{
struct XXH_state32_t * state = (struct XXH_state32_t *) malloc (sizeof(struct XXH_state32_t));
XXH32_resetState(state, seed);
return (void*)state; return (void*)state;
} }
@ -232,10 +268,12 @@ void* XXH32_init (unsigned int seed)
XXH_errorcode XXH32_update (void* state_in, const void* input, int len) XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
{ {
struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
const unsigned char* p = (const unsigned char*)input; const BYTE* p = (const BYTE*)input;
const unsigned char* const bEnd = p + len; const BYTE* const bEnd = p + len;
#ifdef XXH_ACCEPT_NULL_INPUT_POINTER
if (input==NULL) return XXH_ERROR; if (input==NULL) return XXH_ERROR;
#endif
state->total_len += len; state->total_len += len;
@ -250,7 +288,7 @@ XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
{ {
memcpy(state->memory + state->memsize, input, 16-state->memsize); memcpy(state->memory + state->memsize, input, 16-state->memsize);
{ {
const unsigned int* p32 = (const unsigned int*)state->memory; const U32* p32 = (const U32*)state->memory;
state->v1 += XXH_LE32(p32) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++; state->v1 += XXH_LE32(p32) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++;
state->v2 += XXH_LE32(p32) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++; state->v2 += XXH_LE32(p32) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++;
state->v3 += XXH_LE32(p32) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++; state->v3 += XXH_LE32(p32) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++;
@ -260,20 +298,21 @@ XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
state->memsize = 0; state->memsize = 0;
} }
if (p <= bEnd-16)
{ {
const unsigned char* const limit = bEnd - 16; const BYTE* const limit = bEnd - 16;
unsigned int v1 = state->v1; U32 v1 = state->v1;
unsigned int v2 = state->v2; U32 v2 = state->v2;
unsigned int v3 = state->v3; U32 v3 = state->v3;
unsigned int v4 = state->v4; U32 v4 = state->v4;
while (p<=limit) do
{ {
v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4; v1 += XXH_LE32(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4; v2 += XXH_LE32(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4; v3 += XXH_LE32(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4; v4 += XXH_LE32(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
} } while (p<=limit);
state->v1 = v1; state->v1 = v1;
state->v2 = v2; state->v2 = v2;
@ -291,12 +330,12 @@ XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
} }
unsigned int XXH32_intermediateDigest (void* state_in) U32 XXH32_intermediateDigest (void* state_in)
{ {
struct XXH_state32_t * state = (struct XXH_state32_t *) state_in; struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
unsigned char * p = (unsigned char*)state->memory; BYTE * p = (BYTE*)state->memory;
unsigned char* bEnd = (unsigned char*)state->memory + state->memsize; BYTE* bEnd = (BYTE*)state->memory + state->memsize;
unsigned int h32; U32 h32;
if (state->total_len >= 16) if (state->total_len >= 16)
@ -308,19 +347,19 @@ unsigned int XXH32_intermediateDigest (void* state_in)
h32 = state->seed + PRIME32_5; h32 = state->seed + PRIME32_5;
} }
h32 += (unsigned int) state->total_len; h32 += (U32) state->total_len;
while (p<=bEnd-4) while (p<=bEnd-4)
{ {
h32 += XXH_LE32(p) * PRIME32_3; h32 += XXH_LE32(p) * PRIME32_3;
h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; h32 = XXH_rotl32(h32, 17) * PRIME32_4;
p+=4; p+=4;
} }
while (p<bEnd) while (p<bEnd)
{ {
h32 += (*p) * PRIME32_5; h32 += (*p) * PRIME32_5;
h32 = XXH_rotl32(h32, 11) * PRIME32_1 ; h32 = XXH_rotl32(h32, 11) * PRIME32_1;
p++; p++;
} }
@ -334,9 +373,9 @@ unsigned int XXH32_intermediateDigest (void* state_in)
} }
unsigned int XXH32_digest (void* state_in) U32 XXH32_digest (void* state_in)
{ {
unsigned int h32 = XXH32_intermediateDigest(state_in); U32 h32 = XXH32_intermediateDigest(state_in);
free(state_in); free(state_in);

View File

@ -121,6 +121,18 @@ Memory will be freed by XXH32_digest().
*/ */
int XXH32_sizeofState();
XXH_errorcode XXH32_resetState(void* state_in, unsigned int seed);
/*
These functions are the basic elements of XXH32_init();
The objective is to allow user application to make its own allocation.
XXH32_sizeofState() is used to know how much space must be allocated by the application.
This space must be referenced by a void* pointer.
This pointer must be provided as 'state_in' into XXH32_resetState(), which initializes the state.
*/
unsigned int XXH32_intermediateDigest (void* state); unsigned int XXH32_intermediateDigest (void* state);
/* /*
This function does the same as XXH32_digest(), generating a 32-bit hash, This function does the same as XXH32_digest(), generating a 32-bit hash,