std/brotli: speed up the encoder at qualities 10 and 11, byte-exactly

- encode_float.wuffs: package-private fast paths for the f32/f64 add, sub,
  mul, compare and int conversion calls in the encoder's hot loops. Calling
  std/softfloat is a cross-package public call (a receiver check, never
  inlined). Each fast path returns exactly the softfloat result and falls
  back to it for the uncommon cases. script/manual-test-brotli-float.c
  cross-checks them against hardware float and double arithmetic.
- Zopfli update_nodes: compute a length's cost only when its copy code
  changes, hoist the distance cache values and the continuation byte, and
  use a table for small int to f32 conversions.
- FindBlocks: memoize (block_switch_bitcost + insert_cost) for capped costs
  and skip the subtraction for costs at or above a per-byte threshold
  (S(c) = c - min_cost is monotone, so such costs would be capped anyway).
- BrotliPopulationCost: per-call memo of each count's term and depth, and
  skip the zero terms of the entropy sums.
- std/softfloat: a 64-bit f32_add_mags.

Output is unchanged (mimic tests on gcc and clang, parity sweeps).

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-27 23:21:31 +00:00
1 parent 8ac9e2fd4c
commit 044437a8ec
16 files changed
+2650 -400

No files matched your search

+31 -40
View File
@@ -172,50 +172,41 @@ pub func arith.f32_mul(a: base.u32, b: base.u32) base.u32 {
// f32_add_mags returns (sign(a) * (|a| + |b|)), for normal a and b.
pri func arith.f32_add_mags(a: base.u32, b: base.u32) base.u32 {
var ea : base.u32[..= 0x100]
var eb : base.u32[..= 0xFF]
var ma : base.u32[..= 0x3FFF_FFC0]
var mb : base.u32[..= 0x3FFF_FFC0]
var d : base.u32[..= 0xFF]
var x : base.u32
var y : base.u32
var ex : base.u32[..= 0xFF]
var d : base.u32
var s : base.u64
var z : base.u32
var rb : base.u32
ea = (args.a >> 23) & 0xFF
eb = (args.b >> 23) & 0xFF
// The significands, with their implicit bit at bit 29 and 6 extra bits
// of precision at the bottom.
ma = ((args.a & 0x7F_FFFF) | 0x80_0000) << 6
mb = ((args.b & 0x7F_FFFF) | 0x80_0000) << 6
if ea < eb {
d = (eb ~mod- ea) & 0xFF
ea = eb
z = ma
ma = mb
mb = z
// Order the magnitudes so that |x| >= |y|.
x = args.a & F32_ABS
y = args.b & F32_ABS
if y > x {
x = args.b & F32_ABS
y = args.a & F32_ABS
}
ex = (x >> 23) & 0xFF
d = ex ~mod- ((y >> 23) & 0xFF)
if d >= 25 {
// |y| is less than half an ULP of |x|, so the sum rounds to x.
return (args.a & F32_SIGN) | x
}
// The significands, with their implicit bit at bit 62. Shifting y's
// right by at most 24 is exact, and the sum is less than (1 << 64).
s = ((((x & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) ~mod+
(((((y & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31))
// Round to nearest, ties to even: add just under half an ULP, plus one
// if the truncated result is odd. The result is in [1 << 23 ..= 1 << 24]
// and adding it (whose bit 23 is the implicit bit) to the shifted
// exponent carries any rounding overflow into the exponent field.
if s >= 0x8000_0000_0000_0000 {
s = (s ~mod+ (0x7F_FFFF_FFFF ~mod+ ((s >> 40) & 1))) >> 40
z = (ex ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
} else {
d = (ea ~mod- eb) & 0xFF
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
z = ((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
}
if d >= 31 {
mb = 1
} else if d > 0 {
if (mb ~mod<< (32 - d)) <> 0 {
mb = (mb >> d) | 1
} else {
mb = mb >> d
}
}
z = (ma & 0x3FFF_FFFF) + (mb & 0x3FFF_FFFF)
if z >= 0x4000_0000 {
ea += 1
z = (z >> 1) | (z & 1)
}
rb = z & 0x3F
z = (z ~mod+ 0x20) >> 6
if rb == 0x20 {
z &= 0xFFFF_FFFE
}
// ea is in [1 ..= 255] and z is in [1 << 23 ..= 1 << 24].
z = ((ea ~mod- 1) ~mod<< 23) ~mod+ z
if z >= F32_INF {
z = F32_INF
}