Author SHA1 Message Date
Muse fbce3467ab experiments: BWT, PPM, dict transform, BCH corruption harness + results
BWT+MTF+rANS (cyclic-doubling SA; linear sort is NOT rotation
order, sentinel does not fix it), order-2 PPM + WNC arithmetic
coder, word-dictionary transform + lzip, bchcheck (BCH t=8
corruption harness), bench1 single-run harness. Results:
BWT text 6.35 / code 12.31 / bin 42.76 (encode ~3 min/8 MB);
PPM text 32.84 / code 30.82 / mixed 39.18; dict+lzip6 code
9.19 vs plain 9.38 (+2.1%, only code wins); BCH: dict+lzip
code blob survives 8 flips (8 corrected, bit-identical), fails
at 32 flips as expected. No family beats lzip on ratio; rANS
core kept as infrastructure, dict conditional on code. Full
tables + verdicts in experiments/README.md. BWT mixed/rand
still benching at commit time.
2026-12-02 11:06:00 +00:00
Muse d7c558dfbf experiments: rANS core + order-0 tool, LZ77+rANS (lazy/optimal), bench harness
rANS entropy coder (12-bit probs, 23-bit window) with byte and
general-alphabet layers; standalone order-0 tool (enc 30-38 MB/s)
and an LZ77+rANS prototype (hash chains, 1 MiB window, lazy and
optimal-parse modes). Bench harness + deterministic corpora builder
(text/code/bin/mixed 8 MiB, rand 4 MiB) and baseline numbers:
lzip -6 text 3.56% / code 9.38% / bin 32.61% / mixed 17.41%.
lzrans-lazy: text 6.26% / code 13.52% / bin 41.84%; optimal
parsing recovers only ~1-3% relative, so the gap is LZMA adaptive
entropy modelling, not parsing -- lzrans rejected as a lzip
replacement (see experiments/README.md). All round-trips sha256-verified.
2026-12-02 10:40:12 +00:00
27 changed files with 2184 additions and 0 deletions

No files matched your search

+8
View File
@@ -14,3 +14,11 @@
/testdata/
/testwork/
/testwork-hyper/
# compression experiments (branch experiments/comp)
/experiments/corpora/
/experiments/rans_tool
/experiments/lzrans
/experiments/bwt
/experiments/ppm
/experiments/dict
/experiments/bchcheck
+50
View File
@@ -0,0 +1,50 @@
# experiments/Makefile - prototypes for the lzply compression research.
# Built with max cores via -j; each tool is standalone: `tool c in out`,
# `tool d in out`. Shared rANS core in rans.o.
CC = gcc
CFLAGS = -O3 -Wall -Wextra -march=native
LDLIBS = -llz -lm
TOOLS = rans_tool lzrans bwt ppm dict bchcheck
all: $(TOOLS)
rans.o: rans.c rans.h
$(CC) $(CFLAGS) -c -o $@ rans.c
rans_tool: rans_tool.o rans.o
$(CC) $(CFLAGS) -o $@ rans_tool.o rans.o
lzrans.o: lzrans.c rans.h
$(CC) $(CFLAGS) -c -o $@ lzrans.c
lzrans: lzrans.o rans.o
$(CC) $(CFLAGS) -o $@ lzrans.o rans.o
bwt.o: bwt.c rans.h
$(CC) $(CFLAGS) -c -o $@ bwt.c
bwt: bwt.o rans.o
$(CC) $(CFLAGS) -o $@ bwt.o rans.o
ppm.o: ppm.c
$(CC) $(CFLAGS) -c -o $@ ppm.c
ppm: ppm.o
$(CC) $(CFLAGS) -o $@ ppm.o $(LDLIBS)
dict.o: dict.c
$(CC) $(CFLAGS) -c -o $@ dict.c
dict: dict.o
$(CC) $(CFLAGS) -o $@ dict.o
bchcheck.o: bchcheck.c ../bch.h
$(CC) $(CFLAGS) -I.. -c -o $@ bchcheck.c
bchcheck: bchcheck.o ../bch.o ../bch_polys.h
$(CC) $(CFLAGS) -o $@ bchcheck.o ../bch.o
clean:
rm -f $(TOOLS) *.o
.PHONY: all clean
+134
View File
@@ -0,0 +1,134 @@
# lzply compression experiments (branch experiments/comp)
Prototypes for open-ended compression research. Each tool is standalone:
`tool c|o in out`, `tool d in out`. Shared rANS core in rans.[ch].
`bench.py <name> <enc> <dec> [corpora...]` times (best of 3), sizes,
and sha256-verifies round-trips; `bench1.py` is the single-run variant
(for slow tools); CSVs land in results/.
## Corpora (built by mkcorpora.py, deterministic per machine)
- text: 8 MiB UTF-8 docs from /usr/share/doc + common-licenses
- code: 8 MiB C headers (/usr/include) + this repo's sources
- bin: 8 MiB ELF binaries from /bin + /usr/bin
- mixed: 8 MiB round-robin 64 KiB chunks of text+code+bin
- rand: 4 MiB /dev/urandom (seed 1234), incompressible control
Corpora are NOT committed (regenerate with mkcorpora.py).
All ratios below are compressed/orig %; speeds are MB/s (orig bytes).
## Baseline: lzip (the current lzply stage; same liblz, same levels)
| tool | text | code | bin | mixed | rand | enc MB/s | dec MB/s |
|---|---|---|---|---|---|---|---|
| lzip -1 | 6.22 | 15.13 | 37.83 | 21.58 | 101.36 | 1.4-9.1 | 8.9-71.8 |
| lzip -6 | 3.56 | 9.38 | 32.61 | 17.41 | 101.36 | 0.9-2.3 | 7.9-71.4 |
| lzip -9 | 3.44 | 9.05 | 32.53 | 17.17 | 101.36 | 0.8-1.4 | 9.0-70.3 |
References: xz -6 ~= lzip -6 (text 3.51, code 9.20, bin 32.59,
mixed 17.30); zstd -19 text 3.63, code 9.48, bin 34.81, mixed 18.31;
gzip -9 text 23.48, code 16.40, bin 44.14, mixed 30.06;
bzip2 -9 text 9.25, code 12.55, bin 40.22, mixed 23.53,
dec only 5-9 MB/s.
## ANS: rANS order-0 (rans_tool) — REJECT as standalone, KEEP core
Static per-file freqs, 12-bit probs. Enc 30-38 MB/s, dec 47-62 MB/s,
RSS ~20 MB, but no LZ stage: text 64.40, code 67.62, bin 77.14,
mixed 78.11, rand 100.46. The core is the entropy stage for the BWT
and LZ prototypes below. (Two real bugs found and fixed in the core:
renormalisation threshold must be (L>>12)*f<<8 or the decoder
over-reads; the emitted stream can exceed n bytes, scratch is 2n+64.)
## LZ77 + rANS (lzrans) — REJECT as a lzip replacement
Hash-chain finder, 1 MiB window, lazy (`c`) and optimal-parse (`o`)
modes, deflate-style length codes, log2 distance codes, litlen/dist
rANS streams + raw extra bits.
| mode | text | code | bin | mixed | rand | enc | dec | RSS |
|---|---|---|---|---|---|---|---|---|
| lazy | 6.26 | 13.52 | 41.84 | 23.15 | 100.48 | 0.5-4.4 | 26-73 | 44 MB |
| optimal | 6.19 | 13.05 | — | — | — | 0.4-1.0 | 64-73 | 173 MB |
Optimal parsing recovers only ~1-3% relative, so the gap is not
parsing: it is LZMA's adaptive context entropy model plus deeper
match search and a larger dictionary. A static-table rANS cannot
close a ~75% relative gap on text.
## BWT + MTF + run tokens + rANS (bwt) — REJECT as a lzip replacement
Suffix array by cyclic doubling (see the linear-vs-rotation trap
below), MTF, runs >=4 tokenised as (value, 256, k), 257-symbol rANS.
| corpus | ratio | enc | dec |
|---|---|---|---|
| text | 6.35 | 177 s (0.05 MB/s) | 5.3 MB/s |
| code | 12.31 | 173 s | 6.3 MB/s |
| bin | 42.76 | 137 s | 5.9 MB/s |
| mixed | (bench running) | | |
| rand | (bench running) | | |
RSS ~200 MB. Beats bzip2 on text (6.35 vs 9.25), ~ties on code
(12.31 vs 12.55), loses on bin (42.76 vs 40.22); loses to lzip
everywhere by a wide margin. Encode is ~3 min/8 MB (doubling SA is
memory-latency-bound over 160 MB of rank arrays); decode is 5-6 MB/s
(LF-walk random access).
GOTCHA (real bug, fixed): a linear suffix sort is NOT rotation
order — "a" < "a bandana" linearly, but cyclically
"a bandanabanan" < "abanana bandan". A sentinel does not fix it
("a$" < "a bandana$" truncates the comparison). The SA must use
cyclic doubling (rank[(i+k) % n]).
## PPM order-2 + arithmetic coding (ppm) — REJECT
Order-2/1/0 contexts, escape mass = distinct-symbol count,
Witten-Neal-Cleary 32-bit arithmetic coder, counts updated in all
levels after every symbol. RSS ~80 MB.
| corpus | ratio | enc MB/s | dec MB/s |
|---|---|---|---|
| text | 32.84 | 6.3 | 3.9 |
| code | 30.82 | 6.3 | 4.1 |
| mixed | 39.18 | 4.8 | 3.2 |
Far behind lzip and slow: the bit-level coder plus 256-wide level
scans cost ~6 MB/s encode, ~4 MB/s decode. A simple order-2 PPM
with heavy escape mass is nowhere near PPMd-class.
## Dictionary transform + lzip (dict) — CONDITIONAL, code only
Per-file word dictionary (top 4096 words by saving, transmitted in
the header), words -> 3-byte codes, then lzip -6. RSS ~32 MB.
| corpus | dict+lzip6 | plain lzip6 | delta |
|---|---|---|---|
| text | 3.58 | 3.56 | +0.5% worse |
| code | 9.19 | 9.38 | -2.1% BETTER |
| bin | 33.30 | 32.61 | +2.1% worse |
| mixed | 17.65 | 17.41 | +1.4% worse |
| rand | 101.73 | 101.36 | worse |
Only code wins (+2.1%): long repeated identifiers survive as codes
better than as LZ matches. Everywhere else the dictionary costs
more than it saves (lzip already captures word repeats). Not a
default; a code-only optional preprocessing stage at most.
(Gotcha fixed during the build: words are sorted by count, so the
saving filter must `continue`, not `break`, or a short frequent
word like "the" discards the whole dictionary.)
## BCH end-to-end (bchcheck) — winners survive error correction
Blob -> BCH(t=8, lzply -e 4 level) -> flip N bits -> decode ->
decompress -> sha256 check.
- dict+lzip code blob (770,519 B): poly 0050, stored 770,563
(+44 B parity, 0.006%). 8 flips -> 8 corrected, full pipeline
recovers bit-identical. 32 flips -> decode FAIL (exceeds t=8 per
block; honest failure mode).
- plain lzip6 code blob (787,213 B): 8 flips -> 8 corrected,
recovered + decoded bit-identical.
## Verdicts (bottom line)
- No family beats lzip on ratio. lzip/LZMA's adaptive entropy
model + deep match search + large dictionary is very hard to beat
with a from-scratch prototype in this scope.
- KEEP: the rANS core (fast, correct, reusable as an entropy stage).
- CONDITIONAL: dict transform, code-only (+2.1% on code).
- REJECT: lzrans (lazy + optimal), BWT, PPM order-2, rANS order-0
standalone. Honest negatives, numbers above.
- Test totals: 334 timed bench invocations, all sha256-verified OK
(plus ~25 manual small-input round-trips and 3 BCH runs: 2 pass,
1 expected fail at 32 flips).
## Notes / environment
- hot_newyork (172.93.49.176), 8 cores, 30 GB RAM; server load was
10-17 during the runs (shared with other agents), so absolute
MB/s figures are load-dependent; ratios are exact.
- hot_chicago verification pending: its SSH stopped accepting new
sessions Dec 2 (sshd can't fork under load); a retry agent is
probing every 15 min. No Chicago numbers yet.
- The other lz_rs/deep-repair coordinator works in /opt/lzply/src
and /root/lzrs-verify/lzply; untouched by this work. No
Reed-Solomon was (re-)implemented here.
+88
View File
@@ -0,0 +1,88 @@
/* bchcheck.c - BCH end-to-end corruption test for experiment blobs.
* Takes a compressed blob, BCH-encodes it (t=8 by default, the lzply
* -e 4 level; largest t=8 poly if the blob exceeds one m=22 block),
* flips N random bits (seeded), decodes, and writes the recovered
* blob. Exit 0 if the recovered blob is byte-identical to the input,
* 1 if decode failed or the blob differs, 2 on usage/IO error.
* Usage: bchcheck blob nflips seed out [t]
* The caller then decompresses `out` with the prototype and checks
* the sha256 round-trip, proving the compressed stream survives BCH.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "bch.h"
static uint8_t *slurp(const char *path, size_t *n) {
FILE *f = fopen(path, "rb");
if (!f) { perror(path); exit(2); }
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *b = malloc(sz ? (size_t)sz : 1);
if (!b) exit(2);
if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) exit(2);
fclose(f);
*n = (size_t)sz;
return b;
}
/* Largest-capacity poly with the requested t (m=22 wins). */
static const bch_poly_t *largest_t_poly(int t) {
const bch_poly_t *best = NULL;
size_t bestcap = 0;
for (uint16_t id = 0x0001; id <= 0x0052; id++) {
const bch_poly_t *p = bch_poly_by_id(id);
if (!p || p->t != t) continue;
size_t cap = bch_max_data(p);
if (cap > bestcap) { bestcap = cap; best = p; }
}
return best;
}
int main(int argc, char **argv) {
if (argc < 5 || argc > 6) {
fprintf(stderr, "usage: %s blob nflips seed out [t]\n", argv[0]);
return 2;
}
int t = argc == 6 ? atoi(argv[5]) : 8;
long nflips = atol(argv[2]);
unsigned seed = (unsigned)atoi(argv[3]);
size_t n;
uint8_t *blob = slurp(argv[1], &n);
const bch_poly_t *poly = bch_poly_for_data(n, t);
if (!poly) poly = largest_t_poly(t);
if (!poly) { fprintf(stderr, "bchcheck: no poly for t=%d\n", t); return 2; }
size_t slen = bch_encoded_len(n, poly);
uint8_t *stored = malloc(slen ? slen : 1);
uint8_t *rec = malloc(n ? n : 1);
if (!stored || !rec) return 2;
if (bch_encode(blob, n, poly, stored) != slen) {
fprintf(stderr, "bchcheck: encode failed\n");
return 2;
}
/* xorshift64 PRNG for flip positions (seeded, deterministic) */
uint64_t xs = (uint64_t)seed * 0x9E3779B97F4A7C15ULL + 0x123456789ULL;
for (long k = 0; k < nflips; k++) {
xs ^= xs << 13; xs ^= xs >> 7; xs ^= xs << 17;
size_t bit = (size_t)(xs % (uint64_t)(slen * 8));
stored[bit / 8] ^= (uint8_t)(0x80u >> (bit % 8));
}
int corrected = 0;
int rc = bch_decode(stored, slen, n, poly, rec, &corrected);
fprintf(stderr, "bchcheck: poly=%04x blob=%zu stored=%zu overhead=%.1f%% "
"flips=%ld corrected=%d decode=%s\n",
poly->id, n, slen, 100.0 * (double)(slen - n) / (double)n,
nflips, corrected, rc == 0 ? "OK" : "FAIL");
if (rc != 0) return 1;
FILE *f = fopen(argv[4], "wb");
if (!f) { perror(argv[4]); return 2; }
if (n && fwrite(rec, 1, n, f) != n) return 2;
fclose(f);
if (memcmp(blob, rec, n) != 0) {
fprintf(stderr, "bchcheck: recovered blob DIFFERS\n");
return 1;
}
return 0;
}
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""bench.py <name> <enc-template> <dec-template> [corpus...]
Templates use {in} {out} {dec}; run via /bin/sh. Times encode/decode
(wall, best of 3 for encode, best of 3 for decode), records ratio,
MB/s, sha256 round-trip. Appends CSV rows to results/<name>.csv and
prints a table. Encode failure on a corpus records 'FAIL'.
"""
import subprocess, sys, os, time, hashlib, csv
EXP = os.path.dirname(os.path.abspath(__file__))
CORP = os.path.join(EXP, "corpora")
RES = os.path.join(EXP, "results")
TMP = "/tmp/expbench"
os.makedirs(TMP, exist_ok=True)
os.makedirs(RES, exist_ok=True)
def sha(p):
h = hashlib.sha256()
with open(p, "rb") as fh:
for c in iter(lambda: fh.read(1 << 20), b""):
h.update(c)
return h.hexdigest()
def run(cmd, timeout=600):
t0 = time.perf_counter()
r = subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
return r.returncode, time.perf_counter() - t0
def bench_one(name, enc, dec, corpus):
src = os.path.join(CORP, corpus)
n = os.path.getsize(src)
want = sha(src)
out = os.path.join(TMP, name + ".out")
decf = os.path.join(TMP, name + ".dec")
for f in (out, decf):
if os.path.exists(f):
os.unlink(f)
best_enc = None
for _ in range(3):
rc, dt = run(enc.format(**{"in": src, "out": out, "dec": decf}))
if rc != 0:
return (corpus, n, None, None, None, "ENCFAIL")
best_enc = dt if best_enc is None else min(best_enc, dt)
csize = os.path.getsize(out)
best_dec = None
for _ in range(3):
if os.path.exists(decf):
os.unlink(decf)
rc, dt = run(dec.format(**{"in": src, "out": out, "dec": decf}))
if rc != 0:
return (corpus, n, csize, None, None, "DECFAIL")
best_dec = dt if best_dec is None else min(best_dec, dt)
ok = sha(decf) == want
return (corpus, n, csize, best_enc, best_dec, "OK" if ok else "MISMATCH")
def main():
name, enc, dec = sys.argv[1], sys.argv[2], sys.argv[3]
corpora = sys.argv[4:] or ["text", "code", "bin", "mixed", "rand"]
csvp = os.path.join(RES, name + ".csv")
new = not os.path.exists(csvp)
print(f"== {name}: enc=[{enc}] dec=[{dec}]")
print(f"{'corpus':8s} {'orig':>9s} {'comp':>9s} {'ratio%':>7s} {'encMB/s':>8s} {'decMB/s':>8s} status")
with open(csvp, "a", newline="") as fh:
w = csv.writer(fh)
if new:
w.writerow(["tool", "corpus", "orig", "comp", "ratio_pct",
"enc_s", "dec_s", "enc_MBs", "dec_MBs", "status"])
for c in corpora:
corpus, n, cs, es, ds, st = bench_one(name, enc, dec, c)
if cs is None:
print(f"{c:8s} {n:9d} {'-':>9s} {'-':>7s} {'-':>8s} {'-':>8s} {st}")
w.writerow([name, c, n, "", "", "", "", "", "", st])
continue
ratio = 100.0 * cs / n
emb = n / 1e6 / es if es else 0
dmb = n / 1e6 / ds if ds else 0
print(f"{c:8s} {n:9d} {cs:9d} {ratio:7.2f} {emb:8.1f} {dmb:8.1f} {st}")
w.writerow([name, c, n, cs, f"{ratio:.2f}", f"{es:.3f}", f"{ds:.3f}",
f"{emb:.1f}", f"{dmb:.1f}", st])
print("csv: " + csvp)
if __name__ == "__main__":
main()
+71
View File
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
"""bench1.py: single-run variant of bench.py (for slow tools like bwt).
Usage: bench1.py <name> <enc-template> <dec-template> [corpus...]
Appends to results/<name>.csv and prints one line per corpus."""
import subprocess, sys, os, time, hashlib, csv
EXP = os.path.dirname(os.path.abspath(__file__))
CORP = os.path.join(EXP, "corpora")
RES = os.path.join(EXP, "results")
TMP = "/tmp/expbench"
os.makedirs(TMP, exist_ok=True)
os.makedirs(RES, exist_ok=True)
def sha(p):
h = hashlib.sha256()
with open(p, "rb") as fh:
for c in iter(lambda: fh.read(1 << 20), b""):
h.update(c)
return h.hexdigest()
def run(cmd, timeout=3600):
t0 = time.perf_counter()
r = subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
return r.returncode, time.perf_counter() - t0
def main():
name, enc, dec = sys.argv[1], sys.argv[2], sys.argv[3]
corpora = sys.argv[4:] or ["text", "code", "bin", "mixed", "rand"]
csvp = os.path.join(RES, name + ".csv")
new = not os.path.exists(csvp)
print(f"== {name} (single run)", flush=True)
with open(csvp, "a", newline="") as fh:
w = csv.writer(fh)
if new:
w.writerow(["tool", "corpus", "orig", "comp", "ratio_pct",
"enc_s", "dec_s", "enc_MBs", "dec_MBs", "status"])
for c in corpora:
src = os.path.join(CORP, c)
n = os.path.getsize(src)
want = sha(src)
out = os.path.join(TMP, name + ".out")
decf = os.path.join(TMP, name + ".dec")
for f in (out, decf):
if os.path.exists(f):
os.unlink(f)
rc, es = run(enc.format(**{"in": src, "out": out, "dec": decf}))
if rc != 0:
print(f"{c}: ENCFAIL", flush=True)
w.writerow([name, c, n, "", "", "", "", "", "", "ENCFAIL"])
continue
cs = os.path.getsize(out)
if os.path.exists(decf):
os.unlink(decf)
rc, ds = run(dec.format(**{"in": src, "out": out, "dec": decf}))
if rc != 0:
print(f"{c}: DECFAIL", flush=True)
w.writerow([name, c, n, cs, "", "", "", "", "", "DECFAIL"])
continue
ok = sha(decf) == want
st = "OK" if ok else "MISMATCH"
ratio = 100.0 * cs / n
print(f"{c:8s} {n:9d} {cs:9d} {ratio:6.2f}% enc {es:7.1f}s "
f"({n/1e6/es:5.1f} MB/s) dec {ds:6.1f}s ({n/1e6/ds:5.1f} MB/s) {st}",
flush=True)
w.writerow([name, c, n, cs, f"{ratio:.2f}", f"{es:.3f}", f"{ds:.3f}",
f"{n/1e6/es:.1f}", f"{n/1e6/ds:.1f}", st])
print("csv: " + csvp, flush=True)
if __name__ == "__main__":
main()
+273
View File
@@ -0,0 +1,273 @@
/* bwt.c - BWT + MTF + run-length tokens + rANS prototype.
* Classic bzip2-style pipeline, written from scratch: suffix array by
* doubling with counting sort, Burrows-Wheeler transform, move-to-front,
* runs of >=4 identical MTF values tokenised as (value, 256, k),
* rANS over a 257-symbol alphabet.
* Usage: bwt c in out | bwt d in out
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "rans.h"
#define NSYM 257
#define RUN_SYM 256
static uint8_t *slurp(const char *path, size_t *n) {
FILE *f = fopen(path, "rb");
if (!f) { perror(path); exit(2); }
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *b = malloc(sz ? (size_t)sz : 1);
if (!b) exit(2);
if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) exit(2);
fclose(f);
*n = (size_t)sz;
return b;
}
static void spit(const char *path, const uint8_t *b, size_t n) {
FILE *f = fopen(path, "wb");
if (!f) { perror(path); exit(2); }
if (n && fwrite(b, 1, n, f) != n) exit(2);
fclose(f);
}
static void put32(uint8_t *p, uint32_t v) {
p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8);
p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24);
}
static uint32_t get32(const uint8_t *p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
/* Counting-sort SA by key array key[SA[i]] (keys in [0, kmax]).
* sa2 is scratch. */
static void csort(int32_t *sa, int32_t *sa2, const int32_t *key, size_t n,
int32_t kmax, int32_t *cnt) {
memset(cnt, 0, (size_t)(kmax + 1) * sizeof *cnt);
for (size_t i = 0; i < n; i++) cnt[key[sa[i]]]++;
int32_t sum = 0;
for (int32_t k = 0; k <= kmax; k++) {
int32_t c = cnt[k];
cnt[k] = sum;
sum += c;
}
for (size_t i = 0; i < n; i++) {
int32_t s = sa[i];
sa2[cnt[key[s]]++] = s;
}
memcpy(sa, sa2, n * sizeof *sa);
}
/* Suffix array of the CYCLIC rotations, by doubling with wraparound
* rank lookups: rotation i is compared via data[i+k mod n], so the
* order is rotation order (what BWT needs), not linear-suffix order.
* A linear sort puts "a" before "a bandana", but cyclically
* "a bandanabanan" < "abanana bandan"; a sentinel does not fix this
* either ("a$" < "a bandana$" truncates the comparison). Periodic
* rotations stay tied, which is harmless: identical rotations have
* identical BWT chars. */
static int32_t *build_sa(const uint8_t *d, size_t n) {
int32_t *sa = malloc(n * sizeof *sa);
int32_t *sa2 = malloc(n * sizeof *sa2);
int32_t *rank = malloc(n * sizeof *rank);
int32_t *tmp = malloc(n * sizeof *tmp);
int32_t *key = malloc(n * sizeof *key);
int32_t *cnt = malloc((n + 258) * sizeof *cnt);
if (!sa || !sa2 || !rank || !tmp || !key || !cnt) exit(2);
int32_t maxrank = 0;
for (size_t i = 0; i < n; i++) {
sa[i] = (int32_t)i;
rank[i] = d[i];
if (d[i] > maxrank) maxrank = d[i];
}
for (size_t k = 1; k < n; k *= 2) {
/* sort by second key rank[(i+k) % n], then first key rank[i];
* (i+k) % n without division: i+k < 2n always (k < n). */
for (size_t i = 0; i + k < n; i++) key[i] = rank[i + k] + 1;
for (size_t i = n - k; i < n; i++) key[i] = rank[i + k - n] + 1;
csort(sa, sa2, key, n, maxrank + 1, cnt);
for (size_t i = 0; i < n; i++) key[i] = rank[i] + 1;
csort(sa, sa2, key, n, maxrank + 1, cnt);
tmp[sa[0]] = 0;
int32_t r = 0;
for (size_t i = 1; i < n; i++) {
int32_t x = sa[i - 1], y = sa[i];
size_t x2 = (size_t)x + k; if (x2 >= n) x2 -= n;
size_t y2 = (size_t)y + k; if (y2 >= n) y2 -= n;
if (rank[x] != rank[y] || rank[x2] != rank[y2]) r++;
tmp[y] = r;
}
memcpy(rank, tmp, n * sizeof *rank);
maxrank = r;
if (maxrank == (int32_t)n - 1) break;
}
free(sa2); free(rank); free(tmp); free(key); free(cnt);
return sa;
}
static int encode(const char *inpath, const char *outpath) {
size_t n;
uint8_t *in = slurp(inpath, &n);
uint32_t primary = 0;
uint8_t *bwt = malloc(n ? n : 1);
if (!bwt) return 2;
if (n > 0) {
int32_t *sa = build_sa(in, n);
for (size_t i = 0; i < n; i++) {
size_t sv = (size_t)sa[i];
if (sv == 0) primary = (uint32_t)i;
bwt[i] = in[(sv + n - 1) % n];
}
free(sa);
}
/* MTF */
uint8_t *mtf = malloc(n ? n : 1);
uint8_t lst[256];
for (int i = 0; i < 256; i++) lst[i] = (uint8_t)i;
for (size_t i = 0; i < n; i++) {
uint8_t c = bwt[i];
int pos = 0;
while (lst[pos] != c) pos++;
mtf[i] = (uint8_t)pos;
memmove(lst + 1, lst, (size_t)pos);
lst[0] = c;
}
free(bwt);
/* run tokenisation into 257-symbol stream */
uint16_t *tok = malloc((2 * n + 2) * sizeof *tok);
if (!tok) return 2;
size_t nt = 0, i = 0;
while (i < n) {
size_t j = i + 1;
while (j < n && mtf[j] == mtf[i]) j++;
size_t run = j - i;
if (run < 4) {
for (size_t k = 0; k < run; k++) tok[nt++] = mtf[i];
} else {
tok[nt++] = mtf[i];
size_t rem = run - 1;
while (rem > 0) {
size_t chunk = rem > 255 ? 255 : rem;
tok[nt++] = RUN_SYM;
tok[nt++] = (uint16_t)chunk;
rem -= chunk;
}
}
i = j;
}
free(mtf);
uint16_t freq[NSYM];
rans_freq_n(tok, nt, NSYM, freq);
uint8_t *stream = malloc(2 * nt + 64);
if (!stream) return 2;
size_t slen = rans_encode_raw_n(tok, nt, freq, NSYM, stream);
size_t hdr = 4 + 4 + 4 + 4 + NSYM * 2;
uint8_t *out = malloc(hdr + slen);
if (!out) return 2;
uint8_t *p = out;
memcpy(p, "BWTX", 4); p += 4;
put32(p, (uint32_t)n); p += 4;
put32(p, primary); p += 4;
put32(p, (uint32_t)nt); p += 4;
for (int s = 0; s < NSYM; s++) {
p[0] = (uint8_t)freq[s]; p[1] = (uint8_t)(freq[s] >> 8); p += 2;
}
memcpy(p, stream, slen); p += slen;
spit(outpath, out, (size_t)(p - out));
return 0;
}
static int decode(const char *inpath, const char *outpath) {
size_t n;
uint8_t *in = slurp(inpath, &n);
size_t hdr = 4 + 4 + 4 + 4 + NSYM * 2;
if (n < hdr || memcmp(in, "BWTX", 4) != 0) {
fprintf(stderr, "bwt: bad magic/header\n");
return 3;
}
const uint8_t *p = in + 4;
uint32_t orig = get32(p); p += 4;
uint32_t primary = get32(p); p += 4;
uint32_t nt = get32(p); p += 4;
uint16_t freq[NSYM];
for (int s = 0; s < NSYM; s++) {
freq[s] = (uint16_t)(p[0] | (p[1] << 8)); p += 2;
}
uint16_t *tok = malloc((nt ? nt : 1) * sizeof *tok);
if (!tok) return 2;
if (rans_decode_raw_n(p, n - hdr, freq, NSYM, tok, nt) != 0) {
fprintf(stderr, "bwt: token stream corrupt\n");
return 3;
}
/* expand runs to MTF values */
uint8_t *mtf = malloc(orig ? orig : 1);
if (!mtf) return 2;
size_t m = 0;
uint8_t last = 0;
for (uint32_t i = 0; i < nt; i++) {
uint16_t s = tok[i];
if (s == RUN_SYM) {
if (i + 1 >= nt) { fprintf(stderr, "bwt: truncated run\n"); return 3; }
uint16_t k = tok[++i];
if (m + k > orig) { fprintf(stderr, "bwt: run overrun\n"); return 3; }
memset(mtf + m, last, k);
m += k;
} else {
if (m >= orig) { fprintf(stderr, "bwt: mtf overrun\n"); return 3; }
last = (uint8_t)s;
mtf[m++] = last;
}
}
if (m != orig) { fprintf(stderr, "bwt: short mtf %zu != %u\n", m, orig); return 3; }
free(tok);
/* inverse MTF */
uint8_t *bwt = malloc(orig ? orig : 1);
uint8_t lst[256];
for (int i = 0; i < 256; i++) lst[i] = (uint8_t)i;
for (size_t k = 0; k < orig; k++) {
uint8_t c = lst[mtf[k]];
bwt[k] = c;
memmove(lst + 1, lst, mtf[k]);
lst[0] = c;
}
free(mtf);
/* inverse BWT via LF-mapping */
uint8_t *out = malloc(orig ? orig : 1);
if (!out) return 2;
if (orig > 0) {
int32_t cnt[256] = {0};
for (size_t k = 0; k < orig; k++) cnt[bwt[k]]++;
int32_t C[256];
int32_t sum = 0;
for (int c = 0; c < 256; c++) { C[c] = sum; sum += cnt[c]; }
int32_t *lf = malloc(orig * sizeof *lf);
if (!lf) return 2;
int32_t occ[256] = {0};
for (size_t k = 0; k < orig; k++) {
uint8_t c = bwt[k];
lf[k] = C[c] + occ[c]++;
}
size_t idx = primary;
for (size_t k = orig; k-- > 0;) {
out[k] = bwt[idx];
idx = (size_t)lf[idx];
}
free(lf);
}
spit(outpath, out, orig);
return 0;
}
int main(int argc, char **argv) {
if (argc != 4 || (argv[1][0] != 'c' && argv[1][0] != 'd')) {
fprintf(stderr, "usage: %s c|d in out\n", argv[0]);
return 1;
}
return argv[1][0] == 'c' ? encode(argv[2], argv[3])
: decode(argv[2], argv[3]);
}
+260
View File
@@ -0,0 +1,260 @@
/* dict.c - word-dictionary transform (preprocessing stage for lzip).
* Builds a per-file dictionary of the most frequent words (alnum runs
* of length >= 3), replaces them with 2-byte codes, then the caller
* pipes the output through lzip. The dictionary is transmitted in
* the header, so the cost is honest.
* Format: magic "DICT", u32 orig_len, u16 ndict, then ndict entries
* (u8 len, bytes), then the transformed stream: 0xFF escape byte
* introduces a 2-byte code (little-endian); a literal 0xFF in the
* data is escaped as 0xFF 0xFF 0xFF (escape + code 0xFFFF = literal
* 0xFF); other bytes pass through. Words are maximal [A-Za-z0-9_]
* runs; only words in the dictionary are replaced.
* Usage: dict c in out | dict d in out
* (then: lzip -6 transformed; decode: lzip -d | dict d)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define ESC 0xFF
#define MAX_DICT 4096
#define MIN_WORD 3
static uint8_t *slurp(const char *path, size_t *n) {
FILE *f = fopen(path, "rb");
if (!f) { perror(path); exit(2); }
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *b = malloc(sz ? (size_t)sz : 1);
if (!b) exit(2);
if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) exit(2);
fclose(f);
*n = (size_t)sz;
return b;
}
static void spit(const char *path, const uint8_t *b, size_t n) {
FILE *f = fopen(path, "wb");
if (!f) { perror(path); exit(2); }
if (n && fwrite(b, 1, n, f) != n) exit(2);
fclose(f);
}
static int is_word(int c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || c == '_';
}
/* word table entry */
typedef struct { char *w; uint32_t count; } went_t;
static int cmp_count(const void *a, const void *b) {
uint32_t ca = ((const went_t *)a)->count, cb = ((const went_t *)b)->count;
return ca < cb ? 1 : ca > cb ? -1 : 0;
}
static uint32_t hash_str(const char *s, size_t n) {
uint32_t h = 2166136261u;
for (size_t i = 0; i < n; i++) { h ^= (uint8_t)s[i]; h *= 16777619u; }
return h;
}
static int encode(const char *inpath, const char *outpath) {
size_t n;
uint8_t *in = slurp(inpath, &n);
/* count words in a hash table (open addressing) */
size_t hcap = 1 << 20;
went_t *ht = calloc(hcap, sizeof *ht);
if (!ht) return 2;
size_t i = 0;
while (i < n) {
if (!is_word(in[i])) { i++; continue; }
size_t j = i;
while (j < n && is_word(in[j])) j++;
size_t len = j - i;
if (len >= MIN_WORD) {
uint32_t h = hash_str((char *)in + i, len) & (hcap - 1);
while (ht[h].w) {
if (strlen(ht[h].w) == len &&
memcmp(ht[h].w, in + i, len) == 0) break;
h = (h + 1) & (hcap - 1);
}
if (!ht[h].w) {
ht[h].w = malloc(len + 1);
memcpy(ht[h].w, in + i, len);
ht[h].w[len] = 0;
}
ht[h].count++;
}
i = j;
}
/* collect, sort by count, keep the top MAX_DICT by SAVING:
* saving = count * (len - 3) - (len + 1) [dict cost]; keep > 0 */
went_t *all = malloc(hcap * sizeof *all);
size_t na = 0;
for (size_t k = 0; k < hcap; k++)
if (ht[k].w) all[na++] = ht[k];
qsort(all, na, sizeof *all, cmp_count);
went_t *dict = malloc(MAX_DICT * sizeof *dict);
size_t nd = 0;
for (size_t k = 0; k < na && nd < MAX_DICT; k++) {
size_t len = strlen(all[k].w);
/* code costs 3 bytes (ESC + 2), word costs len; saving per
* occurrence = len - 3; dict entry costs len + 1 bytes */
int64_t saving = (int64_t)all[k].count * ((int64_t)len - 3) -
(int64_t)(len + 1);
if (saving <= 0) continue; /* sorted by count, not saving */
dict[nd++] = all[k];
}
/* rebuild a lookup hash for the kept dictionary: code = index */
size_t lcap = 1 << 16;
int32_t *look = malloc(lcap * sizeof *look);
if (!look) return 2;
for (size_t k = 0; k < lcap; k++) look[k] = -1;
for (size_t k = 0; k < nd; k++) {
size_t len = strlen(dict[k].w);
uint32_t h = hash_str(dict[k].w, len) & (lcap - 1);
while (look[h] >= 0) h = (h + 1) & (lcap - 1);
look[h] = (int32_t)k;
}
/* transform */
size_t cap = n + 1024;
uint8_t *out = malloc(cap);
if (!out) return 2;
size_t o = 0;
/* make room for the dictionary header */
{
size_t hdr = 10;
for (size_t k = 0; k < nd; k++) hdr += 1 + strlen(dict[k].w);
if (cap < hdr + 16) {
cap = hdr + 16;
out = realloc(out, cap);
if (!out) return 2;
}
}
/* header: magic, orig_len, ndict, entries */
memcpy(out, "DICT", 4); o += 4;
out[o++] = (uint8_t)(n & 0xff);
out[o++] = (uint8_t)((n >> 8) & 0xff);
out[o++] = (uint8_t)((n >> 16) & 0xff);
out[o++] = (uint8_t)((n >> 24) & 0xff);
out[o++] = (uint8_t)(nd & 0xff);
out[o++] = (uint8_t)((nd >> 8) & 0xff);
for (size_t k = 0; k < nd; k++) {
size_t len = strlen(dict[k].w);
out[o++] = (uint8_t)len;
memcpy(out + o, dict[k].w, len);
o += len;
}
/* growable body: recompute with room (header already written) */
i = 0;
while (i < n) {
/* ensure room for the worst case (1 byte -> 3 bytes) + slack */
if (o + 8 > cap) {
cap *= 2;
out = realloc(out, cap);
if (!out) return 2;
}
if (is_word(in[i])) {
size_t j = i;
while (j < n && is_word(in[j])) j++;
size_t len = j - i;
int code = -1;
if (len >= MIN_WORD) {
uint32_t h = hash_str((char *)in + i, len) & (lcap - 1);
while (look[h] >= 0) {
int32_t k = look[h];
if (strlen(dict[k].w) == len &&
memcmp(dict[k].w, in + i, len) == 0) { code = k; break; }
h = (h + 1) & (lcap - 1);
}
}
if (code >= 0) {
out[o++] = ESC;
out[o++] = (uint8_t)(code & 0xff);
out[o++] = (uint8_t)((code >> 8) & 0xff);
} else {
memcpy(out + o, in + i, len);
o += len;
}
i = j;
} else {
uint8_t c = in[i++];
if (c == ESC) {
out[o++] = ESC;
out[o++] = 0xFF;
out[o++] = 0xFF;
} else {
out[o++] = c;
}
}
}
spit(outpath, out, o);
fprintf(stderr, "dict: %zu words kept (of %zu distinct)\n", nd, na);
return 0;
}
static int decode(const char *inpath, const char *outpath) {
size_t n;
uint8_t *in = slurp(inpath, &n);
if (n < 10 || memcmp(in, "DICT", 4) != 0) {
fprintf(stderr, "dict: bad magic/header\n");
return 3;
}
uint32_t orig = (uint32_t)in[4] | ((uint32_t)in[5] << 8) |
((uint32_t)in[6] << 16) | ((uint32_t)in[7] << 24);
uint32_t nd = (uint32_t)in[8] | ((uint32_t)in[9] << 8);
size_t p = 10;
char **dict = malloc((nd ? nd : 1) * sizeof *dict);
if (!dict) return 2;
for (uint32_t k = 0; k < nd; k++) {
if (p >= n) { fprintf(stderr, "dict: truncated dict\n"); return 3; }
size_t len = in[p++];
if (p + len > n) { fprintf(stderr, "dict: truncated dict\n"); return 3; }
dict[k] = malloc(len + 1);
memcpy(dict[k], in + p, len);
dict[k][len] = 0;
p += len;
}
uint8_t *out = malloc(orig ? orig : 1);
if (!out) return 2;
size_t o = 0;
while (p < n) {
uint8_t c = in[p++];
if (c == ESC) {
if (p + 1 >= n) { fprintf(stderr, "dict: truncated code\n"); return 3; }
uint32_t code = (uint32_t)in[p] | ((uint32_t)in[p + 1] << 8);
p += 2;
if (code == 0xFFFF) {
if (o >= orig) return 3;
out[o++] = ESC;
} else {
if (code >= nd) { fprintf(stderr, "dict: bad code\n"); return 3; }
size_t len = strlen(dict[code]);
if (o + len > orig) return 3;
memcpy(out + o, dict[code], len);
o += len;
}
} else {
if (o >= orig) return 3;
out[o++] = c;
}
}
if (o != orig) {
fprintf(stderr, "dict: short output %zu != %u\n", o, orig);
return 3;
}
spit(outpath, out, o);
return 0;
}
int main(int argc, char **argv) {
if (argc != 4 || (argv[1][0] != 'c' && argv[1][0] != 'd')) {
fprintf(stderr, "usage: %s c|d in out\n", argv[0]);
return 1;
}
return argv[1][0] == 'c' ? encode(argv[2], argv[3])
: decode(argv[2], argv[3]);
}
+412
View File
@@ -0,0 +1,412 @@
/* lzrans.c - LZ77 + rANS prototype for the lzply compression research.
* Hash-chain match finder (1 MiB window), lazy matching (`c`) or
* optimal parsing (`o`, DP over match costs; per candidate only its
* full length is considered), deflate-style length codes (29) and
* log2 distance codes (21, dist up to 2^20). Three streams: litlen
* (286 symbols: 0-255 literal, 256-284 length code, 285 end), dist
* (21 symbols), extra bits (raw LSB-first). Each token stream is
* rANS-coded with its own transmitted freq table.
* Usage: lzrans c|o in out | lzrans d in out
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "rans.h"
#define WINDOW (1u << 20)
#define MAX_MATCH 258
#define MIN_MATCH 3
#define HASH_BITS 16
#define HASH_SIZE (1u << HASH_BITS)
#define LITLEN_END 285
static const int len_base[29] = {3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,
43,51,59,67,83,99,115,131,163,195,227,258};
static const int len_extra[29] = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,
4,4,4,4,5,5,5,5,0};
static int len_code_of(int len) {
int c = 0;
while (c < 28 && len >= len_base[c + 1]) c++;
return c;
}
/* dist 1..2^20 -> code 0..19, extra bits = code, base = 1<<code */
static int dist_code_of(uint32_t d) { return 31 - __builtin_clz(d); }
static uint8_t *slurp(const char *path, size_t *n) {
FILE *f = fopen(path, "rb");
if (!f) { perror(path); exit(2); }
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *b = malloc(sz ? (size_t)sz : 1);
if (!b) exit(2);
if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) exit(2);
fclose(f);
*n = (size_t)sz;
return b;
}
static void spit(const char *path, const uint8_t *b, size_t n) {
FILE *f = fopen(path, "wb");
if (!f) { perror(path); exit(2); }
if (n && fwrite(b, 1, n, f) != n) exit(2);
fclose(f);
}
static void put32(uint8_t *p, uint32_t v) {
p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8);
p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24);
}
static uint32_t get32(const uint8_t *p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
/* ---- bit writer (LSB-first) ---- */
typedef struct { uint8_t *buf; size_t len, cap; uint32_t acc; int nbits; } bitw_t;
static void bitw_put(bitw_t *w, uint32_t v, int n) {
w->acc |= v << w->nbits;
w->nbits += n;
while (w->nbits >= 8) {
if (w->len == w->cap) {
w->cap = w->cap ? w->cap * 2 : 4096;
w->buf = realloc(w->buf, w->cap);
if (!w->buf) exit(2);
}
w->buf[w->len++] = (uint8_t)(w->acc & 0xff);
w->acc >>= 8;
w->nbits -= 8;
}
}
static void bitw_flush(bitw_t *w) {
if (w->nbits) {
if (w->len == w->cap) {
w->cap = w->cap ? w->cap * 2 : 4096;
w->buf = realloc(w->buf, w->cap);
if (!w->buf) exit(2);
}
w->buf[w->len++] = (uint8_t)(w->acc & 0xff);
w->acc = 0; w->nbits = 0;
}
}
/* ---- bit reader (LSB-first) ---- */
typedef struct { const uint8_t *buf; size_t len, pos; uint32_t acc; int nbits; } bitr_t;
static uint32_t bitr_get(bitr_t *r, int n) {
while (r->nbits < n) {
uint32_t b = r->pos < r->len ? r->buf[r->pos++] : 0;
r->acc |= b << r->nbits;
r->nbits += 8;
}
uint32_t v = n == 32 ? r->acc : (r->acc & ((n == 0) ? 0u : ((1u << n) - 1)));
r->acc >>= n;
r->nbits -= n;
return v;
}
/* ---- match finder ---- */
typedef struct {
const uint8_t *data;
size_t n;
int32_t head[HASH_SIZE];
int32_t *prev;
} mf_t;
static uint32_t hash4(const uint8_t *p) {
uint32_t v = (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
return (v * 2654435761u) >> (32 - HASH_BITS);
}
static void mf_insert(mf_t *m, size_t pos) {
if (pos + 4 > m->n) return;
uint32_t h = hash4(m->data + pos);
m->prev[pos] = m->head[h];
m->head[h] = (int32_t)pos;
}
/* Longest match at pos (limit capped by caller). Returns 0 if < 3. */
static int mf_find(mf_t *m, size_t pos, int limit, int max_chain,
int *dist_out) {
if (pos + 4 > m->n) return 0;
uint32_t h = hash4(m->data + pos);
int32_t cand = m->head[h];
int best = 2, bestd = 0, chain = 0;
while (cand >= 0 && chain++ < max_chain) {
size_t c = (size_t)cand;
size_t d = pos - c;
if (d > WINDOW) break;
if (best >= 3) {
/* quick reject: byte at best must match */
if (c + (size_t)best >= m->n ||
m->data[c + best] != m->data[pos + best]) {
cand = m->prev[cand];
continue;
}
}
int l = 0;
while (l < limit && pos + (size_t)l < m->n &&
m->data[c + l] == m->data[pos + l]) l++;
if (l > best) {
best = l; bestd = (int)d;
if (best >= limit) break;
}
cand = m->prev[cand];
}
*dist_out = bestd;
return best >= MIN_MATCH ? best : 0;
}
/* ---- token streams ---- */
typedef struct {
uint16_t *litlen; size_t nlit, clit;
uint16_t *dist; size_t ndist, cdist;
bitw_t bits;
} toks_t;
static void toks_init(toks_t *t, size_t n) {
t->clit = n + 2; t->litlen = malloc(t->clit * 2);
t->cdist = n / 3 + 2; t->dist = malloc(t->cdist * 2);
t->nlit = t->ndist = 0;
memset(&t->bits, 0, sizeof t->bits);
if (!t->litlen || !t->dist) exit(2);
}
static void toks_lit(toks_t *t, uint8_t b) { t->litlen[t->nlit++] = b; }
static void toks_match(toks_t *t, int len, int dist) {
int lc = len_code_of(len);
t->litlen[t->nlit++] = (uint16_t)(256 + lc);
int dc = dist_code_of((uint32_t)dist);
t->dist[t->ndist++] = (uint16_t)dc;
bitw_put(&t->bits, (uint32_t)(len - len_base[lc]), len_extra[lc]);
bitw_put(&t->bits, (uint32_t)(dist - (1 << dc)), dc);
}
/* ---- lazy parse ---- */
static void parse_lazy(mf_t *m, toks_t *t) {
size_t n = m->n, pos = 0;
while (pos < n) {
if (n - pos < MIN_MATCH) { toks_lit(t, m->data[pos++]); continue; }
int d1 = 0;
int l1 = mf_find(m, pos, (int)(n - pos < MAX_MATCH ? n - pos : MAX_MATCH),
128, &d1);
if (l1 >= MIN_MATCH) {
mf_insert(m, pos);
int l2 = 0, d2 = 0;
if (n - (pos + 1) >= MIN_MATCH)
l2 = mf_find(m, pos + 1,
(int)(n - pos - 1 < MAX_MATCH ? n - pos - 1 : MAX_MATCH),
128, &d2);
if (l2 > l1) { toks_lit(t, m->data[pos]); pos++; continue; }
toks_match(t, l1, d1);
for (int k = 1; k < l1; k++) mf_insert(m, pos + (size_t)k);
pos += (size_t)l1;
} else {
toks_lit(t, m->data[pos]);
mf_insert(m, pos);
pos++;
}
}
}
/* ---- optimal parse (DP; per candidate only its full length) ---- */
static void parse_opt(mf_t *m, toks_t *t) {
size_t n = m->n;
double *cost = malloc((n + 1) * sizeof *cost);
int32_t *ch_len = malloc((n + 1) * sizeof *ch_len);
int32_t *ch_dist = malloc((n + 1) * sizeof *ch_dist);
if (!cost || !ch_len || !ch_dist) exit(2);
for (size_t i = 0; i <= n; i++) { cost[i] = 1e100; ch_len[i] = 0; ch_dist[i] = 0; }
cost[0] = 0;
for (size_t i = 0; i < n; i++) {
if (cost[i] < 1e99 && n - i >= MIN_MATCH) {
/* literal */
if (cost[i] + 9.0 < cost[i + 1]) {
cost[i + 1] = cost[i] + 9.0; ch_len[i + 1] = 0;
}
/* matches: walk the chain, full length per candidate */
if (i + 4 <= n) {
uint32_t h = hash4(m->data + i);
int32_t cand = m->head[h];
int chain = 0, best = 2;
int limit = (int)(n - i < MAX_MATCH ? n - i : MAX_MATCH);
while (cand >= 0 && chain++ < 32) {
size_t c = (size_t)cand;
size_t d = i - c;
if (d > WINDOW) break;
if (best >= 3 && (c + (size_t)best >= n ||
m->data[c + best] != m->data[i + best])) {
cand = m->prev[cand];
continue;
}
int l = 0;
while (l < limit && m->data[c + l] == m->data[i + l]) l++;
if (l >= MIN_MATCH && l > 2) {
int lc = len_code_of(l);
int dc = dist_code_of((uint32_t)d);
double mc = cost[i] + 8.0 + len_extra[lc] + 5.0 + dc;
if (mc < cost[i + (size_t)l]) {
cost[i + (size_t)l] = mc;
ch_len[i + (size_t)l] = l;
ch_dist[i + (size_t)l] = (int32_t)d;
}
if (l > best) best = l;
}
cand = m->prev[cand];
}
}
}
mf_insert(m, i);
}
/* reconstruct backwards, then reverse */
size_t pos = n, nt = 0;
uint16_t *rl = malloc((n + 1) * 2);
int32_t *rd = malloc((n + 1) * 4);
int32_t *rlen = malloc((n + 1) * 4);
if (!rl || !rd || !rlen) exit(2);
while (pos > 0) {
int32_t l = ch_len[pos];
if (l == 0) { rl[nt] = m->data[pos - 1]; rlen[nt] = 0; rd[nt] = 0; nt++; pos--; }
else { rl[nt] = 0; rlen[nt] = l; rd[nt] = ch_dist[pos]; nt++; pos -= (size_t)l; }
}
for (size_t k = nt; k-- > 0;) {
if (rlen[k] == 0) toks_lit(t, (uint8_t)rl[k]);
else toks_match(t, rlen[k], rd[k]);
}
free(cost); free(ch_len); free(ch_dist); free(rl); free(rd); free(rlen);
}
static int encode(const char *inpath, const char *outpath, int optimal) {
size_t n;
uint8_t *in = slurp(inpath, &n);
mf_t m;
m.data = in; m.n = n;
memset(m.head, 0xff, sizeof m.head);
m.prev = malloc((n ? n : 1) * sizeof *m.prev);
if (!m.prev) return 2;
toks_t t;
toks_init(&t, n);
if (optimal) parse_opt(&m, &t);
else parse_lazy(&m, &t);
t.litlen[t.nlit++] = LITLEN_END;
bitw_flush(&t.bits);
uint16_t freq_ll[286], freq_d[21];
rans_freq_n(t.litlen, t.nlit, 286, freq_ll);
rans_freq_n(t.dist, t.ndist, 21, freq_d);
uint8_t *s_ll = malloc(2 * t.nlit + 64);
uint8_t *s_d = malloc(2 * t.ndist + 64);
if (!s_ll || !s_d) return 2;
size_t sll_len = rans_encode_raw_n(t.litlen, t.nlit, freq_ll, 286, s_ll);
size_t sd_len = rans_encode_raw_n(t.dist, t.ndist, freq_d, 21, s_d);
size_t hdr = 4 + 4 + 1 + 20 + 286 * 2 + 21 * 2;
size_t total = hdr + sll_len + sd_len + t.bits.len;
uint8_t *out = malloc(total ? total : 1);
if (!out) return 2;
uint8_t *p = out;
memcpy(p, "LZRA", 4); p += 4;
put32(p, (uint32_t)n); p += 4;
*p++ = optimal ? 1 : 0;
put32(p, (uint32_t)t.nlit); p += 4;
put32(p, (uint32_t)t.ndist); p += 4;
put32(p, (uint32_t)t.bits.len); p += 4;
put32(p, (uint32_t)sll_len); p += 4;
put32(p, (uint32_t)sd_len); p += 4;
for (int s = 0; s < 286; s++) { p[0] = (uint8_t)freq_ll[s]; p[1] = (uint8_t)(freq_ll[s] >> 8); p += 2; }
for (int s = 0; s < 21; s++) { p[0] = (uint8_t)freq_d[s]; p[1] = (uint8_t)(freq_d[s] >> 8); p += 2; }
memcpy(p, s_ll, sll_len); p += sll_len;
memcpy(p, s_d, sd_len); p += sd_len;
memcpy(p, t.bits.buf, t.bits.len); p += t.bits.len;
spit(outpath, out, (size_t)(p - out));
return 0;
}
static int decode(const char *inpath, const char *outpath) {
size_t n;
uint8_t *in = slurp(inpath, &n);
size_t hdr = 4 + 4 + 1 + 20 + 286 * 2 + 21 * 2;
if (n < hdr || memcmp(in, "LZRA", 4) != 0) {
fprintf(stderr, "lzrans: bad magic/header\n");
return 3;
}
const uint8_t *p = in + 4;
uint32_t orig = get32(p); p += 4;
p++; /* mode */
uint32_t nlit = get32(p); p += 4;
uint32_t ndist = get32(p); p += 4;
uint32_t ebits = get32(p); p += 4;
uint32_t sll_len = get32(p); p += 4;
uint32_t sd_len = get32(p); p += 4;
uint16_t freq_ll[286], freq_d[21];
for (int s = 0; s < 286; s++) { freq_ll[s] = (uint16_t)(p[0] | (p[1] << 8)); p += 2; }
for (int s = 0; s < 21; s++) { freq_d[s] = (uint16_t)(p[0] | (p[1] << 8)); p += 2; }
if ((size_t)(p - in) + sll_len + sd_len + ebits != n) {
fprintf(stderr, "lzrans: truncated file\n");
return 3;
}
uint16_t *litlen = malloc((nlit ? nlit : 1) * 2);
uint16_t *dist = malloc((ndist ? ndist : 1) * 2);
if (!litlen || !dist) return 2;
if (rans_decode_raw_n(p, sll_len, freq_ll, 286, litlen, nlit) != 0) {
fprintf(stderr, "lzrans: litlen stream corrupt\n");
return 3;
}
p += sll_len;
if (rans_decode_raw_n(p, sd_len, freq_d, 21, dist, ndist) != 0) {
fprintf(stderr, "lzrans: dist stream corrupt\n");
return 3;
}
p += sd_len;
uint8_t *out = malloc(orig ? orig : 1);
if (!out) return 2;
bitr_t br = { p, ebits, 0, 0, 0 };
size_t opos = 0, di = 0;
for (uint32_t i = 0; i < nlit; i++) {
uint16_t s = litlen[i];
if (s < 256) {
if (opos >= orig) { fprintf(stderr, "lzrans: overrun\n"); return 3; }
out[opos++] = (uint8_t)s;
} else if (s == LITLEN_END) {
break;
} else {
int lc = s - 256;
if (lc < 0 || lc > 28 || di >= ndist) {
fprintf(stderr, "lzrans: bad token\n");
return 3;
}
int len = len_base[lc] + (int)bitr_get(&br, len_extra[lc]);
int dc = dist[di++];
if (dc < 0 || dc > 20) { fprintf(stderr, "lzrans: bad dist\n"); return 3; }
int d = (1 << dc) + (int)bitr_get(&br, dc);
if ((size_t)d > opos || opos + (size_t)len > orig) {
fprintf(stderr, "lzrans: bad match\n");
return 3;
}
for (int k = 0; k < len; k++) {
out[opos] = out[opos - (size_t)d];
opos++;
}
}
}
if (opos != orig) {
fprintf(stderr, "lzrans: short output %zu != %u\n", opos, orig);
return 3;
}
spit(outpath, out, opos);
return 0;
}
int main(int argc, char **argv) {
if (argc != 4) {
fprintf(stderr, "usage: %s c|o|d in out\n", argv[0]);
return 1;
}
if (argv[1][0] == 'c') return encode(argv[2], argv[3], 0);
if (argv[1][0] == 'o') return encode(argv[2], argv[3], 1);
if (argv[1][0] == 'd') return decode(argv[2], argv[3]);
fprintf(stderr, "usage: %s c|o|d in out\n", argv[0]);
return 1;
}
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/env python3
"""Build deterministic benchmark corpora on the server. Sizes capped.
text: /usr/share/doc text files + common-licenses (English prose/docs)
code: /usr/include headers + this repo sources (C/Markdown)
bin: ELF binaries from /bin, /usr/bin (executables)
mixed: round-robin 64KiB chunks of text+code+bin
rand: /dev/urandom control (incompressible)
"""
import os, glob, hashlib, random
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "corpora")
CAP = 8 * 1024 * 1024
def collect(patterns, cap, text_only=False):
buf = bytearray()
files = []
for p in patterns:
files += sorted(glob.glob(p, recursive=True))
for f in files:
try:
if not os.path.isfile(f):
continue
with open(f, "rb") as fh:
d = fh.read()
except OSError:
continue
if text_only:
try:
d.decode("utf-8")
except UnicodeDecodeError:
continue
buf += d
if len(buf) >= cap:
break
return bytes(buf[:cap])
def write(name, data):
p = os.path.join(OUT, name)
with open(p, "wb") as fh:
fh.write(data)
print(name + ": " + str(len(data)) + " bytes sha256=" + hashlib.sha256(data).hexdigest()[:16])
text = collect(["/usr/share/doc/**/*", "/usr/share/common-licenses/*"], CAP, text_only=True)
write("text", text)
code = collect(["/usr/include/**/*.h", "/opt/lzply-exp-comp/*.c",
"/opt/lzply-exp-comp/*.h", "/opt/lzply-exp-comp/*.md"], CAP)
write("code", code)
bins = []
for d in ("/bin", "/usr/bin"):
for f in sorted(glob.glob(d + "/*")):
try:
with open(f, "rb") as fh:
h = fh.read(4)
if h[:4] == b"\x7fELF":
bins.append(f)
except OSError:
pass
buf = bytearray()
for f in bins:
with open(f, "rb") as fh:
buf += fh.read()
if len(buf) >= CAP:
break
bindata = bytes(buf[:CAP])
write("bin", bindata)
parts = [text, code, bindata]
m = bytearray()
i = 0
while len(m) < CAP:
for part in parts:
off = (i * 65536) % max(1, len(part) - 65536)
m += part[off:off + 65536]
if len(m) >= CAP:
break
i += 1
write("mixed", bytes(m[:CAP]))
rnd = random.Random(1234).randbytes(4 * 1024 * 1024)
write("rand", rnd)
+349
View File
@@ -0,0 +1,349 @@
/* ppm.c - order-2 PPM + arithmetic coding prototype (context mixing).
* Three context levels (order-2, order-1, order-0) with escape to the
* next level; escape mass = number of distinct symbols seen in the
* context. Order -1 is uniform over 256 symbols. The coder is the
* classic Witten-Neal-Cleary 32-bit arithmetic coder (E1/E2/E3).
* Counts are updated in ALL levels after every symbol, on both sides.
* Usage: ppm c in out | ppm d in out
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define ESC 256
#define NSYM 257
static uint8_t *slurp(const char *path, size_t *n) {
FILE *f = fopen(path, "rb");
if (!f) { perror(path); exit(2); }
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *b = malloc(sz ? (size_t)sz : 1);
if (!b) exit(2);
if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) exit(2);
fclose(f);
*n = (size_t)sz;
return b;
}
static void spit(const char *path, const uint8_t *b, size_t n) {
FILE *f = fopen(path, "wb");
if (!f) { perror(path); exit(2); }
if (n && fwrite(b, 1, n, f) != n) exit(2);
fclose(f);
}
/* ---- bit writer (MSB-first) ---- */
typedef struct {
uint8_t *buf; size_t len, cap;
uint32_t acc; int nbits;
} bitw_t;
static void bitw_put(bitw_t *w, int bit) {
w->acc = (w->acc << 1) | (uint32_t)bit;
if (++w->nbits == 8) {
if (w->len == w->cap) {
w->cap = w->cap ? w->cap * 2 : 65536;
w->buf = realloc(w->buf, w->cap);
if (!w->buf) exit(2);
}
w->buf[w->len++] = (uint8_t)w->acc;
w->acc = 0; w->nbits = 0;
}
}
static void bitw_flush(bitw_t *w) {
if (w->nbits) {
w->acc <<= (8 - w->nbits);
if (w->len == w->cap) {
w->cap = w->cap ? w->cap * 2 : 65536;
w->buf = realloc(w->buf, w->cap);
if (!w->buf) exit(2);
}
w->buf[w->len++] = (uint8_t)w->acc;
w->acc = 0; w->nbits = 0;
}
}
/* ---- bit reader (MSB-first, 0 past EOF) ---- */
typedef struct {
const uint8_t *buf; size_t len, pos;
uint32_t acc; int nbits;
} bitr_t;
static int bitr_get(bitr_t *r) {
if (r->nbits == 0) {
uint8_t b = r->pos < r->len ? r->buf[r->pos++] : 0;
r->acc = b; r->nbits = 8;
}
int bit = (int)((r->acc >> 7) & 1);
r->acc <<= 1; r->nbits--;
return bit;
}
/* ---- arithmetic coder ---- */
typedef struct {
uint32_t low, high;
int follow;
bitw_t *w;
} enc_t;
static void enc_bit(enc_t *e, int bit) {
bitw_put(e->w, bit);
while (e->follow > 0) { bitw_put(e->w, !bit); e->follow--; }
}
static void enc_symbol(enc_t *e, uint32_t cum_lo, uint32_t cum_hi, uint32_t total) {
uint64_t range = (uint64_t)(e->high - e->low) + 1;
e->high = e->low + (uint32_t)(range * cum_hi / total) - 1;
e->low = e->low + (uint32_t)(range * cum_lo / total);
for (;;) {
if (e->high < 0x80000000u) {
enc_bit(e, 0);
} else if (e->low >= 0x80000000u) {
enc_bit(e, 1);
e->low -= 0x80000000u; e->high -= 0x80000000u;
} else if (e->low >= 0x40000000u && e->high < 0xC0000000u) {
e->follow++;
e->low -= 0x40000000u; e->high -= 0x40000000u;
} else break;
e->low <<= 1; e->high = (e->high << 1) | 1;
}
}
static void enc_flush(enc_t *e) {
e->follow++;
if (e->low < 0x40000000u) enc_bit(e, 0);
else enc_bit(e, 1);
bitw_flush(e->w);
}
typedef struct {
uint32_t low, high, value;
bitr_t *r;
} dec_t;
static void dec_init(dec_t *d, bitr_t *r) {
d->low = 0; d->high = 0xFFFFFFFFu; d->value = 0; d->r = r;
for (int i = 0; i < 32; i++) d->value = (d->value << 1) | (uint32_t)bitr_get(r);
}
static uint32_t dec_cum(dec_t *d, uint32_t total) {
uint64_t range = (uint64_t)(d->high - d->low) + 1;
return (uint32_t)(((uint64_t)(d->value - d->low + 1) * total - 1) / range);
}
static void dec_update(dec_t *d, uint32_t cum_lo, uint32_t cum_hi, uint32_t total) {
uint64_t range = (uint64_t)(d->high - d->low) + 1;
d->high = d->low + (uint32_t)(range * cum_hi / total) - 1;
d->low = d->low + (uint32_t)(range * cum_lo / total);
for (;;) {
if (d->high < 0x80000000u) {
/* no shift */
} else if (d->low >= 0x80000000u) {
d->value -= 0x80000000u; d->low -= 0x80000000u; d->high -= 0x80000000u;
} else if (d->low >= 0x40000000u && d->high < 0xC0000000u) {
d->value -= 0x40000000u; d->low -= 0x40000000u; d->high -= 0x40000000u;
} else break;
d->low <<= 1; d->high = (d->high << 1) | 1;
d->value = (d->value << 1) | (uint32_t)bitr_get(d->r);
}
}
/* ---- PPM model ----
* cnt0[256], cnt1[256][256], cnt2[65536][256] (uint32), totals and
* distinct-symbol counts per context. ~64 MB for cnt2. */
static uint32_t *cnt2; /* [65536][256] */
static uint32_t cnt1[256][256];
static uint32_t cnt0[256];
static uint32_t tot2[65536];
static uint32_t tot1[256];
static uint32_t tot0;
static uint32_t dist2[65536];
static uint32_t dist1[256];
static uint32_t dist0;
static void model_reset(void) {
memset(cnt1, 0, sizeof cnt1);
memset(cnt0, 0, sizeof cnt0);
memset(tot2, 0, sizeof tot2);
memset(tot1, 0, sizeof tot1);
memset(dist2, 0, sizeof dist2);
memset(dist1, 0, sizeof dist1);
tot0 = 0; dist0 = 0;
if (cnt2) memset(cnt2, 0, (size_t)65536 * 256 * sizeof *cnt2);
}
static void model_update(int b, int p2, int p1) {
uint32_t c2 = (uint32_t)((p2 << 8) | p1);
uint32_t *row2 = cnt2 + (size_t)c2 * 256;
if (row2[b] == 0) dist2[c2]++;
row2[b]++;
tot2[c2]++;
if (cnt1[p1][b] == 0) dist1[p1]++;
cnt1[p1][b]++;
tot1[p1]++;
if (cnt0[b] == 0) dist0++;
cnt0[b]++;
tot0++;
}
/* Cumulative interval of symbol s (0..255) or ESC in a level.
* cnt: per-symbol counts; dist: escape mass; tot: symbol total.
* total = tot + dist. Returns 0 if the symbol has freq 0. */
static int level_interval(const uint32_t *cnt, uint32_t dist, uint32_t tot,
int s, uint32_t *lo, uint32_t *hi, uint32_t *total) {
*total = tot + dist;
uint32_t acc = 0;
if (s == ESC) {
for (int i = 0; i < 256; i++) acc += cnt[i];
*lo = acc; *hi = acc + dist;
return dist > 0;
}
for (int i = 0; i < s; i++) acc += cnt[i];
*lo = acc; *hi = acc + cnt[s];
return cnt[s] > 0;
}
/* Find the symbol for a cum value in a level; fills lo/hi/total. */
static int level_lookup(const uint32_t *cnt, uint32_t dist, uint32_t tot,
uint32_t cum, uint32_t *lo, uint32_t *hi,
uint32_t *total) {
*total = tot + dist;
uint32_t acc = 0;
for (int i = 0; i < 256; i++) {
uint32_t f = cnt[i];
if (cum < acc + f) { *lo = acc; *hi = acc + f; return i; }
acc += f;
}
*lo = acc; *hi = acc + dist;
return ESC;
}
static int encode(const char *inpath, const char *outpath) {
size_t n;
uint8_t *in = slurp(inpath, &n);
cnt2 = malloc((size_t)65536 * 256 * sizeof *cnt2);
if (!cnt2) return 2;
model_reset();
bitw_t w;
memset(&w, 0, sizeof w);
enc_t e = { 0, 0xFFFFFFFFu, 0, &w };
int p2 = 0, p1 = 0;
for (size_t i = 0; i < n; i++) {
int b = in[i];
uint32_t c2 = (uint32_t)((p2 << 8) | p1);
uint32_t *row2 = cnt2 + (size_t)c2 * 256;
uint32_t lo, hi, total;
int done = 0;
if (tot2[c2] > 0) {
if (level_interval(row2, dist2[c2], tot2[c2], b, &lo, &hi, &total)) {
enc_symbol(&e, lo, hi, total);
done = 1;
} else {
level_interval(row2, dist2[c2], tot2[c2], ESC, &lo, &hi, &total);
enc_symbol(&e, lo, hi, total);
}
}
if (!done && tot1[p1] > 0) {
if (level_interval(cnt1[p1], dist1[p1], tot1[p1], b, &lo, &hi, &total)) {
enc_symbol(&e, lo, hi, total);
done = 1;
} else {
level_interval(cnt1[p1], dist1[p1], tot1[p1], ESC, &lo, &hi, &total);
enc_symbol(&e, lo, hi, total);
}
}
if (!done && tot0 > 0) {
if (level_interval(cnt0, dist0, tot0, b, &lo, &hi, &total)) {
enc_symbol(&e, lo, hi, total);
done = 1;
} else {
level_interval(cnt0, dist0, tot0, ESC, &lo, &hi, &total);
enc_symbol(&e, lo, hi, total);
}
}
if (!done) {
/* order -1: uniform over 256 */
enc_symbol(&e, (uint32_t)b, (uint32_t)b + 1, 256);
}
model_update(b, p2, p1);
p2 = p1; p1 = b;
}
enc_flush(&e);
size_t total_out = 8 + w.len;
uint8_t *out = malloc(total_out ? total_out : 1);
if (!out) return 2;
memcpy(out, "PPM2", 4);
out[4] = (uint8_t)(n & 0xff);
out[5] = (uint8_t)((n >> 8) & 0xff);
out[6] = (uint8_t)((n >> 16) & 0xff);
out[7] = (uint8_t)((n >> 24) & 0xff);
memcpy(out + 8, w.buf, w.len);
spit(outpath, out, total_out);
return 0;
}
static int decode(const char *inpath, const char *outpath) {
size_t n;
uint8_t *in = slurp(inpath, &n);
if (n < 8 || memcmp(in, "PPM2", 4) != 0) {
fprintf(stderr, "ppm: bad magic/header\n");
return 3;
}
uint32_t orig = (uint32_t)in[4] | ((uint32_t)in[5] << 8) |
((uint32_t)in[6] << 16) | ((uint32_t)in[7] << 24);
cnt2 = malloc((size_t)65536 * 256 * sizeof *cnt2);
if (!cnt2) return 2;
model_reset();
bitr_t br = { in + 8, n - 8, 0, 0, 0 };
dec_t d;
dec_init(&d, &br);
uint8_t *out = malloc(orig ? orig : 1);
if (!out) return 2;
int p2 = 0, p1 = 0;
for (uint32_t i = 0; i < orig; i++) {
int b = -1;
uint32_t c2 = (uint32_t)((p2 << 8) | p1);
uint32_t *row2 = cnt2 + (size_t)c2 * 256;
uint32_t lo, hi, total, cum;
if (tot2[c2] > 0) {
cum = dec_cum(&d, tot2[c2] + dist2[c2]);
int s = level_lookup(row2, dist2[c2], tot2[c2], cum, &lo, &hi, &total);
dec_update(&d, lo, hi, total);
if (s != ESC) b = s;
}
if (b < 0 && tot1[p1] > 0) {
cum = dec_cum(&d, tot1[p1] + dist1[p1]);
int s = level_lookup(cnt1[p1], dist1[p1], tot1[p1], cum, &lo, &hi, &total);
dec_update(&d, lo, hi, total);
if (s != ESC) b = s;
}
if (b < 0 && tot0 > 0) {
cum = dec_cum(&d, tot0 + dist0);
int s = level_lookup(cnt0, dist0, tot0, cum, &lo, &hi, &total);
dec_update(&d, lo, hi, total);
if (s != ESC) b = s;
}
if (b < 0) {
cum = dec_cum(&d, 256);
b = (int)cum;
dec_update(&d, cum, cum + 1, 256);
}
out[i] = (uint8_t)b;
model_update(b, p2, p1);
p2 = p1; p1 = b;
}
spit(outpath, out, orig);
return 0;
}
int main(int argc, char **argv) {
if (argc != 4 || (argv[1][0] != 'c' && argv[1][0] != 'd')) {
fprintf(stderr, "usage: %s c|d in out\n", argv[0]);
return 1;
}
return argv[1][0] == 'c' ? encode(argv[2], argv[3])
: decode(argv[2], argv[3]);
}
+221
View File
@@ -0,0 +1,221 @@
/* rans.c - rANS entropy coder (see rans.h). Standard rANS:
* encode backwards, x in [2^23, 2^32); decode forwards from the
* 4-byte final state stored at the end of the stream. */
#include "rans.h"
#include <stdlib.h>
#include <string.h>
void rans_freq(const uint8_t *in, size_t n, uint16_t *freq) {
uint32_t cnt[256] = {0};
for (size_t i = 0; i < n; i++) cnt[in[i]]++;
uint32_t total = 0;
for (int s = 0; s < 256; s++) {
/* scale counts to PROB_SCALE, present symbols keep >= 1 */
uint32_t f = n ? (uint32_t)(((uint64_t)cnt[s] * RANS_PROB_SCALE) / n) : 0;
if (cnt[s] && f == 0) f = 1;
freq[s] = (uint16_t)f;
total += f;
}
/* Fix the sum to exactly PROB_SCALE by adjusting the largest. */
int mx = 0;
for (int s = 1; s < 256; s++) if (freq[s] > freq[mx]) mx = s;
if (total < RANS_PROB_SCALE) freq[mx] += (uint16_t)(RANS_PROB_SCALE - total);
else if (total > RANS_PROB_SCALE) {
uint32_t over = total - RANS_PROB_SCALE;
/* take from the largest symbol, never below 1 if present */
uint32_t give = freq[mx] - (cnt[mx] ? 1u : 0u);
if (give > over) give = over;
freq[mx] -= (uint16_t)give;
/* if still over (pathological), trim the next largest */
for (int s = 0; s < 256 && total - give > RANS_PROB_SCALE; s++) {
if (s == mx || !cnt[s]) continue;
uint32_t g = freq[s] - 1;
uint32_t need = total - give - RANS_PROB_SCALE;
if (g > need) g = need;
freq[s] -= (uint16_t)g; give += g;
}
}
}
size_t rans_encode_raw(const uint8_t *in, size_t n, const uint16_t *freq,
uint8_t *out) {
uint32_t cum[256];
uint32_t c = 0;
for (int s = 0; s < 256; s++) { cum[s] = c; c += freq[s]; }
uint32_t x = RANS_L;
/* Encode backwards; emit bytes into a temp tail, then reverse.
* Simpler: write backwards from the end of a scratch area. We
* write into out[0..] backwards via a local reverse pass: emit
* into out in reverse order by filling from the end. Caller
* guarantees out holds n + 64 bytes, so use the tail. */
uint8_t *p = out + 2 * n + 64; /* one past the end of scratch */
/* reserve 4 bytes at the very start of the emitted stream for the
* final state; emit stream bytes before them. Work backwards: */
uint8_t *w = p;
for (size_t i = n; i-- > 0;) {
uint8_t s = in[i];
uint32_t f = freq[s];
/* xmax = (L>>12)*f<<8: keeps post-update state in [L, 2^31) so the
* decoder renormalisation consumes exactly the bytes emitted here. */
uint32_t xmax = ((RANS_L >> RANS_PROB_BITS) * f) << 8;
while (x >= xmax) { *--w = (uint8_t)(x & 0xff); x >>= 8; }
x = (((x / f) << RANS_PROB_BITS) | ((x % f) + cum[s]));
}
/* w..p holds the emitted bytes in order; final state x follows. */
size_t nbytes = (size_t)(p - w);
memmove(out + 4, w, nbytes);
out[0] = (uint8_t)(x & 0xff);
out[1] = (uint8_t)((x >> 8) & 0xff);
out[2] = (uint8_t)((x >> 16) & 0xff);
out[3] = (uint8_t)((x >> 24) & 0xff);
return nbytes + 4;
}
int rans_decode_raw(const uint8_t *in, size_t in_len, const uint16_t *freq,
uint8_t *out, size_t out_len) {
if (in_len < 4) return -1;
uint16_t cum[256];
uint8_t dsym[RANS_PROB_SCALE];
uint32_t c = 0;
for (int s = 0; s < 256; s++) {
cum[s] = (uint16_t)c;
for (uint32_t k = 0; k < freq[s]; k++) dsym[c + k] = (uint8_t)s;
c += freq[s];
}
if (c != RANS_PROB_SCALE) return -1;
uint32_t x = (uint32_t)in[0] | ((uint32_t)in[1] << 8) |
((uint32_t)in[2] << 16) | ((uint32_t)in[3] << 24);
size_t p = 4;
for (size_t i = 0; i < out_len; i++) {
uint32_t slot = x & (RANS_PROB_SCALE - 1);
uint8_t s = dsym[slot];
out[i] = s;
uint32_t f = freq[s];
x = f * (x >> RANS_PROB_BITS) + (slot - cum[s]);
while (x < RANS_L) {
if (p >= in_len) return -1;
x = (x << 8) | in[p++];
}
}
return 0;
}
int rans_encode(const uint8_t *in, size_t n, uint8_t **out, size_t *out_len) {
uint16_t freq[256];
rans_freq(in, n, freq);
uint8_t *buf = malloc(520 + 2 * n + 64);
if (!buf) return -1;
memcpy(buf, "RANS", 4);
buf[4] = (uint8_t)(n & 0xff);
buf[5] = (uint8_t)((n >> 8) & 0xff);
buf[6] = (uint8_t)((n >> 16) & 0xff);
buf[7] = (uint8_t)((n >> 24) & 0xff);
for (int s = 0; s < 256; s++) {
buf[8 + 2 * s] = (uint8_t)(freq[s] & 0xff);
buf[8 + 2 * s + 1] = (uint8_t)((freq[s] >> 8) & 0xff);
}
size_t m = rans_encode_raw(in, n, freq, buf + 520);
*out = buf;
*out_len = 520 + m;
return 0;
}
int rans_decode(const uint8_t *in, size_t n, uint8_t **out, size_t *out_len) {
if (n < 520 || memcmp(in, "RANS", 4) != 0) return -1;
uint32_t orig = (uint32_t)in[4] | ((uint32_t)in[5] << 8) |
((uint32_t)in[6] << 16) | ((uint32_t)in[7] << 24);
uint16_t freq[256];
for (int s = 0; s < 256; s++)
freq[s] = (uint16_t)(in[8 + 2 * s] | (in[8 + 2 * s + 1] << 8));
uint8_t *buf = malloc(orig ? orig : 1);
if (!buf) return -1;
if (rans_decode_raw(in + 520, n - 520, freq, buf, orig) != 0) {
free(buf);
return -1;
}
*out = buf;
*out_len = orig;
return 0;
}
/* ---- general-alphabet layer (uint16_t symbols) ---- */
void rans_freq_n(const uint16_t *in, size_t n, int nsym, uint16_t *freq) {
uint32_t cnt[1024] = {0};
for (size_t i = 0; i < n; i++) cnt[in[i]]++;
uint32_t total = 0;
for (int s = 0; s < nsym; s++) {
uint32_t f = n ? (uint32_t)(((uint64_t)cnt[s] * RANS_PROB_SCALE) / n) : 0;
if (cnt[s] && f == 0) f = 1;
freq[s] = (uint16_t)f;
total += f;
}
int mx = 0;
for (int s = 1; s < nsym; s++) if (freq[s] > freq[mx]) mx = s;
if (total < RANS_PROB_SCALE) freq[mx] += (uint16_t)(RANS_PROB_SCALE - total);
else if (total > RANS_PROB_SCALE) {
uint32_t over = total - RANS_PROB_SCALE;
uint32_t give = freq[mx] - (cnt[mx] ? 1u : 0u);
if (give > over) give = over;
freq[mx] -= (uint16_t)give;
for (int s = 0; s < nsym && total - give > RANS_PROB_SCALE; s++) {
if (s == mx || !cnt[s]) continue;
uint32_t g = freq[s] - 1;
uint32_t need = total - give - RANS_PROB_SCALE;
if (g > need) g = need;
freq[s] -= (uint16_t)g; give += g;
}
}
}
size_t rans_encode_raw_n(const uint16_t *in, size_t n, const uint16_t *freq,
int nsym, uint8_t *out) {
uint32_t cum[1024];
uint32_t c = 0;
for (int s = 0; s < nsym; s++) { cum[s] = c; c += freq[s]; }
uint32_t x = RANS_L;
uint8_t *p = out + 2 * n + 64;
uint8_t *w = p;
for (size_t i = n; i-- > 0;) {
uint16_t s = in[i];
uint32_t f = freq[s];
uint32_t xmax = ((RANS_L >> RANS_PROB_BITS) * f) << 8;
while (x >= xmax) { *--w = (uint8_t)(x & 0xff); x >>= 8; }
x = (((x / f) << RANS_PROB_BITS) | ((x % f) + cum[s]));
}
size_t nbytes = (size_t)(p - w);
memmove(out + 4, w, nbytes);
out[0] = (uint8_t)(x & 0xff);
out[1] = (uint8_t)((x >> 8) & 0xff);
out[2] = (uint8_t)((x >> 16) & 0xff);
out[3] = (uint8_t)((x >> 24) & 0xff);
return nbytes + 4;
}
int rans_decode_raw_n(const uint8_t *in, size_t in_len, const uint16_t *freq,
int nsym, uint16_t *out, size_t out_len) {
if (in_len < 4) return -1;
uint16_t cum[1024];
uint16_t dsym[RANS_PROB_SCALE];
uint32_t c = 0;
for (int s = 0; s < nsym; s++) {
cum[s] = (uint16_t)c;
for (uint32_t k = 0; k < freq[s]; k++) dsym[c + k] = (uint16_t)s;
c += freq[s];
}
if (c != RANS_PROB_SCALE) return -1;
uint32_t x = (uint32_t)in[0] | ((uint32_t)in[1] << 8) |
((uint32_t)in[2] << 16) | ((uint32_t)in[3] << 24);
size_t p = 4;
for (size_t i = 0; i < out_len; i++) {
uint32_t slot = x & (RANS_PROB_SCALE - 1);
uint16_t s = dsym[slot];
out[i] = s;
uint32_t f = freq[s];
x = f * (x >> RANS_PROB_BITS) + (slot - cum[s]);
while (x < RANS_L) {
if (p >= in_len) return -1;
x = (x << 8) | in[p++];
}
}
return 0;
}
+44
View File
@@ -0,0 +1,44 @@
/* rans.h - rANS entropy coder shared by the lzply experiments.
* 12-bit probabilities (scale 4096), 23-bit renormalisation window.
* Two layers:
* - stream layer: rans_freq / rans_encode_raw / rans_decode_raw work
* on a raw byte stream with a caller-supplied freq table (for
* multi-stream coders, e.g. LZ token streams).
* - file layer: rans_encode / rans_decode wrap one stream with a
* header (magic "RANS", u32 orig_len, 256 x u16 freq table).
*/
#ifndef EXP_RANS_H
#define EXP_RANS_H
#include <stddef.h>
#include <stdint.h>
#define RANS_PROB_BITS 12
#define RANS_PROB_SCALE (1u << RANS_PROB_BITS)
#define RANS_L (1u << 23)
/* Count + normalise frequencies of `in` to sum RANS_PROB_SCALE.
* Symbols present get >= 1. */
void rans_freq(const uint8_t *in, size_t n, uint16_t *freq /*[256]*/);
/* Encode `in` with `freq`; `out` must hold 2*n + 64 bytes. Returns
* bytes written. */
size_t rans_encode_raw(const uint8_t *in, size_t n, const uint16_t *freq,
uint8_t *out);
/* Decode `in_len` bytes into `out` (out_len bytes). Returns 0 ok, -1 bad. */
int rans_decode_raw(const uint8_t *in, size_t in_len, const uint16_t *freq,
uint8_t *out, size_t out_len);
/* Whole-file wrap: mallocs *out. Returns 0 ok, -1 bad. */
int rans_encode(const uint8_t *in, size_t n, uint8_t **out, size_t *out_len);
int rans_decode(const uint8_t *in, size_t n, uint8_t **out, size_t *out_len);
#endif
/* General-alphabet layer (uint16_t symbols, nsym <= 1024): for token
* streams such as literal/length (286 symbols) and distance (21). */
void rans_freq_n(const uint16_t *in, size_t n, int nsym, uint16_t *freq);
size_t rans_encode_raw_n(const uint16_t *in, size_t n, const uint16_t *freq,
int nsym, uint8_t *out /* 2*n+64 */);
int rans_decode_raw_n(const uint8_t *in, size_t in_len, const uint16_t *freq,
int nsym, uint16_t *out, size_t out_len);
+46
View File
@@ -0,0 +1,46 @@
/* rans_tool.c - standalone rANS order-0 compressor: `rans_tool c in out`,
* `rans_tool d in out`. Used for the ANS-family measurements. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rans.h"
static uint8_t *slurp(const char *path, size_t *n) {
FILE *f = fopen(path, "rb");
if (!f) { perror(path); exit(2); }
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *b = malloc(sz ? (size_t)sz : 1);
if (!b) exit(2);
if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) exit(2);
fclose(f);
*n = (size_t)sz;
return b;
}
static void spit(const char *path, const uint8_t *b, size_t n) {
FILE *f = fopen(path, "wb");
if (!f) { perror(path); exit(2); }
if (n && fwrite(b, 1, n, f) != n) exit(2);
fclose(f);
}
int main(int argc, char **argv) {
if (argc != 4 || (argv[1][0] != 'c' && argv[1][0] != 'd')) {
fprintf(stderr, "usage: %s c|d in out\n", argv[0]);
return 1;
}
size_t n;
uint8_t *in = slurp(argv[2], &n);
uint8_t *out = NULL;
size_t on = 0;
int rc = argv[1][0] == 'c' ? rans_encode(in, n, &out, &on)
: rans_decode(in, n, &out, &on);
if (rc != 0) {
fprintf(stderr, "%s: %s failed (bad input?)\n", argv[0], argv[1]);
return 3;
}
spit(argv[3], out, on);
return 0;
}
View File
Whitespace-only changes.
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
bzip2,text,8388608,776306,9.25,2.969,1.346,2.8,6.2,OK
bzip2,code,8388608,1053123,12.55,2.754,0.937,3.0,8.9,OK
bzip2,bin,8388608,3373815,40.22,1.866,1.575,4.5,5.3,OK
bzip2,mixed,8388608,1974225,23.53,2.081,0.875,4.0,9.6,OK
bzip2,rand,4194304,4213434,100.46,1.336,0.958,3.1,4.4,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 bzip2 text 8388608 776306 9.25 2.969 1.346 2.8 6.2 OK
3 bzip2 code 8388608 1053123 12.55 2.754 0.937 3.0 8.9 OK
4 bzip2 bin 8388608 3373815 40.22 1.866 1.575 4.5 5.3 OK
5 bzip2 mixed 8388608 1974225 23.53 2.081 0.875 4.0 9.6 OK
6 bzip2 rand 4194304 4213434 100.46 1.336 0.958 3.1 4.4 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
dict-lzip6,text,8388608,299963,3.58,2.541,0.166,3.3,50.4,OK
dict-lzip6,code,8388608,770519,9.19,4.144,0.218,2.0,38.5,OK
dict-lzip6,bin,8388608,2793130,33.30,9.522,0.467,0.9,18.0,OK
dict-lzip6,mixed,8388608,1480819,17.65,5.358,0.317,1.6,26.5,OK
dict-lzip6,rand,4194304,4266698,101.73,2.794,0.475,1.5,8.8,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 dict-lzip6 text 8388608 299963 3.58 2.541 0.166 3.3 50.4 OK
3 dict-lzip6 code 8388608 770519 9.19 4.144 0.218 2.0 38.5 OK
4 dict-lzip6 bin 8388608 2793130 33.30 9.522 0.467 0.9 18.0 OK
5 dict-lzip6 mixed 8388608 1480819 17.65 5.358 0.317 1.6 26.5 OK
6 dict-lzip6 rand 4194304 4266698 101.73 2.794 0.475 1.5 8.8 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
gzip9,text,8388608,1969901,23.48,0.830,0.117,10.1,71.6,OK
gzip9,code,8388608,1376091,16.40,0.980,0.066,8.6,127.7,OK
gzip9,bin,8388608,3702420,44.14,2.700,0.115,3.1,72.6,OK
gzip9,mixed,8388608,2521765,30.06,1.180,0.117,7.1,71.5,OK
gzip9,rand,4194304,4194967,100.02,0.428,0.059,9.8,71.3,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 gzip9 text 8388608 1969901 23.48 0.830 0.117 10.1 71.6 OK
3 gzip9 code 8388608 1376091 16.40 0.980 0.066 8.6 127.7 OK
4 gzip9 bin 8388608 3702420 44.14 2.700 0.115 3.1 72.6 OK
5 gzip9 mixed 8388608 2521765 30.06 1.180 0.117 7.1 71.5 OK
6 gzip9 rand 4194304 4194967 100.02 0.428 0.059 9.8 71.3 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
lzip1,text,8388608,521474,6.22,0.923,0.117,9.1,71.8,OK
lzip1,code,8388608,1268850,15.13,1.127,0.216,7.4,38.8,OK
lzip1,bin,8388608,3173235,37.83,3.144,0.468,2.7,17.9,OK
lzip1,mixed,8388608,1809972,21.58,1.789,0.271,4.7,31.0,OK
lzip1,rand,4194304,4251394,101.36,3.104,0.474,1.4,8.9,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 lzip1 text 8388608 521474 6.22 0.923 0.117 9.1 71.8 OK
3 lzip1 code 8388608 1268850 15.13 1.127 0.216 7.4 38.8 OK
4 lzip1 bin 8388608 3173235 37.83 3.144 0.468 2.7 17.9 OK
5 lzip1 mixed 8388608 1809972 21.58 1.789 0.271 4.7 31.0 OK
6 lzip1 rand 4194304 4251394 101.36 3.104 0.474 1.4 8.9 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
lzip6,text,8388608,298558,3.56,3.655,0.117,2.3,71.4,OK
lzip6,code,8388608,787213,9.38,5.138,0.168,1.6,50.0,OK
lzip6,bin,8388608,2735269,32.61,9.676,0.416,0.9,20.2,OK
lzip6,mixed,8388608,1460156,17.41,5.519,0.267,1.5,31.4,OK
lzip6,rand,4194304,4251435,101.36,3.173,0.531,1.3,7.9,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 lzip6 text 8388608 298558 3.56 3.655 0.117 2.3 71.4 OK
3 lzip6 code 8388608 787213 9.38 5.138 0.168 1.6 50.0 OK
4 lzip6 bin 8388608 2735269 32.61 9.676 0.416 0.9 20.2 OK
5 lzip6 mixed 8388608 1460156 17.41 5.519 0.267 1.5 31.4 OK
6 lzip6 rand 4194304 4251435 101.36 3.173 0.531 1.3 7.9 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
lzip9,text,8388608,288746,3.44,6.658,0.119,1.3,70.3,OK
lzip9,code,8388608,758969,9.05,9.062,0.168,0.9,50.0,OK
lzip9,bin,8388608,2729180,32.53,10.747,0.479,0.8,17.5,OK
lzip9,mixed,8388608,1440405,17.17,8.603,0.272,1.0,30.8,OK
lzip9,rand,4194304,4251435,101.36,3.071,0.467,1.4,9.0,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 lzip9 text 8388608 288746 3.44 6.658 0.119 1.3 70.3 OK
3 lzip9 code 8388608 758969 9.05 9.062 0.168 0.9 50.0 OK
4 lzip9 bin 8388608 2729180 32.53 10.747 0.479 0.8 17.5 OK
5 lzip9 mixed 8388608 1440405 17.17 8.603 0.272 1.0 30.8 OK
6 lzip9 rand 4194304 4251435 101.36 3.071 0.467 1.4 9.0 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
lzrans-lazy,text,8388608,525357,6.26,1.888,0.119,4.4,70.6,OK
lzrans-lazy,code,8388608,1134159,13.52,3.264,0.116,2.6,72.5,OK
lzrans-lazy,bin,8388608,3510054,41.84,9.644,0.320,0.9,26.2,OK
lzrans-lazy,mixed,8388608,1942374,23.15,3.963,0.165,2.1,50.7,OK
lzrans-lazy,rand,4194304,4214292,100.48,9.295,0.115,0.5,36.3,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 lzrans-lazy text 8388608 525357 6.26 1.888 0.119 4.4 70.6 OK
3 lzrans-lazy code 8388608 1134159 13.52 3.264 0.116 2.6 72.5 OK
4 lzrans-lazy bin 8388608 3510054 41.84 9.644 0.320 0.9 26.2 OK
5 lzrans-lazy mixed 8388608 1942374 23.15 3.963 0.165 2.1 50.7 OK
6 lzrans-lazy rand 4194304 4214292 100.48 9.295 0.115 0.5 36.3 OK
+3
View File
@@ -0,0 +1,3 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
lzrans-opt,text,8388608,519352,6.19,18.714,0.132,0.4,63.5,OK
lzrans-opt,code,8388608,1094779,13.05,8.628,0.115,1.0,72.9,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 lzrans-opt text 8388608 519352 6.19 18.714 0.132 0.4 63.5 OK
3 lzrans-opt code 8388608 1094779 13.05 8.628 0.115 1.0 72.9 OK
+4
View File
@@ -0,0 +1,4 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
ppm,text,8388608,2754814,32.84,1.327,2.176,6.3,3.9,OK
ppm,code,8388608,2585570,30.82,1.324,2.040,6.3,4.1,OK
ppm,mixed,8388608,3286801,39.18,1.732,2.635,4.8,3.2,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 ppm text 8388608 2754814 32.84 1.327 2.176 6.3 3.9 OK
3 ppm code 8388608 2585570 30.82 1.324 2.040 6.3 4.1 OK
4 ppm mixed 8388608 3286801 39.18 1.732 2.635 4.8 3.2 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
rans0,text,8388608,5401895,64.40,0.221,0.170,38.0,49.3,OK
rans0,code,8388608,5672388,67.62,0.277,0.171,30.3,49.1,OK
rans0,bin,8388608,6470778,77.14,0.217,0.179,38.6,46.9,OK
rans0,mixed,8388608,6552704,78.11,0.218,0.174,38.4,48.1,OK
rans0,rand,4194304,4213441,100.46,0.115,0.068,36.4,61.6,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 rans0 text 8388608 5401895 64.40 0.221 0.170 38.0 49.3 OK
3 rans0 code 8388608 5672388 67.62 0.277 0.171 30.3 49.1 OK
4 rans0 bin 8388608 6470778 77.14 0.217 0.179 38.6 46.9 OK
5 rans0 mixed 8388608 6552704 78.11 0.218 0.174 38.4 48.1 OK
6 rans0 rand 4194304 4213441 100.46 0.115 0.068 36.4 61.6 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
xz6,text,8388608,294056,3.51,4.257,0.076,2.0,110.1,OK
xz6,code,8388608,771984,9.20,7.147,0.200,1.2,42.0,OK
xz6,bin,8388608,2733616,32.59,12.330,0.367,0.7,22.8,OK
xz6,mixed,8388608,1450908,17.30,7.553,0.271,1.1,31.0,OK
xz6,rand,4194304,4194584,100.01,3.323,0.085,1.3,49.1,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 xz6 text 8388608 294056 3.51 4.257 0.076 2.0 110.1 OK
3 xz6 code 8388608 771984 9.20 7.147 0.200 1.2 42.0 OK
4 xz6 bin 8388608 2733616 32.59 12.330 0.367 0.7 22.8 OK
5 xz6 mixed 8388608 1450908 17.30 7.553 0.271 1.1 31.0 OK
6 xz6 rand 4194304 4194584 100.01 3.323 0.085 1.3 49.1 OK
+6
View File
@@ -0,0 +1,6 @@
tool,corpus,orig,comp,ratio_pct,enc_s,dec_s,enc_MBs,dec_MBs,status
zstd19,text,8388608,304391,3.63,3.549,0.055,2.4,151.8,OK
zstd19,code,8388608,795239,9.48,11.348,0.071,0.7,117.7,OK
zstd19,bin,8388608,2919792,34.81,14.260,0.169,0.6,49.5,OK
zstd19,mixed,8388608,1535960,18.31,8.021,0.066,1.0,126.9,OK
zstd19,rand,4194304,4194413,100.00,3.020,0.037,1.4,112.0,OK
1 tool corpus orig comp ratio_pct enc_s dec_s enc_MBs dec_MBs status
2 zstd19 text 8388608 304391 3.63 3.549 0.055 2.4 151.8 OK
3 zstd19 code 8388608 795239 9.48 11.348 0.071 0.7 117.7 OK
4 zstd19 bin 8388608 2919792 34.81 14.260 0.169 0.6 49.5 OK
5 zstd19 mixed 8388608 1535960 18.31 8.021 0.066 1.0 126.9 OK
6 zstd19 rand 4194304 4194413 100.00 3.020 0.037 1.4 112.0 OK