diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index 8cc7f4d0..7745ab89 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -8,17 +8,6 @@ #define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) -// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so -// if a benchmark justifies the additional code path. -#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0)) -#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8)) -#define PUFFS_U32BE(p) \ - (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \ - ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0)) -#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 // @@ -58,6 +47,28 @@ extern int printf(const char* __restrict __format, ...); // inline attribute to guide optimizations such as inlining, to avoid the // -Wunused-function warning, and we like to compile with -Wall -Werror. +// TODO: for puffs_u16be and similar functions, look for (ifdef) the x86 +// architecture and cast the pointer? Only do so if a benchmark justifies the +// additional code path. Modern compilers might already generate good code. + +static inline uint16_t puffs_u16be(uint8_t* p) { + return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0); +} + +static inline uint16_t puffs_u16le(uint8_t* p) { + return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8); +} + +static inline uint32_t puffs_u32be(uint8_t* p) { + return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | + ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0); +} + +static inline uint32_t puffs_u32le(uint8_t* p) { + return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | + ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); +} + // puffs_base_slice_u8_prefix returns up to the first n bytes of s. static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( puffs_base_slice_u8 s, diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index dda60400..7c49c75a 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -12,10 +12,10 @@ 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#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\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_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0))\n#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8))\n#define PUFFS_U32BE(p) \\\n (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \\\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0))\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 in" + - "stead 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// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a design goal is that users of\n//" + - " the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple puffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n retu" + - "rn s;\n}\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#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\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// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#de" + + "fine PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a design goal is that users of\n// the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple puffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\n// TODO: for puffs_u16be and similar functions, look for (ifdef) the x86\n// architecture and cast the pointer" + + "? Only do so if a benchmark justifies the\n// additional code path. Modern compilers might already generate good code.\n\nstatic inline uint16_t puffs_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t puffs_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t puffs_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t puffs_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base" + + "_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n return s;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" type template_args_short_read struct { diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 4dd3cfb8..d01944a7 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -305,16 +305,16 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { b.printf(" = *%srptr_src++;\n", bPrefix) } else if isInSrc(g.tm, n, t.KeyReadU16BE, 0) { - return g.writeReadUXX(b, n, "src", 16, "BE") + return g.writeReadUXX(b, n, "src", 16, "be") } else if isInSrc(g.tm, n, t.KeyReadU16LE, 0) { - return g.writeReadUXX(b, n, "src", 16, "LE") + return g.writeReadUXX(b, n, "src", 16, "le") } else if isInSrc(g.tm, n, t.KeyReadU32BE, 0) { - return g.writeReadUXX(b, n, "src", 32, "BE") + return g.writeReadUXX(b, n, "src", 32, "be") } else if isInSrc(g.tm, n, t.KeyReadU32LE, 0) { - return g.writeReadUXX(b, n, "src", 32, "LE") + return g.writeReadUXX(b, n, "src", 32, "le") } else if isInSrc(g.tm, n, t.KeySkip32, 1) { g.currFunk.usesScratch = true @@ -618,7 +618,7 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia if size != 16 && size != 32 { return fmt.Errorf("internal error: bad writeReadUXX size %d", size) } - if endianness != "BE" && endianness != "LE" { + if endianness != "be" && endianness != "le" { return fmt.Errorf("internal error: bad writeReadUXX endianness %q", endianness) } @@ -642,7 +642,7 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia cPrefix, g.currFunk.astFunc.Name().String(g.tm)) b.printf("if (PUFFS_LIKELY(%srend_src - %srptr_src >= %d)) {", bPrefix, bPrefix, size/8) - b.printf("%s%d = PUFFS_U%d%s(%srptr_src);\n", tPrefix, temp1, size, endianness, bPrefix) + b.printf("%s%d = puffs_u%d%s(%srptr_src);\n", tPrefix, temp1, size, endianness, bPrefix) b.printf("%srptr_src += %d;\n", bPrefix, size/8) b.printf("} else {") b.printf("%s = 0;\n", scratchName) @@ -657,13 +657,13 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia b.printf("uint32_t %s%d = %s", tPrefix, temp0, scratchName) switch endianness { - case "BE": + case "be": b.printf("& 0xFF;") b.printf("%s >>= 8;", scratchName) b.printf("%s <<= 8;", scratchName) b.printf("%s |= ((uint64_t)(*%srptr_%s++)) << (64 - %s%d);", scratchName, bPrefix, name, tPrefix, temp0) - case "LE": + case "le": b.printf(">> 56;") b.printf("%s <<= 8;", scratchName) b.printf("%s >>= 8;", scratchName) @@ -673,9 +673,9 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia b.printf("if (%s%d == %d) {", tPrefix, temp0, size-8) switch endianness { - case "BE": + case "be": b.printf("%s%d = %s >> (64 - %d);", tPrefix, temp1, scratchName, size) - case "LE": + case "le": b.printf("%s%d = %s;", tPrefix, temp1, scratchName) } b.printf("break;") @@ -683,9 +683,9 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia b.printf("%s%d += 8;", tPrefix, temp0) switch endianness { - case "BE": + case "be": b.printf("%s |= ((uint64_t)(%s%d));", scratchName, tPrefix, temp0) - case "LE": + case "le": b.printf("%s |= ((uint64_t)(%s%d)) << 56;", scratchName, tPrefix, temp0) } diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index a9a727a6..e6a95502 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -292,17 +292,6 @@ puffs_flate_status puffs_flate_zlib_decoder_decode( #define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) -// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so -// if a benchmark justifies the additional code path. -#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0)) -#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8)) -#define PUFFS_U32BE(p) \ - (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \ - ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0)) -#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 // @@ -342,6 +331,28 @@ extern int printf(const char* __restrict __format, ...); // inline attribute to guide optimizations such as inlining, to avoid the // -Wunused-function warning, and we like to compile with -Wall -Werror. +// TODO: for puffs_u16be and similar functions, look for (ifdef) the x86 +// architecture and cast the pointer? Only do so if a benchmark justifies the +// additional code path. Modern compilers might already generate good code. + +static inline uint16_t puffs_u16be(uint8_t* p) { + return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0); +} + +static inline uint16_t puffs_u16le(uint8_t* p) { + return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8); +} + +static inline uint32_t puffs_u32be(uint8_t* p) { + return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | + ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0); +} + +static inline uint32_t puffs_u32le(uint8_t* p) { + return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | + ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); +} + // puffs_base_slice_u8_prefix returns up to the first n bytes of s. static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( puffs_base_slice_u8 s, @@ -887,7 +898,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed( PUFFS_COROUTINE_SUSPENSION_POINT(1); uint32_t t_1; if (PUFFS_LIKELY(b_rend_src - b_rptr_src >= 4)) { - t_1 = PUFFS_U32LE(b_rptr_src); + t_1 = puffs_u32le(b_rptr_src); b_rptr_src += 4; } else { self->private_impl.c_decode_uncompressed[0].scratch = 0; @@ -1952,7 +1963,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode( PUFFS_COROUTINE_SUSPENSION_POINT(1); uint16_t t_1; if (PUFFS_LIKELY(b_rend_src - b_rptr_src >= 2)) { - t_1 = PUFFS_U16BE(b_rptr_src); + t_1 = puffs_u16be(b_rptr_src); b_rptr_src += 2; } else { self->private_impl.c_decode[0].scratch = 0; @@ -2021,7 +2032,7 @@ puffs_flate_status puffs_flate_zlib_decoder_decode( PUFFS_COROUTINE_SUSPENSION_POINT(4); uint32_t t_3; if (PUFFS_LIKELY(b_rend_src - b_rptr_src >= 4)) { - t_3 = PUFFS_U32BE(b_rptr_src); + t_3 = puffs_u32be(b_rptr_src); b_rptr_src += 4; } else { self->private_impl.c_decode[0].scratch = 0; diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 64d6d0a0..55409a3d 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -265,17 +265,6 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, #define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) -// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so -// if a benchmark justifies the additional code path. -#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0)) -#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8)) -#define PUFFS_U32BE(p) \ - (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \ - ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0)) -#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 // @@ -315,6 +304,28 @@ extern int printf(const char* __restrict __format, ...); // inline attribute to guide optimizations such as inlining, to avoid the // -Wunused-function warning, and we like to compile with -Wall -Werror. +// TODO: for puffs_u16be and similar functions, look for (ifdef) the x86 +// architecture and cast the pointer? Only do so if a benchmark justifies the +// additional code path. Modern compilers might already generate good code. + +static inline uint16_t puffs_u16be(uint8_t* p) { + return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0); +} + +static inline uint16_t puffs_u16le(uint8_t* p) { + return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8); +} + +static inline uint32_t puffs_u32be(uint8_t* p) { + return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | + ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0); +} + +static inline uint32_t puffs_u32le(uint8_t* p) { + return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | + ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); +} + // puffs_base_slice_u8_prefix returns up to the first n bytes of s. static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( puffs_base_slice_u8 s,