ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_decompress.c File Reference

Block-level decompression: SPL / PAK / RAW decoding with SIMD-accelerated bit-unpacking and overlapping copies. More...

#include "zxc_build.h"
#include "../../include/zxc_error.h"
#include "zxc_internal.h"

Go to the source code of this file.

Macros

#define DECODE_MATCH_SAFE(ml, off, COPY)
#define DECODE_MATCH_FAST(ml, off, COPY)
#define SPL_OFF8   (gh.enc_off == 1)
 True when the SPL offset stream stores 1-byte offsets, 2-byte otherwise.
#define DECODE_SPL_SEQ(LL, ML, OFF, RESERVE, N_REM, DECODE, ON_FAIL)
 Decodes one SPL sequence of a 4x batch: extracts ll/ml, applies the varint extensions with their bounds checks, then emits via DECODE.
#define DECODE_SPL_BATCH_4X(DECODE, ON_FAIL)
 One full SPL 4x batch: token word, four offsets (1- or 2-byte form), four DECODE_SPL_SEQ emissions, sequence-count update.
#define DECODE_PAK_SEQ(S, RESERVE, N_REM, DECODE, ON_FAIL)
 PAK twin of DECODE_SPL_SEQ, decoding one sequence word S (ll in the top byte, ml bits, 16-bit offset).
#define DECODE_PAK_BATCH_4X(DECODE, ON_FAIL, PREFETCH)
 One full PAK 4x batch.

Functions

static void zxc_decode_copy_overlap_run32 (uint8_t *dst, const uint32_t off, const uint64_t ml)
 Copies an ml-byte run of period off (2..31) with 32-byte stores.
static void zxc_decode_copy_overlap_short (uint8_t *dst, const uint32_t off, const uint64_t ml)
 Overlap copy for a run of period off (2..31) bounded by ml <= 32.
static void zxc_decode_fill_run (uint8_t *dst, const uint8_t byte, const uint64_t ml)
 Fills an ml-byte single-byte run (LZ offset == 1) with wild stores.
static void zxc_wild_copy32 (uint8_t *dst, const uint8_t *src, const size_t len)
 Copies len bytes in 32-byte wild chunks (the zxc_copy32 width).
static void zxc_decode_copy_literals (uint8_t *dst, const uint8_t *src, const uint64_t ll)
 Copies ll literal bytes from src to dst using 32-byte wild copies.
static void zxc_decode_copy_match (uint8_t *d_ptr, const uint32_t off, const uint64_t ml)
 Copies an ml-byte LZ match from d_ptr-off to d_ptr, handling overlap.
static void zxc_decode_copy_match_short (uint8_t *d_ptr, const uint32_t off, const uint64_t ml)
 Match copy for a length that cannot exceed 32 bytes.
static void zxc_decode_copy_match_spl (uint8_t *d_ptr, const uint32_t off, const uint64_t ml)
 SPL match copy: bounded form when the ml nibble did not saturate.
static void zxc_decode_copy_match_exact (uint8_t *d_ptr, const uint8_t *match_src, const size_t off, const size_t ml)
 Exact-size match copy for the tail loops (no overshoot headroom).
static int zxc_ensure_entropy_scratch (const zxc_cctx_t *ctx)
 Ensures the entropy decode scratch (tok_buffer + pivco_scratch) is available before decoding an entropy section.
static int zxc_decode_lit_pivco (const zxc_cctx_t *ctx, const uint8_t *payload, const size_t psize, const size_t required_size)
static int zxc_decode_lit_pivco_dict (const zxc_cctx_t *ctx, const uint8_t *payload, const size_t psize, const size_t required_size)
static int zxc_decode_tok_pivco (const zxc_cctx_t *ctx, const uint8_t *payload, const size_t psize, const size_t n_tok)
static int zxc_decode_block_spl_entropy (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity)
 Cold specializations for entropy-coded SPL tokens (level 7).
static int zxc_decode_block_spl_entropy_dict (const zxc_cctx_t *ctx, const uint8_t *src, size_t src_size, uint8_t *dst, size_t dst_capacity)
static int zxc_decode_block_spl_entropy_safe (const zxc_cctx_t *ctx, const uint8_t *src, size_t src_size, uint8_t *dst, size_t dst_capacity)
static int zxc_decode_block_spl_impl (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity, const int safe, const int has_dict, const int tok_entropy)
 Unified SPL (split streams) block decoder body, shared by the fast, safe, dictionary and entropy-token variants.
static int zxc_decode_block_pak_impl (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity, const int safe, const int has_dict)
 Unified PAK (packed sequences) block decoder body, shared by the fast, safe and dictionary variants.
static int zxc_decode_block_spl (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity)
 Decode a no-dict SPL block (plain, inlinable path).
static int zxc_decode_block_pak (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity)
 Decode a no-dict PAK block (plain, inlinable path).
static int zxc_decode_block_spl_dict (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity)
 Decode a SPL block against a dictionary prefix (cold path).
static int zxc_decode_block_pak_dict (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity)
 Decode a PAK block against a dictionary prefix (cold path).
static int zxc_decode_block_spl_safe (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity)
 Decode a SPL block with the strict-tail safe loop (no wild copies).
static int zxc_decode_block_pak_safe (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity)
 Decode a PAK block with the strict-tail safe loop (no wild copies).
static int zxc_decompress_chunk_wrapper_body (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index, const int has_dict, const int safe)
 Shared chunk-decode body: validates the header, decodes, then verifies the optional checksum.
int zxc_decompress_chunk_wrapper (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 Public no-dict chunk decoder (decompression hot path).
int zxc_decompress_chunk_wrapper_dict (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 Public dictionary chunk decoder.
int zxc_decompress_chunk_wrapper_safe (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 Public strict-tail safe chunk decoder (dst_cap == exact decoded size).

Variables

static const uint8_t zxc_overlap_strides32 [32]
 Store stride for the 32-byte pattern: (32 / off) * off.

Detailed Description

Block-level decompression: SPL / PAK / RAW decoding with SIMD-accelerated bit-unpacking and overlapping copies.

Like zxc_compress.c, this file is compiled multiple times with ZXC_FUNCTION_SUFFIX to produce per-ISA variants.

Definition in file zxc_decompress.c.

Macro Definition Documentation

◆ DECODE_MATCH_FAST

#define DECODE_MATCH_FAST ( ml,
off,
COPY )
Value:
do { \
COPY(d_ptr, off, ml); \
d_ptr += ml; \
} while (0)

Definition at line 541 of file zxc_decompress.c.

◆ DECODE_MATCH_SAFE

#define DECODE_MATCH_SAFE ( ml,
off,
COPY )
Value:
do { \
if (UNLIKELY((size_t)(d_ptr - d_floor) < (off))) return ZXC_ERROR_BAD_OFFSET; \
COPY(d_ptr, off, ml); \
d_ptr += ml; \
} while (0)
@ ZXC_ERROR_BAD_OFFSET
Invalid match offset during decompression.
Definition zxc_error.h:57

Definition at line 532 of file zxc_decompress.c.

◆ DECODE_PAK_BATCH_4X

#define DECODE_PAK_BATCH_4X ( DECODE,
ON_FAIL,
PREFETCH )
Value:
do { \
uint32_t s1 = zxc_le32(seq_ptr); \
uint32_t s2 = zxc_le32(seq_ptr + sizeof(uint32_t)); \
uint32_t s3 = zxc_le32(seq_ptr + 2 * sizeof(uint32_t)); \
uint32_t s4 = zxc_le32(seq_ptr + 3 * sizeof(uint32_t)); \
seq_ptr += 4 * sizeof(uint32_t); \
PREFETCH; \
DECODE_PAK_SEQ( \
s1, (s2 >> ZXC_SEQ_LL_SHIFT) + (s3 >> ZXC_SEQ_LL_SHIFT) + (s4 >> ZXC_SEQ_LL_SHIFT), \
3U, DECODE, ON_FAIL); \
DECODE_PAK_SEQ(s2, (s3 >> ZXC_SEQ_LL_SHIFT) + (s4 >> ZXC_SEQ_LL_SHIFT), 2U, DECODE, \
ON_FAIL); \
DECODE_PAK_SEQ(s3, (s4 >> ZXC_SEQ_LL_SHIFT), 1U, DECODE, ON_FAIL); \
DECODE_PAK_SEQ(s4, 0, 0U, DECODE, ON_FAIL); \
n_seq -= 4; \
} while (0)
#define ZXC_SEQ_LL_SHIFT
Shift of the Literal Length field: the top byte of the word.
static uint32_t zxc_le32(const void *p)
Reads a little-endian 32-bit value from a possibly unaligned address.

One full PAK 4x batch.

PREFETCH is the literal-stream prefetch statement of the post-threshold FAST loops ((void)0 elsewhere), placed exactly where the hand-unrolled bodies had it.

Definition at line 680 of file zxc_decompress.c.

Referenced by zxc_decode_block_pak_impl().

◆ DECODE_PAK_SEQ

#define DECODE_PAK_SEQ ( S,
RESERVE,
N_REM,
DECODE,
ON_FAIL )
Value:
do { \
uint64_t ll = (S) >> ZXC_SEQ_LL_SHIFT; \
const uint32_t mb = ((S) >> ZXC_SEQ_ML_SHIFT) & ZXC_SEQ_ML_MASK; \
uint64_t ml = mb + ZXC_LZ_MIN_MATCH_LEN; \
if (UNLIKELY(ll == ZXC_SEQ_LL_MASK)) { \
ll += zxc_read_varint(&extras_ptr, extras_end); \
const uint64_t reserve = (RESERVE); \
/* Same reservation as the SPL twin; PAK's inline ml reaches 259. */ \
if (UNLIKELY(ll + reserve > (size_t)(l_end - l_ptr) || \
ll + ml + (N_REM) * ZXC_PAK_MAX_INLINE_OUT_PER_SEQ + ZXC_PAD_SIZE > \
(size_t)(d_end - d_ptr))) \
ON_FAIL; \
} \
if (UNLIKELY(mb == ZXC_SEQ_ML_MASK)) { \
ml += zxc_read_varint(&extras_ptr, extras_end); \
if (UNLIKELY(ll + ml + (N_REM) * ZXC_PAK_MAX_INLINE_OUT_PER_SEQ + ZXC_PAD_SIZE > \
(size_t)(d_end - d_ptr))) \
ON_FAIL; \
} \
const uint32_t off = ((S) & ZXC_SEQ_OFF_MASK) + ZXC_LZ_OFFSET_BIAS; \
/* PAK keeps the ladder: its inline ll reaches 254. */ \
zxc_decode_copy_literals(d_ptr, l_ptr, ll); \
l_ptr += ll; \
d_ptr += ll; \
DECODE(ml, off, zxc_decode_copy_match); \
} while (0)
#define ZXC_SEQ_OFF_MASK
Mask to extract Offset from a PAK sequence.
#define ZXC_SEQ_ML_MASK
Mask to extract Match Length from a PAK sequence.
#define ZXC_SEQ_ML_SHIFT
Shift of the Match Length field: just above the offset.
#define ZXC_LZ_MIN_MATCH_LEN
Minimum match length for an LZ77 match.
#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).
static uint32_t zxc_read_varint(const uint8_t **ptr, const uint8_t *end)
Reads a Prefix Varint encoded integer.
#define ZXC_SEQ_LL_MASK
Mask to extract Literal Length from a PAK sequence.
static void zxc_decode_copy_match(uint8_t *d_ptr, const uint32_t off, const uint64_t ml)
Copies an ml-byte LZ match from d_ptr-off to d_ptr, handling overlap.

PAK twin of DECODE_SPL_SEQ, decoding one sequence word S (ll in the top byte, ml bits, 16-bit offset).

References the call site's extras_ptr/extras_end instead of e_ptr/e_end.

Definition at line 647 of file zxc_decompress.c.

◆ DECODE_SPL_BATCH_4X

#define DECODE_SPL_BATCH_4X ( DECODE,
ON_FAIL )
Value:
do { \
uint32_t tokens = zxc_le32(t_ptr); \
t_ptr += sizeof(uint32_t); \
uint64_t offs; \
if (SPL_OFF8) { \
uint64_t x = zxc_le32(o_ptr); \
o_ptr += sizeof(uint32_t); \
x = (x | (x << 16)) & 0x0000FFFF0000FFFFULL; \
offs = (x | (x << 8)) & 0x00FF00FF00FF00FFULL; \
} else { \
offs = zxc_le64(o_ptr); \
o_ptr += sizeof(uint64_t); \
} \
DECODE_SPL_SEQ((tokens & 0x0F0) >> 4, (tokens & 0x00F), \
ZXC_LZ_OFFSET_BIAS + (uint32_t)(offs & 0xFFFF), \
((tokens >> 12) & 0xF) + ((tokens >> 20) & 0xF) + (tokens >> 28), 3U, \
DECODE, ON_FAIL); \
DECODE_SPL_SEQ((tokens & 0x0F000) >> 12, (tokens & 0x00F00) >> 8, \
ZXC_LZ_OFFSET_BIAS + (uint32_t)((offs >> 16) & 0xFFFF), \
((tokens >> 20) & 0xF) + (tokens >> 28), 2U, DECODE, ON_FAIL); \
DECODE_SPL_SEQ((tokens & 0x0F00000) >> 20, (tokens & 0x00F0000) >> 16, \
ZXC_LZ_OFFSET_BIAS + (uint32_t)((offs >> 32) & 0xFFFF), (tokens >> 28), 1U, \
DECODE, ON_FAIL); \
DECODE_SPL_SEQ((tokens >> 28), (tokens >> 24) & 0x0F, \
ZXC_LZ_OFFSET_BIAS + (uint32_t)(offs >> 48), 0, 0U, DECODE, ON_FAIL); \
n_seq -= 4; \
} while (0)
#define SPL_OFF8
True when the SPL offset stream stores 1-byte offsets, 2-byte otherwise.
static uint64_t zxc_le64(const void *p)
Reads a little-endian 64-bit value from a possibly unaligned address.

One full SPL 4x batch: token word, four offsets (1- or 2-byte form), four DECODE_SPL_SEQ emissions, sequence-count update.

The offsets stay packed in one u64 of 16-bit lanes, 1-byte offsets spread into it, and each sequence extracts its own: four decoded offsets held live cost x86-64 spills.

Definition at line 613 of file zxc_decompress.c.

Referenced by zxc_decode_block_spl_impl().

◆ DECODE_SPL_SEQ

#define DECODE_SPL_SEQ ( LL,
ML,
OFF,
RESERVE,
N_REM,
DECODE,
ON_FAIL )

Decodes one SPL sequence of a 4x batch: extracts ll/ml, applies the varint extensions with their bounds checks, then emits via DECODE.

Like DECODE_MATCH_SAFE, references the call site's local names (e_ptr, e_end, l_ptr, l_end, d_ptr, d_end). RESERVE is the sum of the inline (pre-varint) literal lengths of the batch's remaining sequences, so one l_ptr bound covers the whole batch; N_REM counts the remaining sequences, reserving their worst-case inline output in the d_ptr bound (0 for the last sequence - the compiler folds the dead terms). ON_FAIL is goto rollback_* in the state-saving safe variants and return ZXC_ERROR_OVERFLOW otherwise.

Definition at line 573 of file zxc_decompress.c.

◆ SPL_OFF8

#define SPL_OFF8   (gh.enc_off == 1)

True when the SPL offset stream stores 1-byte offsets, 2-byte otherwise.

The single definition of the offset width: the stream-size check, the validation span and every read site expand from this, so a bound can never drift from the width actually consumed. A macro rather than a local flag on purpose – holding one more value live across the decode costs more spills in these register-starved loops than re-reading the header field.

References the call site's gh, like the DECODE_* macros below.

Definition at line 558 of file zxc_decompress.c.

Referenced by zxc_decode_block_spl_impl().

Function Documentation

◆ zxc_decode_block_pak()

int zxc_decode_block_pak ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity )
static

Decode a no-dict PAK block (plain, inlinable path).

Wrapper over zxc_decode_block_pak_impl with safe=0, has_dict=0.

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed PAK block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer.
[in]dst_capacityCapacity of dst in bytes.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1484 of file zxc_decompress.c.

References zxc_decode_block_pak_impl().

Referenced by zxc_decompress_chunk_wrapper_body().

◆ zxc_decode_block_pak_dict()

int zxc_decode_block_pak_dict ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity )
static

Decode a PAK block against a dictionary prefix (cold path).

Wrapper over zxc_decode_block_pak_impl with safe=0, has_dict=1 (NOINLINE; see zxc_decode_block_spl_dict).

Parameters
[in,out]ctxDecompression context (dict prefix in its buffer).
[in]srcCompressed PAK block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer.
[in]dst_capacityCapacity of dst in bytes.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1527 of file zxc_decompress.c.

References zxc_decode_block_pak_impl(), and ZXC_NOINLINE.

Referenced by zxc_decompress_chunk_wrapper_body().

◆ zxc_decode_block_pak_impl()

int zxc_decode_block_pak_impl ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity,
const int safe,
const int has_dict )
inlinestatic

Unified PAK (packed sequences) block decoder body, shared by the fast, safe and dictionary variants.

Decodes a block in the internal PAK format. The decompressed size is not on the wire: it falls out of walking the sequences. safe and has_dict must be compile-time constants (0 or 1): the 4x-unrolled loops are duplicated inside if(safe)/else branches so each variant keeps single-assignment const save pointers, and after constant propagation only one branch survives per wrapper.

Parameters
[in,out]ctxDecompression context (dict buffer, tables).
[in]srcCompressed block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer for decoded bytes.
[in]dst_capacityCapacity of dst in bytes.
[in]safeCompile-time flag: 1 = strict bounds-checked loop.
[in]has_dictCompile-time flag: 1 = resolve matches against a dict prefix.
Returns
Bytes written to dst on success, or a negative zxc_error_t.

Definition at line 1184 of file zxc_decompress.c.

References DECODE_PAK_BATCH_4X, zxc_gnr_header_t::enc_lit, zxc_gnr_header_t::enc_tok, zxc_gnr_header_t::n_literals, zxc_gnr_header_t::n_sequences, ZXC_ALWAYS_INLINE, ZXC_BLOCK_LIT_SLACK, ZXC_CACHE_LINE_SIZE, zxc_decode_copy_literals(), zxc_decode_copy_match(), zxc_decode_copy_match_exact(), ZXC_DECOMPRESS_TAIL_PAD, ZXC_ERROR_BAD_HEADER, ZXC_ERROR_BAD_OFFSET, ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_OVERFLOW, zxc_le32(), ZXC_LZ_MIN_MATCH_LEN, ZXC_LZ_OFFSET_BIAS, ZXC_OK, ZXC_PAD_SIZE, ZXC_PAK_HEADER_BINARY_SIZE, zxc_read_pak_header(), zxc_read_varint(), ZXC_SEQ_LL_MASK, ZXC_SEQ_LL_SHIFT, ZXC_SEQ_ML_MASK, ZXC_SEQ_ML_SHIFT, and ZXC_SEQ_OFF_MASK.

Referenced by zxc_decode_block_pak(), zxc_decode_block_pak_dict(), and zxc_decode_block_pak_safe().

◆ zxc_decode_block_pak_safe()

int zxc_decode_block_pak_safe ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity )
static

Decode a PAK block with the strict-tail safe loop (no wild copies).

Wrapper over zxc_decode_block_pak_impl with safe=1, has_dict=0 (the strict-tail safe path never carries a dict; see zxc_decode_block_spl_safe).

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed PAK block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer (capacity == exact decoded size).
[in]dst_capacityCapacity of dst in bytes.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1570 of file zxc_decompress.c.

References zxc_decode_block_pak_impl(), and ZXC_NOINLINE.

Referenced by zxc_decompress_chunk_wrapper_body().

◆ zxc_decode_block_spl()

int zxc_decode_block_spl ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity )
static

Decode a no-dict SPL block (plain, inlinable path).

Wrapper over zxc_decode_block_spl_impl with safe=0, has_dict=0, so the no-dict chunk wrapper inlines it exactly like the dict-free build.

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed SPL block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer.
[in]dst_capacityCapacity of dst in bytes.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1466 of file zxc_decompress.c.

References zxc_decode_block_spl_impl().

Referenced by zxc_decompress_chunk_wrapper_body().

◆ zxc_decode_block_spl_dict()

int zxc_decode_block_spl_dict ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity )
static

Decode a SPL block against a dictionary prefix (cold path).

Wrapper over zxc_decode_block_spl_impl with safe=0, has_dict=1. NOINLINE: only reached on the cold dict path (zxc_decompress_chunk_wrapper_dict), so it never loads into I-cache on a no-dict stream.

Parameters
[in,out]ctxDecompression context (dict prefix in its buffer).
[in]srcCompressed SPL block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer.
[in]dst_capacityCapacity of dst in bytes.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1505 of file zxc_decompress.c.

References zxc_decode_block_spl_impl(), and ZXC_NOINLINE.

Referenced by zxc_decompress_chunk_wrapper_body().

◆ zxc_decode_block_spl_entropy()

int zxc_decode_block_spl_entropy ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity )
static

Cold specializations for entropy-coded SPL tokens (level 7).

Each wrapper fixes tok_entropy=1 plus one (safe, has_dict) combo as compile-time constants, so the inlined impl is fully specialized and the RAW-token fast path stays untouched.

Definition at line 1428 of file zxc_decompress.c.

References zxc_decode_block_spl_impl(), and ZXC_NOINLINE.

Referenced by zxc_decode_block_spl_impl().

◆ zxc_decode_block_spl_entropy_dict()

int zxc_decode_block_spl_entropy_dict ( const zxc_cctx_t * ctx,
const uint8_t * src,
size_t src_size,
uint8_t * dst,
size_t dst_capacity )
static

Definition at line 1436 of file zxc_decompress.c.

◆ zxc_decode_block_spl_entropy_safe()

int zxc_decode_block_spl_entropy_safe ( const zxc_cctx_t * ctx,
const uint8_t * src,
size_t src_size,
uint8_t * dst,
size_t dst_capacity )
static

Definition at line 1445 of file zxc_decompress.c.

◆ zxc_decode_block_spl_impl()

int zxc_decode_block_spl_impl ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity,
const int safe,
const int has_dict,
const int tok_entropy )
inlinestatic

Unified SPL (split streams) block decoder body, shared by the fast, safe, dictionary and entropy-token variants.

Decodes a block in the internal SPL format. The decompressed size is not on the wire: it falls out of walking the sequences. safe, has_dict and tok_entropy must be compile-time constants (0 or 1): the 4x-unrolled loops are duplicated inside if(safe)/else branches so each variant keeps single-assignment const save pointers, and after constant propagation only one branch survives per wrapper (codegen equivalent to a hand-written pair).

The tok_entropy dimension keeps the token pointer on a SINGLE provenance per instantiation (in-place src tokens vs ctx->tok_buffer): the tok_entropy=0 instantiations tail-call their entropy twin right after the header parse when they meet enc_tok == 2.

Parameters
[in,out]ctxDecompression context (dict buffer, scratch).
[in]srcCompressed block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer for decoded bytes.
[in]dst_capacityCapacity of dst in bytes.
[in]safeCompile-time flag: 1 = strict bounds-checked loop.
[in]has_dictCompile-time flag: 1 = resolve matches against a dict prefix.
[in]tok_entropyCompile-time flag: 1 = tokens are a PivCo section decoded into ctx->tok_buffer (level 7).
Returns
Bytes written to dst on success, or a negative zxc_error_t.

Definition at line 806 of file zxc_decompress.c.

References DECODE_SPL_BATCH_4X, zxc_gnr_header_t::enc_lit, zxc_gnr_header_t::enc_off, zxc_gnr_header_t::enc_tok, zxc_gnr_header_t::n_literals, zxc_gnr_header_t::n_sequences, SPL_OFF8, ZXC_ALWAYS_INLINE, ZXC_BLOCK_LIT_SLACK, zxc_copy16(), zxc_copy32(), zxc_decode_block_spl_entropy(), zxc_decode_copy_literals(), zxc_decode_copy_match_exact(), zxc_decode_copy_match_spl(), ZXC_ERROR_BAD_HEADER, ZXC_ERROR_BAD_OFFSET, ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_DST_TOO_SMALL, ZXC_ERROR_OVERFLOW, zxc_le16(), ZXC_LIT_LEN_MASK, ZXC_LIT_RLE_FLAG, ZXC_LIT_RLE_RUN_MIN, ZXC_LZ_MIN_MATCH_LEN, ZXC_LZ_OFFSET_BIAS, ZXC_OK, ZXC_PAD_SIZE, zxc_read_spl_header_and_desc(), zxc_read_varint(), ZXC_SPL_MAX_INLINE_OUT_PER_SEQ, ZXC_TOKEN_LIT_BITS, ZXC_TOKEN_LL_MASK, and ZXC_TOKEN_ML_MASK.

Referenced by zxc_decode_block_spl(), zxc_decode_block_spl_dict(), zxc_decode_block_spl_entropy(), and zxc_decode_block_spl_safe().

◆ zxc_decode_block_spl_safe()

int zxc_decode_block_spl_safe ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_size,
uint8_t * dst,
const size_t dst_capacity )
static

Decode a SPL block with the strict-tail safe loop (no wild copies).

Wrapper over zxc_decode_block_spl_impl with safe=1, has_dict=0. The safe path never carries a dict (block_safe routes dict inputs to the bounce path), so has_dict=0 folds the dead dict handling.

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed SPL block payload.
[in]src_sizeSize of src in bytes.
[out]dstDestination buffer (capacity == exact decoded size).
[in]dst_capacityCapacity of dst in bytes.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1549 of file zxc_decompress.c.

References zxc_decode_block_spl_impl(), and ZXC_NOINLINE.

Referenced by zxc_decompress_chunk_wrapper_body().

◆ zxc_decode_copy_literals()

void zxc_decode_copy_literals ( uint8_t * dst,
const uint8_t * src,
const uint64_t ll )
inlinestatic

Copies ll literal bytes from src to dst using 32-byte wild copies.

Writes in 32-byte chunks – the width of zxc_copy32 – and may overshoot by up to 31 bytes past ll; the caller must guarantee dst has at least ZXC_PAD_SIZE bytes of writable headroom (the unrolled loops and the trailing-literal margins ensure this). Pointers are taken by value and the caller advances its own cursors by ll, keeping them in registers on the hot path.

Parameters
[out]dstOutput cursor. Must not overlap src and must have ZXC_PAD_SIZE bytes of overshoot headroom.
[in]srcLiteral-stream source. Must not overlap dst (RESTRICT).
[in]llNumber of literal bytes to copy.

Definition at line 408 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE, and zxc_wild_copy32().

Referenced by zxc_decode_block_pak_impl(), and zxc_decode_block_spl_impl().

◆ zxc_decode_copy_match()

void zxc_decode_copy_match ( uint8_t * d_ptr,
const uint32_t off,
const uint64_t ml )
inlinestatic

Copies an ml-byte LZ match from d_ptr-off to d_ptr, handling overlap.

The source d_ptr-off may overlap the destination (the LZ repeat case), so the copy strategy is chosen by back-reference distance:

  • off >= 32 : 32-byte wild copies (no overlap within a chunk);
  • off == 1 : single-byte run via zxc_decode_fill_run;
  • otherwise 2-31 : pattern-replicating overlap copy.

The 32 and 16 below are the widths of zxc_copy32 and zxc_copy16 – a copy may never be wider than the distance it reads back, or it would read bytes it is still writing. They are not ZXC_PAD_SIZE, which measures the overshoot the caller leaves writable and merely happens to equal 32.

Like zxc_decode_copy_literals it may overshoot up to 31 bytes past ml, so d_ptr must have ZXC_PAD_SIZE bytes of headroom. d_ptr is taken by value; the caller advances its cursor by ml.

Parameters
[in,out]d_ptrOutput cursor; the match source is d_ptr-off. Must have ZXC_PAD_SIZE bytes of overshoot headroom.
[in]offResolved (bias-removed) back-reference distance, >= 1.
[in]mlMatch length in bytes (>= ZXC_LZ_MIN_MATCH_LEN).

Definition at line 437 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE, zxc_decode_copy_overlap_run32(), zxc_decode_fill_run(), and zxc_wild_copy32().

Referenced by zxc_decode_block_pak_impl(), and zxc_decode_copy_match_spl().

◆ zxc_decode_copy_match_exact()

void zxc_decode_copy_match_exact ( uint8_t * d_ptr,
const uint8_t * match_src,
const size_t off,
const size_t ml )
static

Exact-size match copy for the tail loops (no overshoot headroom).

The tail/remaining-sequence loops validate against d_end exactly, so the wild-copy ladder above (which overshoots up to ZXC_PAD_SIZE) is off limits there. For overlapping matches this expands the run by doubling: every ZXC_MEMCPY source range ends at or before its destination start, and nothing is written past d_ptr+ml. Since each copied prefix length is a multiple of off, the result is byte-identical to the naive per-byte copy, in O(log(ml/off)) calls instead of O(ml) iterations.

Parameters
[in,out]d_ptrOutput cursor (match source is d_ptr-off).
[in]match_srcMatch source, equal to d_ptr-off.
[in]offBack-reference distance, >= 1.
[in]mlMatch length in bytes.

Definition at line 508 of file zxc_decompress.c.

References ZXC_NOINLINE.

Referenced by zxc_decode_block_pak_impl(), and zxc_decode_block_spl_impl().

◆ zxc_decode_copy_match_short()

void zxc_decode_copy_match_short ( uint8_t * d_ptr,
const uint32_t off,
const uint64_t ml )
inlinestatic

Match copy for a length that cannot exceed 32 bytes.

zxc_decode_copy_match without its length ladder, which a bounded ml makes unreachable. Below 32 a full-width copy would read destination bytes not written yet, hence the overlap form.

The 32 is the width of zxc_copy32, not ZXC_PAD_SIZE - they only happen to share a value.

Parameters
[in,out]d_ptrOutput cursor; match source is d_ptr-off. Needs ZXC_PAD_SIZE bytes of overshoot headroom.
[in]offResolved back-reference distance, >= 1.
[in]mlMatch length, <= 32 (caller's obligation).

Definition at line 464 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE, zxc_copy32(), zxc_decode_copy_overlap_short(), and zxc_decode_fill_run().

Referenced by zxc_decode_copy_match_spl().

◆ zxc_decode_copy_match_spl()

void zxc_decode_copy_match_spl ( uint8_t * d_ptr,
const uint32_t off,
const uint64_t ml )
inlinestatic

SPL match copy: bounded form when the ml nibble did not saturate.

The test is exact (the escape always yields more) and statically decidable from either predecessor, so jump threading folds it away. PAK uses zxc_decode_copy_match directly - its inline ml reaches 259.

Definition at line 483 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE, zxc_decode_copy_match(), zxc_decode_copy_match_short(), and ZXC_SPL_MAX_INLINE_ML.

Referenced by zxc_decode_block_spl_impl().

◆ zxc_decode_copy_overlap_run32()

void zxc_decode_copy_overlap_run32 ( uint8_t * dst,
const uint32_t off,
const uint64_t ml )
inlinestatic

Copies an ml-byte run of period off (2..31) with 32-byte stores.

Builds the run's first 32 bytes in a register pair, then stores them repeatedly. Nothing is re-shuffled: advancing by a multiple of off keeps the pattern in phase.

Replaced a 16-byte form that reloaded the source every iteration, which for offsets of 17 and up straddled two of its own recent stores and stalled on store forwarding - worth up to 7x on periodic data.

Reading 32 source bytes touches destination bytes not written yet on the smallest offsets. They are never selected (every mask index is < off) and sit inside the ZXC_PAD_SIZE headroom the caller owes.

May overshoot up to 31 bytes past ml, like the 32-byte copy ladder.

Parameters
[out]dstOutput cursor; the run source is dst - off.
[in]offBack-reference distance, in [2, 31].
[in]mlRun length in bytes (>= 1).

Definition at line 148 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE, zxc_copy16(), and zxc_overlap_strides32.

Referenced by zxc_decode_copy_match().

◆ zxc_decode_copy_overlap_short()

void zxc_decode_copy_overlap_short ( uint8_t * dst,
const uint32_t off,
const uint64_t ml )
inlinestatic

Overlap copy for a run of period off (2..31) bounded by ml <= 32.

Bounded length buys two things over zxc_decode_copy_overlap_run32(): the loop disappears, and the upper 16 bytes are skipped whenever ml allows - which is most of the time, SPL matches averaging 7 to 11 bytes here.

The lower half never needs the second source register: its mask indices are i % off for i < 16, hence always below 16. Only the upper half can reach past 15, and only when off > 16.

Overshoots to 16 or 32 bytes, so the caller owes ZXC_PAD_SIZE of headroom. Reading 32 source bytes is safe because no mask index reaches dst (all are < off).

Parameters
[out]dstOutput cursor; the run source is dst - off.
[in]offBack-reference distance, in [2, 31].
[in]mlMatch length, <= 32.

Definition at line 243 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE.

Referenced by zxc_decode_copy_match_short().

◆ zxc_decode_fill_run()

void zxc_decode_fill_run ( uint8_t * dst,
const uint8_t byte,
const uint64_t ml )
inlinestatic

Fills an ml-byte single-byte run (LZ offset == 1) with wild stores.

Splats byte into a vector register and emits 32-byte chunks, avoiding a libc memset call on the typically short runs of the hot path. Like the other run copiers it may overshoot up to 31 bytes past ml; the caller must guarantee ZXC_PAD_SIZE bytes of headroom. Falls back to ZXC_MEMSET on non-SIMD builds.

Parameters
[out]dstOutput cursor.
[in]byteByte value to replicate.
[in]mlRun length in bytes (>= 1).

Definition at line 314 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE.

Referenced by zxc_decode_copy_match(), and zxc_decode_copy_match_short().

◆ zxc_decode_lit_pivco()

int zxc_decode_lit_pivco ( const zxc_cctx_t * ctx,
const uint8_t * payload,
const size_t psize,
const size_t required_size )
static

Definition at line 717 of file zxc_decompress.c.

◆ zxc_decode_lit_pivco_dict()

int zxc_decode_lit_pivco_dict ( const zxc_cctx_t * ctx,
const uint8_t * payload,
const size_t psize,
const size_t required_size )
static

Definition at line 738 of file zxc_decompress.c.

◆ zxc_decode_tok_pivco()

int zxc_decode_tok_pivco ( const zxc_cctx_t * ctx,
const uint8_t * payload,
const size_t psize,
const size_t n_tok )
static

Definition at line 753 of file zxc_decompress.c.

◆ zxc_decompress_chunk_wrapper()

int zxc_decompress_chunk_wrapper ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_sz,
uint8_t * dst,
const size_t dst_cap,
const uint64_t block_index )

Public no-dict chunk decoder (decompression hot path).

Decompresses one chunk through the runtime ISA dispatch.

Inlines the plain SPL/PAK decoders via zxc_decompress_chunk_wrapper_body with has_dict=0, so it carries no dict code and matches the dict-free build.

Definition at line 1673 of file zxc_decompress.c.

References zxc_decompress_chunk_wrapper_body().

Referenced by zxc_dstream_decompress(), zxc_seek_mt_job(), zxc_seekable_decompress_range(), and zxc_stream_process().

◆ zxc_decompress_chunk_wrapper_body()

int zxc_decompress_chunk_wrapper_body ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_sz,
uint8_t * dst,
const size_t dst_cap,
const uint64_t block_index,
const int has_dict,
const int safe )
inlinestatic

Shared chunk-decode body: validates the header, decodes, then verifies the optional checksum.

has_dict and safe are compile-time constants: the no-dict instantiation folds the SPL/PAK selection to the plain (inlinable) decoders, so zxc_decompress_chunk_wrapper carries no dict code and matches the dict-free build; the dict and safe instantiations call the NOINLINE _dict / _safe decoders (the strict-tail safe path never carries a dict).

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed block (header + payload + optional checksum).
[in]src_szSize of src in bytes.
[out]dstDestination buffer for the decoded block.
[in]dst_capCapacity of dst in bytes.
[in]block_indexFrame position of the block: the checksum seed.
[in]has_dictCompile-time flag: 1 = dictionary-aware decoders.
[in]safeCompile-time flag: 1 = strict-tail safe decoders.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1605 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE, ZXC_BLOCK_CHECKSUM_SIZE, zxc_block_header_ok(), ZXC_BLOCK_HEADER_SIZE, zxc_checksum(), ZXC_CHECKSUM_RAPIDHASH, zxc_decode_block_pak(), zxc_decode_block_pak_dict(), zxc_decode_block_pak_safe(), zxc_decode_block_spl(), zxc_decode_block_spl_dict(), zxc_decode_block_spl_safe(), ZXC_ERROR_BAD_BLOCK_SIZE, ZXC_ERROR_BAD_BLOCK_TYPE, ZXC_ERROR_BAD_CHECKSUM, ZXC_ERROR_BAD_HEADER, ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_DST_TOO_SMALL, ZXC_ERROR_SRC_TOO_SMALL, and zxc_le32().

Referenced by zxc_decompress_chunk_wrapper(), zxc_decompress_chunk_wrapper_dict(), and zxc_decompress_chunk_wrapper_safe().

◆ zxc_decompress_chunk_wrapper_dict()

int zxc_decompress_chunk_wrapper_dict ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_sz,
uint8_t * dst,
const size_t dst_cap,
const uint64_t block_index )

Public dictionary chunk decoder.

Routes through zxc_decompress_chunk_wrapper_body with has_dict=1, which calls the NOINLINE _dict decoders (slower: dict back-refs read the prepended dictionary). Used only when ctx->dict_size != 0.

Parameters
[in,out]ctxDecompression context (dict prefix in its buffer).
[in]srcCompressed block bytes.
[in]src_szSize of src in bytes.
[out]dstDestination buffer for the decoded block.
[in]dst_capCapacity of dst in bytes.
[in]block_indexFrame position of the block: the checksum seed.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1696 of file zxc_decompress.c.

References zxc_decompress_chunk_wrapper_body().

◆ zxc_decompress_chunk_wrapper_safe()

int zxc_decompress_chunk_wrapper_safe ( const zxc_cctx_t * ctx,
const uint8_t * src,
const size_t src_sz,
uint8_t * dst,
const size_t dst_cap,
const uint64_t block_index )

Public strict-tail safe chunk decoder (dst_cap == exact decoded size).

Exact-capacity variant for the safe block API; dispatch table only.

Routes through zxc_decompress_chunk_wrapper_body with safe=1, which calls the NOINLINE _safe decoders (no bounce buffer, no tail padding); RAW blocks are copied directly. Dict inputs are not handled here (the caller routes them to the bounce-capable path).

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed block bytes.
[in]src_szSize of src in bytes.
[out]dstDestination buffer (capacity == exact decoded size).
[in]dst_capCapacity of dst in bytes.
[in]block_indexFrame position of the block: the checksum seed.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 1720 of file zxc_decompress.c.

References zxc_decompress_chunk_wrapper_body().

◆ zxc_ensure_entropy_scratch()

int zxc_ensure_entropy_scratch ( const zxc_cctx_t * ctx)
static

Ensures the entropy decode scratch (tok_buffer + pivco_scratch) is available before decoding an entropy section.

Heap contexts defer this scratch to the first entropy section (zxc_cctx_alloc_entropy_scratch allocates it once); static workspaces pre-carve it, making this a no-op. The const cast is sound: every context lives in writable memory (heap allocation or caller workspace) - the decode chain is const only because steady-state decoding never mutates the context.

Parameters
[in]ctxDecompression context (mutated on the first entropy block).
Returns
ZXC_OK, or ZXC_ERROR_MEMORY on allocation failure.

Definition at line 712 of file zxc_decompress.c.

References zxc_cctx_alloc_entropy_scratch(), ZXC_NOINLINE, and ZXC_OK.

◆ zxc_wild_copy32()

void zxc_wild_copy32 ( uint8_t * dst,
const uint8_t * src,
const size_t len )
inlinestatic

Copies len bytes in 32-byte wild chunks (the zxc_copy32 width).

Writes at least one chunk and may overshoot len by up to 31 bytes, into the caller's ZXC_PAD_SIZE headroom. The tail loop is counted on purpose: a pointer bound costs one more register in the inlined SPL 4x loop, which clang pays by reloading d_floor from the stack on every sequence.

Parameters
[out]dstOutput cursor.
[in]srcSource, disjoint from dst or at least 32 bytes behind it.
[in]lenBytes to copy, >= 1.

Definition at line 377 of file zxc_decompress.c.

References ZXC_ALWAYS_INLINE, and zxc_copy32().

Referenced by zxc_decode_copy_literals(), and zxc_decode_copy_match().

Variable Documentation

◆ zxc_overlap_strides32

const uint8_t zxc_overlap_strides32[32]
static
Initial value:
= {32, 32, 32, 30, 32, 30, 30, 28, 32, 27, 30,
22, 24, 26, 28, 30, 32, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31}

Store stride for the 32-byte pattern: (32 / off) * off.

Past 16 no second period fits, so the stride is the offset itself and each store's tail gets rewritten by the next one.

Definition at line 122 of file zxc_decompress.c.

Referenced by zxc_decode_copy_overlap_run32().