mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
std/brotli: speed up qualities 0 to 9 and the decoder
Encoder (output stays byte-exact with libbrotlienc v1.2.1): - H58/H68 (q5-6): match the tags 8 at a time (SWAR, like GetMatchingTagMask's non-SSE2 code) and visit the matches with a de Bruijn ctz; inline the bucket entry check into the H5/H6 and H58/H68 loops, caching the 4 bytes compared at best_len; a dedicated H58/H68 store and hash. - find_last_distances caches the current byte at best_len. - ctz64 and a branch-free log2_floor_non_zero (de Bruijn tables), and find_match_length_with_limit uses ctz. - load32le, load64le, peek_u16, poke_u16 and poke_u32 use one bounds check each. Decoder (same output and statuses): - The literal fast path keeps pos and the counts in local variables. - Copies of at most 16 bytes, with a distance of at least 16, copy 16 bytes (like the C code; the window gap keeps the extra bytes unread). script/manual-test-brotli-float.c also checks log2_floor_non_zero (every u32) and ctz64. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UZ978BikvkegL2JUkqoB1e
This commit is contained in:
8 files changed
+716
-377
No files matched your search
@@ -125,33 +125,27 @@ pri func encoder.store_meta_block_header!(len: base.u64, is_uncompressed: base.b
|
||||
}
|
||||
}
|
||||
|
||||
// LOG2_DEBRUIJN32 maps (v * 0x07C4_ACDD) >> 27, for v = (1 << (k + 1)) - 1,
|
||||
// to k.
|
||||
pri const LOG2_DEBRUIJN32 : roarray[32] base.u32[..= 31] = [
|
||||
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
||||
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31,
|
||||
]
|
||||
|
||||
// Mirrors Log2FloorNonZero in fast_log.h, which (with BROTLI_BSR32) truncates
|
||||
// its argument to 32 bits. It returns 0 for an argument of 0.
|
||||
pri func encoder.log2_floor_non_zero(n: base.u64) base.u32[..= 31] {
|
||||
var x : base.u32
|
||||
var r : base.u32
|
||||
|
||||
// Smear the highest set bit of the low 32 bits of n rightwards, then
|
||||
// look up its position.
|
||||
x = (args.n & 0xFFFF_FFFF) as base.u32
|
||||
if x >= 0x1_0000 {
|
||||
x >>= 16
|
||||
r = 16
|
||||
}
|
||||
if x >= 0x100 {
|
||||
x >>= 8
|
||||
r ~mod+= 8
|
||||
}
|
||||
if x >= 0x10 {
|
||||
x >>= 4
|
||||
r ~mod+= 4
|
||||
}
|
||||
if x >= 4 {
|
||||
x >>= 2
|
||||
r ~mod+= 2
|
||||
}
|
||||
if x >= 2 {
|
||||
r ~mod+= 1
|
||||
}
|
||||
return r & 31
|
||||
x |= x >> 1
|
||||
x |= x >> 2
|
||||
x |= x >> 4
|
||||
x |= x >> 8
|
||||
x |= x >> 16
|
||||
return LOG2_DEBRUIJN32[(x ~mod* 0x07C4_ACDD) >> 27]
|
||||
}
|
||||
|
||||
// Mirrors FastLog2 in fast_log.h. The result is the bit pattern of an IEEE
|
||||
@@ -193,12 +187,10 @@ pri func encoder.bits_entropy_of_histogram() base.u64 {
|
||||
// code's BROTLI_UNALIGNED_LOAD64LE. The C code never reads out of bounds, so
|
||||
// the zero result for an out of bounds read should never be used.
|
||||
pri func encoder.load64le(s: roslice base.u8, i: base.u64) base.u64 {
|
||||
var q : roslice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 8 {
|
||||
return q.peek_u64le()
|
||||
if args.i < 0xFFFF_FFFF_FFFF_0000 {
|
||||
if (args.i + 8) <= args.s.length() {
|
||||
assert args.i <= (args.i + 8) via "a <= (a + b): 0 <= b"(b: 8)
|
||||
return args.s[args.i .. args.i + 8].peek_u64le()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
@@ -206,12 +198,10 @@ pri func encoder.load64le(s: roslice base.u8, i: base.u64) base.u64 {
|
||||
|
||||
// load32le is like load64le but for 4 bytes.
|
||||
pri func encoder.load32le(s: roslice base.u8, i: base.u64) base.u32 {
|
||||
var q : roslice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 4 {
|
||||
return q.peek_u32le()
|
||||
if args.i < 0xFFFF_FFFF_FFFF_0000 {
|
||||
if (args.i + 4) <= args.s.length() {
|
||||
assert args.i <= (args.i + 4) via "a <= (a + b): 0 <= b"(b: 4)
|
||||
return args.s[args.i .. args.i + 4].peek_u32le()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
@@ -235,11 +225,7 @@ pri func encoder.find_match_length_with_limit(s: roslice base.u8, i1: base.u64,
|
||||
while (n >= 8) and (a.length() >= 8) and (b.length() >= 8) {
|
||||
x = a.peek_u64le() ^ b.peek_u64le()
|
||||
if x <> 0 {
|
||||
while (x & 0xFF) == 0 {
|
||||
matched ~mod+= 1
|
||||
x >>= 8
|
||||
}
|
||||
return matched
|
||||
return matched ~mod+ (this.ctz64(x: x) >> 3)
|
||||
}
|
||||
matched ~mod+= 8
|
||||
n -= 8
|
||||
|
||||
Reference in new issue
Block a user