diff --git a/script/puffs-flate-decoder-decode-huffman.c b/script/puffs-flate-decoder-decode-huffman.c index 2c8bc66a..7ee27475 100644 --- a/script/puffs-flate-decoder-decode-huffman.c +++ b/script/puffs-flate-decoder-decode-huffman.c @@ -34,13 +34,12 @@ // // #include "../../../script/puffs-flate-decoder-decode-huffman.c" // -// Then find the call to puffs_flate_decoder_decode_huffman. It should be -// inside the puffs_flate_decoder_decode function body, and the line of code -// should look something like +// Then find the call to puffs_flate__flate_decoder__decode_huffman_fast. It +// should be inside the puffs_flate__flate_decoder__decode_blocks function body, +// and the lines of code should look something like // -// status = puffs_flate_decoder_decode_huffman(self, a_dst, a_src); -// -// TODO: "puffs_flate_decoder" should be "puffs_flate__flate_decoder". +// status = +// puffs_flate__flate_decoder__decode_huffman_fast(self, a_dst, a_src); // // Change the "puffs" to "c_puffs", i.e. add a "c_" prefix. The net result // should look something like: @@ -49,58 +48,93 @@ // // $ git diff gen/c/std/flate.c // diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c -// index 372d6ef..777f30b 100644 +// index 47cb0a9..e7e12b6 100644 // --- a/gen/c/std/flate.c // +++ b/gen/c/std/flate.c -// @@ -317,6 +317,8 @@ puffs_flate__status puffs_flate__zlib_decoder__decode( +// @@ -336,6 +336,7 @@ puffs_flate__status puffs_flate__zlib_decoder__decode( +// #endif // PUFFS_FLATE_H // // // C HEADER ENDS HERE. -// // +#include "../../../script/puffs-flate-decoder-decode-huffman.c" -// + +// // #ifndef PUFFS_BASE_IMPL_H // #define PUFFS_BASE_IMPL_H -// -// @@ -1063,7 +1065,7 @@ puffs_flate__status -// puffs_flate__flate_decoder__decode_blocks( -// } +// @@ -1119,7 +1120,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_blocks( // } // } -// - status = puffs_flate__flate_decoder__decode_huffman(self, a_dst, a_src); -// + status = c_puffs_flate__flate_decoder__decode_huffman(self, a_dst, a_src); +// status = +// - puffs_flate__flate_decoder__decode_huffman_fast(self, a_dst, a_src); +// + c_puffs_flate__flate_decoder__decode_huffman_fast(self, a_dst, a_src); // if (a_src.buf) { // b_rptr_src = a_src.buf->ptr + a_src.buf->ri; -// size_t len = a_src.buf->wi - a_src.buf->ri; +// } // // ---------------- // -// That concludes the two edits to gen/c/std/flate.c. Run the C test and -// benchmark programs directly, instead of running "puffs test" or "puffs -// bench", since the "puffs" tool will re-generate the C code and override your -// gen/c/std/flate.c edit. +// That concludes the two edits to gen/c/std/flate.c. Run the tests and +// benchmarks with the "-skipgen" flag, otherwise the "puffs" tool will +// re-generate the C code and override your gen/c/std/flate.c edit: // -// cd test/c/std -// gcc -O3 flate.c && ./a.out && ./a.out -bench; rm -f ./a.out +// puffs test -skipgen std/flate +// puffs bench -skipgen std/flate +// +// You may also want to focus on one specific test, e.g.: +// +// puffs test -skipgen -focus=puffs_flate_decode_midsummer std/flate +// puffs bench -skipgen -focus=puffs_flate_decode_100k std/flate #include #include // For manual printf debugging. +// Define this macro to further speed up this C implementation, using two +// techniques that are not used by the zlib-the-library C implementation (as of +// version 1.2.11, current as of November 2017). Doing so gives data for "how +// fast can Puffs' zlib-the-format implementation be" instead of "is Puffs' +// generated C code as fast as zlib-the-library's hand-written C code". +// +// Whether the same techniques could apply to zlib-the-library is discussed at +// https://github.com/madler/zlib/pull/292 #ifdef __x86_64__ -#define PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS +//#define PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS #endif -puffs_flate__status c_puffs_flate__flate_decoder__decode_huffman( +// This is the generated function that we are explicitly overriding. Note that +// the function name is "puffs_etc", not "c_puffs_etc". +static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast( + puffs_flate__flate_decoder* self, + puffs_base__writer1 a_dst, + puffs_base__reader1 a_src); + +// This is the overriding implementation. +puffs_flate__status c_puffs_flate__flate_decoder__decode_huffman_fast( puffs_flate__flate_decoder* self, puffs_base__writer1 a_dst, puffs_base__reader1 a_src) { + // Avoid the -Werror=unused-function warning for the now-unused + // overridden puffs_flate__flate_decoder__decode_huffman_fast. + (void)(puffs_flate__flate_decoder__decode_huffman_fast); + if (!a_dst.buf || !a_src.buf) { return PUFFS_FLATE__ERROR_BAD_ARGUMENT; } puffs_flate__status status = PUFFS_FLATE__STATUS_OK; - // Load contextual state. + // Load contextual state. Prepare to check that pdst and psrc remain within + // a_dst's and a_src's bounds. uint8_t* pdst = a_dst.buf->ptr + a_dst.buf->wi; + uint8_t* qdst = a_dst.buf->ptr + a_dst.buf->len; + if ((qdst - pdst) < 258) { + return PUFFS_FLATE__STATUS_OK; + } else { + qdst -= 258; + } uint8_t* psrc = a_src.buf->ptr + a_src.buf->ri; + uint8_t* qsrc = a_src.buf->ptr + a_src.buf->wi; + if ((qsrc - psrc) < 12) { + return PUFFS_FLATE__STATUS_OK; + } else { + qsrc -= 12; + } #ifdef PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS uint64_t bits = self->private_impl.f_bits; #else @@ -115,11 +149,8 @@ puffs_flate__status c_puffs_flate__flate_decoder__decode_huffman( uint32_t dmask = ((((uint32_t)(1)) << self->private_impl.f_n_huffs_bits[1]) - 1); - // NOTE: a reminder that this loop is NOT SAFE, as it does not check that - // pdst and psrc remain within a_dst's and a_src's bounds. - outer_loop: - while (true) { + while ((pdst <= qdst) && (psrc <= qsrc)) { // Ensure that we have at least 15 bits of input. if (n_bits < 15) { #ifdef PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS @@ -151,6 +182,7 @@ outer_loop: } if ((table_entry >> 29) != 0) { // End of block. + self->private_impl.f_end_of_block = true; goto end; } if ((table_entry >> 24) != 0x10) {