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:
Claude committed 2026-10-28 08:50:15 +00:00
1 parent 044437a8ec
commit fbae955a74
8 files changed
+716 -377

No files matched your search

+8 -8
View File
@@ -283,16 +283,16 @@ pri func metablock_builder.h_population_cost!(wb: roslice base.u8, h: base.u64,
// Mirrors Log2FloorNonZero in fast_log.h (which truncates n to 32 bits).
pri func metablock_builder.log2_floor_non_zero(n: base.u64) base.u32[..= 31] {
var x : base.u32
var r : base.u32[..= 31]
// 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
while x > 1 {
x >>= 1
if r < 31 {
r += 1
}
}
return r
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
return LOG2_DEBRUIJN32[(x ~mod* 0x07C4_ACDD) >> 27]
}
// Mirrors PrefixEncodeCopyDistance in prefix.h. It returns the code (the