static inline size_t lz4hc_commonlength(const u8 *p1, const u8 *p2, const u8 *const matchlimit) { const u8 *p1t = p1; while (p1t < matchlimit - (STEPSIZE - 1)) { #if LZ4_ARCH64 u64 diff = A64(p2) ^ A64(p1t); #else u32 diff = A32(p2) ^ A32(p1t); #endif if (!diff) { p1t += STEPSIZE; p2 += STEPSIZE; continue; } p1t += LZ4_NBCOMMONBYTES(diff); return p1t - p1; } #if LZ4_ARCH64 if ((p1t < (matchlimit-3)) && (A32(p2) == A32(p1t))) { p1t += 4; p2 += 4; } #endif if ((p1t < (matchlimit - 1)) && (A16(p2) == A16(p1t))) { p1t += 2; p2 += 2; } if ((p1t < matchlimit) && (*p2 == *p1t)) p1t++; return p1t - p1; }
static inline U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align) { if (align==XXH_unaligned) return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr)); else return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr); }
static inline int lz4hc_insertandgetwidermatch(struct lz4hc_data *hc4, const u8 *ip, const u8 *startlimit, const u8 *matchlimit, int longest, const u8 **matchpos, const u8 **startpos) { u16 *const chaintable = hc4->chaintable; HTYPE *const hashtable = hc4->hashtable; #if LZ4_ARCH64 const BYTE * const base = hc4->base; #else const int base = 0; #endif const u8 *ref; int nbattempts = MAX_NB_ATTEMPTS; int delta = (int)(ip - startlimit); /* First Match */ lz4hc_insert(hc4, ip); ref = hashtable[HASH_VALUE(ip)] + base; while ((ref >= ip - MAX_DISTANCE) && (ref >= hc4->base) && (nbattempts)) { nbattempts--; if (*(startlimit + longest) == *(ref - delta + longest)) { if (A32(ref) == A32(ip)) { const u8 *reft = ref + MINMATCH; const u8 *ipt = ip + MINMATCH; const u8 *startt = ip; while (ipt < matchlimit-(STEPSIZE - 1)) { #if LZ4_ARCH64 u64 diff = A64(reft) ^ A64(ipt); #else u32 diff = A32(reft) ^ A32(ipt); #endif if (!diff) { ipt += STEPSIZE; reft += STEPSIZE; continue; } ipt += LZ4_NBCOMMONBYTES(diff); goto _endcount; } #if LZ4_ARCH64 if ((ipt < (matchlimit - 3)) && (A32(reft) == A32(ipt))) { ipt += 4; reft += 4; } ipt += 2; #endif if ((ipt < (matchlimit - 1)) && (A16(reft) == A16(ipt))) { reft += 2; } if ((ipt < matchlimit) && (*reft == *ipt)) ipt++; _endcount: reft = ref; while ((startt > startlimit) && (reft > hc4->base) && (startt[-1] == reft[-1])) { startt--; reft--; } if ((ipt - startt) > longest) { longest = (int)(ipt - startt); *matchpos = reft; *startpos = startt; } } } ref -= (size_t)chaintable[(size_t)(ref) & MAXD_MASK]; } return longest; }
static inline int lz4hc_insertandfindbestmatch(struct lz4hc_data *hc4, const u8 *ip, const u8 *const matchlimit, const u8 **matchpos) { u16 *const chaintable = hc4->chaintable; HTYPE *const hashtable = hc4->hashtable; const u8 *ref; #if LZ4_ARCH64 const BYTE * const base = hc4->base; #else const int base = 0; #endif int nbattempts = MAX_NB_ATTEMPTS; size_t repl = 0, ml = 0; u16 delta; /* HC4 match finder */ lz4hc_insert(hc4, ip); ref = hashtable[HASH_VALUE(ip)] + base; /* potential repetition */ if (ref >= ip-4) { /* confirmed */ if (A32(ref) == A32(ip)) { delta = (u16)(ip-ref); repl = ml = lz4hc_commonlength(ip + MINMATCH, ref + MINMATCH, matchlimit) + MINMATCH; *matchpos = ref; } ref -= (size_t)chaintable[(size_t)(ref) & MAXD_MASK]; } while ((ref >= ip - MAX_DISTANCE) && nbattempts) { nbattempts--; if (*(ref + ml) == *(ip + ml)) { if (A32(ref) == A32(ip)) { size_t mlt = lz4hc_commonlength(ip + MINMATCH, ref + MINMATCH, matchlimit) + MINMATCH; if (mlt > ml) { ml = mlt; *matchpos = ref; } } } ref -= (size_t)chaintable[(size_t)(ref) & MAXD_MASK]; } /* Complete table */ if (repl) { const BYTE *ptr = ip; const BYTE *end; end = ip + repl - (MINMATCH-1); /* Pre-Load */ while (ptr < end - delta) { chaintable[(size_t)(ptr) & MAXD_MASK] = delta; ptr++; } do { chaintable[(size_t)(ptr) & MAXD_MASK] = delta; /* Head of chain */ hashtable[HASH_VALUE(ptr)] = (ptr) - base; ptr++; } while (ptr < end); hc4->nexttoupdate = end; } return (int)ml; }