mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +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
@@ -9475,14 +9475,14 @@ wuffs_brotli__encoder__add_dictionary_workbuf_len(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__bits_entropy(
|
||||
const wuffs_brotli__metablock_tester* self,
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_population,
|
||||
uint64_t a_size);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__population_cost(
|
||||
const wuffs_brotli__metablock_tester* self,
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_histogram,
|
||||
uint32_t a_data_size);
|
||||
|
||||
@@ -9894,7 +9894,7 @@ struct wuffs_brotli__bit_cost__struct {
|
||||
uint32_t active_coroutine;
|
||||
wuffs_base__vtable null_vtable;
|
||||
|
||||
bool f_log2_cache_ready;
|
||||
uint64_t f_log2_valid[1024];
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
@@ -10400,14 +10400,14 @@ struct wuffs_brotli__metablock_tester__struct {
|
||||
inline uint64_t
|
||||
bits_entropy(
|
||||
wuffs_base__slice_u8 a_population,
|
||||
uint64_t a_size) const {
|
||||
uint64_t a_size) {
|
||||
return wuffs_brotli__metablock_tester__bits_entropy(this, a_population, a_size);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
population_cost(
|
||||
wuffs_base__slice_u8 a_histogram,
|
||||
uint32_t a_data_size) const {
|
||||
uint32_t a_data_size) {
|
||||
return wuffs_brotli__metablock_tester__population_cost(this, a_histogram, a_data_size);
|
||||
}
|
||||
|
||||
@@ -77048,8 +77048,6 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__F64_1E99 6088095589093318446u
|
||||
|
||||
#define WUFFS_BROTLI__LOG2_CACHE_SIZE 65536u
|
||||
|
||||
#define WUFFS_BROTLI__LITERAL_BLOCK_SWITCH_COST 4628602664527698330u
|
||||
|
||||
#define WUFFS_BROTLI__COMMAND_BLOCK_SWITCH_COST 4623789442425946112u
|
||||
@@ -77752,11 +77750,6 @@ wuffs_brotli__encoder__create_backward_references(
|
||||
uint64_t a_num_bytes,
|
||||
uint64_t a_position);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__bit_cost__init_log2_cache(
|
||||
wuffs_brotli__bit_cost* self);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__ld32(
|
||||
@@ -77793,17 +77786,23 @@ wuffs_brotli__bit_cost__fast_log2(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_v);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__fast_log2_memo(
|
||||
wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_v);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__bits_entropy(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
wuffs_brotli__bit_cost* self,
|
||||
wuffs_base__slice_u8 a_population,
|
||||
uint64_t a_size);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__population_cost(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
wuffs_brotli__bit_cost* self,
|
||||
wuffs_base__slice_u8 a_histogram,
|
||||
uint32_t a_data_size);
|
||||
|
||||
@@ -78116,7 +78115,7 @@ wuffs_brotli__encoder__find_match_length_with_limit(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__bit_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_count);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -79785,7 +79784,7 @@ wuffs_brotli__metablock_builder__h_set_bit_cost(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_population_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds);
|
||||
@@ -79952,7 +79951,7 @@ wuffs_brotli__encoder__gbs_histo_off(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__gbs_entropy(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_size);
|
||||
@@ -85284,30 +85283,6 @@ wuffs_brotli__encoder__create_backward_references(
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.init_log2_cache
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__bit_cost__init_log2_cache(
|
||||
wuffs_brotli__bit_cost* self) {
|
||||
uint64_t v_v = 0;
|
||||
|
||||
if (self->private_impl.f_log2_cache_ready) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_v = 0u;
|
||||
while (v_v < 256u) {
|
||||
self->private_data.f_log2_cache[v_v] = WUFFS_BROTLI__LOG2_TABLE[v_v];
|
||||
v_v += 1u;
|
||||
}
|
||||
while (v_v < 65536u) {
|
||||
self->private_data.f_log2_cache[v_v] = wuffs_softfloat__arith__log2_u64(&self->private_data.f_sf, v_v);
|
||||
v_v += 1u;
|
||||
}
|
||||
self->private_impl.f_log2_cache_ready = true;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.ld32
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -85395,23 +85370,44 @@ wuffs_brotli__bit_cost__fast_log2(
|
||||
uint64_t a_v) {
|
||||
if (a_v < 256u) {
|
||||
return WUFFS_BROTLI__LOG2_TABLE[a_v];
|
||||
} else if (self->private_impl.f_log2_cache_ready && (a_v < 65536u)) {
|
||||
return self->private_data.f_log2_cache[a_v];
|
||||
} else if (a_v < 65536u) {
|
||||
if (((self->private_impl.f_log2_valid[(a_v >> 6u)] >> (a_v & 63u)) & 1u) != 0u) {
|
||||
return self->private_data.f_log2_cache[a_v];
|
||||
}
|
||||
}
|
||||
return wuffs_softfloat__arith__log2_u64(&self->private_data.f_sf, a_v);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.fast_log2_memo
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__fast_log2_memo(
|
||||
wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_v) {
|
||||
if (a_v < 256u) {
|
||||
return WUFFS_BROTLI__LOG2_TABLE[a_v];
|
||||
} else if (a_v >= 65536u) {
|
||||
return wuffs_softfloat__arith__log2_u64(&self->private_data.f_sf, a_v);
|
||||
} else if (((self->private_impl.f_log2_valid[(a_v >> 6u)] >> (a_v & 63u)) & 1u) == 0u) {
|
||||
self->private_data.f_log2_cache[a_v] = wuffs_softfloat__arith__log2_u64(&self->private_data.f_sf, a_v);
|
||||
self->private_impl.f_log2_valid[(a_v >> 6u)] |= (((uint64_t)(1u)) << (a_v & 63u));
|
||||
}
|
||||
return self->private_data.f_log2_cache[a_v];
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.bits_entropy
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__bits_entropy(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
wuffs_brotli__bit_cost* self,
|
||||
wuffs_base__slice_u8 a_population,
|
||||
uint64_t a_size) {
|
||||
uint64_t v_sum = 0;
|
||||
uint64_t v_retval = 0;
|
||||
uint64_t v_p = 0;
|
||||
uint64_t v_lp = 0;
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
uint64_t v_n = 0;
|
||||
|
||||
@@ -85425,10 +85421,12 @@ wuffs_brotli__bit_cost__bits_entropy(
|
||||
v_q = wuffs_base__slice_u8__subslice_i(v_q, 4u);
|
||||
}
|
||||
v_sum += v_p;
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), wuffs_brotli__bit_cost__fast_log2(self, v_p)));
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_p);
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), v_lp));
|
||||
}
|
||||
if (v_sum != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), wuffs_brotli__bit_cost__fast_log2(self, v_sum)));
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_sum);
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), v_lp));
|
||||
}
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum)) != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum);
|
||||
@@ -85441,7 +85439,7 @@ wuffs_brotli__bit_cost__bits_entropy(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__population_cost(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
wuffs_brotli__bit_cost* self,
|
||||
wuffs_base__slice_u8 a_histogram,
|
||||
uint32_t a_data_size) {
|
||||
uint64_t v_total = 0;
|
||||
@@ -85462,6 +85460,7 @@ wuffs_brotli__bit_cost__population_cost(
|
||||
uint32_t v_depth_histo[18] = {0};
|
||||
uint64_t v_log2total = 0;
|
||||
uint64_t v_log2p = 0;
|
||||
uint64_t v_lp = 0;
|
||||
uint64_t v_depth = 0;
|
||||
uint32_t v_reps = 0;
|
||||
uint64_t v_sum = 0;
|
||||
@@ -85522,12 +85521,13 @@ wuffs_brotli__bit_cost__population_cost(
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, 4630404104378646528u, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, ((uint32_t)(3u * v_h23)))), wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, ((uint32_t)(2u * ((uint32_t)(v_histo[0u] + v_histo[1u])))))), wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_histomax));
|
||||
}
|
||||
v_max_depth = 1u;
|
||||
v_log2total = wuffs_brotli__bit_cost__fast_log2(self, v_total);
|
||||
v_log2total = wuffs_brotli__bit_cost__fast_log2_memo(self, v_total);
|
||||
v_i = 0u;
|
||||
while (v_i < a_data_size) {
|
||||
v_c = wuffs_brotli__bit_cost__ld32(self, a_histogram, (((uint64_t)(v_i)) * 4u));
|
||||
if (v_c > 0u) {
|
||||
v_log2p = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_log2total, wuffs_brotli__bit_cost__fast_log2(self, ((uint64_t)(v_c))));
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, ((uint64_t)(v_c)));
|
||||
v_log2p = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_log2total, v_lp);
|
||||
v_depth = wuffs_softfloat__arith__f64_to_u64_trunc(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_log2p, 4602678819172646912u));
|
||||
v_bits = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_bits, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_c), v_log2p));
|
||||
if (v_depth > 15u) {
|
||||
@@ -85569,11 +85569,13 @@ wuffs_brotli__bit_cost__population_cost(
|
||||
while (v_i < 18u) {
|
||||
v_p = ((uint64_t)(v_depth_histo[v_i]));
|
||||
v_sum += v_p;
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), wuffs_brotli__bit_cost__fast_log2(self, v_p)));
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_p);
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), v_lp));
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_sum != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), wuffs_brotli__bit_cost__fast_log2(self, v_sum)));
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_sum);
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), v_lp));
|
||||
}
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum)) != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum);
|
||||
@@ -87178,12 +87180,15 @@ wuffs_brotli__encoder__find_match_length_with_limit(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__bit_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_count) {
|
||||
uint64_t v_c = 0;
|
||||
|
||||
if (a_count == 0u) {
|
||||
return 13835058055282163712u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_count);
|
||||
v_c = wuffs_brotli__bit_cost__fast_log2_memo(&self->private_data.f_bc, a_count);
|
||||
return v_c;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_add_vector
|
||||
@@ -87570,7 +87575,7 @@ wuffs_brotli__metablock_builder__find_blocks(
|
||||
wuffs_private_impl__bulk_memset(&self->private_data.f_insert_cost[0], 35200u * (size_t)8u, 0u);
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_histograms) {
|
||||
self->private_data.f_insert_cost[(v_i & 255u)] = wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, (wuffs_brotli__metablock_builder__h_total(self, a_wb, ((uint64_t)(a_histograms + ((uint64_t)(v_i * v_hs)))), a_ds) & 4294967295u));
|
||||
self->private_data.f_insert_cost[(v_i & 255u)] = wuffs_brotli__bit_cost__fast_log2_memo(&self->private_data.f_bc, (wuffs_brotli__metablock_builder__h_total(self, a_wb, ((uint64_t)(a_histograms + ((uint64_t)(v_i * v_hs)))), a_ds) & 4294967295u));
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = v_alphabet_size;
|
||||
@@ -87580,7 +87585,8 @@ wuffs_brotli__metablock_builder__find_blocks(
|
||||
while (v_j < a_num_histograms) {
|
||||
v_k = ((uint64_t)(((uint64_t)(v_i * a_num_histograms)) + v_j));
|
||||
if (v_k < 35200u) {
|
||||
self->private_data.f_insert_cost[v_k] = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, self->private_data.f_insert_cost[(v_j & 255u)], wuffs_brotli__metablock_builder__bit_cost(self, ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(((uint64_t)(a_histograms + ((uint64_t)(v_j * v_hs)))) + ((uint64_t)(v_i * 4u)))))))));
|
||||
v_c = wuffs_brotli__metablock_builder__bit_cost(self, ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(((uint64_t)(a_histograms + ((uint64_t)(v_j * v_hs)))) + ((uint64_t)(v_i * 4u))))))));
|
||||
self->private_data.f_insert_cost[v_k] = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, self->private_data.f_insert_cost[(v_j & 255u)], v_c);
|
||||
}
|
||||
v_j += 1u;
|
||||
}
|
||||
@@ -87750,6 +87756,7 @@ wuffs_brotli__metablock_builder__cluster_blocks(
|
||||
uint64_t a_num_blocks,
|
||||
uint64_t a_block_ids,
|
||||
uint32_t a_ds) {
|
||||
uint64_t v_c = 0;
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_histogram_symbols = 0;
|
||||
uint64_t v_u32s = 0;
|
||||
@@ -87839,11 +87846,12 @@ wuffs_brotli__metablock_builder__cluster_blocks(
|
||||
v_pos += 1u;
|
||||
v_k += 1u;
|
||||
}
|
||||
v_c = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, ((uint64_t)(v_histograms + ((uint64_t)(v_j * v_hs)))), a_ds);
|
||||
wuffs_brotli__metablock_builder__h_set_bit_cost(self,
|
||||
a_wb,
|
||||
((uint64_t)(v_histograms + ((uint64_t)(v_j * v_hs)))),
|
||||
a_ds,
|
||||
wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, ((uint64_t)(v_histograms + ((uint64_t)(v_j * v_hs)))), a_ds));
|
||||
v_c);
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_new_clusters + ((uint64_t)(v_j * 4u)))), ((uint32_t)(v_j)));
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_symbols + ((uint64_t)(v_j * 4u)))), ((uint32_t)(v_j)));
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_sizes + ((uint64_t)(v_j * 4u)))), 1u);
|
||||
@@ -89521,6 +89529,8 @@ wuffs_brotli__metablock_builder__histogram_bit_cost_distance(
|
||||
uint64_t a_histogram,
|
||||
uint64_t a_candidate,
|
||||
uint64_t a_tmp) {
|
||||
uint64_t v_c = 0;
|
||||
|
||||
if (wuffs_brotli__metablock_builder__h_total(self, a_wb, a_histogram, a_ds) == 0u) {
|
||||
return 0u;
|
||||
}
|
||||
@@ -89534,7 +89544,8 @@ wuffs_brotli__metablock_builder__histogram_bit_cost_distance(
|
||||
a_tmp,
|
||||
a_candidate,
|
||||
a_ds);
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, a_ds), wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, a_candidate, a_ds));
|
||||
v_c = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, a_ds);
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_c, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, a_candidate, a_ds));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_remap
|
||||
@@ -89698,6 +89709,7 @@ wuffs_brotli__metablock_builder__cluster_histograms(
|
||||
uint64_t a_max_histograms,
|
||||
uint64_t a_out_histos,
|
||||
uint64_t a_histogram_symbols) {
|
||||
uint64_t v_c = 0;
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_mark = 0;
|
||||
uint64_t v_cluster_size = 0;
|
||||
@@ -89735,11 +89747,12 @@ wuffs_brotli__metablock_builder__cluster_histograms(
|
||||
((uint64_t)(a_out_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
((uint64_t)(a_in_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
a_ds);
|
||||
v_c = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, ((uint64_t)(a_in_histos + ((uint64_t)(v_i * v_hs)))), a_ds);
|
||||
wuffs_brotli__metablock_builder__h_set_bit_cost(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_out_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
a_ds,
|
||||
wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, ((uint64_t)(a_in_histos + ((uint64_t)(v_i * v_hs)))), a_ds));
|
||||
v_c);
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_histogram_symbols + ((uint64_t)(v_i * 4u)))), ((uint32_t)(v_i)));
|
||||
v_i += 1u;
|
||||
}
|
||||
@@ -96423,7 +96436,8 @@ wuffs_brotli__encoder__estimate_bit_costs_for_literals_utf8(
|
||||
} else if (v_utf8_pos == 2u) {
|
||||
v_iw = v_iwu2;
|
||||
}
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_iw), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_brotli__bit_cost__fast_log2_memo(&self->private_data.f_bc, v_iw);
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_lit_cost, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_lit_cost, 4584030313835631162u);
|
||||
wuffs_brotli__encoder__lc_finish_cost(self,
|
||||
a_wb,
|
||||
@@ -96494,7 +96508,8 @@ wuffs_brotli__encoder__estimate_bit_costs_for_literals(
|
||||
if (v_histo == 0u) {
|
||||
v_histo = 1u;
|
||||
}
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_in_window), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_brotli__bit_cost__fast_log2_memo(&self->private_data.f_bc, v_in_window);
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_lit_cost, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_lit_cost, 4584015902316823577u);
|
||||
wuffs_brotli__encoder__lc_finish_cost(self,
|
||||
a_wb,
|
||||
@@ -96824,12 +96839,15 @@ wuffs_brotli__metablock_builder__h_set_bit_cost(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_population_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds) {
|
||||
uint64_t v_c = 0;
|
||||
|
||||
if (a_h <= ((uint64_t)(a_wb.len))) {
|
||||
return wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_h), a_ds);
|
||||
v_c = wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_h), a_ds);
|
||||
return v_c;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
@@ -97085,7 +97103,8 @@ wuffs_brotli__metablock_builder__compute_distance_cost(
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
self->private_impl.f_dist_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, 544u), v_extra_bits);
|
||||
self->private_impl.f_dist_cost = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, 544u);
|
||||
self->private_impl.f_dist_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, self->private_impl.f_dist_cost, v_extra_bits);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -97205,7 +97224,6 @@ wuffs_brotli__metablock_builder__build_meta_block(
|
||||
uint32_t v_v = 0;
|
||||
|
||||
wuffs_brotli__metablock_builder__reset_arena(self, ((uint64_t)(a_wb.len)));
|
||||
wuffs_brotli__bit_cost__init_log2_cache(&self->private_data.f_bc);
|
||||
v_orig_npostfix = self->private_impl.f_dist_postfix_bits;
|
||||
v_orig_ndirect = self->private_impl.f_dist_num_direct_codes;
|
||||
v_orig_asm = self->private_impl.f_dist_alphabet_size_max;
|
||||
@@ -97754,12 +97772,15 @@ wuffs_brotli__encoder__gbs_histo_off(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__gbs_entropy(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_size) {
|
||||
uint64_t v_e = 0;
|
||||
|
||||
if (a_off <= ((uint64_t)(a_wb.len))) {
|
||||
return wuffs_brotli__bit_cost__bits_entropy(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_off), a_size);
|
||||
v_e = wuffs_brotli__bit_cost__bits_entropy(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_off), a_size);
|
||||
return v_e;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
@@ -99396,18 +99417,20 @@ wuffs_brotli__encoder__hq_extend_var_region(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__bits_entropy(
|
||||
const wuffs_brotli__metablock_tester* self,
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_population,
|
||||
uint64_t a_size) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
|
||||
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return wuffs_brotli__bit_cost__bits_entropy(&self->private_data.f_bc, a_population, a_size);
|
||||
uint64_t v_c = 0;
|
||||
|
||||
v_c = wuffs_brotli__bit_cost__bits_entropy(&self->private_data.f_bc, a_population, a_size);
|
||||
return v_c;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.population_cost
|
||||
@@ -99415,23 +99438,27 @@ wuffs_brotli__metablock_tester__bits_entropy(
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__population_cost(
|
||||
const wuffs_brotli__metablock_tester* self,
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_histogram,
|
||||
uint32_t a_data_size) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
|
||||
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t v_c = 0;
|
||||
|
||||
if (a_data_size == 256u) {
|
||||
return wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, a_histogram, 256u);
|
||||
v_c = wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, a_histogram, 256u);
|
||||
return v_c;
|
||||
} else if (a_data_size == 704u) {
|
||||
return wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, a_histogram, 704u);
|
||||
v_c = wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, a_histogram, 704u);
|
||||
return v_c;
|
||||
} else if (a_data_size == 544u) {
|
||||
return wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, a_histogram, 544u);
|
||||
v_c = wuffs_brotli__bit_cost__population_cost(&self->private_data.f_bc, a_histogram, 544u);
|
||||
return v_c;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
@@ -99481,7 +99508,6 @@ wuffs_brotli__metablock_tester__split_block(
|
||||
|
||||
wuffs_brotli__metablock_builder__set_params(&self->private_data.f_mb, a_quality, false, false);
|
||||
wuffs_brotli__metablock_builder__reset_arena(&self->private_data.f_mb, ((uint64_t)(a_workbuf.len)));
|
||||
wuffs_brotli__bit_cost__init_log2_cache(&self->private_data.f_mb.private_data.f_bc);
|
||||
wuffs_brotli__metablock_builder__alloc_block_splits(&self->private_data.f_mb, a_cmds, a_num_commands);
|
||||
if (self->private_data.f_mb.private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
@@ -99535,7 +99561,6 @@ wuffs_brotli__metablock_tester__cluster_histograms(
|
||||
return 18446744073709551615u;
|
||||
}
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(&self->private_data.f_mb, v_ds);
|
||||
wuffs_brotli__bit_cost__init_log2_cache(&self->private_data.f_mb.private_data.f_bc);
|
||||
wuffs_brotli__metablock_builder__reset_arena(&self->private_data.f_mb, ((uint64_t)(a_workbuf.len)));
|
||||
v_n = ((uint64_t)(a_in_size * v_hs));
|
||||
v_in_off = wuffs_brotli__metablock_builder__alloc(&self->private_data.f_mb, v_n);
|
||||
|
||||
@@ -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