22#ifdef ZXC_FUNCTION_SUFFIX
23#define ZXC_CAT_IMPL(x, y) x##y
24#define ZXC_CAT(x, y) ZXC_CAT_IMPL(x, y)
25#define zxc_compress_chunk_wrapper ZXC_CAT(zxc_compress_chunk_wrapper, ZXC_FUNCTION_SUFFIX)
26#define zxc_huf_build_code_lengths ZXC_CAT(zxc_huf_build_code_lengths, ZXC_FUNCTION_SUFFIX)
27#define zxc_huf_unpack_lengths ZXC_CAT(zxc_huf_unpack_lengths, ZXC_FUNCTION_SUFFIX)
28#define zxc_huf_calc_size ZXC_CAT(zxc_huf_calc_size, ZXC_FUNCTION_SUFFIX)
29#define zxc_huf_calc_size_dict ZXC_CAT(zxc_huf_calc_size_dict, ZXC_FUNCTION_SUFFIX)
30#define zxc_huf_encode_section ZXC_CAT(zxc_huf_encode_section, ZXC_FUNCTION_SUFFIX)
31#define zxc_huf_encode_section_dict ZXC_CAT(zxc_huf_encode_section_dict, ZXC_FUNCTION_SUFFIX)
47 const uint64_t v5 = val & 0xFFFFFFFFFFULL;
50 const uint64_t v4 = val ^ (val >> 15);
62 return (uint8_t)(val4 ^ (val4 >> 16));
72 const uint32_t epoch_mark) {
73 return ((head & ~offset_mask) == epoch_mark) ? (head & offset_mask) : 0;
83 const uint32_t dist = cur_pos - prev_idx;
86 return (uint16_t)(dist & valid);
98#if defined(ZXC_USE_AVX512)
101#define ZXC_RLE_CTZ(x) zxc_ctz64(x)
102typedef uint64_t zxc_rle_mask_t;
104 return (uint64_t)_mm512_cmpeq_epi8_mask(_mm512_loadu_si512((
const void*)p),
105 _mm512_loadu_si512((
const void*)(p + 1)));
107#elif defined(ZXC_USE_AVX2)
108#define ZXC_RLE_WIN 32
110#define ZXC_RLE_CTZ(x) zxc_ctz32(x)
111typedef uint32_t zxc_rle_mask_t;
113 const __m256i v0 = _mm256_loadu_si256((
const __m256i*)p);
114 const __m256i v1 = _mm256_loadu_si256((
const __m256i*)(p + 1));
115 return (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v0, v1));
117#elif defined(ZXC_USE_SSE2)
118#define ZXC_RLE_WIN 16
120#define ZXC_RLE_CTZ(x) zxc_ctz32(x)
121typedef uint32_t zxc_rle_mask_t;
123 const __m128i v0 = _mm_loadu_si128((
const __m128i*)p);
124 const __m128i v1 = _mm_loadu_si128((
const __m128i*)(p + 1));
125 return (uint32_t)_mm_movemask_epi8(_mm_cmpeq_epi8(v0, v1));
127#elif defined(ZXC_USE_NEON64)
128#define ZXC_RLE_WIN 16
130#define ZXC_RLE_CTZ(x) zxc_ctz64(x)
131typedef uint64_t zxc_rle_mask_t;
133 const uint8x16_t eq = vceqq_u8(vld1q_u8(p), vld1q_u8(p + 1));
134 return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(eq), 4)), 0);
139#if defined(ZXC_USE_SSE2) && !(defined(ZXC_USE_AVX512) && defined(__AVX512VL__)) && \
140 !defined(ZXC_USE_AVX2) && !defined(ZXC_USE_NEON64) && !defined(ZXC_USE_NEON32)
144#if defined(ZXC_OPT_SSE2)
157static ZXC_ALWAYS_INLINE __m128i zxc_mm_blendv_epi8_sse2(__m128i a, __m128i b, __m128i mask) {
158 return _mm_or_si128(_mm_and_si128(mask, b), _mm_andnot_si128(mask, a));
174 const __m128i bias32 = _mm_set1_epi32(0x8000);
175 const __m128i bias16 = _mm_set1_epi16((
short)0x8000);
176 const __m128i pa = _mm_sub_epi32(a, bias32);
177 const __m128i pb = _mm_sub_epi32(b, bias32);
178 return _mm_add_epi16(_mm_packs_epi32(pa, pb), bias16);
207 if (LIKELY(val < ZXC_VARINT_1B_LIMIT)) {
208 dst[0] = (uint8_t)val;
213 if (LIKELY(val < ZXC_VARINT_2B_LIMIT)) {
214 dst[0] = (uint8_t)(0x80 | (val & 0x3F));
215 dst[1] = (uint8_t)(val >> 6);
221 dst[0] = (uint8_t)(0xC0 | (val & 0x1F));
222 dst[1] = (uint8_t)(val >> 5);
223 dst[2] = (uint8_t)(val >> 13);
234 const uint32_t val,
const uint32_t mask) {
235 if (LIKELY(val < mask))
return 1;
237 if (UNLIKELY(n == 0))
return 0;
259 uint16_t* RESTRICT buf_offsets,
260 uint8_t* RESTRICT buf_extras, uint32_t* RESTRICT seq_c,
261 size_t* RESTRICT extras_sz,
262 uint16_t* RESTRICT max_offset,
const uint32_t ll,
263 const uint32_t ml,
const uint16_t off_biased) {
267 buf_offsets[*seq_c] = off_biased;
268 if (off_biased > *max_offset) *max_offset = off_biased;
278 size_t* RESTRICT r,
const uint8_t* to) {
279 while (extras + *r < to) extras[(*w)++] = extras[(*r)++];
284 size_t* RESTRICT r,
const uint8_t* end) {
285 const uint8_t* p = extras + *r;
315 uint8_t* RESTRICT ctx_bad, uint8_t* RESTRICT side) {
317 while (order > 2 && (n_seq >> order) < 16) order--;
318 const uint32_t hmask = (1U << order) - 1U;
320 for (uint32_t l = 0; l < 4; l++) {
321 ZXC_MEMSET(ctx_hist[l][0], 0, (hmask + 1) *
sizeof(uint32_t));
322 ZXC_MEMSET(ctx_hist[l][1], 0, (hmask + 1) *
sizeof(uint32_t));
324 uint32_t hist = 0, n_esc = 0;
325 for (uint32_t i = 0; i < n_seq; i++) {
327 side[i] = (uint8_t)hist;
328 ctx_hist[i & 3][esc][hist]++;
329 hist = ((hist << 1) | esc) & hmask;
334 for (uint32_t c = 0; c <= hmask; c++) {
335 uint32_t n0 = 0, n1 = 0;
336 for (uint32_t l = 0; l < 4; l++) {
337 n0 += ctx_hist[l][0][c];
338 n1 += ctx_hist[l][1][c];
340 ctx_bad[c] = (n1 < n0);
341 errors += (n0 < n1) ? n0 : n1;
375 uint8_t* RESTRICT extras,
size_t* RESTRICT extras_sz,
376 uint8_t* RESTRICT side,
378 const uint32_t n_seq,
const uint8_t cap) {
385 const uint8_t*
const extras_end = extras + *extras_sz;
388 for (uint32_t i = 0; i < n_seq; i++) {
389 const uint8_t tok = tokens[i];
390 const int bad = ctx_bad[side[i]];
395 const uint8_t* p = extras + r;
397 if (bad && code <= cap) {
398 side[i] = (uint8_t)code;
400 r = (size_t)(p - extras);
406 if (extra == 0)
return n_seq;
410 uint32_t out = n_seq + extra;
411 for (uint32_t i = n_seq; i-- > 0;) {
412 const uint8_t tok = tokens[i];
413 const uint16_t off = offsets[i];
420 uint32_t code = side[i];
426 tokens[k] = (uint8_t)(ll_bits | p);
431 tokens[k] = (uint8_t)(ll_bits | code);
434 return n_seq + extra;
444 const uint8_t* anchor,
const uint32_t ll,
445 const uint8_t* iend) {
451 ZXC_MEMCPY(literals + *lit_c, anchor, ll);
486 uint32_t from,
const uint32_t need,
487 const uint8_t* iend) {
488 while (from < need) {
489 if (UNLIKELY((
size_t)(iend - cur) - from <
sizeof(uint64_t))) {
490 while (from < need && cur[from] == ref[from]) from++;
494 if (d != 0)
return from + (uint32_t)(
zxc_ctz64(d) >> 3) >= need;
495 from +=
sizeof(uint64_t);
508 const uint8_t* iend,
const uint32_t need,
509 const uint32_t* RESTRICT hash_table,
510 const uint8_t* RESTRICT hash_tags,
511 const uint16_t* RESTRICT chain_table,
512 const uint32_t epoch_mark,
const uint32_t offset_mask,
514 if (need > (
size_t)(iend - lp))
return 0;
517 const uint32_t val = (uint32_t)val8;
519 const uint32_t pos = (uint32_t)(lp - src);
520 const uint32_t gate =
zxc_le32(lp + need - 4);
521 uint32_t idx =
zxc_epoch_pos(hash_table[h], offset_mask, epoch_mark);
527 const uint8_t*
const ref = src + idx;
536 if (UNLIKELY(delta == 0))
break;
569 const uint8_t* src,
const uint8_t* ip,
const uint8_t* iend,
const uint8_t* search_limit,
570 const uint8_t* anchor, uint32_t* RESTRICT hash_table, uint8_t* RESTRICT hash_tags,
571 uint16_t* RESTRICT chain_table,
const uint32_t epoch_mark,
const uint32_t offset_mask,
578 uint32_t cur_val = (uint32_t)cur_val8;
584 const uint32_t cur_pos = (uint32_t)(ip - src);
587 const uint8_t stored_tag = hash_tags[h];
592 match_idx =
zxc_epoch_pos(hash_table[h], offset_mask, epoch_mark);
599 const int skip_head = (match_idx != 0) & (stored_tag != cur_tag);
602 hash_table[h] = epoch_mark | cur_pos;
603 hash_tags[h] = cur_tag;
614 last_off <= cur_pos) {
615 const uint8_t*
const rep_ref = src + (cur_pos - last_off);
617 uint32_t mlen =
sizeof(uint32_t);
618 const uint8_t*
const limit_8 = iend -
sizeof(uint64_t);
619 while (ip + mlen < limit_8) {
622 mlen +=
sizeof(uint64_t);
624 mlen += (uint32_t)(
zxc_ctz64(diff) >> 3);
628 while (ip + mlen < iend && rep_ref[mlen] == ip[mlen]) mlen++;
632 if (UNLIKELY(best.len >= (uint32_t)p.
sufficient_len || ip + best.len >= iend))
633 goto _finalize_match;
637 if (match_idx == 0)
goto _finalize_match;
643 const uint32_t next_idx = match_idx - delta;
644 match_idx = (delta != 0) ? next_idx : 0;
648 while (match_idx > 0) {
649 if (UNLIKELY(attempts-- < 0 || cur_pos - match_idx >
ZXC_LZ_MAX_DIST))
break;
650 const uint8_t* ref = src + match_idx;
656 const uint32_t next_idx = match_idx - delta;
657 ZXC_PREFETCH_READ(src + next_idx + best.len - 3);
661 const int should_compare = (cur_pos - match_idx) >= p.
min_offset &&
665 if (should_compare) {
666 uint32_t mlen =
sizeof(uint32_t);
670 const uint8_t* limit_8 = iend -
sizeof(uint64_t);
671 const uint8_t* scalar_limit = ip + mlen + 64;
672 if (scalar_limit > limit_8) scalar_limit = limit_8;
674 while (ip + mlen < scalar_limit) {
677 mlen +=
sizeof(uint64_t);
680 goto _match_len_done;
685#if defined(ZXC_USE_AVX512)
686 const uint8_t* limit_64 = iend - 64;
687 while (ip + mlen < limit_64) {
688 const __m512i v_src = _mm512_loadu_si512((
const void*)(ip + mlen));
689 const __m512i v_ref = _mm512_loadu_si512((
const void*)(ref + mlen));
690 const __mmask64 mask = _mm512_cmpeq_epi8_mask(v_src, v_ref);
691 if (mask == 0xFFFFFFFFFFFFFFFF)
695 goto _match_len_done;
698#elif defined(ZXC_USE_AVX2)
699 const uint8_t* limit_32 = iend - 32;
700 while (ip + mlen < limit_32) {
701 const __m256i v_src = _mm256_loadu_si256((
const __m256i*)(ip + mlen));
702 const __m256i v_ref = _mm256_loadu_si256((
const __m256i*)(ref + mlen));
703 const __m256i v_cmp = _mm256_cmpeq_epi8(v_src, v_ref);
704 const uint32_t mask = (uint32_t)_mm256_movemask_epi8(v_cmp);
705 if (mask == 0xFFFFFFFF)
709 goto _match_len_done;
712#elif defined(ZXC_USE_SSE2)
713 const uint8_t* limit_16 = iend - 16;
714 while (ip + mlen < limit_16) {
715 const __m128i v_src = _mm_loadu_si128((
const __m128i*)(ip + mlen));
716 const __m128i v_ref = _mm_loadu_si128((
const __m128i*)(ref + mlen));
717 const __m128i v_cmp = _mm_cmpeq_epi8(v_src, v_ref);
718 const uint32_t mask = (uint32_t)_mm_movemask_epi8(v_cmp);
725 goto _match_len_done;
728#elif defined(ZXC_USE_NEON64)
730 const uint8_t* limit_32 = iend - 32;
731 while (ip + mlen < limit_32) {
732 const uint8x16_t s0 = vld1q_u8(ip + mlen);
733 const uint8x16_t r0 = vld1q_u8(ref + mlen);
734 const uint8x16_t c0 = vceqq_u8(s0, r0);
735 const uint64_t m0 = vget_lane_u64(
736 vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(c0), 4)), 0);
737 if (UNLIKELY(m0 != ~(uint64_t)0)) {
739 goto _match_len_done;
741 const uint8x16_t s1 = vld1q_u8(ip + mlen + 16);
742 const uint8x16_t r1 = vld1q_u8(ref + mlen + 16);
743 const uint8x16_t c1 = vceqq_u8(s1, r1);
744 const uint64_t m1 = vget_lane_u64(
745 vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(c1), 4)), 0);
746 if (UNLIKELY(m1 != ~(uint64_t)0)) {
747 mlen += 16 + (uint32_t)(
zxc_ctz64(~m1) >> 2);
748 goto _match_len_done;
752 if (ip + mlen < iend - 16) {
753 const uint8x16_t v_src = vld1q_u8(ip + mlen);
754 const uint8x16_t v_ref = vld1q_u8(ref + mlen);
755 const uint8x16_t v_cmp = vceqq_u8(v_src, v_ref);
756 const uint64_t mask = vget_lane_u64(
757 vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(v_cmp), 4)), 0);
758 if (LIKELY(mask == ~(uint64_t)0))
761 mlen += (uint32_t)(
zxc_ctz64(~mask) >> 2);
762 goto _match_len_done;
766#elif defined(ZXC_USE_NEON32)
768 const uint8_t* limit_16 = iend - 16;
769 while (ip + mlen < limit_16) {
770 const uint8x16_t v_src = vld1q_u8(ip + mlen);
771 const uint8x16_t v_ref = vld1q_u8(ref + mlen);
772 const uint8x16_t v_cmp = vceqq_u8(v_src, v_ref);
773 uint8x8_t p1 = vpmin_u8(vget_low_u8(v_cmp), vget_high_u8(v_cmp));
774 uint8x8_t p2 = vpmin_u8(p1, p1);
775 uint8x8_t p3 = vpmin_u8(p2, p2);
776 uint8x8_t p4 = vpmin_u8(p3, p3);
777 uint8_t min_val = vget_lane_u8(p4, 0);
781 uint8x16_t v_diff = vmvnq_u8(v_cmp);
783 (uint64_t)vgetq_lane_u32(vreinterpretq_u32_u8(v_diff), 0) |
784 ((uint64_t)vgetq_lane_u32(vreinterpretq_u32_u8(v_diff), 1) << 32);
790 (uint64_t)vgetq_lane_u32(vreinterpretq_u32_u8(v_diff), 2) |
791 ((uint64_t)vgetq_lane_u32(vreinterpretq_u32_u8(v_diff), 3)
794 goto _match_len_done;
799 while (ip + mlen < limit_8) {
802 mlen +=
sizeof(uint64_t);
805 goto _match_len_done;
808 while (ip + mlen < iend && ref[mlen] == ip[mlen]) mlen++;
811 const int better = (mlen > best.len);
812 best.len = better ? mlen : best.len;
813 best.ref = better ? ref : best.ref;
815 if (UNLIKELY(best.len >= (uint32_t)p.
sufficient_len || ip + best.len >= iend))
break;
818 match_idx = (delta != 0) ? next_idx : 0;
824 const uint8_t* b_ip = ip;
825 const uint8_t* b_ref = best.ref;
826 while (b_ip > anchor && b_ref > src && b_ip[-1] == b_ref[-1]) {
838 ip + 1 < search_limit) {
839 if (
zxc_lazy_probe(src, ip + 1, iend, best.len + 2, hash_table, hash_tags, chain_table,
840 epoch_mark, offset_mask, use_hash5, p) ||
842 zxc_lazy_probe(src, ip + 2, iend, best.len + 3, hash_table, hash_tags, chain_table,
843 epoch_mark, offset_mask, use_hash5, p)))
856 uint16_t* RESTRICT parent_off,
const size_t p,
857 const size_t L,
const uint32_t nxt,
858 const uint16_t off_biased) {
859 if (nxt < dp[p + L]) {
861 parent_len[p + L] = (uint16_t)L;
862 parent_off[p + L] = off_biased;
907 uint32_t* RESTRICT dp, uint16_t* RESTRICT parent_len, uint16_t* RESTRICT parent_off,
908 const size_t p,
size_t L,
const size_t L_end,
const uint32_t nxt,
const uint16_t off_biased) {
909#if defined(ZXC_USE_AVX512) && defined(__AVX512VL__)
910 if (L + 16 <= L_end) {
911 const __m512i v_inc =
912 _mm512_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
913 const __m512i v_nxt = _mm512_set1_epi32((
int)nxt);
914 const __m256i v_off = _mm256_set1_epi16((int16_t)off_biased);
915 for (; L + 16 <= L_end; L += 16) {
916 const __m512i v_L_lanes = _mm512_add_epi32(v_inc, _mm512_set1_epi32((
int)L));
917 const __m512i v_dp = _mm512_loadu_si512((
const void*)&dp[p + L]);
918 const __mmask16 m = _mm512_cmplt_epu32_mask(v_nxt, v_dp);
919 _mm512_mask_storeu_epi32(&dp[p + L], m, v_nxt);
920 const __m256i v_L_u16 = _mm512_cvtusepi32_epi16(v_L_lanes);
921 _mm256_mask_storeu_epi16((
void*)&parent_len[p + L], m, v_L_u16);
922 _mm256_mask_storeu_epi16((
void*)&parent_off[p + L], m, v_off);
925#elif defined(ZXC_USE_AVX2)
926 if (L + 8 <= L_end) {
927 const __m256i v_inc = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
928 const __m256i v_nxt = _mm256_set1_epi32((
int)nxt);
929 const __m256i v_bias = _mm256_set1_epi32((
int)0x80000000);
930 const __m256i v_nxt_b = _mm256_xor_si256(v_nxt, v_bias);
931 const __m128i v_off = _mm_set1_epi16((int16_t)off_biased);
932 for (; L + 8 <= L_end; L += 8) {
933 const __m256i v_L_lanes = _mm256_add_epi32(v_inc, _mm256_set1_epi32((
int)L));
934 const __m256i v_dp = _mm256_loadu_si256((
const __m256i*)&dp[p + L]);
939 const __m256i v_dp_b = _mm256_xor_si256(v_dp, v_bias);
940 const __m256i v_mask = _mm256_cmpgt_epi32(v_dp_b, v_nxt_b);
941 const __m256i v_dp_new = _mm256_blendv_epi8(v_dp, v_nxt, v_mask);
942 _mm256_storeu_si256((__m256i*)&dp[p + L], v_dp_new);
945 const __m128i v_mask16 = _mm_packs_epi32(_mm256_castsi256_si128(v_mask),
946 _mm256_extracti128_si256(v_mask, 1));
947 const __m128i v_L_u16 = _mm_packus_epi32(_mm256_castsi256_si128(v_L_lanes),
948 _mm256_extracti128_si256(v_L_lanes, 1));
949 const __m128i v_pl = _mm_loadu_si128((
const __m128i*)&parent_len[p + L]);
950 const __m128i v_pl_new = _mm_blendv_epi8(v_pl, v_L_u16, v_mask16);
951 _mm_storeu_si128((__m128i*)&parent_len[p + L], v_pl_new);
952 const __m128i v_po = _mm_loadu_si128((
const __m128i*)&parent_off[p + L]);
953 const __m128i v_po_new = _mm_blendv_epi8(v_po, v_off, v_mask16);
954 _mm_storeu_si128((__m128i*)&parent_off[p + L], v_po_new);
957#elif defined(ZXC_USE_NEON64) || defined(ZXC_USE_NEON32)
958 if (L + 4 <= L_end) {
959 static const uint32_t k_inc_array[4] = {0, 1, 2, 3};
960 const uint32x4_t v_inc = vld1q_u32(k_inc_array);
961 const uint32x4_t v_nxt = vdupq_n_u32(nxt);
962 const uint16x4_t v_off = vdup_n_u16(off_biased);
963 for (; L + 4 <= L_end; L += 4) {
964 const uint32x4_t v_L_lanes = vaddq_u32(v_inc, vdupq_n_u32((uint32_t)L));
965 const uint32x4_t v_dp = vld1q_u32(&dp[p + L]);
966 const uint32x4_t v_mask = vcgtq_u32(v_dp, v_nxt);
967 vst1q_u32(&dp[p + L], vbslq_u32(v_mask, v_nxt, v_dp));
968 const uint16x4_t v_mask16 = vmovn_u32(v_mask);
969 const uint16x4_t v_L_u16 = vqmovn_u32(v_L_lanes);
970 const uint16x4_t v_pl = vld1_u16(&parent_len[p + L]);
971 vst1_u16(&parent_len[p + L], vbsl_u16(v_mask16, v_L_u16, v_pl));
972 const uint16x4_t v_po = vld1_u16(&parent_off[p + L]);
973 vst1_u16(&parent_off[p + L], vbsl_u16(v_mask16, v_off, v_po));
976#elif defined(ZXC_OPT_SSE2)
977 if (L + 4 <= L_end) {
978 const __m128i v_inc = _mm_setr_epi32(0, 1, 2, 3);
979 const __m128i v_nxt = _mm_set1_epi32((
int)nxt);
980 const __m128i v_bias = _mm_set1_epi32((
int)0x80000000);
981 const __m128i v_nxt_b = _mm_xor_si128(v_nxt, v_bias);
982 const __m128i v_off = _mm_set1_epi16((
short)off_biased);
983 for (; L + 4 <= L_end; L += 4) {
984 const __m128i v_L_lanes = _mm_add_epi32(v_inc, _mm_set1_epi32((
int)L));
985 const __m128i v_dp = _mm_loadu_si128((
const __m128i*)&dp[p + L]);
988 const __m128i v_dp_b = _mm_xor_si128(v_dp, v_bias);
989 const __m128i v_mask = _mm_cmpgt_epi32(v_dp_b, v_nxt_b);
990 const __m128i v_dp_new = zxc_mm_blendv_epi8_sse2(v_dp, v_nxt, v_mask);
991 _mm_storeu_si128((__m128i*)&dp[p + L], v_dp_new);
994 const __m128i v_mask16 = _mm_packs_epi32(v_mask, v_mask);
995 const __m128i v_L_u16 = zxc_mm_packus_epi32_sse2(v_L_lanes, v_L_lanes);
996 __m128i v_pl = _mm_loadl_epi64((
const __m128i*)&parent_len[p + L]);
997 v_pl = zxc_mm_blendv_epi8_sse2(v_pl, v_L_u16, v_mask16);
998 _mm_storel_epi64((__m128i*)&parent_len[p + L], v_pl);
999 __m128i v_po = _mm_loadl_epi64((
const __m128i*)&parent_off[p + L]);
1000 v_po = zxc_mm_blendv_epi8_sse2(v_po, v_off, v_mask16);
1001 _mm_storel_epi64((__m128i*)&parent_off[p + L], v_po);
1007 for (; L < L_end; L++)
zxc_opt_relax(dp, parent_len, parent_off, p, L, nxt, off_biased);
1032 void* RESTRICT scratch) {
1039 for (
size_t i = 0; i < src_sz; i += step) {
1052 uint32_t total_bits = 0;
1054 total_bits += hist[k] * (uint32_t)code_len[k];
1056 const uint32_t n = (uint32_t)sampled;
1057 const uint32_t avg = (total_bits + n - 1) / n;
1061 return (avg < CHAR_BIT) ? avg : CHAR_BIT;
1110 const size_t src_sz, uint32_t* RESTRICT hash_table,
1111 uint8_t* RESTRICT hash_tags, uint16_t* RESTRICT chain_table,
1112 const uint32_t epoch_mark,
const uint32_t offset_mask,
1113 const int level, uint8_t* RESTRICT literals,
1114 uint8_t* RESTRICT buf_tokens, uint16_t* RESTRICT buf_offsets,
1115 uint8_t* RESTRICT buf_extras, uint32_t* RESTRICT seq_c_out,
1116 size_t* RESTRICT lit_c_out,
size_t* RESTRICT extras_sz_out,
1117 uint16_t* RESTRICT max_offset_out) {
1124 const size_t dict_sz = ctx->dict_size;
1125 const size_t block_sz = src_sz - dict_sz;
1126 const uint8_t*
const src_base = src + dict_sz;
1127 const uint8_t*
const iend = src + src_sz;
1131 if (block_sz > 0) ZXC_MEMCPY(literals, src_base, block_sz);
1132 *lit_c_out = block_sz;
1135 *max_offset_out = 0;
1140 const uint8_t*
const search_limit = src + search_limit_pos;
1165 uint32_t*
const dp = (uint32_t*)ctx->opt_scratch;
1166 uint16_t*
const parent_len = (uint16_t*)(ctx->opt_scratch + sz_dp);
1167 uint16_t*
const parent_off = (uint16_t*)(ctx->opt_scratch + sz_dp + sz_pl);
1168 uint64_t*
const match_end_bits = (uint64_t*)(ctx->opt_scratch + sz_dp + sz_pl + sz_po);
1171 ZXC_MEMSET(dp + 1, 0xFF, block_sz *
sizeof(uint32_t));
1172 ZXC_MEMSET(match_end_bits, 0, sz_bm);
1178 size_t skip_until = 0;
1180 uint32_t last_off = 0;
1183 size_t prev_len = 0;
1184 for (
size_t p = 0; p < search_limit_pos; p++) {
1185 if (UNLIKELY(dp[p] == UINT32_MAX))
continue;
1188 const uint32_t lit_next = dp[p] + lit_cost;
1189 if (lit_next < dp[p + 1]) {
1190 dp[p + 1] = lit_next;
1191 parent_len[p + 1] = 0;
1194 if (p < skip_until)
continue;
1200 const uint8_t* ip = src_base + p;
1202 src, ip, iend, search_limit, ip, hash_table, hash_tags, chain_table,
1203 epoch_mark, offset_mask, level, lzp_opt, last_off);
1206 const uint32_t off = (uint32_t)(ip - m.ref);
1209 const size_t L_max_raw = (m.len > block_sz - p) ? (block_sz - p) : (size_t)m.len;
1210 const size_t L_max = (L_max_raw > UINT16_MAX) ? UINT16_MAX : L_max_raw;
1220 const size_t L_max_plus = L_max + 1;
1222 const size_t L_v1_end = L_cheap_end + ZXC_VARINT_1B_LIMIT;
1224 const uint32_t nxt_v1 = nxt_cheap + CHAR_BIT;
1231 const int covered = prev_len > L_max && prev_p + 1 == p && dp[p] >= dp[p - 1];
1236 if (L_max_plus >= L_cheap_end)
1237 zxc_opt_relax(dp, parent_len, parent_off, p, L_cheap_end - 1, nxt_cheap,
1239 if (L_max_plus >= L_v1_end)
1240 zxc_opt_relax(dp, parent_len, parent_off, p, L_v1_end - 1, nxt_v1,
1246 (L_max_plus < L_cheap_end) ? L_max_plus : L_cheap_end, nxt_cheap,
1252 dp, parent_len, parent_off, p, L,
1253 (L_max_plus < L_v1_end) ? L_max_plus : L_v1_end, nxt_v1, off_biased);
1258 for (; L < L_max_plus; L++) {
1266 zxc_opt_relax(dp, parent_len, parent_off, p, L, dp[p] + cost, off_biased);
1276 for (
size_t p = search_limit_pos; p < block_sz; p++) {
1277 if (UNLIKELY(dp[p] == UINT32_MAX))
continue;
1278 const uint32_t lit_next = dp[p] + lit_cost;
1279 if (lit_next < dp[p + 1]) {
1280 dp[p + 1] = lit_next;
1281 parent_len[p + 1] = 0;
1289 size_t pos = block_sz;
1291 const uint32_t L = parent_len[pos];
1295 match_end_bits[pos >> 6] |= (uint64_t)1 << (pos & 63);
1306 size_t extras_sz = 0;
1307 uint16_t max_offset = 0;
1308 size_t lit_start = 0;
1310 for (
size_t word_idx = 0; word_idx < n_bm_words; word_idx++) {
1311 uint64_t w = match_end_bits[word_idx];
1313 const size_t pos = (word_idx << 6) + (
size_t)
zxc_ctz64(w);
1315 const uint32_t L = parent_len[pos];
1316 const uint16_t off_biased = parent_off[pos];
1317 const size_t match_start = pos - L;
1319 const size_t LL = match_start - lit_start;
1321 ZXC_MEMCPY(literals + lit_c, src_base + lit_start, LL);
1324 const uint32_t ll = (uint32_t)LL;
1326 if (UNLIKELY(!
zxc_spl_put_seq(buf_tokens, buf_offsets, buf_extras, &seq_c, &extras_sz,
1327 &max_offset, ll, ml, off_biased)))
1334 if (lit_start < block_sz) {
1335 const size_t tail = block_sz - lit_start;
1336 ZXC_MEMCPY(literals + lit_c, src_base + lit_start, tail);
1342 *extras_sz_out = extras_sz;
1343 *max_offset_out = max_offset;
1364 uint32_t* RESTRICT hash_table, uint8_t* RESTRICT hash_tags,
1365 uint16_t* RESTRICT chain_table,
const uint32_t epoch_mark,
1366 const uint32_t offset_mask,
const int level) {
1375 const size_t half = limit / 2;
1376 for (
size_t i = 0; i < half; i += 4) {
1377 const uint64_t val8 =
zxc_le64(src + i);
1379 const uint32_t cur_pos = (uint32_t)i;
1381 hash_table[h] = epoch_mark | cur_pos;
1385 for (
size_t i = half; i < limit; i++) {
1386 const uint64_t val8 =
zxc_le64(src + i);
1388 const uint32_t cur_pos = (uint32_t)i;
1389 const uint32_t prev_idx =
zxc_epoch_pos(hash_table[h], offset_mask, epoch_mark);
1391 hash_table[h] = epoch_mark | cur_pos;
1420 const size_t src_sz, uint8_t* RESTRICT dst,
size_t dst_cap,
1421 size_t* RESTRICT out_sz) {
1424 const int level = ctx->compression_level;
1425 const size_t dict_sz = ctx->dict_size;
1433 if (UNLIKELY(ctx->epoch >= ctx->max_epoch)) {
1438 const uint32_t offset_bits = ctx->offset_bits;
1439 const uint32_t offset_mask = ctx->offset_mask;
1440 const uint32_t epoch_mark = ctx->epoch << offset_bits;
1443 zxc_lz_seed_dict(src, dict_sz, ctx->hash_table, ctx->hash_tags, ctx->chain_table,
1444 epoch_mark, offset_mask, level);
1446 const uint8_t* ip = src + dict_sz;
1447 const uint8_t* iend = src + src_sz;
1448 const uint8_t* anchor = ip;
1451 uint32_t*
const hash_table = ctx->hash_table;
1452 uint8_t*
const hash_tags = ctx->hash_tags;
1453 uint16_t*
const chain_table = ctx->chain_table;
1454 uint8_t*
const literals = ctx->literals;
1455 uint8_t*
const buf_tokens = ctx->buf_tokens;
1456 uint16_t*
const buf_offsets = ctx->buf_offsets;
1457 uint8_t*
const buf_extras = ctx->buf_extras;
1461 size_t extras_sz = 0;
1462 uint16_t max_offset = 0;
1468 ctx, src, src_sz, hash_table, hash_tags, chain_table, epoch_mark, offset_mask, level,
1469 literals, buf_tokens, buf_offsets, buf_extras, &seq_c, &lit_c, &extras_sz, &max_offset);
1470 if (UNLIKELY(rc != 0))
return rc;
1474 while (LIKELY(ip < search_limit)) {
1475 const size_t dist = (size_t)(ip - anchor);
1477 if (UNLIKELY(ip + step >= search_limit)) step = 1;
1479 if (LIKELY(ip + step +
sizeof(uint64_t) <= iend)) {
1480 const uint64_t v_next =
zxc_le64(ip + step);
1483 ZXC_PREFETCH_READ(&hash_tags[h_next]);
1484 ZXC_PREFETCH_READ(&hash_table[h_next]);
1487 const zxc_match_t m =
1489 chain_table, epoch_mark, offset_mask, level, lzp,
1494 const uint32_t ll = (uint32_t)(ip - anchor);
1496 const uint32_t off = (uint32_t)(ip - m.ref);
1501 if (UNLIKELY(!
zxc_spl_put_seq(buf_tokens, buf_offsets, buf_extras, &seq_c, &extras_sz,
1502 &max_offset, ll, ml, off_biased)))
1506 const uint8_t* match_end = ip + m.len;
1507 if (match_end < iend - 7) {
1508 const uint32_t pos_u = (uint32_t)((match_end - 2) - src);
1509 const uint64_t val_u8 =
zxc_le64(match_end - 2);
1510 const uint32_t val_u = (uint32_t)val_u8;
1512 const uint32_t prev_idx =
1514 hash_table[h_u] = epoch_mark | pos_u;
1527 const size_t last_lits = (size_t)(iend - anchor);
1528 if (last_lits > 0) {
1529 ZXC_MEMCPY(literals + lit_c, anchor, last_lits);
1536 ctx->buf_split, ctx->buf_split_hist, seq_c, lzp.
split_max);
1539 if (UNLIKELY(ctx->lit_freq_acc)) {
1540 for (
size_t i = 0; i < lit_c; i++) ctx->lit_freq_acc[literals[i]]++;
1544 size_t rle_size = 0;
1545 int enc_lit = ZXC_SECTION_ENCODING_RAW;
1546 size_t best_j = lit_c;
1549 const uint8_t* p = literals;
1550 const uint8_t*
const p_end = literals + lit_c;
1551 const uint8_t*
const p_end_4 = p_end - 3;
1553 while (LIKELY(p < p_end)) {
1554 const uint8_t b = *p;
1555 const uint8_t* run_start = p++;
1558#if defined(ZXC_USE_AVX512)
1559 const __m512i vb = _mm512_set1_epi8((
char)b);
1560 while (p <= p_end - 64) {
1561 const __m512i v = _mm512_loadu_si512((
const void*)p);
1562 const __mmask64 mask = _mm512_cmpeq_epi8_mask(v, vb);
1563 if (mask != 0xFFFFFFFFFFFFFFFFULL) {
1569#elif defined(ZXC_USE_AVX2)
1570 const __m256i vb = _mm256_set1_epi8((
char)b);
1571 while (p <= p_end - 32) {
1572 const __m256i v = _mm256_loadu_si256((
const __m256i*)p);
1573 const uint32_t mask = (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, vb));
1574 if (mask != 0xFFFFFFFF) {
1580#elif defined(ZXC_USE_SSE2)
1581 const __m128i vb = _mm_set1_epi8((
char)b);
1582 while (p <= p_end - 16) {
1583 const __m128i v = _mm_loadu_si128((
const __m128i*)p);
1584 const uint32_t mask = (uint32_t)_mm_movemask_epi8(_mm_cmpeq_epi8(v, vb));
1585 if (mask != 0xFFFFU) {
1591#elif defined(ZXC_USE_NEON64)
1592 const uint8x16_t vb = vdupq_n_u8(b);
1593 while (p <= p_end - 16) {
1594 const uint8x16_t v = vld1q_u8(p);
1595 const uint8x16_t eq = vceqq_u8(v, vb);
1597 const uint64_t mask =
1598 vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(eq), 4)), 0);
1599 if (LIKELY(mask == ~(uint64_t)0)) {
1606#elif defined(ZXC_USE_NEON32)
1607 uint8x16_t vb = vdupq_n_u8(b);
1608 while (p <= p_end - 16) {
1609 uint8x16_t v = vld1q_u8(p);
1610 uint8x16_t eq = vceqq_u8(v, vb);
1611 uint8x16_t not_eq = vmvnq_u8(eq);
1615 const uint32x4_t neq32 = vreinterpretq_u32_u8(not_eq);
1616 const uint32_t l0 = vgetq_lane_u32(neq32, 0);
1617 const uint32_t l1 = vgetq_lane_u32(neq32, 1);
1619 const uint64_t lo = ((uint64_t)l1 << 32) | l0;
1625 const uint32_t h0 = vgetq_lane_u32(neq32, 2);
1626 const uint32_t h1 = vgetq_lane_u32(neq32, 3);
1627 const uint64_t hi = ((uint64_t)h1 << 32) | h0;
1636 while (p < p_end && *p == b) p++;
1638#if defined(ZXC_USE_AVX512) || defined(ZXC_USE_AVX2) || defined(ZXC_USE_NEON64) || \
1639 defined(ZXC_USE_NEON32) || defined(ZXC_USE_SSE2)
1642 const size_t run = (size_t)(p - run_start);
1649 rle_size += full_chunks * 2;
1654 rle_size += 1 + rem;
1657 const uint8_t* lit_start = run_start;
1659#if defined(ZXC_RLE_WIN)
1660 while (p <= p_end_4 - ZXC_RLE_WIN) {
1661 const zxc_rle_mask_t m = zxc_rle_pair_mask(p);
1662 const zxc_rle_mask_t mask = m & (m >> ZXC_RLE_BPB) & (m >> (2 * ZXC_RLE_BPB));
1663 if (LIKELY(mask == 0)) {
1664 p += ZXC_RLE_WIN - 2;
1667 const unsigned rpos = (unsigned)(ZXC_RLE_CTZ(mask) / ZXC_RLE_BPB);
1668 const unsigned rpairs =
1669 (unsigned)(ZXC_RLE_CTZ(~(m >> (rpos * ZXC_RLE_BPB))) / ZXC_RLE_BPB);
1670 if (UNLIKELY(rpos + rpairs >= ZXC_RLE_WIN)) {
1674 const size_t seg = (size_t)(p + rpos - lit_start);
1677 p += rpos + rpairs + 1;
1680#elif defined(ZXC_USE_NEON32)
1681 while (p <= p_end_4 - 16) {
1682 uint8x16_t v0 = vld1q_u8(p);
1683 uint8x16_t v1 = vld1q_u8(p + 1);
1684 const uint8x16_t pe = vceqq_u8(v0, v1);
1685 const uint8x16_t zv = vdupq_n_u8(0);
1686 const uint8x16_t eq =
1687 vandq_u8(pe, vandq_u8(vextq_u8(pe, zv, 1), vextq_u8(pe, zv, 2)));
1689 uint32x4_t eq32 = vreinterpretq_u32_u8(eq);
1690 uint32_t l0 = vgetq_lane_u32(eq32, 0);
1691 uint32_t l1 = vgetq_lane_u32(eq32, 1);
1692 uint64_t lo = ((uint64_t)l1 << 32) | l0;
1699 uint32_t h0 = vgetq_lane_u32(eq32, 2);
1700 uint32_t h1 = vgetq_lane_u32(eq32, 3);
1701 uint64_t hi = ((uint64_t)h1 << 32) | h0;
1710 while (p < p_end_4) {
1711 if (UNLIKELY(p[0] == p[1] && p[1] == p[2] && p[2] == p[3]))
break;
1714 if (p >= p_end_4) p = p_end;
1716#if defined(ZXC_USE_AVX512) || defined(ZXC_USE_AVX2) || defined(ZXC_USE_NEON64) || \
1717 defined(ZXC_USE_NEON32) || defined(ZXC_USE_SSE2)
1720 const size_t lit_run = (size_t)(p - lit_start);
1729 if (rle_j < best_j) {
1730 enc_lit = ZXC_SECTION_ENCODING_RLE;
1739 size_t huf_total_size = SIZE_MAX;
1748 for (; i + 4 <= lit_c; i += 4) {
1749 freq0[literals[i + 0]]++;
1750 freq1[literals[i + 1]]++;
1751 freq2[literals[i + 2]]++;
1752 freq3[literals[i + 3]]++;
1754 for (; i < lit_c; i++) freq0[literals[i]]++;
1756 uint32_t*
const freq = lit_freq;
1758 freq[k] = freq0[k] + freq1[k] + freq2[k] + freq3[k];
1768 if (huf_total_size != SIZE_MAX) {
1770 if (huf_j < best_j) {
1771 enc_lit = ZXC_SECTION_ENCODING_HUFFMAN;
1781 size_t huf_dict_total_size = SIZE_MAX;
1786 if (huf_total_size == SIZE_MAX) {
1788 for (
size_t i = 0; i < lit_c; i++) lit_freq[literals[i]]++;
1792 huf_dict_total_size =
1794 if (huf_dict_total_size != SIZE_MAX) {
1798 const size_t huf_dict_j =
1801 if (huf_dict_j < best_j) enc_lit = ZXC_SECTION_ENCODING_HUFFMAN_DICT;
1810 uint8_t enc_tok = ZXC_SECTION_ENCODING_RAW;
1811 size_t tok_huf_size = 0;
1814 ZXC_MEMSET(tfreq, 0,
sizeof(tfreq));
1815 for (uint32_t i = 0; i < seq_c; i++) tfreq[buf_tokens[i]]++;
1823 if (tok_huf_size != SIZE_MAX &&
1825 enc_tok = ZXC_SECTION_ENCODING_HUFFMAN;
1833 const int use_8bit_off = (max_offset <= 255) ? 1 : 0;
1834 const size_t off_stream_size = use_8bit_off ? seq_c : (seq_c * 2);
1837 .n_literals = (uint32_t)lit_c,
1838 .enc_lit = (uint8_t)enc_lit,
1841 .enc_off = (uint8_t)use_8bit_off};
1843 const size_t sz_lit = (enc_lit == ZXC_SECTION_ENCODING_RLE) ? rle_size
1844 : (enc_lit == ZXC_SECTION_ENCODING_HUFFMAN) ? huf_total_size
1845 : (enc_lit == ZXC_SECTION_ENCODING_HUFFMAN_DICT) ? huf_dict_total_size
1847 const size_t sz_tok = (enc_tok == ZXC_SECTION_ENCODING_HUFFMAN) ? tok_huf_size : seq_c;
1848 const size_t sz_off = off_stream_size;
1849 const size_t sz_ext = extras_sz;
1852 if (UNLIKELY(ghs < 0))
return ghs;
1854 uint8_t* p_curr = p + ghs;
1859 if (enc_lit == ZXC_SECTION_ENCODING_HUFFMAN) {
1862 if (UNLIKELY(written < 0))
return written;
1866 }
else if (enc_lit == ZXC_SECTION_ENCODING_HUFFMAN_DICT) {
1869 &ctx->dict_huf->tree, ctx->dict_huf->codes, p_curr, rem);
1870 if (UNLIKELY(written < 0))
return written;
1874 }
else if (enc_lit == ZXC_SECTION_ENCODING_RLE) {
1876 const uint8_t* lit_ptr = literals;
1877 const uint8_t*
const lit_end = literals + lit_c;
1879 while (lit_ptr < lit_end) {
1880 uint8_t b = *lit_ptr;
1881 const uint8_t* run_start = lit_ptr++;
1883 while (lit_ptr < lit_end && *lit_ptr == b) lit_ptr++;
1884 size_t run = (size_t)(lit_ptr - run_start);
1896 *p_curr++ = (uint8_t)(run - 1);
1897 ZXC_MEMCPY(p_curr, lit_ptr - run, run);
1902 const uint8_t* lit_run_start = run_start;
1904 while (lit_ptr < lit_end) {
1905 if (UNLIKELY(lit_ptr + 3 < lit_end && lit_ptr[0] == lit_ptr[1] &&
1906 lit_ptr[1] == lit_ptr[2] && lit_ptr[2] == lit_ptr[3])) {
1912 size_t lit_run = (size_t)(lit_ptr - lit_run_start);
1913 const uint8_t* src_ptr = lit_run_start;
1916 while (lit_run > 0) {
1918 *p_curr++ = (uint8_t)(chunk - 1);
1919 ZXC_MEMCPY(p_curr, src_ptr, chunk);
1927 ZXC_MEMCPY(p_curr, literals, lit_c);
1934 if (enc_tok == ZXC_SECTION_ENCODING_HUFFMAN) {
1937 if (UNLIKELY(written < 0))
return written;
1941 ZXC_MEMCPY(p_curr, buf_tokens, seq_c);
1951 for (; i + 8 <= seq_c; i += 8) {
1952 p_curr[0] = (uint8_t)buf_offsets[i + 0];
1953 p_curr[1] = (uint8_t)buf_offsets[i + 1];
1954 p_curr[2] = (uint8_t)buf_offsets[i + 2];
1955 p_curr[3] = (uint8_t)buf_offsets[i + 3];
1956 p_curr[4] = (uint8_t)buf_offsets[i + 4];
1957 p_curr[5] = (uint8_t)buf_offsets[i + 5];
1958 p_curr[6] = (uint8_t)buf_offsets[i + 6];
1959 p_curr[7] = (uint8_t)buf_offsets[i + 7];
1962 for (; i < seq_c; i++) {
1963 *p_curr++ = (uint8_t)buf_offsets[i];
1967#ifdef ZXC_BIG_ENDIAN
1968 for (uint32_t i = 0; i < seq_c; i++) {
1970 p_curr +=
sizeof(uint16_t);
1973 ZXC_MEMCPY(p_curr, buf_offsets, seq_c *
sizeof(uint16_t));
1974 p_curr += seq_c *
sizeof(uint16_t);
1984 const size_t behind_lit = sz_tok + sz_off + sz_ext;
1989 ZXC_MEMCPY(p_curr, buf_extras, extras_sz);
1990 p_curr += extras_sz;
1992 ZXC_MEMSET(p_curr, 0, pad);
1997 if (UNLIKELY(hw < 0))
return hw;
2024 const size_t src_sz, uint8_t* RESTRICT dst,
const size_t dst_cap,
2025 size_t* RESTRICT
const out_sz) {
2028 const int level = ctx->compression_level;
2029 const size_t dict_sz = ctx->dict_size;
2037 if (UNLIKELY(ctx->epoch >= ctx->max_epoch)) {
2042 const uint32_t offset_bits = ctx->offset_bits;
2043 const uint32_t offset_mask = ctx->offset_mask;
2044 const uint32_t epoch_mark = ctx->epoch << offset_bits;
2047 zxc_lz_seed_dict(src, dict_sz, ctx->hash_table, ctx->hash_tags, ctx->chain_table,
2048 epoch_mark, offset_mask, level);
2050 const uint8_t* ip = src + dict_sz;
2051 const uint8_t* iend = src + src_sz;
2052 const uint8_t* anchor = ip;
2055 uint32_t*
const hash_table = ctx->hash_table;
2056 uint8_t*
const hash_tags = ctx->hash_tags;
2057 uint8_t*
const buf_extras = ctx->buf_extras;
2058 uint16_t*
const chain_table = ctx->chain_table;
2059 uint8_t*
const literals = ctx->literals;
2060 uint32_t*
const buf_sequences = ctx->buf_sequences;
2063 size_t extras_c = 0;
2066 while (LIKELY(ip < search_limit)) {
2067 size_t dist = (size_t)(ip - anchor);
2069 if (UNLIKELY(ip + step >= search_limit)) step = 1;
2073 if (LIKELY(ip + step +
sizeof(uint64_t) <= iend)) {
2074 const uint64_t v_next =
zxc_le64(ip + step);
2077 ZXC_PREFETCH_READ(&hash_tags[h_next]);
2078 ZXC_PREFETCH_READ(&hash_table[h_next]);
2081 const zxc_match_t m =
2083 chain_table, epoch_mark, offset_mask, level, lzp,
2088 const uint32_t ll = (uint32_t)(ip - anchor);
2090 const uint32_t off = (uint32_t)(ip - m.ref);
2099 buf_sequences[seq_c] = seq_val;
2113 const size_t last_lits = (size_t)(iend - anchor);
2114 if (last_lits > 0) {
2115 ZXC_MEMCPY(literals + lit_c, anchor, last_lits);
2124 .n_literals = (uint32_t)lit_c,
2125 .enc_lit = ZXC_SECTION_ENCODING_RAW,
2131 if (UNLIKELY(ghs < 0))
return ghs;
2133 uint8_t* p_curr = p + ghs;
2136 const size_t sz_lit = lit_c;
2137 const size_t sz_seq = seq_c *
sizeof(uint32_t);
2138 const size_t sz_ext = extras_c;
2142 const size_t behind_lit = sz_seq + sz_ext;
2147 ZXC_MEMCPY(p_curr, literals, lit_c);
2151#ifdef ZXC_BIG_ENDIAN
2152 for (uint32_t i = 0; i < seq_c; i++) {
2154 p_curr +=
sizeof(uint32_t);
2157 ZXC_MEMCPY(p_curr, buf_sequences, sz_seq);
2162 ZXC_MEMCPY(p_curr, buf_extras, sz_ext);
2165 ZXC_MEMSET(p_curr, 0, pad);
2170 if (UNLIKELY(hw < 0))
return hw;
2193 uint8_t* RESTRICT
const dst,
const size_t dst_cap,
2194 size_t* RESTRICT
const out_sz) {
2204 if (UNLIKELY(hw < 0))
return hw;
2224 const size_t src_sz, uint8_t* RESTRICT dst,
const size_t dst_cap,
2225 const uint64_t block_index) {
2228 const size_t dict_sz = ctx->dict_size;
2229 const size_t block_sz = src_sz - dict_sz;
2230 const uint8_t* block_data = chunk + dict_sz;
2234 if (ctx->compression_level <= 2)
2240 if (UNLIKELY(res !=
ZXC_OK || w >= block_sz)) {
2242 if (UNLIKELY(res !=
ZXC_OK))
return res;
2245 if (ctx->checksum_enabled) {
@ ZXC_ERROR_OVERFLOW
Buffer overflow detected during processing.
@ ZXC_ERROR_DST_TOO_SMALL
Destination buffer too small.
@ ZXC_OK
Success (no error).
#define ZXC_SPL_INLINE_ML_CODE
Longest match length code (length minus ZXC_LZ_MIN_MATCH_LEN) a SPL token carries inline; the code ab...
#define ZXC_LIT_LEN_MASK
Mask to extract the run/literal length (lower 7 bits).
#define ZXC_SEQ_OFF_MASK
Mask to extract Offset from a PAK sequence.
#define ZXC_BITMAP_WORDS(n_bits)
Number of uint64_t words needed to hold a bitmap of n_bits.
#define ZXC_ALWAYS_INLINE
Forces a function to be inlined (fallback for non-GCC/Clang/MSVC compilers).
#define ZXC_LIT_RLE_RUN_MIN
Shortest run worth an RLE token: a length field of 0 means this many bytes.
#define ZXC_SEQ_ML_MASK
Mask to extract Match Length from a PAK sequence.
#define ZXC_CHECKSUM_RAPIDHASH
Checksum algorithm id for RapidHash (default, sole implementation).
#define ZXC_SEQ_ML_SHIFT
Shift of the Match Length field: just above the offset.
#define ZXC_LZ_MAX_DIST
Maximum allowed offset distance.
#define ZXC_MAX_VARINT_VALUE
Maximum legitimate value a varint can decode to.
#define ZXC_SEQ_LL_SHIFT
Shift of the Literal Length field: the top byte of the word.
#define ZXC_LZ_SEARCH_MARGIN
Bytes at the block end where match search stops (left as literals).
#define ZXC_TOKEN_ML_MASK
Mask to extract Match Length from a SPL token.
#define ZXC_TOKEN_LL_MASK
Mask to extract Literal Length from a SPL token.
#define ZXC_LZ_HASH_SIZE
Maximum number of entries in the hash table.
#define ZXC_LZ_HASH_BITS
Address bits for the LZ77 hash table (2^15 = 32 768 buckets).
#define ZXC_LZ_MIN_MATCH_LEN
Minimum match length for an LZ77 match.
#define ZXC_LZ_WINDOW_SIZE
Sliding window size (64 KB).
#define ZXC_PAD_SIZE
Safety padding appended to buffers to tolerate overruns.
#define ZXC_LZ_OFFSET_BIAS
Base bias added to encoded offsets (stored = actual - bias).
#define ZXC_BLOCK_HEADER_SIZE
Block header size: Type(1)+Flags(1)+Reserved(1)+Checksum(1)+CompSize(4).
#define ZXC_BLOCK_LIT_SLACK
Readable bytes a SPL/PAK payload guarantees after its literal section.
#define ZXC_LIT_RUN_MAX
Longest literal run one token carries: a length field of 0 means 1 byte.
#define ZXC_SPL_MAX_INLINE_ML
Longest match a SPL sequence carries without a varint extension.
static uint32_t zxc_read_varint(const uint8_t **ptr, const uint8_t *end)
Reads a Prefix Varint encoded integer.
#define ZXC_LIT_RLE_FLAG
Flag bit indicating an RLE run in the literal stream (0x80).
#define ZXC_BLOCK_CHECKSUM_SIZE
Size of the per-block checksum field in bytes.
#define ZXC_CACHE_LINE_SIZE
Assumed CPU cache line size for alignment.
#define ZXC_LZ_WINDOW_MASK
Mask for ring-buffer indexing into chain_table (power-of-two window).
#define ZXC_TOKEN_LIT_BITS
Bits for Literal Length in a SPL token.
#define ZXC_SEQ_LL_MASK
Mask to extract Literal Length from a PAK sequence.
#define ZXC_LIT_RLE_RUN_MAX
Longest run one RLE token carries.
@ ZXC_LEVEL_ULTRA
Maximum density: Huffman-coded literals and sequence tokens.
@ ZXC_LEVEL_DEFAULT
Recommended default: better ratio and decode speed than LZ4.
@ ZXC_LEVEL_DENSITY
Higher density: adds Huffman-coded literals on top of COMPACT.
@ ZXC_LEVEL_BALANCED
Balanced trade-off between ratio and decode speed.
@ ZXC_LEVEL_FAST
Fast compression; slightly better ratio than FASTEST.
Compression / decompression context.
Search parameters for LZ77 compression levels.
int lazy_attempts
Maximum number of candidates explored during lazy evaluation (same semantics as search_depth but appl...
int search_depth
Maximum number of candidates explored in the hash chain per position.
int use_lazy
Enable lazy matching.
int lazy_len_threshold
Skip lazy evaluation when the current match length already reaches this threshold: a match this long ...
uint32_t step_shift
Acceleration factor for step size: step = step_base + (distance >> step_shift).
uint8_t split_max
Longest match re-emitted as inline pieces (zxc_spl_match_split_block), in the token's units (length -...
int sufficient_len
"Good enough" match length: once a match reaches this threshold the chain walk stops immediately,...
uint32_t min_offset
Shortest match distance the parser may emit; 1 = unconstrained.
uint32_t step_base
Base step size when advancing through unmatched literals.
int zxc_write_block_header(uint8_t *dst, const size_t dst_capacity, const zxc_block_header_t *bh)
Serialises a block header (8 bytes) into dst.
int zxc_write_pak_header(uint8_t *dst, const size_t rem, const zxc_gnr_header_t *gh)
Serialises a PAK block header.
int zxc_write_spl_header_and_desc(uint8_t *dst, const size_t rem, const zxc_gnr_header_t *gh, const uint32_t lit_comp, const uint32_t tok_comp)
Serialises a SPL block header followed by its section descriptors.
static int zxc_match_reaches(const uint8_t *cur, const uint8_t *ref, uint32_t from, const uint32_t need, const uint8_t *iend)
Tells whether a match reaches a given length, without measuring it.
static void zxc_opt_relax(uint32_t *dp, uint16_t *parent_len, uint16_t *parent_off, const size_t p, const size_t L, const uint32_t nxt, const uint16_t off_biased)
Relaxes dp[p + L] with a match of length L costing nxt in total.
static int zxc_lz77_optimal_parse_spl(zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint32_t *hash_table, uint8_t *hash_tags, uint16_t *chain_table, const uint32_t epoch_mark, const uint32_t offset_mask, const int level, uint8_t *literals, uint8_t *buf_tokens, uint16_t *buf_offsets, uint8_t *buf_extras, uint32_t *seq_c_out, size_t *lit_c_out, size_t *extras_sz_out, uint16_t *max_offset_out)
Static price-based optimal LZ77 parser for level 6.
static int zxc_spl_match_split_analyze(const uint8_t *tokens, const uint32_t n_seq, zxc_spl_match_split_hist_t *hist_buf, uint8_t *ctx_bad, uint8_t *side)
Predictability of the block's ML-escape branch, for the split decision.
static int zxc_encode_block_pak(zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, size_t *const out_sz)
Encodes one block in the PAK format (levels 1 and 2, the speed path).
int zxc_compress_chunk_wrapper(zxc_cctx_t *ctx, const uint8_t *chunk, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
Compresses one chunk into a single ZXC block (the compression hot path).
static void zxc_lz_seed_dict(const uint8_t *src, const size_t dict_size, uint32_t *hash_table, uint8_t *hash_tags, uint16_t *chain_table, const uint32_t epoch_mark, const uint32_t offset_mask, const int level)
Seeds the hash/chain tables from dictionary content prepended to src.
static uint32_t zxc_spl_match_split_pieces(const uint32_t code)
Inline pieces a match of length code code splits into: ceil(length / 19).
static void zxc_varint_keep(uint8_t *extras, size_t *w, size_t *r, const uint8_t *end)
Keeps the varint at extras[*r]: moves it down to extras[*w].
static void zxc_extras_move(uint8_t *extras, size_t *w, size_t *r, const uint8_t *to)
Moves extras[*r, to) down to extras[*w] (w <= r), advancing both.
static void zxc_flush_literals(uint8_t *literals, size_t *lit_c, const uint8_t *anchor, const uint32_t ll, const uint8_t *iend)
Stages a run of literals into the literal stream.
static uint8_t zxc_hash_tag(const uint32_t val4)
8-bit rejection tag: XOR fold of the first four bytes at a position.
static int zxc_encode_block_spl(zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, size_t dst_cap, size_t *out_sz)
Encodes one block in the SPL format (levels 3 and up).
static int zxc_spl_put_seq(uint8_t *buf_tokens, uint16_t *buf_offsets, uint8_t *buf_extras, uint32_t *seq_c, size_t *extras_sz, uint16_t *max_offset, const uint32_t ll, const uint32_t ml, const uint16_t off_biased)
Emits one SPL sequence (token, offset, LL/ML escapes) and updates max_offset, the block's widest bias...
static size_t zxc_opt_dp_update_const_cost(uint32_t *dp, uint16_t *parent_len, uint16_t *parent_off, const size_t p, size_t L, const size_t L_end, const uint32_t nxt, const uint16_t off_biased)
Update dp[p + L_start .
static int zxc_emit_extra(uint8_t *extras, size_t *used, const uint32_t val, const uint32_t mask)
Appends a saturated token field's excess to the extras stream.
static size_t zxc_write_varint(uint8_t *dst, const uint32_t val)
Pair-equality mask for the RLE lookahead: element i is set when p[i] == p[i+1].
static uint16_t zxc_chain_delta(const uint32_t cur_pos, const uint32_t prev_idx)
Chain link from a position back to the one the hash slot held.
static int zxc_lazy_probe(const uint8_t *src, const uint8_t *lp, const uint8_t *iend, const uint32_t need, const uint32_t *hash_table, const uint8_t *hash_tags, const uint16_t *chain_table, const uint32_t epoch_mark, const uint32_t offset_mask, const int use_hash5, const zxc_lz77_params_t p)
Tells whether lp holds a match of at least need bytes, within the lazy search budget.
static uint32_t zxc_epoch_pos(const uint32_t head, const uint32_t offset_mask, const uint32_t epoch_mark)
Decodes a hash-table slot into a block position, 0 if it is stale.
static uint32_t zxc_opt_estimate_lit_bits(const uint8_t *src, const size_t src_sz, void *scratch)
Estimate per-block literal cost from a sampled histogram passed through the actual length-limited Huf...
static uint32_t zxc_hash_func(const uint64_t val, const int use_hash5)
Computes a hash value for either a 4-byte or 5-byte sequence.
static uint32_t zxc_spl_match_split_block(uint8_t *tokens, uint16_t *offsets, uint8_t *extras, size_t *extras_sz, uint8_t *side, zxc_spl_match_split_hist_t *hist_buf, const uint32_t n_seq, const uint8_t cap)
Per-block match splitting: escaped matches re-emitted as inline pieces.
static int zxc_encode_block_raw(const uint8_t *src, const size_t src_sz, uint8_t *const dst, const size_t dst_cap, size_t *const out_sz)
Encodes a RAW (stored) block: header plus a verbatim copy.
static uint32_t zxc_spl_match_split_piece(const uint32_t code)
Next piece of a match of length code code above the inline reach: the longest inline one that leaves ...
static zxc_match_t zxc_lz77_find_best_match(const uint8_t *src, const uint8_t *ip, const uint8_t *iend, const uint8_t *search_limit, const uint8_t *anchor, uint32_t *hash_table, uint8_t *hash_tags, uint16_t *chain_table, const uint32_t epoch_mark, const uint32_t offset_mask, const int level, const zxc_lz77_params_t p, const uint32_t last_off)
Finds the best matching sequence for LZ77 compression.
size_t zxc_huf_calc_size(const uint32_t *freq, const uint8_t *code_len, const int with_header)
Un-suffixed forwarders for the PivCo section codec (tests, tools).
int zxc_huf_build_code_lengths(const uint32_t *freq, uint8_t *code_len, void *scratch, const int max_code_len)
Build length-limited per-symbol Huffman code lengths from frequencies.
size_t zxc_huf_calc_size_dict(const uint32_t *freq, const uint8_t *code_len, const zxc_pivco_tree_t *tree)
zxc_huf_calc_size for a dict section: prebuilt tree, no header.
int zxc_huf_encode_section_dict(const uint8_t *literals, const size_t n_literals, const uint32_t *freq, const uint8_t *code_len, const zxc_pivco_tree_t *tree, const uint32_t *codes, uint8_t *dst, const size_t dst_cap)
Encode a PivCo section against a prebuilt dict tree/codes (no header).
int zxc_huf_encode_section(const uint8_t *literals, const size_t n_literals, const uint32_t *freq, const uint8_t *code_len, uint8_t *dst, const size_t dst_cap)
Encode a PivCo literal section (128-byte lengths header + payload).
Error codes and error-name lookup for the ZXC library.
int zxc_huf_nudge_code_lengths(const uint32_t *freq, uint8_t *code_len, void *scratch, const size_t scratch_cap, const int max_code_len)
Optionally reshape freshly built code lengths for faster PivCo decode.
Internal definitions, constants, SIMD helpers, and utility functions.
static uint64_t zxc_le64(const void *p)
Reads a little-endian 64-bit value from a possibly unaligned address.
static int zxc_ctz32(const uint32_t x)
Counts trailing zeros, returning 32 for a zero input.
static int zxc_ss_prem_rle_q8(const int level)
Per-level RLE-vs-RAW decode premium (Q8) for the space-speed selector.
static uint32_t zxc_le32(const void *p)
Reads a little-endian 32-bit value from a possibly unaligned address.
static void zxc_copy32(void *dst, const void *src)
Copies 32 bytes from source to destination using SIMD when available.
static int zxc_huf_enc_max_code_len(const int level)
Encoder Huffman code-length cap for a compression level: levels below ZXC_LEVEL_ULTRA use ZXC_HUF_MAX...
#define ZXC_HUF_MIN_LITERALS
Absolute floor (in literals) below which a Huffman candidate is never evaluated.
#define ZXC_OPT_LONG_MATCH_SKIP
Threshold above which find_best_match is skipped at intra-match positions, keeping the parser O(N) on...
uint32_t zxc_spl_match_split_hist_t[4][2][1U<< 8]
zxc_spl_match_split_analyze's escape histograms: four rotating lanes of [escape][context] counts,...
#define ZXC_OPT_MATCH_COST_BASE
Static price (bits) of a match token before varint extras: 1 byte token + 2 byte offset.
#define ZXC_OPT_LIT_SAMPLE_TARGET
Bytes that estimator samples per block: enough for a 256-symbol histogram.
static int zxc_ss_prem_huf_q8(const int level)
Per-level Huffman/PivCo-vs-RAW decode premium (Q8) for the space-speed selector.
static zxc_lz77_params_t zxc_get_lz77_params(const int level)
Retrieves LZ77 compression parameters based on the specified compression level.
#define ZXC_OPT_LIT_SAMPLE_MIN
Minimum literal count for the sample-based Huffman cost estimator used by the optimal parser.
static void zxc_store_le16(void *p, const uint16_t v)
Writes a 16-bit value little-endian to a possibly unaligned address.
#define ZXC_SS_TAX(n, prem_q8)
Decode tax of a candidate: n decoded bytes at premium prem (Q8).
#define ZXC_HASH_MULT32
Marsaglia 32-bit multiplier: the 4-byte LZ and dictionary hash.
#define ZXC_HUF_MAX_CODE_LEN_DENSITY
Encoder code-length cap for levels up to ZXC_LEVEL_DENSITY (below ZXC_LEVEL_ULTRA): shallow 8-bit tre...
static void zxc_store_le32(void *p, const uint32_t v)
Writes a 32-bit value little-endian to a possibly unaligned address.
static int zxc_ctz64(const uint64_t x)
Counts trailing zeros, returning 64 for a zero input.
static int zxc_block_is_short_dist_bound(const uint8_t *const blk, const size_t size)
Reports whether a block leans on back-references shorter than ZXC_LZ_MINDIST.
#define ZXC_HASH_XORSHIFT64
Vigna's xorshift* multiplier: the 5-byte LZ hash and the digest fold.
#define ZXC_SPL_MATCH_SPLIT_MIN_MISPREDICT_PCT
Mispredict rate, in percent of a block's escapes, from which the block is split.
#define ZXC_SPL_MATCH_SPLIT_CTX_BITS
Longest escape history a context keys on; small blocks shorten it (down to 2 bits) towards 16 samples...
static uint32_t zxc_checksum(const void *input, const size_t len, const uint64_t seed, const uint8_t hash_method)
Calculates a 32-bit hash for a given input buffer.
static void zxc_opt_dp_sizes(const size_t chunk_size, size_t *sz_dp, size_t *sz_pl, size_t *sz_po, size_t *sz_bm)
The four DP partitions the optimal parser carves out of opt_scratch.
#define ZXC_HUF_NUM_SYMBOLS
Alphabet size: one entry per possible byte value.