mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Add a read_u32le method.
This commit is contained in:
1 parent
7e9b3aab67
commit
368bd5e4f7
10 files changed
+126
-73
No files matched your search
@@ -6,6 +6,12 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so
|
||||
// if a benchmark justifies the additional code path.
|
||||
#define PUFFS_U32LE(p) \
|
||||
(((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \
|
||||
((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))
|
||||
|
||||
// Use switch cases for coroutine suspension points, similar to the technique
|
||||
// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
||||
//
|
||||
|
||||
@@ -12,5 +12,6 @@ const baseHeader = "" +
|
||||
""
|
||||
|
||||
const baseImpl = "" +
|
||||
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so\n// if a benchmark justifies the additional code path.\n#define PUFFS_U32LE(p) \\\n (((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \\\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(" +
|
||||
"!!(expr), 0))\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
""
|
||||
@@ -799,6 +799,9 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
g.printf(" = *%srptr_src++;\n", bPrefix)
|
||||
|
||||
} else if isInSrc(g.tm, n, t.KeyReadU32LE, 0) {
|
||||
return g.writeReadUXX(n, "src", 32, "le")
|
||||
|
||||
} else if isInSrc(g.tm, n, t.KeySkip32, 1) {
|
||||
if g.perFunc.tempW > maxTemp {
|
||||
return fmt.Errorf("too many temporary variables required")
|
||||
@@ -984,6 +987,53 @@ func (g *gen) writeShortRead(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeReadUXX(n *a.Expr, name string, size uint32, endianness string) error {
|
||||
if g.perFunc.tempW > maxTemp-1 {
|
||||
return fmt.Errorf("too many temporary variables required")
|
||||
}
|
||||
// temp0 is read by code generated in this function. temp1 is read elsewhere.
|
||||
temp0 := g.perFunc.tempW + 0
|
||||
temp1 := g.perFunc.tempW + 1
|
||||
g.perFunc.tempW += 2
|
||||
g.perFunc.tempR += 1
|
||||
|
||||
if err := g.writeCTypeName(n.MType(), tPrefix, fmt.Sprint(temp1)); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes(";")
|
||||
|
||||
g.printf("if (PUFFS_LIKELY(%srend_src - %srptr_src >= 4)) {", bPrefix, bPrefix)
|
||||
g.printf("%s%d = PUFFS_U32LE(%srptr_src);\n", tPrefix, temp1, bPrefix)
|
||||
g.printf("%srptr_src += 4;\n", bPrefix)
|
||||
g.printf("} else {")
|
||||
g.printf("self->private_impl.scratch = 0;\n")
|
||||
if err := g.writeCoroSuspPoint(); err != nil {
|
||||
return err
|
||||
}
|
||||
g.printf("while (true) {")
|
||||
|
||||
g.printf("if (PUFFS_UNLIKELY(%srptr_%s == %srend_%s)) { goto short_read_%s; }",
|
||||
bPrefix, name, bPrefix, name, name)
|
||||
g.perFunc.shortReads = append(g.perFunc.shortReads, name)
|
||||
|
||||
// TODO: look at endianness.
|
||||
g.printf("uint32_t %s%d = self->private_impl.scratch >> 56;", tPrefix, temp0)
|
||||
g.printf("self->private_impl.scratch <<= 8;")
|
||||
g.printf("self->private_impl.scratch >>= 8;")
|
||||
g.printf("self->private_impl.scratch |= ((uint64_t)(*%srptr_%s++)) << %s%d;", bPrefix, name, tPrefix, temp0)
|
||||
|
||||
g.printf("if (%s%d == %d) {", tPrefix, temp0, size-8)
|
||||
g.printf("%s%d = self->private_impl.scratch;", tPrefix, temp1)
|
||||
g.printf("break;")
|
||||
g.printf("}")
|
||||
|
||||
g.printf("%s%d += 8;", tPrefix, temp0)
|
||||
g.printf("self->private_impl.scratch |= ((uint64_t)(%s%d)) << 56;", tPrefix, temp0)
|
||||
|
||||
g.writes("}}\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExpr(n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
if depth > a.MaxExprDepth {
|
||||
return fmt.Errorf("expression recursion depth too large")
|
||||
|
||||
+47
-54
@@ -137,9 +137,7 @@ typedef struct {
|
||||
} c_decode[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_n0;
|
||||
uint8_t v_n1;
|
||||
uint8_t v_complement;
|
||||
uint32_t v_n;
|
||||
} c_decode_uncompressed[1];
|
||||
} private_impl;
|
||||
} puffs_flate_decoder;
|
||||
@@ -182,6 +180,12 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so
|
||||
// if a benchmark justifies the additional code path.
|
||||
#define PUFFS_U32LE(p) \
|
||||
(((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \
|
||||
((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))
|
||||
|
||||
// Use switch cases for coroutine suspension points, similar to the technique
|
||||
// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
||||
//
|
||||
@@ -432,9 +436,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
}
|
||||
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
||||
|
||||
uint8_t v_n0;
|
||||
uint8_t v_n1;
|
||||
uint8_t v_complement;
|
||||
uint32_t v_n;
|
||||
|
||||
uint8_t* b_wptr_dst = NULL;
|
||||
uint8_t* b_wend_dst = NULL;
|
||||
@@ -466,9 +468,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
uint32_t coro_susp_point =
|
||||
self->private_impl.c_decode_uncompressed[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_n0 = self->private_impl.c_decode_uncompressed[0].v_n0;
|
||||
v_n1 = self->private_impl.c_decode_uncompressed[0].v_n1;
|
||||
v_complement = self->private_impl.c_decode_uncompressed[0].v_complement;
|
||||
v_n = self->private_impl.c_decode_uncompressed[0].v_n;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||
@@ -479,57 +479,52 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
}
|
||||
self->private_impl.f_n_bits = 0;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
||||
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
uint32_t t_1;
|
||||
if (PUFFS_LIKELY(b_rend_src - b_rptr_src >= 4)) {
|
||||
t_1 = PUFFS_U32LE(b_rptr_src);
|
||||
b_rptr_src += 4;
|
||||
} else {
|
||||
self->private_impl.scratch = 0;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
||||
while (true) {
|
||||
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint32_t t_0 = self->private_impl.scratch >> 56;
|
||||
self->private_impl.scratch <<= 8;
|
||||
self->private_impl.scratch >>= 8;
|
||||
self->private_impl.scratch |= ((uint64_t)(*b_rptr_src++)) << t_0;
|
||||
if (t_0 == 24) {
|
||||
t_1 = self->private_impl.scratch;
|
||||
break;
|
||||
}
|
||||
t_0 += 8;
|
||||
self->private_impl.scratch |= ((uint64_t)(t_0)) << 56;
|
||||
}
|
||||
}
|
||||
uint8_t t_0 = *b_rptr_src++;
|
||||
v_n0 = t_0;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(2);
|
||||
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
v_n = t_1;
|
||||
if ((((v_n) & ((1 << (16)) - 1)) + ((v_n) >> (32 - (16)))) != 65535) {
|
||||
status = PUFFS_FLATE_ERROR_INCONSISTENT_STORED_BLOCK_LENGTH;
|
||||
goto suspend;
|
||||
}
|
||||
uint8_t t_1 = *b_rptr_src++;
|
||||
v_n1 = t_1;
|
||||
v_complement = 0;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
||||
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_2 = *b_rptr_src++;
|
||||
v_complement = t_2;
|
||||
if ((255 - v_complement) != v_n0) {
|
||||
status = PUFFS_FLATE_ERROR_INCONSISTENT_STORED_BLOCK_LENGTH;
|
||||
goto suspend;
|
||||
}
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
||||
if (PUFFS_UNLIKELY(b_rptr_src == b_rend_src)) {
|
||||
goto short_read_src;
|
||||
}
|
||||
uint8_t t_3 = *b_rptr_src++;
|
||||
v_complement = t_3;
|
||||
if ((255 - v_complement) != v_n1) {
|
||||
status = PUFFS_FLATE_ERROR_INCONSISTENT_STORED_BLOCK_LENGTH;
|
||||
goto suspend;
|
||||
}
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(5);
|
||||
{
|
||||
self->private_impl.scratch =
|
||||
((((uint32_t)(v_n1)) << 8) | ((uint32_t)(v_n0)));
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(6);
|
||||
size_t t_4 = self->private_impl.scratch;
|
||||
if (t_4 > b_wend_dst - b_wptr_dst) {
|
||||
t_4 = b_wend_dst - b_wptr_dst;
|
||||
self->private_impl.scratch = ((v_n) & ((1 << (16)) - 1));
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
||||
size_t t_2 = self->private_impl.scratch;
|
||||
if (t_2 > b_wend_dst - b_wptr_dst) {
|
||||
t_2 = b_wend_dst - b_wptr_dst;
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
|
||||
}
|
||||
if (t_4 > b_rend_src - b_rptr_src) {
|
||||
t_4 = b_rend_src - b_rptr_src;
|
||||
if (t_2 > b_rend_src - b_rptr_src) {
|
||||
t_2 = b_rend_src - b_rptr_src;
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_READ;
|
||||
}
|
||||
memmove(b_wptr_dst, b_rptr_src, t_4);
|
||||
b_wptr_dst += t_4;
|
||||
b_rptr_src += t_4;
|
||||
memmove(b_wptr_dst, b_rptr_src, t_2);
|
||||
b_wptr_dst += t_2;
|
||||
b_rptr_src += t_2;
|
||||
if (status) {
|
||||
self->private_impl.scratch -= t_4;
|
||||
self->private_impl.scratch -= t_2;
|
||||
goto suspend;
|
||||
}
|
||||
}
|
||||
@@ -539,9 +534,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.c_decode_uncompressed[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode_uncompressed[0].v_n0 = v_n0;
|
||||
self->private_impl.c_decode_uncompressed[0].v_n1 = v_n1;
|
||||
self->private_impl.c_decode_uncompressed[0].v_complement = v_complement;
|
||||
self->private_impl.c_decode_uncompressed[0].v_n = v_n;
|
||||
|
||||
if (a_dst.buf) {
|
||||
size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi);
|
||||
|
||||
@@ -252,6 +252,12 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so
|
||||
// if a benchmark justifies the additional code path.
|
||||
#define PUFFS_U32LE(p) \
|
||||
(((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \
|
||||
((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))
|
||||
|
||||
// Use switch cases for coroutine suspension points, similar to the technique
|
||||
// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
||||
//
|
||||
|
||||
+1
-3
@@ -137,9 +137,7 @@ typedef struct {
|
||||
} c_decode[1];
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint8_t v_n0;
|
||||
uint8_t v_n1;
|
||||
uint8_t v_complement;
|
||||
uint32_t v_n;
|
||||
} c_decode_uncompressed[1];
|
||||
} private_impl;
|
||||
} puffs_flate_decoder;
|
||||
|
||||
@@ -618,7 +618,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
|
||||
case t.KeyOpenParen:
|
||||
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
|
||||
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeySkip32, 1) ||
|
||||
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) ||
|
||||
isInSrc(q.tm, n, t.KeySkip32, 1) ||
|
||||
isInDst(q.tm, n, t.KeyWrite, 1) || isInDst(q.tm, n, t.KeyWriteU8, 1) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFrom32, 2) ||
|
||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||
|
||||
+2
-1
@@ -58,7 +58,8 @@ var (
|
||||
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
|
||||
|
||||
// TODO: delete this.
|
||||
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
|
||||
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
|
||||
typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
|
||||
)
|
||||
|
||||
// TypeMap maps from variable names (as token IDs) to types.
|
||||
|
||||
+7
-2
@@ -336,7 +336,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
case t.KeyOpenParen:
|
||||
// n is a function call.
|
||||
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
|
||||
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeySkip32, 1) ||
|
||||
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) ||
|
||||
isInSrc(q.tm, n, t.KeySkip32, 1) ||
|
||||
isInDst(q.tm, n, t.KeyWrite, 1) || isInDst(q.tm, n, t.KeyWriteU8, 1) ||
|
||||
isInDst(q.tm, n, t.KeyCopyFrom32, 2) ||
|
||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||
@@ -351,7 +352,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
if isInSrc(q.tm, n, t.KeyReadU32LE, 0) {
|
||||
n.SetMType(typeExprPlaceholder32) // HACK.
|
||||
} else {
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
|
||||
|
||||
@@ -53,17 +53,9 @@ pub func decoder.decode_uncompressed?(dst writer1, src reader1)() {
|
||||
}
|
||||
this.n_bits = 0
|
||||
|
||||
// TODO: a read_u16le method.
|
||||
var n0 u8 = in.src.read_u8?()
|
||||
var n1 u8 = in.src.read_u8?()
|
||||
var complement u8
|
||||
complement = in.src.read_u8?()
|
||||
if (0xFF - complement) != n0 {
|
||||
var n u32 = in.src.read_u32le?()
|
||||
if (n.low_bits(n:16) + n.high_bits(n:16)) != 0xFFFF {
|
||||
return error "inconsistent stored block length"
|
||||
}
|
||||
complement = in.src.read_u8?()
|
||||
if (0xFF - complement) != n1 {
|
||||
return error "inconsistent stored block length"
|
||||
}
|
||||
in.dst.copy_from32?(r:in.src, n:((n1 as u32) << 8) | (n0 as u32))
|
||||
in.dst.copy_from32?(r:in.src, n:n.low_bits(n:16))
|
||||
}
|
||||
Reference in new issue
Block a user