mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 16:47:28 +01:00
Add a huffman_decoder.consts field.
This commit is contained in:
1 parent
34198f3de5
commit
02cd837d9d
6 files changed
+47
-33
No files matched your search
+15
-9
@@ -969,12 +969,16 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
|||||||
}
|
}
|
||||||
g.writes("if (status) { goto suspend; }\n")
|
g.writes("if (status) { goto suspend; }\n")
|
||||||
|
|
||||||
} else if isThisMethod(g.tm, n, "init_huffman", 1) {
|
} else if isThisMethod(g.tm, n, "init_huffman", 2) {
|
||||||
g.printf("status = %s%s_init_huffman(self,",
|
g.printf("status = %s%s_init_huffman(self,",
|
||||||
g.pkgPrefix, g.perFunc.funk.Receiver().String(g.tm))
|
g.pkgPrefix, g.perFunc.funk.Receiver().String(g.tm))
|
||||||
if err := g.writeExpr(n.Args()[0].Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil {
|
if err := g.writeExpr(n.Args()[0].Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
g.writes(",")
|
||||||
|
if err := g.writeExpr(n.Args()[1].Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
g.writes(");\n")
|
g.writes(");\n")
|
||||||
if err := g.writeLoadExprDerivedVars(n); err != nil {
|
if err := g.writeLoadExprDerivedVars(n); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -1120,7 +1124,7 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
|
|||||||
switch n.ID0().Key() {
|
switch n.ID0().Key() {
|
||||||
case 0:
|
case 0:
|
||||||
if id1 := n.ID1(); id1.Key() == t.KeyThis {
|
if id1 := n.ID1(); id1.Key() == t.KeyThis {
|
||||||
g.writes("self->private_impl")
|
g.writes("self")
|
||||||
} else {
|
} else {
|
||||||
if n.GlobalIdent() {
|
if n.GlobalIdent() {
|
||||||
g.writes(g.pkgPrefix)
|
g.writes(g.pkgPrefix)
|
||||||
@@ -1187,20 +1191,22 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
|
|||||||
// TODO.
|
// TODO.
|
||||||
|
|
||||||
case t.KeyDot:
|
case t.KeyDot:
|
||||||
if n.LHS().Expr().ID1().Key() == t.KeyIn {
|
lhs := n.LHS().Expr()
|
||||||
|
if lhs.ID1().Key() == t.KeyIn {
|
||||||
g.writes(aPrefix)
|
g.writes(aPrefix)
|
||||||
g.writes(n.ID1().String(g.tm))
|
g.writes(n.ID1().String(g.tm))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := g.writeExpr(n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
|
if err := g.writeExpr(lhs, rp, parenthesesMandatory, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// TODO: choose between . vs -> operators.
|
if k := lhs.MType().Decorator().Key(); k == t.KeyPtr || k == t.KeyNptr {
|
||||||
//
|
g.writes("->")
|
||||||
// TODO: don't assume that the fPrefix is necessary.
|
} else {
|
||||||
g.writes(".")
|
g.writes(".")
|
||||||
g.writes(fPrefix)
|
}
|
||||||
|
g.writes("private_impl." + fPrefix)
|
||||||
g.writes(n.ID1().String(g.tm))
|
g.writes(n.ID1().String(g.tm))
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
|||||||
+17
-14
@@ -131,6 +131,7 @@ typedef struct {
|
|||||||
//
|
//
|
||||||
// It is a struct, not a struct*, so that it can be stack allocated.
|
// It is a struct, not a struct*, so that it can be stack allocated.
|
||||||
struct {
|
struct {
|
||||||
|
uint16_t f_counts[16];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_flate_huffman_decoder;
|
} puffs_flate_huffman_decoder;
|
||||||
|
|
||||||
@@ -174,7 +175,6 @@ typedef struct {
|
|||||||
} c_decode_dynamic[1];
|
} c_decode_dynamic[1];
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_counts[16];
|
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
} c_init_huffman[1];
|
} c_init_huffman[1];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
@@ -307,6 +307,7 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(puffs_flate_decoder* self,
|
|||||||
puffs_base_reader1 a_src);
|
puffs_base_reader1 a_src);
|
||||||
|
|
||||||
puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
||||||
|
uint32_t a_which,
|
||||||
uint32_t a_n_codes);
|
uint32_t a_n_codes);
|
||||||
|
|
||||||
// ---------------- Constructor and Destructor Implementations
|
// ---------------- Constructor and Destructor Implementations
|
||||||
@@ -739,7 +740,7 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(
|
|||||||
self->private_impl.f_bits = v_bits;
|
self->private_impl.f_bits = v_bits;
|
||||||
self->private_impl.f_n_bits = v_n_bits;
|
self->private_impl.f_n_bits = v_n_bits;
|
||||||
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
||||||
status = puffs_flate_decoder_init_huffman(self, 19);
|
status = puffs_flate_decoder_init_huffman(self, 0, 19);
|
||||||
if (status) {
|
if (status) {
|
||||||
goto suspend;
|
goto suspend;
|
||||||
}
|
}
|
||||||
@@ -786,37 +787,40 @@ short_read_src:
|
|||||||
}
|
}
|
||||||
|
|
||||||
puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
||||||
|
uint32_t a_which,
|
||||||
uint32_t a_n_codes) {
|
uint32_t a_n_codes) {
|
||||||
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
||||||
|
|
||||||
uint32_t v_counts[16];
|
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
|
|
||||||
uint32_t coro_susp_point =
|
uint32_t coro_susp_point =
|
||||||
self->private_impl.c_init_huffman[0].coro_susp_point;
|
self->private_impl.c_init_huffman[0].coro_susp_point;
|
||||||
if (coro_susp_point) {
|
if (coro_susp_point) {
|
||||||
memcpy(v_counts, self->private_impl.c_init_huffman[0].v_counts, 4 * 16);
|
|
||||||
v_i = self->private_impl.c_init_huffman[0].v_i;
|
v_i = self->private_impl.c_init_huffman[0].v_i;
|
||||||
}
|
}
|
||||||
switch (coro_susp_point) {
|
switch (coro_susp_point) {
|
||||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||||
|
|
||||||
{
|
v_i = 0;
|
||||||
size_t i;
|
while (v_i < 16) {
|
||||||
for (i = 0; i < 16; i++) {
|
self->private_impl.f_huff[a_which].private_impl.f_counts[v_i] = 0;
|
||||||
v_counts[i] = 0;
|
v_i += 1;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
v_i = 0;
|
v_i = 0;
|
||||||
while (v_i < a_n_codes) {
|
while (v_i < a_n_codes) {
|
||||||
if (v_counts[self->private_impl.f_code_lengths[v_i]] >= 289) {
|
if (self->private_impl.f_huff[a_which]
|
||||||
|
.private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] >=
|
||||||
|
289) {
|
||||||
status = PUFFS_FLATE_ERROR_INCONSISTENT_HUFFMAN_TABLE;
|
status = PUFFS_FLATE_ERROR_INCONSISTENT_HUFFMAN_TABLE;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
v_counts[self->private_impl.f_code_lengths[v_i]] += 1;
|
self->private_impl.f_huff[a_which]
|
||||||
|
.private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] += 1;
|
||||||
v_i += 1;
|
v_i += 1;
|
||||||
}
|
}
|
||||||
if (v_counts[0] == a_n_codes) {
|
if (((uint32_t)(
|
||||||
|
self->private_impl.f_huff[a_which].private_impl.f_counts[0])) ==
|
||||||
|
a_n_codes) {
|
||||||
status = PUFFS_FLATE_ERROR_INCONSISTENT_HUFFMAN_TABLE;
|
status = PUFFS_FLATE_ERROR_INCONSISTENT_HUFFMAN_TABLE;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
@@ -827,7 +831,6 @@ puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self,
|
|||||||
goto suspend;
|
goto suspend;
|
||||||
suspend:
|
suspend:
|
||||||
self->private_impl.c_init_huffman[0].coro_susp_point = coro_susp_point;
|
self->private_impl.c_init_huffman[0].coro_susp_point = coro_susp_point;
|
||||||
memcpy(self->private_impl.c_init_huffman[0].v_counts, v_counts, 4 * 16);
|
|
||||||
self->private_impl.c_init_huffman[0].v_i = v_i;
|
self->private_impl.c_init_huffman[0].v_i = v_i;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
|
|||||||
+1
-1
@@ -131,6 +131,7 @@ typedef struct {
|
|||||||
//
|
//
|
||||||
// It is a struct, not a struct*, so that it can be stack allocated.
|
// It is a struct, not a struct*, so that it can be stack allocated.
|
||||||
struct {
|
struct {
|
||||||
|
uint16_t f_counts[16];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_flate_huffman_decoder;
|
} puffs_flate_huffman_decoder;
|
||||||
|
|
||||||
@@ -174,7 +175,6 @@ typedef struct {
|
|||||||
} c_decode_dynamic[1];
|
} c_decode_dynamic[1];
|
||||||
struct {
|
struct {
|
||||||
uint32_t coro_susp_point;
|
uint32_t coro_susp_point;
|
||||||
uint32_t v_counts[16];
|
|
||||||
uint32_t v_i;
|
uint32_t v_i;
|
||||||
} c_init_huffman[1];
|
} c_init_huffman[1];
|
||||||
} private_impl;
|
} private_impl;
|
||||||
|
|||||||
@@ -625,7 +625,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
|||||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||||
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
||||||
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
||||||
isThisMethod(q.tm, n, "init_huffman", 1) {
|
isThisMethod(q.tm, n, "init_huffman", 2) {
|
||||||
|
|
||||||
for _, o := range n.Args() {
|
for _, o := range n.Args() {
|
||||||
// TODO: check that the arg range at the caller and the
|
// TODO: check that the arg range at the caller and the
|
||||||
@@ -913,7 +913,7 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
|
|||||||
b = numTypeBounds[key]
|
b = numTypeBounds[key]
|
||||||
}
|
}
|
||||||
if b[0] == nil || b[1] == nil {
|
if b[0] == nil || b[1] == nil {
|
||||||
return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.tm))
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
if n.IsRefined() {
|
if n.IsRefined() {
|
||||||
if x := n.Min(); x != nil {
|
if x := n.Min(); x != nil {
|
||||||
|
|||||||
+1
-1
@@ -350,7 +350,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
|||||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||||
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
||||||
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_dynamic", 2) ||
|
||||||
isThisMethod(q.tm, n, "init_huffman", 1) {
|
isThisMethod(q.tm, n, "init_huffman", 2) {
|
||||||
|
|
||||||
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
|
|||||||
this.bits = bits
|
this.bits = bits
|
||||||
this.n_bits = n_bits
|
this.n_bits = n_bits
|
||||||
|
|
||||||
this.init_huffman?(n_codes:19)
|
this.init_huffman?(which:0, n_codes:19)
|
||||||
|
|
||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
this.wip0 = hlit
|
this.wip0 = hlit
|
||||||
@@ -132,20 +132,24 @@ pri func decoder.decode_dynamic?(dst writer1, src reader1)() {
|
|||||||
// TODO: skip generating PUFFS_COROUTINE_SUSPENSION_POINTs for this function,
|
// TODO: skip generating PUFFS_COROUTINE_SUSPENSION_POINTs for this function,
|
||||||
// and its callers, as it can only return errors, not suspensions.
|
// and its callers, as it can only return errors, not suspensions.
|
||||||
|
|
||||||
pri func decoder.init_huffman?(n_codes u32[..289])() {
|
pri func decoder.init_huffman?(which u32[..1], n_codes u32[..289])() {
|
||||||
var counts[16] u32[..289]
|
|
||||||
var i u32
|
var i u32
|
||||||
|
while i < 16 {
|
||||||
|
this.huff[in.which].counts[i] = 0
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
i = 0
|
||||||
while i < in.n_codes {
|
while i < in.n_codes {
|
||||||
assert i < 289 via "a < b: a < c; c <= b"(c:in.n_codes)
|
assert i < 289 via "a < b: a < c; c <= b"(c:in.n_codes)
|
||||||
// TODO: this if should be unnecessary. Have some way to assert that,
|
// TODO: this if should be unnecessary. Have some way to assert that,
|
||||||
// for all j, counts[j] <= i, and thus counts[j]++ will not overflow.
|
// for all j, counts[j] <= i, and thus counts[j]++ will not overflow.
|
||||||
if counts[this.code_lengths[i]] >= 289 {
|
if this.huff[in.which].counts[this.code_lengths[i]] >= 289 {
|
||||||
return error "inconsistent Huffman table"
|
return error "inconsistent Huffman table"
|
||||||
}
|
}
|
||||||
counts[this.code_lengths[i]] += 1
|
this.huff[in.which].counts[this.code_lengths[i]] += 1
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
if counts[0] == in.n_codes {
|
if (this.huff[in.which].counts[0] as u32) == in.n_codes {
|
||||||
// There are no codes.
|
// There are no codes.
|
||||||
return error "inconsistent Huffman table"
|
return error "inconsistent Huffman table"
|
||||||
}
|
}
|
||||||
@@ -160,4 +164,5 @@ pri func decoder.init_huffman?(n_codes u32[..289])() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pri struct huffman_decoder(
|
pri struct huffman_decoder(
|
||||||
|
counts[16] u16[..289],
|
||||||
)
|
)
|
||||||
Reference in new issue
Block a user