Remove use of struct field initial values

This commit is contained in:
Nigel Tao committed 2018-05-29 09:07:23 +10:00
1 parent 7a4f6e9e1f
commit 0e498cb86d
10 files changed
+53 -26

No files matched your search

+5 -1
View File
@@ -1204,6 +1204,7 @@ typedef struct {
uint32_t magic; uint32_t magic;
uint32_t f_state; uint32_t f_state;
bool f_started;
} private_impl; } private_impl;
} wuffs_adler32__hasher; } wuffs_adler32__hasher;
@@ -1809,7 +1810,6 @@ void wuffs_adler32__hasher__initialize(wuffs_adler32__hasher* self,
memset(self, 0, sizeof(*self)); memset(self, 0, sizeof(*self));
} }
self->private_impl.magic = WUFFS_BASE__MAGIC; self->private_impl.magic = WUFFS_BASE__MAGIC;
self->private_impl.f_state = 1;
} }
// ---------------- Function Implementations // ---------------- Function Implementations
@@ -1832,6 +1832,10 @@ uint32_t wuffs_adler32__hasher__update(wuffs_adler32__hasher* self,
uint32_t v_s2; uint32_t v_s2;
wuffs_base__slice_u8 v_remaining; wuffs_base__slice_u8 v_remaining;
if (!self->private_impl.f_started) {
self->private_impl.f_started = true;
self->private_impl.f_state = 1;
}
v_s1 = ((self->private_impl.f_state) & ((1 << (16)) - 1)); v_s1 = ((self->private_impl.f_state) & ((1 << (16)) - 1));
v_s2 = ((self->private_impl.f_state) >> (32 - (16))); v_s2 = ((self->private_impl.f_state) >> (32 - (16)));
while (((uint64_t)(a_x.len)) > 0) { while (((uint64_t)(a_x.len)) > 0) {
+21 -11
View File
@@ -1222,11 +1222,11 @@ typedef struct {
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint32_t v_literal_width;
uint32_t v_clear_code; uint32_t v_clear_code;
uint32_t v_end_code; uint32_t v_end_code;
uint32_t v_save_code; uint32_t v_save_code;
uint32_t v_prev_code; uint32_t v_prev_code;
uint32_t v_initial_width;
uint32_t v_width; uint32_t v_width;
uint32_t v_bits; uint32_t v_bits;
uint32_t v_n_bits; uint32_t v_n_bits;
@@ -1279,6 +1279,7 @@ typedef struct {
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint32_t v_num_loops;
} c_decode_config[1]; } c_decode_config[1];
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
@@ -2022,7 +2023,6 @@ void wuffs_gif__lzw_decoder__initialize(wuffs_gif__lzw_decoder* self,
memset(self, 0, sizeof(*self)); memset(self, 0, sizeof(*self));
} }
self->private_impl.magic = WUFFS_BASE__MAGIC; self->private_impl.magic = WUFFS_BASE__MAGIC;
self->private_impl.f_literal_width = 8;
} }
void wuffs_gif__decoder__initialize(wuffs_gif__decoder* self, void wuffs_gif__decoder__initialize(wuffs_gif__decoder* self,
@@ -2039,7 +2039,6 @@ void wuffs_gif__decoder__initialize(wuffs_gif__decoder* self,
memset(self, 0, sizeof(*self)); memset(self, 0, sizeof(*self));
} }
self->private_impl.magic = WUFFS_BASE__MAGIC; self->private_impl.magic = WUFFS_BASE__MAGIC;
self->private_impl.f_num_loops = 1;
wuffs_gif__lzw_decoder__initialize(&self->private_impl.f_lzw, WUFFS_VERSION, wuffs_gif__lzw_decoder__initialize(&self->private_impl.f_lzw, WUFFS_VERSION,
WUFFS_BASE__ALREADY_ZEROED); WUFFS_BASE__ALREADY_ZEROED);
} }
@@ -2067,9 +2066,12 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(
} }
wuffs_gif__status status = WUFFS_GIF__STATUS_OK; wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
uint32_t v_num_loops;
uint32_t coro_susp_point = uint32_t coro_susp_point =
self->private_impl.c_decode_config[0].coro_susp_point; self->private_impl.c_decode_config[0].coro_susp_point;
if (coro_susp_point) { if (coro_susp_point) {
v_num_loops = self->private_impl.c_decode_config[0].v_num_loops;
} else { } else {
} }
switch (coro_susp_point) { switch (coro_susp_point) {
@@ -2094,9 +2096,13 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(
if (status) { if (status) {
goto suspend; goto suspend;
} }
v_num_loops = 1;
if (self->private_impl.f_seen_num_loops) {
v_num_loops = self->private_impl.f_num_loops;
}
wuffs_base__image_config__initialize( wuffs_base__image_config__initialize(
a_dst, 570984584, 0, self->private_impl.f_width, a_dst, 570984584, 0, self->private_impl.f_width,
self->private_impl.f_height, self->private_impl.f_num_loops); self->private_impl.f_height, v_num_loops);
self->private_impl.f_call_sequence = 1; self->private_impl.f_call_sequence = 1;
goto ok; goto ok;
@@ -2108,6 +2114,7 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(
goto suspend; goto suspend;
suspend: suspend:
self->private_impl.c_decode_config[0].coro_susp_point = coro_susp_point; self->private_impl.c_decode_config[0].coro_susp_point = coro_susp_point;
self->private_impl.c_decode_config[0].v_num_loops = v_num_loops;
goto exit; goto exit;
exit: exit:
@@ -3649,11 +3656,11 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode(
wuffs_base__io_reader a_src) { wuffs_base__io_reader a_src) {
wuffs_gif__status status = WUFFS_GIF__STATUS_OK; wuffs_gif__status status = WUFFS_GIF__STATUS_OK;
uint32_t v_literal_width;
uint32_t v_clear_code; uint32_t v_clear_code;
uint32_t v_end_code; uint32_t v_end_code;
uint32_t v_save_code; uint32_t v_save_code;
uint32_t v_prev_code; uint32_t v_prev_code;
uint32_t v_initial_width;
uint32_t v_width; uint32_t v_width;
uint32_t v_bits; uint32_t v_bits;
uint32_t v_n_bits; uint32_t v_n_bits;
@@ -3699,11 +3706,11 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode(
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
if (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_clear_code = self->private_impl.c_decode[0].v_clear_code;
v_end_code = self->private_impl.c_decode[0].v_end_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_save_code = self->private_impl.c_decode[0].v_save_code;
v_prev_code = self->private_impl.c_decode[0].v_prev_code; v_prev_code = self->private_impl.c_decode[0].v_prev_code;
v_initial_width = self->private_impl.c_decode[0].v_initial_width;
v_width = self->private_impl.c_decode[0].v_width; v_width = self->private_impl.c_decode[0].v_width;
v_bits = self->private_impl.c_decode[0].v_bits; v_bits = self->private_impl.c_decode[0].v_bits;
v_n_bits = self->private_impl.c_decode[0].v_n_bits; v_n_bits = self->private_impl.c_decode[0].v_n_bits;
@@ -3718,12 +3725,15 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode(
switch (coro_susp_point) { switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
v_clear_code = (((uint32_t)(1)) << self->private_impl.f_literal_width); 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_end_code = (v_clear_code + 1);
v_save_code = v_end_code; v_save_code = v_end_code;
v_prev_code = 0; v_prev_code = 0;
v_initial_width = (self->private_impl.f_literal_width + 1); v_width = (v_literal_width + 1);
v_width = v_initial_width;
v_bits = 0; v_bits = 0;
v_n_bits = 0; v_n_bits = 0;
label_0_continue:; label_0_continue:;
@@ -3757,7 +3767,7 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode(
} else if (v_code == v_clear_code) { } else if (v_code == v_clear_code) {
v_save_code = v_end_code; v_save_code = v_end_code;
v_prev_code = 0; v_prev_code = 0;
v_width = v_initial_width; v_width = (v_literal_width + 1);
goto label_0_continue; goto label_0_continue;
} else if (v_code == v_end_code) { } else if (v_code == v_end_code) {
status = WUFFS_GIF__STATUS_OK; status = WUFFS_GIF__STATUS_OK;
@@ -3824,11 +3834,11 @@ static wuffs_gif__status wuffs_gif__lzw_decoder__decode(
goto suspend; goto suspend;
suspend: suspend:
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; 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_clear_code = v_clear_code;
self->private_impl.c_decode[0].v_end_code = v_end_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_save_code = v_save_code;
self->private_impl.c_decode[0].v_prev_code = v_prev_code; self->private_impl.c_decode[0].v_prev_code = v_prev_code;
self->private_impl.c_decode[0].v_initial_width = v_initial_width;
self->private_impl.c_decode[0].v_width = v_width; self->private_impl.c_decode[0].v_width = v_width;
self->private_impl.c_decode[0].v_bits = v_bits; 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_n_bits = v_n_bits;
+1
View File
@@ -1213,6 +1213,7 @@ typedef struct {
uint32_t magic; uint32_t magic;
uint32_t f_state; uint32_t f_state;
bool f_started;
} private_impl; } private_impl;
} wuffs_adler32__hasher; } wuffs_adler32__hasher;
+1
View File
@@ -1204,6 +1204,7 @@ typedef struct {
uint32_t magic; uint32_t magic;
uint32_t f_state; uint32_t f_state;
bool f_started;
} private_impl; } private_impl;
} wuffs_adler32__hasher; } wuffs_adler32__hasher;
+2 -1
View File
@@ -1222,11 +1222,11 @@ typedef struct {
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint32_t v_literal_width;
uint32_t v_clear_code; uint32_t v_clear_code;
uint32_t v_end_code; uint32_t v_end_code;
uint32_t v_save_code; uint32_t v_save_code;
uint32_t v_prev_code; uint32_t v_prev_code;
uint32_t v_initial_width;
uint32_t v_width; uint32_t v_width;
uint32_t v_bits; uint32_t v_bits;
uint32_t v_n_bits; uint32_t v_n_bits;
@@ -1279,6 +1279,7 @@ typedef struct {
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
uint32_t v_num_loops;
} c_decode_config[1]; } c_decode_config[1];
struct { struct {
uint32_t coro_susp_point; uint32_t coro_susp_point;
+1
View File
@@ -1213,6 +1213,7 @@ typedef struct {
uint32_t magic; uint32_t magic;
uint32_t f_state; uint32_t f_state;
bool f_started;
} private_impl; } private_impl;
} wuffs_adler32__hasher; } wuffs_adler32__hasher;
+7 -1
View File
@@ -16,13 +16,19 @@ packageid "adl3"
// TODO: drop the '?' but still generate wuffs_adler32__hasher__initialize? // TODO: drop the '?' but still generate wuffs_adler32__hasher__initialize?
pub struct hasher?( pub struct hasher?(
state base.u32 = 1, state base.u32,
started base.bool,
) )
pub func hasher.update!(x slice base.u8)(checksum base.u32) { pub func hasher.update!(x slice base.u8)(checksum base.u32) {
// The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in // The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in
// RFC 1950. // RFC 1950.
if not this.started {
this.started = true
this.state = 1
}
var s1 base.u32 = this.state.low_bits(n:16) var s1 base.u32 = this.state.low_bits(n:16)
var s2 base.u32 = this.state.high_bits(n:16) var s2 base.u32 = this.state.high_bits(n:16)
+7 -2
View File
@@ -53,7 +53,7 @@ pub struct decoder?(
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of // Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
// animation loops is 1. // animation loops is 1.
seen_num_loops base.bool, seen_num_loops base.bool,
num_loops base.u32 = 1, num_loops base.u32,
seen_graphic_control base.bool, seen_graphic_control base.bool,
gc_has_transparent_index base.bool, gc_has_transparent_index base.bool,
@@ -92,12 +92,17 @@ pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)()
// TODO: if this.end_of_data, return an error and/or set dst to zero? // TODO: if this.end_of_data, return an error and/or set dst to zero?
var num_loops base.u32 = 1
if this.seen_num_loops {
num_loops = this.num_loops
}
// TODO: rename initialize to set? // TODO: rename initialize to set?
// //
// TODO: a Wuffs (not just C) name for the // TODO: a Wuffs (not just C) name for the
// WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant. // WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant.
in.dst.initialize!(pixfmt:0x22088888, pixsub:0, in.dst.initialize!(pixfmt:0x22088888, pixsub:0,
width:this.width, height:this.height, num_loops:this.num_loops) width:this.width, height:this.height, num_loops:num_loops)
this.call_sequence = 1 this.call_sequence = 1
} }
+8 -5
View File
@@ -18,7 +18,7 @@ pub error "LZW code is out of range"
pub error "LZW prefix chain is cyclical" pub error "LZW prefix chain is cyclical"
pri struct lzw_decoder?( pri struct lzw_decoder?(
literal_width base.u32[2..8] = 8, literal_width base.u32[..8],
stack array[4096] base.u8, stack array[4096] base.u8,
suffixes array[4096] base.u8, suffixes array[4096] base.u8,
prefixes array[4096] base.u16[..4095], prefixes array[4096] base.u16[..4095],
@@ -30,7 +30,11 @@ pri func lzw_decoder.set_literal_width!(lw base.u32[2..8] = 8)() {
pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() { pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
// These variables don't change over the lifetime of this func. // These variables don't change over the lifetime of this func.
var clear_code base.u32[4..256] = (1 as base.u32) << this.literal_width var literal_width base.u32[2..8] = 8
if this.literal_width >= 2 {
literal_width = this.literal_width
}
var clear_code base.u32[4..256] = (1 as base.u32) << literal_width
var end_code base.u32[5..257] = clear_code + 1 var end_code base.u32[5..257] = clear_code + 1
// These variables do change. // These variables do change.
@@ -40,8 +44,7 @@ pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
// `max`. 4096 means do not save. // `max`. 4096 means do not save.
var save_code base.u32[..4096] = end_code var save_code base.u32[..4096] = end_code
var prev_code base.u32[..4095] var prev_code base.u32[..4095]
var initial_width base.u32[..12] = this.literal_width + 1 var width base.u32[..12] = literal_width + 1
var width base.u32[..12] = initial_width
// These variables yield src's bits in Least Significant Bits order. // These variables yield src's bits in Least Significant Bits order.
var bits base.u32 var bits base.u32
@@ -74,7 +77,7 @@ pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
} else if code == clear_code { } else if code == clear_code {
save_code = end_code save_code = end_code
prev_code = 0 prev_code = 0
width = initial_width width = literal_width + 1
continue continue
} else if code == end_code { } else if code == end_code {
-5
View File
@@ -90,11 +90,6 @@ void test_basic_wuffs_version_good() {
FAIL("magic: got %u, want %u", dec.private_impl.magic, WUFFS_BASE__MAGIC); FAIL("magic: got %u, want %u", dec.private_impl.magic, WUFFS_BASE__MAGIC);
return; return;
} }
if (dec.private_impl.f_lzw.private_impl.f_literal_width != 8) {
FAIL("f_literal_width: got %u, want %u",
dec.private_impl.f_lzw.private_impl.f_literal_width, 8);
return;
}
} }
void test_basic_status_is_error() { void test_basic_status_is_error() {