Fix hand-written decode_huffman_fast C impl.

This commit is contained in:
Nigel Tao committed 2017-11-08 10:12:47 +11:00
1 parent 848d66ffdf
commit 7ccb9234a4
1 file changed
+62 -30
+62 -30
View File
@@ -34,13 +34,12 @@
// //
// #include "../../../script/puffs-flate-decoder-decode-huffman.c" // #include "../../../script/puffs-flate-decoder-decode-huffman.c"
// //
// Then find the call to puffs_flate_decoder_decode_huffman. It should be // Then find the call to puffs_flate__flate_decoder__decode_huffman_fast. It
// inside the puffs_flate_decoder_decode function body, and the line of code // should be inside the puffs_flate__flate_decoder__decode_blocks function body,
// should look something like // and the lines of code should look something like
// //
// status = puffs_flate_decoder_decode_huffman(self, a_dst, a_src); // status =
// // puffs_flate__flate_decoder__decode_huffman_fast(self, a_dst, a_src);
// TODO: "puffs_flate_decoder" should be "puffs_flate__flate_decoder".
// //
// Change the "puffs" to "c_puffs", i.e. add a "c_" prefix. The net result // Change the "puffs" to "c_puffs", i.e. add a "c_" prefix. The net result
// should look something like: // should look something like:
@@ -49,58 +48,93 @@
// //
// $ git diff gen/c/std/flate.c // $ git diff gen/c/std/flate.c
// diff --git a/gen/c/std/flate.c b/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 // --- a/gen/c/std/flate.c
// +++ b/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. // // C HEADER ENDS HERE.
//
// +#include "../../../script/puffs-flate-decoder-decode-huffman.c" // +#include "../../../script/puffs-flate-decoder-decode-huffman.c"
// + //
// #ifndef PUFFS_BASE_IMPL_H // #ifndef PUFFS_BASE_IMPL_H
// #define PUFFS_BASE_IMPL_H // #define PUFFS_BASE_IMPL_H
// // @@ -1119,7 +1120,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_blocks(
// @@ -1063,7 +1065,7 @@ puffs_flate__status
// puffs_flate__flate_decoder__decode_blocks(
// }
// } // }
// } // }
// - status = puffs_flate__flate_decoder__decode_huffman(self, a_dst, a_src); // status =
// + status = c_puffs_flate__flate_decoder__decode_huffman(self, a_dst, a_src); // - 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) { // if (a_src.buf) {
// b_rptr_src = a_src.buf->ptr + a_src.buf->ri; // 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 // That concludes the two edits to gen/c/std/flate.c. Run the tests and
// benchmark programs directly, instead of running "puffs test" or "puffs // benchmarks with the "-skipgen" flag, otherwise the "puffs" tool will
// bench", since the "puffs" tool will re-generate the C code and override your // re-generate the C code and override your gen/c/std/flate.c edit:
// gen/c/std/flate.c edit.
// //
// cd test/c/std // puffs test -skipgen std/flate
// gcc -O3 flate.c && ./a.out && ./a.out -bench; rm -f ./a.out // 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 <stddef.h> #include <stddef.h>
#include <stdio.h> // For manual printf debugging. #include <stdio.h> // 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__ #ifdef __x86_64__
#define PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS //#define PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS
#endif #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_flate__flate_decoder* self,
puffs_base__writer1 a_dst, puffs_base__writer1 a_dst,
puffs_base__reader1 a_src) { 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) { if (!a_dst.buf || !a_src.buf) {
return PUFFS_FLATE__ERROR_BAD_ARGUMENT; return PUFFS_FLATE__ERROR_BAD_ARGUMENT;
} }
puffs_flate__status status = PUFFS_FLATE__STATUS_OK; 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* 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* 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 #ifdef PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS
uint64_t bits = self->private_impl.f_bits; uint64_t bits = self->private_impl.f_bits;
#else #else
@@ -115,11 +149,8 @@ puffs_flate__status c_puffs_flate__flate_decoder__decode_huffman(
uint32_t dmask = uint32_t dmask =
((((uint32_t)(1)) << self->private_impl.f_n_huffs_bits[1]) - 1); ((((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: outer_loop:
while (true) { while ((pdst <= qdst) && (psrc <= qsrc)) {
// Ensure that we have at least 15 bits of input. // Ensure that we have at least 15 bits of input.
if (n_bits < 15) { if (n_bits < 15) {
#ifdef PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS #ifdef PUFFS_FLATE__HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS
@@ -151,6 +182,7 @@ outer_loop:
} }
if ((table_entry >> 29) != 0) { if ((table_entry >> 29) != 0) {
// End of block. // End of block.
self->private_impl.f_end_of_block = true;
goto end; goto end;
} }
if ((table_entry >> 24) != 0x10) { if ((table_entry >> 24) != 0x10) {