39 #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode
47 #if defined (__GLIBC__)
49 # if (__BYTE_ORDER == __BIG_ENDIAN)
50 # define LZ4_BIG_ENDIAN 1
52 #elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
53 # define LZ4_BIG_ENDIAN 1
54 #elif defined(__sparc) || defined(__sparc__) \
55 || defined(__ppc__) || defined(_POWER) || defined(__powerpc__) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) \
56 || defined(__hpux) || defined(__hppa) \
57 || defined(_MIPSEB) || defined(__s390__)
58 # define LZ4_BIG_ENDIAN 1
66 #if defined(__ARM_FEATURE_UNALIGNED)
67 # define LZ4_FORCE_UNALIGNED_ACCESS 1
71 #if defined(_MSC_VER) && defined(_WIN32_WCE) // Visual Studio for Windows CE does not support Hardware bit count
72 # define LZ4_FORCE_SW_BITCOUNT
79 #if __STDC_VERSION__ >= 199901L // C99
82 # define restrict // Disable restrict
86 # define inline __inline // Visual is not C99, but supports some kind of inline
87 # define forceinline __forceinline
89 # if LZ4_ARCH64 // 64-bit
90 # pragma intrinsic(_BitScanForward64) // For Visual 2005
91 # pragma intrinsic(_BitScanReverse64) // For Visual 2005
93 # pragma intrinsic(_BitScanForward) // For Visual 2005
94 # pragma intrinsic(_BitScanReverse) // For Visual 2005
98 # define forceinline inline __attribute__((always_inline))
100 # define forceinline inline
104 #ifdef _MSC_VER // Visual Studio
105 #define lz4_bswap16(x) _byteswap_ushort(x)
107 #define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
118 #define ALLOCATOR(s) calloc(1,s)
120 #define MEM_INIT memset
126 #if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
127 #define BYTE unsigned __int8
128 #define U16 unsigned __int16
129 #define U32 unsigned __int32
131 #define U64 unsigned __int64
141 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
142 #pragma pack(push, 1)
149 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
153 #define A64(x) (((U64_S *)(x))->v)
154 #define A32(x) (((U32_S *)(x))->v)
155 #define A16(x) (((U16_S *)(x))->v)
163 #define DICTIONARY_LOGSIZE 16
164 #define MAXD (1<<DICTIONARY_LOGSIZE)
165 #define MAXD_MASK ((U32)(MAXD - 1))
166 #define MAX_DISTANCE (MAXD - 1)
168 #define HASH_LOG (DICTIONARY_LOGSIZE-1)
169 #define HASHTABLESIZE (1 << HASH_LOG)
170 #define HASH_MASK (HASHTABLESIZE - 1)
172 #define MAX_NB_ATTEMPTS 256
175 #define ML_MASK (size_t)((1U<<ML_BITS)-1)
176 #define RUN_BITS (8-ML_BITS)
177 #define RUN_MASK ((1U<<RUN_BITS)-1)
180 #define LASTLITERALS 5
181 #define MFLIMIT (COPYLENGTH+MINMATCH)
182 #define MINLENGTH (MFLIMIT+1)
183 #define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH)
189 #if LZ4_ARCH64 // 64-bit
191 #define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
192 #define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
196 #define INITBASE(b,s) const BYTE* const b = s
199 #define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
200 #define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
203 #define HTYPE const BYTE*
204 #define INITBASE(b,s) const int b = 0
207 #if defined(LZ4_BIG_ENDIAN)
208 #define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
209 #define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
210 #else // Little Endian
211 #define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
212 #define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
231 #define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
232 #define LZ4_BLINDCOPY(s,d,l) { BYTE* e=d+l; LZ4_WILDCOPY(s,d,e); d=e; }
233 #define HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASH_LOG))
234 #define HASH_VALUE(p) HASH_FUNCTION(A32(p))
235 #define HASH_POINTER(p) (HashTable[HASH_VALUE(p)] + base)
236 #define DELTANEXT(p) chainTable[(size_t)(p) & MAXD_MASK]
237 #define GETNEXT(p) ((p) - (size_t)DELTANEXT(p))
247 #if defined(LZ4_BIG_ENDIAN)
248 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
250 _BitScanReverse64( &r, val );
252 #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
253 return (__builtin_clzll(val) >> 3);
256 if (!(val>>32)) { r=4; }
else { r=0; val>>=32; }
257 if (!(val>>16)) { r+=2; val>>=8; }
else { val>>=24; }
262 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
264 _BitScanForward64( &r, val );
266 #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
267 return (__builtin_ctzll(val) >> 3);
269 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 };
270 return DeBruijnBytePos[((
U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
279 #if defined(LZ4_BIG_ENDIAN)
280 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
282 _BitScanReverse( &r, val );
284 #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
285 return (__builtin_clz(val) >> 3);
288 if (!(val>>16)) { r=2; val>>=8; }
else { r=0; val>>=24; }
293 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
295 _BitScanForward( &r, val );
297 #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
298 return (__builtin_ctz(val) >> 3);
300 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 };
301 return DeBruijnBytePos[((
U32)((val & -(
S32)val) * 0x077CB531U)) >> 27];
357 const BYTE* p1t = p1;
366 if (
LZ4_ARCH64)
if ((p1t<(matchlimit-3)) && (
A32(p2) ==
A32(p1t))) { p1t+=4; p2+=4; }
367 if ((p1t<(matchlimit-1)) && (
A16(p2) ==
A16(p1t))) { p1t+=2; p2+=2; }
368 if ((p1t<matchlimit) && (*p2 == *p1t)) p1t++;
387 #define REPEAT_OPTIMIZATION
388 #ifdef REPEAT_OPTIMIZATION
394 delta = (
U16)(ip-ref);
405 if (*(ref+ml) == *(ip+ml))
409 if (mlt > ml) { ml = mlt; *matchpos = ref; }
414 #ifdef REPEAT_OPTIMIZATION
418 const BYTE* ptr = ip;
422 while(ptr < end-delta)
448 int delta = (int)(ip-startLimit);
457 if (*(startLimit + longest) == *(ref - delta + longest))
463 const BYTE* startt = ip;
472 if (
LZ4_ARCH64)
if ((ipt<(matchlimit-3)) && (
A32(reft) ==
A32(ipt))) { ipt+=4; reft+=4; }
473 if ((ipt<(matchlimit-1)) && (
A16(reft) ==
A16(ipt))) { ipt+=2; reft+=2; }
474 if ((ipt<matchlimit) && (*reft == *ipt)) ipt++;
479 const BYTE* startt = ip;
480 const BYTE* reft = ref;
484 while ((startt>startLimit) && (reft > hc4->
base) && (startt[-1] == reft[-1])) {startt--; reft--;}
486 if ((ipt-startt) > longest)
488 longest = (int)(ipt-startt);
506 length = (int)(*ip - *anchor);
509 else *token = (length<<
ML_BITS);
519 if (len>=(
int)
ML_MASK) { *token+=
ML_MASK; len-=
ML_MASK;
for(; len > 509 ; len-=510) { *(*op)++ = 255; *(*op)++ = 255; }
if (len > 254) { len-=255; *(*op)++ = 255; } *(*op)++ = (
BYTE)len; }
539 const BYTE* ip = (
const BYTE*) source;
540 const BYTE* anchor = ip;
541 const BYTE*
const iend = ip + isize;
547 int ml, ml2, ml3, ml0;
562 if (!ml) { ip++;
continue; }
582 if (start2 < ip + ml0)
591 if ((start2 - ip) < 3)
608 if (ip+new_ml > start2 + ml2 -
MINMATCH) new_ml = (int)(start2 - ip) + ml2 -
MINMATCH;
609 correction = new_ml - (int)(start2 - ip);
612 start2 += correction;
619 if (start2 + ml2 < mflimit)
626 if (start2 < ip+ml) ml = (int)(start2 - ip);
634 if (start3 < ip+ml+3)
636 if (start3 >= (ip+ml))
640 int correction = (int)(ip+ml - start2);
641 start2 += correction;
673 if ((start2 - ip) < (
int)
ML_MASK)
677 if (ip + ml > start2 + ml2 -
MINMATCH) ml = (int)(start2 - ip) + ml2 -
MINMATCH;
678 correction = ml - (int)(start2 - ip);
681 start2 += correction;
688 ml = (int)(start2 - ip);
707 int lastRun = (int)(iend - anchor);
709 else *op++ = (lastRun<<
ML_BITS);
710 memcpy(op, anchor, iend - anchor);
715 return (
int) (((
char*)op)-dest);