mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
std/brotli: port bit_cost.c and histogram.c for the encoder
Add the bit_cost struct (encode_bit_cost.wuffs, encode_histogram.wuffs):
BrotliBitsEntropy, BrotliPopulationCost{Literal,Command,Distance},
FastLog2, the histogram helpers and BrotliBuildHistogramsWithContext,
with doubles computed by std/softfloat in the C code's operation order.
Histograms and commands are little-endian byte slices with the C structs'
memory layout.
Add a test-only metablock_tester struct and the
script/manual-test-brotli-metablock-parity.c harness, which compares
BitsEntropy and PopulationCost* bit for bit with libbrotlienc v1.2.1
(200000 random histograms: 0 mismatches).
This commit is contained in:
6 files changed
+1948
No files matched your search
@@ -189,6 +189,15 @@ The source files are:
|
||||
- `encode_fragment_fast.wuffs`: `compress_fragment.c` (quality 0).
|
||||
- `encode_fragment_two_pass.wuffs`: `compress_fragment_two_pass.c` (quality
|
||||
1).
|
||||
- `encode_bit_cost.wuffs`: `bit_cost.c`, `bit_cost_inc.h` and `fast_log.h`
|
||||
(`BrotliBitsEntropy`, `BrotliPopulationCost{Literal,Command,Distance}`), as
|
||||
methods of the `bit_cost` struct.
|
||||
- `encode_histogram.wuffs`: `histogram.h`, `histogram_inc.h` and
|
||||
`histogram.c` (histogram helpers and `BrotliBuildHistogramsWithContext`),
|
||||
also as `bit_cost` methods.
|
||||
- `encode_tester.wuffs`: TEST ONLY. The `metablock_tester` struct exposes
|
||||
the internal functions above to C test programs. It is not part of the
|
||||
encoder API.
|
||||
- `common_encoder_tables.wuffs`: tables generated by `go run
|
||||
script/print-brotli-tables.go -encoder` from the C code.
|
||||
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// This file mirrors bit_cost.c, bit_cost_inc.h and fast_log.h. The functions
|
||||
// are methods of the bit_cost struct (whose only state is the stateless
|
||||
// softfloat arith), so that other structs (the encoder, the
|
||||
// metablock_builder) can embed one with a single field.
|
||||
//
|
||||
// Doubles are IEEE 754 binary64 bit patterns (base.u64). A uint32_t array
|
||||
// (e.g. a histogram's data_) is a slice of little-endian bytes, because
|
||||
// Wuffs' C code generator does not support "slice base.u32" arguments.
|
||||
//
|
||||
// A histogram (the C code's HistogramLiteral, HistogramCommand and
|
||||
// HistogramDistance structs) is (4 * data_size) + 16 bytes, exactly the C
|
||||
// struct's memory layout on a 64-bit little-endian machine:
|
||||
//
|
||||
// [0 .. 4 * data_size) data_, as u32le values
|
||||
// [4 * data_size .. (4 * data_size) + 8) total_count_, as a u64le
|
||||
// [(4 * data_size) + 8 .. (4 * data_size) + 16) bit_cost_, as the u64le
|
||||
// bits of a binary64
|
||||
//
|
||||
// data_size is 256 (HISTOGRAM_LITERAL_DATA_SIZE), 704
|
||||
// (HISTOGRAM_COMMAND_DATA_SIZE) or 544 (HISTOGRAM_DISTANCE_DATA_SIZE). The C
|
||||
// code's templated functions (the FN(X) macros, instantiated three times) are
|
||||
// one Wuffs function each, with a data_size argument.
|
||||
|
||||
pri const HISTOGRAM_LITERAL_DATA_SIZE : base.u32 = 256
|
||||
pri const HISTOGRAM_COMMAND_DATA_SIZE : base.u32 = 704
|
||||
pri const HISTOGRAM_DISTANCE_DATA_SIZE : base.u32 = 544
|
||||
|
||||
// The byte sizes of the three histogram types.
|
||||
pri const HISTOGRAM_LITERAL_SIZE : base.u64 = 1040
|
||||
pri const HISTOGRAM_COMMAND_SIZE : base.u64 = 2832
|
||||
pri const HISTOGRAM_DISTANCE_SIZE : base.u64 = 2192
|
||||
|
||||
// F64_HUGE_VAL is +infinity (the C code's HUGE_VAL).
|
||||
pri const F64_HUGE_VAL : base.u64 = 0x7FF0_0000_0000_0000
|
||||
|
||||
// F64_1E99 is the C code's 1e99.
|
||||
pri const F64_1E99 : base.u64 = 0x547D_42AE_A287_9F2E
|
||||
|
||||
pri struct bit_cost?(
|
||||
sf : softfloat.arith,
|
||||
)
|
||||
|
||||
// ld32 returns the u32le at s[i ..], or 0 if that is out of bounds (which
|
||||
// the callers never do).
|
||||
pri func bit_cost.ld32(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()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// ld64 is like ld32 but for a u64le.
|
||||
pri func bit_cost.ld64(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()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// st32 stores v as a u32le at s[i ..], or does nothing if that is out of
|
||||
// bounds (which the callers never do).
|
||||
pri func bit_cost.st32!(s: slice base.u8, i: base.u64, v: base.u32) {
|
||||
var q : slice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 4 {
|
||||
q.poke_u32le!(a: args.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// st64 is like st32 but for a u64le.
|
||||
pri func bit_cost.st64!(s: slice base.u8, i: base.u64, v: base.u64) {
|
||||
var q : slice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 8 {
|
||||
q.poke_u64le!(a: args.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors FastLog2 in fast_log.h. The result is the bit pattern of an IEEE
|
||||
// 754 binary64 value.
|
||||
pri func bit_cost.fast_log2(v: base.u64) base.u64 {
|
||||
if args.v < 256 {
|
||||
return LOG2_TABLE[args.v]
|
||||
}
|
||||
return this.sf.log2_u64(v: args.v)
|
||||
}
|
||||
|
||||
// Mirrors BrotliBitsEntropy in bit_cost.c. population holds (at least) size
|
||||
// u32le values. The C code's "goto odd_number_of_elements_left" only changes
|
||||
// the loop's unrolling, not the order of the operations.
|
||||
pri func bit_cost.bits_entropy(population: roslice base.u8, size: base.u64) base.u64 {
|
||||
var sum : base.u64
|
||||
var retval : base.u64
|
||||
var p : base.u64
|
||||
var q : roslice base.u8
|
||||
var n : base.u64
|
||||
|
||||
q = args.population
|
||||
n = args.size
|
||||
while n > 0 {
|
||||
n -= 1
|
||||
p = 0
|
||||
if q.length() >= 4 {
|
||||
p = q.peek_u32le() as base.u64
|
||||
q = q[4 ..]
|
||||
}
|
||||
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.fast_log2(v: p)))
|
||||
}
|
||||
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.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)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
|
||||
// Mirrors BrotliPopulationCostLiteral, BrotliPopulationCostCommand and
|
||||
// BrotliPopulationCostDistance (FN(BrotliPopulationCost) in bit_cost_inc.h).
|
||||
// histogram is a histogram of data_size symbols (see the layout above).
|
||||
pri func bit_cost.population_cost(histogram: roslice base.u8, data_size: base.u32[..= 704]) base.u64 {
|
||||
var total : base.u64
|
||||
var count : base.u32
|
||||
var s : array[4] base.u32
|
||||
var bits : base.u64
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var k : base.u32
|
||||
var c : base.u32
|
||||
var histo : array[4] base.u32
|
||||
var h0 : base.u32
|
||||
var h1 : base.u32
|
||||
var h2 : base.u32
|
||||
var h23 : base.u32
|
||||
var histomax : base.u32
|
||||
var max_depth : base.u64[..= 15]
|
||||
var depth_histo : array[18] base.u32
|
||||
var log2total : base.u64
|
||||
var log2p : base.u64
|
||||
var depth : base.u64
|
||||
var reps : base.u32
|
||||
var sum : base.u64
|
||||
var retval : base.u64
|
||||
var p : base.u64
|
||||
|
||||
total = this.ld64(s: args.histogram, i: (args.data_size as base.u64) * 4)
|
||||
if total == 0 {
|
||||
// kOneSymbolHistogramCost.
|
||||
return 0x4028_0000_0000_0000
|
||||
}
|
||||
i = 0
|
||||
while i < args.data_size {
|
||||
if this.ld32(s: args.histogram, i: (i as base.u64) * 4) > 0 {
|
||||
if count < 4 {
|
||||
s[count] = i
|
||||
}
|
||||
count ~mod+= 1
|
||||
if count > 4 {
|
||||
break
|
||||
}
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
if count == 1 {
|
||||
// kOneSymbolHistogramCost.
|
||||
return 0x4028_0000_0000_0000
|
||||
}
|
||||
if count == 2 {
|
||||
// kTwoSymbolHistogramCost + (double)total_count_.
|
||||
return this.sf.f64_add(a: 0x4034_0000_0000_0000, b: this.sf.f64_from_u64(a: total))
|
||||
}
|
||||
if count == 3 {
|
||||
h0 = this.ld32(s: args.histogram, i: (s[0] as base.u64) * 4)
|
||||
h1 = this.ld32(s: args.histogram, i: (s[1] as base.u64) * 4)
|
||||
h2 = this.ld32(s: args.histogram, i: (s[2] as base.u64) * 4)
|
||||
histomax = h0.max(no_less_than: h1.max(no_less_than: h2))
|
||||
// kThreeSymbolHistogramCost + 2 * (histo0 + histo1 + histo2) -
|
||||
// histomax, where the integer expression is uint32_t arithmetic.
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
a: 0x403C_0000_0000_0000,
|
||||
b: this.sf.f64_from_u32(a: 2 ~mod* ((h0 ~mod+ h1) ~mod+ h2))),
|
||||
b: this.sf.f64_from_u32(a: histomax))
|
||||
}
|
||||
if count == 4 {
|
||||
i = 0
|
||||
while i < 4 {
|
||||
histo[i] = this.ld32(s: args.histogram, i: (s[i] as base.u64) * 4)
|
||||
i ~mod+= 1
|
||||
}
|
||||
// Sort.
|
||||
i = 0
|
||||
while i < 4 {
|
||||
j = i ~mod+ 1
|
||||
while j < 4 {
|
||||
if histo[j] > histo[i & 3] {
|
||||
k = histo[j]
|
||||
histo[j] = histo[i & 3]
|
||||
histo[i & 3] = k
|
||||
}
|
||||
j ~mod+= 1
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
h23 = histo[2] ~mod+ histo[3]
|
||||
histomax = h23.max(no_less_than: histo[0])
|
||||
// kFourSymbolHistogramCost + 3 * h23 + 2 * (histo[0] + histo[1]) -
|
||||
// histomax, evaluated left to right.
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
a: this.sf.f64_add(
|
||||
a: 0x4042_8000_0000_0000,
|
||||
b: this.sf.f64_from_u32(a: 3 ~mod* h23)),
|
||||
b: this.sf.f64_from_u32(a: 2 ~mod* (histo[0] ~mod+ histo[1]))),
|
||||
b: this.sf.f64_from_u32(a: histomax))
|
||||
}
|
||||
|
||||
// Compute the entropy of the histogram and simultaneously build a
|
||||
// simplified histogram of the code length codes.
|
||||
max_depth = 1
|
||||
log2total = this.fast_log2(v: total)
|
||||
i = 0
|
||||
while i < args.data_size {
|
||||
c = this.ld32(s: args.histogram, i: (i as base.u64) * 4)
|
||||
if c > 0 {
|
||||
log2p = this.sf.f64_sub(a: log2total, b: this.fast_log2(v: c as base.u64))
|
||||
depth = this.sf.f64_to_u64_trunc(a: this.sf.f64_add(a: log2p, b: 0x3FE0_0000_0000_0000))
|
||||
bits = this.sf.f64_add(a: bits, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u32(a: c), b: log2p))
|
||||
if depth > 15 {
|
||||
depth = 15
|
||||
}
|
||||
if depth > (max_depth as base.u64) {
|
||||
max_depth = depth & 15
|
||||
}
|
||||
depth_histo[depth & 15] ~mod+= 1
|
||||
i ~mod+= 1
|
||||
} else {
|
||||
// Compute the run length of zeros.
|
||||
reps = 1
|
||||
k = i ~mod+ 1
|
||||
while k < args.data_size {
|
||||
if this.ld32(s: args.histogram, i: (k as base.u64) * 4) <> 0 {
|
||||
break
|
||||
}
|
||||
reps ~mod+= 1
|
||||
k ~mod+= 1
|
||||
}
|
||||
i ~sat+= reps
|
||||
if i >= args.data_size {
|
||||
// Don't add any cost for the last zero run.
|
||||
break
|
||||
}
|
||||
if reps < 3 {
|
||||
depth_histo[0] ~mod+= reps
|
||||
} else {
|
||||
reps -= 2
|
||||
while reps > 0 {
|
||||
depth_histo[17] ~mod+= 1
|
||||
// Add the 3 extra bits for the 17 code length code.
|
||||
bits = this.sf.f64_add(a: bits, b: 0x4008_0000_0000_0000)
|
||||
reps >>= 3
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add the estimated encoding cost of the code length code histogram.
|
||||
bits = this.sf.f64_add(a: bits, b: this.sf.f64_from_u64(a: 18 + (2 * max_depth)))
|
||||
|
||||
// Add the entropy of the code length code histogram. This is
|
||||
// BrotliBitsEntropy(depth_histo, BROTLI_CODE_LENGTH_CODES), inlined
|
||||
// because depth_histo is a local array.
|
||||
i = 0
|
||||
while i < 18 {
|
||||
p = depth_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.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.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)
|
||||
}
|
||||
return this.sf.f64_add(a: bits, b: retval)
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// This file mirrors histogram.h, histogram_inc.h and histogram.c. See
|
||||
// encode_bit_cost.wuffs for the histograms' byte layout. The functions are
|
||||
// methods of the bit_cost struct.
|
||||
//
|
||||
// A command (the C code's Command struct) is 16 bytes, exactly the C struct's
|
||||
// memory layout on a little-endian machine:
|
||||
//
|
||||
// [ 0 .. 4) insert_len_, as a u32le
|
||||
// [ 4 .. 8) copy_len_, as a u32le (the copy length in the low 25 bits,
|
||||
// copy_code - copy_len in the high 7 bits)
|
||||
// [ 8 .. 12) dist_extra_, as a u32le
|
||||
// [12 .. 14) cmd_prefix_, as a u16le
|
||||
// [14 .. 16) dist_prefix_, as a u16le
|
||||
//
|
||||
// A BlockSplit's types are a slice of u8 (one per block) and its lengths are
|
||||
// a slice of u32le values (one per block).
|
||||
|
||||
// Mirrors FN(HistogramClear) in histogram_inc.h.
|
||||
pri func bit_cost.histogram_clear!(h: slice base.u8, data_size: base.u32[..= 704]) {
|
||||
var n : base.u64
|
||||
|
||||
n = (args.data_size as base.u64) * 4
|
||||
if n <= args.h.length() {
|
||||
args.h[.. n].bulk_memset!(byte_value: 0)
|
||||
}
|
||||
this.st64!(s: args.h, i: n, v: 0)
|
||||
this.st64!(s: args.h, i: n + 8, v: F64_HUGE_VAL)
|
||||
}
|
||||
|
||||
// Mirrors FN(HistogramAdd) in histogram_inc.h.
|
||||
pri func bit_cost.histogram_add!(h: slice base.u8, data_size: base.u32[..= 704], val: base.u64) {
|
||||
var n : base.u64
|
||||
|
||||
n = (args.data_size as base.u64) * 4
|
||||
if args.val < (args.data_size as base.u64) {
|
||||
this.st32!(s: args.h, i: args.val ~mod* 4, v: this.ld32(s: args.h, i: args.val ~mod* 4) ~mod+ 1)
|
||||
}
|
||||
this.st64!(s: args.h, i: n, v: this.ld64(s: args.h, i: n) ~mod+ 1)
|
||||
}
|
||||
|
||||
// Mirrors FN(HistogramAddHistogram) in histogram_inc.h: h += v.
|
||||
pri func bit_cost.histogram_add_histogram!(h: slice base.u8, v: roslice base.u8, data_size: base.u32[..= 704]) {
|
||||
var n : base.u64
|
||||
var p : slice base.u8
|
||||
var q : roslice base.u8
|
||||
|
||||
n = (args.data_size as base.u64) * 4
|
||||
this.st64!(s: args.h, i: n, v: this.ld64(s: args.h, i: n) ~mod+ this.ld64(s: args.v, i: n))
|
||||
if (n <= args.h.length()) and (n <= args.v.length()) {
|
||||
iterate (p = args.h[.. n], q = args.v[.. n])(length: 4, advance: 4, unroll: 1) {
|
||||
p.poke_u32le!(a: p.peek_u32le() ~mod+ q.peek_u32le())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// histogram_copy is the C code's struct assignment "*h = *v".
|
||||
pri func bit_cost.histogram_copy!(h: slice base.u8, v: roslice base.u8, data_size: base.u32[..= 704]) {
|
||||
var n : base.u64
|
||||
|
||||
n = ((args.data_size as base.u64) * 4) + 16
|
||||
if (n <= args.h.length()) and (n <= args.v.length()) {
|
||||
args.h[.. n].copy_from_slice!(s: args.v[.. n])
|
||||
}
|
||||
}
|
||||
|
||||
// histogram_total_count returns the histogram's total_count_.
|
||||
pri func bit_cost.histogram_total_count(h: roslice base.u8, data_size: base.u32[..= 704]) base.u64 {
|
||||
return this.ld64(s: args.h, i: (args.data_size as base.u64) * 4)
|
||||
}
|
||||
|
||||
// histogram_bit_cost returns the histogram's bit_cost_.
|
||||
pri func bit_cost.histogram_bit_cost(h: roslice base.u8, data_size: base.u32[..= 704]) base.u64 {
|
||||
return this.ld64(s: args.h, i: ((args.data_size as base.u64) * 4) + 8)
|
||||
}
|
||||
|
||||
// histogram_set_bit_cost sets the histogram's bit_cost_.
|
||||
pri func bit_cost.histogram_set_bit_cost!(h: slice base.u8, data_size: base.u32[..= 704], bit_cost: base.u64) {
|
||||
this.st64!(s: args.h, i: ((args.data_size as base.u64) * 4) + 8, v: args.bit_cost)
|
||||
}
|
||||
|
||||
// Mirrors BrotliBuildHistogramsWithContext in histogram.c (with the
|
||||
// BlockSplitIterator functions inlined).
|
||||
//
|
||||
// The literal, command and distance block splits are given as their types
|
||||
// (u8s), lengths (u32les) and num_blocks. A block split's lengths are the C
|
||||
// code's NULL pointer when its num_blocks is zero.
|
||||
//
|
||||
// The C code's context_modes argument is either NULL or an array whose
|
||||
// elements are all the same (BrotliBuildMetaBlock's literal_context_mode).
|
||||
// Here, context_mode is that common element, or 0xFFFF_FFFF for NULL.
|
||||
//
|
||||
// lit_histos, cmd_histos and dist_histos are arrays of literal (1040 byte),
|
||||
// command (2832 byte) and distance (2192 byte) histograms.
|
||||
pri func bit_cost.build_histograms_with_context!(
|
||||
cmds: roslice base.u8,
|
||||
num_commands: base.u64,
|
||||
lit_types: roslice base.u8,
|
||||
lit_lengths: roslice base.u8,
|
||||
lit_num_blocks: base.u64,
|
||||
cmd_types: roslice base.u8,
|
||||
cmd_lengths: roslice base.u8,
|
||||
cmd_num_blocks: base.u64,
|
||||
dist_types: roslice base.u8,
|
||||
dist_lengths: roslice base.u8,
|
||||
dist_num_blocks: base.u64,
|
||||
ringbuffer: roslice base.u8,
|
||||
start_pos: base.u64,
|
||||
mask: base.u64,
|
||||
prev_byte: base.u8,
|
||||
prev_byte2: base.u8,
|
||||
context_mode: base.u32,
|
||||
lit_histos: slice base.u8,
|
||||
cmd_histos: slice base.u8,
|
||||
dist_histos: slice base.u8) {
|
||||
var pos : base.u64
|
||||
var prev_byte : base.u8
|
||||
var prev_byte2 : base.u8
|
||||
var lit_idx : base.u64
|
||||
var lit_type : base.u64
|
||||
var lit_length : base.u64
|
||||
var cmd_idx : base.u64
|
||||
var cmd_type : base.u64
|
||||
var cmd_length : base.u64
|
||||
var dist_idx : base.u64
|
||||
var dist_type : base.u64
|
||||
var dist_length : base.u64
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var c : base.u64
|
||||
var insert_len : base.u32
|
||||
var copy_len : base.u32
|
||||
var cmd_prefix : base.u32
|
||||
var dist_prefix : base.u32
|
||||
var context : base.u64
|
||||
var lut_offset : base.u32[..= 1536]
|
||||
var b : base.u8
|
||||
var off : base.u64
|
||||
var r : base.u32
|
||||
|
||||
pos = args.start_pos
|
||||
prev_byte = args.prev_byte
|
||||
prev_byte2 = args.prev_byte2
|
||||
if args.lit_num_blocks > 0 {
|
||||
lit_length = this.ld32(s: args.lit_lengths, i: 0) as base.u64
|
||||
}
|
||||
if args.cmd_num_blocks > 0 {
|
||||
cmd_length = this.ld32(s: args.cmd_lengths, i: 0) as base.u64
|
||||
}
|
||||
if args.dist_num_blocks > 0 {
|
||||
dist_length = this.ld32(s: args.dist_lengths, i: 0) as base.u64
|
||||
}
|
||||
lut_offset = (args.context_mode & 3) << 9
|
||||
|
||||
i = 0
|
||||
while i < args.num_commands {
|
||||
c = i ~mod* 16
|
||||
insert_len = this.ld32(s: args.cmds, i: c)
|
||||
copy_len = this.ld32(s: args.cmds, i: c ~mod+ 4) & 0x1FF_FFFF
|
||||
r = this.ld32(s: args.cmds, i: c ~mod+ 12)
|
||||
cmd_prefix = r & 0xFFFF
|
||||
dist_prefix = r >> 16
|
||||
|
||||
// BlockSplitIteratorNext(&insert_and_copy_it).
|
||||
if cmd_length == 0 {
|
||||
cmd_idx ~mod+= 1
|
||||
if cmd_idx < args.cmd_types.length() {
|
||||
cmd_type = args.cmd_types[cmd_idx] as base.u64
|
||||
} else {
|
||||
cmd_type = 0
|
||||
}
|
||||
cmd_length = this.ld32(s: args.cmd_lengths, i: cmd_idx ~mod* 4) as base.u64
|
||||
}
|
||||
cmd_length ~mod-= 1
|
||||
off = cmd_type ~mod* HISTOGRAM_COMMAND_SIZE
|
||||
if off <= args.cmd_histos.length() {
|
||||
this.histogram_add!(h: args.cmd_histos[off ..], data_size: HISTOGRAM_COMMAND_DATA_SIZE, val: cmd_prefix as base.u64)
|
||||
}
|
||||
|
||||
j = insert_len as base.u64
|
||||
while j <> 0 {
|
||||
j -= 1
|
||||
// BlockSplitIteratorNext(&literal_it).
|
||||
if lit_length == 0 {
|
||||
lit_idx ~mod+= 1
|
||||
if lit_idx < args.lit_types.length() {
|
||||
lit_type = args.lit_types[lit_idx] as base.u64
|
||||
} else {
|
||||
lit_type = 0
|
||||
}
|
||||
lit_length = this.ld32(s: args.lit_lengths, i: lit_idx ~mod* 4) as base.u64
|
||||
}
|
||||
lit_length ~mod-= 1
|
||||
context = lit_type
|
||||
if args.context_mode <> 0xFFFF_FFFF {
|
||||
context = (context ~mod<< 6) ~mod+ ((
|
||||
CONTEXT_LOOKUP[(lut_offset + (prev_byte as base.u32)) & 2047] |
|
||||
CONTEXT_LOOKUP[(lut_offset + 256 + (prev_byte2 as base.u32)) & 2047]) as base.u64)
|
||||
}
|
||||
b = 0
|
||||
if (pos & args.mask) < args.ringbuffer.length() {
|
||||
b = args.ringbuffer[pos & args.mask]
|
||||
}
|
||||
off = context ~mod* HISTOGRAM_LITERAL_SIZE
|
||||
if off <= args.lit_histos.length() {
|
||||
this.histogram_add!(h: args.lit_histos[off ..], data_size: HISTOGRAM_LITERAL_DATA_SIZE, val: b as base.u64)
|
||||
}
|
||||
prev_byte2 = prev_byte
|
||||
prev_byte = b
|
||||
pos ~mod+= 1
|
||||
}
|
||||
|
||||
pos ~mod+= copy_len as base.u64
|
||||
if copy_len <> 0 {
|
||||
prev_byte2 = 0
|
||||
if ((pos ~mod- 2) & args.mask) < args.ringbuffer.length() {
|
||||
prev_byte2 = args.ringbuffer[(pos ~mod- 2) & args.mask]
|
||||
}
|
||||
prev_byte = 0
|
||||
if ((pos ~mod- 1) & args.mask) < args.ringbuffer.length() {
|
||||
prev_byte = args.ringbuffer[(pos ~mod- 1) & args.mask]
|
||||
}
|
||||
if cmd_prefix >= 128 {
|
||||
// BlockSplitIteratorNext(&dist_it).
|
||||
if dist_length == 0 {
|
||||
dist_idx ~mod+= 1
|
||||
if dist_idx < args.dist_types.length() {
|
||||
dist_type = args.dist_types[dist_idx] as base.u64
|
||||
} else {
|
||||
dist_type = 0
|
||||
}
|
||||
dist_length = this.ld32(s: args.dist_lengths, i: dist_idx ~mod* 4) as base.u64
|
||||
}
|
||||
dist_length ~mod-= 1
|
||||
context = (dist_type ~mod<< 2) ~mod+ (this.command_distance_context(cmd_prefix: cmd_prefix) as base.u64)
|
||||
off = context ~mod* HISTOGRAM_DISTANCE_SIZE
|
||||
if off <= args.dist_histos.length() {
|
||||
this.histogram_add!(h: args.dist_histos[off ..], data_size: HISTOGRAM_DISTANCE_DATA_SIZE, val: (dist_prefix & 0x3FF) as base.u64)
|
||||
}
|
||||
}
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors CommandDistanceContext in command.h.
|
||||
pri func bit_cost.command_distance_context(cmd_prefix: base.u32) base.u32 {
|
||||
var r : base.u32
|
||||
var c : base.u32
|
||||
|
||||
r = args.cmd_prefix >> 6
|
||||
c = args.cmd_prefix & 7
|
||||
if ((r == 0) or (r == 2) or (r == 4) or (r == 7)) and (c <= 2) {
|
||||
return c
|
||||
}
|
||||
return 3
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// TEST ONLY. The metablock_tester struct is not part of the encoder API. It
|
||||
// exposes the encoder's internal (pri) bit cost, histogram, block splitter,
|
||||
// clustering and metablock functions as pub methods, so that C test programs
|
||||
// (test/c/std/brotli.c and script/manual-test-brotli-metablock-parity.c) can
|
||||
// compare them with the C code's BROTLI_INTERNAL functions. Its methods and
|
||||
// their semantics may change at any time.
|
||||
|
||||
pub struct metablock_tester?(
|
||||
dummy : base.u32,
|
||||
) + (
|
||||
// Wuffs' C code generator expects same-package struct fields to be in
|
||||
// the private_data section (after the "+").
|
||||
bc : bit_cost,
|
||||
)
|
||||
|
||||
// bits_entropy mirrors BrotliBitsEntropy. population holds size u32le values.
|
||||
pub func metablock_tester.bits_entropy(population: roslice base.u8, size: base.u64) base.u64 {
|
||||
return this.bc.bits_entropy(population: args.population, size: args.size)
|
||||
}
|
||||
|
||||
// population_cost mirrors BrotliPopulationCostLiteral (data_size 256),
|
||||
// BrotliPopulationCostCommand (704) and BrotliPopulationCostDistance (544).
|
||||
// histogram has the C struct's layout: data_size u32le counts, then the
|
||||
// total_count_ u64le (and then the bit_cost_, which is ignored). It returns 0
|
||||
// for any other data_size.
|
||||
pub func metablock_tester.population_cost(histogram: roslice base.u8, data_size: base.u32) base.u64 {
|
||||
if args.data_size == 256 {
|
||||
return this.bc.population_cost(histogram: args.histogram, data_size: 256)
|
||||
} else if args.data_size == 704 {
|
||||
return this.bc.population_cost(histogram: args.histogram, data_size: 704)
|
||||
} else if args.data_size == 544 {
|
||||
return this.bc.population_cost(histogram: args.histogram, data_size: 544)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user