Factor lzw's decode into read and write halves

name                                    old speed      new speed      delta

wuffs_lzw_decode_20k/clang               203MB/s ± 3%   240MB/s ± 1%  +17.93%  (p=0.000 n=49+5)
wuffs_lzw_decode_100k/clang              334MB/s ± 4%   413MB/s ± 0%  +23.75%  (p=0.000 n=50+5)

wuffs_lzw_decode_20k/gcc                 235MB/s ± 2%   254MB/s ± 2%   +8.40%  (p=0.000 n=45+5)
wuffs_lzw_decode_100k/gcc                395MB/s ± 2%   455MB/s ± 2%  +15.06%  (p=0.000 n=45+5)

wuffs_gif_decode_1k_bw/clang             229MB/s ± 1%   240MB/s ± 0%   +4.51%  (p=0.000 n=49+5)
wuffs_gif_decode_1k_color/clang          120MB/s ± 1%   134MB/s ± 1%  +11.44%  (p=0.000 n=46+5)
wuffs_gif_decode_10k_bgra/clang          557MB/s ± 1%   649MB/s ± 0%  +16.56%  (p=0.000 n=47+5)
wuffs_gif_decode_10k_indexed/clang       149MB/s ± 1%   174MB/s ± 1%  +17.11%  (p=0.000 n=48+5)
wuffs_gif_decode_100k_artificial/clang   459MB/s ± 1%   514MB/s ± 1%  +11.95%  (p=0.000 n=47+5)
wuffs_gif_decode_100k_realistic/clang    175MB/s ± 1%   204MB/s ± 0%  +16.80%  (p=0.000 n=47+5)
wuffs_gif_decode_1000k/clang             178MB/s ± 1%   207MB/s ± 0%  +16.21%  (p=0.000 n=46+5)
wuffs_gif_decode_anim_screencap/clang    961MB/s ± 1%  1060MB/s ± 1%  +10.35%  (p=0.000 n=46+5)

wuffs_gif_decode_1k_bw/gcc               244MB/s ± 1%   261MB/s ± 0%   +6.86%  (p=0.000 n=47+5)
wuffs_gif_decode_1k_color/gcc            133MB/s ± 1%   145MB/s ± 0%   +9.44%  (p=0.000 n=48+5)
wuffs_gif_decode_10k_bgra/gcc            543MB/s ± 2%   593MB/s ± 1%   +9.22%  (p=0.000 n=49+5)
wuffs_gif_decode_10k_indexed/gcc         166MB/s ± 1%   185MB/s ± 0%  +11.55%  (p=0.000 n=47+5)
wuffs_gif_decode_100k_artificial/gcc     464MB/s ± 1%   512MB/s ± 0%  +10.51%  (p=0.000 n=47+5)
wuffs_gif_decode_100k_realistic/gcc      190MB/s ± 1%   211MB/s ± 0%  +11.05%  (p=0.000 n=47+5)
wuffs_gif_decode_1000k/gcc               193MB/s ± 1%   215MB/s ± 0%  +11.54%  (p=0.000 n=44+5)
wuffs_gif_decode_anim_screencap/gcc     1.00GB/s ± 1%  1.07GB/s ± 0%   +7.76%  (p=0.000 n=45+5)
This commit is contained in:
Nigel Tao committed 2018-12-26 11:08:37 +11:00
1 parent c2a48ae6d3
commit c2826e87e3
3 files changed
+372 -331

No files matched your search

+15 -1
View File
@@ -263,7 +263,10 @@ func (g *gen) writeFuncImplPrologue(b *buffer) error {
}
b.writes("\n")
if g.currFunk.astFunc.Effect().Optional() {
if g.currFunk.astFunc.Effect().Optional() ||
// TODO: delete this lzw.read_from hack.
((g.pkgName == "lzw") && (g.currFunk.astFunc.FuncName().Str(g.tm) == "read_from")) {
g.findDerivedVars()
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.AsField()
@@ -331,6 +334,17 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error {
}
func (g *gen) writeFuncImplEpilogue(b *buffer) error {
// TODO: delete this lzw.read_from hack.
if (g.pkgName == "lzw") && (g.currFunk.astFunc.FuncName().Str(g.tm) == "read_from") {
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.AsField()
if err := g.writeSaveDerivedVar(b, "", aPrefix, o.Name(), o.XType()); err != nil {
return err
}
}
b.writes("\n")
}
if g.currFunk.astFunc.Effect().Optional() {
b.writes("goto exit;exit:") // The goto avoids the "unused label" warning.
+240 -242
View File
@@ -2613,8 +2613,17 @@ struct wuffs_lzw__decoder__struct {
struct {
uint32_t magic;
uint32_t f_lw;
uint32_t f_literal_width;
uint32_t f_clear_code;
uint32_t f_end_code;
uint32_t f_save_code;
uint32_t f_prev_code;
uint32_t f_width;
uint32_t f_bits;
uint32_t f_n_bits;
uint32_t f_flush_j;
uint32_t f_read_from_return_value;
uint8_t f_suffixes[4096][8];
uint16_t f_prefixes[4096];
uint16_t f_lm1s[4096];
@@ -2622,21 +2631,11 @@ struct wuffs_lzw__decoder__struct {
struct {
uint32_t coro_susp_point;
uint32_t v_literal_width;
uint32_t v_clear_code;
uint32_t v_end_code;
uint32_t v_save_code;
uint32_t v_prev_code;
uint32_t v_width;
bool v_bad;
uint32_t v_j;
uint32_t v_bits;
uint32_t v_n_bits;
} c_decode[1];
struct {
uint32_t coro_susp_point;
uint32_t v_i;
} c_auto_flush[1];
} c_write_to[1];
} private_impl;
#ifdef __cplusplus
@@ -7076,9 +7075,13 @@ const char* wuffs_lzw__error__internal_error_inconsistent_i_o =
// ---------------- Private Function Prototypes
static wuffs_base__empty_struct //
wuffs_lzw__decoder__read_from(wuffs_lzw__decoder* self,
wuffs_base__io_reader a_src);
static wuffs_base__status //
wuffs_lzw__decoder__auto_flush(wuffs_lzw__decoder* self,
wuffs_base__io_writer a_dst);
wuffs_lzw__decoder__write_to(wuffs_lzw__decoder* self,
wuffs_base__io_writer a_dst);
// ---------------- Initializer Implementations
@@ -7125,7 +7128,7 @@ wuffs_lzw__decoder__set_literal_width(wuffs_lzw__decoder* self, uint32_t a_lw) {
return ((wuffs_base__empty_struct){});
}
self->private_impl.f_literal_width = a_lw;
self->private_impl.f_lw = a_lw;
return ((wuffs_base__empty_struct){});
}
@@ -7145,16 +7148,93 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self,
}
wuffs_base__status status = NULL;
uint32_t v_literal_width;
uint32_t v_i;
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
if (coro_susp_point) {
v_i = 0;
} else {
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
self->private_impl.f_literal_width = 8;
if (self->private_impl.f_lw >= 2) {
self->private_impl.f_literal_width = self->private_impl.f_lw;
}
self->private_impl.f_clear_code =
(((uint32_t)(1)) << self->private_impl.f_literal_width);
self->private_impl.f_end_code = (self->private_impl.f_clear_code + 1);
self->private_impl.f_save_code = self->private_impl.f_end_code;
self->private_impl.f_prev_code = self->private_impl.f_end_code;
self->private_impl.f_width = (self->private_impl.f_literal_width + 1);
self->private_impl.f_bits = 0;
self->private_impl.f_n_bits = 0;
self->private_impl.f_flush_j = 0;
v_i = 0;
while (v_i < self->private_impl.f_clear_code) {
self->private_impl.f_lm1s[v_i] = 0;
self->private_impl.f_suffixes[v_i][0] = ((uint8_t)(v_i));
v_i += 1;
}
label_0_continue:;
while (true) {
wuffs_lzw__decoder__read_from(self, a_src);
if (self->private_impl.f_flush_j > 0) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_lzw__decoder__write_to(self, a_dst);
if (status) {
goto suspend;
}
}
if (self->private_impl.f_read_from_return_value == 0) {
goto label_0_break;
} else if (self->private_impl.f_read_from_return_value == 1) {
goto label_0_continue;
} else if (self->private_impl.f_read_from_return_value == 2) {
status = wuffs_base__suspension__short_read;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
} else if (self->private_impl.f_read_from_return_value == 3) {
status = wuffs_lzw__error__bad_code;
goto exit;
} else {
status = wuffs_lzw__error__internal_error_inconsistent_i_o;
goto exit;
}
}
label_0_break:;
goto ok;
ok:
self->private_impl.c_decode[0].coro_susp_point = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
goto exit;
exit:
if (wuffs_base__status__is_error(status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func lzw.decoder.read_from
static wuffs_base__empty_struct //
wuffs_lzw__decoder__read_from(wuffs_lzw__decoder* self,
wuffs_base__io_reader a_src) {
uint32_t v_clear_code;
uint32_t v_end_code;
uint32_t v_save_code;
uint32_t v_prev_code;
uint32_t v_width;
bool v_bad;
uint32_t v_j;
uint32_t v_bits;
uint32_t v_n_bits;
uint32_t v_j;
uint32_t v_code;
uint32_t v_c;
uint32_t v_o;
@@ -7180,253 +7260,170 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self,
io1_a_src = a_src.private_impl.limit;
}
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
if (coro_susp_point) {
v_literal_width = self->private_impl.c_decode[0].v_literal_width;
v_clear_code = self->private_impl.c_decode[0].v_clear_code;
v_end_code = self->private_impl.c_decode[0].v_end_code;
v_save_code = self->private_impl.c_decode[0].v_save_code;
v_prev_code = self->private_impl.c_decode[0].v_prev_code;
v_width = self->private_impl.c_decode[0].v_width;
v_bad = self->private_impl.c_decode[0].v_bad;
v_j = self->private_impl.c_decode[0].v_j;
v_bits = self->private_impl.c_decode[0].v_bits;
v_n_bits = self->private_impl.c_decode[0].v_n_bits;
v_code = 0;
v_c = 0;
v_o = 0;
v_steps = 0;
v_first_byte = 0;
v_lm1_b = 0;
v_lm1_a = 0;
} else {
v_bad = false;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
v_literal_width = 8;
if (self->private_impl.f_literal_width >= 2) {
v_literal_width = self->private_impl.f_literal_width;
}
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 = v_end_code;
v_width = (v_literal_width + 1);
v_bad = 0;
v_j = 0;
while (v_j < v_clear_code) {
self->private_impl.f_lm1s[v_j] = 0;
self->private_impl.f_suffixes[v_j][0] = ((uint8_t)(v_j));
v_j += 1;
}
v_j = 0;
v_bits = 0;
v_n_bits = 0;
while (true) {
if (v_n_bits < v_width) {
if (((uint64_t)(io1_a_src - iop_a_src)) >= 4) {
v_bits |= (wuffs_base__load_u32le(iop_a_src) << v_n_bits);
(iop_a_src += ((31 - v_n_bits) >> 3),
wuffs_base__return_empty_struct());
v_n_bits |= 24;
v_clear_code = self->private_impl.f_clear_code;
v_end_code = self->private_impl.f_end_code;
v_save_code = self->private_impl.f_save_code;
v_prev_code = self->private_impl.f_prev_code;
v_width = self->private_impl.f_width;
v_bits = self->private_impl.f_bits;
v_n_bits = self->private_impl.f_n_bits;
v_j = self->private_impl.f_flush_j;
while (true) {
if (v_n_bits < v_width) {
if (((uint64_t)(io1_a_src - iop_a_src)) >= 4) {
v_bits |= (wuffs_base__load_u32le(iop_a_src) << v_n_bits);
(iop_a_src += ((31 - v_n_bits) >> 3),
wuffs_base__return_empty_struct());
v_n_bits |= 24;
} else if (((uint64_t)(io1_a_src - iop_a_src)) <= 0) {
self->private_impl.f_read_from_return_value = 2;
goto label_0_break;
} else {
v_bits |= (((uint32_t)(wuffs_base__load_u8be(iop_a_src))) << v_n_bits);
(iop_a_src += 1, wuffs_base__return_empty_struct());
v_n_bits += 8;
if (v_n_bits >= v_width) {
} else if (((uint64_t)(io1_a_src - iop_a_src)) <= 0) {
self->private_impl.f_read_from_return_value = 2;
goto label_0_break;
} else {
while (true) {
while (((uint64_t)(io1_a_src - iop_a_src)) <= 0) {
if (v_j > 0) {
self->private_impl.f_flush_j = v_j;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_lzw__decoder__auto_flush(self, a_dst);
if (status) {
goto suspend;
}
v_j = 0;
} else {
status = wuffs_base__suspension__short_read;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
}
}
v_bits |=
(((uint32_t)(wuffs_base__load_u8be(iop_a_src))) << v_n_bits);
(iop_a_src += 1, wuffs_base__return_empty_struct());
v_n_bits += 8;
if (v_n_bits >= v_width) {
goto label_0_break;
}
v_bits |=
(((uint32_t)(wuffs_base__load_u8be(iop_a_src))) << v_n_bits);
(iop_a_src += 1, wuffs_base__return_empty_struct());
v_n_bits += 8;
if (v_n_bits < v_width) {
self->private_impl.f_read_from_return_value = 4;
goto label_0_break;
}
label_0_break:;
}
}
v_code = ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_width));
v_bits >>= v_width;
v_n_bits -= v_width;
if (v_code < v_clear_code) {
self->private_impl.f_output[v_j] = ((uint8_t)(v_code));
v_j = ((v_j + 1) & 8191);
if (v_save_code <= 4095) {
v_lm1_a = ((self->private_impl.f_lm1s[v_prev_code] + 1) & 4095);
self->private_impl.f_lm1s[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],
sizeof(self->private_impl.f_suffixes[v_save_code]));
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;
}
v_prev_code = v_code;
}
v_code = ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_width));
v_bits >>= v_width;
v_n_bits -= v_width;
if (v_code < v_clear_code) {
self->private_impl.f_output[v_j] = ((uint8_t)(v_code));
v_j = ((v_j + 1) & 8191);
if (v_save_code <= 4095) {
v_lm1_a = ((self->private_impl.f_lm1s[v_prev_code] + 1) & 4095);
self->private_impl.f_lm1s[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],
sizeof(self->private_impl.f_suffixes[v_save_code]));
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));
}
} else if (v_code <= v_end_code) {
if (v_code == v_end_code) {
v_save_code += 1;
if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) {
v_width += 1;
}
v_prev_code = v_code;
}
} else if (v_code <= v_end_code) {
if (v_code == v_end_code) {
self->private_impl.f_read_from_return_value = 0;
goto label_0_break;
}
v_save_code = v_end_code;
v_prev_code = v_end_code;
v_width = (self->private_impl.f_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_lm1s[v_c])) & 4294967288)) &
8191);
v_j = ((v_j + 1 + ((uint32_t)(self->private_impl.f_lm1s[v_c]))) & 8191);
v_steps = (((uint32_t)(self->private_impl.f_lm1s[v_c])) >> 3);
while (true) {
memcpy((self->private_impl.f_output) + (v_o),
(self->private_impl.f_suffixes[v_c]), 8);
if (v_steps <= 0) {
goto label_1_break;
}
v_save_code = v_end_code;
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_lm1s[v_c])) & 4294967288)) &
8191);
v_j = ((v_j + 1 + ((uint32_t)(self->private_impl.f_lm1s[v_c]))) & 8191);
v_steps = (((uint32_t)(self->private_impl.f_lm1s[v_c])) >> 3);
while (true) {
memcpy((self->private_impl.f_output) + (v_o),
(self->private_impl.f_suffixes[v_c]), 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]));
}
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] = v_first_byte;
v_j = ((v_j + 1) & 8191);
}
if (v_save_code <= 4095) {
v_lm1_b = ((self->private_impl.f_lm1s[v_prev_code] + 1) & 4095);
self->private_impl.f_lm1s[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],
sizeof(self->private_impl.f_suffixes[v_save_code]));
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;
}
v_prev_code = v_code;
}
} else {
v_bad = true;
goto label_1_break;
v_steps -= 1;
v_o = ((v_o - 8) & 8191);
v_c = ((uint32_t)(self->private_impl.f_prefixes[v_c]));
}
if (v_j > 4095) {
while (v_n_bits >= 8) {
v_n_bits -= 8;
if (iop_a_src > io0_a_src) {
(iop_a_src--, wuffs_base__return_empty_struct());
} else {
status = wuffs_lzw__error__internal_error_inconsistent_i_o;
goto exit;
}
}
v_bits = ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_n_bits));
self->private_impl.f_flush_j = v_j;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
status = wuffs_lzw__decoder__auto_flush(self, a_dst);
if (status) {
goto suspend;
}
v_j = 0;
label_1_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] = v_first_byte;
v_j = ((v_j + 1) & 8191);
}
if (v_save_code <= 4095) {
v_lm1_b = ((self->private_impl.f_lm1s[v_prev_code] + 1) & 4095);
self->private_impl.f_lm1s[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],
sizeof(self->private_impl.f_suffixes[v_save_code]));
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;
}
v_prev_code = v_code;
}
} else {
self->private_impl.f_read_from_return_value = 3;
goto label_0_break;
}
label_1_break:;
if (v_j > 4095) {
self->private_impl.f_read_from_return_value = 1;
goto label_0_break;
}
}
label_0_break:;
if (self->private_impl.f_read_from_return_value != 2) {
while (v_n_bits >= 8) {
v_n_bits -= 8;
if (iop_a_src > io0_a_src) {
(iop_a_src--, wuffs_base__return_empty_struct());
} else {
status = wuffs_lzw__error__internal_error_inconsistent_i_o;
goto exit;
self->private_impl.f_read_from_return_value = 4;
goto label_2_break;
}
}
if (v_j > 0) {
self->private_impl.f_flush_j = v_j;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
status = wuffs_lzw__decoder__auto_flush(self, a_dst);
if (status) {
goto suspend;
}
}
if (v_bad) {
status = wuffs_lzw__error__bad_code;
goto exit;
}
goto ok;
ok:
self->private_impl.c_decode[0].coro_susp_point = 0;
goto exit;
label_2_break:;
}
goto suspend;
suspend:
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
self->private_impl.c_decode[0].v_literal_width = v_literal_width;
self->private_impl.c_decode[0].v_clear_code = v_clear_code;
self->private_impl.c_decode[0].v_end_code = v_end_code;
self->private_impl.c_decode[0].v_save_code = v_save_code;
self->private_impl.c_decode[0].v_prev_code = v_prev_code;
self->private_impl.c_decode[0].v_width = v_width;
self->private_impl.c_decode[0].v_bad = v_bad;
self->private_impl.c_decode[0].v_j = v_j;
self->private_impl.c_decode[0].v_bits = v_bits;
self->private_impl.c_decode[0].v_n_bits = v_n_bits;
goto exit;
exit:
self->private_impl.f_save_code = v_save_code;
self->private_impl.f_prev_code = v_prev_code;
self->private_impl.f_width = v_width;
self->private_impl.f_bits = v_bits;
self->private_impl.f_n_bits = v_n_bits;
self->private_impl.f_flush_j = v_j;
if (a_src.private_impl.buf) {
a_src.private_impl.buf->meta.ri =
iop_a_src - a_src.private_impl.buf->data.ptr;
}
if (wuffs_base__status__is_error(status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
return ((wuffs_base__empty_struct){});
}
// -------- func lzw.decoder.auto_flush
// -------- func lzw.decoder.write_to
static wuffs_base__status //
wuffs_lzw__decoder__auto_flush(wuffs_lzw__decoder* self,
wuffs_base__io_writer a_dst) {
wuffs_lzw__decoder__write_to(wuffs_lzw__decoder* self,
wuffs_base__io_writer a_dst) {
wuffs_base__status status = NULL;
uint32_t v_i;
@@ -7453,9 +7450,9 @@ wuffs_lzw__decoder__auto_flush(wuffs_lzw__decoder* self,
io1_a_dst = a_dst.private_impl.limit;
}
uint32_t coro_susp_point = self->private_impl.c_auto_flush[0].coro_susp_point;
uint32_t coro_susp_point = self->private_impl.c_write_to[0].coro_susp_point;
if (coro_susp_point) {
v_i = self->private_impl.c_auto_flush[0].v_i;
v_i = self->private_impl.c_write_to[0].v_i;
v_s = ((wuffs_base__slice_u8){});
v_n = 0;
} else {
@@ -7478,6 +7475,7 @@ wuffs_lzw__decoder__auto_flush(wuffs_lzw__decoder* self,
v_i, self->private_impl.f_flush_j);
v_n = wuffs_base__io_writer__copy_from_slice(&iop_a_dst, io1_a_dst, v_s);
if (v_n == ((uint64_t)(v_s.len))) {
self->private_impl.f_flush_j = 0;
status = NULL;
goto ok;
}
@@ -7488,14 +7486,14 @@ wuffs_lzw__decoder__auto_flush(wuffs_lzw__decoder* self,
goto ok;
ok:
self->private_impl.c_auto_flush[0].coro_susp_point = 0;
self->private_impl.c_write_to[0].coro_susp_point = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.c_auto_flush[0].coro_susp_point = coro_susp_point;
self->private_impl.c_auto_flush[0].v_i = v_i;
self->private_impl.c_write_to[0].coro_susp_point = coro_susp_point;
self->private_impl.c_write_to[0].v_i = v_i;
goto exit;
exit:
+117 -88
View File
@@ -17,58 +17,103 @@ pub status "?bad code"
pri status "?internal error: inconsistent I/O"
pub struct decoder?(
// The value passed to set_literal_width. This lw field is copied to the
// literal_width field at the start of the decode method. During that
// method, calling set_literal_width will change lw but not literal_width.
lw base.u32[..8],
// read_from state that does not change during a decode call.
literal_width base.u32[..8],
clear_code base.u32[..256],
end_code base.u32[..257],
// read_from state that does change during a decode call.
save_code base.u32[..4096],
prev_code base.u32[..4095],
width base.u32[..12],
bits base.u32,
n_bits base.u32[..31],
flush_j base.u32[..8191],
// read_from return value. The read_from method effectively returns a
// base.u32 to show how decode should continue after calling write_to. That
// value needs to be saved across write_to's possible suspension, so we
// might as well save it explicitly as a decoder field.
read_from_return_value base.u32,
// read_from per-code state.
suffixes array[4096] array[8] base.u8,
prefixes array[4096] base.u16[..4095],
// lm1s is the "length minus 1"s of the values for the implicit key-value
// table in this decoder. See std/lzw/README.md for more detail.
lm1s array[4096] base.u16[..4095],
// output[:flush_j] is the buffered output, connecting read_from with
// write_to and flush.
output array[8192 + 7] base.u8,
)
pub func decoder.set_literal_width!(lw base.u32[2..8]) {
this.literal_width = args.lw
this.lw = args.lw
}
pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
// These variables don't change over the lifetime of this func.
var literal_width base.u32[2..8] = 8
if this.literal_width >= 2 {
literal_width = this.literal_width
// Initialize read_from state.
this.literal_width = 8
if this.lw >= 2 {
this.literal_width = this.lw
}
var clear_code base.u32[4..256] = (1 as base.u32) << literal_width
var end_code base.u32[5..257] = clear_code + 1
// These variables do change.
//
// save_code is the code for which, after decoding a code, we save what the
// 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] = end_code
var width base.u32[..12] = literal_width + 1
var bad base.bool
// First, use the j variable to initialize some state.
var j base.u32[..8191]
while j < clear_code {
assert j < 256 via "a < b: a < c; c <= b"(c:clear_code)
this.lm1s[j] = 0
this.suffixes[j][0] = j as base.u8
j += 1
this.clear_code = (1 as base.u32) << this.literal_width
this.end_code = this.clear_code + 1
this.save_code = this.end_code
this.prev_code = this.end_code
this.width = this.literal_width + 1
this.bits = 0
this.n_bits = 0
this.flush_j = 0
var i base.u32[..8191]
while i < this.clear_code {
assert i < 256 via "a < b: a < c; c <= b"(c:this.clear_code)
this.lm1s[i] = 0
this.suffixes[i][0] = i as base.u8
i += 1
}
// For the rest of this function, j is such that output[:j] accumulates the
// decoded output to write (in batches) to args.dst.
j = 0
// These variables produce src's bits in Least Significant Bits order.
var bits base.u32
var n_bits base.u32[..31]
while true {
this.read_from!(src:args.src)
if this.flush_j > 0 {
this.write_to!??(dst:args.dst)
}
if this.read_from_return_value == 0 {
break
} else if this.read_from_return_value == 1 {
continue
} else if this.read_from_return_value == 2 {
yield base."$short read"
} else if this.read_from_return_value == 3 {
return "?bad code"
} else {
return "?internal error: inconsistent I/O"
}
}
}
pri func decoder.read_from!(src base.io_reader) {
var clear_code base.u32[..256] = this.clear_code
var end_code base.u32[..257] = this.end_code
var save_code base.u32[..4096] = this.save_code
var prev_code base.u32[..4095] = this.prev_code
var width base.u32[..12] = this.width
var bits base.u32 = this.bits
var n_bits base.u32[..31] = this.n_bits
var j base.u32[..8191] = this.flush_j
while true {
if n_bits < width {
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
if args.src.available() >= 4 {
// Read 4 bytes, using the "Variant 4" technique of
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
@@ -77,34 +122,26 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
n_bits |= 24
assert width <= n_bits via "a <= b: a <= c; c <= b"(c:12)
assert n_bits >= width via "a >= b: b <= a"()
} else if args.src.available() <= 0 {
this.read_from_return_value = 2
break
} else {
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
while true,
pre n_bits < 12,
post n_bits >= width,
{
// TODO: there is some optimization we could probably do
// around here. See commit ecb3995 `Tweak how std/lzw
// yields "short read"`.
while args.src.available() <= 0,
inv n_bits < 12,
post args.src.available() > 0,
{
if j > 0 {
this.flush_j = j
this.auto_flush!??(dst:args.dst)
j = 0
} else {
yield base."$short read"
}
}
bits |= args.src.peek_u8_as_u32() << n_bits
args.src.skip_fast!(actual:1, worst_case:1)
n_bits += 8
if n_bits >= width {
// No-op.
} else if args.src.available() <= 0 {
this.read_from_return_value = 2
break
} else {
bits |= args.src.peek_u8_as_u32() << n_bits
args.src.skip_fast!(actual:1, worst_case:1)
n_bits += 8
if n_bits >= width {
if n_bits < width { // TODO: should be unnecessary: n_bits >= 16 > width.
this.read_from_return_value = 4
break
}
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
}
}
}
@@ -139,11 +176,12 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
} else if code <= end_code {
if code == end_code {
this.read_from_return_value = 0
break
}
save_code = end_code
prev_code = end_code
width = literal_width + 1
width = this.literal_width + 1
} else if code <= save_code {
var c base.u32[..4095] = code
@@ -210,7 +248,7 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
}
} else {
bad = true
this.read_from_return_value = 3
break
}
@@ -219,44 +257,34 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
// less than 4096 and output's length is 8192, so a conservative
// threshold is ensuring that j <= 4095.
if j > 4095 {
// Rewind args.src, if we've read too many bits.
while n_bits >= 8 {
n_bits -= 8
if args.src.can_undo_byte() {
args.src.undo_byte!()
} else {
return "?internal error: inconsistent I/O"
}
this.read_from_return_value = 1
break
}
}
// Rewind args.src, if we're not in "$short read" and we've read too many
// bits.
if this.read_from_return_value != 2 {
while n_bits >= 8 {
n_bits -= 8
if args.src.can_undo_byte() {
args.src.undo_byte!()
} else {
this.read_from_return_value = 4
break
}
bits = bits.low_bits(n:n_bits)
this.flush_j = j
this.auto_flush!??(dst:args.dst)
j = 0
}
}
// Rewind args.src, if we've read too many bits.
while n_bits >= 8 {
n_bits -= 8
if args.src.can_undo_byte() {
args.src.undo_byte!()
} else {
return "?internal error: inconsistent I/O"
}
}
if j > 0 {
this.flush_j = j
this.auto_flush!??(dst:args.dst)
}
if bad {
return "?bad code"
}
this.save_code = save_code
this.prev_code = prev_code
this.width = width
this.bits = bits
this.n_bits = n_bits
this.flush_j = j
}
pri func decoder.auto_flush!??(dst base.io_writer) {
pri func decoder.write_to!??(dst base.io_writer) {
var i base.u32[..8191]
while this.flush_j > 0 {
if i > this.flush_j {
@@ -265,6 +293,7 @@ pri func decoder.auto_flush!??(dst base.io_writer) {
var s slice base.u8 = this.output[i:this.flush_j]
var n base.u64 = args.dst.copy_from_slice!(s:s)
if n == s.length() {
this.flush_j = 0
return ok
}
i = (i + ((n & 8191) as base.u32)) & 8191