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

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

Go to the source code of this file.

Macros

#define DECODE_MATCH_SAFE(ml, off, COPY)
 
#define DECODE_MATCH_FAST(ml, off, COPY)
 
#define GLO_OFF8   (gh.enc_off == 1)
 True when the GLO offset stream stores 1-byte offsets, 2-byte otherwise.
 
#define DECODE_GLO_SEQ(LL, ML, OFF, RESERVE, N_REM, DECODE, ON_FAIL)
 Decodes one GLO sequence of a 4x batch: extracts ll/ml, applies the varint extensions with their bounds checks, then emits via DECODE.
 
#define DECODE_GLO_BATCH_4X(DECODE, ON_FAIL)
 One full GLO 4x batch: token word, four offsets (1- or 2-byte form), four DECODE_GLO_SEQ emissions, sequence-count update.
 
#define DECODE_GHI_SEQ(S, RESERVE, N_REM, DECODE, ON_FAIL)
 GHI twin of DECODE_GLO_SEQ, decoding one sequence word S (ll in the top byte, ml bits, 16-bit offset).
 
#define DECODE_GHI_BATCH_4X(DECODE, ON_FAIL, PREFETCH)
 One full GHI 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_glo (uint8_t *d_ptr, const uint32_t off, const uint64_t ml)
 GLO 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_glo_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 GLO tokens (level 7).
 
static int zxc_decode_block_glo_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_glo_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_glo_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 GLO (General Low) block decoder body, shared by the fast, safe, dictionary and entropy-token variants.
 
static int zxc_decode_block_ghi_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 GHI (General High) block decoder body, shared by the fast, safe and dictionary variants.
 
static int zxc_decode_block_glo (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 GLO block (plain, inlinable path).
 
static int zxc_decode_block_ghi (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 GHI block (plain, inlinable path).
 
static int zxc_decode_block_glo_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 GLO block against a dictionary prefix (cold path).
 
static int zxc_decode_block_ghi_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 GHI block against a dictionary prefix (cold path).
 
static int zxc_decode_block_glo_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 GLO block with the strict-tail safe loop (no wild copies).
 
static int zxc_decode_block_ghi_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 GHI 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: GLO / GHI / 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_GHI_BATCH_4X

#define DECODE_GHI_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_GHI_SEQ(s1, (s2 >> 24) + (s3 >> 24) + (s4 >> 24), 3U, DECODE, ON_FAIL); \
DECODE_GHI_SEQ(s2, (s3 >> 24) + (s4 >> 24), 2U, DECODE, ON_FAIL); \
DECODE_GHI_SEQ(s3, (s4 >> 24), 1U, DECODE, ON_FAIL); \
DECODE_GHI_SEQ(s4, 0, 0U, DECODE, ON_FAIL); \
n_seq -= 4; \
} while (0)
static uint32_t zxc_le32(const void *p)
Reads a little-endian 32-bit value from a possibly unaligned address.

One full GHI 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 675 of file zxc_decompress.c.

◆ DECODE_GHI_SEQ

#define DECODE_GHI_SEQ (   S,
  RESERVE,
  N_REM,
  DECODE,
  ON_FAIL 
)
Value:
do { \
uint64_t ll = (S) >> 24; \
const uint32_t mb = ((S) >> 16) & 0xFF; \
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 GLO twin; GHI's inline ml reaches 259. */ \
if (UNLIKELY(ll + reserve > (size_t)(l_end - l_ptr) || \
ll + ml + (N_REM) * ZXC_GHI_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_GHI_MAX_INLINE_OUT_PER_SEQ + ZXC_PAD_SIZE > \
(size_t)(d_end - d_ptr))) \
ON_FAIL; \
} \
const uint32_t off = ((S) & 0xFFFF) + ZXC_LZ_OFFSET_BIAS; \
/* GHI 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_ML_MASK
Mask to extract Match Length from a GHI sequence.
#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 GHI 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.

GHI twin of DECODE_GLO_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 642 of file zxc_decompress.c.

◆ DECODE_GLO_BATCH_4X

#define DECODE_GLO_BATCH_4X (   DECODE,
  ON_FAIL 
)
Value:
do { \
uint32_t tokens = zxc_le32(t_ptr); \
t_ptr += sizeof(uint32_t); \
uint64_t offs; \
if (GLO_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_GLO_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_GLO_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_GLO_SEQ((tokens & 0x0F00000) >> 20, (tokens & 0x00F0000) >> 16, \
ZXC_LZ_OFFSET_BIAS + (uint32_t)((offs >> 32) & 0xFFFF), (tokens >> 28), 1U, \
DECODE, ON_FAIL); \
DECODE_GLO_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 GLO_OFF8
True when the GLO 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 GLO 4x batch: token word, four offsets (1- or 2-byte form), four DECODE_GLO_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 608 of file zxc_decompress.c.

◆ DECODE_GLO_SEQ

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

Decodes one GLO 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 568 of file zxc_decompress.c.

◆ 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 536 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 527 of file zxc_decompress.c.

◆ GLO_OFF8

#define GLO_OFF8   (gh.enc_off == 1)

True when the GLO 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 553 of file zxc_decompress.c.

Function Documentation

◆ zxc_decode_block_ghi()

static int zxc_decode_block_ghi ( 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 GHI block (plain, inlinable path).

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

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed GHI 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 1474 of file zxc_decompress.c.

◆ zxc_decode_block_ghi_dict()

static int zxc_decode_block_ghi_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 GHI block against a dictionary prefix (cold path).

Wrapper over zxc_decode_block_ghi_impl with safe=0, has_dict=1 (NOINLINE; see zxc_decode_block_glo_dict).

Parameters
[in,out]ctxDecompression context (dict prefix in its buffer).
[in]srcCompressed GHI 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 1514 of file zxc_decompress.c.

◆ zxc_decode_block_ghi_impl()

static int zxc_decode_block_ghi_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 GHI (General High) block decoder body, shared by the fast, safe and dictionary variants.

Decodes a block in the internal GHI 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 1173 of file zxc_decompress.c.

◆ zxc_decode_block_ghi_safe()

static int zxc_decode_block_ghi_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 GHI block with the strict-tail safe loop (no wild copies).

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

Parameters
[in,out]ctxDecompression context.
[in]srcCompressed GHI 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 1556 of file zxc_decompress.c.

◆ zxc_decode_block_glo()

static int zxc_decode_block_glo ( 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 GLO block (plain, inlinable path).

Wrapper over zxc_decode_block_glo_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 GLO 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 1456 of file zxc_decompress.c.

◆ zxc_decode_block_glo_dict()

static int zxc_decode_block_glo_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 GLO block against a dictionary prefix (cold path).

Wrapper over zxc_decode_block_glo_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 GLO 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 1494 of file zxc_decompress.c.

◆ zxc_decode_block_glo_entropy()

static int zxc_decode_block_glo_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 GLO 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 1420 of file zxc_decompress.c.

◆ zxc_decode_block_glo_entropy_dict()

static int zxc_decode_block_glo_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 1427 of file zxc_decompress.c.

◆ zxc_decode_block_glo_entropy_safe()

static int zxc_decode_block_glo_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 1435 of file zxc_decompress.c.

◆ zxc_decode_block_glo_impl()

static int zxc_decode_block_glo_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 GLO (General Low) block decoder body, shared by the fast, safe, dictionary and entropy-token variants.

Decodes a block in the internal GLO 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 787 of file zxc_decompress.c.

◆ zxc_decode_block_glo_safe()

static int zxc_decode_block_glo_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 GLO block with the strict-tail safe loop (no wild copies).

Wrapper over zxc_decode_block_glo_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 GLO 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 1535 of file zxc_decompress.c.

◆ zxc_decode_copy_literals()

static 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 403 of file zxc_decompress.c.

References zxc_wild_copy32().

◆ zxc_decode_copy_match()

static 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 432 of file zxc_decompress.c.

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

Referenced by zxc_decode_copy_match_glo().

◆ zxc_decode_copy_match_exact()

static 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 503 of file zxc_decompress.c.

◆ zxc_decode_copy_match_glo()

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

GLO 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. GHI uses zxc_decode_copy_match directly - its inline ml reaches 259.

Definition at line 478 of file zxc_decompress.c.

References zxc_decode_copy_match(), zxc_decode_copy_match_short(), and ZXC_GLO_MAX_INLINE_ML.

◆ zxc_decode_copy_match_short()

static 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 459 of file zxc_decompress.c.

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

Referenced by zxc_decode_copy_match_glo().

◆ zxc_decode_copy_overlap_run32()

static 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 143 of file zxc_decompress.c.

References zxc_copy16(), and zxc_overlap_strides32.

Referenced by zxc_decode_copy_match().

◆ zxc_decode_copy_overlap_short()

static 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, GLO 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 238 of file zxc_decompress.c.

Referenced by zxc_decode_copy_match_short().

◆ zxc_decode_fill_run()

static 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 309 of file zxc_decompress.c.

Referenced by zxc_decode_copy_match(), and zxc_decode_copy_match_short().

◆ zxc_decode_lit_pivco()

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

Definition at line 709 of file zxc_decompress.c.

◆ zxc_decode_lit_pivco_dict()

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

Definition at line 722 of file zxc_decompress.c.

◆ zxc_decode_tok_pivco()

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

Definition at line 736 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 GLO/GHI 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 1654 of file zxc_decompress.c.

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

◆ zxc_decompress_chunk_wrapper_body()

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 
)
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 GLO/GHI 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 1591 of file zxc_decompress.c.

◆ 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 1676 of file zxc_decompress.c.

◆ 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 1699 of file zxc_decompress.c.

◆ zxc_ensure_entropy_scratch()

static 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 704 of file zxc_decompress.c.

◆ zxc_wild_copy32()

static 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 GLO 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 372 of file zxc_decompress.c.

References 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 117 of file zxc_decompress.c.

Referenced by zxc_decode_copy_overlap_run32().