mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +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")
|
||||
|
||||
Reference in new issue
Block a user