10 Commits
Author SHA1 Message Date
Muse a34cd7dcaf exp: gitignore the diag binaries 2026-12-02 10:50:26 +00:00
Muse 3dce1f4e59 exp: final README — pass-2 verdict (0/875), speeds, cross-corpus, totals
- Pass 2: 35 rows x 25 trials = 875, rescued 0. Repo's own test
  agrees (t+1 0/100, t+2 0/100).
- Isolated speed table (sequential): interleave costs nothing
  measurable; LDPC dec 0.91 MB/s; polar dec 0.58 MB/s.
- Cross-corpus (bin.lz, rand.lz) reproduces text.lz exactly.
- Test totals: 51,875 matrix trials + diagnostics.
- hot_chicago verification BLOCKED (tunnel down 4 attempts);
  snapshot zips staged on old VPS for a later retry.
2026-12-02 10:48:16 +00:00
Muse f87c7e3007 exp: fix Makefile mangled by a sed (ldpc_diag/pass2_diag recipes)
The previous commit's sed ate the $(CC)/$(CFLAGS) variables and
moved the ldpc_diag recipe under pass2_diag. Rewrote the Makefile
cleanly; all four binaries build.
2026-12-02 10:42:41 +00:00
Muse f07a2ba488 exp: results write-up + pass-2 diagnostic (pass 2 rescues nothing)
README now carries the measured tables for all four families.
pass2_diag.c: targeted sweep of BCH pass-2 recovery. Finding: the
repo's own bch_test reports t+1 recovered 0/100, t+2 0/100; this
harness's pass-2 matrix shows rescued=0 in every setting; the
targeted sweep (m=13 t=16, k=t+1..t+8) rescues 0/50 at every k.
Pass 2's demonstrated value is the loud-failure guarantee (0
silent miscorrections), not recovery -- its residual-BM mechanism
addresses pass-1 miscorrection, which essentially never happens
for random errors. Relevant to the sibling deep-repair job.
2026-12-02 10:42:20 +00:00
Muse 4b6d4cfa45 exp: interleave groups merge the short tail (no weak tail group)
A burst landing in the final partial group (4 blocks of a 68-block
blob) hit each of its blocks with L/4 bytes instead of L/64 -- that
is why 64 B bursts measured 0.94 instead of 1.00. Groups are now
formed so the last partial group is absorbed into the previous one
(every group >= depth). Re-measured, 50 trials: d16/d32/d64-t32 at
64 B bursts 1.00 (was 0.94); d64-t32 at 256 B and 64x4 bursts 1.00
(was 0.95 / 0.83).
2026-12-02 10:35:24 +00:00
Muse 5d3ee79fd3 exp: unequal error protection over small-block BCH (family 4)
Prefix (first C bytes: lzip header + stream start) at t_hi, rest at
t_lo. Fixed a real bug en route: the stored-length helper hardcoded
the t=32 block size (971 B); block size depends on t (1010 B at
t=8), which broke decode for every tier but t=32. uep_diag reports
prefix survival separately: at BER 1e-3, uep-c1024-h32-l8 has
whole=0.00 but prefix=1.00 -- the graceful-degradation claim is
real, but it only helps if lzply gains a salvage mode (today the
entry SHA gates all-or-nothing extraction). Pass 2 not wired: the
oracle is the whole-blob SHA.
2026-12-02 10:31:39 +00:00
Muse 22436a7990 exp: polar code, SC decoding (family 3)
N=4096, BEC-Bhattacharyya construction, min-sum SC. Fixed a real
bug en route: the SC partial sums overwrote the leaf decisions, so
clean round-trips failed (bits[] and psum[] are now separate).
Key finding: the design point matters enormously. Designing for
p=1e-2 gives 0.00 recovery at BER 1e-4; designing for p=1e-3/1e-4
gives 0.90/0.95. Codec names select the design point
(polar-r90-d3 = design 1e-3, default d2 = 1e-2). Even the best
variant collapses at BER 1e-3 -- SC at N=4096/R=0.9 is weak there;
full matrix will decide whether polar is a reject.
2026-12-02 10:29:52 +00:00
Muse 5d4f17a57e exp: regular LDPC codec, PEG construction + min-sum BP (family 2)
n=8192, column weight 3; r90 (m=768, rate 0.906) and r95 (m=384,
rate 0.953). PEG construction (the greedy 4-cycle-rejection builder
deadlocked in the endgame -- replaced, see comment). Systematic
encoding via Gauss-Jordan; normalized min-sum (0.8), 50 iters.
Single-block diagnostics (ldpc_diag): r90 clean to 37 errors/block,
first failures ~38-40; r95 starts failing at 6 errors/block -- the
high-rate regular (3,64) code is genuinely weak, an honest negative
for naive regular LDPC at rate 0.95. Clean round-trips bit-identical.
2026-12-02 10:20:02 +00:00
Muse 50b3b3a80f exp: byte interleaver over small-block BCH (family 1)
Zero extra parity: groups of D blocks are read column-wise, so a
burst of L bytes hits any one block with at most ceil(L/D) bytes
instead of killing a block outright. Random-error performance is
unchanged by construction (same BCH underneath); burst tolerance
improves ~Dx. Smoke test on text.lz: d64-t32 survives 256 B bursts
at 0.95 (plain bch1k-t32: 0.00), clean round-trips bit-identical,
overhead identical to bch1k-t32 (5.40%).
2026-12-02 10:14:32 +00:00
Muse 1f959bb211 exp: corruption-matrix harness + BCH baseline codecs
bench.c measures every ECC family the same way: encode a blob window,
corrupt the stored bytes (random bit flips at a BER, or randomized
byte bursts), decode, check exact recovery (memcmp + SHA-256).
bch_wrap.c wraps the shipped BCH two ways: as-shipped (auto-pick poly
for the whole blob) and forced 1 KiB blocks. Pass 2 is wired to
recover_verified; --nopass2 skips it for matrix speed.
Stubs for interleave/ldpc/polar/uep return NULL until implemented.
Corpora + results are generated, gitignored.
2026-12-02 10:12:50 +00:00
18 changed files with 2487 additions and 0 deletions

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
bench
ldpc_diag
pass2_diag
uep_diag
*.o
corpora/
results/
+29
View File
@@ -0,0 +1,29 @@
# exp/Makefile - build the ECC experiments harness (bench).
# Links the real lzply BCH + SHA-256 from the repo root.
CC ?= gcc
CFLAGS ?= -O2 -Wall -Wextra -march=native
CPPFLAGS ?= -I. -I..
SRCS = bench.c bch_wrap.c interleave.c ldpc.c polar.c uep.c \
../bch.c ../sha256.c
OBJS = $(SRCS:.c=.o)
bench: $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) -lm
ldpc_diag: ldpc_diag.c ldpc.c
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ ldpc_diag.c ldpc.c -lm
pass2_diag: pass2_diag.c ../bch.c ../sha256.c
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ pass2_diag.c ../bch.c ../sha256.c -lm
uep_diag: uep_diag.c uep.c bch_wrap.c ../bch.c ../sha256.c
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ uep_diag.c uep.c bch_wrap.c ../bch.c ../sha256.c -lm
%.o: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
clean:
rm -f bench ldpc_diag pass2_diag uep_diag $(OBJS)
.PHONY: clean
+201
View File
@@ -0,0 +1,201 @@
# lzply ECC experiments (branch `experiments/ecc`)
Open-ended error-correction research on top of lzply's BCH baseline.
Each family implements `ecc_iface.h` (whole-blob encode/decode) and is
measured by `bench.c` on the same corpora, same corruption matrix, same
success criterion (exact recovery, memcmp + SHA-256).
## How to run
```
./bench --codec bch-t32 --blob corpora/text.lz --size 65536 \
--trials 200 --ber 1e-3 --nopass2 --out results/x.csv
./bench --codec ldpc-r90 --blob corpora/text.lz --burst 256,1 --trials 200
./bench --codec bch-t32 --blob corpora/text.lz --speed
./ldpc_diag r90 40 50 # single-block error-count sweep
./uep_diag uep-c1024-h32-l8 corpora/text.lz 65536 50 --ber 1e-3
```
Modes: `--ber X` (independent bit flips), `--burst LEN,COUNT`
(COUNT randomized-byte bursts of LEN bytes), `--clean`, `--speed`.
`--nopass2` skips the (slow) second-chance recovery hook so the matrix
stays fast; `run_pass2.sh` measures rescue rates separately.
`run_matrix.sh <corpus> <size> <trials> <out.csv> <codec...>` runs the
full matrix (5 BERs + 5 burst settings + clean), parallel over cores.
Corpora (`mkcorpora.sh`, generated, not committed): `text.lz` /
`bin.lz` / `rand.lz` — lzip -9 of 4 MiB text, binaries, seeded random
bytes (the compressed blob is what lzply protects); `*.raw` — the
stored-entry case. Default window is 64 KiB.
## Families
| codec | what it is |
|---|---|
| `bch-t{2,4,8,16,32}` | Baseline: BCH exactly as lzply ships it (auto-pick poly for the whole blob; pass 2 via `recover_verified`). |
| `bch1k-t{8,16,32}` | Same BCH, forced m=13 blocks (971 data bytes/block) — the "spend real redundancy" point. |
| `intlv-d{D}-bch1k-t{T}` | Byte interleaver (depth D) over bch1k-t{T}. Zero extra parity; bursts spread across blocks. Groups merge the short tail (see below). |
| `ldpc-r{90,95}` | Regular LDPC, PEG construction, min-sum belief propagation. |
| `polar-r{90,95}[-d{2,3,4}]` | Polar code, SC decoding. `-dN` is the design crossover 1e-N (default d2 = 1e-2). The design point matters enormously (see below). |
| `uep-c{C}-h{H}-l{L}` | Unequal error protection: first C bytes at BCH t=H, rest at t=L (small blocks). |
## Results (text.lz, 64 KiB windows, 200 trials/setting, hot_newyork)
Recovery rate = fraction of trials exactly recovered. Overhead is
measured (stored/data - 1). Pass-2 rescue rates are in
results/pass2-text.csv (separate, 25 trials/setting).
### Baseline: what lzply ships is nearly unprotected
| codec | ovh | BER 1e-5 | 1e-4 | 1e-3 | 3e-3 | 1e-2 | burst 256 B |
|---|---|---|---|---|---|---|---|
| bch-t2 | 0.01% | 0.11 | 0 | 0 | 0 | 0 | 0 |
| bch-t8 | 0.03% | 0.88 | 0 | 0 | 0 | 0 | 0 |
| bch-t16 | 0.06% | 1.00 | 0 | 0 | 0 | 0 | 0 |
| bch-t32 | 0.12% | 1.00 | 0.01 | 0 | 0 | 0 | 0 |
One 64 KiB blob is a single BCH block (m=20, 80 parity bytes), so
t=32 corrects 32 bit errors per 64 KiB. At BER 1e-4 the blob already
carries ~52 errors: the cliff is between 1e-5 and 1e-4.
Pass 2 (hash-gated candidates) rescues nothing in practice:
the repo's own bch_test reports t+1 recovered 0/100 and t+2
recovered 0/100; this harness's pass2 matrix (35 rows, 875 trials,
all BCH codecs) shows rescued=0 in every setting, and a targeted
sweep (pass2_diag, m=13 t=16, k=t+1..t+8) rescues 0/50 at every k. Its mechanism (residual-BM)
addresses pass-1 miscorrection, which essentially never happens
for random errors (pass 1 fails rather than miscorrecting), and
the flip-and-redecode fallback does not reach the true codeword.
Its demonstrated value is the loud-failure guarantee: 0 silent
miscorrections in 7813 checks. The matrix numbers above are
therefore the true lzply numbers with or without pass 2.
### Spending redundancy: small-block BCH
| codec | ovh | BER 1e-4 | 1e-3 | 3e-3 |
|---|---|---|---|---|
| bch1k-t8 | 1.29% | 1.00 | 0 | 0 |
| bch1k-t16 | 2.62% | 1.00 | 0.73 | 0 |
| bch1k-t32 | 5.40% | 1.00 | 1.00 | 0.01 |
Same code, 1 KiB blocks: t=32 buys survival to BER 1e-3 at 5.4%
overhead. Bursts still kill it (256 B burst: 0.00 at every t).
### Interleaving: zero-overhead burst protection (WINNER)
Byte interleave over bch1k-t32, 5.40% overhead (unchanged):
| codec | burst 16 B | 64 B | 256 B | 1024 B | 4x64 B |
|---|---|---|---|---|---|
| bch1k-t32 (no intlv) | 0 | 0 | 0 | 0 | 0 |
| intlv-d16-t32 | 1.00 | 1.00 | 0 | 0 | 0.10 |
| intlv-d32-t32 | 1.00 | 1.00 | 0 | 0 | 0.80 |
| intlv-d64-t32 | 1.00 | 1.00 | 1.00 | 0 | 1.00 |
Random-error performance is unchanged by construction (same BCH
underneath). Even at 1.29% overhead, intlv-d64-bch1k-t8 survives
64 B bursts at 1.00. Gotcha fixed en route: a short tail group
(4 blocks of 68) was the weak point and measured 0.94 on 64 B
bursts; groups now merge the tail.
### LDPC: the random-error winner (WINNER at ~11% overhead)
| codec | ovh | BER 1e-3 | 3e-3 | 1e-2 | burst 16 B |
|---|---|---|---|---|---|
| ldpc-r90 | 10.94% | 1.00 | 0.97 | 0 | 0.24 |
| ldpc-r95 | 6.25% | 0.32 | 0 | 0 | 0 |
At BER 3e-3, ldpc-r90 recovers 0.97 where bch1k-t32 recovers 0.01
— the iterative decoder's cliff sits a decade higher in BER. But
bursts kill it (a 16 B burst is ~64 errors in one 1 KiB block),
and r95 is weak: the regular (3,64) code starts failing at 6
errors/block (ldpc_diag). Irregular LDPC would be the follow-up;
LDPC+interleave would fix bursts.
### Polar SC: dominated, rejected (honest negative)
Designing for p=1e-2 gives 0.00 at BER 1e-4; designing for the
operating point (d3/d4) gives 0.90/0.95 at 1e-4 — but every variant
is 0.00 at BER 1e-3. SC at N=4096/R=0.9 is far weaker than
bch1k-t32 (1.00 at 1e-3, half the overhead) and ldpc-r90 (1.00 at
1e-3, same overhead). Two real bugs were fixed en route (SC partial
sums overwrote the leaf decisions; see polar.c). SCL list decoding
is the follow-up that could rescue polar; SC as measured is a reject.
### UEP: graceful degradation, conditional
Exact recovery is dominated by the weak tier (uep-c1024-h32-l8 at
1e-3: 0.00, same as uniform bch1k-t8). The prefix tells another
story (uep_diag, 50 trials):
| codec | BER 1e-3 whole | BER 1e-3 prefix | burst 256 B prefix |
|---|---|---|---|
| uep-c1024-h32-l8 | 0.00 | 1.00 | 1.00 |
| uep-c4096-h32-l8 | 0.00 | 1.00 | 0.94 |
The first 1-4 KiB (lzip header + stream start) survives when the
file is lost. Only useful if lzply gains a salvage mode; today the
entry SHA gates all-or-nothing extraction.
## Speed and memory (isolated, sequential, 64 KiB blob, clean)
`--speed` mode, 5 reps each. RSS is the bench process peak (harness
+ buffers ~6.5 MB); the codec delta is what matters.
| codec | enc MB/s | dec MB/s | RSS KB | ovh |
|---|---|---|---|---|
| bch-t32 | 6.46 | 3.44 | 6564 | 0.12% |
| bch1k-t8 | 9.98 | 5.87 | 6560 | 1.29% |
| bch1k-t16 | 5.39 | 3.14 | 6492 | 2.62% |
| bch1k-t32 | 2.97 | 1.55 | 6652 | 5.40% |
| intlv-d64-t32 | 3.39 | 1.73 | 6720 | 5.40% |
| intlv-d64-t8 | 11.11 | 5.80 | 6572 | 1.29% |
| ldpc-r90 | 3.34 | 0.91 | 7776 | 10.94% |
| ldpc-r95 | 5.18 | 1.16 | 7652 | 6.25% |
| polar-r90-d4 | 4.82 | 0.58 | 7252 | 11.72% |
| uep-c1024-h32-l8 | 9.96 | 4.80 | 6612 | 1.43% |
Interleaving costs nothing measurable (same speed and memory as
the BCH underneath; it is a permutation). LDPC pays a one-time
~1.8 s init (PEG + Gauss-Jordan) and decodes ~4x slower than
bch1k-t32; polar decodes slowest of all (0.58 MB/s). Failed BCH
decodes are far slower than clean ones (pass 2 dominates; matrix
rows for failed whole-blob blocks read 0.02-0.10 MB/s).
## Generalization
Cross-corpus check (bin.lz, rand.lz, 100 trials/setting, headline
codecs, BER 1e-4/1e-3 + 256 B burst + clean) reproduces text.lz
exactly for every codec and setting: bch1k-t32 1.00/1.00/0.00,
intlv-d64-t32 1.00/1.00/1.00, ldpc-r90 1.00/1.00/0.00,
polar-r90-d4 0.94/0.00/0.00, uep-c1024 1.00/0.00/0.00. Clean
round-trips are bit-identical everywhere (all clean rows 1.00).
## Test totals
- BCH matrix (text.lz): 143 rows x 200 trials = 28,600 trials
- LDPC matrix: 22 rows x 200 = 4,400
- Polar matrix: 33 rows x 200 = 6,600
- UEP matrix: 33 rows x 200 = 6,600
- Pass-2 matrix: 35 rows x 25 = 875 (rescued 0)
- Cross-corpus: 48 rows x 100 = 4,800
- Diagnostics: ldpc_diag single-block sweeps, uep_diag prefix
survival (50 trials/setting), pass2_diag targeted sweep
(t+1..t+8, 50 trials each)
- Total matrix trials: 51,875 (+ diagnostics)
- hot_chicago verification: BLOCKED — the old-VPS tunnel's SSH
leg to 104.225.223.162 failed 4 times over ~1.5 h (banner-exchange
timeout on the last attempt). Snapshot zips are staged on the
old VPS (/tmp/lzply-*.zip) for a retry when the tunnel recovers.
## Rules of this branch
- Never touch `main` or the other coordinator's branches/checkouts.
- No Reed-Solomon (the other job's territory).
- No legacy support; the format may change freely.
- Do not break the existing `-r` levels.
- Honest negatives count: a family that loses on every axis is killed
early and its numbers recorded.
- After every push: zip snapshot to /root/git-backups/lzply/ on both
servers (newest 7 kept); use /root/scripts/git-push-snapshot.sh
when present, else temp-clone + zip + copy manually.
+158
View File
@@ -0,0 +1,158 @@
/* bch_wrap.c - BCH baseline codecs for the experiments harness.
*
* Two operating points of the same code, so the harness can show the
* overhead/correction tradeoff honestly:
*
* bch-t<N> - exactly what lzply ships: bch_poly_for_data(blob, t)
* picks the smallest parent covering the WHOLE blob, so a
* 64 KiB blob is a single huge block (m=20, t=32 -> 80
* parity bytes, 0.12% overhead, 32 correctable bits per
* 64 KiB). Pass 2 (hash-gated candidates) is wired to
* recover_verified, matching entry_decode().
* bch1k-t<N> - same code, forced small blocks: pinned m=13 poly, so a
* block holds 971 data bytes (t=32 -> 5.4% overhead).
* This is the "what if we spent real redundancy" point.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ecc_iface.h"
#include "bch.h"
#include "bch_polys.h"
#include "sha256.h"
typedef struct {
int t;
const bch_poly_t *pinned; /* NULL = auto-pick (as-shipped) */
} bch_priv_t;
static const bch_poly_t *poly_by_mt(int m, int t)
{
for (int i = 0; i < BCH_NPOLYS; i++)
if (bch_polys[i].m == m && bch_polys[i].t == t)
return &bch_polys[i];
return NULL;
}
static size_t bch_encode_fn(ecc_codec_t *c, const uint8_t *data, size_t len,
uint8_t **out)
{
bch_priv_t *p = (bch_priv_t *)c->priv;
const bch_poly_t *poly = p->pinned;
if (!poly)
poly = bch_poly_for_data(len, p->t);
if (!poly)
return 0;
size_t slen = bch_encoded_len(len, poly);
uint8_t *buf = (uint8_t *)malloc(slen ? slen : 1);
if (!buf)
return 0;
if (bch_encode(data, len, poly, buf) != slen) {
free(buf);
return 0;
}
c->overhead = len ? (double)slen / (double)len - 1.0 : 0.0;
*out = buf;
return slen;
}
static int bch_decode_fn(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out, long *corrected)
{
bch_priv_t *p = (bch_priv_t *)c->priv;
const bch_poly_t *poly = p->pinned;
if (!poly)
poly = bch_poly_for_data(orig_len, p->t);
if (!poly)
return -1;
if (slen != bch_encoded_len(orig_len, poly))
return -1;
int corr = 0;
int rc = bch_decode(stored, slen, orig_len, poly, out, &corr);
if (corrected)
*corrected = rc == 0 ? corr : -1;
return rc;
}
static int bch_recover_fn(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, const uint8_t want_sha[32],
uint8_t *out)
{
bch_priv_t *p = (bch_priv_t *)c->priv;
const bch_poly_t *poly = p->pinned;
if (!poly)
poly = bch_poly_for_data(orig_len, p->t);
if (!poly)
return -1;
if (slen != bch_encoded_len(orig_len, poly))
return -1;
uint8_t *cands = (uint8_t *)malloc((size_t)BCH_PASS2_MAX_CANDS * orig_len);
if (!cands)
return -1;
int nc = bch_decode_candidates(stored, slen, orig_len, poly, cands,
BCH_PASS2_MAX_CANDS);
int found = -1;
for (int i = 0; i < nc && found < 0; i++) {
uint8_t h[32];
lzply_sha256(cands + (size_t)i * orig_len, orig_len, h);
if (memcmp(h, want_sha, 32) == 0)
found = i;
}
if (found >= 0)
memcpy(out, cands + (size_t)found * orig_len, orig_len);
free(cands);
return found >= 0 ? 0 : -1;
}
static void bch_destroy(ecc_codec_t *c)
{
free(c->priv);
free((void *)c->name);
free(c);
}
static ecc_codec_t *bch_new(int t, const bch_poly_t *pinned, const char *name)
{
ecc_codec_t *c = (ecc_codec_t *)calloc(1, sizeof(*c));
bch_priv_t *p = (bch_priv_t *)calloc(1, sizeof(*p));
if (!c || !p) {
free(c);
free(p);
return NULL;
}
p->t = t;
p->pinned = pinned;
c->name = name;
c->desc = pinned ? "BCH, forced 1 KiB blocks" : "BCH as shipped in lzply";
c->block_data = pinned ? bch_max_data(pinned) : 0;
c->encode = bch_encode_fn;
c->decode = bch_decode_fn;
c->recover_verified = bch_recover_fn;
c->destroy = bch_destroy;
c->priv = p;
return c;
}
/* As-shipped: auto-pick poly for the whole blob. */
ecc_codec_t *bch_ship_new(int t)
{
char *name = (char *)malloc(32);
if (!name)
return NULL;
snprintf(name, 32, "bch-t%d", t);
return bch_new(t, NULL, name);
}
/* Forced small blocks: pinned m=13 poly (971 data bytes/block at any t). */
ecc_codec_t *bch_1k_new(int t)
{
const bch_poly_t *poly = poly_by_mt(13, t);
if (!poly)
return NULL;
char *name = (char *)malloc(32);
if (!name)
return NULL;
snprintf(name, 32, "bch1k-t%d", t);
return bch_new(t, poly, name);
}
+10
View File
@@ -0,0 +1,10 @@
/* bch_wrap.h - constructors for the BCH baseline codecs (bch_wrap.c). */
#ifndef EXP_BCH_WRAP_H
#define EXP_BCH_WRAP_H
#include "ecc_iface.h"
ecc_codec_t *bch_ship_new(int t);
ecc_codec_t *bch_1k_new(int t);
#endif /* EXP_BCH_WRAP_H */
+349
View File
@@ -0,0 +1,349 @@
/* bench.c - corruption-matrix harness for the lzply ECC experiments.
*
* One invocation = one codec x one corruption setting x N trials.
* Each trial: take a window of the corpus blob, encode it, corrupt the
* stored bytes (random bit flips at a BER, or randomized byte bursts),
* decode, and check exact recovery (memcmp + SHA-256). On decode
* failure, the codec's recover_verified hook (if any) gets one shot
* with the expected SHA -- that is how BCH pass 2 is credited.
*
* Usage:
* bench --codec NAME --blob FILE [--size N] [--trials N] [--seed S]
* (--ber X | --burst LEN,COUNT | --clean | --speed)
* [--out results.csv]
*
* CSV (appended to --out, or stdout if omitted):
* codec,corpus,blob_size,trials,mode,param,success,rescued,rate,
* enc_MBps,dec_MBps,peak_rss_kb,overhead_pct
*
* Exit 0 on a completed run (even if recovery is 0 -- that is data),
* 2 on usage/codec errors.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <time.h>
#include <libgen.h>
#include <sys/resource.h>
#include "ecc_iface.h"
#include "sha256.h"
/* ---- codec factory (one constructor per family file) ---- */
ecc_codec_t *bch_ship_new(int t);
ecc_codec_t *bch_1k_new(int t);
ecc_codec_t *interleave_new(int depth, int t);
ecc_codec_t *ldpc_new(int rate_pct);
ecc_codec_t *polar_new(int rate_pct, int design_exp);
ecc_codec_t *uep_new(int crit_bytes, int t_hi, int t_lo);
static ecc_codec_t *make_codec(const char *name)
{
int v;
if (sscanf(name, "bch-t%d", &v) == 1)
return bch_ship_new(v);
if (sscanf(name, "bch1k-t%d", &v) == 1)
return bch_1k_new(v);
int d, t;
if (sscanf(name, "intlv-d%d-bch1k-t%d", &d, &t) == 2)
return interleave_new(d, t);
if (sscanf(name, "ldpc-r%d", &v) == 1)
return ldpc_new(v);
int dexp;
if (sscanf(name, "polar-r%d-d%d", &v, &dexp) == 2)
return polar_new(v, dexp);
if (sscanf(name, "polar-r%d", &v) == 1)
return polar_new(v, 2);
int cb, th, tl;
if (sscanf(name, "uep-c%d-h%d-l%d", &cb, &th, &tl) == 3)
return uep_new(cb, th, tl);
return NULL;
}
/* ---- xorshift64* RNG ---- */
static uint64_t rs = 0x12345678abcdefULL;
static inline uint64_t xr(void)
{
rs ^= rs << 13;
rs ^= rs >> 7;
rs ^= rs << 17;
return rs;
}
static double now_s(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
static uint8_t *read_file(const char *path, size_t *len_out)
{
FILE *f = fopen(path, "rb");
if (!f)
return NULL;
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n <= 0) {
fclose(f);
return NULL;
}
uint8_t *buf = (uint8_t *)malloc((size_t)n);
if (!buf) {
fclose(f);
return NULL;
}
if (fread(buf, 1, (size_t)n, f) != (size_t)n) {
free(buf);
fclose(f);
return NULL;
}
fclose(f);
*len_out = (size_t)n;
return buf;
}
static void usage(void)
{
fprintf(stderr,
"usage: bench --codec NAME --blob FILE [--size N] [--trials N]\n"
" [--seed S] (--ber X | --burst LEN,COUNT | --clean | --speed)\n"
" [--out results.csv]\n"
" codecs: bch-t{2,4,8,16,32} bch1k-t{8,16,32}\n"
" intlv-d{16,32,64}-bch1k-t{8,16,32} ldpc-r{90,95}\n"
" polar-r{90,95} uep-c{1024,4096}-h{16,32}-l{2,4}\n");
}
int main(int argc, char **argv)
{
const char *codec_name = NULL, *blob_path = NULL, *out_path = NULL;
size_t win_size = 65536;
long trials = 200;
uint64_t seed = 0x12345678abcdefULL;
enum { M_BER, M_BURST, M_CLEAN, M_SPEED } mode = M_BER;
double ber = 1e-3;
long burst_len = 256, burst_count = 1;
int nopass2 = 0;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--codec") && i + 1 < argc)
codec_name = argv[++i];
else if (!strcmp(argv[i], "--blob") && i + 1 < argc)
blob_path = argv[++i];
else if (!strcmp(argv[i], "--out") && i + 1 < argc)
out_path = argv[++i];
else if (!strcmp(argv[i], "--size") && i + 1 < argc)
win_size = (size_t)strtoull(argv[++i], NULL, 0);
else if (!strcmp(argv[i], "--trials") && i + 1 < argc)
trials = strtol(argv[++i], NULL, 0);
else if (!strcmp(argv[i], "--seed") && i + 1 < argc)
seed = strtoull(argv[++i], NULL, 0);
else if (!strcmp(argv[i], "--ber") && i + 1 < argc) {
mode = M_BER;
ber = strtod(argv[++i], NULL);
} else if (!strcmp(argv[i], "--burst") && i + 1 < argc) {
mode = M_BURST;
char *a = argv[++i];
burst_len = strtol(a, NULL, 0);
char *comma = strchr(a, ',');
burst_count = comma ? strtol(comma + 1, NULL, 0) : 1;
} else if (!strcmp(argv[i], "--nopass2")) {
nopass2 = 1;
} else if (!strcmp(argv[i], "--clean")) {
mode = M_CLEAN;
} else if (!strcmp(argv[i], "--speed")) {
mode = M_SPEED;
} else {
usage();
return 2;
}
}
if (!codec_name || !blob_path) {
usage();
return 2;
}
size_t file_len = 0;
uint8_t *file = read_file(blob_path, &file_len);
if (!file || file_len <= win_size) {
fprintf(stderr, "bench: cannot read blob %s (need > %zu bytes)\n",
blob_path, win_size);
return 2;
}
rs = seed ^ 0x9e3779b97f4a7c15ULL;
double t0 = now_s();
ecc_codec_t *c = make_codec(codec_name);
double init_s = now_s() - t0;
if (!c) {
fprintf(stderr, "bench: unknown codec %s\n", codec_name);
return 2;
}
const char *corpus = strrchr(blob_path, '/');
corpus = corpus ? corpus + 1 : blob_path;
uint8_t *win = (uint8_t *)malloc(win_size);
uint8_t *stored = NULL, *rx = NULL, *out = NULL;
uint8_t want_sha[32];
if (!win) return 2;
out = (uint8_t *)malloc(win_size);
if (!out) return 2;
long success = 0, rescued = 0;
double enc_s = 0, dec_s = 0;
double overhead = 0;
int have_overhead = 0;
if (mode == M_SPEED) {
/* encode/decode the whole file in windows, 5 passes */
long passes = 5;
size_t nw = file_len / win_size;
double te0 = now_s();
for (long p = 0; p < passes; p++)
for (size_t w = 0; w < nw; w++) {
free(stored);
stored = NULL;
size_t sl = c->encode(c, file + w * win_size, win_size, &stored);
if (!sl) {
fprintf(stderr, "bench: encode failed in speed mode\n");
return 2;
}
if (!have_overhead) {
overhead = c->overhead;
have_overhead = 1;
}
}
double te1 = now_s();
double td0 = now_s();
for (long p = 0; p < passes; p++)
for (size_t w = 0; w < nw; w++) {
size_t sl = c->encode(c, file + w * win_size, win_size, &stored);
long corr = -1;
if (c->decode(c, stored, sl, win_size, out, &corr) != 0 ||
memcmp(out, file + w * win_size, win_size) != 0) {
fprintf(stderr, "bench: speed-mode roundtrip FAILED\n");
return 2;
}
}
double td1 = now_s();
double bytes = (double)passes * nw * win_size;
double enc_mb = bytes / (te1 - te0) / 1e6;
double dec_mb = bytes / (td1 - td0) / 1e6;
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
fprintf(stderr,
"SPEED %-28s corpus=%s win=%zu init=%.2fs enc=%.1f MB/s "
"dec=%.1f MB/s rss=%ld KiB overhead=%.2f%%\n",
codec_name, corpus, win_size, init_s, enc_mb, dec_mb,
ru.ru_maxrss, overhead * 100.0);
FILE *f = out_path ? fopen(out_path, "a") : stdout;
if (f) {
fprintf(f, "%s,%s,%zu,%ld,speed,init=%.2fs,0,0,1.000,"
"%.2f,%.2f,%ld,%.4f\n",
codec_name, corpus, win_size, passes, init_s, enc_mb,
dec_mb, ru.ru_maxrss, overhead * 100.0);
if (f != stdout)
fclose(f);
}
return 0;
}
/* corruption trials */
uint64_t ber_thr = (uint64_t)(ber * 18446744073709551616.0);
rx = (uint8_t *)malloc(1 << 20); /* grown below if needed */
for (long tr = 0; tr < trials; tr++) {
size_t off = (size_t)(xr() % (file_len - win_size + 1));
memcpy(win, file + off, win_size);
lzply_sha256(win, win_size, want_sha);
double te0 = now_s();
size_t slen = c->encode(c, win, win_size, &stored);
double te1 = now_s();
if (!slen) {
fprintf(stderr, "bench: encode failed trial %ld\n", tr);
return 2;
}
if (!have_overhead) {
overhead = c->overhead;
have_overhead = 1;
}
if (tr == 0) {
free(rx);
rx = (uint8_t *)malloc(slen);
if (!rx)
return 2;
}
memcpy(rx, stored, slen);
/* corrupt the stored bytes */
if (mode == M_BER) {
for (size_t i = 0; i < slen; i++) {
uint8_t m = 0;
for (int b = 0; b < 8; b++)
if (xr() < ber_thr)
m |= (uint8_t)(1u << b);
rx[i] ^= m;
}
} else if (mode == M_BURST) {
for (long b = 0; b < burst_count; b++) {
if ((size_t)burst_len >= slen)
continue;
size_t start = (size_t)(xr() % (slen - (size_t)burst_len + 1));
for (long k = 0; k < burst_len; k++)
rx[start + k] = (uint8_t)(xr() & 0xff);
}
}
double td0 = now_s();
long corr = -1;
int rc = c->decode(c, rx, slen, win_size, out, &corr);
int ok = (rc == 0 && memcmp(out, win, win_size) == 0);
if (!ok && !nopass2 && c->recover_verified) {
if (c->recover_verified(c, rx, slen, win_size, want_sha, out) == 0) {
ok = 1;
rescued++;
}
}
double td1 = now_s();
enc_s += te1 - te0;
dec_s += td1 - td0;
if (ok)
success++;
free(stored);
stored = NULL;
}
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
double rate = trials ? (double)success / (double)trials : 0;
double enc_mb = enc_s > 0 ? (double)trials * win_size / enc_s / 1e6 : 0;
double dec_mb = dec_s > 0 ? (double)trials * win_size / dec_s / 1e6 : 0;
char param[64];
if (mode == M_BER)
snprintf(param, sizeof param, "ber=%g", ber);
else if (mode == M_BURST)
snprintf(param, sizeof param, "burst=%ldx%ld", burst_len, burst_count);
else
snprintf(param, sizeof param, "clean");
const char *mname =
mode == M_BER ? "ber" : mode == M_BURST ? "burst" : "clean";
FILE *f = out_path ? fopen(out_path, "a") : stdout;
if (f) {
fprintf(f, "%s,%s,%zu,%ld,%s,%s,%ld,%ld,%.4f,%.2f,%.2f,%ld,%.4f\n",
codec_name, corpus, win_size, trials, mname, param, success,
rescued, rate, enc_mb, dec_mb, ru.ru_maxrss,
overhead * 100.0);
if (f != stdout)
fclose(f);
}
fprintf(stderr, "%-28s %-12s %-14s %4ld trials rate=%.3f rescued=%ld "
"enc=%.0fMB/s dec=%.0fMB/s ovh=%.2f%% rss=%ldKiB\n",
codec_name, corpus, param, trials, rate, rescued, enc_mb, dec_mb,
overhead * 100.0, ru.ru_maxrss);
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
/* ecc_iface.h - generic error-correction codec interface for the lzply
* experiments harness (exp/). Every family under test implements this
* interface so bench.c measures them all the same way: encode a blob,
* corrupt the stored bytes, decode, and check exact recovery (SHA-256).
*
* A codec is a whole-blob transform: encode(data) -> stored bytes,
* decode(stored) -> data or failure. Block structure inside is the
* codec's business; the harness only sees blob in / blob out.
*/
#ifndef EXP_ECC_IFACE_H
#define EXP_ECC_IFACE_H
#include <stddef.h>
#include <stdint.h>
typedef struct ecc_codec ecc_codec_t;
struct ecc_codec {
const char *name; /* short id, e.g. "bch-t32" (used in CSV output) */
const char *desc; /* human-readable description */
size_t block_data; /* nominal data bytes per code block (0 = whole blob) */
/* encode data[0..len) -> malloc'd *out; return stored length, 0 on error.
Sets c->overhead = stored/len - 1. */
size_t (*encode)(ecc_codec_t *c, const uint8_t *data, size_t len,
uint8_t **out);
/* decode stored[0..slen) -> out[0..orig_len); 0 = exact recovery,
-1 = failure. *corrected = bits/symbols corrected if known, else -1. */
int (*decode)(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out, long *corrected);
/* Optional second-chance recovery: given the expected SHA-256 of the
original data, try harder (e.g. BCH pass-2 candidates) and use the
first candidate that verifies. 0 = rescued, -1 = not. May be NULL. */
int (*recover_verified)(ecc_codec_t *c, const uint8_t *stored,
size_t slen, size_t orig_len,
const uint8_t want_sha[32], uint8_t *out);
double overhead; /* from the last encode() call */
void (*destroy)(ecc_codec_t *c);
void *priv;
};
#endif /* EXP_ECC_IFACE_H */
+253
View File
@@ -0,0 +1,253 @@
/* interleave.c - byte interleaver over small-block BCH.
*
* The encoded stream (bch1k-t{T}: blocks of ~1 KiB data, t parity bits
* each) is permuted before it hits the channel: the stream is cut into
* groups of D consecutive blocks, each group written row-wise into a
* D-row matrix (row j = block j) and read out column-wise. A burst of
* L consecutive bytes in the channel then hits any one block at most
* ceil(L/D) bytes -- about 4*L/D bit errors, since a randomized byte
* flips ~4 bits -- instead of all L bytes landing in one block and
* killing it outright. Zero extra parity: the interleaver is a pure
* permutation, inverted on decode, so overhead is exactly the BCH
* overhead and random-error performance is unchanged. The win is
* purely burst tolerance, roughly a factor D in burst length.
*
* Groups are self-delimiting: block boundaries are recomputed from
* orig_len, so the decoder knows every group's shape. The final group
* may be short and the final block short; the column traversal simply
* skips missing cells, which keeps the permutation exact with no
* padding and no length change.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ecc_iface.h"
#include "bch.h"
#include "bch_wrap.h"
typedef struct {
ecc_codec_t *inner; /* bch1k-t{T} */
int depth; /* blocks per interleave group */
int t;
} intlv_priv_t;
/* Permute the encoded stream (forward = interleave).
* blocks[] holds the stored length of each block; nblocks total.
* out must hold the same total length.
*
* Groups: blocks are cut into groups of `depth`, EXCEPT the final
* partial group is merged into the previous one instead of standing
* alone. A tiny tail group is the weak point: a burst landing in a
* 4-block tail hits each of its blocks with L/4 bytes instead of
* L/64, which is exactly why the first measurement showed 0.94
* instead of 1.00 on 64-byte bursts (the 68-block blob ends in a
* 4-block tail). Merging makes every group >= depth (the last may
* exceed it); when nblocks <= depth there is a single group. */
static void permute(const uint8_t *in, uint8_t *out, const size_t *blocks,
size_t nblocks, int depth, int forward)
{
size_t *start = (size_t *)malloc((nblocks + 1) * sizeof(size_t));
if (!start)
return;
start[0] = 0;
for (size_t i = 0; i < nblocks; i++)
start[i + 1] = start[i] + blocks[i];
/* group boundaries: groups of `depth`, last partial group merged
into the previous one */
size_t g0 = 0;
while (g0 < nblocks) {
size_t remain = nblocks - g0;
size_t g = remain < (size_t)depth ? remain : (size_t)depth;
/* If this would leave a short tail after this group, extend
this group to absorb the tail. (Invariant: a short tail
never survives an iteration, so no group is ever < depth
except when the whole blob is.) */
if (remain > (size_t)depth && remain - (size_t)depth < (size_t)depth)
g = remain;
size_t maxlen = 0;
for (size_t j = 0; j < g; j++)
if (blocks[g0 + j] > maxlen)
maxlen = blocks[g0 + j];
size_t goff = start[g0];
size_t pos = 0;
for (size_t c = 0; c < maxlen; c++) {
for (size_t j = 0; j < g; j++) {
if (c >= blocks[g0 + j])
continue;
size_t src = start[g0 + j] + c;
size_t dst = goff + pos;
if (forward)
out[dst] = in[src];
else
out[src] = in[dst];
pos++;
}
}
g0 += g;
}
free(start);
}
static size_t intlv_encode(ecc_codec_t *c, const uint8_t *data, size_t len,
uint8_t **out)
{
intlv_priv_t *p = (intlv_priv_t *)c->priv;
uint8_t *enc = NULL;
size_t slen = p->inner->encode(p->inner, data, len, &enc);
if (!slen)
return 0;
/* block layout of the inner encoding */
size_t bd = p->inner->block_data;
size_t nblocks = (len + bd - 1) / bd;
size_t *blocks = (size_t *)malloc(nblocks * sizeof(size_t));
if (!blocks) {
free(enc);
return 0;
}
/* stored length of block i: full blocks first, last may be short.
The inner codec is bch_encode with a pinned poly, so stored
length is a pure function of the block's data length. Recover
it by splitting slen: all full blocks share one length. */
size_t off = 0;
for (size_t i = 0; i < nblocks; i++) {
size_t dlen = len - i * bd < bd ? len - i * bd : bd;
/* stored bytes for dlen data bytes under the pinned poly:
ceil((8*dlen + r)/8); r = t*13 for m=13 */
size_t r = (size_t)p->t * 13;
blocks[i] = (8 * dlen + r + 7) / 8;
off += blocks[i];
}
if (off != slen) { /* should not happen; bail out cleanly */
free(blocks);
free(enc);
return 0;
}
uint8_t *buf = (uint8_t *)malloc(slen);
if (!buf) {
free(blocks);
free(enc);
return 0;
}
permute(enc, buf, blocks, nblocks, p->depth, 1);
free(blocks);
free(enc);
c->overhead = p->inner->overhead;
*out = buf;
return slen;
}
static int intlv_decode(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out, long *corrected)
{
intlv_priv_t *p = (intlv_priv_t *)c->priv;
size_t bd = p->inner->block_data;
size_t nblocks = (orig_len + bd - 1) / bd;
size_t *blocks = (size_t *)malloc(nblocks * sizeof(size_t));
if (!blocks)
return -1;
size_t off = 0;
for (size_t i = 0; i < nblocks; i++) {
size_t dlen = orig_len - i * bd < bd ? orig_len - i * bd : bd;
size_t r = (size_t)p->t * 13;
blocks[i] = (8 * dlen + r + 7) / 8;
off += blocks[i];
}
if (off != slen) {
free(blocks);
return -1;
}
uint8_t *deint = (uint8_t *)malloc(slen);
if (!deint) {
free(blocks);
return -1;
}
permute(stored, deint, blocks, nblocks, p->depth, 0);
free(blocks);
int rc = p->inner->decode(p->inner, deint, slen, orig_len, out,
corrected);
free(deint);
return rc;
}
static int intlv_recover(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, const uint8_t want_sha[32],
uint8_t *out)
{
intlv_priv_t *p = (intlv_priv_t *)c->priv;
size_t bd = p->inner->block_data;
size_t nblocks = (orig_len + bd - 1) / bd;
size_t *blocks = (size_t *)malloc(nblocks * sizeof(size_t));
if (!blocks)
return -1;
size_t off = 0;
for (size_t i = 0; i < nblocks; i++) {
size_t dlen = orig_len - i * bd < bd ? orig_len - i * bd : bd;
size_t r = (size_t)p->t * 13;
blocks[i] = (8 * dlen + r + 7) / 8;
off += blocks[i];
}
if (off != slen) {
free(blocks);
return -1;
}
uint8_t *deint = (uint8_t *)malloc(slen);
if (!deint) {
free(blocks);
return -1;
}
permute(stored, deint, blocks, nblocks, p->depth, 0);
free(blocks);
int rc = p->inner->recover_verified
? p->inner->recover_verified(p->inner, deint, slen, orig_len,
want_sha, out)
: -1;
free(deint);
return rc;
}
static void intlv_destroy(ecc_codec_t *c)
{
intlv_priv_t *p = (intlv_priv_t *)c->priv;
if (p->inner)
p->inner->destroy(p->inner);
free(p);
free((void *)c->name);
free(c);
}
ecc_codec_t *interleave_new(int depth, int t)
{
ecc_codec_t *inner = bch_1k_new(t);
if (!inner)
return NULL;
ecc_codec_t *c = (ecc_codec_t *)calloc(1, sizeof(*c));
intlv_priv_t *p = (intlv_priv_t *)calloc(1, sizeof(*p));
if (!c || !p) {
free(c);
free(p);
inner->destroy(inner);
return NULL;
}
char *name = (char *)malloc(32);
if (!name) {
free(c);
free(p);
inner->destroy(inner);
return NULL;
}
snprintf(name, 32, "intlv-d%d-bch1k-t%d", depth, t);
p->inner = inner;
p->depth = depth;
p->t = t;
c->name = name;
c->desc = "byte interleaver over small-block BCH";
c->block_data = inner->block_data;
c->encode = intlv_encode;
c->decode = intlv_decode;
c->recover_verified = intlv_recover;
c->destroy = intlv_destroy;
c->priv = p;
return c;
}
+565
View File
@@ -0,0 +1,565 @@
/* ldpc.c - regular LDPC codec for the experiments harness.
*
* Two operating points, both n=8192 bits, column weight 3:
* ldpc-r90: m=768 checks, row weight 32, k=7424 bits (928 B),
* rate 0.90625, overhead 10.34%.
* ldpc-r95: m=384 checks, row weight 64, k=7808 bits (976 B),
* rate 0.953125, overhead 4.92%.
*
* Construction: random regular parity-check matrix (configuration
* model with 4-cycle rejection), fixed seed, so the code is identical
* on every run and every server. Systematic encoding by Gauss-Jordan
* reduction of H to [I | P] (one-time, at codec creation); decoding
* is normalized min-sum belief propagation (factor 0.8), max 50
* iterations, early stop when the syndrome is zero. Channel LLR uses
* a fixed nominal crossover p0 = 1e-3 (the decoder does not know the
* true BER; this is the honest setting for a storage channel).
*
* A blob is split into blocks of k data bytes; the last block is
* zero-padded to k bytes (padding is discarded on decode). Stored
* length per block is exactly n/8 = 1024 bytes.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ecc_iface.h"
#define LDPC_N 8192
#define LDPC_WC 3
#define LDPC_MAX_ITER 50
#define LDPC_MS_NORM 0.8f
#define LDPC_P0 1e-3
typedef struct {
int m; /* checks */
int wr; /* row weight */
int k; /* data bits */
int kbytes; /* data bytes per block */
int nedges;
int *chk_var; /* m * wr: vars of each check */
int *var_chk; /* n * wc: checks of each var */
int *chk_edge; /* m * wr: edge id (var*wc+w) of each check entry */
int *pivot_col; /* m: pivot column of each RREF row */
uint64_t *enc_rows; /* m * kwords: RREF rows over data positions */
int kwords;
int *col_of_dp; /* k: column holding data position i */
float *msg_vc; /* var->check, nedges */
float *msg_cv; /* check->var, nedges */
float *llr_ch; /* n */
uint8_t *hard; /* n */
} ldpc_t;
static void free_ldpc(ldpc_t *L)
{
if (!L)
return;
free(L->chk_var);
free(L->var_chk);
free(L->chk_edge);
free(L->pivot_col);
free(L->enc_rows);
free(L->col_of_dp);
free(L->msg_vc);
free(L->msg_cv);
free(L->llr_ch);
free(L->hard);
free(L);
}
static uint64_t lrs = 0x243f6a8885a308d3ULL;
static inline uint64_t lxr(void)
{
lrs ^= lrs << 13;
lrs ^= lrs >> 7;
lrs ^= lrs << 17;
return lrs;
}
/* ---- construction: progressive edge growth (PEG) ----
* Edge by edge, each new edge of variable node v connects to the
* check node farthest from v in the graph built so far (unreached
* checks count as infinitely far), tie-broken by lowest check degree.
* PEG never deadlocks (there is always a farthest check) and
* maximizes girth by construction -- unlike greedy 4-cycle
* rejection, which stalls in the endgame when the remaining stub
* rows all close short cycles. Fallback: if every farthest check is
* already full (cannot happen with balanced degrees, but be safe),
* take the lowest-degree check overall. */
static int build_H(ldpc_t *L, uint64_t seed)
{
int m = L->m, n = LDPC_N, wc = LDPC_WC, wr = L->wr;
int *chk_deg = (int *)calloc((size_t)m, sizeof(int));
int *var_deg = (int *)calloc((size_t)n, sizeof(int));
int *dist = (int *)malloc((size_t)(n + m) * sizeof(int));
int *queue = (int *)malloc((size_t)(n + m) * sizeof(int));
if (!chk_deg || !var_deg || !dist || !queue) {
free(chk_deg);
free(var_deg);
free(dist);
free(queue);
return -1;
}
lrs = seed;
(void)lxr; /* seed only matters for tie-breaking below */
for (int v = 0; v < n; v++) {
for (int e = 0; e < wc; e++) {
/* BFS from v over the graph built so far. Node ids:
0..n-1 variables, n..n+m-1 checks. */
for (int i = 0; i < n + m; i++)
dist[i] = -1;
int qh = 0, qt = 0;
dist[v] = 0;
queue[qt++] = v;
int found_checks = 0;
while (qh < qt && found_checks < m) {
int u = queue[qh++];
int d = dist[u];
if (u < n) {
for (int w = 0; w < var_deg[u]; w++) {
int nid = n + L->var_chk[u * wc + w];
if (dist[nid] < 0) {
dist[nid] = d + 1;
queue[qt++] = nid;
found_checks++;
}
}
} else {
int ch = u - n;
for (int k = 0; k < chk_deg[ch]; k++) {
int vv = L->chk_var[ch * wr + k];
if (dist[vv] < 0) {
dist[vv] = d + 1;
queue[qt++] = vv;
}
}
}
}
/* farthest distance among reached checks (-1 = unreached) */
int bestd = -2, have_unreached = 0;
for (int ch = 0; ch < m; ch++) {
int d = dist[n + ch];
if (d < 0) {
have_unreached = 1;
} else if (d > bestd) {
bestd = d;
}
}
int best = -1, bestdeg = wr + 1;
for (int ch = 0; ch < m; ch++) {
int d = dist[n + ch];
int far = have_unreached ? (d < 0) : (d == bestd);
if (!far)
continue;
if (chk_deg[ch] < wr && chk_deg[ch] < bestdeg) {
bestdeg = chk_deg[ch];
best = ch;
}
}
if (best < 0) {
/* fallback: lowest-degree check anywhere */
for (int ch = 0; ch < m; ch++)
if (chk_deg[ch] < wr && chk_deg[ch] < bestdeg) {
bestdeg = chk_deg[ch];
best = ch;
}
}
if (best < 0) {
free(chk_deg);
free(var_deg);
free(dist);
free(queue);
return -1;
}
L->var_chk[v * wc + var_deg[v]++] = best;
L->chk_var[best * wr + chk_deg[best]++] = v;
}
}
free(chk_deg);
free(var_deg);
free(dist);
free(queue);
return 0;
}
/* ---- Gauss-Jordan: H -> RREF, record pivot columns + enc rows ---- */
static int gauss_jordan(ldpc_t *L)
{
int m = L->m, n = LDPC_N;
int words = (n + 63) / 64;
uint64_t *H = (uint64_t *)calloc((size_t)m * words, sizeof(uint64_t));
if (!H)
return -1;
for (int r = 0; r < m; r++)
for (int k = 0; k < L->wr; k++) {
int c = L->chk_var[r * L->wr + k];
H[(size_t)r * words + c / 64] |= 1ULL << (c % 64);
}
int prow = 0;
for (int col = 0; col < n && prow < m; col++) {
int sel = -1;
for (int r = prow; r < m; r++)
if ((H[(size_t)r * words + col / 64] >> (col % 64)) & 1ULL) {
sel = r;
break;
}
if (sel < 0)
continue;
if (sel != prow) {
for (int w = 0; w < words; w++) {
uint64_t t = H[(size_t)sel * words + w];
H[(size_t)sel * words + w] = H[(size_t)prow * words + w];
H[(size_t)prow * words + w] = t;
}
}
for (int r = 0; r < m; r++) {
if (r != prow &&
((H[(size_t)r * words + col / 64] >> (col % 64)) & 1ULL)) {
for (int w = 0; w < words; w++)
H[(size_t)r * words + w] ^=
H[(size_t)prow * words + w];
}
}
L->pivot_col[prow++] = col;
}
if (prow != m) {
free(H);
return -1; /* H not full rank */
}
int *is_pivot = (int *)calloc((size_t)n, sizeof(int));
if (!is_pivot) {
free(H);
return -1;
}
for (int i = 0; i < m; i++)
is_pivot[L->pivot_col[i]] = 1;
L->col_of_dp = (int *)malloc((size_t)L->k * sizeof(int));
if (!L->col_of_dp) {
free(H);
free(is_pivot);
return -1;
}
int dp = 0;
for (int c = 0; c < n; c++)
if (!is_pivot[c])
L->col_of_dp[dp++] = c;
if (dp != L->k) {
free(H);
free(is_pivot);
return -1;
}
L->kwords = (L->k + 63) / 64;
L->enc_rows = (uint64_t *)calloc((size_t)m * L->kwords,
sizeof(uint64_t));
if (!L->enc_rows) {
free(H);
free(is_pivot);
return -1;
}
int *data_pos = (int *)malloc((size_t)n * sizeof(int));
if (!data_pos) {
free(H);
free(is_pivot);
return -1;
}
dp = 0;
for (int c = 0; c < n; c++)
data_pos[c] = is_pivot[c] ? -1 : dp++;
for (int i = 0; i < m; i++) {
for (int c = 0; c < n; c++) {
if (is_pivot[c])
continue;
if ((H[(size_t)i * words + c / 64] >> (c % 64)) & 1ULL) {
int p = data_pos[c];
L->enc_rows[(size_t)i * L->kwords + p / 64] |=
1ULL << (p % 64);
}
}
}
free(data_pos);
free(H);
free(is_pivot);
return 0;
}
/* bit helpers: MSB-first within each byte */
static inline int get_bit(const uint8_t *buf, int i)
{
return (buf[i / 8] >> (7 - (i % 8))) & 1;
}
static inline void set_bit(uint8_t *buf, int i, int v)
{
if (v)
buf[i / 8] |= (uint8_t)(1u << (7 - (i % 8)));
else
buf[i / 8] &= (uint8_t) ~(1u << (7 - (i % 8)));
}
static size_t ldpc_encode(ecc_codec_t *c, const uint8_t *data, size_t len,
uint8_t **out)
{
ldpc_t *L = (ldpc_t *)c->priv;
size_t nblocks = (len + (size_t)L->kbytes - 1) / (size_t)L->kbytes;
size_t slen = nblocks * (LDPC_N / 8);
uint8_t *buf = (uint8_t *)calloc(slen ? slen : 1, 1);
if (!buf)
return 0;
uint8_t *dbytes = (uint8_t *)calloc((size_t)L->kbytes, 1);
uint64_t *dw = (uint64_t *)malloc((size_t)L->kwords * sizeof(uint64_t));
if (!dbytes || !dw) {
free(buf);
free(dbytes);
free(dw);
return 0;
}
for (size_t b = 0; b < nblocks; b++) {
size_t dlen = len - b * (size_t)L->kbytes < (size_t)L->kbytes
? len - b * (size_t)L->kbytes
: (size_t)L->kbytes;
memset(dbytes, 0, (size_t)L->kbytes);
memcpy(dbytes, data + b * (size_t)L->kbytes, dlen);
/* data as words, bit i of the block -> word i/64, bit i%64 */
memset(dw, 0, (size_t)L->kwords * sizeof(uint64_t));
for (int i = 0; i < L->k; i++)
if (get_bit(dbytes, i))
dw[i / 64] |= 1ULL << (i % 64);
uint8_t *cw = buf + b * (LDPC_N / 8);
for (int i = 0; i < L->k; i++)
set_bit(cw, L->col_of_dp[i], get_bit(dbytes, i));
for (int i = 0; i < L->m; i++) {
const uint64_t *row = L->enc_rows + (size_t)i * L->kwords;
int acc = 0;
for (int w = 0; w < L->kwords; w++)
acc ^= __builtin_parityll(row[w] & dw[w]);
set_bit(cw, L->pivot_col[i], acc);
}
}
free(dbytes);
free(dw);
c->overhead = len ? (double)slen / (double)len - 1.0 : 0.0;
*out = buf;
return slen;
}
static int ldpc_decode_block(ldpc_t *L, const uint8_t *cw_in,
uint8_t *data_out, long *corrected_out)
{
int n = LDPC_N, m = L->m, wc = LDPC_WC, wr = L->wr;
float L0 = (float)log((1.0 - LDPC_P0) / LDPC_P0);
for (int i = 0; i < n; i++) {
L->llr_ch[i] = get_bit(cw_in, i) ? -L0 : L0;
for (int w = 0; w < wc; w++)
L->msg_vc[i * wc + w] = L->llr_ch[i];
}
int converged = 0;
for (int it = 0; it < LDPC_MAX_ITER && !converged; it++) {
/* check update: two-minimum min-sum, via chk_edge ids */
for (int j = 0; j < m; j++) {
float min1 = 1e30f, min2 = 1e30f;
int argmin1 = -1, sign = 1;
for (int k = 0; k < wr; k++) {
int e = L->chk_edge[j * wr + k];
float x = L->msg_vc[e];
float a = fabsf(x);
if (a < min1) {
min2 = min1;
min1 = a;
argmin1 = k;
} else if (a < min2) {
min2 = a;
}
if (x < 0)
sign = -sign;
}
for (int k = 0; k < wr; k++) {
int e = L->chk_edge[j * wr + k];
float x = L->msg_vc[e];
int s = sign;
if (x < 0)
s = -s;
float mag = (k == argmin1 ? min2 : min1) * LDPC_MS_NORM;
L->msg_cv[e] = s > 0 ? mag : -mag;
}
}
/* hard decision + syndrome check */
converged = 1;
for (int i = 0; i < n; i++) {
float tot = L->llr_ch[i];
for (int w = 0; w < wc; w++)
tot += L->msg_cv[i * wc + w];
L->hard[i] = tot < 0 ? 1 : 0;
}
for (int j = 0; j < m; j++) {
int p = 0;
for (int k = 0; k < wr; k++)
p ^= L->hard[L->chk_var[j * wr + k]];
if (p) {
converged = 0;
break;
}
}
if (converged)
break;
/* var->check messages for the next iteration */
for (int i = 0; i < n; i++)
for (int w = 0; w < wc; w++) {
float tot = L->llr_ch[i];
for (int w2 = 0; w2 < wc; w2++)
if (w2 != w)
tot += L->msg_cv[i * wc + w2];
L->msg_vc[i * wc + w] = tot;
}
}
if (!converged)
return -1;
long corr = 0;
for (int i = 0; i < n; i++)
if (L->hard[i] != get_bit(cw_in, i))
corr++;
if (corrected_out)
*corrected_out = corr;
int *is_pivot = (int *)calloc((size_t)n, sizeof(int));
if (!is_pivot)
return -1;
for (int i = 0; i < m; i++)
is_pivot[L->pivot_col[i]] = 1;
int dp = 0;
memset(data_out, 0, (size_t)L->kbytes);
for (int col = 0; col < n; col++)
if (!is_pivot[col])
set_bit(data_out, dp++, L->hard[col]);
free(is_pivot);
return 0;
}
static int ldpc_decode(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out, long *corrected)
{
ldpc_t *L = (ldpc_t *)c->priv;
size_t nblocks = (orig_len + (size_t)L->kbytes - 1) / (size_t)L->kbytes;
if (slen != nblocks * (LDPC_N / 8))
return -1;
long corr = 0;
for (size_t b = 0; b < nblocks; b++) {
uint8_t *db = (uint8_t *)malloc((size_t)L->kbytes);
if (!db)
return -1;
long bc = 0;
int rc = ldpc_decode_block(L, stored + b * (LDPC_N / 8), db, &bc);
if (rc != 0) {
free(db);
if (corrected)
*corrected = -1;
return -1;
}
corr += bc;
size_t dlen = orig_len - b * (size_t)L->kbytes < (size_t)L->kbytes
? orig_len - b * (size_t)L->kbytes
: (size_t)L->kbytes;
memcpy(out + b * (size_t)L->kbytes, db, dlen);
free(db);
}
if (corrected)
*corrected = corr;
return 0;
}
static void ldpc_destroy(ecc_codec_t *c)
{
ldpc_t *L = (ldpc_t *)c->priv;
free_ldpc(L);
free((void *)c->name);
free(c);
}
ecc_codec_t *ldpc_new(int rate_pct)
{
int m, wr;
const char *tag;
if (rate_pct == 90) {
m = 768;
wr = 32;
tag = "ldpc-r90";
} else if (rate_pct == 95) {
m = 384;
wr = 64;
tag = "ldpc-r95";
} else {
return NULL;
}
ldpc_t *L = (ldpc_t *)calloc(1, sizeof(*L));
if (!L)
return NULL;
L->m = m;
L->wr = wr;
L->k = LDPC_N - m;
L->kbytes = L->k / 8;
L->nedges = LDPC_N * LDPC_WC;
L->chk_var = (int *)malloc((size_t)m * wr * sizeof(int));
L->var_chk = (int *)malloc((size_t)LDPC_N * LDPC_WC * sizeof(int));
L->chk_edge = (int *)malloc((size_t)m * wr * sizeof(int));
L->pivot_col = (int *)malloc((size_t)m * sizeof(int));
L->msg_vc = (float *)malloc((size_t)L->nedges * sizeof(float));
L->msg_cv = (float *)malloc((size_t)L->nedges * sizeof(float));
L->llr_ch = (float *)malloc((size_t)LDPC_N * sizeof(float));
L->hard = (uint8_t *)malloc((size_t)LDPC_N);
if (!L->chk_var || !L->var_chk || !L->chk_edge || !L->pivot_col ||
!L->msg_vc || !L->msg_cv || !L->llr_ch || !L->hard) {
free_ldpc(L);
return NULL;
}
int ok = -1;
int attempt_used = -1;
for (int attempt = 0; attempt < 10 && ok != 0; attempt++) {
ok = build_H(L, 0x9e3779b97f4a7c15ULL +
(uint64_t)attempt * 0x100000001b3ULL);
attempt_used = attempt;
}
if (getenv("LDPC_DEBUG"))
fprintf(stderr, "ldpc: build_H ok=%d attempts=%d\n", ok,
attempt_used + 1);
if (ok != 0) {
free_ldpc(L);
return NULL;
}
int gj = gauss_jordan(L);
if (getenv("LDPC_DEBUG"))
fprintf(stderr, "ldpc: gauss_jordan=%d\n", gj);
if (gj != 0) {
free_ldpc(L);
return NULL;
}
/* edge ids for the decoder: chk_edge[j*wr+k] = var*wc+w */
for (int j = 0; j < m; j++)
for (int k = 0; k < wr; k++) {
int v = L->chk_var[j * wr + k];
int e = -1;
for (int w = 0; w < LDPC_WC; w++)
if (L->var_chk[v * LDPC_WC + w] == j)
e = v * LDPC_WC + w;
L->chk_edge[j * wr + k] = e;
}
ecc_codec_t *c = (ecc_codec_t *)calloc(1, sizeof(*c));
if (!c) {
free_ldpc(L);
return NULL;
}
char *name = (char *)malloc(16);
if (!name) {
free_ldpc(L);
free(c);
return NULL;
}
snprintf(name, 16, "%s", tag);
c->name = name;
c->desc = "regular LDPC, min-sum belief propagation";
c->block_data = (size_t)L->kbytes;
c->encode = ldpc_encode;
c->decode = ldpc_decode;
c->recover_verified = NULL;
c->destroy = ldpc_destroy;
c->priv = L;
return c;
}
+71
View File
@@ -0,0 +1,71 @@
/* ldpc_diag.c - diagnostic for the LDPC codec (NOT part of bench).
* Encodes one fixed block, flips exactly k bits (k = 0..KMAX), decodes,
* and reports the failure count per k. This separates "decoder bug"
* (fails even at k=1..3) from "code weakness" (fails only near the
* expected threshold). Usage: ldpc_diag <r90|r95> [kmax] [trials]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "ecc_iface.h"
ecc_codec_t *ldpc_new(int rate_pct);
static uint64_t rs = 0xabcdef1234567ULL;
static inline uint64_t xr(void)
{
rs ^= rs << 13;
rs ^= rs >> 7;
rs ^= rs << 17;
return rs;
}
int main(int argc, char **argv)
{
int rate = 90;
int kmax = 24, trials = 100;
if (argc > 1)
rate = atoi(argv[1] + 1); /* "r90" -> 90 */
if (argc > 2)
kmax = atoi(argv[2]);
if (argc > 3)
trials = atoi(argv[3]);
ecc_codec_t *c = ldpc_new(rate);
if (!c) {
fprintf(stderr, "ldpc_new failed\n");
return 2;
}
size_t dlen = c->block_data;
uint8_t *data = (uint8_t *)malloc(dlen);
uint8_t *out = (uint8_t *)malloc(dlen);
for (size_t i = 0; i < dlen; i++)
data[i] = (uint8_t)(xr() & 0xff);
uint8_t *stored = NULL;
size_t slen = c->encode(c, data, dlen, &stored);
if (!slen) {
fprintf(stderr, "encode failed\n");
return 2;
}
printf("rate=%d block_data=%zu stored=%zu overhead=%.2f%%\n", rate,
dlen, slen, c->overhead * 100.0);
uint8_t *rx = (uint8_t *)malloc(slen);
for (int k = 0; k <= kmax; k++) {
int fails = 0;
for (int t = 0; t < trials; t++) {
memcpy(rx, stored, slen);
/* flip k distinct random bits */
for (int f = 0; f < k; f++) {
size_t p = (size_t)(xr() % (slen * 8));
rx[p / 8] ^= (uint8_t)(1u << (7 - (p % 8)));
}
long corr = -1;
int rc = c->decode(c, rx, slen, dlen, out, &corr);
if (rc != 0 || memcmp(out, data, dlen) != 0)
fails++;
}
printf("k=%2d fails=%d/%d\n", k, fails, trials);
fflush(stdout);
}
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
#!/bin/bash
# mkcorpora.sh - build the experiment corpora in exp/corpora/.
# text.raw / text.lz : English-ish text (docs + sources)
# bin.raw / bin.lz : ELF binaries
# rand.raw / rand.lz : seeded pseudo-random bytes (incompressible)
# lz files are lzip -9 of the raw files: that is what lzply actually
# protects (the compressed blob), plus the raw files for stored entries.
set -e
cd "$(dirname "$0")/corpora"
# text: repo docs/sources + system docs
{ find /opt/lzply-exp-ecc -name "*.md" -o -name "*.c" -o -name "*.h" | sort | xargs cat
find /usr/share/doc -type f -size -200k 2>/dev/null | head -200 | sort | xargs cat 2>/dev/null
find /usr/include -name "*.h" 2>/dev/null | head -300 | sort | xargs cat 2>/dev/null
} > text.raw
head -c 4194304 text.raw > text4m.raw && mv text4m.raw text.raw
# binaries: ELF files from /bin and /usr/bin
{ for f in /bin/* /usr/bin/*; do
[ -f "$f" ] && head -c1 "$f" | grep -q . && file "$f" 2>/dev/null | grep -q ELF && cat "$f"
done
} > bin.raw 2>/dev/null
head -c 4194304 bin.raw > bin4m.raw && mv bin4m.raw bin.raw
# random: seeded python (reproducible on both servers)
python3 -c "import random; random.seed(1234); open(\"rand.raw\",\"wb\").write(bytes(random.getrandbits(8) for _ in range(4194304)))"
for f in text bin rand; do
lzip -9 -f -c "$f.raw" > "$f.lz" 2>/dev/null || lzip -9 -c "$f.raw" > "$f.lz"
done
ls -la
+89
View File
@@ -0,0 +1,89 @@
/* pass2_diag.c - targeted test of BCH pass-2 (hash-gated candidates).
* Encodes one bch1k block, flips exactly k bits (k around t), and
* reports: pass-1 success, and pass-2 rescue (candidate verifies
* against the SHA-256 of the original). This separates "pass 2 is
* weak" from "the harness wiring is broken".
* Usage: pass2_diag <t> [kmax] [trials]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "bch.h"
#include "bch_polys.h"
#include "sha256.h"
static uint64_t rs = 0xabcdef1234567ULL;
static inline uint64_t xr(void)
{
rs ^= rs << 13;
rs ^= rs >> 7;
rs ^= rs << 17;
return rs;
}
static const bch_poly_t *poly_by_mt(int m, int t)
{
for (int i = 0; i < BCH_NPOLYS; i++)
if (bch_polys[i].m == m && bch_polys[i].t == t)
return &bch_polys[i];
return NULL;
}
int main(int argc, char **argv)
{
int t = argc > 1 ? atoi(argv[1]) : 16;
int kmax = argc > 2 ? atoi(argv[2]) : t + 8;
int trials = argc > 3 ? atoi(argv[3]) : 50;
const bch_poly_t *p = poly_by_mt(13, t);
if (!p) {
fprintf(stderr, "no poly m=13 t=%d\n", t);
return 2;
}
size_t dlen = bch_max_data(p);
size_t slen = bch_block_stored_len(dlen, p);
uint8_t *data = (uint8_t *)malloc(dlen);
uint8_t *stored = (uint8_t *)malloc(slen);
uint8_t *rx = (uint8_t *)malloc(slen);
uint8_t *out = (uint8_t *)malloc(dlen);
uint8_t *cands = (uint8_t *)malloc((size_t)BCH_PASS2_MAX_CANDS * dlen);
for (size_t i = 0; i < dlen; i++)
data[i] = (uint8_t)(xr() & 0xff);
if (bch_encode_block(data, dlen, p, stored) != 0) {
fprintf(stderr, "encode failed\n");
return 2;
}
uint8_t want[32];
lzply_sha256(data, dlen, want);
printf("t=%d block_data=%zu stored=%zu\n", t, dlen, slen);
for (int k = t - 2; k <= kmax; k++) {
int p1 = 0, rescued = 0;
for (int tr = 0; tr < trials; tr++) {
memcpy(rx, stored, slen);
for (int f = 0; f < k; f++) {
size_t pos = (size_t)(xr() % (slen * 8));
rx[pos / 8] ^= (uint8_t)(1u << (7 - (pos % 8)));
}
int corr = -1;
if (bch_decode_block(rx, slen, dlen, p, out, &corr) == 0 &&
memcmp(out, data, dlen) == 0) {
p1++;
continue;
}
int nc = bch_decode_block_candidates(rx, slen, dlen, p, cands,
BCH_PASS2_MAX_CANDS);
for (int i = 0; i < nc; i++) {
uint8_t h[32];
lzply_sha256(cands + (size_t)i * dlen, dlen, h);
if (memcmp(h, want, 32) == 0) {
rescued++;
break;
}
}
}
printf("k=%2d pass1=%d/%d rescued=%d/%d\n", k, p1, trials, rescued,
trials);
fflush(stdout);
}
return 0;
}
+333
View File
@@ -0,0 +1,333 @@
/* polar.c - polar code codec for the experiments harness.
*
* N=4096, successive-cancellation (SC) decoding with the min-sum
* f-function. Construction is Arikan's Bhattacharyya recursion for
* the binary erasure channel with erasure probability equal to the
* BSC's Bhattacharyya parameter Z = 2*sqrt(p(1-p)) at a design
* crossover p (see polar_new: the codec name selects it, e.g.
* polar-r90-d3 designs for p=1e-3; plain polar-r90 designs for
* p=1e-2). The design point matters: designing for a much worse
* channel than the operating point ranks the marginal channels
* wrong and SC pays for it.
*
* polar-r90: K=3680 info bits (460 B), rate 0.89844, overhead 11.30%.
* polar-r95: K=3888 info bits (486 B), rate 0.94922, overhead 5.35%.
*
* A blob is split into blocks of K data bytes; the last block is
* zero-padded. Stored length per block is exactly N/8 = 512 bytes.
*
* Note: SC decoding always returns a codeword, so decode() cannot
* detect failure by itself -- it returns 0 whenever decoding
* completes, and the caller (bench / lzply's SHA-256) is the oracle
* that decides whether the recovered data is correct. That matches
* lzply's architecture: the entry SHA is the miscorrection guard.
* Channel LLR uses a fixed nominal crossover p0 = 1e-3, same honest
* setting as the LDPC codec.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ecc_iface.h"
#define POLAR_N 4096
#define POLAR_LOGN 12
#define POLAR_P0 1e-3
typedef struct {
int k; /* info bits */
int kbytes; /* info bytes per block */
double p_design; /* design crossover for the BEC construction */
uint8_t frozen[POLAR_N]; /* 1 = frozen (0 bit) */
float *llr; /* N, decode workspace */
float *ws; /* N, SC scratch (bump-allocated down the tree) */
uint8_t *bits; /* N, decoded info vector (leaf decisions) */
uint8_t *psum; /* N, partial sums (re-encoded codeword) */
} polar_t;
static void polar_build_frozen(polar_t *P)
{
/* BEC Bhattacharyya recursion: Z starts at eps, then per level
the upper (bad) channel gets 2Z-Z^2 and the lower (good) one
gets Z^2. z[] holds the values in bit-reversed order matching
the standard construction; sorting by Z ascending gives the
reliability order. */
double eps = 2.0 * sqrt(P->p_design * (1.0 - P->p_design));
double z[POLAR_N];
for (int i = 0; i < POLAR_N; i++)
z[i] = eps;
for (int level = 0; level < POLAR_LOGN; level++) {
int step = 1 << (level + 1);
int half = 1 << level;
for (int base = 0; base < POLAR_N; base += step) {
for (int i = 0; i < half; i++) {
double a = z[base + i];
double b = z[base + half + i];
/* upper (first half) is the worse channel */
z[base + i] = 2 * a - a * a;
z[base + half + i] = b * b;
}
}
}
/* The recursion above indexes channels in natural order only if
the pairing is right; verify by construction: sort indices by
Z and take the K smallest as information positions. */
int idx[POLAR_N];
for (int i = 0; i < POLAR_N; i++)
idx[i] = i;
/* simple insertion-free sort: qsort with a comparator on z */
/* (z is captured via a file-scope pointer for the comparator) */
extern double *polar_sort_z;
polar_sort_z = z;
/* qsort needs a comparator; defined below */
extern int polar_cmp_idx(const void *, const void *);
qsort(idx, POLAR_N, sizeof(int), polar_cmp_idx);
memset(P->frozen, 1, POLAR_N);
for (int i = 0; i < P->k; i++)
P->frozen[idx[i]] = 0;
}
double *polar_sort_z = NULL;
int polar_cmp_idx(const void *a, const void *b)
{
int ia = *(const int *)a, ib = *(const int *)b;
if (polar_sort_z[ia] < polar_sort_z[ib])
return -1;
if (polar_sort_z[ia] > polar_sort_z[ib])
return 1;
return ia - ib;
}
/* polar transform: in-place butterfly, u ^= u shifted */
static void polar_transform(uint8_t *u, int n)
{
for (int len = 1; len < n; len <<= 1) {
for (int base = 0; base < n; base += 2 * len) {
for (int i = 0; i < len; i++) {
int a = base + i, b = base + len + i;
u[a] ^= u[b];
}
}
}
}
static inline int get_bit(const uint8_t *buf, int i)
{
return (buf[i / 8] >> (7 - (i % 8))) & 1;
}
static inline void set_bit(uint8_t *buf, int i, int v)
{
if (v)
buf[i / 8] |= (uint8_t)(1u << (7 - (i % 8)));
else
buf[i / 8] &= (uint8_t) ~(1u << (7 - (i % 8)));
}
static size_t polar_encode(ecc_codec_t *c, const uint8_t *data, size_t len,
uint8_t **out)
{
polar_t *P = (polar_t *)c->priv;
size_t nblocks = (len + (size_t)P->kbytes - 1) / (size_t)P->kbytes;
size_t slen = nblocks * (POLAR_N / 8);
uint8_t *buf = (uint8_t *)calloc(slen ? slen : 1, 1);
if (!buf)
return 0;
uint8_t *u = (uint8_t *)malloc(POLAR_N);
if (!u) {
free(buf);
return 0;
}
for (size_t b = 0; b < nblocks; b++) {
size_t dlen = len - b * (size_t)P->kbytes < (size_t)P->kbytes
? len - b * (size_t)P->kbytes
: (size_t)P->kbytes;
memset(u, 0, POLAR_N);
int dp = 0;
for (int i = 0; i < POLAR_N; i++) {
if (P->frozen[i])
continue;
if (dp < (int)dlen * 8)
u[i] = (uint8_t)get_bit(data + b * (size_t)P->kbytes, dp);
dp++;
}
polar_transform(u, POLAR_N);
uint8_t *cw = buf + b * (POLAR_N / 8);
for (int i = 0; i < POLAR_N; i++)
set_bit(cw, i, u[i]);
}
free(u);
c->overhead = len ? (double)slen / (double)len - 1.0 : 0.0;
*out = buf;
return slen;
}
static inline float polar_f(float a, float b)
{
float m = fabsf(a) < fabsf(b) ? fabsf(a) : fabsf(b);
int s = ((a < 0) ^ (b < 0)) ? -1 : 1;
return s * m;
}
static inline float polar_g(float a, float b, int u)
{
return b + (u ? -a : a);
}
static void sc_rec(polar_t *P, const float *llr, int off, int len,
float *ws)
{
if (len == 1) {
/* Leaf: the decision is the decoded info bit. The partial
sum at a leaf is the bit itself. The two arrays must stay
separate: the combine step below rewrites psum (it becomes
the re-encoded codeword) and must not touch bits. */
int b = 0;
if (!P->frozen[off])
b = llr[0] < 0 ? 1 : 0;
P->bits[off] = (uint8_t)b;
P->psum[off] = (uint8_t)b;
return;
}
int half = len / 2;
/* Scratch for this level lives in ws[0..half); children bump
past it. Total scratch along any path is N floats. */
float *tmp = ws;
for (int i = 0; i < half; i++)
tmp[i] = polar_f(llr[i], llr[half + i]);
sc_rec(P, tmp, off, half, ws + half);
for (int i = 0; i < half; i++)
tmp[i] = polar_g(llr[i], llr[half + i], P->psum[off + i]);
sc_rec(P, tmp, off + half, half, ws + half);
for (int i = 0; i < half; i++)
P->psum[off + i] ^= P->psum[off + half + i];
}
static int polar_decode_block(polar_t *P, const uint8_t *cw_in,
uint8_t *data_out, long *corrected_out)
{
float L0 = (float)log((1.0 - POLAR_P0) / POLAR_P0);
for (int i = 0; i < POLAR_N; i++)
P->llr[i] = get_bit(cw_in, i) ? -L0 : L0;
sc_rec(P, P->llr, 0, POLAR_N, P->ws);
/* psum now holds the decoded codeword; count corrected bits
against the received codeword. */
long corr = 0;
for (int i = 0; i < POLAR_N; i++)
if (P->psum[i] != get_bit(cw_in, i))
corr++;
if (corrected_out)
*corrected_out = corr;
/* data bits out: info positions in increasing index order */
int dp = 0;
memset(data_out, 0, (size_t)P->kbytes);
for (int i = 0; i < POLAR_N; i++) {
if (P->frozen[i])
continue;
set_bit(data_out, dp++, P->bits[i]);
}
return 0;
}
static int polar_decode(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out, long *corrected)
{
polar_t *P = (polar_t *)c->priv;
size_t nblocks = (orig_len + (size_t)P->kbytes - 1) / (size_t)P->kbytes;
if (slen != nblocks * (POLAR_N / 8))
return -1;
long corr = 0;
for (size_t b = 0; b < nblocks; b++) {
uint8_t *db = (uint8_t *)malloc((size_t)P->kbytes);
if (!db)
return -1;
long bc = 0;
polar_decode_block(P, stored + b * (POLAR_N / 8), db, &bc);
corr += bc;
size_t dlen = orig_len - b * (size_t)P->kbytes < (size_t)P->kbytes
? orig_len - b * (size_t)P->kbytes
: (size_t)P->kbytes;
memcpy(out + b * (size_t)P->kbytes, db, dlen);
free(db);
}
if (corrected)
*corrected = corr;
return 0;
}
static void polar_destroy(ecc_codec_t *c)
{
polar_t *P = (polar_t *)c->priv;
free(P->llr);
free(P->ws);
free(P->bits);
free(P->psum);
free(P);
free((void *)c->name);
free(c);
}
ecc_codec_t *polar_new(int rate_pct, int design_exp)
{
int k;
const char *tag;
if (rate_pct == 90) {
k = 3680;
tag = "polar-r90";
} else if (rate_pct == 95) {
k = 3888;
tag = "polar-r95";
} else {
return NULL;
}
polar_t *P = (polar_t *)calloc(1, sizeof(*P));
if (!P)
return NULL;
P->k = k;
P->kbytes = k / 8;
P->p_design = pow(10.0, -(double)design_exp);
P->llr = (float *)malloc((size_t)POLAR_N * sizeof(float));
P->ws = (float *)malloc((size_t)POLAR_N * sizeof(float));
P->bits = (uint8_t *)malloc((size_t)POLAR_N);
P->psum = (uint8_t *)malloc((size_t)POLAR_N);
if (!P->llr || !P->ws || !P->bits || !P->psum) {
free(P->llr);
free(P->ws);
free(P->bits);
free(P->psum);
free(P);
return NULL;
}
polar_build_frozen(P);
ecc_codec_t *c = (ecc_codec_t *)calloc(1, sizeof(*c));
if (!c) {
free(P->llr);
free(P->ws);
free(P->bits);
free(P->psum);
free(P);
return NULL;
}
char *name = (char *)malloc(24);
if (!name) {
free(P->llr);
free(P->ws);
free(P->bits);
free(P->psum);
free(P);
free(c);
return NULL;
}
if (design_exp == 2)
snprintf(name, 24, "%s", tag);
else
snprintf(name, 24, "%s-d%d", tag, design_exp);
c->name = name;
c->desc = "polar code, successive-cancellation decoding";
c->block_data = (size_t)P->kbytes;
c->encode = polar_encode;
c->decode = polar_decode;
c->recover_verified = NULL;
c->destroy = polar_destroy;
c->priv = P;
return c;
}
+21
View File
@@ -0,0 +1,21 @@
#!/bin/bash
# run_matrix.sh - full corruption matrix for a list of codecs.
# Usage: run_matrix.sh <corpus> <blobsize> <trials> <outfile> <codec...>
# Each codec x setting is its own bench invocation; xargs -P spreads
# them over all cores. Pass 2 is OFF for the matrix (too slow at scale);
# rescue rates are measured separately by run_pass2.sh.
set -u
CORPUS="$1"; SIZE="$2"; TRIALS="$3"; OUT="$4"; shift 4
mkdir -p "$(dirname "$OUT")"
{
for CODEC in "$@"; do
for BER in 1e-5 1e-4 1e-3 3e-3 1e-2; do
echo "./bench --codec $CODEC --blob $CORPUS --size $SIZE --trials $TRIALS --ber $BER --nopass2 --out $OUT"
done
for B in "16,1" "64,1" "256,1" "1024,1" "64,4"; do
echo "./bench --codec $CODEC --blob $CORPUS --size $SIZE --trials $TRIALS --burst $B --nopass2 --out $OUT"
done
echo "./bench --codec $CODEC --blob $CORPUS --size $SIZE --trials $TRIALS --clean --out $OUT"
done
} | xargs -P "$(nproc)" -I CMD sh -c 'CMD 2>>results/matrix.log'
echo "matrix done: $OUT"
+18
View File
@@ -0,0 +1,18 @@
#!/bin/bash
# run_pass2.sh - BCH pass-2 rescue rates, small trial count (pass 2 is
# slow; 25 trials per setting is enough to see rescues).
# Usage: run_pass2.sh <corpus> <blobsize> <trials> <outfile> <codec...>
set -u
CORPUS="$1"; SIZE="$2"; TRIALS="$3"; OUT="$4"; shift 4
mkdir -p "$(dirname "$OUT")"
{
for CODEC in "$@"; do
for BER in 1e-4 1e-3 3e-3; do
echo "./bench --codec $CODEC --blob $CORPUS --size $SIZE --trials $TRIALS --ber $BER --out $OUT"
done
for B in "64,1" "256,1"; do
echo "./bench --codec $CODEC --blob $CORPUS --size $SIZE --trials $TRIALS --burst $B --out $OUT"
done
done
} | xargs -P "$(nproc)" -I CMD sh -c 'CMD 2>>results/pass2.log'
echo "pass2 done: $OUT"
+177
View File
@@ -0,0 +1,177 @@
/* uep.c - unequal error protection over small-block BCH.
*
* The blob is split into a critical prefix (the first C bytes -- the
* lzip header and the start of the LZMA stream, where an error costs
* the most) protected at BCH t_hi, and the remainder at t_lo, both
* with m=13 small blocks. Stored = prefix_stored + rest_stored; the
* split point is recomputed from C and orig_len, so no per-entry
* metadata is needed beyond the codec parameters.
*
* Two things to know before judging it:
* - Exact recovery is dominated by the WEAK tier: UEP cannot beat
* uniform BCH at the same total overhead on whole-blob recovery,
* because the weak tier fails first. The fair comparison is
* t_lo == the uniform baseline t, with t_hi > t_lo costing only
* a little extra overhead (the prefix is short).
* - The win, if any, is graceful degradation: the prefix survives
* when the rest does not. That only helps lzply if it gains a
* salvage mode (today the entry SHA gates all-or-nothing
* extraction), so prefix survival is reported separately by
* uep_diag.c, not by bench.c.
* Pass 2 is not wired: the recovery oracle is the whole-blob SHA,
* which cannot verify a prefix candidate on its own.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ecc_iface.h"
#include "bch_wrap.h"
#include "uep.h"
typedef struct {
ecc_codec_t *hi; /* bch1k-t{t_hi} */
ecc_codec_t *lo; /* bch1k-t{t_lo} */
int crit_bytes;
int t_hi, t_lo;
} uep_priv_t;
/* Max data bytes per block under small-block BCH at t (pinned m=13
poly, N=8191): floor((8191 - 13*t)/8). NOTE: this depends on t --
971 for t=32 but 1010 for t=8. An earlier version hardcoded 971
and broke every tier but t=32. */
static size_t bch1k_max_data(int t)
{
return (8191 - (size_t)13 * (size_t)t) / 8;
}
/* Stored length of dlen data bytes under small-block BCH at t:
blocks of bch1k_max_data(t) bytes, ceil((8*dlen + t*13)/8) stored
each. (Mirrors bch_block_stored_len for the pinned m=13 poly.) */
static size_t bch1k_stored_len(size_t dlen, int t)
{
size_t maxd = bch1k_max_data(t);
size_t total = 0;
size_t r = (size_t)t * 13;
while (dlen > 0) {
size_t b = dlen < maxd ? dlen : maxd;
total += (8 * b + r + 7) / 8;
dlen -= b;
}
return total;
}
static size_t uep_encode(ecc_codec_t *c, const uint8_t *data, size_t len,
uint8_t **out)
{
uep_priv_t *p = (uep_priv_t *)c->priv;
size_t clen = len < (size_t)p->crit_bytes ? len : (size_t)p->crit_bytes;
uint8_t *hi = NULL, *lo = NULL;
size_t hi_len = p->hi->encode(p->hi, data, clen, &hi);
size_t lo_len = 0;
if (hi_len)
lo_len = p->lo->encode(p->lo, data + clen, len - clen, &lo);
if (!hi_len || (len > clen && !lo_len)) {
free(hi);
free(lo);
return 0;
}
uint8_t *buf = (uint8_t *)malloc(hi_len + lo_len);
if (!buf) {
free(hi);
free(lo);
return 0;
}
memcpy(buf, hi, hi_len);
memcpy(buf + hi_len, lo, lo_len);
free(hi);
free(lo);
c->overhead = len ? (double)(hi_len + lo_len) / (double)len - 1.0 : 0.0;
*out = buf;
return hi_len + lo_len;
}
int uep_decode_parts(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out)
{
uep_priv_t *p = (uep_priv_t *)c->priv;
size_t clen = orig_len < (size_t)p->crit_bytes
? orig_len
: (size_t)p->crit_bytes;
size_t hi_len = bch1k_stored_len(clen, p->t_hi);
size_t lo_len = bch1k_stored_len(orig_len - clen, p->t_lo);
if (hi_len + lo_len != slen)
return 0;
int mask = 0;
long corr = -1;
if (p->hi->decode(p->hi, stored, hi_len, clen, out, &corr) == 0)
mask |= 1;
if (p->lo->decode(p->lo, stored + hi_len, lo_len, orig_len - clen,
out + clen, &corr) == 0)
mask |= 2;
return mask;
}
static int uep_decode(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out, long *corrected)
{
(void)corrected;
return uep_decode_parts(c, stored, slen, orig_len, out) == 3 ? 0 : -1;
}
static void uep_destroy(ecc_codec_t *c)
{
uep_priv_t *p = (uep_priv_t *)c->priv;
if (p->hi)
p->hi->destroy(p->hi);
if (p->lo)
p->lo->destroy(p->lo);
free(p);
free((void *)c->name);
free(c);
}
ecc_codec_t *uep_new(int crit_bytes, int t_hi, int t_lo)
{
ecc_codec_t *hi = bch_1k_new(t_hi);
ecc_codec_t *lo = bch_1k_new(t_lo);
if (!hi || !lo) {
if (hi)
hi->destroy(hi);
if (lo)
lo->destroy(lo);
return NULL;
}
ecc_codec_t *c = (ecc_codec_t *)calloc(1, sizeof(*c));
uep_priv_t *p = (uep_priv_t *)calloc(1, sizeof(*p));
if (!c || !p) {
free(c);
free(p);
hi->destroy(hi);
lo->destroy(lo);
return NULL;
}
char *name = (char *)malloc(32);
if (!name) {
free(c);
free(p);
hi->destroy(hi);
lo->destroy(lo);
return NULL;
}
snprintf(name, 32, "uep-c%d-h%d-l%d", crit_bytes, t_hi, t_lo);
p->hi = hi;
p->lo = lo;
p->crit_bytes = crit_bytes;
p->t_hi = t_hi;
p->t_lo = t_lo;
c->name = name;
c->desc = "unequal error protection over small-block BCH";
c->block_data = bch1k_max_data(t_lo); /* bulk tier's block size */
c->encode = uep_encode;
c->decode = uep_decode;
c->recover_verified = NULL;
c->destroy = uep_destroy;
c->priv = p;
return c;
}
+14
View File
@@ -0,0 +1,14 @@
/* uep.h - UEP codec plus the part-wise decode helper used by uep_diag. */
#ifndef EXP_UEP_H
#define EXP_UEP_H
#include "ecc_iface.h"
ecc_codec_t *uep_new(int crit_bytes, int t_hi, int t_lo);
/* Decode and report per-part success: bit 1 = critical prefix decoded,
bit 2 = remainder decoded. Returns the bitmask (0..3). */
int uep_decode_parts(ecc_codec_t *c, const uint8_t *stored, size_t slen,
size_t orig_len, uint8_t *out);
#endif /* EXP_UEP_H */
+125
View File
@@ -0,0 +1,125 @@
/* uep_diag.c - diagnostic for the UEP codec (NOT part of bench).
* Reports BOTH whole-blob exact recovery AND critical-prefix survival
* per corruption setting, so the graceful-degradation claim can be
* judged: a prefix that survives while the rest fails is only useful
* with a salvage mode, but it is the metric UEP exists for.
* Usage: uep_diag <codec> <blob> <size> <trials> (--ber X | --burst L,C)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "ecc_iface.h"
#include "uep.h"
static uint64_t rs = 0x13579bdf2468aceULL;
static inline uint64_t xr(void)
{
rs ^= rs << 13;
rs ^= rs >> 7;
rs ^= rs << 17;
return rs;
}
int main(int argc, char **argv)
{
if (argc < 6) {
fprintf(stderr,
"usage: uep_diag <codec> <blob> <size> <trials> "
"(--ber X | --burst L,C)\n");
return 2;
}
const char *codec_name = argv[1];
const char *blob_path = argv[2];
size_t win_size = (size_t)strtoull(argv[3], NULL, 0);
long trials = strtol(argv[4], NULL, 0);
double ber = 0;
long burst_len = 0, burst_count = 0;
int is_burst = 0;
if (!strcmp(argv[5], "--ber") && argc > 6)
ber = strtod(argv[6], NULL);
else if (!strcmp(argv[5], "--burst") && argc > 6) {
is_burst = 1;
burst_len = strtol(argv[6], NULL, 0);
char *comma = strchr(argv[6], ',');
burst_count = comma ? strtol(comma + 1, NULL, 0) : 1;
} else {
fprintf(stderr, "uep_diag: need --ber X or --burst L,C\n");
return 2;
}
FILE *f = fopen(blob_path, "rb");
if (!f) {
fprintf(stderr, "uep_diag: cannot open %s\n", blob_path);
return 2;
}
fseek(f, 0, SEEK_END);
long file_len = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *file = (uint8_t *)malloc((size_t)file_len);
if (!file || fread(file, 1, (size_t)file_len, f) != (size_t)file_len)
return 2;
fclose(f);
ecc_codec_t *c = NULL;
/* parse the codec name the same way bench does */
int cb, th, tl;
if (sscanf(codec_name, "uep-c%d-h%d-l%d", &cb, &th, &tl) != 3) {
fprintf(stderr, "uep_diag: bad codec %s\n", codec_name);
return 2;
}
c = uep_new(cb, th, tl);
if (!c) {
fprintf(stderr, "uep_diag: uep_new failed\n");
return 2;
}
uint8_t *win = (uint8_t *)malloc(win_size);
uint8_t *out = (uint8_t *)malloc(win_size);
uint8_t *stored = NULL, *rx = NULL;
long whole = 0, prefix = 0;
uint64_t ber_thr = (uint64_t)(ber * 18446744073709551616.0);
for (long tr = 0; tr < trials; tr++) {
size_t off = (size_t)(xr() % ((size_t)file_len - win_size + 1));
memcpy(win, file + off, win_size);
size_t slen = c->encode(c, win, win_size, &stored);
if (!slen) {
fprintf(stderr, "uep_diag: encode failed\n");
return 2;
}
if (tr == 0) {
rx = (uint8_t *)malloc(slen);
if (!rx)
return 2;
}
memcpy(rx, stored, slen);
if (is_burst) {
for (long b = 0; b < burst_count; b++) {
if ((size_t)burst_len >= slen)
continue;
size_t start =
(size_t)(xr() % (slen - (size_t)burst_len + 1));
for (long k = 0; k < burst_len; k++)
rx[start + k] = (uint8_t)(xr() & 0xff);
}
} else {
for (size_t i = 0; i < slen; i++) {
uint8_t m = 0;
for (int b = 0; b < 8; b++)
if (xr() < ber_thr)
m |= (uint8_t)(1u << b);
rx[i] ^= m;
}
}
int mask = uep_decode_parts(c, rx, slen, win_size, out);
if (mask == 3 && memcmp(out, win, win_size) == 0)
whole++;
if (mask & 1)
prefix++;
free(stored);
stored = NULL;
}
printf("%s %s trials=%ld whole=%.4f prefix=%.4f overhead=%.2f%%\n",
codec_name, is_burst ? "burst" : "ber", trials,
(double)whole / (double)trials, (double)prefix / (double)trials,
c->overhead * 100.0);
return 0;
}