Merge branch 'm4b-metablock' into m4a-encoder-core

# Conflicts:
#	release/c/wuffs-unsupported-snapshot.c
This commit is contained in:
Claude committed 2026-10-27 01:10:29 +00:00
commit abecc768e5
7 files changed
+7382 -4

No files matched your search

+35
View File
@@ -48,10 +48,43 @@ 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
// LOG2_CACHE_SIZE is the size of the bit_cost.log2_cache table.
pri const LOG2_CACHE_SIZE : base.u64 = 65536
pri struct bit_cost?(
// log2_cache_ready is whether log2_cache has been filled (by
// init_log2_cache).
log2_cache_ready : base.bool,
sf : softfloat.arith,
) + (
// log2_cache[v] is FastLog2(v), for v < LOG2_CACHE_SIZE. It is only a
// speed optimization (glibc's log2 is expensive to emulate): the
// values are computed by the same functions, so the results are
// identical with or without it.
log2_cache : array[65536] base.u64,
)
// init_log2_cache fills the log2_cache table (if it is not already filled).
// Calling it is optional, but it makes fast_log2 faster.
pri func bit_cost.init_log2_cache!() {
var v : base.u64
if this.log2_cache_ready {
return nothing
}
v = 0
while v < 256 {
this.log2_cache[v] = LOG2_TABLE[v]
v += 1
}
while v < 65536 {
this.log2_cache[v] = this.sf.log2_u64(v: v)
v += 1
}
this.log2_cache_ready = true
}
// 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 {
@@ -109,6 +142,8 @@ pri func bit_cost.st64!(s: slice base.u8, i: base.u64, v: base.u64) {
pri func bit_cost.fast_log2(v: base.u64) base.u64 {
if args.v < 256 {
return LOG2_TABLE[args.v]
} else if this.log2_cache_ready and (args.v < 65536) {
return this.log2_cache[args.v]
}
return this.sf.log2_u64(v: args.v)
}
+764
View File
@@ -0,0 +1,764 @@
// 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 block_splitter.h, block_splitter.c and
// block_splitter_inc.h. The functions are metablock_builder methods. The C
// code's templated functions (FN(X), for X in Literal, Command and Distance)
// are one Wuffs function each, with these arguments:
// - cat: CATEGORY_LITERAL, CATEGORY_COMMAND or CATEGORY_DISTANCE, which
// selects the BlockSplit (this.split_etc[cat]) to write to.
// - ds: the histogram's data size (256, 704 or 544).
// - width: the DataType's size in bytes (1 for uint8_t, 2 for uint16_t).
// A DataType array (the "data" arguments) is at wb[data ..].
// The C code's kLiteralBlockSwitchCost (28.1), kCommandBlockSwitchCost (13.5)
// and kDistanceBlockSwitchCost (14.6).
pri const LITERAL_BLOCK_SWITCH_COST : base.u64 = 0x403C_1999_9999_999A
pri const COMMAND_BLOCK_SWITCH_COST : base.u64 = 0x402B_0000_0000_0000
pri const DISTANCE_BLOCK_SWITCH_COST : base.u64 = 0x402D_3333_3333_3333
// The C code's HISTOGRAMS_PER_BATCH and CLUSTERS_PER_BATCH.
pri const HISTOGRAMS_PER_BATCH : base.u64 = 64
pri const CLUSTERS_PER_BATCH : base.u64 = 16
// Mirrors BitCost in block_splitter.c.
pri func metablock_builder.bit_cost(count: base.u64) base.u64 {
if args.count == 0 {
// -2.0
return 0xC000_0000_0000_0000
}
return this.bc.fast_log2(v: args.count)
}
// Mirrors FN(HistogramAddVector) in histogram_inc.h, adding the n symbols at
// data[pos ..] to the histogram at wb[h ..].
pri func metablock_builder.histogram_add_vector!(wb: slice base.u8, h: base.u64, ds: base.u32[..= 704],
data: base.u64, pos: base.u64, n: base.u64, width: base.u32) {
var total_offset : base.u64
var i : base.u64
var s : base.u64
var o : base.u64
total_offset = args.h ~mod+ ((args.ds as base.u64) * 4)
this.st64!(wb: args.wb, i: total_offset, v: this.ld64(wb: args.wb, i: total_offset) ~mod+ args.n)
i = 0
while i < args.n {
s = this.sym(wb: args.wb, base: args.data, i: args.pos ~mod+ i, width: args.width)
if s < (args.ds as base.u64) {
o = args.h ~mod+ (s ~mod* 4)
this.st32!(wb: args.wb, i: o, v: this.ld32(wb: args.wb, i: o) ~mod+ 1)
}
i ~mod+= 1
}
}
// Mirrors CopyLiteralsToByteArray in block_splitter.c, writing to wb[literals
// ..].
pri func metablock_builder.copy_literals_to_byte_array!(wb: slice base.u8, cmds: roslice base.u8, num_commands: base.u64,
data: roslice base.u8, offset: base.u64, mask: base.u64, literals: base.u64) {
var pos : base.u64
var from_pos : base.u64
var i : base.u64
var c : base.u64
var insert_len : base.u64
var head_size : base.u64
from_pos = args.offset & args.mask
i = 0
while i < args.num_commands {
c = i ~mod* 16
insert_len = this.ld32(wb: args.cmds, i: c) as base.u64
if (from_pos ~mod+ insert_len) > args.mask {
head_size = (args.mask ~mod+ 1) ~mod- from_pos
this.copy_bytes!(wb: args.wb, dst: args.literals ~mod+ pos, data: args.data, src: from_pos, n: head_size)
from_pos = 0
pos ~mod+= head_size
insert_len ~mod-= head_size
}
if insert_len > 0 {
this.copy_bytes!(wb: args.wb, dst: args.literals ~mod+ pos, data: args.data, src: from_pos, n: insert_len)
pos ~mod+= insert_len
}
from_pos = ((from_pos ~mod+ insert_len) ~mod+ ((this.ld32(wb: args.cmds, i: c ~mod+ 4) & 0x1FF_FFFF) as base.u64)) & args.mask
i ~mod+= 1
}
}
// copy_bytes is memcpy(wb + dst, data + src, n), for in-bounds arguments.
pri func metablock_builder.copy_bytes!(wb: slice base.u8, dst: base.u64, data: roslice base.u8, src: base.u64, n: base.u64) {
var d : slice base.u8
var s : roslice base.u8
if (args.dst <= args.wb.length()) and (args.src <= args.data.length()) {
d = args.wb[args.dst ..]
s = args.data[args.src ..]
if (args.n <= d.length()) and (args.n <= s.length()) {
d[.. args.n].copy_from_slice!(s: s[.. args.n])
}
}
}
// Mirrors BrotliSplitBlock in block_splitter.c. The commands are at cmds and
// the data (the ring buffer) at data. It sets this.split_etc (whose
// split_types_off and split_lengths_off arrays must already be allocated,
// with room for one block per symbol, and whose split_num_blocks must be
// zero).
pri func metablock_builder.split_block!(wb: slice base.u8, cmds: roslice base.u8, num_commands: base.u64,
data: roslice base.u8, pos: base.u64, mask: base.u64) {
var mark : base.u64
var literals_count : base.u64
var literals : base.u64
var codes : base.u64
var i : base.u64
var j : base.u64
var c : base.u64
mark = this.wb_top
// CountLiterals.
i = 0
while i < args.num_commands {
literals_count ~mod+= this.ld32(wb: args.cmds, i: i ~mod* 16) as base.u64
i ~mod+= 1
}
literals = this.alloc!(n: literals_count)
if this.oom {
return nothing
}
// Create a continuous array of literals.
this.copy_literals_to_byte_array!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
data: args.data, offset: args.pos, mask: args.mask, literals: literals)
// Create the block split on the array of literals.
this.split_byte_vector!(wb: args.wb, cat: 0, data: literals, length: literals_count, width: 1,
ds: HISTOGRAM_LITERAL_DATA_SIZE, symbols_per_histogram: 544, max_histograms: 100,
sampling_stride_length: 70, block_switch_cost: LITERAL_BLOCK_SWITCH_COST)
this.wb_top = mark
if this.oom {
return nothing
}
// Compute prefix codes for commands.
codes = this.alloc!(n: args.num_commands ~mod* 2)
if this.oom {
return nothing
}
i = 0
while i < args.num_commands {
this.st16!(wb: args.wb, i: codes ~mod+ (i ~mod* 2), v: this.ld16(wb: args.cmds, i: (i ~mod* 16) ~mod+ 12))
i ~mod+= 1
}
// Create the block split on the array of command prefixes.
this.split_byte_vector!(wb: args.wb, cat: 1, data: codes, length: args.num_commands, width: 2,
ds: HISTOGRAM_COMMAND_DATA_SIZE, symbols_per_histogram: 530, max_histograms: 50,
sampling_stride_length: 40, block_switch_cost: COMMAND_BLOCK_SWITCH_COST)
this.wb_top = mark
if this.oom {
return nothing
}
// Create a continuous array of distance prefixes.
codes = this.alloc!(n: args.num_commands ~mod* 2)
if this.oom {
return nothing
}
j = 0
i = 0
while i < args.num_commands {
c = i ~mod* 16
if ((this.ld32(wb: args.cmds, i: c ~mod+ 4) & 0x1FF_FFFF) <> 0) and
(this.ld16(wb: args.cmds, i: c ~mod+ 12) >= 128) {
this.st16!(wb: args.wb, i: codes ~mod+ (j ~mod* 2), v: this.ld16(wb: args.cmds, i: c ~mod+ 14) & 0x3FF)
j ~mod+= 1
}
i ~mod+= 1
}
// Create the block split on the array of distance prefixes.
this.split_byte_vector!(wb: args.wb, cat: 2, data: codes, length: j, width: 2,
ds: HISTOGRAM_DISTANCE_DATA_SIZE, symbols_per_histogram: 544, max_histograms: 50,
sampling_stride_length: 40, block_switch_cost: DISTANCE_BLOCK_SWITCH_COST)
this.wb_top = mark
}
// Mirrors FN(InitialEntropyCodes) in block_splitter_inc.h.
pri func metablock_builder.initial_entropy_codes!(wb: slice base.u8, data: base.u64, length: base.u64, width: base.u32,
stride: base.u64, num_histograms: base.u64, histograms: base.u64, ds: base.u32[..= 704]) {
var seed : base.u32
var block_length : base.u64
var i : base.u64
var pos : base.u64
var hs : base.u64
var nh : base.u64
hs = this.hsize(ds: args.ds)
seed = 7
if args.num_histograms == 0 {
return nothing
}
nh = args.num_histograms.max(no_less_than: 1)
block_length = args.length / nh.max(no_less_than: 1)
i = 0
while i < args.num_histograms {
this.h_clear!(wb: args.wb, h: args.histograms ~mod+ (i ~mod* hs), ds: args.ds)
i ~mod+= 1
}
i = 0
while i < args.num_histograms {
pos = (args.length ~mod* i) / nh.max(no_less_than: 1)
if i <> 0 {
// MyRand.
seed ~mod*= 16807
if block_length > 0 {
pos ~mod+= (seed as base.u64) % block_length
}
}
if (pos ~mod+ args.stride) >= args.length {
pos = (args.length ~mod- args.stride) ~mod- 1
}
this.histogram_add_vector!(wb: args.wb, h: args.histograms ~mod+ (i ~mod* hs), ds: args.ds,
data: args.data, pos: pos, n: args.stride, width: args.width)
i ~mod+= 1
}
}
// Mirrors FN(RefineEntropyCodes) in block_splitter_inc.h, with
// FN(RandomSample) inlined.
pri func metablock_builder.refine_entropy_codes!(wb: slice base.u8, data: base.u64, length: base.u64, width: base.u32,
stride: base.u64, num_histograms: base.u64, histograms: base.u64, tmp: base.u64, ds: base.u32[..= 704]) {
var iters : base.u64
var seed : base.u32
var iter : base.u64
var pos : base.u64
var stride : base.u64
var hs : base.u64
var nh : base.u64
if (args.stride == 0) or (args.num_histograms == 0) {
return nothing
}
hs = this.hsize(ds: args.ds)
// kIterMulForRefining * length / stride + kMinItersForRefining.
nh = args.num_histograms.max(no_less_than: 1)
iters = ((2 ~mod* args.length) / args.stride.max(no_less_than: 1)) ~mod+ 100
seed = 7
iters = (((iters ~mod+ nh) ~mod- 1) / nh.max(no_less_than: 1)) ~mod* nh
iter = 0
while iter < iters {
this.h_clear!(wb: args.wb, h: args.tmp, ds: args.ds)
// RandomSample.
pos = 0
stride = args.stride
if stride >= args.length {
stride = args.length
} else {
seed ~mod*= 16807
pos = (args.length ~mod- stride) ~mod+ 1
pos = (seed as base.u64) % pos.max(no_less_than: 1)
}
this.histogram_add_vector!(wb: args.wb, h: args.tmp, ds: args.ds,
data: args.data, pos: pos, n: stride, width: args.width)
this.h_add_histogram!(wb: args.wb, h: args.histograms ~mod+ ((iter % nh.max(no_less_than: 1)) ~mod* hs), v: args.tmp, ds: args.ds)
iter ~mod+= 1
}
}
// Mirrors FN(FindBlocks) in block_splitter_inc.h. num_histograms is at most
// 100 (or 50 when ds is more than 256), so that the insert_cost array fits.
// The block ids are u8s at wb[block_id ..] and the switch signal bitmaps at
// wb[switch_signal ..]. It returns the number of blocks.
pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, length: base.u64, width: base.u32,
block_switch_bitcost: base.u64, num_histograms: base.u64, histograms: base.u64,
switch_signal: base.u64, block_id: base.u64, ds: base.u32[..= 704]) base.u64 {
var alphabet_size : base.u64
var bitmap_len : base.u64
var num_blocks : base.u64
var byte_ix : base.u64
var i : base.u64
var j : base.u64
var k : base.u64
var hs : base.u64
var ix : base.u64
var insert_cost_ix : base.u64
var min_cost : base.u64
var block_switch_cost : base.u64
var c : base.u64
var cur_id : base.u8
var b : base.u8
alphabet_size = args.ds as base.u64
bitmap_len = (args.num_histograms ~mod+ 7) >> 3
num_blocks = 1
hs = this.hsize(ds: args.ds)
// Trivial case: single histogram -> single block type.
if args.num_histograms <= 1 {
this.zero!(wb: args.wb, i: args.block_id, n: args.length)
return 1
}
if (args.num_histograms > 256) or ((alphabet_size ~mod* args.num_histograms) > 35200) {
// This never happens: see the comment above.
this.oom = true
return 1
}
// Fill bitcost for each symbol of all histograms.
this.insert_cost[.. 35200].bulk_memset!(byte_value: 0)
i = 0
while i < args.num_histograms {
// FastLog2((uint32_t)histograms[i].total_count_).
this.insert_cost[i & 255] = this.bc.fast_log2(
v: this.h_total(wb: args.wb, h: args.histograms ~mod+ (i ~mod* hs), ds: args.ds) & 0xFFFF_FFFF)
i ~mod+= 1
}
i = alphabet_size
while i <> 0 {
// Reverse order to use the 0-th row as a temporary storage.
i ~mod-= 1
j = 0
while j < args.num_histograms {
k = (i ~mod* args.num_histograms) ~mod+ j
if k < 35200 {
this.insert_cost[k] = this.sf.f64_sub(
a: this.insert_cost[j & 255],
b: this.bit_cost(count: this.ld32(wb: args.wb, i: (args.histograms ~mod+ (j ~mod* hs)) ~mod+ (i ~mod* 4)) as base.u64))
}
j ~mod+= 1
}
}
// After each iteration of this loop, cost[k] will contain the difference
// between the minimum cost of arriving at the current byte position using
// entropy code k, and the minimum cost of arriving at the current byte
// position. This difference is capped at the block switch cost, and if it
// reaches block switch cost, it means that when we trace back from the
// last position, we need to switch here.
this.cost[.. 256].bulk_memset!(byte_value: 0)
this.zero!(wb: args.wb, i: args.switch_signal, n: args.length ~mod* bitmap_len)
byte_ix = 0
while byte_ix < args.length {
ix = byte_ix ~mod* bitmap_len
insert_cost_ix = this.sym(wb: args.wb, base: args.data, i: byte_ix, width: args.width) ~mod* args.num_histograms
min_cost = F64_1E99
block_switch_cost = args.block_switch_bitcost
k = 0
while k < args.num_histograms {
// We are coding the symbol with entropy code k.
c = 0
if (insert_cost_ix ~mod+ k) < 35200 {
c = this.insert_cost[(insert_cost_ix ~mod+ k) % 35200]
}
c = this.sf.f64_add(a: this.cost[k & 255], b: c)
this.cost[k & 255] = c
if this.sf.f64_lt(a: c, b: min_cost) <> 0 {
min_cost = c
this.st8!(wb: args.wb, i: args.block_id ~mod+ byte_ix, v: (k & 0xFF) as base.u8)
}
k ~mod+= 1
}
// More blocks for the beginning.
if byte_ix < 2000 {
// block_switch_cost *= 0.77 + multiplier * (double)byte_ix, with
// multiplier = 0.07 / 2000.
block_switch_cost = this.sf.f64_mul(
a: block_switch_cost,
b: this.sf.f64_add(
a: 0x3FE8_A3D7_0A3D_70A4,
b: this.sf.f64_mul(a: 0x3F02_599E_D7C6_FBD3, b: this.sf.f64_from_u64(a: byte_ix))))
}
k = 0
while k < args.num_histograms {
c = this.sf.f64_sub(a: this.cost[k & 255], b: min_cost)
this.cost[k & 255] = c
if this.sf.f64_ge(a: c, b: block_switch_cost) <> 0 {
this.cost[k & 255] = block_switch_cost
j = ix ~mod+ (k >> 3)
if true {
b = this.ld8(wb: args.wb, i: args.switch_signal ~mod+ j)
this.st8!(wb: args.wb, i: args.switch_signal ~mod+ j, v: b | ((1 as base.u8) << (k & 7)))
}
}
k ~mod+= 1
}
byte_ix ~mod+= 1
}
// Trace back from the last position and switch at the marked places.
byte_ix = args.length ~mod- 1
ix = byte_ix ~mod* bitmap_len
cur_id = this.ld8(wb: args.wb, i: args.block_id ~mod+ byte_ix)
while byte_ix > 0 {
byte_ix ~mod-= 1
ix ~mod-= bitmap_len
b = this.ld8(wb: args.wb, i: (args.switch_signal ~mod+ ix) ~mod+ ((cur_id >> 3) as base.u64))
if (b & ((1 as base.u8) << (cur_id & 7))) <> 0 {
if cur_id <> this.ld8(wb: args.wb, i: args.block_id ~mod+ byte_ix) {
cur_id = this.ld8(wb: args.wb, i: args.block_id ~mod+ byte_ix)
num_blocks ~mod+= 1
}
}
this.st8!(wb: args.wb, i: args.block_id ~mod+ byte_ix, v: cur_id)
}
return num_blocks
}
pri func metablock_builder.ld8(wb: roslice base.u8, i: base.u64) base.u8 {
if args.i < args.wb.length() {
return args.wb[args.i]
}
return 0
}
// Mirrors FN(RemapBlockIds) in block_splitter_inc.h.
pri func metablock_builder.remap_block_ids!(wb: slice base.u8, block_ids: base.u64, length: base.u64, num_histograms: base.u64) base.u64 {
var next_id : base.u32
var i : base.u64
var b : base.u8
i = 0
while i < args.num_histograms {
this.new_id[i & 255] = 256
i ~mod+= 1
}
i = 0
while i < args.length {
b = this.ld8(wb: args.wb, i: args.block_ids ~mod+ i)
if this.new_id[b] == 256 {
this.new_id[b] = (next_id & 0xFFFF) as base.u16
next_id ~mod+= 1
}
i ~mod+= 1
}
i = 0
while i < args.length {
b = this.ld8(wb: args.wb, i: args.block_ids ~mod+ i)
this.st8!(wb: args.wb, i: args.block_ids ~mod+ i, v: (this.new_id[b] & 0xFF) as base.u8)
i ~mod+= 1
}
return next_id as base.u64
}
// Mirrors FN(BuildBlockHistograms) in block_splitter_inc.h.
pri func metablock_builder.build_block_histograms!(wb: slice base.u8, data: base.u64, length: base.u64, width: base.u32,
block_ids: base.u64, num_histograms: base.u64, histograms: base.u64, ds: base.u32[..= 704]) {
var i : base.u64
var hs : base.u64
hs = this.hsize(ds: args.ds)
i = 0
while i < args.num_histograms {
this.h_clear!(wb: args.wb, h: args.histograms ~mod+ (i ~mod* hs), ds: args.ds)
i ~mod+= 1
}
i = 0
while i < args.length {
this.h_add!(wb: args.wb,
h: args.histograms ~mod+ ((this.ld8(wb: args.wb, i: args.block_ids ~mod+ i) as base.u64) ~mod* hs),
ds: args.ds,
val: this.sym(wb: args.wb, base: args.data, i: i, width: args.width))
i ~mod+= 1
}
}
// Mirrors FN(ClusterBlocks) in block_splitter_inc.h. It writes the result to
// this.split_etc[cat].
pri func metablock_builder.cluster_blocks!(wb: slice base.u8, cat: base.u32[..= 2], data: base.u64, length: base.u64, width: base.u32,
num_blocks: base.u64, block_ids: base.u64, ds: base.u32[..= 704]) {
var hs : base.u64
var histogram_symbols : base.u64
var u32s : base.u64
var sizes : base.u64
var new_clusters : base.u64
var symbols : base.u64
var remap : base.u64
var block_lengths : base.u64
var all_histograms : base.u64
var all_histograms_size : base.u64
var cluster_size : base.u64
var cluster_size_size : base.u64
var num_clusters : base.u64
var histograms : base.u64
var max_num_pairs : base.u64
var pairs : base.u64
var pos : base.u64
var clusters : base.u64
var num_final_clusters : base.u64
var new_index : base.u64
var tmp : base.u64
var i : base.u64
var j : base.u64
var k : base.u64
var block_idx : base.u64
var num_to_combine : base.u64
var num_new_clusters : base.u64
var block_length : base.u64
var nc : base.u64
var next_index : base.u32
var best_out : base.u32
var best_bits : base.u64
var cur_bits : base.u64
var cur_length : base.u32
var max_type : base.u32
var id : base.u32
var s : base.u32
var types_off : base.u64
var lengths_off : base.u64
hs = this.hsize(ds: args.ds)
histogram_symbols = this.alloc!(n: args.num_blocks ~mod* 4)
u32s = this.alloc!(n: (args.num_blocks ~mod+ (4 * HISTOGRAMS_PER_BATCH)) ~mod* 4)
// The C code's all_histograms and cluster_size arrays grow (with
// BROTLI_ENSURE_CAPACITY) up to num_clusters, which is at most
// num_blocks.
all_histograms = this.alloc!(n: args.num_blocks ~mod* hs)
cluster_size = this.alloc!(n: args.num_blocks ~mod* 4)
histograms = this.alloc!(n: args.num_blocks.min(no_more_than: HISTOGRAMS_PER_BATCH) ~mod* hs)
max_num_pairs = (HISTOGRAMS_PER_BATCH * HISTOGRAMS_PER_BATCH) / 2
pairs = this.alloc!(n: (max_num_pairs + 1) * HISTOGRAM_PAIR_SIZE)
tmp = this.alloc!(n: 2 ~mod* hs)
if this.oom {
return nothing
}
sizes = u32s
new_clusters = u32s ~mod+ (1 * HISTOGRAMS_PER_BATCH * 4)
symbols = u32s ~mod+ (2 * HISTOGRAMS_PER_BATCH * 4)
remap = u32s ~mod+ (3 * HISTOGRAMS_PER_BATCH * 4)
block_lengths = u32s ~mod+ (4 * HISTOGRAMS_PER_BATCH * 4)
this.zero!(wb: args.wb, i: u32s, n: (args.num_blocks ~mod+ (4 * HISTOGRAMS_PER_BATCH)) ~mod* 4)
// Calculate block lengths (convert repeating values -> series length).
block_idx = 0
i = 0
while i < args.length {
this.st32!(wb: args.wb, i: block_lengths ~mod+ (block_idx ~mod* 4),
v: this.ld32(wb: args.wb, i: block_lengths ~mod+ (block_idx ~mod* 4)) ~mod+ 1)
if ((i ~mod+ 1) == args.length) or
(this.ld8(wb: args.wb, i: args.block_ids ~mod+ i) <> this.ld8(wb: args.wb, i: (args.block_ids ~mod+ i) ~mod+ 1)) {
block_idx ~mod+= 1
}
i ~mod+= 1
}
// Pre-cluster blocks (cluster batches).
i = 0
while i < args.num_blocks {
num_to_combine = args.num_blocks ~mod- i
num_to_combine = num_to_combine.min(no_more_than: HISTOGRAMS_PER_BATCH)
j = 0
while j < num_to_combine {
block_length = this.ld32(wb: args.wb, i: block_lengths ~mod+ ((i ~mod+ j) ~mod* 4)) as base.u64
this.h_clear!(wb: args.wb, h: histograms ~mod+ (j ~mod* hs), ds: args.ds)
k = 0
while k < block_length {
this.h_add!(wb: args.wb, h: histograms ~mod+ (j ~mod* hs), ds: args.ds,
val: this.sym(wb: args.wb, base: args.data, i: pos, width: args.width))
pos ~mod+= 1
k ~mod+= 1
}
this.h_set_bit_cost!(wb: args.wb, h: histograms ~mod+ (j ~mod* hs), ds: args.ds,
bit_cost: this.h_population_cost(wb: args.wb, h: histograms ~mod+ (j ~mod* hs), ds: args.ds))
this.st32!(wb: args.wb, i: new_clusters ~mod+ (j ~mod* 4), v: (j & 0xFFFF_FFFF) as base.u32)
this.st32!(wb: args.wb, i: symbols ~mod+ (j ~mod* 4), v: (j & 0xFFFF_FFFF) as base.u32)
this.st32!(wb: args.wb, i: sizes ~mod+ (j ~mod* 4), v: 1)
j ~mod+= 1
}
num_new_clusters = this.histogram_combine!(wb: args.wb, ds: args.ds, out: histograms, tmp: tmp,
cluster_size: sizes, symbols: symbols, clusters: new_clusters, pairs: pairs,
num_clusters: num_to_combine, symbols_size: num_to_combine,
max_clusters: HISTOGRAMS_PER_BATCH, max_num_pairs: max_num_pairs)
j = 0
while j < num_new_clusters {
nc = this.ld32(wb: args.wb, i: new_clusters ~mod+ (j ~mod* 4)) as base.u64
this.h_copy!(wb: args.wb, h: all_histograms ~mod+ (all_histograms_size ~mod* hs), v: histograms ~mod+ (nc ~mod* hs), ds: args.ds)
all_histograms_size ~mod+= 1
this.st32!(wb: args.wb, i: cluster_size ~mod+ (cluster_size_size ~mod* 4), v: this.ld32(wb: args.wb, i: sizes ~mod+ (nc ~mod* 4)))
cluster_size_size ~mod+= 1
this.st32!(wb: args.wb, i: remap ~mod+ (nc ~mod* 4), v: (j & 0xFFFF_FFFF) as base.u32)
j ~mod+= 1
}
j = 0
while j < num_to_combine {
s = this.ld32(wb: args.wb, i: symbols ~mod+ (j ~mod* 4))
this.st32!(wb: args.wb, i: histogram_symbols ~mod+ ((i ~mod+ j) ~mod* 4),
v: ((num_clusters & 0xFFFF_FFFF) as base.u32) ~mod+ this.ld32(wb: args.wb, i: remap ~mod+ ((s as base.u64) ~mod* 4)))
j ~mod+= 1
}
num_clusters ~mod+= num_new_clusters
i ~mod+= HISTOGRAMS_PER_BATCH
}
// Final clustering.
max_num_pairs = 64 ~mod* num_clusters
max_num_pairs = max_num_pairs.min(no_more_than: (num_clusters / 2) ~mod* num_clusters)
if ((HISTOGRAMS_PER_BATCH * HISTOGRAMS_PER_BATCH) / 2) < max_num_pairs {
pairs = this.alloc!(n: (max_num_pairs ~mod+ 1) ~mod* HISTOGRAM_PAIR_SIZE)
}
clusters = this.alloc!(n: num_clusters ~mod* 4)
if this.oom {
return nothing
}
i = 0
while i < num_clusters {
this.st32!(wb: args.wb, i: clusters ~mod+ (i ~mod* 4), v: (i & 0xFFFF_FFFF) as base.u32)
i ~mod+= 1
}
num_final_clusters = this.histogram_combine!(wb: args.wb, ds: args.ds, out: all_histograms, tmp: tmp,
cluster_size: cluster_size, symbols: histogram_symbols, clusters: clusters, pairs: pairs,
num_clusters: num_clusters, symbols_size: args.num_blocks,
max_clusters: MAX_NUMBER_OF_HISTOGRAMS, max_num_pairs: max_num_pairs)
// Assign blocks to final histograms.
new_index = this.alloc!(n: num_clusters ~mod* 4)
if this.oom {
return nothing
}
this.fill32!(wb: args.wb, i: new_index, n: num_clusters, v: INVALID_INDEX)
pos = 0
next_index = 0
i = 0
while i < args.num_blocks {
this.h_clear!(wb: args.wb, h: tmp, ds: args.ds)
block_length = this.ld32(wb: args.wb, i: block_lengths ~mod+ (i ~mod* 4)) as base.u64
j = 0
while j < block_length {
this.h_add!(wb: args.wb, h: tmp, ds: args.ds, val: this.sym(wb: args.wb, base: args.data, i: pos, width: args.width))
pos ~mod+= 1
j ~mod+= 1
}
// Among equally good histograms prefer last used.
if i == 0 {
best_out = this.ld32(wb: args.wb, i: histogram_symbols)
} else {
best_out = this.ld32(wb: args.wb, i: histogram_symbols ~mod+ ((i ~mod- 1) ~mod* 4))
}
best_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds, histogram: tmp,
candidate: all_histograms ~mod+ ((best_out as base.u64) ~mod* hs), tmp: tmp ~mod+ hs)
j = 0
while j < num_final_clusters {
s = this.ld32(wb: args.wb, i: clusters ~mod+ (j ~mod* 4))
cur_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds, histogram: tmp,
candidate: all_histograms ~mod+ ((s as base.u64) ~mod* hs), tmp: tmp ~mod+ hs)
if this.sf.f64_lt(a: cur_bits, b: best_bits) <> 0 {
best_bits = cur_bits
best_out = s
}
j ~mod+= 1
}
this.st32!(wb: args.wb, i: histogram_symbols ~mod+ (i ~mod* 4), v: best_out)
if this.ld32(wb: args.wb, i: new_index ~mod+ ((best_out as base.u64) ~mod* 4)) == INVALID_INDEX {
this.st32!(wb: args.wb, i: new_index ~mod+ ((best_out as base.u64) ~mod* 4), v: next_index)
next_index ~mod+= 1
}
i ~mod+= 1
}
// Rewrite final assignment to block-split. There might be less blocks
// than num_blocks due to clustering.
types_off = this.split_types_off[args.cat]
lengths_off = this.split_lengths_off[args.cat]
cur_length = 0
block_idx = 0
max_type = 0
i = 0
while i < args.num_blocks {
cur_length ~mod+= this.ld32(wb: args.wb, i: block_lengths ~mod+ (i ~mod* 4))
s = this.ld32(wb: args.wb, i: histogram_symbols ~mod+ (i ~mod* 4))
if ((i ~mod+ 1) == args.num_blocks) or
(s <> this.ld32(wb: args.wb, i: histogram_symbols ~mod+ ((i ~mod+ 1) ~mod* 4))) {
id = this.ld32(wb: args.wb, i: new_index ~mod+ ((s as base.u64) ~mod* 4)) & 0xFF
this.st8!(wb: args.wb, i: types_off ~mod+ block_idx, v: id as base.u8)
this.st32!(wb: args.wb, i: lengths_off ~mod+ (block_idx ~mod* 4), v: cur_length)
max_type = max_type.max(no_less_than: id)
cur_length = 0
block_idx ~mod+= 1
}
i ~mod+= 1
}
this.split_num_blocks[args.cat] = block_idx
this.split_num_types[args.cat] = (max_type as base.u64) + 1
}
// Mirrors FN(SplitByteVector) in block_splitter_inc.h. It writes the result
// to this.split_etc[cat].
pri func metablock_builder.split_byte_vector!(wb: slice base.u8, cat: base.u32[..= 2], data: base.u64, length: base.u64, width: base.u32,
ds: base.u32[..= 704], symbols_per_histogram: base.u64, max_histograms: base.u64,
sampling_stride_length: base.u64, block_switch_cost: base.u64) {
var num_histograms : base.u64
var nb : base.u64
var hs : base.u64
var mark : base.u64
var block_ids : base.u64
var histograms : base.u64
var tmp : base.u64
var bitmaplen : base.u64
var switch_signal : base.u64
var iters : base.u64
var i : base.u64
var num_blocks : base.u64
hs = this.hsize(ds: args.ds)
// Calculate number of histograms; initial estimate is one histogram per
// specified amount of symbols; however, this value is capped.
num_histograms = (args.length / args.symbols_per_histogram.max(no_less_than: 1)) ~mod+ 1
if num_histograms > args.max_histograms {
num_histograms = args.max_histograms
}
// Corner case: no input.
if args.length == 0 {
this.split_num_types[args.cat] = 1
return nothing
}
if args.length < 128 {
nb = this.split_num_blocks[args.cat]
this.split_num_types[args.cat] = 1
this.st8!(wb: args.wb, i: this.split_types_off[args.cat] ~mod+ nb, v: 0)
this.st32!(wb: args.wb, i: this.split_lengths_off[args.cat] ~mod+ (nb ~mod* 4), v: (args.length & 0xFFFF_FFFF) as base.u32)
this.split_num_blocks[args.cat] = nb ~mod+ 1
return nothing
}
// The C code allocates block_ids after histograms, and frees histograms
// (and FindBlocks' arrays) before ClusterBlocks. Here, block_ids comes
// first, so that the bump allocator can reclaim the others.
block_ids = this.alloc!(n: args.length)
mark = this.wb_top
histograms = this.alloc!(n: (num_histograms ~mod+ 1) ~mod* hs)
tmp = histograms ~mod+ (num_histograms ~mod* hs)
bitmaplen = (num_histograms ~mod+ 7) >> 3
switch_signal = this.alloc!(n: args.length ~mod* bitmaplen)
if this.oom {
return nothing
}
// Find good entropy codes.
this.initial_entropy_codes!(wb: args.wb, data: args.data, length: args.length, width: args.width,
stride: args.sampling_stride_length, num_histograms: num_histograms, histograms: histograms, ds: args.ds)
this.refine_entropy_codes!(wb: args.wb, data: args.data, length: args.length, width: args.width,
stride: args.sampling_stride_length, num_histograms: num_histograms, histograms: histograms, tmp: tmp, ds: args.ds)
// Find a good path through literals with the good entropy codes.
iters = 3
if this.quality >= 11 {
iters = 10
}
i = 0
while i < iters {
num_blocks = this.find_blocks!(wb: args.wb, data: args.data, length: args.length, width: args.width,
block_switch_bitcost: args.block_switch_cost, num_histograms: num_histograms, histograms: histograms,
switch_signal: switch_signal, block_id: block_ids, ds: args.ds)
num_histograms = this.remap_block_ids!(wb: args.wb, block_ids: block_ids, length: args.length, num_histograms: num_histograms)
this.build_block_histograms!(wb: args.wb, data: args.data, length: args.length, width: args.width,
block_ids: block_ids, num_histograms: num_histograms, histograms: histograms, ds: args.ds)
i ~mod+= 1
}
this.wb_top = mark
this.cluster_blocks!(wb: args.wb, cat: args.cat, data: args.data, length: args.length, width: args.width,
num_blocks: num_blocks, block_ids: block_ids, ds: args.ds)
}
+481
View File
@@ -0,0 +1,481 @@
// 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 cluster.h, cluster.c and cluster_inc.h. The functions are
// metablock_builder methods. The C code's templated functions (FN(X), for X
// in Literal, Command and Distance) are one Wuffs function each, with a ds
// argument (the histogram's data size: 256, 704 or 544).
//
// Arrays (histograms, uint32_t arrays and HistogramPair arrays) are given as
// offsets into wb. A HistogramPair is HISTOGRAM_PAIR_SIZE bytes: idx1
// (u32le), idx2 (u32le), cost_combo (f64 bits, u64le), cost_diff (f64 bits,
// u64le).
// Mirrors HistogramPairIsLess in cluster.c, given the two pairs' cost_diff,
// idx1 and idx2 fields.
pri func metablock_builder.histogram_pair_is_less(
p1_cost_diff: base.u64, p1_idx1: base.u32, p1_idx2: base.u32,
p2_cost_diff: base.u64, p2_idx1: base.u32, p2_idx2: base.u32) base.bool {
if this.sf.f64_ne(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0 {
return this.sf.f64_gt(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0
}
return (args.p1_idx2 ~mod- args.p1_idx1) > (args.p2_idx2 ~mod- args.p2_idx1)
}
// Mirrors ClusterCostDiff in cluster.c.
pri func metablock_builder.cluster_cost_diff(size_a: base.u64, size_b: base.u64) base.u64 {
var size_c : base.u64
size_c = args.size_a ~mod+ args.size_b
return this.sf.f64_sub(
a: this.sf.f64_add(
a: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.size_a), b: this.bc.fast_log2(v: args.size_a)),
b: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.size_b), b: this.bc.fast_log2(v: args.size_b))),
b: this.sf.f64_mul(a: this.sf.f64_from_u64(a: size_c), b: this.bc.fast_log2(v: size_c)))
}
// pair_copy is the struct assignment pairs[dst] = pairs[src], where dst and
// src are byte offsets in wb.
pri func metablock_builder.pair_copy!(wb: slice base.u8, dst: base.u64, src: base.u64) {
this.st64!(wb: args.wb, i: args.dst, v: this.ld64(wb: args.wb, i: args.src))
this.st64!(wb: args.wb, i: args.dst ~mod+ 8, v: this.ld64(wb: args.wb, i: args.src ~mod+ 8))
this.st64!(wb: args.wb, i: args.dst ~mod+ 16, v: this.ld64(wb: args.wb, i: args.src ~mod+ 16))
}
// pair_set sets the pair at wb[dst ..].
pri func metablock_builder.pair_set!(wb: slice base.u8, dst: base.u64, idx1: base.u32, idx2: base.u32, cost_combo: base.u64, cost_diff: base.u64) {
this.st32!(wb: args.wb, i: args.dst, v: args.idx1)
this.st32!(wb: args.wb, i: args.dst ~mod+ 4, v: args.idx2)
this.st64!(wb: args.wb, i: args.dst ~mod+ 8, v: args.cost_combo)
this.st64!(wb: args.wb, i: args.dst ~mod+ 16, v: args.cost_diff)
}
// Mirrors FN(BrotliCompareAndPushToQueue) in cluster_inc.h. The C code's
// *num_pairs is this.num_pairs.
pri func metablock_builder.compare_and_push_to_queue!(wb: slice base.u8, ds: base.u32[..= 704], out: base.u64, tmp: base.u64,
cluster_size: base.u64, idx1: base.u32, idx2: base.u32, max_num_pairs: base.u64, pairs: base.u64) {
var hs : base.u64
var is_good_pair : base.bool
var idx1 : base.u32
var idx2 : base.u32
var t : base.u32
var cost_diff : base.u64
var cost_combo : base.u64
var threshold : base.u64
var h1 : base.u64
var h2 : base.u64
var c : base.u64
hs = this.hsize(ds: args.ds)
if args.idx1 == args.idx2 {
return nothing
}
idx1 = args.idx1
idx2 = args.idx2
if idx2 < idx1 {
t = idx2
idx2 = idx1
idx1 = t
}
h1 = args.out ~mod+ ((idx1 as base.u64) ~mod* hs)
h2 = args.out ~mod+ ((idx2 as base.u64) ~mod* hs)
cost_diff = this.sf.f64_mul(a: 0x3FE0_0000_0000_0000, b: this.cluster_cost_diff(
size_a: this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((idx1 as base.u64) ~mod* 4)) as base.u64,
size_b: this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((idx2 as base.u64) ~mod* 4)) as base.u64))
cost_diff = this.sf.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h1, ds: args.ds))
cost_diff = this.sf.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds))
if this.h_total(wb: args.wb, h: h1, ds: args.ds) == 0 {
cost_combo = this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds)
is_good_pair = true
} else if this.h_total(wb: args.wb, h: h2, ds: args.ds) == 0 {
cost_combo = this.h_bit_cost(wb: args.wb, h: h1, ds: args.ds)
is_good_pair = true
} else {
if this.num_pairs == 0 {
threshold = F64_1E99
} else {
threshold = this.sf.f64_max(a: 0, b: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16))
}
this.h_copy!(wb: args.wb, h: args.tmp, v: h1, ds: args.ds)
this.h_add_histogram!(wb: args.wb, h: args.tmp, v: h2, ds: args.ds)
c = this.h_population_cost(wb: args.wb, h: args.tmp, ds: args.ds)
if this.sf.f64_lt(a: c, b: this.sf.f64_sub(a: threshold, b: cost_diff)) <> 0 {
cost_combo = c
is_good_pair = true
}
}
if is_good_pair {
cost_diff = this.sf.f64_add(a: cost_diff, b: cost_combo)
if (this.num_pairs > 0) and this.histogram_pair_is_less(
p1_cost_diff: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16),
p1_idx1: this.ld32(wb: args.wb, i: args.pairs),
p1_idx2: this.ld32(wb: args.wb, i: args.pairs ~mod+ 4),
p2_cost_diff: cost_diff,
p2_idx1: idx1,
p2_idx2: idx2) {
// Replace the top of the queue if needed.
if this.num_pairs < args.max_num_pairs {
this.pair_copy!(wb: args.wb, dst: args.pairs ~mod+ (this.num_pairs ~mod* HISTOGRAM_PAIR_SIZE), src: args.pairs)
this.num_pairs ~mod+= 1
}
this.pair_set!(wb: args.wb, dst: args.pairs, idx1: idx1, idx2: idx2, cost_combo: cost_combo, cost_diff: cost_diff)
} else if this.num_pairs < args.max_num_pairs {
this.pair_set!(wb: args.wb, dst: args.pairs ~mod+ (this.num_pairs ~mod* HISTOGRAM_PAIR_SIZE),
idx1: idx1, idx2: idx2, cost_combo: cost_combo, cost_diff: cost_diff)
this.num_pairs ~mod+= 1
}
}
}
// Mirrors FN(BrotliHistogramCombine) in cluster_inc.h. out, tmp,
// cluster_size, symbols, clusters and pairs are offsets in wb.
pri func metablock_builder.histogram_combine!(wb: slice base.u8, ds: base.u32[..= 704], out: base.u64, tmp: base.u64,
cluster_size: base.u64, symbols: base.u64, clusters: base.u64, pairs: base.u64,
num_clusters: base.u64, symbols_size: base.u64, max_clusters: base.u64, max_num_pairs: base.u64) base.u64 {
var hs : base.u64
var cost_diff_threshold : base.u64
var min_cluster_size : base.u64
var num_clusters : base.u64
var idx1 : base.u64
var idx2 : base.u64
var best_idx1 : base.u32
var best_idx2 : base.u32
var i : base.u64
var j : base.u64
var copy_to_idx : base.u64
var p : base.u64
var p_idx1 : base.u32
var p_idx2 : base.u32
var p_cost_combo : base.u64
var p_cost_diff : base.u64
hs = this.hsize(ds: args.ds)
cost_diff_threshold = 0
min_cluster_size = 1
num_clusters = args.num_clusters
this.num_pairs = 0
// We maintain a vector of histogram pairs, with the property that the
// pair with the maximum bit cost reduction is the first.
idx1 = 0
while idx1 < num_clusters {
idx2 = idx1 ~mod+ 1
while idx2 < num_clusters {
this.compare_and_push_to_queue!(wb: args.wb, ds: args.ds, out: args.out, tmp: args.tmp,
cluster_size: args.cluster_size,
idx1: this.ld32(wb: args.wb, i: args.clusters ~mod+ (idx1 ~mod* 4)),
idx2: this.ld32(wb: args.wb, i: args.clusters ~mod+ (idx2 ~mod* 4)),
max_num_pairs: args.max_num_pairs, pairs: args.pairs)
idx2 ~mod+= 1
}
idx1 ~mod+= 1
}
while num_clusters > min_cluster_size {
if this.sf.f64_ge(a: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16), b: cost_diff_threshold) <> 0 {
cost_diff_threshold = F64_1E99
min_cluster_size = args.max_clusters
continue
}
// Take the best pair from the top of heap.
best_idx1 = this.ld32(wb: args.wb, i: args.pairs)
best_idx2 = this.ld32(wb: args.wb, i: args.pairs ~mod+ 4)
this.h_add_histogram!(wb: args.wb,
h: args.out ~mod+ ((best_idx1 as base.u64) ~mod* hs),
v: args.out ~mod+ ((best_idx2 as base.u64) ~mod* hs), ds: args.ds)
this.h_set_bit_cost!(wb: args.wb, h: args.out ~mod+ ((best_idx1 as base.u64) ~mod* hs), ds: args.ds,
bit_cost: this.ld64(wb: args.wb, i: args.pairs ~mod+ 8))
this.st32!(wb: args.wb, i: args.cluster_size ~mod+ ((best_idx1 as base.u64) ~mod* 4),
v: this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((best_idx1 as base.u64) ~mod* 4)) ~mod+
this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((best_idx2 as base.u64) ~mod* 4)))
i = 0
while i < args.symbols_size {
if this.ld32(wb: args.wb, i: args.symbols ~mod+ (i ~mod* 4)) == best_idx2 {
this.st32!(wb: args.wb, i: args.symbols ~mod+ (i ~mod* 4), v: best_idx1)
}
i ~mod+= 1
}
i = 0
while i < num_clusters {
if this.ld32(wb: args.wb, i: args.clusters ~mod+ (i ~mod* 4)) == best_idx2 {
// memmove(&clusters[i], &clusters[i + 1], ...).
j = i
while (j ~mod+ 1) < num_clusters {
this.st32!(wb: args.wb, i: args.clusters ~mod+ (j ~mod* 4),
v: this.ld32(wb: args.wb, i: args.clusters ~mod+ ((j ~mod+ 1) ~mod* 4)))
j ~mod+= 1
}
break
}
i ~mod+= 1
}
num_clusters ~mod-= 1
// Remove pairs intersecting the just combined best pair.
copy_to_idx = 0
i = 0
while i < this.num_pairs {
p = args.pairs ~mod+ (i ~mod* HISTOGRAM_PAIR_SIZE)
p_idx1 = this.ld32(wb: args.wb, i: p)
p_idx2 = this.ld32(wb: args.wb, i: p ~mod+ 4)
p_cost_combo = this.ld64(wb: args.wb, i: p ~mod+ 8)
p_cost_diff = this.ld64(wb: args.wb, i: p ~mod+ 16)
if (p_idx1 == best_idx1) or (p_idx2 == best_idx1) or (p_idx1 == best_idx2) or (p_idx2 == best_idx2) {
// Remove invalid pair from the queue.
i ~mod+= 1
continue
}
if this.histogram_pair_is_less(
p1_cost_diff: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16),
p1_idx1: this.ld32(wb: args.wb, i: args.pairs),
p1_idx2: this.ld32(wb: args.wb, i: args.pairs ~mod+ 4),
p2_cost_diff: p_cost_diff,
p2_idx1: p_idx1,
p2_idx2: p_idx2) {
// Replace the top of the queue if needed.
this.pair_copy!(wb: args.wb, dst: args.pairs ~mod+ (copy_to_idx ~mod* HISTOGRAM_PAIR_SIZE), src: args.pairs)
this.pair_set!(wb: args.wb, dst: args.pairs, idx1: p_idx1, idx2: p_idx2, cost_combo: p_cost_combo, cost_diff: p_cost_diff)
} else {
this.pair_set!(wb: args.wb, dst: args.pairs ~mod+ (copy_to_idx ~mod* HISTOGRAM_PAIR_SIZE),
idx1: p_idx1, idx2: p_idx2, cost_combo: p_cost_combo, cost_diff: p_cost_diff)
}
copy_to_idx ~mod+= 1
i ~mod+= 1
}
this.num_pairs = copy_to_idx
// Push new pairs formed with the combined histogram to the heap.
i = 0
while i < num_clusters {
this.compare_and_push_to_queue!(wb: args.wb, ds: args.ds, out: args.out, tmp: args.tmp,
cluster_size: args.cluster_size,
idx1: best_idx1,
idx2: this.ld32(wb: args.wb, i: args.clusters ~mod+ (i ~mod* 4)),
max_num_pairs: args.max_num_pairs, pairs: args.pairs)
i ~mod+= 1
}
}
return num_clusters
}
// Mirrors FN(BrotliHistogramBitCostDistance) in cluster_inc.h.
pri func metablock_builder.histogram_bit_cost_distance!(wb: slice base.u8, ds: base.u32[..= 704],
histogram: base.u64, candidate: base.u64, tmp: base.u64) base.u64 {
if this.h_total(wb: args.wb, h: args.histogram, ds: args.ds) == 0 {
return 0
}
this.h_copy!(wb: args.wb, h: args.tmp, v: args.histogram, ds: args.ds)
this.h_add_histogram!(wb: args.wb, h: args.tmp, v: args.candidate, ds: args.ds)
return this.sf.f64_sub(
a: this.h_population_cost(wb: args.wb, h: args.tmp, ds: args.ds),
b: this.h_bit_cost(wb: args.wb, h: args.candidate, ds: args.ds))
}
// Mirrors FN(BrotliHistogramRemap) in cluster_inc.h.
pri func metablock_builder.histogram_remap!(wb: slice base.u8, ds: base.u32[..= 704], in_histos: base.u64, in_size: base.u64,
clusters: base.u64, num_clusters: base.u64, out: base.u64, tmp: base.u64, symbols: base.u64) {
var hs : base.u64
var i : base.u64
var j : base.u64
var best_out : base.u32
var best_bits : base.u64
var cur_bits : base.u64
var c : base.u32
hs = this.hsize(ds: args.ds)
i = 0
while i < args.in_size {
if i == 0 {
best_out = this.ld32(wb: args.wb, i: args.symbols)
} else {
best_out = this.ld32(wb: args.wb, i: args.symbols ~mod+ ((i ~mod- 1) ~mod* 4))
}
best_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds,
histogram: args.in_histos ~mod+ (i ~mod* hs),
candidate: args.out ~mod+ ((best_out as base.u64) ~mod* hs), tmp: args.tmp)
j = 0
while j < args.num_clusters {
c = this.ld32(wb: args.wb, i: args.clusters ~mod+ (j ~mod* 4))
cur_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds,
histogram: args.in_histos ~mod+ (i ~mod* hs),
candidate: args.out ~mod+ ((c as base.u64) ~mod* hs), tmp: args.tmp)
if this.sf.f64_lt(a: cur_bits, b: best_bits) <> 0 {
best_bits = cur_bits
best_out = c
}
j ~mod+= 1
}
this.st32!(wb: args.wb, i: args.symbols ~mod+ (i ~mod* 4), v: best_out)
i ~mod+= 1
}
// Recompute each out based on raw and symbols.
i = 0
while i < args.num_clusters {
c = this.ld32(wb: args.wb, i: args.clusters ~mod+ (i ~mod* 4))
this.h_clear!(wb: args.wb, h: args.out ~mod+ ((c as base.u64) ~mod* hs), ds: args.ds)
i ~mod+= 1
}
i = 0
while i < args.in_size {
c = this.ld32(wb: args.wb, i: args.symbols ~mod+ (i ~mod* 4))
this.h_add_histogram!(wb: args.wb,
h: args.out ~mod+ ((c as base.u64) ~mod* hs),
v: args.in_histos ~mod+ (i ~mod* hs), ds: args.ds)
i ~mod+= 1
}
}
// Mirrors FN(BrotliHistogramReindex) in cluster_inc.h.
pri func metablock_builder.histogram_reindex!(wb: slice base.u8, ds: base.u32[..= 704], out: base.u64, symbols: base.u64, length: base.u64) base.u64 {
var hs : base.u64
var mark : base.u64
var new_index : base.u64
var next_index : base.u32
var tmp : base.u64
var i : base.u64
var s : base.u32
var ni : base.u32
hs = this.hsize(ds: args.ds)
mark = this.wb_top
new_index = this.alloc!(n: args.length ~mod* 4)
if this.oom {
return 0
}
this.fill32!(wb: args.wb, i: new_index, n: args.length, v: INVALID_INDEX)
next_index = 0
i = 0
while i < args.length {
s = this.ld32(wb: args.wb, i: args.symbols ~mod+ (i ~mod* 4))
if this.ld32(wb: args.wb, i: new_index ~mod+ ((s as base.u64) ~mod* 4)) == INVALID_INDEX {
this.st32!(wb: args.wb, i: new_index ~mod+ ((s as base.u64) ~mod* 4), v: next_index)
next_index ~mod+= 1
}
i ~mod+= 1
}
tmp = this.alloc!(n: (next_index as base.u64) ~mod* hs)
if this.oom {
return 0
}
next_index = 0
i = 0
while i < args.length {
s = this.ld32(wb: args.wb, i: args.symbols ~mod+ (i ~mod* 4))
ni = this.ld32(wb: args.wb, i: new_index ~mod+ ((s as base.u64) ~mod* 4))
if ni == next_index {
this.h_copy!(wb: args.wb, h: tmp ~mod+ ((next_index as base.u64) ~mod* hs), v: args.out ~mod+ ((s as base.u64) ~mod* hs), ds: args.ds)
next_index ~mod+= 1
}
this.st32!(wb: args.wb, i: args.symbols ~mod+ (i ~mod* 4), v: ni)
i ~mod+= 1
}
i = 0
while i < (next_index as base.u64) {
this.h_copy!(wb: args.wb, h: args.out ~mod+ (i ~mod* hs), v: tmp ~mod+ (i ~mod* hs), ds: args.ds)
i ~mod+= 1
}
this.wb_top = mark
return next_index as base.u64
}
// Mirrors FN(BrotliClusterHistograms) in cluster_inc.h. The in_size input
// histograms are at in_histos, the output histograms (with room for in_size
// histograms) at out_histos and the histogram_symbols (in_size u32les) at
// histogram_symbols. It returns the C code's *out_size.
pri func metablock_builder.cluster_histograms!(wb: slice base.u8, ds: base.u32[..= 704], in_histos: base.u64, in_size: base.u64,
max_histograms: base.u64, out_histos: base.u64, histogram_symbols: base.u64) base.u64 {
var hs : base.u64
var mark : base.u64
var cluster_size : base.u64
var clusters : base.u64
var num_clusters : base.u64
var pairs_capacity : base.u64
var pairs : base.u64
var tmp : base.u64
var i : base.u64
var j : base.u64
var num_to_combine : base.u64
var num_new_clusters : base.u64
var max_num_pairs : base.u64
var out_size : base.u64
hs = this.hsize(ds: args.ds)
mark = this.wb_top
cluster_size = this.alloc!(n: args.in_size ~mod* 4)
clusters = this.alloc!(n: args.in_size ~mod* 4)
// max_input_histograms is 64.
pairs_capacity = (64 * 64) / 2
pairs = this.alloc!(n: (pairs_capacity + 1) * HISTOGRAM_PAIR_SIZE)
tmp = this.alloc!(n: hs)
if this.oom {
return 0
}
this.fill32!(wb: args.wb, i: cluster_size, n: args.in_size, v: 1)
i = 0
while i < args.in_size {
this.h_copy!(wb: args.wb, h: args.out_histos ~mod+ (i ~mod* hs), v: args.in_histos ~mod+ (i ~mod* hs), ds: args.ds)
this.h_set_bit_cost!(wb: args.wb, h: args.out_histos ~mod+ (i ~mod* hs), ds: args.ds,
bit_cost: this.h_population_cost(wb: args.wb, h: args.in_histos ~mod+ (i ~mod* hs), ds: args.ds))
this.st32!(wb: args.wb, i: args.histogram_symbols ~mod+ (i ~mod* 4), v: (i & 0xFFFF_FFFF) as base.u32)
i ~mod+= 1
}
i = 0
while i < args.in_size {
num_to_combine = args.in_size ~mod- i
num_to_combine = num_to_combine.min(no_more_than: 64)
j = 0
while j < num_to_combine {
this.st32!(wb: args.wb, i: clusters ~mod+ ((num_clusters ~mod+ j) ~mod* 4), v: ((i ~mod+ j) & 0xFFFF_FFFF) as base.u32)
j ~mod+= 1
}
num_new_clusters = this.histogram_combine!(wb: args.wb, ds: args.ds, out: args.out_histos, tmp: tmp,
cluster_size: cluster_size, symbols: args.histogram_symbols ~mod+ (i ~mod* 4),
clusters: clusters ~mod+ (num_clusters ~mod* 4), pairs: pairs,
num_clusters: num_to_combine, symbols_size: num_to_combine,
max_clusters: args.max_histograms, max_num_pairs: pairs_capacity)
num_clusters ~mod+= num_new_clusters
i ~mod+= 64
}
// For the second pass, we limit the total number of histogram pairs.
// After this limit is reached, we only keep searching for the best pair.
max_num_pairs = 64 ~mod* num_clusters
max_num_pairs = max_num_pairs.min(no_more_than: (num_clusters / 2) ~mod* num_clusters)
if pairs_capacity < (max_num_pairs ~mod+ 1) {
// BROTLI_ENSURE_CAPACITY. The C code copies the old pairs, but the
// second BrotliHistogramCombine call never reads them.
pairs = this.alloc!(n: (max_num_pairs ~mod+ 1) ~mod* HISTOGRAM_PAIR_SIZE)
if this.oom {
return 0
}
}
// Collapse similar histograms.
num_clusters = this.histogram_combine!(wb: args.wb, ds: args.ds, out: args.out_histos, tmp: tmp,
cluster_size: cluster_size, symbols: args.histogram_symbols,
clusters: clusters, pairs: pairs,
num_clusters: num_clusters, symbols_size: args.in_size,
max_clusters: args.max_histograms, max_num_pairs: max_num_pairs)
// Find the optimal map from original histograms to the final ones.
this.histogram_remap!(wb: args.wb, ds: args.ds, in_histos: args.in_histos, in_size: args.in_size,
clusters: clusters, num_clusters: num_clusters, out: args.out_histos, tmp: tmp,
symbols: args.histogram_symbols)
// Convert the context map to a canonical form.
out_size = this.histogram_reindex!(wb: args.wb, ds: args.ds, out: args.out_histos,
symbols: args.histogram_symbols, length: args.in_size)
this.wb_top = mark
return out_size
}
+1049
View File
@@ -0,0 +1,1049 @@
// 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 metablock.h and metablock.c (except for
// metablock_inc.h's greedy block splitter and BrotliBuildMetaBlockGreedy):
// BrotliInitDistanceParams, BrotliBuildMetaBlock (with its distance
// parameter search) and BrotliOptimizeHistograms. The functions are methods
// of the metablock_builder struct, whose other methods (in
// encode_block_splitter.wuffs and encode_cluster.wuffs) mirror
// block_splitter.c and cluster.c.
//
// Memory: the C code allocates its scratch and output arrays on the heap.
// Here, they all live in a caller-supplied workbuf (the wb argument), carved
// out by a bump allocator (the wb_top field). See README.md ("Encoder
// Metablock Builder") for the layout. A MetaBlockSplit's outputs are
// described by this struct's fields: offsets into wb and sizes.
//
// Histogram, command and block split byte layouts are described in
// encode_bit_cost.wuffs and encode_histogram.wuffs. Arrays of uint32_t
// (context maps, histogram symbols, etc.) are u32le values, arrays of
// uint16_t are u16le values and doubles are the u64le bits of a binary64.
// The C code's kMaxNumberOfHistograms in BrotliBuildMetaBlock (histogram ids
// need to fit in one byte), also BROTLI_MAX_NUMBER_OF_BLOCK_TYPES.
pri const MAX_NUMBER_OF_HISTOGRAMS : base.u64 = 256
// The C code's kInvalidIndex (BROTLI_UINT32_MAX).
pri const INVALID_INDEX : base.u32 = 0xFFFF_FFFF
// HISTOGRAM_PAIR_SIZE is the byte size of the C code's HistogramPair struct:
// idx1 (u32le), idx2 (u32le), cost_combo (f64), cost_diff (f64).
pri const HISTOGRAM_PAIR_SIZE : base.u64 = 24
// Indexes into the metablock_builder's per-category arrays.
pri const CATEGORY_LITERAL : base.u32 = 0
pri const CATEGORY_COMMAND : base.u32 = 1
pri const CATEGORY_DISTANCE : base.u32 = 2
pri struct metablock_builder?(
// The C code's BrotliEncoderParams fields that these functions use.
quality : base.u32,
large_window : base.bool,
disable_lcm : base.bool,
// The C code's params->dist (a BrotliDistanceParams). The
// build_meta_block method reads it (the original params) and then
// overwrites it with the chosen distance params.
dist_postfix_bits : base.u32,
dist_num_direct_codes : base.u32,
dist_alphabet_size_max : base.u32,
dist_alphabet_size_limit : base.u32,
dist_max_distance : base.u64,
// The MetaBlockSplit's three BlockSplits, indexed by CATEGORY_ETC:
// num_types, num_blocks and the offsets in wb of the types (u8) and
// lengths (u32le) arrays.
split_num_types : array[3] base.u64,
split_num_blocks : array[3] base.u64,
split_types_off : array[3] base.u64,
split_lengths_off : array[3] base.u64,
// The MetaBlockSplit's context maps (u32le arrays in wb) and
// histograms (arrays of histograms in wb), indexed by CATEGORY_ETC
// (there is no command context map).
context_map_off : array[3] base.u64,
context_map_size : array[3] base.u64,
histograms_off : array[3] base.u64,
histograms_size : array[3] base.u64,
// The bump allocator: wb's length, the next free offset, the highest
// offset used so far and whether an allocation failed.
wb_len : base.u64,
wb_top : base.u64,
wb_high : base.u64,
oom : base.bool,
// num_pairs is BrotliHistogramCombine's num_pairs, which
// BrotliCompareAndPushToQueue updates.
num_pairs : base.u64,
// dist_cost is ComputeDistanceCost's *cost result.
dist_cost : base.u64,
sf : softfloat.arith,
) + (
// Wuffs' C code generator expects same-package struct fields to be in
// the private_data section (after the "+").
bc : bit_cost,
// FindBlocks' cost and insert_cost arrays (doubles). insert_cost has
// (data_size * num_histograms) elements, and num_histograms is at
// most 100 for literals (256 symbols) and 50 for commands (704
// symbols) and distances (544 symbols).
cost : array[256] base.u64,
insert_cost : array[35200] base.u64,
// RemapBlockIds' new_id array.
new_id : array[256] base.u16,
// BrotliOptimizeHistograms' good_for_rle array.
good_for_rle : array[704] base.u8,
)
// -------- Little-endian workbuf accessors.
pri func metablock_builder.ld16(wb: roslice base.u8, i: base.u64) base.u32 {
var q : roslice base.u8
if args.i < args.wb.length() {
q = args.wb[args.i ..]
if q.length() >= 2 {
return q.peek_u16le() as base.u32
}
}
return 0
}
pri func metablock_builder.ld32(wb: roslice base.u8, i: base.u64) base.u32 {
return this.bc.ld32(s: args.wb, i: args.i)
}
pri func metablock_builder.ld64(wb: roslice base.u8, i: base.u64) base.u64 {
return this.bc.ld64(s: args.wb, i: args.i)
}
pri func metablock_builder.st8!(wb: slice base.u8, i: base.u64, v: base.u8) {
if args.i < args.wb.length() {
args.wb[args.i] = args.v
}
}
pri func metablock_builder.st16!(wb: slice base.u8, i: base.u64, v: base.u32) {
var q : slice base.u8
if args.i < args.wb.length() {
q = args.wb[args.i ..]
if q.length() >= 2 {
q.poke_u16le!(a: (args.v & 0xFFFF) as base.u16)
}
}
}
pri func metablock_builder.st32!(wb: slice base.u8, i: base.u64, v: base.u32) {
this.bc.st32!(s: args.wb, i: args.i, v: args.v)
}
pri func metablock_builder.st64!(wb: slice base.u8, i: base.u64, v: base.u64) {
this.bc.st64!(s: args.wb, i: args.i, v: args.v)
}
// fill32 sets n u32le values, starting at wb[i ..], to v.
pri func metablock_builder.fill32!(wb: slice base.u8, i: base.u64, n: base.u64, v: base.u32) {
var j : base.u64
j = 0
while j < args.n {
this.st32!(wb: args.wb, i: args.i ~mod+ (j ~mod* 4), v: args.v)
j ~mod+= 1
}
}
// zero sets n bytes, starting at wb[i ..], to zero.
pri func metablock_builder.zero!(wb: slice base.u8, i: base.u64, n: base.u64) {
var j : base.u64
var hi : base.u64
if args.i <= args.wb.length() {
j = args.wb.length() ~mod- args.i
if args.n < j {
j = args.n
}
hi = args.i ~mod+ j
if (args.i <= hi) and (hi <= args.wb.length()) {
args.wb[args.i .. hi].bulk_memset!(byte_value: 0)
}
}
}
// sym returns the i'th element of a DataType array (u8 if width is 1, u16le
// otherwise) at wb[base ..].
pri func metablock_builder.sym(wb: roslice base.u8, base: base.u64, i: base.u64, width: base.u32) base.u64 {
var j : base.u64
if args.width == 1 {
j = args.base ~mod+ args.i
if j < args.wb.length() {
return args.wb[j] as base.u64
}
return 0
}
return this.ld16(wb: args.wb, i: args.base ~mod+ (args.i ~mod* 2)) as base.u64
}
// -------- The bump allocator.
// alloc returns the offset of n fresh (uninitialized) bytes of the workbuf.
// When out of memory, it sets this.oom and returns this.wb_len (so that the
// bounds-checked accessors ignore any writes).
pri func metablock_builder.alloc!(n: base.u64) base.u64 {
var r : base.u64
if this.oom or (this.wb_top > this.wb_len) or (args.n > (this.wb_len ~mod- this.wb_top)) {
this.oom = true
return this.wb_len
}
r = this.wb_top
this.wb_top = r ~mod+ args.n
if this.wb_high < this.wb_top {
this.wb_high = this.wb_top
}
return r
}
// -------- Histogram helpers, on histograms at offsets in the workbuf.
pri func metablock_builder.hsize(ds: base.u32[..= 704]) base.u64 {
return ((args.ds as base.u64) * 4) + 16
}
pri func metablock_builder.h_clear!(wb: slice base.u8, h: base.u64, ds: base.u32[..= 704]) {
if args.h <= args.wb.length() {
this.bc.histogram_clear!(h: args.wb[args.h ..], data_size: args.ds)
}
}
pri func metablock_builder.h_add!(wb: slice base.u8, h: base.u64, ds: base.u32[..= 704], val: base.u64) {
if args.h <= args.wb.length() {
this.bc.histogram_add!(h: args.wb[args.h ..], data_size: args.ds, val: args.val)
}
}
// h_add_histogram is HistogramAddHistogram(&wb[h], &wb[v]).
pri func metablock_builder.h_add_histogram!(wb: slice base.u8, h: base.u64, v: base.u64, ds: base.u32[..= 704]) {
if (args.h <= args.wb.length()) and (args.v <= args.wb.length()) {
this.bc.histogram_add_histogram!(h: args.wb[args.h ..], v: args.wb[args.v ..], data_size: args.ds)
}
}
// h_copy is the struct assignment wb[h] = wb[v].
pri func metablock_builder.h_copy!(wb: slice base.u8, h: base.u64, v: base.u64, ds: base.u32[..= 704]) {
if (args.h <= args.wb.length()) and (args.v <= args.wb.length()) {
this.bc.histogram_copy!(h: args.wb[args.h ..], v: args.wb[args.v ..], data_size: args.ds)
}
}
pri func metablock_builder.h_total(wb: roslice base.u8, h: base.u64, ds: base.u32[..= 704]) base.u64 {
return this.ld64(wb: args.wb, i: args.h ~mod+ ((args.ds as base.u64) * 4))
}
pri func metablock_builder.h_bit_cost(wb: roslice base.u8, h: base.u64, ds: base.u32[..= 704]) base.u64 {
return this.ld64(wb: args.wb, i: args.h ~mod+ (((args.ds as base.u64) * 4) + 8))
}
pri func metablock_builder.h_set_bit_cost!(wb: slice base.u8, h: base.u64, ds: base.u32[..= 704], bit_cost: base.u64) {
this.st64!(wb: args.wb, i: args.h ~mod+ (((args.ds as base.u64) * 4) + 8), v: args.bit_cost)
}
pri func metablock_builder.h_population_cost(wb: roslice base.u8, h: base.u64, ds: base.u32[..= 704]) base.u64 {
if args.h <= args.wb.length() {
return this.bc.population_cost(histogram: args.wb[args.h ..], data_size: args.ds)
}
return 0
}
// -------- Commands and distance prefixes.
// 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]
x = (args.n & 0xFFFF_FFFF) as base.u32
while x > 1 {
x >>= 1
if r < 31 {
r += 1
}
}
return r
}
// Mirrors PrefixEncodeCopyDistance in prefix.h. It returns the code (the
// C code's *code, in the low 16 bits) and extra bits (the C code's
// *extra_bits, in the high 32 bits).
pri func metablock_builder.prefix_encode_copy_distance(distance_code: base.u64, num_direct_codes: base.u64, postfix_bits: base.u32) base.u64 {
var dist : base.u64
var bucket : base.u64
var postfix_mask : base.u64
var postfix : base.u64
var prefix : base.u64
var offset : base.u64
var nbits : base.u64
var code : base.u64
var extra : base.u64
if args.distance_code < (16 ~mod+ args.num_direct_codes) {
return args.distance_code & 0xFFFF
}
dist = ((1 as base.u64) << ((args.postfix_bits & 31) + 2)) ~mod+
((args.distance_code ~mod- 16) ~mod- args.num_direct_codes)
bucket = (this.log2_floor_non_zero(n: dist) as base.u64) ~mod- 1
postfix_mask = ((1 as base.u64) << (args.postfix_bits & 31)) ~mod- 1
postfix = dist & postfix_mask
prefix = (dist >> (bucket & 63)) & 1
offset = (2 ~mod+ prefix) ~mod<< (bucket & 63)
nbits = bucket ~mod- (args.postfix_bits as base.u64)
code = (nbits ~mod<< 10) | (((16 ~mod+ args.num_direct_codes) ~mod+
(((2 ~mod* (nbits ~mod- 1)) ~mod+ prefix) ~mod<< (args.postfix_bits & 63))) ~mod+ postfix)
extra = (dist ~mod- offset) >> (args.postfix_bits & 63)
return (code & 0xFFFF) | ((extra & 0xFFFF_FFFF) << 32)
}
// Mirrors CommandRestoreDistanceCode in command.h, for the command at wb[c
// ..], with the given (npostfix, ndirect) distance params.
pri func metablock_builder.command_restore_distance_code(wb: roslice base.u8, c: base.u64, npostfix: base.u32, ndirect: base.u32) base.u32 {
var dist_prefix : base.u32
var dcode : base.u32
var nbits : base.u32
var extra : base.u32
var postfix_mask : base.u32
var hcode : base.u32
var lcode : base.u32
var offset : base.u32
dist_prefix = this.ld16(wb: args.wb, i: args.c ~mod+ 14)
if (dist_prefix & 0x3FF) < (16 ~mod+ args.ndirect) {
return dist_prefix & 0x3FF
}
dcode = dist_prefix & 0x3FF
nbits = dist_prefix >> 10
extra = this.ld32(wb: args.wb, i: args.c ~mod+ 8)
postfix_mask = ((1 as base.u32) << (args.npostfix & 31)) ~mod- 1
hcode = ((dcode ~mod- args.ndirect) ~mod- 16) >> (args.npostfix & 31)
lcode = ((dcode ~mod- args.ndirect) ~mod- 16) & postfix_mask
offset = ((2 ~mod+ (hcode & 1)) ~mod<< (nbits & 31)) ~mod- 4
return (((((offset ~mod+ extra) ~mod<< (args.npostfix & 31)) ~mod+ lcode) ~mod+ args.ndirect) ~mod+ 16)
}
// Mirrors BrotliCalculateDistanceCodeLimit in constants.h. It returns
// max_alphabet_size in the low 32 bits and max_distance in the high 32 bits.
pri func metablock_builder.calculate_distance_code_limit(max_distance: base.u32, npostfix: base.u32, ndirect: base.u32) base.u64 {
var forbidden_distance : base.u32
var offset : base.u32
var ndistbits : base.u32
var tmp : base.u32
var half : base.u32
var group : base.u32
var postfix : base.u32
var extra : base.u32
var start : base.u32
var mas : base.u32
var md : base.u32
if args.max_distance <= args.ndirect {
mas = args.max_distance ~mod+ 16
md = args.max_distance
return (mas as base.u64) | ((md as base.u64) << 32)
}
forbidden_distance = args.max_distance ~mod+ 1
offset = (forbidden_distance ~mod- args.ndirect) ~mod- 1
postfix = ((1 as base.u32) << (args.npostfix & 31)) ~mod- 1
offset = (offset >> (args.npostfix & 31)) ~mod+ 4
tmp = offset / 2
while tmp <> 0 {
ndistbits ~mod+= 1
tmp = tmp >> 1
}
ndistbits ~mod-= 1
half = (offset >> (ndistbits & 31)) & 1
group = ((ndistbits ~mod- 1) ~mod<< 1) | half
if group == 0 {
mas = args.ndirect ~mod+ 16
md = args.ndirect
return (mas as base.u64) | ((md as base.u64) << 32)
}
group ~mod-= 1
ndistbits = (group >> 1) ~mod+ 1
extra = ((1 as base.u32) << (ndistbits & 31)) ~mod- 1
start = ((1 as base.u32) << ((ndistbits ~mod+ 1) & 31)) ~mod- 4
start ~mod+= (group & 1) ~mod<< (ndistbits & 31)
mas = (((((group ~mod<< (args.npostfix & 31)) | postfix) ~mod+ args.ndirect) ~mod+ 16) ~mod+ 1)
md = (((((start ~mod+ extra) ~mod<< (args.npostfix & 31)) ~mod+ postfix) ~mod+ args.ndirect) ~mod+ 1)
return (mas as base.u64) | ((md as base.u64) << 32)
}
// Mirrors BrotliInitDistanceParams in metablock.c, setting this struct's
// dist_etc fields (the C code's *params).
pri func metablock_builder.init_distance_params!(npostfix: base.u32, ndirect: base.u32, large_window: base.bool) {
var limit : base.u64
this.dist_postfix_bits = args.npostfix
this.dist_num_direct_codes = args.ndirect
// BROTLI_DISTANCE_ALPHABET_SIZE(npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS).
this.dist_alphabet_size_max = (16 ~mod+ args.ndirect) ~mod+ ((24 as base.u32) ~mod<< ((args.npostfix ~mod+ 1) & 31))
this.dist_alphabet_size_limit = this.dist_alphabet_size_max
this.dist_max_distance = ((args.ndirect ~mod+
((1 as base.u32) ~mod<< (((24 ~mod+ args.npostfix) ~mod+ 2) & 31))) ~mod-
((1 as base.u32) ~mod<< ((args.npostfix ~mod+ 2) & 31))) as base.u64
if args.large_window {
// BROTLI_MAX_ALLOWED_DISTANCE is 0x7FFF_FFFC.
limit = this.calculate_distance_code_limit(max_distance: 0x7FFF_FFFC, npostfix: args.npostfix, ndirect: args.ndirect)
// BROTLI_DISTANCE_ALPHABET_SIZE(npostfix, ndirect,
// BROTLI_LARGE_MAX_DISTANCE_BITS).
this.dist_alphabet_size_max = (16 ~mod+ args.ndirect) ~mod+ ((62 as base.u32) ~mod<< ((args.npostfix ~mod+ 1) & 31))
this.dist_alphabet_size_limit = (limit & 0xFFFF_FFFF) as base.u32
this.dist_max_distance = limit >> 32
}
}
// Mirrors RecomputeDistancePrefixes in metablock.c. The original params are
// (orig_npostfix, orig_ndirect) and the new ones are this.dist_etc.
pri func metablock_builder.recompute_distance_prefixes!(cmds: slice base.u8, num_commands: base.u64, orig_npostfix: base.u32, orig_ndirect: base.u32) {
var i : base.u64
var c : base.u64
var d : base.u32
var r : base.u64
if (args.orig_npostfix == this.dist_postfix_bits) and (args.orig_ndirect == this.dist_num_direct_codes) {
return nothing
}
i = 0
while i < args.num_commands {
c = i ~mod* 16
if ((this.ld32(wb: args.cmds, i: c ~mod+ 4) & 0x1FF_FFFF) <> 0) and
(this.ld16(wb: args.cmds, i: c ~mod+ 12) >= 128) {
d = this.command_restore_distance_code(wb: args.cmds, c: c, npostfix: args.orig_npostfix, ndirect: args.orig_ndirect)
r = this.prefix_encode_copy_distance(
distance_code: d as base.u64,
num_direct_codes: this.dist_num_direct_codes as base.u64,
postfix_bits: this.dist_postfix_bits)
this.st16!(wb: args.cmds, i: c ~mod+ 14, v: (r & 0xFFFF) as base.u32)
this.st32!(wb: args.cmds, i: c ~mod+ 8, v: (r >> 32) as base.u32)
}
i ~mod+= 1
}
}
// Mirrors ComputeDistanceCost in metablock.c. The original params are
// (orig_npostfix, orig_ndirect) and the new ones are (new_npostfix,
// new_ndirect, new_max_distance). tmp is the offset of a distance histogram
// in wb. On success, it sets this.dist_cost and returns true.
pri func metablock_builder.compute_distance_cost!(wb: slice base.u8, cmds: roslice base.u8, num_commands: base.u64,
orig_npostfix: base.u32, orig_ndirect: base.u32,
new_npostfix: base.u32, new_ndirect: base.u32, new_max_distance: base.u64, tmp: base.u64) base.bool {
var i : base.u64
var c : base.u64
var equal : base.bool
var dist_prefix : base.u32
var distance : base.u32
var extra_bits : base.u64
this.h_clear!(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE)
equal = (args.orig_npostfix == args.new_npostfix) and (args.orig_ndirect == args.new_ndirect)
i = 0
while i < args.num_commands {
c = i ~mod* 16
if ((this.ld32(wb: args.cmds, i: c ~mod+ 4) & 0x1FF_FFFF) <> 0) and
(this.ld16(wb: args.cmds, i: c ~mod+ 12) >= 128) {
if equal {
dist_prefix = this.ld16(wb: args.cmds, i: c ~mod+ 14)
} else {
distance = this.command_restore_distance_code(wb: args.cmds, c: c, npostfix: args.orig_npostfix, ndirect: args.orig_ndirect)
if (distance as base.u64) > args.new_max_distance {
return false
}
dist_prefix = (this.prefix_encode_copy_distance(
distance_code: distance as base.u64,
num_direct_codes: args.new_ndirect as base.u64,
postfix_bits: args.new_npostfix) & 0xFFFF) as base.u32
}
this.h_add!(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE, val: (dist_prefix & 0x3FF) as base.u64)
extra_bits = this.sf.f64_add(a: extra_bits, b: this.sf.f64_from_u32(a: dist_prefix >> 10))
}
i ~mod+= 1
}
this.dist_cost = this.sf.f64_add(
a: this.h_population_cost(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE),
b: extra_bits)
return true
}
// set_params sets the C code's BrotliEncoderParams fields that
// build_meta_block uses, other than the distance params (see
// init_distance_params).
pri func metablock_builder.set_params!(quality: base.u32, large_window: base.bool, disable_literal_context_modeling: base.bool) {
this.quality = args.quality
this.large_window = args.large_window
this.disable_lcm = args.disable_literal_context_modeling
}
// alloc_block_splits resets the MetaBlockSplit's three BlockSplits (the C
// code's BrotliInitBlockSplit) and allocates their types and lengths arrays
// (the C code's BROTLI_ENSURE_CAPACITY calls), with room for the maximum
// number of blocks: one per symbol (or one, for an empty category).
pri func metablock_builder.alloc_block_splits!(cmds: roslice base.u8, num_commands: base.u64) {
var num_literals : base.u64
var num_distances : base.u64
var i : base.u64
var c : base.u64
var n : base.u64
i = 0
while i < args.num_commands {
c = i ~mod* 16
num_literals ~mod+= this.ld32(wb: args.cmds, i: c) as base.u64
if ((this.ld32(wb: args.cmds, i: c ~mod+ 4) & 0x1FF_FFFF) <> 0) and
(this.ld16(wb: args.cmds, i: c ~mod+ 12) >= 128) {
num_distances ~mod+= 1
}
i ~mod+= 1
}
i = 0
while i < 3 {
this.split_num_types[i] = 0
this.split_num_blocks[i] = 0
this.context_map_off[i] = 0
this.context_map_size[i] = 0
this.histograms_off[i] = 0
this.histograms_size[i] = 0
i += 1
}
n = num_literals.max(no_less_than: 1)
this.split_types_off[0] = this.alloc!(n: n)
this.split_lengths_off[0] = this.alloc!(n: n ~mod* 4)
n = args.num_commands.max(no_less_than: 1)
this.split_types_off[1] = this.alloc!(n: n)
this.split_lengths_off[1] = this.alloc!(n: n ~mod* 4)
n = num_distances.max(no_less_than: 1)
this.split_types_off[2] = this.alloc!(n: n)
this.split_lengths_off[2] = this.alloc!(n: n ~mod* 4)
}
// reset_arena starts using wb (of length wb_len) as the bump allocator's
// memory.
pri func metablock_builder.reset_arena!(wb_len: base.u64) {
this.wb_len = args.wb_len
this.wb_top = 0
this.wb_high = 0
this.oom = false
}
// Mirrors BrotliBuildMetaBlock in metablock.c.
//
// The params (the C code's *params) are this struct's quality, large_window,
// disable_lcm and dist_etc fields: see set_params and init_distance_params.
// Like the C code, it updates the dist_etc fields (the chosen distance
// params) and rewrites the commands' dist_prefix_ and dist_extra_ fields.
//
// ringbuffer, pos and mask are the C code's ringbuffer, pos and mask. cmds
// holds num_commands commands (16 bytes each). literal_context_mode is a
// ContextType (0 to 3).
//
// The MetaBlockSplit result is described by this struct's split_etc,
// context_map_etc and histograms_etc fields, as offsets into wb. It returns
// "#bad workbuf length" (and the results are invalid) if wb is too short.
pri func metablock_builder.build_meta_block!(wb: slice base.u8, ringbuffer: roslice base.u8, pos: base.u64, mask: base.u64,
prev_byte: base.u8, prev_byte2: base.u8, cmds: slice base.u8, num_commands: base.u64,
literal_context_mode: base.u32) base.status {
var orig_npostfix : base.u32
var orig_ndirect : base.u32
var orig_asm : base.u32
var orig_asl : base.u32
var orig_md : base.u64
var best_npostfix : base.u32
var best_ndirect : base.u32
var best_asm : base.u32
var best_asl : base.u32
var best_md : base.u64
var npostfix : base.u32
var ndirect_msb : base.u32
var ndirect : base.u32
var check_orig : base.bool
var best_dist_cost : base.u64
var success : base.bool
var tmp : base.u64
var i : base.u64
var j : base.u64
var lcm : base.u64
var lit_histos : base.u64
var lit_histos_size : base.u64
var dist_histos : base.u64
var context_mode : base.u32
var v : base.u32
this.reset_arena!(wb_len: args.wb.length())
this.bc.init_log2_cache!()
orig_npostfix = this.dist_postfix_bits
orig_ndirect = this.dist_num_direct_codes
orig_asm = this.dist_alphabet_size_max
orig_asl = this.dist_alphabet_size_limit
orig_md = this.dist_max_distance
// best_etc is the C code's params->dist during the search.
best_npostfix = orig_npostfix
best_ndirect = orig_ndirect
best_asm = orig_asm
best_asl = orig_asl
best_md = orig_md
check_orig = true
best_dist_cost = F64_1E99
tmp = this.alloc!(n: HISTOGRAM_DISTANCE_SIZE)
if this.oom {
return base."#bad workbuf length"
}
npostfix = 0
while npostfix <= 3 {
while ndirect_msb < 16 {
ndirect = ndirect_msb ~mod<< (npostfix & 31)
this.init_distance_params!(npostfix: npostfix, ndirect: ndirect, large_window: this.large_window)
if (npostfix == orig_npostfix) and (ndirect == orig_ndirect) {
check_orig = false
}
success = this.compute_distance_cost!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
orig_npostfix: orig_npostfix, orig_ndirect: orig_ndirect,
new_npostfix: npostfix, new_ndirect: ndirect, new_max_distance: this.dist_max_distance, tmp: tmp)
if (not success) or (this.sf.f64_gt(a: this.dist_cost, b: best_dist_cost) <> 0) {
break
}
best_dist_cost = this.dist_cost
best_npostfix = this.dist_postfix_bits
best_ndirect = this.dist_num_direct_codes
best_asm = this.dist_alphabet_size_max
best_asl = this.dist_alphabet_size_limit
best_md = this.dist_max_distance
ndirect_msb ~mod+= 1
}
if ndirect_msb > 0 {
ndirect_msb ~mod-= 1
}
ndirect_msb /= 2
npostfix ~mod+= 1
}
if check_orig {
success = this.compute_distance_cost!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
orig_npostfix: orig_npostfix, orig_ndirect: orig_ndirect,
new_npostfix: orig_npostfix, new_ndirect: orig_ndirect, new_max_distance: orig_md, tmp: tmp)
if this.sf.f64_lt(a: this.dist_cost, b: best_dist_cost) <> 0 {
best_npostfix = orig_npostfix
best_ndirect = orig_ndirect
best_asm = orig_asm
best_asl = orig_asl
best_md = orig_md
}
}
this.dist_postfix_bits = best_npostfix
this.dist_num_direct_codes = best_ndirect
this.dist_alphabet_size_max = best_asm
this.dist_alphabet_size_limit = best_asl
this.dist_max_distance = best_md
// BROTLI_FREE(m, tmp).
this.wb_top = 0
this.recompute_distance_prefixes!(cmds: args.cmds, num_commands: args.num_commands, orig_npostfix: orig_npostfix, orig_ndirect: orig_ndirect)
this.alloc_block_splits!(cmds: args.cmds, num_commands: args.num_commands)
if this.oom {
return base."#bad workbuf length"
}
this.split_block!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
data: args.ringbuffer, pos: args.pos, mask: args.mask)
if this.oom {
return base."#bad workbuf length"
}
// Allocate the MetaBlockSplit's histograms and context maps.
this.histograms_size[1] = this.split_num_types[1]
this.histograms_off[1] = this.alloc!(n: this.histograms_size[1] ~mod* HISTOGRAM_COMMAND_SIZE)
this.context_map_off[1] = 0
this.context_map_size[1] = 0
this.context_map_size[0] = this.split_num_types[0] ~mod<< 6
this.context_map_off[0] = this.alloc!(n: this.context_map_size[0] ~mod* 4)
this.histograms_size[0] = this.context_map_size[0]
this.histograms_off[0] = this.alloc!(n: this.histograms_size[0] ~mod* HISTOGRAM_LITERAL_SIZE)
this.context_map_size[2] = this.split_num_types[2] ~mod<< 2
this.context_map_off[2] = this.alloc!(n: this.context_map_size[2] ~mod* 4)
this.histograms_size[2] = this.context_map_size[2]
this.histograms_off[2] = this.alloc!(n: this.histograms_size[2] ~mod* HISTOGRAM_DISTANCE_SIZE)
// The (scratch) literal and distance histograms.
lcm = 1
context_mode = 0xFFFF_FFFF
if not this.disable_lcm {
lcm = 64
context_mode = args.literal_context_mode & 3
}
lit_histos_size = this.split_num_types[0] ~mod* lcm
lit_histos = this.alloc!(n: lit_histos_size ~mod* HISTOGRAM_LITERAL_SIZE)
dist_histos = this.alloc!(n: this.context_map_size[2] ~mod* HISTOGRAM_DISTANCE_SIZE)
if this.oom {
return base."#bad workbuf length"
}
i = 0
while i < lit_histos_size {
this.h_clear!(wb: args.wb, h: lit_histos ~mod+ (i ~mod* HISTOGRAM_LITERAL_SIZE), ds: HISTOGRAM_LITERAL_DATA_SIZE)
i ~mod+= 1
}
i = 0
while i < this.context_map_size[2] {
this.h_clear!(wb: args.wb, h: dist_histos ~mod+ (i ~mod* HISTOGRAM_DISTANCE_SIZE), ds: HISTOGRAM_DISTANCE_DATA_SIZE)
i ~mod+= 1
}
i = 0
while i < this.histograms_size[1] {
this.h_clear!(wb: args.wb, h: this.histograms_off[1] ~mod+ (i ~mod* HISTOGRAM_COMMAND_SIZE), ds: HISTOGRAM_COMMAND_DATA_SIZE)
i ~mod+= 1
}
if (this.split_types_off[0] <= args.wb.length()) and
(this.split_lengths_off[0] <= args.wb.length()) and
(this.split_types_off[1] <= args.wb.length()) and
(this.split_lengths_off[1] <= args.wb.length()) and
(this.split_types_off[2] <= args.wb.length()) and
(this.split_lengths_off[2] <= args.wb.length()) and
(lit_histos <= args.wb.length()) and
(this.histograms_off[1] <= args.wb.length()) and
(dist_histos <= args.wb.length()) {
this.bc.build_histograms_with_context!(
cmds: args.cmds,
num_commands: args.num_commands,
lit_types: args.wb[this.split_types_off[0] ..],
lit_lengths: args.wb[this.split_lengths_off[0] ..],
lit_num_blocks: this.split_num_blocks[0],
cmd_types: args.wb[this.split_types_off[1] ..],
cmd_lengths: args.wb[this.split_lengths_off[1] ..],
cmd_num_blocks: this.split_num_blocks[1],
dist_types: args.wb[this.split_types_off[2] ..],
dist_lengths: args.wb[this.split_lengths_off[2] ..],
dist_num_blocks: this.split_num_blocks[2],
ringbuffer: args.ringbuffer,
start_pos: args.pos,
mask: args.mask,
prev_byte: args.prev_byte,
prev_byte2: args.prev_byte2,
context_mode: context_mode,
lit_histos: args.wb[lit_histos ..],
cmd_histos: args.wb[this.histograms_off[1] ..],
dist_histos: args.wb[dist_histos ..])
}
this.histograms_size[0] = this.cluster_histograms!(wb: args.wb, ds: HISTOGRAM_LITERAL_DATA_SIZE,
in_histos: lit_histos, in_size: lit_histos_size, max_histograms: MAX_NUMBER_OF_HISTOGRAMS,
out_histos: this.histograms_off[0], histogram_symbols: this.context_map_off[0])
if this.oom {
return base."#bad workbuf length"
}
if this.disable_lcm {
// Distribute assignment to all contexts.
i = this.split_num_types[0]
while i <> 0 {
i ~mod-= 1
v = this.ld32(wb: args.wb, i: this.context_map_off[0] ~mod+ (i ~mod* 4))
j = 0
while j < 64 {
this.st32!(wb: args.wb, i: this.context_map_off[0] ~mod+ (((i ~mod<< 6) ~mod+ j) ~mod* 4), v: v)
j ~mod+= 1
}
}
}
this.histograms_size[2] = this.cluster_histograms!(wb: args.wb, ds: HISTOGRAM_DISTANCE_DATA_SIZE,
in_histos: dist_histos, in_size: this.context_map_size[2], max_histograms: MAX_NUMBER_OF_HISTOGRAMS,
out_histos: this.histograms_off[2], histogram_symbols: this.context_map_off[2])
if this.oom {
return base."#bad workbuf length"
}
return ok
}
// Mirrors BrotliOptimizeHistograms in metablock.c, for this struct's
// MetaBlockSplit (as set by build_meta_block, or by the caller).
pri func metablock_builder.optimize_histograms!(wb: slice base.u8, num_distance_codes: base.u32) {
var i : base.u64
i = 0
while i < this.histograms_size[0] {
this.optimize_huffman_counts_for_rle!(wb: args.wb,
counts: this.histograms_off[0] ~mod+ (i ~mod* HISTOGRAM_LITERAL_SIZE), length: 256)
i ~mod+= 1
}
i = 0
while i < this.histograms_size[1] {
this.optimize_huffman_counts_for_rle!(wb: args.wb,
counts: this.histograms_off[1] ~mod+ (i ~mod* HISTOGRAM_COMMAND_SIZE), length: 704)
i ~mod+= 1
}
i = 0
while i < this.histograms_size[2] {
this.optimize_huffman_counts_for_rle!(wb: args.wb,
counts: this.histograms_off[2] ~mod+ (i ~mod* HISTOGRAM_DISTANCE_SIZE), length: args.num_distance_codes as base.u64)
i ~mod+= 1
}
}
// Mirrors BrotliOptimizeHuffmanCountsForRle in entropy_encode.c, for the
// u32le counts at wb[counts ..] (length is at most 704). Like the C code,
// the "256 * counts[i]" products are uint32_t arithmetic (which can wrap) and
// the rest is size_t arithmetic.
pri func metablock_builder.optimize_huffman_counts_for_rle!(wb: slice base.u8, counts: base.u64, length: base.u64) {
var length : base.u64
var nonzero_count : base.u64
var stride : base.u64
var limit : base.u64
var sum : base.u64
var i : base.u64
var k : base.u64
var nonzeros : base.u64
var smallest_nonzero : base.u32
var zeros : base.u64
var symbol : base.u32
var step : base.u64
var cnt : base.u64
var c0 : base.u32
var c1 : base.u32
var c2 : base.u32
var flag : base.bool
length = args.length.min(no_more_than: 704)
i = 0
while i < length {
if this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4)) <> 0 {
nonzero_count ~mod+= 1
}
i ~mod+= 1
}
if nonzero_count < 16 {
return nothing
}
while (length <> 0) and (this.ld32(wb: args.wb, i: args.counts ~mod+ ((length ~mod- 1) ~mod* 4)) == 0) {
length ~mod-= 1
}
if length == 0 {
return nothing
}
smallest_nonzero = 0x4000_0000
i = 0
while i < length {
c0 = this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4))
if c0 <> 0 {
nonzeros ~mod+= 1
if smallest_nonzero > c0 {
smallest_nonzero = c0
}
}
i ~mod+= 1
}
if nonzeros < 5 {
return nothing
}
if smallest_nonzero < 4 {
zeros = length ~mod- nonzeros
if zeros < 6 {
i = 1
while (i ~mod+ 1) < length {
if (this.ld32(wb: args.wb, i: args.counts ~mod+ ((i ~mod- 1) ~mod* 4)) <> 0) and
(this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4)) == 0) and
(this.ld32(wb: args.wb, i: args.counts ~mod+ ((i ~mod+ 1) ~mod* 4)) <> 0) {
this.st32!(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4), v: 1)
}
i ~mod+= 1
}
}
}
if nonzeros < 28 {
return nothing
}
// 2) Mark all population counts that already can be encoded with an RLE
// code.
this.good_for_rle[.. 704].bulk_memset!(byte_value: 0)
symbol = this.ld32(wb: args.wb, i: args.counts)
step = 0
i = 0
while i <= length {
if (i == length) or (this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4)) <> symbol) {
if ((symbol == 0) and (step >= 5)) or ((symbol <> 0) and (step >= 7)) {
k = 0
while k < step {
this.good_for_rle[((i ~mod- k) ~mod- 1) % 704] = 1
k ~mod+= 1
}
}
step = 1
if i <> length {
symbol = this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4))
}
} else {
step ~mod+= 1
}
i ~mod+= 1
}
// 3) Replace those population counts that lead to more RLE codes. The
// math here is in 24.8 fixed point representation.
stride = 0
c0 = this.ld32(wb: args.wb, i: args.counts)
c1 = this.ld32(wb: args.wb, i: args.counts ~mod+ 4)
c2 = this.ld32(wb: args.wb, i: args.counts ~mod+ 8)
limit = ((((256 as base.u32) ~mod* ((c0 ~mod+ c1) ~mod+ c2)) / 3) ~mod+ 420) as base.u64
sum = 0
i = 0
while i <= length {
flag = i == length
if not flag {
flag = this.good_for_rle[i % 704] <> 0
}
if (not flag) and (i <> 0) {
flag = this.good_for_rle[(i ~mod- 1) % 704] <> 0
}
if not flag {
c0 = this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4))
// (256 * counts[i] - limit + streak_limit) >= 2 * streak_limit,
// with streak_limit = 1240.
flag = (((((256 as base.u32) ~mod* c0) as base.u64) ~mod- limit) ~mod+ 1240) >= 2480
}
if flag {
if (stride >= 4) or ((stride >= 3) and (sum == 0)) {
// The stride must end, collapse what we have, if we have
// enough (4).
cnt = (sum ~mod+ (stride / 2)) / stride.max(no_less_than: 1)
if cnt == 0 {
cnt = 1
}
if sum == 0 {
// Don't make an all zeros stride to be upgraded to ones.
cnt = 0
}
k = 0
while k < stride {
this.st32!(wb: args.wb, i: args.counts ~mod+ (((i ~mod- k) ~mod- 1) ~mod* 4), v: (cnt & 0xFFFF_FFFF) as base.u32)
k ~mod+= 1
}
}
stride = 0
sum = 0
if i < (length ~mod- 2) {
// All interesting strides have a count of at least 4, at
// least when non-zeros.
c0 = this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4))
c1 = this.ld32(wb: args.wb, i: args.counts ~mod+ ((i ~mod+ 1) ~mod* 4))
c2 = this.ld32(wb: args.wb, i: args.counts ~mod+ ((i ~mod+ 2) ~mod* 4))
limit = ((((256 as base.u32) ~mod* ((c0 ~mod+ c1) ~mod+ c2)) / 3) ~mod+ 420) as base.u64
} else if i < length {
c0 = this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4))
limit = ((256 as base.u32) ~mod* c0) as base.u64
} else {
limit = 0
}
}
stride ~mod+= 1
if i <> length {
sum ~mod+= this.ld32(wb: args.wb, i: args.counts ~mod+ (i ~mod* 4)) as base.u64
if stride >= 4 {
limit = ((256 ~mod* sum) ~mod+ (stride / 2)) / stride.max(no_less_than: 1)
}
if stride == 4 {
limit ~mod+= 120
}
}
i ~mod+= 1
}
}
// sbv_scratch_len is an upper bound on how much workbuf (beyond the split
// arrays) split_byte_vector uses, for length symbols with the given
// histogram size, symbols_per_histogram and max_histograms.
pri func metablock_builder.sbv_scratch_len(length: base.u64, hs: base.u64, symbols_per_histogram: base.u64, max_histograms: base.u64) base.u64 {
var nh : base.u64
var a : base.u64
var b : base.u64
var nb : base.u64
if args.length < 128 {
return 0
}
nh = (args.length / args.symbols_per_histogram.max(no_less_than: 1)) ~sat+ 1
nh = nh.min(no_more_than: args.max_histograms)
// The histograms and the switch signal bitmaps.
a = ((nh ~sat+ 1) ~mod* args.hs) ~sat+ (args.length ~mod* ((nh ~sat+ 7) >> 3))
// cluster_blocks, with num_blocks (and num_clusters) at most length.
nb = args.length
b = (nb ~mod* (args.hs ~sat+ 24)) ~sat+ 1024
b ~sat+= (nb.min(no_more_than: 64)) ~mod* args.hs
b ~sat+= (2049 * HISTOGRAM_PAIR_SIZE) ~sat+ (2 ~mod* args.hs)
nb = 64 ~mod* args.length
nb = nb.min(no_more_than: (args.length / 2) ~mod* args.length)
b ~sat+= (nb ~sat+ 1) ~mod* HISTOGRAM_PAIR_SIZE
return args.length ~sat+ a.max(no_less_than: b)
}
// cluster_scratch_len is an upper bound on how much workbuf
// cluster_histograms uses, for in_size histograms of hs bytes.
pri func metablock_builder.cluster_scratch_len(in_size: base.u64, hs: base.u64) base.u64 {
var n : base.u64
var p : base.u64
n = ((12 ~mod* args.in_size) ~sat+ (2049 * HISTOGRAM_PAIR_SIZE)) ~sat+ args.hs
p = 64 ~mod* args.in_size
p = p.min(no_more_than: (args.in_size / 2) ~mod* args.in_size)
n ~sat+= (p ~sat+ 1) ~mod* HISTOGRAM_PAIR_SIZE
return n ~sat+ (args.in_size ~mod* args.hs)
}
// workbuf_len_worst_case returns an upper bound on the workbuf length that
// build_meta_block needs for a metablock with num_literals literals (the sum
// of the commands' insert lengths) and num_commands commands. The bound
// assumes the worst case for every allocation that the C code sizes at run
// time (e.g. one FindBlocks block per symbol), so it is much larger than what
// typical inputs need. The wb_high field records the actual high water mark.
pri func metablock_builder.workbuf_len_worst_case(num_literals: base.u64, num_commands: base.u64) base.u64 {
var l : base.u64
var c : base.u64
var n : base.u64
var split : base.u64
// Metablocks are at most (1 << 24) bytes long, so these clamps (which
// keep the arithmetic below from overflowing) change nothing.
l = args.num_literals.min(no_more_than: 0xFFFF_FFFF)
c = args.num_commands.min(no_more_than: 0xFFFF_FFFF)
// The split arrays (types and lengths) for the three categories.
n = (5 ~mod* l.max(no_less_than: 1)) ~sat+ (10 ~mod* c.max(no_less_than: 1))
// split_block's scratch.
split = l ~sat+ this.sbv_scratch_len(length: l, hs: HISTOGRAM_LITERAL_SIZE, symbols_per_histogram: 544, max_histograms: 100)
split = split.max(no_less_than: (2 ~mod* c) ~sat+
this.sbv_scratch_len(length: c, hs: HISTOGRAM_COMMAND_SIZE, symbols_per_histogram: 530, max_histograms: 50))
split = split.max(no_less_than: (2 ~mod* c) ~sat+
this.sbv_scratch_len(length: c, hs: HISTOGRAM_DISTANCE_SIZE, symbols_per_histogram: 544, max_histograms: 50))
n ~sat+= split.max(no_less_than: HISTOGRAM_DISTANCE_SIZE)
// The MetaBlockSplit's histograms and context maps (with at most 256
// block types per category), plus the input histograms for clustering.
n ~sat+= (256 * HISTOGRAM_COMMAND_SIZE) + (16384 * 4) + (16384 * HISTOGRAM_LITERAL_SIZE) +
(1024 * 4) + (1024 * HISTOGRAM_DISTANCE_SIZE)
n ~sat+= (16384 * HISTOGRAM_LITERAL_SIZE) + (1024 * HISTOGRAM_DISTANCE_SIZE)
// cluster_histograms' scratch.
n ~sat+= this.cluster_scratch_len(in_size: 16384, hs: HISTOGRAM_LITERAL_SIZE).max(no_less_than:
this.cluster_scratch_len(in_size: 1024, hs: HISTOGRAM_DISTANCE_SIZE))
return n
}
+167 -1
View File
@@ -18,11 +18,14 @@
// their semantics may change at any time.
pub struct metablock_tester?(
dummy : base.u32,
// worst_case is the workbuf_len_worst_case for the last
// build_meta_block call.
worst_case : base.u64,
) + (
// Wuffs' C code generator expects same-package struct fields to be in
// the private_data section (after the "+").
bc : bit_cost,
mb : metablock_builder,
)
// bits_entropy mirrors BrotliBitsEntropy. population holds size u32le values.
@@ -45,3 +48,166 @@ pub func metablock_tester.population_cost(histogram: roslice base.u8, data_size:
}
return 0
}
// init_distance_params mirrors BrotliInitDistanceParams. Its results are
// returned by get (keys 24 to 28).
pub func metablock_tester.init_distance_params!(npostfix: base.u32, ndirect: base.u32, large_window: base.u32) {
this.mb.init_distance_params!(npostfix: args.npostfix, ndirect: args.ndirect, large_window: args.large_window <> 0)
}
// split_block mirrors BrotliSplitBlock (on a fresh MetaBlockSplit), with the
// given quality (the only BrotliEncoderParams field it uses). Its results
// are returned by get (keys 0 to 11) and are in workbuf.
pub func metablock_tester.split_block!(cmds: roslice base.u8, num_commands: base.u64,
data: roslice base.u8, pos: base.u64, mask: base.u64, quality: base.u32, workbuf: slice base.u8) base.status {
this.mb.set_params!(quality: args.quality, large_window: false, disable_literal_context_modeling: false)
this.mb.reset_arena!(wb_len: args.workbuf.length())
this.mb.bc.init_log2_cache!()
this.mb.alloc_block_splits!(cmds: args.cmds, num_commands: args.num_commands)
if this.mb.oom {
return base."#bad workbuf length"
}
this.mb.split_block!(wb: args.workbuf, cmds: args.cmds, num_commands: args.num_commands,
data: args.data, pos: args.pos, mask: args.mask)
if this.mb.oom {
return base."#bad workbuf length"
}
return ok
}
// cluster_histograms mirrors BrotliClusterHistogramsLiteral (data_size 256),
// BrotliClusterHistogramsCommand (704) and BrotliClusterHistogramsDistance
// (544). The in_size input histograms are copied to the start of workbuf.
// The output histograms (with room for in_size histograms) and
// histogram_symbols (in_size u32les) are at the workbuf offsets returned by
// get (keys 18 and 12). It returns the C code's *out_size, or 0xFFFF_FFFF_FFFF_FFFF
// if workbuf is too short or data_size is invalid.
pub func metablock_tester.cluster_histograms!(data_size: base.u32, in_histos: roslice base.u8, in_size: base.u64,
max_histograms: base.u64, workbuf: slice base.u8) base.u64 {
var ds : base.u32[..= 704]
var hs : base.u64
var n : base.u64
var in_off : base.u64
var out_off : base.u64
var sym_off : base.u64
var out_size : base.u64
if args.data_size == 256 {
ds = 256
} else if args.data_size == 704 {
ds = 704
} else if args.data_size == 544 {
ds = 544
} else {
return 0xFFFF_FFFF_FFFF_FFFF
}
hs = this.mb.hsize(ds: ds)
this.mb.bc.init_log2_cache!()
this.mb.reset_arena!(wb_len: args.workbuf.length())
n = args.in_size ~mod* hs
in_off = this.mb.alloc!(n: n)
out_off = this.mb.alloc!(n: n)
sym_off = this.mb.alloc!(n: args.in_size ~mod* 4)
if this.mb.oom or (n > args.in_histos.length()) or (n > args.workbuf.length()) {
return 0xFFFF_FFFF_FFFF_FFFF
}
args.workbuf[.. n].copy_from_slice!(s: args.in_histos[.. n])
out_size = this.mb.cluster_histograms!(wb: args.workbuf, ds: ds, in_histos: in_off, in_size: args.in_size,
max_histograms: args.max_histograms, out_histos: out_off, histogram_symbols: sym_off)
if this.mb.oom {
return 0xFFFF_FFFF_FFFF_FFFF
}
this.mb.histograms_off[0] = out_off
this.mb.context_map_off[0] = sym_off
return out_size
}
// build_meta_block mirrors BrotliBuildMetaBlock. The params are the given
// quality, large_window, disable_literal_context_modeling and distance params
// (as set by BrotliInitDistanceParams(npostfix, ndirect, large_window)). Like
// the C code, it rewrites the commands' distance prefixes and extra bits. Its
// results are returned by get and are in workbuf.
pub func metablock_tester.build_meta_block!(workbuf: slice base.u8, ringbuffer: roslice base.u8, pos: base.u64, mask: base.u64,
prev_byte: base.u8, prev_byte2: base.u8, cmds: slice base.u8, num_commands: base.u64,
literal_context_mode: base.u32, quality: base.u32, large_window: base.u32,
disable_literal_context_modeling: base.u32, npostfix: base.u32, ndirect: base.u32) base.status {
var status : base.status
var i : base.u64
var literals : base.u64
i = 0
while i < args.num_commands {
literals ~mod+= this.mb.ld32(wb: args.cmds, i: i ~mod* 16) as base.u64
i ~mod+= 1
}
this.worst_case = this.mb.workbuf_len_worst_case(num_literals: literals, num_commands: args.num_commands)
this.mb.set_params!(quality: args.quality, large_window: args.large_window <> 0,
disable_literal_context_modeling: args.disable_literal_context_modeling <> 0)
this.mb.init_distance_params!(npostfix: args.npostfix, ndirect: args.ndirect, large_window: args.large_window <> 0)
status = this.mb.build_meta_block!(wb: args.workbuf, ringbuffer: args.ringbuffer, pos: args.pos, mask: args.mask,
prev_byte: args.prev_byte, prev_byte2: args.prev_byte2, cmds: args.cmds, num_commands: args.num_commands,
literal_context_mode: args.literal_context_mode)
return status
}
// optimize_histograms mirrors BrotliOptimizeHistograms, on the
// MetaBlockSplit from the last build_meta_block call (in workbuf).
pub func metablock_tester.optimize_histograms!(workbuf: slice base.u8, num_distance_codes: base.u32) {
this.mb.optimize_histograms!(wb: args.workbuf, num_distance_codes: args.num_distance_codes)
}
// get returns one of the metablock_builder's fields:
// - 4 * c + 0: split_num_types[c], for c (the category) in 0, 1, 2
// (literal, command, distance).
// - 4 * c + 1: split_num_blocks[c].
// - 4 * c + 2: split_types_off[c].
// - 4 * c + 3: split_lengths_off[c].
// - 12 + c: context_map_off[c].
// - 15 + c: context_map_size[c].
// - 18 + c: histograms_off[c].
// - 21 + c: histograms_size[c].
// - 24 to 28: the distance params' distance_postfix_bits,
// num_direct_distance_codes, alphabet_size_max, alphabet_size_limit and
// max_distance.
// - 29: the highest workbuf offset used (the workbuf length needed).
// - 30: the workbuf_len_worst_case for the last build_meta_block call.
pub func metablock_tester.get(key: base.u32) base.u64 {
var c : base.u32[..= 2]
if args.key < 12 {
c = ((args.key / 4) % 3) as base.u32
if (args.key % 4) == 0 {
return this.mb.split_num_types[c]
} else if (args.key % 4) == 1 {
return this.mb.split_num_blocks[c]
} else if (args.key % 4) == 2 {
return this.mb.split_types_off[c]
}
return this.mb.split_lengths_off[c]
} else if args.key < 24 {
c = ((args.key % 3) as base.u32)
if args.key < 15 {
return this.mb.context_map_off[c]
} else if args.key < 18 {
return this.mb.context_map_size[c]
} else if args.key < 21 {
return this.mb.histograms_off[c]
}
return this.mb.histograms_size[c]
} else if args.key == 24 {
return this.mb.dist_postfix_bits as base.u64
} else if args.key == 25 {
return this.mb.dist_num_direct_codes as base.u64
} else if args.key == 26 {
return this.mb.dist_alphabet_size_max as base.u64
} else if args.key == 27 {
return this.mb.dist_alphabet_size_limit as base.u64
} else if args.key == 28 {
return this.mb.dist_max_distance
} else if args.key == 29 {
return this.mb.wb_high
} else if args.key == 30 {
return this.worst_case
}
return 0
}