Add use_save_code to std/gif/decode_lzw.puffs.

This commit is contained in:
Nigel Tao committed 2017-06-09 14:51:39 +10:00
1 parent 0b034d7f6e
commit ddbbdecce6
5 files changed
+65 -6

No files matched your search

+15 -2
View File
@@ -14,6 +14,7 @@ package main
import (
"bytes"
"fmt"
"math/big"
"os"
"os/exec"
"strings"
@@ -24,6 +25,11 @@ import (
t "github.com/google/puffs/lang/token"
)
var (
zero = big.NewInt(0)
one = big.NewInt(1)
)
// Prefixes are prepended to names to form a namespace and to avoid e.g.
// "double" being a valid Puffs variable name but not a valid C one.
const (
@@ -890,8 +896,15 @@ func (g *gen) writeExpr(n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, d
}
if cv := n.ConstValue(); cv != nil {
// TODO: write false/true instead of 0/1 if n.MType() is bool?
g.writes(cv.String())
if nTyp := n.MType(); nTyp.PackageOrDecorator() != 0 || nTyp.Name().Key() != t.KeyBool {
g.writes(cv.String())
} else if cv.Cmp(zero) == 0 {
g.writes("false")
} else if cv.Cmp(one) == 0 {
g.writes("true")
} else {
return fmt.Errorf("%v has type bool but constant value %v is neither 0 or 1", n.String(g.tm), cv)
}
return nil
}
+13 -1
View File
@@ -217,6 +217,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
uint32_t v_clear_code;
uint32_t v_end_code;
bool v_use_save_code;
uint32_t v_save_code;
uint32_t v_width;
uint32_t v_bits;
@@ -225,12 +226,13 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
v_clear_code = (((uint32_t)(1)) << self->f_literal_width);
v_end_code = (v_clear_code + 1);
v_use_save_code = 0;
v_save_code = v_end_code;
v_width = (self->f_literal_width + 1);
v_bits = 0;
v_n_bits = 0;
label_0_continue:;
while (1) {
while (true) {
while (v_n_bits < v_width) {
if (a_src->ri >= a_src->wi) {
status = puffs_gif_status_short_read;
@@ -246,6 +248,7 @@ label_0_continue:;
if (v_code < v_clear_code) {
goto label_0_break;
} else if (v_code == v_clear_code) {
v_use_save_code = false;
v_save_code = v_end_code;
v_width = (self->f_literal_width + 1);
goto label_0_continue;
@@ -259,6 +262,15 @@ label_0_continue:;
status = puffs_gif_error_bad_gif_image;
goto cleanup0;
}
v_use_save_code = (v_save_code < 4095);
if (v_use_save_code) {
v_save_code += 1;
if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) {
v_width += 1;
goto label_0_break;
}
goto label_0_break;
}
goto label_0_break;
}
label_0_break:;
+21
View File
@@ -21,6 +21,27 @@ func (z *facts) appendFact(fact *a.Expr) {
return
}
}
switch fact.ID0().Key() {
case 0:
for _, f := range *z {
if f.ID0().Key() != t.KeyXBinaryEqEq {
continue
}
if fact.Eq(f.LHS().Expr()) {
z.appendFact(f.RHS().Expr())
} else if fact.Eq(f.RHS().Expr()) {
z.appendFact(f.LHS().Expr())
}
}
case t.KeyXBinaryAnd:
z.appendFact(fact.LHS().Expr())
z.appendFact(fact.RHS().Expr())
return
case t.KeyXAssociativeAnd:
// TODO.
}
*z = append(*z, fact)
}
+3 -1
View File
@@ -765,7 +765,9 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
}
b := [2]*big.Int{}
if key := n.Name().Key(); key < t.Key(len(numTypeBounds)) {
if key := n.Name().Key(); key == t.KeyBool {
b = [2]*big.Int{zero, one}
} else if key < t.Key(len(numTypeBounds)) {
b = numTypeBounds[key]
}
if b[0] == nil || b[1] == nil {
+13 -2
View File
@@ -18,6 +18,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
var end_code u32[5..257] = clear_code + 1
// These variables do change.
var use_save_code bool
var save_code u32[..4095] = end_code
var width u32[..12] = this.literal_width + 1
@@ -42,8 +43,10 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
n_bits -= width
if code < clear_code {
// TODO: if use_save_code { etc }
break
} else if code == clear_code {
use_save_code = false
save_code = end_code
width = this.literal_width + 1
continue
@@ -55,7 +58,15 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
return error "bad GIF image"
}
// TODO: implement.
break
use_save_code = save_code < 4095
if use_save_code {
save_code += 1
if (save_code == ((1 as u32) << width)) and (width < 12) {
width += 1
break // TODO: remove.
}
break // TODO: remove.
}
break // TODO: remove.
}
}