From fa89c397b0c8c1466e6e41978b2895835f711d68 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 3 Aug 2017 10:47:44 +1000 Subject: [PATCH] Do one 8-byte load instead of six 1-byte loads. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta puffs_flate_decode_1k/clang 178MB/s ± 2% 188MB/s ± 0% +5.98% (p=0.008 n=5+5) puffs_flate_decode_10k/clang 235MB/s ± 1% 242MB/s ± 2% +2.69% (p=0.032 n=5+5) puffs_flate_decode_100k/clang 274MB/s ± 0% 273MB/s ± 1% ~ (p=0.690 n=5+5) puffs_flate_decode_1k/gcc 177MB/s ± 0% 187MB/s ± 0% +5.44% (p=0.008 n=5+5) puffs_flate_decode_10k/gcc 234MB/s ± 0% 242MB/s ± 0% +3.64% (p=0.008 n=5+5) puffs_flate_decode_100k/gcc 275MB/s ± 0% 288MB/s ± 1% +4.68% (p=0.008 n=5+5) Note that these are benchmark numbers for an 'ideal', hand-written C implementation, not for Puffs' generated C code. --- script/puffs-flate-decoder-decode-huffman.c | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/script/puffs-flate-decoder-decode-huffman.c b/script/puffs-flate-decoder-decode-huffman.c index 1ff351ad..fbcc221e 100644 --- a/script/puffs-flate-decoder-decode-huffman.c +++ b/script/puffs-flate-decoder-decode-huffman.c @@ -73,6 +73,10 @@ #include #include // For manual printf debugging. +#if __x86_64__ +#define PUFFS_FLATE_HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS +#endif + puffs_flate_status c_puffs_flate_decoder_decode_huffman( puffs_flate_decoder* self, puffs_base_writer1 a_dst, @@ -85,7 +89,11 @@ puffs_flate_status c_puffs_flate_decoder_decode_huffman( // Load contextual state. uint8_t* pdst = a_dst.buf->ptr + a_dst.buf->wi; uint8_t* psrc = a_src.buf->ptr + a_src.buf->ri; +#ifdef PUFFS_FLATE_HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS + uint64_t bits = self->private_impl.f_bits; +#else uint32_t bits = self->private_impl.f_bits; +#endif uint32_t n_bits = self->private_impl.f_n_bits; // Initialize other local variables. @@ -102,10 +110,16 @@ outer_loop: while (true) { // Ensure that we have at least 15 bits of input. if (n_bits < 15) { +#ifdef PUFFS_FLATE_HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS + bits |= *((uint64_t*)psrc) << n_bits; + psrc += 6; + n_bits += 48; +#else bits |= (((uint32_t)(*psrc++)) << n_bits); n_bits += 8; bits |= (((uint32_t)(*psrc++)) << n_bits); n_bits += 8; +#endif } // Decode an lcode symbol from H-L. @@ -142,8 +156,14 @@ outer_loop: uint32_t n = (table_entry >> 4) & 0x0F; if (n) { if (n_bits < n) { +#ifdef PUFFS_FLATE_HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS + bits |= *((uint64_t*)psrc) << n_bits; + psrc += 6; + n_bits += 48; +#else bits |= (((uint32_t)(*psrc++)) << n_bits); n_bits += 8; +#endif } length += bits & ((((uint32_t)(1)) << n) - 1); bits >>= n; @@ -153,10 +173,16 @@ outer_loop: // Ensure that we have at least 15 bits of input. if (n_bits < 15) { +#ifdef PUFFS_FLATE_HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS + bits |= *((uint64_t*)psrc) << n_bits; + psrc += 6; + n_bits += 48; +#else bits |= (((uint32_t)(*psrc++)) << n_bits); n_bits += 8; bits |= (((uint32_t)(*psrc++)) << n_bits); n_bits += 8; +#endif } // Decode a dcode symbol from H-D. @@ -185,10 +211,16 @@ outer_loop: uint32_t n = (table_entry >> 4) & 0x0F; if (n) { if (n_bits < 15) { +#ifdef PUFFS_FLATE_HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS + bits |= *((uint64_t*)psrc) << n_bits; + psrc += 6; + n_bits += 48; +#else bits |= (((uint32_t)(*psrc++)) << n_bits); n_bits += 8; bits |= (((uint32_t)(*psrc++)) << n_bits); n_bits += 8; +#endif } distance += bits & ((((uint32_t)(1)) << n) - 1); bits >>= n;