mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
std/brotli: fill the FastLog2 caches lazily
Each bit_cost (one in the encoder, one in its metablock_builder) has a 64Ki-entry FastLog2 cache. The metablock_builder filled all of it before building each quality 10 or 11 metablock (about 20 ms per encoder, since each entry is an exact software log2), and the encoder's own cache was never filled, so the quality 10 and 11 literal cost estimates computed one software log2 per input byte. Instead, a bitmap (zeroed when the struct is initialized) records which cache entries are valid, and the new fast_log2_memo fills an entry the first time it is needed. Typically only a few thousand entries are used. bits_entropy, population_cost and their callers become impure (!) to call it. The values are computed by the same functions, so the results are unchanged. On alice29 at quality 10, the encoder executes about 20% fewer instructions, and a 1000-byte input takes 6 ms instead of 21 ms.
This commit is contained in:
9 files changed
+208
-157
No files matched your search
@@ -896,7 +896,12 @@ uses it at qualities 10 and 11 (and its smaller helpers at lower
|
||||
qualities). Its
|
||||
fixed-size state (about 0.8 MiB: FindBlocks' cost arrays and a 64Ki-entry
|
||||
FastLog2 cache in its `bit_cost`) lives in the struct; everything that the C
|
||||
code allocates on the heap lives in a caller-supplied workbuf.
|
||||
code allocates on the heap lives in a caller-supplied workbuf. The FastLog2
|
||||
cache (like the encoder's own `bit_cost` one) is filled lazily, one entry at
|
||||
a time, when `fast_log2_memo` first needs that entry (a bitmap, zeroed when the
|
||||
struct is initialized, records which entries are valid): typically only a few
|
||||
thousand of the 65536 entries are ever used, and each one costs a (slow, exact)
|
||||
software `log2`.
|
||||
|
||||
Data formats (all little-endian, matching the C structs' memory layout on a
|
||||
64-bit little-endian machine, so that results can be compared with `memcmp`):
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
|
||||
// This file mirrors bit_cost.c, bit_cost_inc.h and fast_log.h. The functions
|
||||
// are methods of the bit_cost struct (whose only state is the stateless
|
||||
// softfloat arith), so that other structs (the encoder, the
|
||||
// metablock_builder) can embed one with a single field.
|
||||
// softfloat arith and a lazily filled FastLog2 cache), so that other structs
|
||||
// (the encoder, the metablock_builder) can embed one with a single field.
|
||||
//
|
||||
// Doubles are IEEE 754 binary64 bit patterns (base.u64). A uint32_t array
|
||||
// (e.g. a histogram's data_) is a slice of little-endian bytes, because
|
||||
@@ -48,43 +48,23 @@ 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,
|
||||
// Bit (v & 63) of log2_valid[v >> 6] is whether log2_cache[v] holds
|
||||
// FastLog2(v). This bitmap is zeroed when the struct is initialized
|
||||
// (but log2_cache need not be), so the cache starts empty.
|
||||
log2_valid : array[1024] base.u64,
|
||||
|
||||
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[v] is FastLog2(v), for 256 <= v < 65536, filled lazily
|
||||
// (see fast_log2_memo). 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. Few of
|
||||
// the 65536 entries are typically needed, so filling it lazily is
|
||||
// much cheaper than filling it all up front.
|
||||
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 {
|
||||
@@ -138,23 +118,41 @@ pri func bit_cost.st64!(s: slice base.u8, i: base.u64, v: base.u64) {
|
||||
}
|
||||
|
||||
// Mirrors FastLog2 in fast_log.h. The result is the bit pattern of an IEEE
|
||||
// 754 binary64 value.
|
||||
// 754 binary64 value. It uses (but does not fill) log2_cache: see
|
||||
// fast_log2_memo.
|
||||
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]
|
||||
} else if args.v < 65536 {
|
||||
if ((this.log2_valid[args.v >> 6] >> (args.v & 63)) & 1) <> 0 {
|
||||
return this.log2_cache[args.v]
|
||||
}
|
||||
}
|
||||
return this.sf.log2_u64(v: args.v)
|
||||
}
|
||||
|
||||
// fast_log2_memo is like fast_log2 (it also mirrors FastLog2) but it fills
|
||||
// the log2_cache entry for v (if v is in the cache's range).
|
||||
pri func bit_cost.fast_log2_memo!(v: base.u64) base.u64 {
|
||||
if args.v < 256 {
|
||||
return LOG2_TABLE[args.v]
|
||||
} else if args.v >= 65536 {
|
||||
return this.sf.log2_u64(v: args.v)
|
||||
} else if ((this.log2_valid[args.v >> 6] >> (args.v & 63)) & 1) == 0 {
|
||||
this.log2_cache[args.v] = this.sf.log2_u64(v: args.v)
|
||||
this.log2_valid[args.v >> 6] |= (1 as base.u64) << (args.v & 63)
|
||||
}
|
||||
return this.log2_cache[args.v]
|
||||
}
|
||||
|
||||
// Mirrors BrotliBitsEntropy in bit_cost.c. population holds (at least) size
|
||||
// u32le values. The C code's "goto odd_number_of_elements_left" only changes
|
||||
// the loop's unrolling, not the order of the operations.
|
||||
pri func bit_cost.bits_entropy(population: roslice base.u8, size: base.u64) base.u64 {
|
||||
pri func bit_cost.bits_entropy!(population: roslice base.u8, size: base.u64) base.u64 {
|
||||
var sum : base.u64
|
||||
var retval : base.u64
|
||||
var p : base.u64
|
||||
var lp : base.u64
|
||||
var q : roslice base.u8
|
||||
var n : base.u64
|
||||
|
||||
@@ -168,12 +166,14 @@ pri func bit_cost.bits_entropy(population: roslice base.u8, size: base.u64) base
|
||||
q = q[4 ..]
|
||||
}
|
||||
sum ~mod+= p
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: this.fast_log2(v: p)))
|
||||
a: this.sf.f64_from_u64(a: p), b: lp))
|
||||
}
|
||||
if sum <> 0 {
|
||||
lp = this.fast_log2_memo!(v: sum)
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: this.fast_log2(v: sum)))
|
||||
a: this.sf.f64_from_u64(a: sum), b: lp))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
@@ -184,7 +184,7 @@ pri func bit_cost.bits_entropy(population: roslice base.u8, size: base.u64) base
|
||||
// Mirrors BrotliPopulationCostLiteral, BrotliPopulationCostCommand and
|
||||
// BrotliPopulationCostDistance (FN(BrotliPopulationCost) in bit_cost_inc.h).
|
||||
// histogram is a histogram of data_size symbols (see the layout above).
|
||||
pri func bit_cost.population_cost(histogram: roslice base.u8, data_size: base.u32[..= 704]) base.u64 {
|
||||
pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u32[..= 704]) base.u64 {
|
||||
var total : base.u64
|
||||
var count : base.u32
|
||||
var s : array[4] base.u32
|
||||
@@ -203,6 +203,7 @@ pri func bit_cost.population_cost(histogram: roslice base.u8, data_size: base.u3
|
||||
var depth_histo : array[18] base.u32
|
||||
var log2total : base.u64
|
||||
var log2p : base.u64
|
||||
var lp : base.u64
|
||||
var depth : base.u64
|
||||
var reps : base.u32
|
||||
var sum : base.u64
|
||||
@@ -284,12 +285,13 @@ pri func bit_cost.population_cost(histogram: roslice base.u8, data_size: base.u3
|
||||
// Compute the entropy of the histogram and simultaneously build a
|
||||
// simplified histogram of the code length codes.
|
||||
max_depth = 1
|
||||
log2total = this.fast_log2(v: total)
|
||||
log2total = this.fast_log2_memo!(v: total)
|
||||
i = 0
|
||||
while i < args.data_size {
|
||||
c = this.ld32(s: args.histogram, i: (i as base.u64) * 4)
|
||||
if c > 0 {
|
||||
log2p = this.sf.f64_sub(a: log2total, b: this.fast_log2(v: c as base.u64))
|
||||
lp = this.fast_log2_memo!(v: c as base.u64)
|
||||
log2p = this.sf.f64_sub(a: log2total, b: lp)
|
||||
depth = this.sf.f64_to_u64_trunc(a: this.sf.f64_add(a: log2p, b: 0x3FE0_0000_0000_0000))
|
||||
bits = this.sf.f64_add(a: bits, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u32(a: c), b: log2p))
|
||||
@@ -340,13 +342,15 @@ pri func bit_cost.population_cost(histogram: roslice base.u8, data_size: base.u3
|
||||
while i < 18 {
|
||||
p = depth_histo[i] as base.u64
|
||||
sum ~mod+= p
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: this.fast_log2(v: p)))
|
||||
a: this.sf.f64_from_u64(a: p), b: lp))
|
||||
i ~mod+= 1
|
||||
}
|
||||
if sum <> 0 {
|
||||
lp = this.fast_log2_memo!(v: sum)
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: this.fast_log2(v: sum)))
|
||||
a: this.sf.f64_from_u64(a: sum), b: lp))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
|
||||
@@ -31,12 +31,15 @@ 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 {
|
||||
pri func metablock_builder.bit_cost!(count: base.u64) base.u64 {
|
||||
var c : base.u64
|
||||
|
||||
if args.count == 0 {
|
||||
// -2.0
|
||||
return 0xC000_0000_0000_0000
|
||||
}
|
||||
return this.bc.fast_log2(v: args.count)
|
||||
c = this.bc.fast_log2_memo!(v: args.count)
|
||||
return c
|
||||
}
|
||||
|
||||
// Mirrors FN(HistogramAddVector) in histogram_inc.h, adding the n symbols at
|
||||
@@ -314,7 +317,7 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
i = 0
|
||||
while i < args.num_histograms {
|
||||
// FastLog2((uint32_t)histograms[i].total_count_).
|
||||
this.insert_cost[i & 255] = this.bc.fast_log2(
|
||||
this.insert_cost[i & 255] = this.bc.fast_log2_memo!(
|
||||
v: this.h_total(wb: args.wb, h: args.histograms ~mod+ (i ~mod* hs), ds: args.ds) & 0xFFFF_FFFF)
|
||||
i ~mod+= 1
|
||||
}
|
||||
@@ -326,9 +329,8 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
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))
|
||||
c = this.bit_cost!(count: this.ld32(wb: args.wb, i: (args.histograms ~mod+ (j ~mod* hs)) ~mod+ (i ~mod* 4)) as base.u64)
|
||||
this.insert_cost[k] = this.sf.f64_sub(a: this.insert_cost[j & 255], b: c)
|
||||
}
|
||||
j ~mod+= 1
|
||||
}
|
||||
@@ -471,6 +473,7 @@ pri func metablock_builder.build_block_histograms!(wb: slice base.u8, data: base
|
||||
// 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 c : base.u64
|
||||
var hs : base.u64
|
||||
var histogram_symbols : base.u64
|
||||
var u32s : base.u64
|
||||
@@ -562,8 +565,8 @@ pri func metablock_builder.cluster_blocks!(wb: slice base.u8, cat: base.u32[..=
|
||||
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))
|
||||
c = this.h_population_cost!(wb: args.wb, h: histograms ~mod+ (j ~mod* hs), ds: args.ds)
|
||||
this.h_set_bit_cost!(wb: args.wb, h: histograms ~mod+ (j ~mod* hs), ds: args.ds, bit_cost: c)
|
||||
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)
|
||||
|
||||
@@ -108,7 +108,7 @@ pri func metablock_builder.compare_and_push_to_queue!(wb: slice base.u8, ds: bas
|
||||
}
|
||||
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)
|
||||
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
|
||||
@@ -271,14 +271,15 @@ pri func metablock_builder.histogram_combine!(wb: slice base.u8, ds: base.u32[..
|
||||
// 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 {
|
||||
var c : 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))
|
||||
c = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: args.ds)
|
||||
return this.sf.f64_sub(a: c, b: this.h_bit_cost(wb: args.wb, h: args.candidate, ds: args.ds))
|
||||
}
|
||||
|
||||
// Mirrors FN(BrotliHistogramRemap) in cluster_inc.h.
|
||||
@@ -395,6 +396,7 @@ pri func metablock_builder.histogram_reindex!(wb: slice base.u8, ds: base.u32[..
|
||||
// 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 c : base.u64
|
||||
var hs : base.u64
|
||||
var mark : base.u64
|
||||
var cluster_size : base.u64
|
||||
@@ -426,8 +428,8 @@ pri func metablock_builder.cluster_histograms!(wb: slice base.u8, ds: base.u32[.
|
||||
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))
|
||||
c = this.h_population_cost!(wb: args.wb, h: 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: c)
|
||||
this.st32!(wb: args.wb, i: args.histogram_symbols ~mod+ (i ~mod* 4), v: (i & 0xFFFF_FFFF) as base.u32)
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
@@ -277,7 +277,8 @@ pri func encoder.estimate_bit_costs_for_literals_utf8!(wb: slice base.u8, pos: b
|
||||
} else if utf8_pos == 2 {
|
||||
iw = iwu2
|
||||
}
|
||||
lit_cost = this.sf.f64_sub(a: this.bc.fast_log2(v: iw), b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.bc.fast_log2_memo!(v: iw)
|
||||
lit_cost = this.sf.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_02905)
|
||||
this.lc_finish_cost!(wb: args.wb, cost_off: args.cost_off, i: i, lit_cost: lit_cost, utf8: true)
|
||||
i ~mod+= 1
|
||||
@@ -337,7 +338,8 @@ pri func encoder.estimate_bit_costs_for_literals!(wb: slice base.u8, pos: base.u
|
||||
if histo == 0 {
|
||||
histo = 1
|
||||
}
|
||||
lit_cost = this.sf.f64_sub(a: this.bc.fast_log2(v: in_window), b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.bc.fast_log2_memo!(v: in_window)
|
||||
lit_cost = this.sf.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_029)
|
||||
this.lc_finish_cost!(wb: args.wb, cost_off: args.cost_off, i: i, lit_cost: lit_cost, utf8: false)
|
||||
i ~mod+= 1
|
||||
|
||||
@@ -264,9 +264,12 @@ pri func metablock_builder.h_set_bit_cost!(wb: slice base.u8, h: base.u64, ds: b
|
||||
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 {
|
||||
pri func metablock_builder.h_population_cost!(wb: roslice base.u8, h: base.u64, ds: base.u32[..= 704]) base.u64 {
|
||||
var c : base.u64
|
||||
|
||||
if args.h <= args.wb.length() {
|
||||
return this.bc.population_cost(histogram: args.wb[args.h ..], data_size: args.ds)
|
||||
c = this.bc.population_cost!(histogram: args.wb[args.h ..], data_size: args.ds)
|
||||
return c
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -482,9 +485,8 @@ pri func metablock_builder.compute_distance_cost!(wb: slice base.u8, cmds: rosli
|
||||
}
|
||||
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)
|
||||
this.dist_cost = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE)
|
||||
this.dist_cost = this.sf.f64_add(a: this.dist_cost, b: extra_bits)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -592,7 +594,6 @@ pri func metablock_builder.build_meta_block!(wb: slice base.u8, ringbuffer: rosl
|
||||
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
|
||||
|
||||
@@ -47,9 +47,12 @@ pri func encoder.gbs_histo_off(c: base.u32[..= 3], ix: base.u64) base.u64 {
|
||||
|
||||
// gbs_entropy returns BrotliBitsEntropy of the size u32le values at
|
||||
// args.wb[off ..].
|
||||
pri func encoder.gbs_entropy(wb: roslice base.u8, off: base.u64, size: base.u64) base.u64 {
|
||||
pri func encoder.gbs_entropy!(wb: roslice base.u8, off: base.u64, size: base.u64) base.u64 {
|
||||
var e : base.u64
|
||||
|
||||
if args.off <= args.wb.length() {
|
||||
return this.bc.bits_entropy(population: args.wb[args.off ..], size: args.size)
|
||||
e = this.bc.bits_entropy!(population: args.wb[args.off ..], size: args.size)
|
||||
return e
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -162,7 +165,7 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
this.set_byte!(s: args.wb, i: types_off, v: 0)
|
||||
i = 0
|
||||
while i < nc {
|
||||
e = this.gbs_entropy(wb: args.wb, off: this.gbs_histo_off(c: args.c, ix: i), size: alphabet_size)
|
||||
e = this.gbs_entropy!(wb: args.wb, off: this.gbs_histo_off(c: args.c, ix: i), size: alphabet_size)
|
||||
this.gbs_last_entropy[((le_base as base.u64) ~mod+ i) & 127] = e
|
||||
this.gbs_last_entropy[(((le_base as base.u64) ~mod+ nc) ~mod+ i) & 127] = e
|
||||
i ~mod+= 1
|
||||
@@ -185,7 +188,7 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
last1 = this.gbs_last_histogram_ix[(args.c * 2) + 1]
|
||||
i = 0
|
||||
while i < nc {
|
||||
e = this.gbs_entropy(wb: args.wb, off: this.gbs_histo_off(c: args.c, ix: curr ~mod+ i), size: alphabet_size)
|
||||
e = this.gbs_entropy!(wb: args.wb, off: this.gbs_histo_off(c: args.c, ix: curr ~mod+ i), size: alphabet_size)
|
||||
this.gbs_entropy_tmp[i & 15] = e
|
||||
j = 0
|
||||
while j < 2 {
|
||||
@@ -197,7 +200,7 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
} else {
|
||||
this.gbs_add_histogram!(wb: args.wb, dst: tmp, src: this.gbs_histo_off(c: args.c, ix: last1 ~mod+ i), n: data_size)
|
||||
}
|
||||
this.gbs_comb_entropy[jx & 31] = this.gbs_entropy(wb: args.wb, off: tmp, size: alphabet_size)
|
||||
this.gbs_comb_entropy[jx & 31] = this.gbs_entropy!(wb: args.wb, off: tmp, size: alphabet_size)
|
||||
d = this.sf.f64_sub(
|
||||
a: this.sf.f64_sub(a: this.gbs_comb_entropy[jx & 31], b: e),
|
||||
b: this.gbs_last_entropy[((le_base as base.u64) ~mod+ jx) & 127])
|
||||
|
||||
@@ -29,8 +29,11 @@ pub struct metablock_tester?(
|
||||
)
|
||||
|
||||
// bits_entropy mirrors BrotliBitsEntropy. population holds size u32le values.
|
||||
pub func metablock_tester.bits_entropy(population: roslice base.u8, size: base.u64) base.u64 {
|
||||
return this.bc.bits_entropy(population: args.population, size: args.size)
|
||||
pub func metablock_tester.bits_entropy!(population: roslice base.u8, size: base.u64) base.u64 {
|
||||
var c : base.u64
|
||||
|
||||
c = this.bc.bits_entropy!(population: args.population, size: args.size)
|
||||
return c
|
||||
}
|
||||
|
||||
// population_cost mirrors BrotliPopulationCostLiteral (data_size 256),
|
||||
@@ -38,13 +41,18 @@ pub func metablock_tester.bits_entropy(population: roslice base.u8, size: base.u
|
||||
// histogram has the C struct's layout: data_size u32le counts, then the
|
||||
// total_count_ u64le (and then the bit_cost_, which is ignored). It returns 0
|
||||
// for any other data_size.
|
||||
pub func metablock_tester.population_cost(histogram: roslice base.u8, data_size: base.u32) base.u64 {
|
||||
pub func metablock_tester.population_cost!(histogram: roslice base.u8, data_size: base.u32) base.u64 {
|
||||
var c : base.u64
|
||||
|
||||
if args.data_size == 256 {
|
||||
return this.bc.population_cost(histogram: args.histogram, data_size: 256)
|
||||
c = this.bc.population_cost!(histogram: args.histogram, data_size: 256)
|
||||
return c
|
||||
} else if args.data_size == 704 {
|
||||
return this.bc.population_cost(histogram: args.histogram, data_size: 704)
|
||||
c = this.bc.population_cost!(histogram: args.histogram, data_size: 704)
|
||||
return c
|
||||
} else if args.data_size == 544 {
|
||||
return this.bc.population_cost(histogram: args.histogram, data_size: 544)
|
||||
c = this.bc.population_cost!(histogram: args.histogram, data_size: 544)
|
||||
return c
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -62,7 +70,6 @@ pub func metablock_tester.split_block!(cmds: roslice base.u8, num_commands: base
|
||||
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"
|
||||
@@ -102,7 +109,6 @@ pub func metablock_tester.cluster_histograms!(data_size: base.u32, in_histos: ro
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user