From 3338d6d1bc700fd7b819c1761efe1379ec739b86 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 21 Oct 2018 14:48:40 +1100 Subject: [PATCH] Write LZW output 8 bytes at a time, not 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The benchmarks are mixed, but I think it's still a net win. ---- name old speed new speed delta wuffs_gif_decode_1k_bw/clang 258MB/s ± 0% 253MB/s ± 1% -2.04% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/clang 159MB/s ± 0% 116MB/s ± 0% -26.76% (p=0.008 n=5+5) wuffs_gif_decode_10k/clang 167MB/s ± 0% 138MB/s ± 0% -17.42% (p=0.008 n=5+5) wuffs_gif_decode_100k_artificial/clang 205MB/s ± 0% 439MB/s ± 0% +114.53% (p=0.008 n=5+5) wuffs_gif_decode_100k_realistic/clang 151MB/s ± 0% 164MB/s ± 0% +8.41% (p=0.008 n=5+5) wuffs_gif_decode_1000k/clang 155MB/s ± 0% 165MB/s ± 0% +6.50% (p=0.008 n=5+5) wuffs_gif_decode_anim_screencap/clang 255MB/s ± 0% 921MB/s ± 0% +261.50% (p=0.008 n=5+5) wuffs_gif_decode_1k_bw/gcc 316MB/s ± 2% 267MB/s ± 1% -15.78% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/gcc 184MB/s ± 1% 134MB/s ± 0% -27.57% (p=0.008 n=5+5) wuffs_gif_decode_10k/gcc 196MB/s ± 0% 161MB/s ± 0% -17.50% (p=0.008 n=5+5) wuffs_gif_decode_100k_artificial/gcc 287MB/s ± 0% 449MB/s ± 1% +56.22% (p=0.008 n=5+5) wuffs_gif_decode_100k_realistic/gcc 182MB/s ± 0% 182MB/s ± 1% ~ (p=0.690 n=5+5) wuffs_gif_decode_1000k/gcc 187MB/s ± 1% 185MB/s ± 1% ~ (p=0.056 n=5+5) wuffs_gif_decode_anim_screencap/gcc 397MB/s ± 0% 973MB/s ± 0% +145.42% (p=0.008 n=5+5) wuffs_lzw_decode_20k/clang 170MB/s ± 0% 191MB/s ± 1% +12.52% (p=0.008 n=5+5) wuffs_lzw_decode_100k/clang 242MB/s ± 0% 312MB/s ± 2% +29.00% (p=0.008 n=5+5) wuffs_lzw_decode_20k/gcc 220MB/s ± 0% 227MB/s ± 0% +3.31% (p=0.008 n=5+5) wuffs_lzw_decode_100k/gcc 317MB/s ± 0% 369MB/s ± 1% +16.53% (p=0.008 n=5+5) ---- LZW is the compression method used by GIF images. Roughly speaking, LZW is a sequence of codes, and each code emits either 1 byte (a literal code) or multiple bytes (a copy code). The speed gain in this commit comes from, when emitting an N-byte copy code, this now requires N/8 iterations instead of N iterations, as each iteration now writes 8 bytes instead of 1. Literal codes, both before and after this commit, requires a single iteration. Decoding should therefore get faster or slower depending on whether it's copy-code-heavy or literal-code-heavy respectively. For what it's worth, copy-code-heavy GIF images also usually compress better. Running script/extract-giflzw.go (or examining the cached output at test/data/analysis/giflzw-emission-lengths.txt) on the anim_screencap (which got faster) and 1k_color (which got slower) cases (also known as gifplayer-muybridge.gif and hippopotamus.regular.gif) shows that the proportion of literal codes agrees with that explanation. ---- gifplayer-muybridge.gif (all frames) Percent Count Emission_Length_Bucket 12.78 34658 1 (Literal Code) 14.05 38106 2 13.25 35937 3 10.59 28707 4 7.85 21286 5 6.08 16481 6 4.85 13156 7 3.60 9764 8 9.34 25318 9 .. 16 4.89 13257 17 .. 32 4.78 12954 33 .. 64 4.72 12806 65 .. 128 3.02 8190 127 .. 256 0.20 550 256+ hippopotamus.regular.gif (all frames) Percent Count Emission_Length_Bucket 82.66 672 1 (Literal Code) 13.04 106 2 2.83 23 3 0.86 7 4 0.37 3 5 0.25 2 6 0.00 0 7 0.00 0 8 0.00 0 9 .. 16 0.00 0 17 .. 32 0.00 0 33 .. 64 0.00 0 65 .. 128 0.00 0 127 .. 256 0.00 0 256+ ---- The new algorithm also adds more CPU and RAM overhead, regardless of whether the input GIF image's LZW data is copy-code-heavy or literal-code-heavy. For example, the suffix table grows by 8x, from 4KiB to 32 KiB. The gifplayer-muybridge.gif image is also larger (472 x 298) and the hippopotamus.regular.gif image is also smaller (36 x 28). Higher fixed costs may have larger (negative) relative effects on decoding smaller images. To repeat, I think it's still a net win. An upcoming commit will update the std/lzw/README.md file to provide more detail on the algorithm. Thanks to Tushar Khandelwal for the idea. --- cmd/wuffs-c/internal/cgen/builtin.go | 60 +++++++++++++++++- cmd/wuffs-c/internal/cgen/cgen.go | 7 +- cmd/wuffs-c/internal/cgen/statement.go | 57 +++++++++++------ lang/check/data.go | 61 ++++++++++++++++++ lang/check/gen.go | 14 ++++ release/c/wuffs-unsupported-snapshot.h | 88 ++++++++++++++++++++------ std/lzw/decode_lzw.wuffs | 72 ++++++++++++++------- 7 files changed, 292 insertions(+), 67 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/builtin.go b/cmd/wuffs-c/internal/cgen/builtin.go index ecca6675..65cb3c05 100644 --- a/cmd/wuffs-c/internal/cgen/builtin.go +++ b/cmd/wuffs-c/internal/cgen/builtin.go @@ -22,7 +22,10 @@ import ( t "github.com/google/wuffs/lang/token" ) -var errNoSuchBuiltin = errors.New("cgen: internal error: no such built-in") +var ( + errNoSuchBuiltin = errors.New("cgen: internal error: no such built-in") + errOptimizationNotApplicable = errors.New("cgen: internal error: optimization not applicable") +) func (g *gen) writeBuiltinCall(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32) error { if n.Operator() != t.IDOpenParen { @@ -323,6 +326,10 @@ func (g *gen) writeBuiltinNumType(b *buffer, recv *a.Expr, method t.ID, args []* func (g *gen) writeBuiltinSlice(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error { switch method { case t.IDCopyFromSlice: + if err := g.writeBuiltinSliceCopyFromSlice8(b, recv, method, args, rp, depth); err != errOptimizationNotApplicable { + return err + } + // TODO: don't assume that the slice is a slice of base.u8. b.writes("wuffs_base__slice_u8__copy_from_slice(") if err := g.writeExpr(b, recv, rp, depth); err != nil { @@ -351,6 +358,57 @@ func (g *gen) writeBuiltinSlice(b *buffer, recv *a.Expr, method t.ID, args []*a. return errNoSuchBuiltin } +// writeBuiltinSliceCopyFromSlice8 writes an optimized version of: +// +// foo[fIndex:fIndex + 8].copy_from_slice!(s:bar[bIndex:bIndex + 8]) +func (g *gen) writeBuiltinSliceCopyFromSlice8(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error { + if method != t.IDCopyFromSlice || len(args) != 1 { + return errOptimizationNotApplicable + } + foo, fIndex := matchFooIndexIndexPlus8(recv) + bar, bIndex := matchFooIndexIndexPlus8(args[0].AsArg().Value()) + if foo == nil || bar == nil { + return errOptimizationNotApplicable + } + b.writes("memcpy((") + if err := g.writeExpr(b, foo, rp, depth); err != nil { + return err + } + b.writes(")+(") + if err := g.writeExpr(b, fIndex, rp, depth); err != nil { + return err + } + b.writes("),(") + if err := g.writeExpr(b, bar, rp, depth); err != nil { + return err + } + b.writes(")+(") + if err := g.writeExpr(b, bIndex, rp, depth); err != nil { + return err + } + // TODO: don't assume that the slice is a slice of base.u8. + b.writes("), 8)") + return nil +} + +// matchFooIndexIndexPlus8 matches n with "foo[index:index + 8]". It returns +// nil values if there isn't a match. +func matchFooIndexIndexPlus8(n *a.Expr) (foo *a.Expr, index *a.Expr) { + if n.Operator() != t.IDColon { + return nil, nil + } + foo = n.LHS().AsExpr() + index = n.MHS().AsExpr() + rhs := n.RHS().AsExpr() + if index == nil || rhs == nil || rhs.Operator() != t.IDXBinaryPlus || !rhs.LHS().AsExpr().Eq(index) { + return nil, nil + } + if cv := rhs.RHS().AsExpr().ConstValue(); cv == nil || cv.Cmp(eight) != 0 { + return nil, nil + } + return foo, index +} + func (g *gen) writeBuiltinTable(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error { field := "" diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index 08c1ab43..e7143efd 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -40,8 +40,11 @@ import ( ) var ( - zero = big.NewInt(0) - one = big.NewInt(1) + zero = big.NewInt(0) + one = big.NewInt(1) + eight = big.NewInt(8) + + mibi = big.NewInt(1 << 20) ) // Prefixes are prepended to names to form a namespace and to avoid e.g. diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index a056ae30..0ce03f12 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -98,26 +98,43 @@ func (g *gen) writeStatementAssign(b *buffer, n *a.Assign, depth uint32) error { if err := g.writeSuspendibles(b, n.RHS(), depth); err != nil { return err } - opName, tilde := "", false + opName, closer := "", "" - op := n.Operator() - switch op { - case t.IDTildeSatPlusEq, t.IDTildeSatMinusEq: - uBits := uintBits(n.LHS().MType().QID()) - if uBits == 0 { - return fmt.Errorf("unsupported tilde-operator type %q", n.LHS().MType().Str(g.tm)) - } - uOp := "add" - if op != t.IDTildeSatPlusEq { - uOp = "sub" - } - b.printf("wuffs_base__u%d__sat_%s_indirect(&", uBits, uOp) - opName, tilde = ",", true + if lTyp := n.LHS().MType(); lTyp.IsNumType() || lTyp.IsBool() || lTyp.IsSliceType() { + op := n.Operator() + switch op { + case t.IDTildeSatPlusEq, t.IDTildeSatMinusEq: + uBits := uintBits(n.LHS().MType().QID()) + if uBits == 0 { + return fmt.Errorf("unsupported tilde-operator type %q", lTyp.Str(g.tm)) + } + uOp := "add" + if op != t.IDTildeSatPlusEq { + uOp = "sub" + } + b.printf("wuffs_base__u%d__sat_%s_indirect(&", uBits, uOp) + opName, closer = ",", ")" - default: - opName = cOpName(op) - if opName == "" { - return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) + default: + opName = cOpName(op) + if opName == "" { + return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) + } + } + } else { + if lTyp.IsArrayType() { + if nElem := lTyp.ArrayLength().ConstValue(); nElem.Sign() > 0 && nElem.Cmp(mibi) <= 0 { + if elemTyp := lTyp.Inner(); elemTyp.IsNumType() { + if nBits := uintBits(elemTyp.QID()); nBits != 0 { + b.writes("memcpy(") + opName, closer = ",", fmt.Sprintf(", %d)", nElem.Int64()*int64(nBits/8)) + } + } + } + } + + if closer == "" { + return fmt.Errorf("unsupported assignment type %q", lTyp.Str(g.tm)) } } @@ -128,9 +145,7 @@ func (g *gen) writeStatementAssign(b *buffer, n *a.Assign, depth uint32) error { if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, depth); err != nil { return err } - if tilde { - b.writeb(')') - } + b.writes(closer) b.writes(";\n") return nil } diff --git a/lang/check/data.go b/lang/check/data.go index e0b825d6..345a833a 100644 --- a/lang/check/data.go +++ b/lang/check/data.go @@ -35,6 +35,8 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryGreaterThan, xb, xa); err != nil { return err } + _ = xa + _ = xb return nil }}, @@ -55,6 +57,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessThan, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -75,6 +80,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryEqEq, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -95,6 +103,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessThan, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -115,6 +126,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -135,6 +149,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessThan, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -147,6 +164,8 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessThan, xb, xa); err != nil { return err } + _ = xa + _ = xb return nil }}, @@ -159,6 +178,8 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryGreaterEq, xb, xa); err != nil { return err } + _ = xa + _ = xb return nil }}, @@ -179,6 +200,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -199,6 +223,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryEqEq, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -219,6 +246,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xc, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -231,6 +261,8 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xb, xa); err != nil { return err } + _ = xa + _ = xb return nil }}, @@ -251,6 +283,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessEq, zeroExpr, xb); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, @@ -284,6 +319,29 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xc0, xc); err != nil { return err } + _ = xa + _ = xb + _ = xb0 + _ = xc + _ = xc0 + return nil + }}, + + {`"a <= (a + b): 0 <= b"`, func(q *checker, n *a.Assert) error { + op, xa, t0 := parseBinaryOp(n.Condition()) + if op != t.IDXBinaryLessEq { + return errFailed + } + op, xa, xb := parseBinaryOp(t0) + if op != t.IDXBinaryPlus { + return errFailed + } + // 0 <= b + if err := proveReasonRequirement(q, t.IDXBinaryLessEq, zeroExpr, xb); err != nil { + return err + } + _ = xa + _ = xb return nil }}, @@ -301,6 +359,9 @@ var reasons = [...]struct { if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xa, t1); err != nil { return err } + _ = xa + _ = xb + _ = xc return nil }}, } diff --git a/lang/check/gen.go b/lang/check/gen.go index c8ee9a38..bf8a28c6 100644 --- a/lang/check/gen.go +++ b/lang/check/gen.go @@ -26,6 +26,7 @@ import ( "go/format" "io/ioutil" "os" + "sort" "strings" ) @@ -49,6 +50,8 @@ var reasons = [...]string{ "a < (b + c): a < c; 0 <= b", "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c", + "a <= (a + b): 0 <= b", + "(a + b) <= c: a <= (c - b)", } @@ -109,6 +112,17 @@ func gen(reason string) error { } } + { + namesAsList := []string(nil) + for name := range names { + namesAsList = append(namesAsList, name) + } + sort.Strings(namesAsList) + for _, name := range namesAsList { + fmt.Fprintf(&out, "_ = x%s\n", name) + } + } + fmt.Fprintf(&out, "return nil }},\n") return nil } diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index cabd348e..f9f0287d 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2569,10 +2569,10 @@ struct wuffs_lzw__decoder__struct { uint32_t f_literal_width; uint32_t f_flush_j; - uint8_t f_suffixes[4096]; + uint8_t f_suffixes[4096][8]; uint16_t f_prefixes[4096]; uint16_t f_prefix_lengths[4096]; - uint8_t f_output[8192]; + uint8_t f_output[8199]; struct { uint32_t coro_susp_point; @@ -2586,8 +2586,12 @@ struct wuffs_lzw__decoder__struct { uint32_t v_bits; uint32_t v_n_bits; uint32_t v_code; + uint16_t v_lm1_a; uint32_t v_c; uint32_t v_o; + uint32_t v_steps; + uint8_t v_first_byte; + uint16_t v_lm1_b; } c_decode[1]; struct { uint32_t coro_susp_point; @@ -9507,8 +9511,12 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, uint32_t v_bits; uint32_t v_n_bits; uint32_t v_code; + uint16_t v_lm1_a; uint32_t v_c; uint32_t v_o; + uint32_t v_steps; + uint8_t v_first_byte; + uint16_t v_lm1_b; uint8_t* iop_a_src = NULL; uint8_t* io0_a_src = NULL; @@ -9539,8 +9547,12 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, v_bits = self->private_impl.c_decode[0].v_bits; v_n_bits = self->private_impl.c_decode[0].v_n_bits; v_code = self->private_impl.c_decode[0].v_code; + v_lm1_a = self->private_impl.c_decode[0].v_lm1_a; v_c = self->private_impl.c_decode[0].v_c; v_o = self->private_impl.c_decode[0].v_o; + v_steps = self->private_impl.c_decode[0].v_steps; + v_first_byte = self->private_impl.c_decode[0].v_first_byte; + v_lm1_b = self->private_impl.c_decode[0].v_lm1_b; } else { } switch (coro_susp_point) { @@ -9553,11 +9565,12 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, v_clear_code = (((uint32_t)(1)) << v_literal_width); v_end_code = (v_clear_code + 1); v_save_code = v_end_code; - v_prev_code = 0; + v_prev_code = v_end_code; v_width = (v_literal_width + 1); v_j = 0; while (v_j < v_clear_code) { self->private_impl.f_prefix_lengths[v_j] = 0; + self->private_impl.f_suffixes[v_j][0] = ((uint8_t)(v_j)); v_j += 1; } v_j = 0; @@ -9604,11 +9617,21 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, self->private_impl.f_output[v_j] = ((uint8_t)(v_code)); v_j = ((v_j + 1) & 8191); if (v_save_code <= 4095) { - self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_code)); - self->private_impl.f_prefixes[v_save_code] = - ((uint16_t)(v_prev_code)); - self->private_impl.f_prefix_lengths[v_save_code] = + v_lm1_a = ((self->private_impl.f_prefix_lengths[v_prev_code] + 1) & 4095); + self->private_impl.f_prefix_lengths[v_save_code] = v_lm1_a; + if ((v_lm1_a % 8) != 0) { + self->private_impl.f_prefixes[v_save_code] = + self->private_impl.f_prefixes[v_prev_code]; + memcpy(self->private_impl.f_suffixes[v_save_code], + self->private_impl.f_suffixes[v_prev_code], 8); + self->private_impl.f_suffixes[v_save_code][(v_lm1_a % 8)] = + ((uint8_t)(v_code)); + } else { + self->private_impl.f_prefixes[v_save_code] = + ((uint16_t)(v_prev_code)); + self->private_impl.f_suffixes[v_save_code][0] = ((uint8_t)(v_code)); + } v_save_code += 1; if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) { v_width += 1; @@ -9620,32 +9643,53 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, goto label_1_break; } v_save_code = v_end_code; - v_prev_code = 0; + v_prev_code = v_end_code; v_width = (v_literal_width + 1); } else if (v_code <= v_save_code) { v_c = v_code; if (v_code == v_save_code) { v_c = v_prev_code; } - v_o = ((v_j + ((uint32_t)(self->private_impl.f_prefix_lengths[v_c]))) & + v_o = ((v_j + (((uint32_t)(self->private_impl.f_prefix_lengths[v_c])) & + 4294967288)) & 8191); - v_j = ((v_o + 1) & 8191); - while (v_c >= v_clear_code) { - self->private_impl.f_output[v_o] = self->private_impl.f_suffixes[v_c]; - v_o = ((v_o - 1) & 8191); + v_j = ((v_j + 1 + + ((uint32_t)(self->private_impl.f_prefix_lengths[v_c]))) & + 8191); + v_steps = (((uint32_t)(self->private_impl.f_prefix_lengths[v_c])) >> 3); + while (true) { + memcpy((self->private_impl.f_output) + (v_o), + (self->private_impl.f_suffixes[v_c]) + (0), 8); + if (v_steps <= 0) { + goto label_2_break; + } + v_steps -= 1; + v_o = ((v_o - 8) & 8191); v_c = ((uint32_t)(self->private_impl.f_prefixes[v_c])); } - self->private_impl.f_output[v_o] = ((uint8_t)(v_c)); + label_2_break:; + v_first_byte = self->private_impl.f_suffixes[v_c][0]; if (v_code == v_save_code) { - self->private_impl.f_output[v_j] = ((uint8_t)(v_c)); + self->private_impl.f_output[v_j] = v_first_byte; v_j = ((v_j + 1) & 8191); } if (v_save_code <= 4095) { - self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_c)); - self->private_impl.f_prefixes[v_save_code] = - ((uint16_t)(v_prev_code)); - self->private_impl.f_prefix_lengths[v_save_code] = + v_lm1_b = ((self->private_impl.f_prefix_lengths[v_prev_code] + 1) & 4095); + self->private_impl.f_prefix_lengths[v_save_code] = v_lm1_b; + if ((v_lm1_b % 8) != 0) { + self->private_impl.f_prefixes[v_save_code] = + self->private_impl.f_prefixes[v_prev_code]; + memcpy(self->private_impl.f_suffixes[v_save_code], + self->private_impl.f_suffixes[v_prev_code], 8); + self->private_impl.f_suffixes[v_save_code][(v_lm1_b % 8)] = + v_first_byte; + } else { + self->private_impl.f_prefixes[v_save_code] = + ((uint16_t)(v_prev_code)); + self->private_impl.f_suffixes[v_save_code][0] = + ((uint8_t)(v_first_byte)); + } v_save_code += 1; if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) { v_width += 1; @@ -9715,8 +9759,12 @@ suspend: self->private_impl.c_decode[0].v_bits = v_bits; self->private_impl.c_decode[0].v_n_bits = v_n_bits; self->private_impl.c_decode[0].v_code = v_code; + self->private_impl.c_decode[0].v_lm1_a = v_lm1_a; self->private_impl.c_decode[0].v_c = v_c; self->private_impl.c_decode[0].v_o = v_o; + self->private_impl.c_decode[0].v_steps = v_steps; + self->private_impl.c_decode[0].v_first_byte = v_first_byte; + self->private_impl.c_decode[0].v_lm1_b = v_lm1_b; goto exit; exit: @@ -9785,7 +9833,7 @@ wuffs_lzw__decoder__flush(wuffs_lzw__decoder* self, v_s = wuffs_base__slice_u8__subslice_ij( ((wuffs_base__slice_u8){ .ptr = self->private_impl.f_output, - .len = 8192, + .len = 8199, }), v_i, v_j); v_n = wuffs_base__io_writer__copy_from_slice(&iop_a_dst, io1_a_dst, v_s); diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index 476c84e8..52ee11c6 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -19,10 +19,10 @@ pri status "?internal error: inconsistent I/O" pub struct decoder?( literal_width base.u32[..8], flush_j base.u32[..8191], - suffixes array[4096] base.u8, + suffixes array[4096] array[8] base.u8, prefixes array[4096] base.u16[..4095], - prefix_lengths array[4096] base.u16[..4095], - output array[8192] base.u8, + prefix_lengths array[4096] base.u16[..4095], // TODO: rename this field. + output array[8192 + 7] base.u8, ) pub func decoder.set_literal_width!(lw base.u32[2..8]) { @@ -44,7 +44,7 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { // next back-reference expands to. The README.md file also calls this value // `max`. 4096 means do not save. var save_code base.u32[..4096] = end_code - var prev_code base.u32[..4095] + var prev_code base.u32[..4095] = end_code var width base.u32[..12] = literal_width + 1 // First, use the j variable to iterate over prefix_lengths. @@ -52,6 +52,7 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { while j < clear_code { assert j < 256 via "a < b: a < c; c <= b"(c:clear_code) this.prefix_lengths[j] = 0 + this.suffixes[j][0] = j as base.u8 j += 1 } // For the rest of this function, j is such that output[:j] accumulates the @@ -109,9 +110,18 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { this.output[j] = code as base.u8 j = (j + 1) & 8191 if save_code <= 4095 { - this.suffixes[save_code] = code as base.u8 - this.prefixes[save_code] = prev_code as base.u16 - this.prefix_lengths[save_code] = (this.prefix_lengths[prev_code] + 1) & 4095 + var lm1_a base.u16[..4095] = (this.prefix_lengths[prev_code] + 1) & 4095 + this.prefix_lengths[save_code] = lm1_a + + if (lm1_a % 8) != 0 { + this.prefixes[save_code] = this.prefixes[prev_code] + this.suffixes[save_code] = this.suffixes[prev_code] + this.suffixes[save_code][lm1_a % 8] = code as base.u8 + } else { + this.prefixes[save_code] = prev_code as base.u16 + this.suffixes[save_code][0] = code as base.u8 + } + save_code += 1 if (save_code == ((1 as base.u32) << width)) and (width < 12) { width += 1 @@ -124,8 +134,9 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { break } save_code = end_code - prev_code = 0 + prev_code = end_code width = literal_width + 1 + } else if code <= save_code { var c base.u32[..4095] = code if code == save_code { @@ -134,35 +145,50 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { // Letting old_j and new_j denote the values of j before and after // these two lines of code, the decoded bytes will be written to - // output[old_j:new_j]. They will be written back-to-front, - // starting by writing output[o], also known as output[new_j - 1]. + // output[old_j:new_j]. They will be written back-to-front, 8 bytes + // at a time, starting by writing output[o:o + 8], which will + // contain output[new_j - 1]. // // In the special case that code == save_code, the decoded bytes // contain an extra copy (at the end) of the first byte, and will // be written to output[old_j:new_j + 1]. - var o base.u32[..8191] = (j + (this.prefix_lengths[c] as base.u32)) & 8191 - j = (o + 1) & 8191 + var o base.u32[..8191] = (j + ((this.prefix_lengths[c] as base.u32) & 0xFFFFFFF8)) & 8191 + j = (j + 1 + (this.prefix_lengths[c] as base.u32)) & 8191 - while c >= clear_code, - post c < 256 via "a < b: a < c; c <= b"(c:clear_code), - { - this.output[o] = this.suffixes[c] - // This line is essentially "o -= 1". The "& 8191" is a no-op + var steps base.u32 = (this.prefix_lengths[c] as base.u32) >> 3 + while true { + assert o <= (o + 8) via "a <= (a + b): 0 <= b"(b:8) + this.output[o:o + 8].copy_from_slice!(s:this.suffixes[c][0:0 + 8]) + if steps <= 0 { + break + } + steps -= 1 + + // This line is essentially "o -= 8". The "& 8191" is a no-op // in practice, but is necessary for the overflow checker. - o = (o ~mod- 1) & 8191 + o = (o ~mod- 8) & 8191 c = this.prefixes[c] as base.u32 } - this.output[o] = c as base.u8 + var first_byte base.u8 = this.suffixes[c][0] if code == save_code { - this.output[j] = c as base.u8 + this.output[j] = first_byte j = (j + 1) & 8191 } if save_code <= 4095 { - this.suffixes[save_code] = c as base.u8 - this.prefixes[save_code] = prev_code as base.u16 - this.prefix_lengths[save_code] = (this.prefix_lengths[prev_code] + 1) & 4095 + var lm1_b base.u16[..4095] = (this.prefix_lengths[prev_code] + 1) & 4095 + this.prefix_lengths[save_code] = lm1_b + + if (lm1_b % 8) != 0 { + this.prefixes[save_code] = this.prefixes[prev_code] + this.suffixes[save_code] = this.suffixes[prev_code] + this.suffixes[save_code][lm1_b % 8] = first_byte + } else { + this.prefixes[save_code] = prev_code as base.u16 + this.suffixes[save_code][0] = first_byte as base.u8 + } + save_code += 1 if (save_code == ((1 as base.u32) << width)) and (width < 12) { width += 1