mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Re-organize flate Huffman decoder table bits.
Benchmarks are mixed, but it seems conceptually cleaner to arrange the bits in the order that they're examined. name old speed new speed delta puffs_flate_decode_1k/clang 125MB/s ± 0% 123MB/s ± 1% -1.91% (p=0.008 n=5+5) puffs_flate_decode_10k/clang 163MB/s ± 1% 157MB/s ± 0% -3.55% (p=0.008 n=5+5) puffs_flate_decode_100k/clang 175MB/s ± 0% 173MB/s ± 0% -0.75% (p=0.016 n=4+5) puffs_flate_decode_1k/gcc 127MB/s ± 0% 130MB/s ± 0% +2.14% (p=0.008 n=5+5) puffs_flate_decode_10k/gcc 142MB/s ± 1% 149MB/s ± 0% +4.99% (p=0.016 n=5+4) puffs_flate_decode_100k/gcc 162MB/s ± 2% 168MB/s ± 0% +3.66% (p=0.016 n=5+4)
This commit is contained in:
1 parent
cec4e30245
commit
cf6b1be5b5
5 files changed
+71
-68
No files matched your search
+22
-20
@@ -501,19 +501,21 @@ static const uint8_t puffs_flate_reverse8[256] = {
|
||||
};
|
||||
|
||||
static const uint32_t puffs_flate_lcode_magic_numbers[32] = {
|
||||
536871680, 536871936, 536872192, 536872448, 536872704, 536872960, 536873216,
|
||||
536873472, 536873744, 536874256, 536874768, 536875280, 536875808, 536876832,
|
||||
536877856, 536878880, 536879920, 536881968, 536884016, 536886064, 536888128,
|
||||
536892224, 536896320, 536900416, 536904528, 536912720, 536920912, 536929104,
|
||||
536936960, 134217728, 134217728, 134217728,
|
||||
1073742592, 1073742848, 1073743104, 1073743360, 1073743616, 1073743872,
|
||||
1073744128, 1073744384, 1073744656, 1073745168, 1073745680, 1073746192,
|
||||
1073746720, 1073747744, 1073748768, 1073749792, 1073750832, 1073752880,
|
||||
1073754928, 1073756976, 1073759040, 1073763136, 1073767232, 1073771328,
|
||||
1073775440, 1073783632, 1073791824, 1073800016, 1073807872, 134217728,
|
||||
134217728, 134217728,
|
||||
};
|
||||
|
||||
static const uint32_t puffs_flate_dcode_magic_numbers[32] = {
|
||||
536871168, 536871424, 536871680, 536871936, 536872208, 536872720, 536873248,
|
||||
536874272, 536875312, 536877360, 536879424, 536883520, 536887632, 536895824,
|
||||
536904032, 536920416, 536936816, 536969584, 537002368, 537067904, 537133456,
|
||||
537264528, 537395616, 537657760, 537919920, 538444208, 538968512, 540017088,
|
||||
541065680, 543162832, 134217728, 134217728,
|
||||
1073742080, 1073742336, 1073742592, 1073742848, 1073743120, 1073743632,
|
||||
1073744160, 1073745184, 1073746224, 1073748272, 1073750336, 1073754432,
|
||||
1073758544, 1073766736, 1073774944, 1073791328, 1073807728, 1073840496,
|
||||
1073873280, 1073938816, 1074004368, 1074135440, 1074266528, 1074528672,
|
||||
1074790832, 1075315120, 1075839424, 1076888000, 1077936592, 1080033744,
|
||||
134217728, 134217728,
|
||||
};
|
||||
|
||||
// ---------------- Private Initializer Prototypes
|
||||
@@ -1163,7 +1165,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
v_n_bits += 8;
|
||||
}
|
||||
label_1_break:;
|
||||
if ((v_table_entry >> 31) != 0) {
|
||||
if ((v_table_entry >> 28) == 1) {
|
||||
v_redir_top = ((v_table_entry >> 8) & 65535);
|
||||
v_redir_mask = ((((uint32_t)(1)) << ((v_table_entry >> 4) & 15)) - 1);
|
||||
while (true) {
|
||||
@@ -1190,13 +1192,13 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
v_n_bits += 8;
|
||||
}
|
||||
label_2_break:;
|
||||
if ((v_table_entry >> 31) != 0) {
|
||||
if ((v_table_entry >> 28) == 1) {
|
||||
status =
|
||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
if ((v_table_entry >> 30) != 0) {
|
||||
if ((v_table_entry >> 31) != 0) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
||||
if (b_wptr_dst == b_wend_dst) {
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
|
||||
@@ -1204,8 +1206,8 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
}
|
||||
*b_wptr_dst++ = ((v_table_entry >> 8) & 255);
|
||||
goto label_0_continue;
|
||||
} else if ((v_table_entry >> 30) != 0) {
|
||||
} else if ((v_table_entry >> 29) != 0) {
|
||||
} else if ((v_table_entry >> 28) != 0) {
|
||||
goto label_0_break;
|
||||
} else if ((v_table_entry >> 24) == 8) {
|
||||
status = PUFFS_FLATE_ERROR_BAD_HUFFMAN_CODE;
|
||||
@@ -1251,7 +1253,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
v_n_bits += 8;
|
||||
}
|
||||
label_3_break:;
|
||||
if ((v_table_entry >> 31) != 0) {
|
||||
if ((v_table_entry >> 28) == 1) {
|
||||
v_redir_top = ((v_table_entry >> 8) & 65535);
|
||||
v_redir_mask = ((((uint32_t)(1)) << ((v_table_entry >> 4) & 15)) - 1);
|
||||
while (true) {
|
||||
@@ -1284,7 +1286,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
if ((v_table_entry >> 24) != 32) {
|
||||
if ((v_table_entry >> 24) != 64) {
|
||||
if ((v_table_entry >> 24) == 8) {
|
||||
status = PUFFS_FLATE_ERROR_BAD_HUFFMAN_CODE;
|
||||
goto exit;
|
||||
@@ -1600,7 +1602,7 @@ puffs_flate_status puffs_flate_decoder_init_dynamic_huffman(
|
||||
v_n_bits += 8;
|
||||
}
|
||||
label_1_break:;
|
||||
if ((v_table_entry >> 24) != 64) {
|
||||
if ((v_table_entry >> 24) != 128) {
|
||||
status =
|
||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE;
|
||||
goto exit;
|
||||
@@ -1949,7 +1951,7 @@ label_1_break:;
|
||||
(((uint32_t)(puffs_flate_reverse8[v_redirect_key >> 1])) |
|
||||
((v_redirect_key & 1) << 8));
|
||||
self->private_impl.f_huffs[a_which][v_redirect_key] =
|
||||
(2147483657 | (v_top << 8) | (v_tmp << 4));
|
||||
(268435465 | (v_top << 8) | (v_tmp << 4));
|
||||
}
|
||||
}
|
||||
if ((v_key >= 512) || (v_counts[v_prev_cl] <= 0)) {
|
||||
@@ -1963,9 +1965,9 @@ label_1_break:;
|
||||
v_reversed_key >>= (9 - v_cl);
|
||||
v_symbol = ((uint32_t)(v_symbols[v_i]));
|
||||
if (v_symbol == 256) {
|
||||
v_value = (268435456 | v_cl);
|
||||
v_value = (536870912 | v_cl);
|
||||
} else if ((v_symbol < 256) && (a_which == 0)) {
|
||||
v_value = (1073741824 | (v_symbol << 8) | v_cl);
|
||||
v_value = (2147483648 | (v_symbol << 8) | v_cl);
|
||||
} else if (v_symbol >= a_base_symbol) {
|
||||
v_symbol -= a_base_symbol;
|
||||
if (a_which == 0) {
|
||||
|
||||
@@ -30,7 +30,7 @@ func main1() error {
|
||||
for j := 0; j < 32; j++ {
|
||||
x := uint32(0x08000000)
|
||||
if baseNumbers[i][j] != bad {
|
||||
x = 0x20000000 | (baseNumbers[i][j] << 8) | (extraBits[i][j] << 4)
|
||||
x = 0x40000000 | (baseNumbers[i][j] << 8) | (extraBits[i][j] << 4)
|
||||
}
|
||||
fmt.Printf("0x%08X,", x)
|
||||
if j&7 == 7 {
|
||||
|
||||
@@ -128,19 +128,20 @@ outer_loop:
|
||||
uint32_t n = table_entry & 0x0F;
|
||||
bits >>= n;
|
||||
n_bits -= n;
|
||||
if ((table_entry >> 30) == 1) {
|
||||
if ((table_entry >> 31) != 0) {
|
||||
// Literal.
|
||||
*pdst++ = (uint8_t)(table_entry >> 8);
|
||||
goto outer_loop;
|
||||
}
|
||||
if ((table_entry >> 29) == 1) {
|
||||
// Back-reference.
|
||||
if ((table_entry >> 30) != 0) {
|
||||
// Back-reference; length = base number + extra bits.
|
||||
break;
|
||||
}
|
||||
if ((table_entry >> 28) == 1) {
|
||||
if ((table_entry >> 29) != 0) {
|
||||
// End of block.
|
||||
goto end;
|
||||
}
|
||||
if ((table_entry >> 24) != 0x80) {
|
||||
if ((table_entry >> 24) != 0x10) {
|
||||
status =
|
||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE;
|
||||
goto end;
|
||||
@@ -191,11 +192,11 @@ outer_loop:
|
||||
uint32_t n = table_entry & 0x0F;
|
||||
bits >>= n;
|
||||
n_bits -= n;
|
||||
if ((table_entry >> 29) == 1) {
|
||||
// Back-reference.
|
||||
if ((table_entry >> 30) != 0) {
|
||||
// Back-reference; distance = base number + extra bits.
|
||||
break;
|
||||
}
|
||||
if ((table_entry >> 24) != 0x80) {
|
||||
if ((table_entry >> 24) != 0x10) {
|
||||
status =
|
||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE;
|
||||
goto end;
|
||||
|
||||
@@ -23,23 +23,23 @@ pub error "no Huffman codes"
|
||||
// The next two tables were created by script/print-flate-magic-numbers.go.
|
||||
//
|
||||
// The u32 values' meanings are the same as the decoder.huffs u32 values. In
|
||||
// particular, bit 29 indicates a base number + extra bits, bits 23-8 are the
|
||||
// particular, bit 30 indicates a base number + extra bits, bits 23-8 are the
|
||||
// base number and bits 7-4 are the number of those extra bits.
|
||||
//
|
||||
// Some trailing elements are 0x08000000. Bit 27 indicates an invalid value.
|
||||
|
||||
pri const lcode_magic_numbers[32] u32 = $(
|
||||
0x20000300, 0x20000400, 0x20000500, 0x20000600, 0x20000700, 0x20000800, 0x20000900, 0x20000A00,
|
||||
0x20000B10, 0x20000D10, 0x20000F10, 0x20001110, 0x20001320, 0x20001720, 0x20001B20, 0x20001F20,
|
||||
0x20002330, 0x20002B30, 0x20003330, 0x20003B30, 0x20004340, 0x20005340, 0x20006340, 0x20007340,
|
||||
0x20008350, 0x2000A350, 0x2000C350, 0x2000E350, 0x20010200, 0x08000000, 0x08000000, 0x08000000,
|
||||
0x40000300, 0x40000400, 0x40000500, 0x40000600, 0x40000700, 0x40000800, 0x40000900, 0x40000A00,
|
||||
0x40000B10, 0x40000D10, 0x40000F10, 0x40001110, 0x40001320, 0x40001720, 0x40001B20, 0x40001F20,
|
||||
0x40002330, 0x40002B30, 0x40003330, 0x40003B30, 0x40004340, 0x40005340, 0x40006340, 0x40007340,
|
||||
0x40008350, 0x4000A350, 0x4000C350, 0x4000E350, 0x40010200, 0x08000000, 0x08000000, 0x08000000,
|
||||
)
|
||||
|
||||
pri const dcode_magic_numbers[32] u32 = $(
|
||||
0x20000100, 0x20000200, 0x20000300, 0x20000400, 0x20000510, 0x20000710, 0x20000920, 0x20000D20,
|
||||
0x20001130, 0x20001930, 0x20002140, 0x20003140, 0x20004150, 0x20006150, 0x20008160, 0x2000C160,
|
||||
0x20010170, 0x20018170, 0x20020180, 0x20030180, 0x20040190, 0x20060190, 0x200801A0, 0x200C01A0,
|
||||
0x201001B0, 0x201801B0, 0x202001C0, 0x203001C0, 0x204001D0, 0x206001D0, 0x08000000, 0x08000000,
|
||||
0x40000100, 0x40000200, 0x40000300, 0x40000400, 0x40000510, 0x40000710, 0x40000920, 0x40000D20,
|
||||
0x40001130, 0x40001930, 0x40002140, 0x40003140, 0x40004150, 0x40006150, 0x40008160, 0x4000C160,
|
||||
0x40010170, 0x40018170, 0x40020180, 0x40030180, 0x40040190, 0x40060190, 0x400801A0, 0x400C01A0,
|
||||
0x401001B0, 0x401801B0, 0x402001C0, 0x403001C0, 0x404001D0, 0x406001D0, 0x08000000, 0x08000000,
|
||||
)
|
||||
|
||||
pub struct decoder?(
|
||||
@@ -66,10 +66,10 @@ pub struct decoder?(
|
||||
// less, then more source bytes are read and the table lookup re-tried.
|
||||
//
|
||||
// The table value is:
|
||||
// - bit 31 indicates a redirect to another part of the table.
|
||||
// - bit 30 indicates the symbol is a literal.
|
||||
// - bit 29 indicates the symbol is a base number + extra bits.
|
||||
// - bit 28 indicates the symbol is the end-of-block.
|
||||
// - bit 31 indicates the symbol is a literal.
|
||||
// - bit 30 indicates the symbol is a base number + extra bits.
|
||||
// - bit 29 indicates the symbol is the end-of-block.
|
||||
// - bit 28 indicates a redirect to another part of the table.
|
||||
// - bit 27 indicates an invalid value.
|
||||
// - bits 26-24 are zero.
|
||||
// - bits 23- 8 is the redirect offset, literal (in bits 15-8) or base number.
|
||||
@@ -219,7 +219,7 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() {
|
||||
n_bits += 8
|
||||
}
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 31) != 0 {
|
||||
if (table_entry >> 28) == 1 {
|
||||
var redir_top u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
|
||||
var redir_mask u32[..0x7FFF] = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
@@ -237,18 +237,18 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() {
|
||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
if (table_entry >> 31) != 0 {
|
||||
if (table_entry >> 28) == 1 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
}
|
||||
|
||||
if (table_entry >> 30) != 0 {
|
||||
if (table_entry >> 31) != 0 {
|
||||
// Literal.
|
||||
in.dst.write_u8?(x:(table_entry >> 8) & 0xFF)
|
||||
continue
|
||||
} else if (table_entry >> 29) != 0 {
|
||||
} else if (table_entry >> 30) != 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 28) != 0 {
|
||||
} else if (table_entry >> 29) != 0 {
|
||||
// End of block.
|
||||
break
|
||||
} else if (table_entry >> 24) == 0x08 {
|
||||
@@ -290,7 +290,7 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() {
|
||||
n_bits += 8
|
||||
}
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 31) != 0 {
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
@@ -314,7 +314,7 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() {
|
||||
}
|
||||
|
||||
// For H-D, all symbols should be base number + extra bits.
|
||||
if (table_entry >> 24) != 0x20 {
|
||||
if (table_entry >> 24) != 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return error "bad Huffman code"
|
||||
}
|
||||
@@ -447,7 +447,7 @@ pri func decoder.init_dynamic_huffman?(src reader1)() {
|
||||
}
|
||||
// For H-CL, there should be no redirections and all symbols should be
|
||||
// literals.
|
||||
if (table_entry >> 24) != 0x40 {
|
||||
if (table_entry >> 24) != 0x80 {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
table_entry = (table_entry >> 8) & 0xFF
|
||||
@@ -706,16 +706,16 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
// Calculate this.huffs[in.which].
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - this.huffs[0][0b..000] = 0x40000003 (literal, symbols[0]=0x00, code_length=3)
|
||||
// - this.huffs[0][0b..100] = 0x40000403 (literal, symbols[1]=0x04, code_length=3)
|
||||
// - this.huffs[0][0b..010] = 0x40000503 (literal, symbols[2]=0x05, code_length=3)
|
||||
// - this.huffs[0][0b..110] = 0x40000603 (literal, symbols[3]=0x06, code_length=3)
|
||||
// - this.huffs[0][0b..001] = 0x40000703 (literal, symbols[4]=0x07, code_length=3)
|
||||
// - this.huffs[0][0b..101] = 0x40000803 (literal, symbols[5]=0x08, code_length=3)
|
||||
// - this.huffs[0][0b..011] = 0x40000903 (literal, symbols[6]=0x09, code_length=3)
|
||||
// - this.huffs[0][0b.0111] = 0x40001104 (literal, symbols[7]=0x11, code_length=4)
|
||||
// - this.huffs[0][0b01111] = 0x40000305 (literal, symbols[8]=0x03, code_length=5)
|
||||
// - this.huffs[0][0b11111] = 0x40001805 (literal, symbols[9]=0x18, code_length=5)
|
||||
// - this.huffs[0][0b..000] = 0x80000003 (literal, symbols[0]=0x00, code_length=3)
|
||||
// - this.huffs[0][0b..100] = 0x80000403 (literal, symbols[1]=0x04, code_length=3)
|
||||
// - this.huffs[0][0b..010] = 0x80000503 (literal, symbols[2]=0x05, code_length=3)
|
||||
// - this.huffs[0][0b..110] = 0x80000603 (literal, symbols[3]=0x06, code_length=3)
|
||||
// - this.huffs[0][0b..001] = 0x80000703 (literal, symbols[4]=0x07, code_length=3)
|
||||
// - this.huffs[0][0b..101] = 0x80000803 (literal, symbols[5]=0x08, code_length=3)
|
||||
// - this.huffs[0][0b..011] = 0x80000903 (literal, symbols[6]=0x09, code_length=3)
|
||||
// - this.huffs[0][0b.0111] = 0x80001104 (literal, symbols[7]=0x11, code_length=4)
|
||||
// - this.huffs[0][0b01111] = 0x80000305 (literal, symbols[8]=0x03, code_length=5)
|
||||
// - this.huffs[0][0b11111] = 0x80001805 (literal, symbols[9]=0x18, code_length=5)
|
||||
i = 0
|
||||
if (n_symbols != (offsets[max_cl] as u32)) or (n_symbols != (offsets[15] as u32)) {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
@@ -800,7 +800,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
next_top = top + ((1 as u32) << tmp)
|
||||
|
||||
redirect_key = (reverse8[redirect_key >> 1] as u32) | ((redirect_key & 1) << 8)
|
||||
this.huffs[in.which][redirect_key] = 0x80000009 | (top << 8) | (tmp << 4)
|
||||
this.huffs[in.which][redirect_key] = 0x10000009 | (top << 8) | (tmp << 4)
|
||||
}
|
||||
}
|
||||
if (key >= (1 << 9)) or (counts[prev_cl] <= 0) {
|
||||
@@ -814,10 +814,10 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
|
||||
var symbol u32[..319] = symbols[i] as u32
|
||||
if symbol == 256 {
|
||||
// End-of-block.
|
||||
value = 0x10000000 | cl
|
||||
value = 0x20000000 | cl
|
||||
} else if (symbol < 256) and (in.which == 0) {
|
||||
// Literal.
|
||||
value = 0x40000000 | (symbol << 8) | cl
|
||||
value = 0x80000000 | (symbol << 8) | cl
|
||||
} else if symbol >= in.base_symbol {
|
||||
// Base number + extra bits.
|
||||
symbol -= in.base_symbol
|
||||
|
||||
+5
-5
@@ -463,19 +463,19 @@ void test_puffs_flate_table_redirect() {
|
||||
uint32_t got;
|
||||
uint32_t want;
|
||||
got = dec.private_impl.f_huffs[0][0x017F];
|
||||
want = 0x80020039;
|
||||
want = 0x10020039;
|
||||
if (got != want) {
|
||||
FAIL("huffs[0][0x017F]: got 0x%08" PRIX32 ", want 0x%08" PRIX32, got, want);
|
||||
return;
|
||||
}
|
||||
got = dec.private_impl.f_huffs[0][0x00FF];
|
||||
want = 0x80020839;
|
||||
want = 0x10020839;
|
||||
if (got != want) {
|
||||
FAIL("huffs[0][0x00FF]: got 0x%08" PRIX32 ", want 0x%08" PRIX32, got, want);
|
||||
return;
|
||||
}
|
||||
got = dec.private_impl.f_huffs[0][0x01FF];
|
||||
want = 0x80021049;
|
||||
want = 0x10021049;
|
||||
if (got != want) {
|
||||
FAIL("huffs[0][0x01FF]: got 0x%08" PRIX32 ", want 0x%08" PRIX32, got, want);
|
||||
return;
|
||||
@@ -483,8 +483,8 @@ void test_puffs_flate_table_redirect() {
|
||||
|
||||
// The first 2nd-level table should look like wants.
|
||||
const uint32_t wants[8] = {
|
||||
0x40000801, 0x40000903, 0x40000801, 0x40000B03,
|
||||
0x40000801, 0x40000A03, 0x40000801, 0x40000C03,
|
||||
0x80000801, 0x80000903, 0x80000801, 0x80000B03,
|
||||
0x80000801, 0x80000A03, 0x80000801, 0x80000C03,
|
||||
};
|
||||
for (i = 0; i < 8; i++) {
|
||||
got = dec.private_impl.f_huffs[0][0x0200 + i];
|
||||
|
||||
Reference in new issue
Block a user