mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 16:47:28 +01:00
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:
16 files changed
+2650
-400
No files matched your search
@@ -553,12 +553,12 @@ pri func encoder.estimate_entropy(off: base.u32, size: base.u32) base.u64 {
|
||||
while i < args.size {
|
||||
p = this.ctx_histo[(args.off ~mod+ i) & 511]
|
||||
total ~mod+= p as base.u64
|
||||
result = this.sf.f64_add(a: result, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u32(a: p), b: this.bc.fast_log2(v: p as base.u64)))
|
||||
result = this.bc.f64_add(a: result, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u32(a: p), b: this.bc.fast_log2(v: p as base.u64)))
|
||||
i ~mod+= 1
|
||||
}
|
||||
return this.sf.f64_sub(a: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: total), b: this.bc.fast_log2(v: total)), b: result)
|
||||
return this.bc.f64_sub(a: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: total), b: this.bc.fast_log2(v: total)), b: result)
|
||||
}
|
||||
|
||||
// Mirrors ChooseContextMap in encode.c. The bigram_histo is
|
||||
@@ -585,30 +585,30 @@ pri func encoder.choose_context_map!() base.u32 {
|
||||
i ~mod+= 1
|
||||
}
|
||||
e1 = this.estimate_entropy(off: 16, size: 3)
|
||||
e2 = this.sf.f64_add(a: this.estimate_entropy(off: 24, size: 3), b: this.estimate_entropy(off: 27, size: 3))
|
||||
e2 = this.bc.f64_add(a: this.estimate_entropy(off: 24, size: 3), b: this.estimate_entropy(off: 27, size: 3))
|
||||
e3 = 0
|
||||
i = 0
|
||||
while i < 3 {
|
||||
e3 = this.sf.f64_add(a: e3, b: this.estimate_entropy(off: 3 * i, size: 3))
|
||||
e3 = this.bc.f64_add(a: e3, b: this.estimate_entropy(off: 3 * i, size: 3))
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
total = (this.ctx_histo[16] ~mod+ this.ctx_histo[17]) ~mod+ this.ctx_histo[18]
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.sf.f64_from_u32(a: total))
|
||||
e1 = this.sf.f64_mul(a: e1, b: e0)
|
||||
e2 = this.sf.f64_mul(a: e2, b: e0)
|
||||
e3 = this.sf.f64_mul(a: e3, b: e0)
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.bc.f64_from_u32(a: total))
|
||||
e1 = this.bc.f64_mul(a: e1, b: e0)
|
||||
e2 = this.bc.f64_mul(a: e2, b: e0)
|
||||
e3 = this.bc.f64_mul(a: e3, b: e0)
|
||||
|
||||
if this.quality < 7 {
|
||||
// 3 context models is a bit slower, don't use it at lower qualities.
|
||||
e3 = this.sf.f64_mul(a: e1, b: F64_10)
|
||||
e3 = this.bc.f64_mul(a: e1, b: F64_10)
|
||||
}
|
||||
// If expected savings by symbol are less than 0.2 bits, skip the context
|
||||
// modeling -- in exchange for faster decoding speed.
|
||||
if (this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) and
|
||||
(this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e3), b: F64_0_2) <> 0) {
|
||||
if (this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) and
|
||||
(this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e3), b: F64_0_2) <> 0) {
|
||||
return 1
|
||||
} else if this.sf.f64_lt(a: this.sf.f64_sub(a: e2, b: e3), b: F64_0_02) <> 0 {
|
||||
} else if this.bc.f64_lt(a: this.bc.f64_sub(a: e2, b: e3), b: F64_0_02) <> 0 {
|
||||
return 2
|
||||
}
|
||||
return 3
|
||||
@@ -668,16 +668,16 @@ pri func encoder.should_use_complex_static_context_map!(wb: roslice base.u8, sta
|
||||
e2 = 0
|
||||
i = 0
|
||||
while i < 13 {
|
||||
e2 = this.sf.f64_add(a: e2, b: this.estimate_entropy(off: 32 + (i << 5), size: 32))
|
||||
e2 = this.bc.f64_add(a: e2, b: this.estimate_entropy(off: 32 + (i << 5), size: 32))
|
||||
i ~mod+= 1
|
||||
}
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.sf.f64_from_u32(a: total))
|
||||
e1 = this.sf.f64_mul(a: e1, b: e0)
|
||||
e2 = this.sf.f64_mul(a: e2, b: e0)
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.bc.f64_from_u32(a: total))
|
||||
e1 = this.bc.f64_mul(a: e1, b: e0)
|
||||
e2 = this.bc.f64_mul(a: e2, b: e0)
|
||||
// The triggering heuristics below were tuned by compressing the
|
||||
// individual files of the silesia corpus.
|
||||
if (this.sf.f64_gt(a: e2, b: F64_3) <> 0) or
|
||||
(this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) {
|
||||
if (this.bc.f64_gt(a: e2, b: F64_3) <> 0) or
|
||||
(this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) {
|
||||
return 0
|
||||
}
|
||||
return 13
|
||||
@@ -748,16 +748,16 @@ pri func encoder.should_compress!(wb: roslice base.u8, last_flush_pos: base.u64,
|
||||
return false
|
||||
}
|
||||
if args.num_commands < ((args.bytes >> 8) + 2) {
|
||||
if this.sf.f64_gt(
|
||||
a: this.sf.f64_from_u64(a: args.num_literals),
|
||||
b: this.sf.f64_mul(a: F64_0_99, b: this.sf.f64_from_u64(a: args.bytes))) <> 0 {
|
||||
if this.bc.f64_gt(
|
||||
a: this.bc.f64_from_u64(a: args.num_literals),
|
||||
b: this.bc.f64_mul(a: F64_0_99, b: this.bc.f64_from_u64(a: args.bytes))) <> 0 {
|
||||
i = 0
|
||||
while i < 256 {
|
||||
this.ctx_histo[i] = 0
|
||||
i ~mod+= 1
|
||||
}
|
||||
bit_cost_threshold = this.sf.f64_mul(
|
||||
a: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.bytes), b: F64_7_92),
|
||||
bit_cost_threshold = this.bc.f64_mul(
|
||||
a: this.bc.f64_mul(a: this.bc.f64_from_u64(a: args.bytes), b: F64_7_92),
|
||||
b: F64_INV_13)
|
||||
t = (args.bytes ~mod+ 12) / 13
|
||||
pos = (args.last_flush_pos & 0xFFFF_FFFF) as base.u32
|
||||
@@ -767,7 +767,7 @@ pri func encoder.should_compress!(wb: roslice base.u8, last_flush_pos: base.u64,
|
||||
pos ~mod+= 13
|
||||
i ~mod+= 1
|
||||
}
|
||||
if this.sf.f64_gt(a: this.ctx_histo_bits_entropy(), b: bit_cost_threshold) <> 0 {
|
||||
if this.bc.f64_gt(a: this.ctx_histo_bits_entropy(), b: bit_cost_threshold) <> 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -787,16 +787,16 @@ pri func encoder.ctx_histo_bits_entropy() base.u64 {
|
||||
while i < 256 {
|
||||
p = this.ctx_histo[i] as base.u64
|
||||
sum ~mod+= p
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: this.bc.fast_log2(v: p)))
|
||||
retval = this.bc.f64_sub(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: p), b: this.bc.fast_log2(v: p)))
|
||||
i ~mod+= 1
|
||||
}
|
||||
if sum <> 0 {
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: this.bc.fast_log2(v: sum)))
|
||||
retval = this.bc.f64_add(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: sum), b: this.bc.fast_log2(v: sum)))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.bc.f64_lt(a: retval, b: this.bc.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.bc.f64_from_u64(a: sum)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user