mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Merge branch 'm4b-metablock' into m4a-encoder-core
# Conflicts: # release/c/wuffs-unsupported-snapshot.c
This commit is contained in:
7 files changed
+7382
-4
No files matched your search
@@ -9281,6 +9281,8 @@ typedef struct wuffs_brotli__bit_cost__struct wuffs_brotli__bit_cost;
|
||||
|
||||
typedef struct wuffs_brotli__encoder__struct wuffs_brotli__encoder;
|
||||
|
||||
typedef struct wuffs_brotli__metablock_builder__struct wuffs_brotli__metablock_builder;
|
||||
|
||||
typedef struct wuffs_brotli__metablock_tester__struct wuffs_brotli__metablock_tester;
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -9469,6 +9471,68 @@ wuffs_brotli__metablock_tester__population_cost(
|
||||
wuffs_base__slice_u8 a_histogram,
|
||||
uint32_t a_data_size);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_tester__init_distance_params(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect,
|
||||
uint32_t a_large_window);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_brotli__metablock_tester__split_block(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint32_t a_quality,
|
||||
wuffs_base__slice_u8 a_workbuf);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__cluster_histograms(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
uint32_t a_data_size,
|
||||
wuffs_base__slice_u8 a_in_histos,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_max_histograms,
|
||||
wuffs_base__slice_u8 a_workbuf);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_brotli__metablock_tester__build_meta_block(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
wuffs_base__slice_u8 a_ringbuffer,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint8_t a_prev_byte,
|
||||
uint8_t a_prev_byte2,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_literal_context_mode,
|
||||
uint32_t a_quality,
|
||||
uint32_t a_large_window,
|
||||
uint32_t a_disable_literal_context_modeling,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_tester__optimize_histograms(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
uint32_t a_num_distance_codes);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__get(
|
||||
const wuffs_brotli__metablock_tester* self,
|
||||
uint32_t a_key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -9815,10 +9879,12 @@ struct wuffs_brotli__bit_cost__struct {
|
||||
uint32_t active_coroutine;
|
||||
wuffs_base__vtable null_vtable;
|
||||
|
||||
bool f_log2_cache_ready;
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
wuffs_softfloat__arith f_sf;
|
||||
uint64_t f_log2_cache[65536];
|
||||
} private_data;
|
||||
|
||||
}; // struct wuffs_brotli__bit_cost__struct
|
||||
@@ -10127,6 +10193,54 @@ struct wuffs_brotli__encoder__struct {
|
||||
#endif // __cplusplus
|
||||
}; // struct wuffs_brotli__encoder__struct
|
||||
|
||||
struct wuffs_brotli__metablock_builder__struct {
|
||||
// Do not access the private_impl's or private_data's fields directly. There
|
||||
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
|
||||
// the wuffs_foo__bar__baz functions.
|
||||
//
|
||||
// It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct
|
||||
// can be stack allocated when WUFFS_IMPLEMENTATION is defined.
|
||||
|
||||
struct {
|
||||
uint32_t magic;
|
||||
uint32_t active_coroutine;
|
||||
wuffs_base__vtable null_vtable;
|
||||
|
||||
uint32_t f_quality;
|
||||
bool f_large_window;
|
||||
bool f_disable_lcm;
|
||||
uint32_t f_dist_postfix_bits;
|
||||
uint32_t f_dist_num_direct_codes;
|
||||
uint32_t f_dist_alphabet_size_max;
|
||||
uint32_t f_dist_alphabet_size_limit;
|
||||
uint64_t f_dist_max_distance;
|
||||
uint64_t f_split_num_types[3];
|
||||
uint64_t f_split_num_blocks[3];
|
||||
uint64_t f_split_types_off[3];
|
||||
uint64_t f_split_lengths_off[3];
|
||||
uint64_t f_context_map_off[3];
|
||||
uint64_t f_context_map_size[3];
|
||||
uint64_t f_histograms_off[3];
|
||||
uint64_t f_histograms_size[3];
|
||||
uint64_t f_wb_len;
|
||||
uint64_t f_wb_top;
|
||||
uint64_t f_wb_high;
|
||||
bool f_oom;
|
||||
uint64_t f_num_pairs;
|
||||
uint64_t f_dist_cost;
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
wuffs_softfloat__arith f_sf;
|
||||
wuffs_brotli__bit_cost f_bc;
|
||||
uint64_t f_cost[256];
|
||||
uint64_t f_insert_cost[35200];
|
||||
uint16_t f_new_id[256];
|
||||
uint8_t f_good_for_rle[704];
|
||||
} private_data;
|
||||
|
||||
}; // struct wuffs_brotli__metablock_builder__struct
|
||||
|
||||
struct wuffs_brotli__metablock_tester__struct {
|
||||
// Do not access the private_impl's or private_data's fields directly. There
|
||||
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
|
||||
@@ -10140,11 +10254,12 @@ struct wuffs_brotli__metablock_tester__struct {
|
||||
uint32_t active_coroutine;
|
||||
wuffs_base__vtable null_vtable;
|
||||
|
||||
uint32_t f_dummy;
|
||||
uint64_t f_worst_case;
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
wuffs_brotli__bit_cost f_bc;
|
||||
wuffs_brotli__metablock_builder f_mb;
|
||||
} private_data;
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -10212,6 +10327,68 @@ struct wuffs_brotli__metablock_tester__struct {
|
||||
return wuffs_brotli__metablock_tester__population_cost(this, a_histogram, a_data_size);
|
||||
}
|
||||
|
||||
inline wuffs_base__empty_struct
|
||||
init_distance_params(
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect,
|
||||
uint32_t a_large_window) {
|
||||
return wuffs_brotli__metablock_tester__init_distance_params(this, a_npostfix, a_ndirect, a_large_window);
|
||||
}
|
||||
|
||||
inline wuffs_base__status
|
||||
split_block(
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint32_t a_quality,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
return wuffs_brotli__metablock_tester__split_block(this, a_cmds, a_num_commands, a_data, a_pos, a_mask, a_quality, a_workbuf);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
cluster_histograms(
|
||||
uint32_t a_data_size,
|
||||
wuffs_base__slice_u8 a_in_histos,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_max_histograms,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
return wuffs_brotli__metablock_tester__cluster_histograms(this, a_data_size, a_in_histos, a_in_size, a_max_histograms, a_workbuf);
|
||||
}
|
||||
|
||||
inline wuffs_base__status
|
||||
build_meta_block(
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
wuffs_base__slice_u8 a_ringbuffer,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint8_t a_prev_byte,
|
||||
uint8_t a_prev_byte2,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_literal_context_mode,
|
||||
uint32_t a_quality,
|
||||
uint32_t a_large_window,
|
||||
uint32_t a_disable_literal_context_modeling,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect) {
|
||||
return wuffs_brotli__metablock_tester__build_meta_block(this, a_workbuf, a_ringbuffer, a_pos, a_mask, a_prev_byte, a_prev_byte2, a_cmds, a_num_commands, a_literal_context_mode, a_quality, a_large_window, a_disable_literal_context_modeling, a_npostfix, a_ndirect);
|
||||
}
|
||||
|
||||
inline wuffs_base__empty_struct
|
||||
optimize_histograms(
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
uint32_t a_num_distance_codes) {
|
||||
return wuffs_brotli__metablock_tester__optimize_histograms(this, a_workbuf, a_num_distance_codes);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
get(
|
||||
uint32_t a_key) const {
|
||||
return wuffs_brotli__metablock_tester__get(this, a_key);
|
||||
}
|
||||
|
||||
#endif // __cplusplus
|
||||
}; // struct wuffs_brotli__metablock_tester__struct
|
||||
|
||||
@@ -68271,6 +68448,18 @@ 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
|
||||
|
||||
#define WUFFS_BROTLI__DISTANCE_BLOCK_SWITCH_COST 4624408687374709555u
|
||||
|
||||
#define WUFFS_BROTLI__HISTOGRAMS_PER_BATCH 64u
|
||||
|
||||
#define WUFFS_BROTLI__CLUSTERS_PER_BATCH 16u
|
||||
|
||||
#define WUFFS_BROTLI__STREAM_PROCESSING 0u
|
||||
|
||||
#define WUFFS_BROTLI__STREAM_FLUSH_REQUESTED 1u
|
||||
@@ -68357,6 +68546,18 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__SCORE_BASE 1920u
|
||||
|
||||
#define WUFFS_BROTLI__MAX_NUMBER_OF_HISTOGRAMS 256u
|
||||
|
||||
#define WUFFS_BROTLI__INVALID_INDEX 4294967295u
|
||||
|
||||
#define WUFFS_BROTLI__HISTOGRAM_PAIR_SIZE 24u
|
||||
|
||||
#define WUFFS_BROTLI__CATEGORY_LITERAL 0u
|
||||
|
||||
#define WUFFS_BROTLI__CATEGORY_COMMAND 1u
|
||||
|
||||
#define WUFFS_BROTLI__CATEGORY_DISTANCE 2u
|
||||
|
||||
#define WUFFS_BROTLI__GBS_MAX_NUMBER_OF_BLOCK_TYPES 256u
|
||||
|
||||
#define WUFFS_BROTLI__GBS_F64_400 4645744490609377280u
|
||||
@@ -68376,6 +68577,13 @@ wuffs_brotli__bit_cost__initialize(
|
||||
uint64_t wuffs_version,
|
||||
uint32_t options);
|
||||
|
||||
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
wuffs_brotli__metablock_builder__initialize(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
size_t sizeof_star_self,
|
||||
uint64_t wuffs_version,
|
||||
uint32_t options);
|
||||
|
||||
// ---------------- Private Function Prototypes
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -68869,6 +69077,11 @@ 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(
|
||||
@@ -69225,6 +69438,156 @@ wuffs_brotli__encoder__find_match_length_with_limit(
|
||||
uint64_t a_i2,
|
||||
uint64_t a_limit);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__bit_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_count);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__histogram_add_vector(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_data,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_n,
|
||||
uint32_t a_width);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__copy_literals_to_byte_array(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_offset,
|
||||
uint64_t a_mask,
|
||||
uint64_t a_literals);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__copy_bytes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_dst,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_src,
|
||||
uint64_t a_n);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__split_block(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__initial_entropy_codes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_stride,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__refine_entropy_codes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_stride,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint64_t a_tmp,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__find_blocks(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_block_switch_bitcost,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint64_t a_switch_signal,
|
||||
uint64_t a_block_id,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint8_t
|
||||
wuffs_brotli__metablock_builder__ld8(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__remap_block_ids(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_block_ids,
|
||||
uint64_t a_length,
|
||||
uint64_t a_num_histograms);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__build_block_histograms(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_block_ids,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__cluster_blocks(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_cat,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_num_blocks,
|
||||
uint64_t a_block_ids,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__split_byte_vector(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_cat,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_symbols_per_histogram,
|
||||
uint64_t a_max_histograms,
|
||||
uint64_t a_sampling_stride_length,
|
||||
uint64_t a_block_switch_cost);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__sanitized_quality(
|
||||
@@ -69282,6 +69645,120 @@ wuffs_brotli__encoder__write_metadata_header(
|
||||
wuffs_brotli__encoder* self,
|
||||
uint32_t a_block_size);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__metablock_builder__histogram_pair_is_less(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_p1_cost_diff,
|
||||
uint32_t a_p1_idx1,
|
||||
uint32_t a_p1_idx2,
|
||||
uint64_t a_p2_cost_diff,
|
||||
uint32_t a_p2_idx1,
|
||||
uint32_t a_p2_idx2);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__cluster_cost_diff(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_size_a,
|
||||
uint64_t a_size_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__pair_copy(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_dst,
|
||||
uint64_t a_src);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__pair_set(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_dst,
|
||||
uint32_t a_idx1,
|
||||
uint32_t a_idx2,
|
||||
uint64_t a_cost_combo,
|
||||
uint64_t a_cost_diff);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__compare_and_push_to_queue(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_out,
|
||||
uint64_t a_tmp,
|
||||
uint64_t a_cluster_size,
|
||||
uint32_t a_idx1,
|
||||
uint32_t a_idx2,
|
||||
uint64_t a_max_num_pairs,
|
||||
uint64_t a_pairs);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__histogram_combine(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_out,
|
||||
uint64_t a_tmp,
|
||||
uint64_t a_cluster_size,
|
||||
uint64_t a_symbols,
|
||||
uint64_t a_clusters,
|
||||
uint64_t a_pairs,
|
||||
uint64_t a_num_clusters,
|
||||
uint64_t a_symbols_size,
|
||||
uint64_t a_max_clusters,
|
||||
uint64_t a_max_num_pairs);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__histogram_bit_cost_distance(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_histogram,
|
||||
uint64_t a_candidate,
|
||||
uint64_t a_tmp);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__histogram_remap(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_in_histos,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_clusters,
|
||||
uint64_t a_num_clusters,
|
||||
uint64_t a_out,
|
||||
uint64_t a_tmp,
|
||||
uint64_t a_symbols);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__histogram_reindex(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_out,
|
||||
uint64_t a_symbols,
|
||||
uint64_t a_length);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__cluster_histograms(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_in_histos,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_max_histograms,
|
||||
uint64_t a_out_histos,
|
||||
uint64_t a_histogram_symbols);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__computed_lgblock(
|
||||
@@ -70211,6 +70688,300 @@ wuffs_brotli__bit_cost__command_distance_context(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_cmd_prefix);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__ld16(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__ld32(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__ld64(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st8(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint8_t a_v);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st16(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint32_t a_v);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st32(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint32_t a_v);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st64(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint64_t a_v);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__fill32(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint64_t a_n,
|
||||
uint32_t a_v);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__zero(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint64_t a_n);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__sym(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_base,
|
||||
uint64_t a_i,
|
||||
uint32_t a_width);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__alloc(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_n);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__hsize(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_clear(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_add(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_val);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_add_histogram(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint64_t a_v,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_copy(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint64_t a_v,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_total(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_bit_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_set_bit_cost(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_bit_cost);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_population_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__log2_floor_non_zero(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_n);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__prefix_encode_copy_distance(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_distance_code,
|
||||
uint64_t a_num_direct_codes,
|
||||
uint32_t a_postfix_bits);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__command_restore_distance_code(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_c,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__calculate_distance_code_limit(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_max_distance,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__init_distance_params(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect,
|
||||
bool a_large_window);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__recompute_distance_prefixes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_orig_npostfix,
|
||||
uint32_t a_orig_ndirect);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__metablock_builder__compute_distance_cost(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_orig_npostfix,
|
||||
uint32_t a_orig_ndirect,
|
||||
uint32_t a_new_npostfix,
|
||||
uint32_t a_new_ndirect,
|
||||
uint64_t a_new_max_distance,
|
||||
uint64_t a_tmp);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__set_params(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_quality,
|
||||
bool a_large_window,
|
||||
bool a_disable_literal_context_modeling);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__alloc_block_splits(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__reset_arena(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_wb_len);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__metablock_builder__build_meta_block(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_ringbuffer,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint8_t a_prev_byte,
|
||||
uint8_t a_prev_byte2,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_literal_context_mode);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__optimize_histograms(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_num_distance_codes);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_counts,
|
||||
uint64_t a_length);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__sbv_scratch_len(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_length,
|
||||
uint64_t a_hs,
|
||||
uint64_t a_symbols_per_histogram,
|
||||
uint64_t a_max_histograms);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__cluster_scratch_len(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_hs);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__workbuf_len_worst_case(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_num_literals,
|
||||
uint64_t a_num_commands);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__gbs_histo_off(
|
||||
@@ -70630,6 +71401,63 @@ sizeof__wuffs_brotli__encoder(void) {
|
||||
return sizeof(wuffs_brotli__encoder);
|
||||
}
|
||||
|
||||
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
wuffs_brotli__metablock_builder__initialize(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
size_t sizeof_star_self,
|
||||
uint64_t wuffs_version,
|
||||
uint32_t options){
|
||||
if (!self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
if (sizeof(*self) != sizeof_star_self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);
|
||||
}
|
||||
if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) ||
|
||||
(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);
|
||||
}
|
||||
|
||||
if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) {
|
||||
// The whole point of this if-check is to detect an uninitialized *self.
|
||||
// We disable the warning on GCC. Clang-5.0 does not have this warning.
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
||||
#endif
|
||||
if (self->private_impl.magic != 0) {
|
||||
return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed);
|
||||
}
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
} else {
|
||||
if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) {
|
||||
memset(self, 0, sizeof(*self));
|
||||
options |= WUFFS_INITIALIZE__ALREADY_ZEROED;
|
||||
} else {
|
||||
memset(&(self->private_impl), 0, sizeof(self->private_impl));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
wuffs_base__status z = wuffs_softfloat__arith__initialize(
|
||||
&self->private_data.f_sf, sizeof(self->private_data.f_sf), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
{
|
||||
wuffs_base__status z = wuffs_brotli__bit_cost__initialize(
|
||||
&self->private_data.f_bc, sizeof(self->private_data.f_bc), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
wuffs_brotli__metablock_tester__initialize(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
@@ -70676,6 +71504,13 @@ wuffs_brotli__metablock_tester__initialize(
|
||||
return z;
|
||||
}
|
||||
}
|
||||
{
|
||||
wuffs_base__status z = wuffs_brotli__metablock_builder__initialize(
|
||||
&self->private_data.f_mb, sizeof(self->private_data.f_mb), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
@@ -75073,6 +75908,30 @@ 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
|
||||
@@ -75160,6 +76019,8 @@ 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];
|
||||
}
|
||||
return wuffs_softfloat__arith__log2_u64(&self->private_data.f_sf, a_v);
|
||||
}
|
||||
@@ -76936,6 +77797,951 @@ wuffs_brotli__encoder__find_match_length_with_limit(
|
||||
return v_matched;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.bit_cost
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__bit_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_count) {
|
||||
if (a_count == 0u) {
|
||||
return 13835058055282163712u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_count);
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_add_vector
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__histogram_add_vector(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_data,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_n,
|
||||
uint32_t a_width) {
|
||||
uint64_t v_total_offset = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_s = 0;
|
||||
uint64_t v_o = 0;
|
||||
|
||||
v_total_offset = ((uint64_t)(a_h + (((uint64_t)(a_ds)) * 4u)));
|
||||
wuffs_brotli__metablock_builder__st64(self, a_wb, v_total_offset, ((uint64_t)(wuffs_brotli__metablock_builder__ld64(self, a_wb, v_total_offset) + a_n)));
|
||||
v_i = 0u;
|
||||
while (v_i < a_n) {
|
||||
v_s = wuffs_brotli__metablock_builder__sym(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
((uint64_t)(a_pos + v_i)),
|
||||
a_width);
|
||||
if (v_s < ((uint64_t)(a_ds))) {
|
||||
v_o = ((uint64_t)(a_h + ((uint64_t)(v_s * 4u))));
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, v_o, ((uint32_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, v_o) + 1u)));
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.copy_literals_to_byte_array
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__copy_literals_to_byte_array(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_offset,
|
||||
uint64_t a_mask,
|
||||
uint64_t a_literals) {
|
||||
uint64_t v_pos = 0;
|
||||
uint64_t v_from_pos = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_c = 0;
|
||||
uint64_t v_insert_len = 0;
|
||||
uint64_t v_head_size = 0;
|
||||
|
||||
v_from_pos = (a_offset & a_mask);
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
v_c = ((uint64_t)(v_i * 16u));
|
||||
v_insert_len = ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_cmds, v_c)));
|
||||
if (((uint64_t)(v_from_pos + v_insert_len)) > a_mask) {
|
||||
v_head_size = ((uint64_t)(((uint64_t)(a_mask + 1u)) - v_from_pos));
|
||||
wuffs_brotli__metablock_builder__copy_bytes(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_literals + v_pos)),
|
||||
a_data,
|
||||
v_from_pos,
|
||||
v_head_size);
|
||||
v_from_pos = 0u;
|
||||
v_pos += v_head_size;
|
||||
v_insert_len -= v_head_size;
|
||||
}
|
||||
if (v_insert_len > 0u) {
|
||||
wuffs_brotli__metablock_builder__copy_bytes(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_literals + v_pos)),
|
||||
a_data,
|
||||
v_from_pos,
|
||||
v_insert_len);
|
||||
v_pos += v_insert_len;
|
||||
}
|
||||
v_from_pos = (((uint64_t)(((uint64_t)(v_from_pos + v_insert_len)) + ((uint64_t)((wuffs_brotli__metablock_builder__ld32(self, a_cmds, ((uint64_t)(v_c + 4u))) & 33554431u))))) & a_mask);
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.copy_bytes
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__copy_bytes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_dst,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_src,
|
||||
uint64_t a_n) {
|
||||
wuffs_base__slice_u8 v_d = {0};
|
||||
wuffs_base__slice_u8 v_s = {0};
|
||||
|
||||
if ((a_dst <= ((uint64_t)(a_wb.len))) && (a_src <= ((uint64_t)(a_data.len)))) {
|
||||
v_d = wuffs_base__slice_u8__subslice_i(a_wb, a_dst);
|
||||
v_s = wuffs_base__slice_u8__subslice_i(a_data, a_src);
|
||||
if ((a_n <= ((uint64_t)(v_d.len))) && (a_n <= ((uint64_t)(v_s.len)))) {
|
||||
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__slice_u8__subslice_j(v_d, a_n), wuffs_base__slice_u8__subslice_j(v_s, a_n));
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.split_block
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__split_block(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask) {
|
||||
uint64_t v_mark = 0;
|
||||
uint64_t v_literals_count = 0;
|
||||
uint64_t v_literals = 0;
|
||||
uint64_t v_codes = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_c = 0;
|
||||
|
||||
v_mark = self->private_impl.f_wb_top;
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
v_literals_count += ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_cmds, ((uint64_t)(v_i * 16u)))));
|
||||
v_i += 1u;
|
||||
}
|
||||
v_literals = wuffs_brotli__metablock_builder__alloc(self, v_literals_count);
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
wuffs_brotli__metablock_builder__copy_literals_to_byte_array(self,
|
||||
a_wb,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
a_data,
|
||||
a_pos,
|
||||
a_mask,
|
||||
v_literals);
|
||||
wuffs_brotli__metablock_builder__split_byte_vector(self,
|
||||
a_wb,
|
||||
0u,
|
||||
v_literals,
|
||||
v_literals_count,
|
||||
1u,
|
||||
256u,
|
||||
544u,
|
||||
100u,
|
||||
70u,
|
||||
4628602664527698330u);
|
||||
self->private_impl.f_wb_top = v_mark;
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_codes = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_num_commands * 2u)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
wuffs_brotli__metablock_builder__st16(self, a_wb, ((uint64_t)(v_codes + ((uint64_t)(v_i * 2u)))), wuffs_brotli__metablock_builder__ld16(self, a_cmds, ((uint64_t)(((uint64_t)(v_i * 16u)) + 12u))));
|
||||
v_i += 1u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__split_byte_vector(self,
|
||||
a_wb,
|
||||
1u,
|
||||
v_codes,
|
||||
a_num_commands,
|
||||
2u,
|
||||
704u,
|
||||
530u,
|
||||
50u,
|
||||
40u,
|
||||
4623789442425946112u);
|
||||
self->private_impl.f_wb_top = v_mark;
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_codes = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_num_commands * 2u)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_j = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
v_c = ((uint64_t)(v_i * 16u));
|
||||
if (((wuffs_brotli__metablock_builder__ld32(self, a_cmds, ((uint64_t)(v_c + 4u))) & 33554431u) != 0u) && (wuffs_brotli__metablock_builder__ld16(self, a_cmds, ((uint64_t)(v_c + 12u))) >= 128u)) {
|
||||
wuffs_brotli__metablock_builder__st16(self, a_wb, ((uint64_t)(v_codes + ((uint64_t)(v_j * 2u)))), (wuffs_brotli__metablock_builder__ld16(self, a_cmds, ((uint64_t)(v_c + 14u))) & 1023u));
|
||||
v_j += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__split_byte_vector(self,
|
||||
a_wb,
|
||||
2u,
|
||||
v_codes,
|
||||
v_j,
|
||||
2u,
|
||||
544u,
|
||||
544u,
|
||||
50u,
|
||||
40u,
|
||||
4624408687374709555u);
|
||||
self->private_impl.f_wb_top = v_mark;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.initial_entropy_codes
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__initial_entropy_codes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_stride,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint32_t a_ds) {
|
||||
uint32_t v_seed = 0;
|
||||
uint64_t v_block_length = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_pos = 0;
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_nh = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_seed = 7u;
|
||||
if (a_num_histograms == 0u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_nh = wuffs_base__u64__max(a_num_histograms, 1u);
|
||||
v_block_length = (a_length / wuffs_base__u64__max(v_nh, 1u));
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_histograms) {
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, ((uint64_t)(a_histograms + ((uint64_t)(v_i * v_hs)))), a_ds);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_histograms) {
|
||||
v_pos = (((uint64_t)(a_length * v_i)) / wuffs_base__u64__max(v_nh, 1u));
|
||||
if (v_i != 0u) {
|
||||
v_seed *= 16807u;
|
||||
if (v_block_length > 0u) {
|
||||
v_pos += (((uint64_t)(v_seed)) % v_block_length);
|
||||
}
|
||||
}
|
||||
if (((uint64_t)(v_pos + a_stride)) >= a_length) {
|
||||
v_pos = ((uint64_t)(((uint64_t)(a_length - a_stride)) - 1u));
|
||||
}
|
||||
wuffs_brotli__metablock_builder__histogram_add_vector(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_histograms + ((uint64_t)(v_i * v_hs)))),
|
||||
a_ds,
|
||||
a_data,
|
||||
v_pos,
|
||||
a_stride,
|
||||
a_width);
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.refine_entropy_codes
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__refine_entropy_codes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_stride,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint64_t a_tmp,
|
||||
uint32_t a_ds) {
|
||||
uint64_t v_iters = 0;
|
||||
uint32_t v_seed = 0;
|
||||
uint64_t v_iter = 0;
|
||||
uint64_t v_pos = 0;
|
||||
uint64_t v_stride = 0;
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_nh = 0;
|
||||
|
||||
if ((a_stride == 0u) || (a_num_histograms == 0u)) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_nh = wuffs_base__u64__max(a_num_histograms, 1u);
|
||||
v_iters = ((uint64_t)((((uint64_t)(2u * a_length)) / wuffs_base__u64__max(a_stride, 1u)) + 100u));
|
||||
v_seed = 7u;
|
||||
v_iters = ((uint64_t)((((uint64_t)(((uint64_t)(v_iters + v_nh)) - 1u)) / wuffs_base__u64__max(v_nh, 1u)) * v_nh));
|
||||
v_iter = 0u;
|
||||
while (v_iter < v_iters) {
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, a_tmp, a_ds);
|
||||
v_pos = 0u;
|
||||
v_stride = a_stride;
|
||||
if (v_stride >= a_length) {
|
||||
v_stride = a_length;
|
||||
} else {
|
||||
v_seed *= 16807u;
|
||||
v_pos = ((uint64_t)(((uint64_t)(a_length - v_stride)) + 1u));
|
||||
v_pos = (((uint64_t)(v_seed)) % wuffs_base__u64__max(v_pos, 1u));
|
||||
}
|
||||
wuffs_brotli__metablock_builder__histogram_add_vector(self,
|
||||
a_wb,
|
||||
a_tmp,
|
||||
a_ds,
|
||||
a_data,
|
||||
v_pos,
|
||||
v_stride,
|
||||
a_width);
|
||||
wuffs_brotli__metablock_builder__h_add_histogram(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_histograms + ((uint64_t)((v_iter % wuffs_base__u64__max(v_nh, 1u)) * v_hs)))),
|
||||
a_tmp,
|
||||
a_ds);
|
||||
v_iter += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.find_blocks
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__find_blocks(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_block_switch_bitcost,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint64_t a_switch_signal,
|
||||
uint64_t a_block_id,
|
||||
uint32_t a_ds) {
|
||||
uint64_t v_alphabet_size = 0;
|
||||
uint64_t v_bitmap_len = 0;
|
||||
uint64_t v_num_blocks = 0;
|
||||
uint64_t v_byte_ix = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_k = 0;
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_ix = 0;
|
||||
uint64_t v_insert_cost_ix = 0;
|
||||
uint64_t v_min_cost = 0;
|
||||
uint64_t v_block_switch_cost = 0;
|
||||
uint64_t v_c = 0;
|
||||
uint8_t v_cur_id = 0;
|
||||
uint8_t v_b = 0;
|
||||
|
||||
v_alphabet_size = ((uint64_t)(a_ds));
|
||||
v_bitmap_len = (((uint64_t)(a_num_histograms + 7u)) >> 3u);
|
||||
v_num_blocks = 1u;
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
if (a_num_histograms <= 1u) {
|
||||
wuffs_brotli__metablock_builder__zero(self, a_wb, a_block_id, a_length);
|
||||
return 1u;
|
||||
}
|
||||
if ((a_num_histograms > 256u) || (((uint64_t)(v_alphabet_size * a_num_histograms)) > 35200u)) {
|
||||
self->private_impl.f_oom = true;
|
||||
return 1u;
|
||||
}
|
||||
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));
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = v_alphabet_size;
|
||||
while (v_i != 0u) {
|
||||
v_i -= 1u;
|
||||
v_j = 0u;
|
||||
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_j += 1u;
|
||||
}
|
||||
}
|
||||
wuffs_private_impl__bulk_memset(&self->private_data.f_cost[0], 256u * (size_t)8u, 0u);
|
||||
wuffs_brotli__metablock_builder__zero(self, a_wb, a_switch_signal, ((uint64_t)(a_length * v_bitmap_len)));
|
||||
v_byte_ix = 0u;
|
||||
while (v_byte_ix < a_length) {
|
||||
v_ix = ((uint64_t)(v_byte_ix * v_bitmap_len));
|
||||
v_insert_cost_ix = ((uint64_t)(wuffs_brotli__metablock_builder__sym(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
v_byte_ix,
|
||||
a_width) * a_num_histograms));
|
||||
v_min_cost = 6088095589093318446u;
|
||||
v_block_switch_cost = a_block_switch_bitcost;
|
||||
v_k = 0u;
|
||||
while (v_k < a_num_histograms) {
|
||||
v_c = 0u;
|
||||
if (((uint64_t)(v_insert_cost_ix + v_k)) < 35200u) {
|
||||
v_c = self->private_data.f_insert_cost[(((uint64_t)(v_insert_cost_ix + v_k)) % 35200u)];
|
||||
}
|
||||
v_c = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, self->private_data.f_cost[(v_k & 255u)], v_c);
|
||||
self->private_data.f_cost[(v_k & 255u)] = v_c;
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_c, v_min_cost) != 0u) {
|
||||
v_min_cost = v_c;
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(a_block_id + v_byte_ix)), ((uint8_t)(v_k)));
|
||||
}
|
||||
v_k += 1u;
|
||||
}
|
||||
if (v_byte_ix < 2000u) {
|
||||
v_block_switch_cost = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_block_switch_cost, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, 4605110762971426980u, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4540289913102728147u, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_byte_ix))));
|
||||
}
|
||||
v_k = 0u;
|
||||
while (v_k < a_num_histograms) {
|
||||
v_c = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, self->private_data.f_cost[(v_k & 255u)], v_min_cost);
|
||||
self->private_data.f_cost[(v_k & 255u)] = v_c;
|
||||
if (wuffs_softfloat__arith__f64_ge(&self->private_data.f_sf, v_c, v_block_switch_cost) != 0u) {
|
||||
self->private_data.f_cost[(v_k & 255u)] = v_block_switch_cost;
|
||||
v_j = ((uint64_t)(v_ix + (v_k >> 3u)));
|
||||
v_b = wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_switch_signal + v_j)));
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(a_switch_signal + v_j)), ((uint8_t)(v_b | ((uint8_t)(((uint8_t)(1u)) << (v_k & 7u))))));
|
||||
}
|
||||
v_k += 1u;
|
||||
}
|
||||
v_byte_ix += 1u;
|
||||
}
|
||||
v_byte_ix = ((uint64_t)(a_length - 1u));
|
||||
v_ix = ((uint64_t)(v_byte_ix * v_bitmap_len));
|
||||
v_cur_id = wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_block_id + v_byte_ix)));
|
||||
while (v_byte_ix > 0u) {
|
||||
v_byte_ix -= 1u;
|
||||
v_ix -= v_bitmap_len;
|
||||
v_b = wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(((uint64_t)(a_switch_signal + v_ix)) + ((uint64_t)(((uint8_t)(v_cur_id >> 3u)))))));
|
||||
if (((uint8_t)(v_b & ((uint8_t)(((uint8_t)(1u)) << ((uint8_t)(v_cur_id & 7u)))))) != 0u) {
|
||||
if (v_cur_id != wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_block_id + v_byte_ix)))) {
|
||||
v_cur_id = wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_block_id + v_byte_ix)));
|
||||
v_num_blocks += 1u;
|
||||
}
|
||||
}
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(a_block_id + v_byte_ix)), v_cur_id);
|
||||
}
|
||||
return v_num_blocks;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.ld8
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint8_t
|
||||
wuffs_brotli__metablock_builder__ld8(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i) {
|
||||
if (a_i < ((uint64_t)(a_wb.len))) {
|
||||
return a_wb.ptr[a_i];
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.remap_block_ids
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__remap_block_ids(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_block_ids,
|
||||
uint64_t a_length,
|
||||
uint64_t a_num_histograms) {
|
||||
uint32_t v_next_id = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint8_t v_b = 0;
|
||||
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_histograms) {
|
||||
self->private_data.f_new_id[(v_i & 255u)] = 256u;
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_length) {
|
||||
v_b = wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_block_ids + v_i)));
|
||||
if (self->private_data.f_new_id[v_b] == 256u) {
|
||||
self->private_data.f_new_id[v_b] = ((uint16_t)(v_next_id));
|
||||
v_next_id += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_length) {
|
||||
v_b = wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_block_ids + v_i)));
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(a_block_ids + v_i)), ((uint8_t)(self->private_data.f_new_id[v_b])));
|
||||
v_i += 1u;
|
||||
}
|
||||
return ((uint64_t)(v_next_id));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.build_block_histograms
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__build_block_histograms(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_block_ids,
|
||||
uint64_t a_num_histograms,
|
||||
uint64_t a_histograms,
|
||||
uint32_t a_ds) {
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_hs = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_histograms) {
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, ((uint64_t)(a_histograms + ((uint64_t)(v_i * v_hs)))), a_ds);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_length) {
|
||||
wuffs_brotli__metablock_builder__h_add(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_histograms + ((uint64_t)(((uint64_t)(wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_block_ids + v_i))))) * v_hs)))),
|
||||
a_ds,
|
||||
wuffs_brotli__metablock_builder__sym(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
v_i,
|
||||
a_width));
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.cluster_blocks
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__cluster_blocks(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_cat,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint64_t a_num_blocks,
|
||||
uint64_t a_block_ids,
|
||||
uint32_t a_ds) {
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_histogram_symbols = 0;
|
||||
uint64_t v_u32s = 0;
|
||||
uint64_t v_sizes = 0;
|
||||
uint64_t v_new_clusters = 0;
|
||||
uint64_t v_symbols = 0;
|
||||
uint64_t v_remap = 0;
|
||||
uint64_t v_block_lengths = 0;
|
||||
uint64_t v_all_histograms = 0;
|
||||
uint64_t v_all_histograms_size = 0;
|
||||
uint64_t v_cluster_size = 0;
|
||||
uint64_t v_cluster_size_size = 0;
|
||||
uint64_t v_num_clusters = 0;
|
||||
uint64_t v_histograms = 0;
|
||||
uint64_t v_max_num_pairs = 0;
|
||||
uint64_t v_pairs = 0;
|
||||
uint64_t v_pos = 0;
|
||||
uint64_t v_clusters = 0;
|
||||
uint64_t v_num_final_clusters = 0;
|
||||
uint64_t v_new_index = 0;
|
||||
uint64_t v_tmp = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_k = 0;
|
||||
uint64_t v_block_idx = 0;
|
||||
uint64_t v_num_to_combine = 0;
|
||||
uint64_t v_num_new_clusters = 0;
|
||||
uint64_t v_block_length = 0;
|
||||
uint64_t v_nc = 0;
|
||||
uint32_t v_next_index = 0;
|
||||
uint32_t v_best_out = 0;
|
||||
uint64_t v_best_bits = 0;
|
||||
uint64_t v_cur_bits = 0;
|
||||
uint32_t v_cur_length = 0;
|
||||
uint32_t v_max_type = 0;
|
||||
uint32_t v_id = 0;
|
||||
uint32_t v_s = 0;
|
||||
uint64_t v_types_off = 0;
|
||||
uint64_t v_lengths_off = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_histogram_symbols = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_num_blocks * 4u)));
|
||||
v_u32s = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(((uint64_t)(a_num_blocks + 256u)) * 4u)));
|
||||
v_all_histograms = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_num_blocks * v_hs)));
|
||||
v_cluster_size = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_num_blocks * 4u)));
|
||||
v_histograms = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(wuffs_base__u64__min(a_num_blocks, 64u) * v_hs)));
|
||||
v_max_num_pairs = 2048u;
|
||||
v_pairs = wuffs_brotli__metablock_builder__alloc(self, ((v_max_num_pairs + 1u) * 24u));
|
||||
v_tmp = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(2u * v_hs)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_sizes = v_u32s;
|
||||
v_new_clusters = ((uint64_t)(v_u32s + 256u));
|
||||
v_symbols = ((uint64_t)(v_u32s + 512u));
|
||||
v_remap = ((uint64_t)(v_u32s + 768u));
|
||||
v_block_lengths = ((uint64_t)(v_u32s + 1024u));
|
||||
wuffs_brotli__metablock_builder__zero(self, a_wb, v_u32s, ((uint64_t)(((uint64_t)(a_num_blocks + 256u)) * 4u)));
|
||||
v_block_idx = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < a_length) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_block_lengths + ((uint64_t)(v_block_idx * 4u)))), ((uint32_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_block_lengths + ((uint64_t)(v_block_idx * 4u))))) + 1u)));
|
||||
if ((((uint64_t)(v_i + 1u)) == a_length) || (wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_block_ids + v_i))) != wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(((uint64_t)(a_block_ids + v_i)) + 1u))))) {
|
||||
v_block_idx += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_blocks) {
|
||||
v_num_to_combine = ((uint64_t)(a_num_blocks - v_i));
|
||||
v_num_to_combine = wuffs_base__u64__min(v_num_to_combine, 64u);
|
||||
v_j = 0u;
|
||||
while (v_j < v_num_to_combine) {
|
||||
v_block_length = ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_block_lengths + ((uint64_t)(((uint64_t)(v_i + v_j)) * 4u)))))));
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, ((uint64_t)(v_histograms + ((uint64_t)(v_j * v_hs)))), a_ds);
|
||||
v_k = 0u;
|
||||
while (v_k < v_block_length) {
|
||||
wuffs_brotli__metablock_builder__h_add(self,
|
||||
a_wb,
|
||||
((uint64_t)(v_histograms + ((uint64_t)(v_j * v_hs)))),
|
||||
a_ds,
|
||||
wuffs_brotli__metablock_builder__sym(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
v_pos,
|
||||
a_width));
|
||||
v_pos += 1u;
|
||||
v_k += 1u;
|
||||
}
|
||||
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));
|
||||
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);
|
||||
v_j += 1u;
|
||||
}
|
||||
v_num_new_clusters = wuffs_brotli__metablock_builder__histogram_combine(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
v_histograms,
|
||||
v_tmp,
|
||||
v_sizes,
|
||||
v_symbols,
|
||||
v_new_clusters,
|
||||
v_pairs,
|
||||
v_num_to_combine,
|
||||
v_num_to_combine,
|
||||
64u,
|
||||
v_max_num_pairs);
|
||||
v_j = 0u;
|
||||
while (v_j < v_num_new_clusters) {
|
||||
v_nc = ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_new_clusters + ((uint64_t)(v_j * 4u)))))));
|
||||
wuffs_brotli__metablock_builder__h_copy(self,
|
||||
a_wb,
|
||||
((uint64_t)(v_all_histograms + ((uint64_t)(v_all_histograms_size * v_hs)))),
|
||||
((uint64_t)(v_histograms + ((uint64_t)(v_nc * v_hs)))),
|
||||
a_ds);
|
||||
v_all_histograms_size += 1u;
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_cluster_size + ((uint64_t)(v_cluster_size_size * 4u)))), wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_sizes + ((uint64_t)(v_nc * 4u))))));
|
||||
v_cluster_size_size += 1u;
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_remap + ((uint64_t)(v_nc * 4u)))), ((uint32_t)(v_j)));
|
||||
v_j += 1u;
|
||||
}
|
||||
v_j = 0u;
|
||||
while (v_j < v_num_to_combine) {
|
||||
v_s = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_symbols + ((uint64_t)(v_j * 4u)))));
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_histogram_symbols + ((uint64_t)(((uint64_t)(v_i + v_j)) * 4u)))), ((uint32_t)(((uint32_t)(v_num_clusters)) + wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_remap + ((uint64_t)(((uint64_t)(v_s)) * 4u))))))));
|
||||
v_j += 1u;
|
||||
}
|
||||
v_num_clusters += v_num_new_clusters;
|
||||
v_i += 64u;
|
||||
}
|
||||
v_max_num_pairs = ((uint64_t)(64u * v_num_clusters));
|
||||
v_max_num_pairs = wuffs_base__u64__min(v_max_num_pairs, ((uint64_t)((v_num_clusters / 2u) * v_num_clusters)));
|
||||
if (2048u < v_max_num_pairs) {
|
||||
v_pairs = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(((uint64_t)(v_max_num_pairs + 1u)) * 24u)));
|
||||
}
|
||||
v_clusters = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(v_num_clusters * 4u)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < v_num_clusters) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_clusters + ((uint64_t)(v_i * 4u)))), ((uint32_t)(v_i)));
|
||||
v_i += 1u;
|
||||
}
|
||||
v_num_final_clusters = wuffs_brotli__metablock_builder__histogram_combine(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
v_all_histograms,
|
||||
v_tmp,
|
||||
v_cluster_size,
|
||||
v_histogram_symbols,
|
||||
v_clusters,
|
||||
v_pairs,
|
||||
v_num_clusters,
|
||||
a_num_blocks,
|
||||
256u,
|
||||
v_max_num_pairs);
|
||||
v_new_index = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(v_num_clusters * 4u)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
wuffs_brotli__metablock_builder__fill32(self,
|
||||
a_wb,
|
||||
v_new_index,
|
||||
v_num_clusters,
|
||||
4294967295u);
|
||||
v_pos = 0u;
|
||||
v_next_index = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_blocks) {
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, v_tmp, a_ds);
|
||||
v_block_length = ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_block_lengths + ((uint64_t)(v_i * 4u)))))));
|
||||
v_j = 0u;
|
||||
while (v_j < v_block_length) {
|
||||
wuffs_brotli__metablock_builder__h_add(self,
|
||||
a_wb,
|
||||
v_tmp,
|
||||
a_ds,
|
||||
wuffs_brotli__metablock_builder__sym(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
v_pos,
|
||||
a_width));
|
||||
v_pos += 1u;
|
||||
v_j += 1u;
|
||||
}
|
||||
if (v_i == 0u) {
|
||||
v_best_out = wuffs_brotli__metablock_builder__ld32(self, a_wb, v_histogram_symbols);
|
||||
} else {
|
||||
v_best_out = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_histogram_symbols + ((uint64_t)(((uint64_t)(v_i - 1u)) * 4u)))));
|
||||
}
|
||||
v_best_bits = wuffs_brotli__metablock_builder__histogram_bit_cost_distance(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
v_tmp,
|
||||
((uint64_t)(v_all_histograms + ((uint64_t)(((uint64_t)(v_best_out)) * v_hs)))),
|
||||
((uint64_t)(v_tmp + v_hs)));
|
||||
v_j = 0u;
|
||||
while (v_j < v_num_final_clusters) {
|
||||
v_s = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_clusters + ((uint64_t)(v_j * 4u)))));
|
||||
v_cur_bits = wuffs_brotli__metablock_builder__histogram_bit_cost_distance(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
v_tmp,
|
||||
((uint64_t)(v_all_histograms + ((uint64_t)(((uint64_t)(v_s)) * v_hs)))),
|
||||
((uint64_t)(v_tmp + v_hs)));
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_cur_bits, v_best_bits) != 0u) {
|
||||
v_best_bits = v_cur_bits;
|
||||
v_best_out = v_s;
|
||||
}
|
||||
v_j += 1u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_histogram_symbols + ((uint64_t)(v_i * 4u)))), v_best_out);
|
||||
if (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_new_index + ((uint64_t)(((uint64_t)(v_best_out)) * 4u))))) == 4294967295u) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_new_index + ((uint64_t)(((uint64_t)(v_best_out)) * 4u)))), v_next_index);
|
||||
v_next_index += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_types_off = self->private_impl.f_split_types_off[a_cat];
|
||||
v_lengths_off = self->private_impl.f_split_lengths_off[a_cat];
|
||||
v_cur_length = 0u;
|
||||
v_block_idx = 0u;
|
||||
v_max_type = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_blocks) {
|
||||
v_cur_length += wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_block_lengths + ((uint64_t)(v_i * 4u)))));
|
||||
v_s = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_histogram_symbols + ((uint64_t)(v_i * 4u)))));
|
||||
if ((((uint64_t)(v_i + 1u)) == a_num_blocks) || (v_s != wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_histogram_symbols + ((uint64_t)(((uint64_t)(v_i + 1u)) * 4u))))))) {
|
||||
v_id = (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_new_index + ((uint64_t)(((uint64_t)(v_s)) * 4u))))) & 255u);
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(v_types_off + v_block_idx)), ((uint8_t)(v_id)));
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_lengths_off + ((uint64_t)(v_block_idx * 4u)))), v_cur_length);
|
||||
v_max_type = wuffs_base__u32__max(v_max_type, v_id);
|
||||
v_cur_length = 0u;
|
||||
v_block_idx += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
self->private_impl.f_split_num_blocks[a_cat] = v_block_idx;
|
||||
self->private_impl.f_split_num_types[a_cat] = (((uint64_t)(v_max_type)) + 1u);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.split_byte_vector
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__split_byte_vector(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_cat,
|
||||
uint64_t a_data,
|
||||
uint64_t a_length,
|
||||
uint32_t a_width,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_symbols_per_histogram,
|
||||
uint64_t a_max_histograms,
|
||||
uint64_t a_sampling_stride_length,
|
||||
uint64_t a_block_switch_cost) {
|
||||
uint64_t v_num_histograms = 0;
|
||||
uint64_t v_nb = 0;
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_mark = 0;
|
||||
uint64_t v_block_ids = 0;
|
||||
uint64_t v_histograms = 0;
|
||||
uint64_t v_tmp = 0;
|
||||
uint64_t v_bitmaplen = 0;
|
||||
uint64_t v_switch_signal = 0;
|
||||
uint64_t v_iters = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_num_blocks = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_num_histograms = ((uint64_t)((a_length / wuffs_base__u64__max(a_symbols_per_histogram, 1u)) + 1u));
|
||||
if (v_num_histograms > a_max_histograms) {
|
||||
v_num_histograms = a_max_histograms;
|
||||
}
|
||||
if (a_length == 0u) {
|
||||
self->private_impl.f_split_num_types[a_cat] = 1u;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (a_length < 128u) {
|
||||
v_nb = self->private_impl.f_split_num_blocks[a_cat];
|
||||
self->private_impl.f_split_num_types[a_cat] = 1u;
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(self->private_impl.f_split_types_off[a_cat] + v_nb)), 0u);
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(self->private_impl.f_split_lengths_off[a_cat] + ((uint64_t)(v_nb * 4u)))), ((uint32_t)(a_length)));
|
||||
self->private_impl.f_split_num_blocks[a_cat] = ((uint64_t)(v_nb + 1u));
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_block_ids = wuffs_brotli__metablock_builder__alloc(self, a_length);
|
||||
v_mark = self->private_impl.f_wb_top;
|
||||
v_histograms = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(((uint64_t)(v_num_histograms + 1u)) * v_hs)));
|
||||
v_tmp = ((uint64_t)(v_histograms + ((uint64_t)(v_num_histograms * v_hs))));
|
||||
v_bitmaplen = (((uint64_t)(v_num_histograms + 7u)) >> 3u);
|
||||
v_switch_signal = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_length * v_bitmaplen)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
wuffs_brotli__metablock_builder__initial_entropy_codes(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
a_length,
|
||||
a_width,
|
||||
a_sampling_stride_length,
|
||||
v_num_histograms,
|
||||
v_histograms,
|
||||
a_ds);
|
||||
wuffs_brotli__metablock_builder__refine_entropy_codes(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
a_length,
|
||||
a_width,
|
||||
a_sampling_stride_length,
|
||||
v_num_histograms,
|
||||
v_histograms,
|
||||
v_tmp,
|
||||
a_ds);
|
||||
v_iters = 3u;
|
||||
if (self->private_impl.f_quality >= 11u) {
|
||||
v_iters = 10u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < v_iters) {
|
||||
v_num_blocks = wuffs_brotli__metablock_builder__find_blocks(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
a_length,
|
||||
a_width,
|
||||
a_block_switch_cost,
|
||||
v_num_histograms,
|
||||
v_histograms,
|
||||
v_switch_signal,
|
||||
v_block_ids,
|
||||
a_ds);
|
||||
v_num_histograms = wuffs_brotli__metablock_builder__remap_block_ids(self,
|
||||
a_wb,
|
||||
v_block_ids,
|
||||
a_length,
|
||||
v_num_histograms);
|
||||
wuffs_brotli__metablock_builder__build_block_histograms(self,
|
||||
a_wb,
|
||||
a_data,
|
||||
a_length,
|
||||
a_width,
|
||||
v_block_ids,
|
||||
v_num_histograms,
|
||||
v_histograms,
|
||||
a_ds);
|
||||
v_i += 1u;
|
||||
}
|
||||
self->private_impl.f_wb_top = v_mark;
|
||||
wuffs_brotli__metablock_builder__cluster_blocks(self,
|
||||
a_wb,
|
||||
a_cat,
|
||||
a_data,
|
||||
a_length,
|
||||
a_width,
|
||||
v_num_blocks,
|
||||
v_block_ids,
|
||||
a_ds);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.get_quirk
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -77911,6 +79717,634 @@ wuffs_brotli__encoder__transform_io(
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_pair_is_less
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__metablock_builder__histogram_pair_is_less(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_p1_cost_diff,
|
||||
uint32_t a_p1_idx1,
|
||||
uint32_t a_p1_idx2,
|
||||
uint64_t a_p2_cost_diff,
|
||||
uint32_t a_p2_idx1,
|
||||
uint32_t a_p2_idx2) {
|
||||
if (wuffs_softfloat__arith__f64_ne(&self->private_data.f_sf, a_p1_cost_diff, a_p2_cost_diff) != 0u) {
|
||||
return (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, a_p1_cost_diff, a_p2_cost_diff) != 0u);
|
||||
}
|
||||
return (((uint32_t)(a_p1_idx2 - a_p1_idx1)) > ((uint32_t)(a_p2_idx2 - a_p2_idx1)));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.cluster_cost_diff
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__cluster_cost_diff(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_size_a,
|
||||
uint64_t a_size_b) {
|
||||
uint64_t v_size_c = 0;
|
||||
|
||||
v_size_c = ((uint64_t)(a_size_a + a_size_b));
|
||||
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_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_size_a), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_size_a)), wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_size_b), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_size_b))), wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_size_c), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_size_c)));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.pair_copy
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__pair_copy(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_dst,
|
||||
uint64_t a_src) {
|
||||
wuffs_brotli__metablock_builder__st64(self, a_wb, a_dst, wuffs_brotli__metablock_builder__ld64(self, a_wb, a_src));
|
||||
wuffs_brotli__metablock_builder__st64(self, a_wb, ((uint64_t)(a_dst + 8u)), wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_src + 8u))));
|
||||
wuffs_brotli__metablock_builder__st64(self, a_wb, ((uint64_t)(a_dst + 16u)), wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_src + 16u))));
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.pair_set
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__pair_set(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_dst,
|
||||
uint32_t a_idx1,
|
||||
uint32_t a_idx2,
|
||||
uint64_t a_cost_combo,
|
||||
uint64_t a_cost_diff) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, a_dst, a_idx1);
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_dst + 4u)), a_idx2);
|
||||
wuffs_brotli__metablock_builder__st64(self, a_wb, ((uint64_t)(a_dst + 8u)), a_cost_combo);
|
||||
wuffs_brotli__metablock_builder__st64(self, a_wb, ((uint64_t)(a_dst + 16u)), a_cost_diff);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.compare_and_push_to_queue
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__compare_and_push_to_queue(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_out,
|
||||
uint64_t a_tmp,
|
||||
uint64_t a_cluster_size,
|
||||
uint32_t a_idx1,
|
||||
uint32_t a_idx2,
|
||||
uint64_t a_max_num_pairs,
|
||||
uint64_t a_pairs) {
|
||||
uint64_t v_hs = 0;
|
||||
bool v_is_good_pair = false;
|
||||
uint32_t v_idx1 = 0;
|
||||
uint32_t v_idx2 = 0;
|
||||
uint32_t v_t = 0;
|
||||
uint64_t v_cost_diff = 0;
|
||||
uint64_t v_cost_combo = 0;
|
||||
uint64_t v_threshold = 0;
|
||||
uint64_t v_h1 = 0;
|
||||
uint64_t v_h2 = 0;
|
||||
uint64_t v_c = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
if (a_idx1 == a_idx2) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_idx1 = a_idx1;
|
||||
v_idx2 = a_idx2;
|
||||
if (v_idx2 < v_idx1) {
|
||||
v_t = v_idx2;
|
||||
v_idx2 = v_idx1;
|
||||
v_idx1 = v_t;
|
||||
}
|
||||
v_h1 = ((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_idx1)) * v_hs))));
|
||||
v_h2 = ((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_idx2)) * v_hs))));
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4602678819172646912u, wuffs_brotli__metablock_builder__cluster_cost_diff(self, ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_idx1)) * 4u))))))), ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_idx2)) * 4u)))))))));
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_cost_diff, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h1, a_ds));
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_cost_diff, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h2, a_ds));
|
||||
if (wuffs_brotli__metablock_builder__h_total(self, a_wb, v_h1, a_ds) == 0u) {
|
||||
v_cost_combo = wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h2, a_ds);
|
||||
v_is_good_pair = true;
|
||||
} else if (wuffs_brotli__metablock_builder__h_total(self, a_wb, v_h2, a_ds) == 0u) {
|
||||
v_cost_combo = wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h1, a_ds);
|
||||
v_is_good_pair = true;
|
||||
} else {
|
||||
if (self->private_impl.f_num_pairs == 0u) {
|
||||
v_threshold = 6088095589093318446u;
|
||||
} else {
|
||||
v_threshold = wuffs_softfloat__arith__f64_max(&self->private_data.f_sf, 0u, wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 16u))));
|
||||
}
|
||||
wuffs_brotli__metablock_builder__h_copy(self,
|
||||
a_wb,
|
||||
a_tmp,
|
||||
v_h1,
|
||||
a_ds);
|
||||
wuffs_brotli__metablock_builder__h_add_histogram(self,
|
||||
a_wb,
|
||||
a_tmp,
|
||||
v_h2,
|
||||
a_ds);
|
||||
v_c = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, a_ds);
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_c, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_threshold, v_cost_diff)) != 0u) {
|
||||
v_cost_combo = v_c;
|
||||
v_is_good_pair = true;
|
||||
}
|
||||
}
|
||||
if (v_is_good_pair) {
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_cost_diff, v_cost_combo);
|
||||
if ((self->private_impl.f_num_pairs > 0u) && wuffs_brotli__metablock_builder__histogram_pair_is_less(self,
|
||||
wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 16u))),
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, a_pairs),
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_pairs + 4u))),
|
||||
v_cost_diff,
|
||||
v_idx1,
|
||||
v_idx2)) {
|
||||
if (self->private_impl.f_num_pairs < a_max_num_pairs) {
|
||||
wuffs_brotli__metablock_builder__pair_copy(self, a_wb, ((uint64_t)(a_pairs + ((uint64_t)(self->private_impl.f_num_pairs * 24u)))), a_pairs);
|
||||
self->private_impl.f_num_pairs += 1u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__pair_set(self,
|
||||
a_wb,
|
||||
a_pairs,
|
||||
v_idx1,
|
||||
v_idx2,
|
||||
v_cost_combo,
|
||||
v_cost_diff);
|
||||
} else if (self->private_impl.f_num_pairs < a_max_num_pairs) {
|
||||
wuffs_brotli__metablock_builder__pair_set(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_pairs + ((uint64_t)(self->private_impl.f_num_pairs * 24u)))),
|
||||
v_idx1,
|
||||
v_idx2,
|
||||
v_cost_combo,
|
||||
v_cost_diff);
|
||||
self->private_impl.f_num_pairs += 1u;
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_combine
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__histogram_combine(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_out,
|
||||
uint64_t a_tmp,
|
||||
uint64_t a_cluster_size,
|
||||
uint64_t a_symbols,
|
||||
uint64_t a_clusters,
|
||||
uint64_t a_pairs,
|
||||
uint64_t a_num_clusters,
|
||||
uint64_t a_symbols_size,
|
||||
uint64_t a_max_clusters,
|
||||
uint64_t a_max_num_pairs) {
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_cost_diff_threshold = 0;
|
||||
uint64_t v_min_cluster_size = 0;
|
||||
uint64_t v_num_clusters = 0;
|
||||
uint64_t v_idx1 = 0;
|
||||
uint64_t v_idx2 = 0;
|
||||
uint32_t v_best_idx1 = 0;
|
||||
uint32_t v_best_idx2 = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_copy_to_idx = 0;
|
||||
uint64_t v_p = 0;
|
||||
uint32_t v_p_idx1 = 0;
|
||||
uint32_t v_p_idx2 = 0;
|
||||
uint64_t v_p_cost_combo = 0;
|
||||
uint64_t v_p_cost_diff = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_cost_diff_threshold = 0u;
|
||||
v_min_cluster_size = 1u;
|
||||
v_num_clusters = a_num_clusters;
|
||||
self->private_impl.f_num_pairs = 0u;
|
||||
v_idx1 = 0u;
|
||||
while (v_idx1 < v_num_clusters) {
|
||||
v_idx2 = ((uint64_t)(v_idx1 + 1u));
|
||||
while (v_idx2 < v_num_clusters) {
|
||||
wuffs_brotli__metablock_builder__compare_and_push_to_queue(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
a_out,
|
||||
a_tmp,
|
||||
a_cluster_size,
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(v_idx1 * 4u))))),
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(v_idx2 * 4u))))),
|
||||
a_max_num_pairs,
|
||||
a_pairs);
|
||||
v_idx2 += 1u;
|
||||
}
|
||||
v_idx1 += 1u;
|
||||
}
|
||||
while (v_num_clusters > v_min_cluster_size) {
|
||||
if (wuffs_softfloat__arith__f64_ge(&self->private_data.f_sf, wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 16u))), v_cost_diff_threshold) != 0u) {
|
||||
v_cost_diff_threshold = 6088095589093318446u;
|
||||
v_min_cluster_size = a_max_clusters;
|
||||
continue;
|
||||
}
|
||||
v_best_idx1 = wuffs_brotli__metablock_builder__ld32(self, a_wb, a_pairs);
|
||||
v_best_idx2 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_pairs + 4u)));
|
||||
wuffs_brotli__metablock_builder__h_add_histogram(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_best_idx1)) * v_hs)))),
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_best_idx2)) * v_hs)))),
|
||||
a_ds);
|
||||
wuffs_brotli__metablock_builder__h_set_bit_cost(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_best_idx1)) * v_hs)))),
|
||||
a_ds,
|
||||
wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 8u))));
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_best_idx1)) * 4u)))), ((uint32_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_best_idx1)) * 4u))))) + wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_best_idx2)) * 4u))))))));
|
||||
v_i = 0u;
|
||||
while (v_i < a_symbols_size) {
|
||||
if (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(v_i * 4u))))) == v_best_idx2) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(v_i * 4u)))), v_best_idx1);
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < v_num_clusters) {
|
||||
if (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(v_i * 4u))))) == v_best_idx2) {
|
||||
v_j = v_i;
|
||||
while (((uint64_t)(v_j + 1u)) < v_num_clusters) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(v_j * 4u)))), wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(((uint64_t)(v_j + 1u)) * 4u))))));
|
||||
v_j += 1u;
|
||||
}
|
||||
break;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_num_clusters -= 1u;
|
||||
v_copy_to_idx = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_num_pairs) {
|
||||
v_p = ((uint64_t)(a_pairs + ((uint64_t)(v_i * 24u))));
|
||||
v_p_idx1 = wuffs_brotli__metablock_builder__ld32(self, a_wb, v_p);
|
||||
v_p_idx2 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_p + 4u)));
|
||||
v_p_cost_combo = wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(v_p + 8u)));
|
||||
v_p_cost_diff = wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(v_p + 16u)));
|
||||
if ((v_p_idx1 == v_best_idx1) ||
|
||||
(v_p_idx2 == v_best_idx1) ||
|
||||
(v_p_idx1 == v_best_idx2) ||
|
||||
(v_p_idx2 == v_best_idx2)) {
|
||||
v_i += 1u;
|
||||
continue;
|
||||
}
|
||||
if (wuffs_brotli__metablock_builder__histogram_pair_is_less(self,
|
||||
wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 16u))),
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, a_pairs),
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_pairs + 4u))),
|
||||
v_p_cost_diff,
|
||||
v_p_idx1,
|
||||
v_p_idx2)) {
|
||||
wuffs_brotli__metablock_builder__pair_copy(self, a_wb, ((uint64_t)(a_pairs + ((uint64_t)(v_copy_to_idx * 24u)))), a_pairs);
|
||||
wuffs_brotli__metablock_builder__pair_set(self,
|
||||
a_wb,
|
||||
a_pairs,
|
||||
v_p_idx1,
|
||||
v_p_idx2,
|
||||
v_p_cost_combo,
|
||||
v_p_cost_diff);
|
||||
} else {
|
||||
wuffs_brotli__metablock_builder__pair_set(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_pairs + ((uint64_t)(v_copy_to_idx * 24u)))),
|
||||
v_p_idx1,
|
||||
v_p_idx2,
|
||||
v_p_cost_combo,
|
||||
v_p_cost_diff);
|
||||
}
|
||||
v_copy_to_idx += 1u;
|
||||
v_i += 1u;
|
||||
}
|
||||
self->private_impl.f_num_pairs = v_copy_to_idx;
|
||||
v_i = 0u;
|
||||
while (v_i < v_num_clusters) {
|
||||
wuffs_brotli__metablock_builder__compare_and_push_to_queue(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
a_out,
|
||||
a_tmp,
|
||||
a_cluster_size,
|
||||
v_best_idx1,
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(v_i * 4u))))),
|
||||
a_max_num_pairs,
|
||||
a_pairs);
|
||||
v_i += 1u;
|
||||
}
|
||||
}
|
||||
return v_num_clusters;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_bit_cost_distance
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__histogram_bit_cost_distance(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_histogram,
|
||||
uint64_t a_candidate,
|
||||
uint64_t a_tmp) {
|
||||
if (wuffs_brotli__metablock_builder__h_total(self, a_wb, a_histogram, a_ds) == 0u) {
|
||||
return 0u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__h_copy(self,
|
||||
a_wb,
|
||||
a_tmp,
|
||||
a_histogram,
|
||||
a_ds);
|
||||
wuffs_brotli__metablock_builder__h_add_histogram(self,
|
||||
a_wb,
|
||||
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));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_remap
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__histogram_remap(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_in_histos,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_clusters,
|
||||
uint64_t a_num_clusters,
|
||||
uint64_t a_out,
|
||||
uint64_t a_tmp,
|
||||
uint64_t a_symbols) {
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint32_t v_best_out = 0;
|
||||
uint64_t v_best_bits = 0;
|
||||
uint64_t v_cur_bits = 0;
|
||||
uint32_t v_c = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_i = 0u;
|
||||
while (v_i < a_in_size) {
|
||||
if (v_i == 0u) {
|
||||
v_best_out = wuffs_brotli__metablock_builder__ld32(self, a_wb, a_symbols);
|
||||
} else {
|
||||
v_best_out = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(((uint64_t)(v_i - 1u)) * 4u)))));
|
||||
}
|
||||
v_best_bits = wuffs_brotli__metablock_builder__histogram_bit_cost_distance(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
((uint64_t)(a_in_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_best_out)) * v_hs)))),
|
||||
a_tmp);
|
||||
v_j = 0u;
|
||||
while (v_j < a_num_clusters) {
|
||||
v_c = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(v_j * 4u)))));
|
||||
v_cur_bits = wuffs_brotli__metablock_builder__histogram_bit_cost_distance(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
((uint64_t)(a_in_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_c)) * v_hs)))),
|
||||
a_tmp);
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_cur_bits, v_best_bits) != 0u) {
|
||||
v_best_bits = v_cur_bits;
|
||||
v_best_out = v_c;
|
||||
}
|
||||
v_j += 1u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(v_i * 4u)))), v_best_out);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_clusters) {
|
||||
v_c = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_clusters + ((uint64_t)(v_i * 4u)))));
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, ((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_c)) * v_hs)))), a_ds);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_in_size) {
|
||||
v_c = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(v_i * 4u)))));
|
||||
wuffs_brotli__metablock_builder__h_add_histogram(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_c)) * v_hs)))),
|
||||
((uint64_t)(a_in_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
a_ds);
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_reindex
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__histogram_reindex(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_out,
|
||||
uint64_t a_symbols,
|
||||
uint64_t a_length) {
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_mark = 0;
|
||||
uint64_t v_new_index = 0;
|
||||
uint32_t v_next_index = 0;
|
||||
uint64_t v_tmp = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint32_t v_s = 0;
|
||||
uint32_t v_ni = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_mark = self->private_impl.f_wb_top;
|
||||
v_new_index = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_length * 4u)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return 0u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__fill32(self,
|
||||
a_wb,
|
||||
v_new_index,
|
||||
a_length,
|
||||
4294967295u);
|
||||
v_next_index = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < a_length) {
|
||||
v_s = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(v_i * 4u)))));
|
||||
if (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_new_index + ((uint64_t)(((uint64_t)(v_s)) * 4u))))) == 4294967295u) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_new_index + ((uint64_t)(((uint64_t)(v_s)) * 4u)))), v_next_index);
|
||||
v_next_index += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_tmp = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(((uint64_t)(v_next_index)) * v_hs)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return 0u;
|
||||
}
|
||||
v_next_index = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < a_length) {
|
||||
v_s = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(v_i * 4u)))));
|
||||
v_ni = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(v_new_index + ((uint64_t)(((uint64_t)(v_s)) * 4u)))));
|
||||
if (v_ni == v_next_index) {
|
||||
wuffs_brotli__metablock_builder__h_copy(self,
|
||||
a_wb,
|
||||
((uint64_t)(v_tmp + ((uint64_t)(((uint64_t)(v_next_index)) * v_hs)))),
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_s)) * v_hs)))),
|
||||
a_ds);
|
||||
v_next_index += 1u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_symbols + ((uint64_t)(v_i * 4u)))), v_ni);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < ((uint64_t)(v_next_index))) {
|
||||
wuffs_brotli__metablock_builder__h_copy(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_out + ((uint64_t)(v_i * v_hs)))),
|
||||
((uint64_t)(v_tmp + ((uint64_t)(v_i * v_hs)))),
|
||||
a_ds);
|
||||
v_i += 1u;
|
||||
}
|
||||
self->private_impl.f_wb_top = v_mark;
|
||||
return ((uint64_t)(v_next_index));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.cluster_histograms
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__cluster_histograms(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_in_histos,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_max_histograms,
|
||||
uint64_t a_out_histos,
|
||||
uint64_t a_histogram_symbols) {
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_mark = 0;
|
||||
uint64_t v_cluster_size = 0;
|
||||
uint64_t v_clusters = 0;
|
||||
uint64_t v_num_clusters = 0;
|
||||
uint64_t v_pairs_capacity = 0;
|
||||
uint64_t v_pairs = 0;
|
||||
uint64_t v_tmp = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_num_to_combine = 0;
|
||||
uint64_t v_num_new_clusters = 0;
|
||||
uint64_t v_max_num_pairs = 0;
|
||||
uint64_t v_out_size = 0;
|
||||
|
||||
v_hs = wuffs_brotli__metablock_builder__hsize(self, a_ds);
|
||||
v_mark = self->private_impl.f_wb_top;
|
||||
v_cluster_size = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_in_size * 4u)));
|
||||
v_clusters = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(a_in_size * 4u)));
|
||||
v_pairs_capacity = 2048u;
|
||||
v_pairs = wuffs_brotli__metablock_builder__alloc(self, ((v_pairs_capacity + 1u) * 24u));
|
||||
v_tmp = wuffs_brotli__metablock_builder__alloc(self, v_hs);
|
||||
if (self->private_impl.f_oom) {
|
||||
return 0u;
|
||||
}
|
||||
wuffs_brotli__metablock_builder__fill32(self,
|
||||
a_wb,
|
||||
v_cluster_size,
|
||||
a_in_size,
|
||||
1u);
|
||||
v_i = 0u;
|
||||
while (v_i < a_in_size) {
|
||||
wuffs_brotli__metablock_builder__h_copy(self,
|
||||
a_wb,
|
||||
((uint64_t)(a_out_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
((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));
|
||||
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;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_in_size) {
|
||||
v_num_to_combine = ((uint64_t)(a_in_size - v_i));
|
||||
v_num_to_combine = wuffs_base__u64__min(v_num_to_combine, 64u);
|
||||
v_j = 0u;
|
||||
while (v_j < v_num_to_combine) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(v_clusters + ((uint64_t)(((uint64_t)(v_num_clusters + v_j)) * 4u)))), ((uint32_t)(((uint64_t)(v_i + v_j)))));
|
||||
v_j += 1u;
|
||||
}
|
||||
v_num_new_clusters = wuffs_brotli__metablock_builder__histogram_combine(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
a_out_histos,
|
||||
v_tmp,
|
||||
v_cluster_size,
|
||||
((uint64_t)(a_histogram_symbols + ((uint64_t)(v_i * 4u)))),
|
||||
((uint64_t)(v_clusters + ((uint64_t)(v_num_clusters * 4u)))),
|
||||
v_pairs,
|
||||
v_num_to_combine,
|
||||
v_num_to_combine,
|
||||
a_max_histograms,
|
||||
v_pairs_capacity);
|
||||
v_num_clusters += v_num_new_clusters;
|
||||
v_i += 64u;
|
||||
}
|
||||
v_max_num_pairs = ((uint64_t)(64u * v_num_clusters));
|
||||
v_max_num_pairs = wuffs_base__u64__min(v_max_num_pairs, ((uint64_t)((v_num_clusters / 2u) * v_num_clusters)));
|
||||
if (v_pairs_capacity < ((uint64_t)(v_max_num_pairs + 1u))) {
|
||||
v_pairs = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(((uint64_t)(v_max_num_pairs + 1u)) * 24u)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return 0u;
|
||||
}
|
||||
}
|
||||
v_num_clusters = wuffs_brotli__metablock_builder__histogram_combine(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
a_out_histos,
|
||||
v_tmp,
|
||||
v_cluster_size,
|
||||
a_histogram_symbols,
|
||||
v_clusters,
|
||||
v_pairs,
|
||||
v_num_clusters,
|
||||
a_in_size,
|
||||
a_max_histograms,
|
||||
v_max_num_pairs);
|
||||
wuffs_brotli__metablock_builder__histogram_remap(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
a_in_histos,
|
||||
a_in_size,
|
||||
v_clusters,
|
||||
v_num_clusters,
|
||||
a_out_histos,
|
||||
v_tmp,
|
||||
a_histogram_symbols);
|
||||
v_out_size = wuffs_brotli__metablock_builder__histogram_reindex(self,
|
||||
a_wb,
|
||||
a_ds,
|
||||
a_out_histos,
|
||||
a_histogram_symbols,
|
||||
a_in_size);
|
||||
self->private_impl.f_wb_top = v_mark;
|
||||
return v_out_size;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.computed_lgblock
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -83028,6 +85462,1170 @@ wuffs_brotli__bit_cost__command_distance_context(
|
||||
return 3u;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.ld16
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__ld16(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i) {
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
|
||||
if (a_i < ((uint64_t)(a_wb.len))) {
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_wb, a_i);
|
||||
if (((uint64_t)(v_q.len)) >= 2u) {
|
||||
return ((uint32_t)(wuffs_base__peek_u16le__no_bounds_check(v_q.ptr)));
|
||||
}
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.ld32
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__ld32(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i) {
|
||||
return wuffs_brotli__bit_cost__ld32(&self->private_data.f_bc, a_wb, a_i);
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.ld64
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__ld64(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i) {
|
||||
return wuffs_brotli__bit_cost__ld64(&self->private_data.f_bc, a_wb, a_i);
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.st8
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st8(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint8_t a_v) {
|
||||
if (a_i < ((uint64_t)(a_wb.len))) {
|
||||
a_wb.ptr[a_i] = a_v;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.st16
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st16(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint32_t a_v) {
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
|
||||
if (a_i < ((uint64_t)(a_wb.len))) {
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_wb, a_i);
|
||||
if (((uint64_t)(v_q.len)) >= 2u) {
|
||||
wuffs_base__poke_u16le__no_bounds_check(v_q.ptr, ((uint16_t)(a_v)));
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.st32
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st32(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint32_t a_v) {
|
||||
wuffs_brotli__bit_cost__st32(&self->private_data.f_bc, a_wb, a_i, a_v);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.st64
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__st64(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint64_t a_v) {
|
||||
wuffs_brotli__bit_cost__st64(&self->private_data.f_bc, a_wb, a_i, a_v);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.fill32
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__fill32(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint64_t a_n,
|
||||
uint32_t a_v) {
|
||||
uint64_t v_j = 0;
|
||||
|
||||
v_j = 0u;
|
||||
while (v_j < a_n) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_i + ((uint64_t)(v_j * 4u)))), a_v);
|
||||
v_j += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.zero
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__zero(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i,
|
||||
uint64_t a_n) {
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_hi = 0;
|
||||
|
||||
if (a_i <= ((uint64_t)(a_wb.len))) {
|
||||
v_j = ((uint64_t)(((uint64_t)(a_wb.len)) - a_i));
|
||||
if (a_n < v_j) {
|
||||
v_j = a_n;
|
||||
}
|
||||
v_hi = ((uint64_t)(a_i + v_j));
|
||||
if ((a_i <= v_hi) && (v_hi <= ((uint64_t)(a_wb.len)))) {
|
||||
wuffs_private_impl__bulk_memset(a_wb.ptr + a_i, (v_hi - a_i), 0u);
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.sym
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__sym(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_base,
|
||||
uint64_t a_i,
|
||||
uint32_t a_width) {
|
||||
uint64_t v_j = 0;
|
||||
|
||||
if (a_width == 1u) {
|
||||
v_j = ((uint64_t)(a_base + a_i));
|
||||
if (v_j < ((uint64_t)(a_wb.len))) {
|
||||
return ((uint64_t)(a_wb.ptr[v_j]));
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return ((uint64_t)(wuffs_brotli__metablock_builder__ld16(self, a_wb, ((uint64_t)(a_base + ((uint64_t)(a_i * 2u)))))));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.alloc
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__alloc(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_n) {
|
||||
uint64_t v_r = 0;
|
||||
|
||||
if (self->private_impl.f_oom || (self->private_impl.f_wb_top > self->private_impl.f_wb_len) || (a_n > ((uint64_t)(self->private_impl.f_wb_len - self->private_impl.f_wb_top)))) {
|
||||
self->private_impl.f_oom = true;
|
||||
return self->private_impl.f_wb_len;
|
||||
}
|
||||
v_r = self->private_impl.f_wb_top;
|
||||
self->private_impl.f_wb_top = ((uint64_t)(v_r + a_n));
|
||||
if (self->private_impl.f_wb_high < self->private_impl.f_wb_top) {
|
||||
self->private_impl.f_wb_high = self->private_impl.f_wb_top;
|
||||
}
|
||||
return v_r;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.hsize
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__hsize(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_ds) {
|
||||
return ((((uint64_t)(a_ds)) * 4u) + 16u);
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_clear
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_clear(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds) {
|
||||
if (a_h <= ((uint64_t)(a_wb.len))) {
|
||||
wuffs_brotli__bit_cost__histogram_clear(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_h), a_ds);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_add
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_add(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_val) {
|
||||
if (a_h <= ((uint64_t)(a_wb.len))) {
|
||||
wuffs_brotli__bit_cost__histogram_add(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_h), a_ds, a_val);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_add_histogram
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_add_histogram(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint64_t a_v,
|
||||
uint32_t a_ds) {
|
||||
if ((a_h <= ((uint64_t)(a_wb.len))) && (a_v <= ((uint64_t)(a_wb.len)))) {
|
||||
wuffs_brotli__bit_cost__histogram_add_histogram(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_h), wuffs_base__slice_u8__subslice_i(a_wb, a_v), a_ds);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_copy
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_copy(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint64_t a_v,
|
||||
uint32_t a_ds) {
|
||||
if ((a_h <= ((uint64_t)(a_wb.len))) && (a_v <= ((uint64_t)(a_wb.len)))) {
|
||||
wuffs_brotli__bit_cost__histogram_copy(&self->private_data.f_bc, wuffs_base__slice_u8__subslice_i(a_wb, a_h), wuffs_base__slice_u8__subslice_i(a_wb, a_v), a_ds);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_total
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_total(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds) {
|
||||
return wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_h + (((uint64_t)(a_ds)) * 4u))));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_bit_cost
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_bit_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds) {
|
||||
return wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_h + ((((uint64_t)(a_ds)) * 4u) + 8u))));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_set_bit_cost
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__h_set_bit_cost(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds,
|
||||
uint64_t a_bit_cost) {
|
||||
wuffs_brotli__metablock_builder__st64(self, a_wb, ((uint64_t)(a_h + ((((uint64_t)(a_ds)) * 4u) + 8u))), a_bit_cost);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.h_population_cost
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__h_population_cost(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_h,
|
||||
uint32_t a_ds) {
|
||||
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);
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.log2_floor_non_zero
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__log2_floor_non_zero(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_n) {
|
||||
uint32_t v_x = 0;
|
||||
uint32_t v_r = 0;
|
||||
|
||||
v_x = ((uint32_t)(a_n));
|
||||
while (v_x > 1u) {
|
||||
v_x >>= 1u;
|
||||
if (v_r < 31u) {
|
||||
v_r += 1u;
|
||||
}
|
||||
}
|
||||
return v_r;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.prefix_encode_copy_distance
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__prefix_encode_copy_distance(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_distance_code,
|
||||
uint64_t a_num_direct_codes,
|
||||
uint32_t a_postfix_bits) {
|
||||
uint64_t v_dist = 0;
|
||||
uint64_t v_bucket = 0;
|
||||
uint64_t v_postfix_mask = 0;
|
||||
uint64_t v_postfix = 0;
|
||||
uint64_t v_prefix = 0;
|
||||
uint64_t v_offset = 0;
|
||||
uint64_t v_nbits = 0;
|
||||
uint64_t v_code = 0;
|
||||
uint64_t v_extra = 0;
|
||||
|
||||
if (a_distance_code < ((uint64_t)(16u + a_num_direct_codes))) {
|
||||
return (a_distance_code & 65535u);
|
||||
}
|
||||
v_dist = ((uint64_t)((((uint64_t)(1u)) << ((a_postfix_bits & 31u) + 2u)) + ((uint64_t)(((uint64_t)(a_distance_code - 16u)) - a_num_direct_codes))));
|
||||
v_bucket = ((uint64_t)(((uint64_t)(wuffs_brotli__metablock_builder__log2_floor_non_zero(self, v_dist))) - 1u));
|
||||
v_postfix_mask = ((uint64_t)((((uint64_t)(1u)) << (a_postfix_bits & 31u)) - 1u));
|
||||
v_postfix = (v_dist & v_postfix_mask);
|
||||
v_prefix = ((v_dist >> (v_bucket & 63u)) & 1u);
|
||||
v_offset = ((uint64_t)(((uint64_t)(2u + v_prefix)) << (v_bucket & 63u)));
|
||||
v_nbits = ((uint64_t)(v_bucket - ((uint64_t)(a_postfix_bits))));
|
||||
v_code = (((uint64_t)(v_nbits << 10u)) | ((uint64_t)(((uint64_t)(((uint64_t)(16u + a_num_direct_codes)) + ((uint64_t)(((uint64_t)(((uint64_t)(2u * ((uint64_t)(v_nbits - 1u)))) + v_prefix)) << (a_postfix_bits & 63u))))) + v_postfix)));
|
||||
v_extra = (((uint64_t)(v_dist - v_offset)) >> (a_postfix_bits & 63u));
|
||||
return ((v_code & 65535u) | ((v_extra & 4294967295u) << 32u));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.command_restore_distance_code
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__metablock_builder__command_restore_distance_code(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_c,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect) {
|
||||
uint32_t v_dist_prefix = 0;
|
||||
uint32_t v_dcode = 0;
|
||||
uint32_t v_nbits = 0;
|
||||
uint32_t v_extra = 0;
|
||||
uint32_t v_postfix_mask = 0;
|
||||
uint32_t v_hcode = 0;
|
||||
uint32_t v_lcode = 0;
|
||||
uint32_t v_offset = 0;
|
||||
|
||||
v_dist_prefix = wuffs_brotli__metablock_builder__ld16(self, a_wb, ((uint64_t)(a_c + 14u)));
|
||||
if ((v_dist_prefix & 1023u) < ((uint32_t)(16u + a_ndirect))) {
|
||||
return (v_dist_prefix & 1023u);
|
||||
}
|
||||
v_dcode = (v_dist_prefix & 1023u);
|
||||
v_nbits = (v_dist_prefix >> 10u);
|
||||
v_extra = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_c + 8u)));
|
||||
v_postfix_mask = ((uint32_t)((((uint32_t)(1u)) << (a_npostfix & 31u)) - 1u));
|
||||
v_hcode = (((uint32_t)(((uint32_t)(v_dcode - a_ndirect)) - 16u)) >> (a_npostfix & 31u));
|
||||
v_lcode = (((uint32_t)(((uint32_t)(v_dcode - a_ndirect)) - 16u)) & v_postfix_mask);
|
||||
v_offset = ((uint32_t)(((uint32_t)(((uint32_t)(2u + (v_hcode & 1u))) << (v_nbits & 31u))) - 4u));
|
||||
return ((uint32_t)(((uint32_t)(((uint32_t)(((uint32_t)(((uint32_t)(v_offset + v_extra)) << (a_npostfix & 31u))) + v_lcode)) + a_ndirect)) + 16u));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.calculate_distance_code_limit
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__calculate_distance_code_limit(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_max_distance,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect) {
|
||||
uint32_t v_forbidden_distance = 0;
|
||||
uint32_t v_offset = 0;
|
||||
uint32_t v_ndistbits = 0;
|
||||
uint32_t v_tmp = 0;
|
||||
uint32_t v_half = 0;
|
||||
uint32_t v_group = 0;
|
||||
uint32_t v_postfix = 0;
|
||||
uint32_t v_extra = 0;
|
||||
uint32_t v_start = 0;
|
||||
uint32_t v_mas = 0;
|
||||
uint32_t v_md = 0;
|
||||
|
||||
if (a_max_distance <= a_ndirect) {
|
||||
v_mas = ((uint32_t)(a_max_distance + 16u));
|
||||
v_md = a_max_distance;
|
||||
return (((uint64_t)(v_mas)) | (((uint64_t)(v_md)) << 32u));
|
||||
}
|
||||
v_forbidden_distance = ((uint32_t)(a_max_distance + 1u));
|
||||
v_offset = ((uint32_t)(((uint32_t)(v_forbidden_distance - a_ndirect)) - 1u));
|
||||
v_postfix = ((uint32_t)((((uint32_t)(1u)) << (a_npostfix & 31u)) - 1u));
|
||||
v_offset = ((uint32_t)((v_offset >> (a_npostfix & 31u)) + 4u));
|
||||
v_tmp = (v_offset / 2u);
|
||||
while (v_tmp != 0u) {
|
||||
v_ndistbits += 1u;
|
||||
v_tmp = (v_tmp >> 1u);
|
||||
}
|
||||
v_ndistbits -= 1u;
|
||||
v_half = ((v_offset >> (v_ndistbits & 31u)) & 1u);
|
||||
v_group = (((uint32_t)(((uint32_t)(v_ndistbits - 1u)) << 1u)) | v_half);
|
||||
if (v_group == 0u) {
|
||||
v_mas = ((uint32_t)(a_ndirect + 16u));
|
||||
v_md = a_ndirect;
|
||||
return (((uint64_t)(v_mas)) | (((uint64_t)(v_md)) << 32u));
|
||||
}
|
||||
v_group -= 1u;
|
||||
v_ndistbits = ((uint32_t)((v_group >> 1u) + 1u));
|
||||
v_extra = ((uint32_t)((((uint32_t)(1u)) << (v_ndistbits & 31u)) - 1u));
|
||||
v_start = ((uint32_t)((((uint32_t)(1u)) << (((uint32_t)(v_ndistbits + 1u)) & 31u)) - 4u));
|
||||
v_start += ((uint32_t)((v_group & 1u) << (v_ndistbits & 31u)));
|
||||
v_mas = ((uint32_t)(((uint32_t)(((uint32_t)((((uint32_t)(v_group << (a_npostfix & 31u))) | v_postfix) + a_ndirect)) + 16u)) + 1u));
|
||||
v_md = ((uint32_t)(((uint32_t)(((uint32_t)(((uint32_t)(((uint32_t)(v_start + v_extra)) << (a_npostfix & 31u))) + v_postfix)) + a_ndirect)) + 1u));
|
||||
return (((uint64_t)(v_mas)) | (((uint64_t)(v_md)) << 32u));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.init_distance_params
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__init_distance_params(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect,
|
||||
bool a_large_window) {
|
||||
uint64_t v_limit = 0;
|
||||
|
||||
self->private_impl.f_dist_postfix_bits = a_npostfix;
|
||||
self->private_impl.f_dist_num_direct_codes = a_ndirect;
|
||||
self->private_impl.f_dist_alphabet_size_max = ((uint32_t)(((uint32_t)(16u + a_ndirect)) + ((uint32_t)(((uint32_t)(24u)) << (((uint32_t)(a_npostfix + 1u)) & 31u)))));
|
||||
self->private_impl.f_dist_alphabet_size_limit = self->private_impl.f_dist_alphabet_size_max;
|
||||
self->private_impl.f_dist_max_distance = ((uint64_t)(((uint32_t)(((uint32_t)(a_ndirect + ((uint32_t)(((uint32_t)(1u)) << (((uint32_t)(((uint32_t)(24u + a_npostfix)) + 2u)) & 31u))))) - ((uint32_t)(((uint32_t)(1u)) << (((uint32_t)(a_npostfix + 2u)) & 31u)))))));
|
||||
if (a_large_window) {
|
||||
v_limit = wuffs_brotli__metablock_builder__calculate_distance_code_limit(self, 2147483644u, a_npostfix, a_ndirect);
|
||||
self->private_impl.f_dist_alphabet_size_max = ((uint32_t)(((uint32_t)(16u + a_ndirect)) + ((uint32_t)(((uint32_t)(62u)) << (((uint32_t)(a_npostfix + 1u)) & 31u)))));
|
||||
self->private_impl.f_dist_alphabet_size_limit = ((uint32_t)(v_limit));
|
||||
self->private_impl.f_dist_max_distance = (v_limit >> 32u);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.recompute_distance_prefixes
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__recompute_distance_prefixes(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_orig_npostfix,
|
||||
uint32_t a_orig_ndirect) {
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_c = 0;
|
||||
uint32_t v_d = 0;
|
||||
uint64_t v_r = 0;
|
||||
|
||||
if ((a_orig_npostfix == self->private_impl.f_dist_postfix_bits) && (a_orig_ndirect == self->private_impl.f_dist_num_direct_codes)) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
v_c = ((uint64_t)(v_i * 16u));
|
||||
if (((wuffs_brotli__metablock_builder__ld32(self, a_cmds, ((uint64_t)(v_c + 4u))) & 33554431u) != 0u) && (wuffs_brotli__metablock_builder__ld16(self, a_cmds, ((uint64_t)(v_c + 12u))) >= 128u)) {
|
||||
v_d = wuffs_brotli__metablock_builder__command_restore_distance_code(self,
|
||||
a_cmds,
|
||||
v_c,
|
||||
a_orig_npostfix,
|
||||
a_orig_ndirect);
|
||||
v_r = wuffs_brotli__metablock_builder__prefix_encode_copy_distance(self, ((uint64_t)(v_d)), ((uint64_t)(self->private_impl.f_dist_num_direct_codes)), self->private_impl.f_dist_postfix_bits);
|
||||
wuffs_brotli__metablock_builder__st16(self, a_cmds, ((uint64_t)(v_c + 14u)), ((uint32_t)((v_r & 65535u))));
|
||||
wuffs_brotli__metablock_builder__st32(self, a_cmds, ((uint64_t)(v_c + 8u)), ((uint32_t)((v_r >> 32u))));
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.compute_distance_cost
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__metablock_builder__compute_distance_cost(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_orig_npostfix,
|
||||
uint32_t a_orig_ndirect,
|
||||
uint32_t a_new_npostfix,
|
||||
uint32_t a_new_ndirect,
|
||||
uint64_t a_new_max_distance,
|
||||
uint64_t a_tmp) {
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_c = 0;
|
||||
bool v_equal = false;
|
||||
uint32_t v_dist_prefix = 0;
|
||||
uint32_t v_distance = 0;
|
||||
uint64_t v_extra_bits = 0;
|
||||
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, a_tmp, 544u);
|
||||
v_equal = ((a_orig_npostfix == a_new_npostfix) && (a_orig_ndirect == a_new_ndirect));
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
v_c = ((uint64_t)(v_i * 16u));
|
||||
if (((wuffs_brotli__metablock_builder__ld32(self, a_cmds, ((uint64_t)(v_c + 4u))) & 33554431u) != 0u) && (wuffs_brotli__metablock_builder__ld16(self, a_cmds, ((uint64_t)(v_c + 12u))) >= 128u)) {
|
||||
if (v_equal) {
|
||||
v_dist_prefix = wuffs_brotli__metablock_builder__ld16(self, a_cmds, ((uint64_t)(v_c + 14u)));
|
||||
} else {
|
||||
v_distance = wuffs_brotli__metablock_builder__command_restore_distance_code(self,
|
||||
a_cmds,
|
||||
v_c,
|
||||
a_orig_npostfix,
|
||||
a_orig_ndirect);
|
||||
if (((uint64_t)(v_distance)) > a_new_max_distance) {
|
||||
return false;
|
||||
}
|
||||
v_dist_prefix = ((uint32_t)((wuffs_brotli__metablock_builder__prefix_encode_copy_distance(self, ((uint64_t)(v_distance)), ((uint64_t)(a_new_ndirect)), a_new_npostfix) & 65535u)));
|
||||
}
|
||||
wuffs_brotli__metablock_builder__h_add(self,
|
||||
a_wb,
|
||||
a_tmp,
|
||||
544u,
|
||||
((uint64_t)((v_dist_prefix & 1023u))));
|
||||
v_extra_bits = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_extra_bits, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, (v_dist_prefix >> 10u)));
|
||||
}
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.set_params
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__set_params(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint32_t a_quality,
|
||||
bool a_large_window,
|
||||
bool a_disable_literal_context_modeling) {
|
||||
self->private_impl.f_quality = a_quality;
|
||||
self->private_impl.f_large_window = a_large_window;
|
||||
self->private_impl.f_disable_lcm = a_disable_literal_context_modeling;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.alloc_block_splits
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__alloc_block_splits(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands) {
|
||||
uint64_t v_num_literals = 0;
|
||||
uint64_t v_num_distances = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_c = 0;
|
||||
uint64_t v_n = 0;
|
||||
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
v_c = ((uint64_t)(v_i * 16u));
|
||||
v_num_literals += ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_cmds, v_c)));
|
||||
if (((wuffs_brotli__metablock_builder__ld32(self, a_cmds, ((uint64_t)(v_c + 4u))) & 33554431u) != 0u) && (wuffs_brotli__metablock_builder__ld16(self, a_cmds, ((uint64_t)(v_c + 12u))) >= 128u)) {
|
||||
v_num_distances += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < 3u) {
|
||||
self->private_impl.f_split_num_types[v_i] = 0u;
|
||||
self->private_impl.f_split_num_blocks[v_i] = 0u;
|
||||
self->private_impl.f_context_map_off[v_i] = 0u;
|
||||
self->private_impl.f_context_map_size[v_i] = 0u;
|
||||
self->private_impl.f_histograms_off[v_i] = 0u;
|
||||
self->private_impl.f_histograms_size[v_i] = 0u;
|
||||
v_i += 1u;
|
||||
}
|
||||
v_n = wuffs_base__u64__max(v_num_literals, 1u);
|
||||
self->private_impl.f_split_types_off[0u] = wuffs_brotli__metablock_builder__alloc(self, v_n);
|
||||
self->private_impl.f_split_lengths_off[0u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(v_n * 4u)));
|
||||
v_n = wuffs_base__u64__max(a_num_commands, 1u);
|
||||
self->private_impl.f_split_types_off[1u] = wuffs_brotli__metablock_builder__alloc(self, v_n);
|
||||
self->private_impl.f_split_lengths_off[1u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(v_n * 4u)));
|
||||
v_n = wuffs_base__u64__max(v_num_distances, 1u);
|
||||
self->private_impl.f_split_types_off[2u] = wuffs_brotli__metablock_builder__alloc(self, v_n);
|
||||
self->private_impl.f_split_lengths_off[2u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(v_n * 4u)));
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.reset_arena
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__reset_arena(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_wb_len) {
|
||||
self->private_impl.f_wb_len = a_wb_len;
|
||||
self->private_impl.f_wb_top = 0u;
|
||||
self->private_impl.f_wb_high = 0u;
|
||||
self->private_impl.f_oom = false;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.build_meta_block
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__metablock_builder__build_meta_block(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_ringbuffer,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint8_t a_prev_byte,
|
||||
uint8_t a_prev_byte2,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_literal_context_mode) {
|
||||
uint32_t v_orig_npostfix = 0;
|
||||
uint32_t v_orig_ndirect = 0;
|
||||
uint32_t v_orig_asm = 0;
|
||||
uint32_t v_orig_asl = 0;
|
||||
uint64_t v_orig_md = 0;
|
||||
uint32_t v_best_npostfix = 0;
|
||||
uint32_t v_best_ndirect = 0;
|
||||
uint32_t v_best_asm = 0;
|
||||
uint32_t v_best_asl = 0;
|
||||
uint64_t v_best_md = 0;
|
||||
uint32_t v_npostfix = 0;
|
||||
uint32_t v_ndirect_msb = 0;
|
||||
uint32_t v_ndirect = 0;
|
||||
bool v_check_orig = false;
|
||||
uint64_t v_best_dist_cost = 0;
|
||||
bool v_success = false;
|
||||
uint64_t v_tmp = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_lcm = 0;
|
||||
uint64_t v_lit_histos = 0;
|
||||
uint64_t v_lit_histos_size = 0;
|
||||
uint64_t v_dist_histos = 0;
|
||||
uint32_t v_context_mode = 0;
|
||||
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;
|
||||
v_orig_asl = self->private_impl.f_dist_alphabet_size_limit;
|
||||
v_orig_md = self->private_impl.f_dist_max_distance;
|
||||
v_best_npostfix = v_orig_npostfix;
|
||||
v_best_ndirect = v_orig_ndirect;
|
||||
v_best_asm = v_orig_asm;
|
||||
v_best_asl = v_orig_asl;
|
||||
v_best_md = v_orig_md;
|
||||
v_check_orig = true;
|
||||
v_best_dist_cost = 6088095589093318446u;
|
||||
v_tmp = wuffs_brotli__metablock_builder__alloc(self, 2192u);
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
v_npostfix = 0u;
|
||||
while (v_npostfix <= 3u) {
|
||||
while (v_ndirect_msb < 16u) {
|
||||
v_ndirect = ((uint32_t)(v_ndirect_msb << (v_npostfix & 31u)));
|
||||
wuffs_brotli__metablock_builder__init_distance_params(self, v_npostfix, v_ndirect, self->private_impl.f_large_window);
|
||||
if ((v_npostfix == v_orig_npostfix) && (v_ndirect == v_orig_ndirect)) {
|
||||
v_check_orig = false;
|
||||
}
|
||||
v_success = wuffs_brotli__metablock_builder__compute_distance_cost(self,
|
||||
a_wb,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
v_orig_npostfix,
|
||||
v_orig_ndirect,
|
||||
v_npostfix,
|
||||
v_ndirect,
|
||||
self->private_impl.f_dist_max_distance,
|
||||
v_tmp);
|
||||
if ( ! v_success || (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, self->private_impl.f_dist_cost, v_best_dist_cost) != 0u)) {
|
||||
break;
|
||||
}
|
||||
v_best_dist_cost = self->private_impl.f_dist_cost;
|
||||
v_best_npostfix = self->private_impl.f_dist_postfix_bits;
|
||||
v_best_ndirect = self->private_impl.f_dist_num_direct_codes;
|
||||
v_best_asm = self->private_impl.f_dist_alphabet_size_max;
|
||||
v_best_asl = self->private_impl.f_dist_alphabet_size_limit;
|
||||
v_best_md = self->private_impl.f_dist_max_distance;
|
||||
v_ndirect_msb += 1u;
|
||||
}
|
||||
if (v_ndirect_msb > 0u) {
|
||||
v_ndirect_msb -= 1u;
|
||||
}
|
||||
v_ndirect_msb /= 2u;
|
||||
v_npostfix += 1u;
|
||||
}
|
||||
if (v_check_orig) {
|
||||
v_success = wuffs_brotli__metablock_builder__compute_distance_cost(self,
|
||||
a_wb,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
v_orig_npostfix,
|
||||
v_orig_ndirect,
|
||||
v_orig_npostfix,
|
||||
v_orig_ndirect,
|
||||
v_orig_md,
|
||||
v_tmp);
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, self->private_impl.f_dist_cost, v_best_dist_cost) != 0u) {
|
||||
v_best_npostfix = v_orig_npostfix;
|
||||
v_best_ndirect = v_orig_ndirect;
|
||||
v_best_asm = v_orig_asm;
|
||||
v_best_asl = v_orig_asl;
|
||||
v_best_md = v_orig_md;
|
||||
}
|
||||
}
|
||||
self->private_impl.f_dist_postfix_bits = v_best_npostfix;
|
||||
self->private_impl.f_dist_num_direct_codes = v_best_ndirect;
|
||||
self->private_impl.f_dist_alphabet_size_max = v_best_asm;
|
||||
self->private_impl.f_dist_alphabet_size_limit = v_best_asl;
|
||||
self->private_impl.f_dist_max_distance = v_best_md;
|
||||
self->private_impl.f_wb_top = 0u;
|
||||
wuffs_brotli__metablock_builder__recompute_distance_prefixes(self,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
v_orig_npostfix,
|
||||
v_orig_ndirect);
|
||||
wuffs_brotli__metablock_builder__alloc_block_splits(self, a_cmds, a_num_commands);
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
wuffs_brotli__metablock_builder__split_block(self,
|
||||
a_wb,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
a_ringbuffer,
|
||||
a_pos,
|
||||
a_mask);
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
self->private_impl.f_histograms_size[1u] = self->private_impl.f_split_num_types[1u];
|
||||
self->private_impl.f_histograms_off[1u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(self->private_impl.f_histograms_size[1u] * 2832u)));
|
||||
self->private_impl.f_context_map_off[1u] = 0u;
|
||||
self->private_impl.f_context_map_size[1u] = 0u;
|
||||
self->private_impl.f_context_map_size[0u] = ((uint64_t)(self->private_impl.f_split_num_types[0u] << 6u));
|
||||
self->private_impl.f_context_map_off[0u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(self->private_impl.f_context_map_size[0u] * 4u)));
|
||||
self->private_impl.f_histograms_size[0u] = self->private_impl.f_context_map_size[0u];
|
||||
self->private_impl.f_histograms_off[0u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(self->private_impl.f_histograms_size[0u] * 1040u)));
|
||||
self->private_impl.f_context_map_size[2u] = ((uint64_t)(self->private_impl.f_split_num_types[2u] << 2u));
|
||||
self->private_impl.f_context_map_off[2u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(self->private_impl.f_context_map_size[2u] * 4u)));
|
||||
self->private_impl.f_histograms_size[2u] = self->private_impl.f_context_map_size[2u];
|
||||
self->private_impl.f_histograms_off[2u] = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(self->private_impl.f_histograms_size[2u] * 2192u)));
|
||||
v_lcm = 1u;
|
||||
v_context_mode = 4294967295u;
|
||||
if ( ! self->private_impl.f_disable_lcm) {
|
||||
v_lcm = 64u;
|
||||
v_context_mode = (a_literal_context_mode & 3u);
|
||||
}
|
||||
v_lit_histos_size = ((uint64_t)(self->private_impl.f_split_num_types[0u] * v_lcm));
|
||||
v_lit_histos = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(v_lit_histos_size * 1040u)));
|
||||
v_dist_histos = wuffs_brotli__metablock_builder__alloc(self, ((uint64_t)(self->private_impl.f_context_map_size[2u] * 2192u)));
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < v_lit_histos_size) {
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, ((uint64_t)(v_lit_histos + ((uint64_t)(v_i * 1040u)))), 256u);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_context_map_size[2u]) {
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, ((uint64_t)(v_dist_histos + ((uint64_t)(v_i * 2192u)))), 544u);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_histograms_size[1u]) {
|
||||
wuffs_brotli__metablock_builder__h_clear(self, a_wb, ((uint64_t)(self->private_impl.f_histograms_off[1u] + ((uint64_t)(v_i * 2832u)))), 704u);
|
||||
v_i += 1u;
|
||||
}
|
||||
if ((self->private_impl.f_split_types_off[0u] <= ((uint64_t)(a_wb.len))) &&
|
||||
(self->private_impl.f_split_lengths_off[0u] <= ((uint64_t)(a_wb.len))) &&
|
||||
(self->private_impl.f_split_types_off[1u] <= ((uint64_t)(a_wb.len))) &&
|
||||
(self->private_impl.f_split_lengths_off[1u] <= ((uint64_t)(a_wb.len))) &&
|
||||
(self->private_impl.f_split_types_off[2u] <= ((uint64_t)(a_wb.len))) &&
|
||||
(self->private_impl.f_split_lengths_off[2u] <= ((uint64_t)(a_wb.len))) &&
|
||||
(v_lit_histos <= ((uint64_t)(a_wb.len))) &&
|
||||
(self->private_impl.f_histograms_off[1u] <= ((uint64_t)(a_wb.len))) &&
|
||||
(v_dist_histos <= ((uint64_t)(a_wb.len)))) {
|
||||
wuffs_brotli__bit_cost__build_histograms_with_context(&self->private_data.f_bc,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_split_types_off[0u]),
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_split_lengths_off[0u]),
|
||||
self->private_impl.f_split_num_blocks[0u],
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_split_types_off[1u]),
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_split_lengths_off[1u]),
|
||||
self->private_impl.f_split_num_blocks[1u],
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_split_types_off[2u]),
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_split_lengths_off[2u]),
|
||||
self->private_impl.f_split_num_blocks[2u],
|
||||
a_ringbuffer,
|
||||
a_pos,
|
||||
a_mask,
|
||||
a_prev_byte,
|
||||
a_prev_byte2,
|
||||
v_context_mode,
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, v_lit_histos),
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_histograms_off[1u]),
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, v_dist_histos));
|
||||
}
|
||||
self->private_impl.f_histograms_size[0u] = wuffs_brotli__metablock_builder__cluster_histograms(self,
|
||||
a_wb,
|
||||
256u,
|
||||
v_lit_histos,
|
||||
v_lit_histos_size,
|
||||
256u,
|
||||
self->private_impl.f_histograms_off[0u],
|
||||
self->private_impl.f_context_map_off[0u]);
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
if (self->private_impl.f_disable_lcm) {
|
||||
v_i = self->private_impl.f_split_num_types[0u];
|
||||
while (v_i != 0u) {
|
||||
v_i -= 1u;
|
||||
v_v = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(self->private_impl.f_context_map_off[0u] + ((uint64_t)(v_i * 4u)))));
|
||||
v_j = 0u;
|
||||
while (v_j < 64u) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(self->private_impl.f_context_map_off[0u] + ((uint64_t)(((uint64_t)(((uint64_t)(v_i << 6u)) + v_j)) * 4u)))), v_v);
|
||||
v_j += 1u;
|
||||
}
|
||||
}
|
||||
}
|
||||
self->private_impl.f_histograms_size[2u] = wuffs_brotli__metablock_builder__cluster_histograms(self,
|
||||
a_wb,
|
||||
544u,
|
||||
v_dist_histos,
|
||||
self->private_impl.f_context_map_size[2u],
|
||||
256u,
|
||||
self->private_impl.f_histograms_off[2u],
|
||||
self->private_impl.f_context_map_off[2u]);
|
||||
if (self->private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.optimize_histograms
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__optimize_histograms(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_num_distance_codes) {
|
||||
uint64_t v_i = 0;
|
||||
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_histograms_size[0u]) {
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(self, a_wb, ((uint64_t)(self->private_impl.f_histograms_off[0u] + ((uint64_t)(v_i * 1040u)))), 256u);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_histograms_size[1u]) {
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(self, a_wb, ((uint64_t)(self->private_impl.f_histograms_off[1u] + ((uint64_t)(v_i * 2832u)))), 704u);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_histograms_size[2u]) {
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(self, a_wb, ((uint64_t)(self->private_impl.f_histograms_off[2u] + ((uint64_t)(v_i * 2192u)))), ((uint64_t)(a_num_distance_codes)));
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.optimize_huffman_counts_for_rle
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_counts,
|
||||
uint64_t a_length) {
|
||||
uint64_t v_length = 0;
|
||||
uint64_t v_nonzero_count = 0;
|
||||
uint64_t v_stride = 0;
|
||||
uint64_t v_limit = 0;
|
||||
uint64_t v_sum = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_k = 0;
|
||||
uint64_t v_nonzeros = 0;
|
||||
uint32_t v_smallest_nonzero = 0;
|
||||
uint64_t v_zeros = 0;
|
||||
uint32_t v_symbol = 0;
|
||||
uint64_t v_step = 0;
|
||||
uint64_t v_cnt = 0;
|
||||
uint32_t v_c0 = 0;
|
||||
uint32_t v_c1 = 0;
|
||||
uint32_t v_c2 = 0;
|
||||
bool v_flag = false;
|
||||
|
||||
v_length = wuffs_base__u64__min(a_length, 704u);
|
||||
v_i = 0u;
|
||||
while (v_i < v_length) {
|
||||
if (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u))))) != 0u) {
|
||||
v_nonzero_count += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_nonzero_count < 16u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
while ((v_length != 0u) && (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(((uint64_t)(v_length - 1u)) * 4u))))) == 0u)) {
|
||||
v_length -= 1u;
|
||||
}
|
||||
if (v_length == 0u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_smallest_nonzero = 1073741824u;
|
||||
v_i = 0u;
|
||||
while (v_i < v_length) {
|
||||
v_c0 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u)))));
|
||||
if (v_c0 != 0u) {
|
||||
v_nonzeros += 1u;
|
||||
if (v_smallest_nonzero > v_c0) {
|
||||
v_smallest_nonzero = v_c0;
|
||||
}
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_nonzeros < 5u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (v_smallest_nonzero < 4u) {
|
||||
v_zeros = ((uint64_t)(v_length - v_nonzeros));
|
||||
if (v_zeros < 6u) {
|
||||
v_i = 1u;
|
||||
while (((uint64_t)(v_i + 1u)) < v_length) {
|
||||
if ((wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(((uint64_t)(v_i - 1u)) * 4u))))) != 0u) && (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u))))) == 0u) && (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(((uint64_t)(v_i + 1u)) * 4u))))) != 0u)) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u)))), 1u);
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (v_nonzeros < 28u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
wuffs_private_impl__bulk_memset(&self->private_data.f_good_for_rle[0], 704u, 0u);
|
||||
v_symbol = wuffs_brotli__metablock_builder__ld32(self, a_wb, a_counts);
|
||||
v_step = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i <= v_length) {
|
||||
if ((v_i == v_length) || (wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u))))) != v_symbol)) {
|
||||
if (((v_symbol == 0u) && (v_step >= 5u)) || ((v_symbol != 0u) && (v_step >= 7u))) {
|
||||
v_k = 0u;
|
||||
while (v_k < v_step) {
|
||||
self->private_data.f_good_for_rle[(((uint64_t)(((uint64_t)(v_i - v_k)) - 1u)) % 704u)] = 1u;
|
||||
v_k += 1u;
|
||||
}
|
||||
}
|
||||
v_step = 1u;
|
||||
if (v_i != v_length) {
|
||||
v_symbol = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u)))));
|
||||
}
|
||||
} else {
|
||||
v_step += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_stride = 0u;
|
||||
v_c0 = wuffs_brotli__metablock_builder__ld32(self, a_wb, a_counts);
|
||||
v_c1 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + 4u)));
|
||||
v_c2 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + 8u)));
|
||||
v_limit = ((uint64_t)(((uint32_t)((((uint32_t)(((uint32_t)(256u)) * ((uint32_t)(((uint32_t)(v_c0 + v_c1)) + v_c2)))) / 3u) + 420u))));
|
||||
v_sum = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i <= v_length) {
|
||||
v_flag = (v_i == v_length);
|
||||
if ( ! v_flag) {
|
||||
v_flag = (self->private_data.f_good_for_rle[(v_i % 704u)] != 0u);
|
||||
}
|
||||
if ( ! v_flag && (v_i != 0u)) {
|
||||
v_flag = (self->private_data.f_good_for_rle[(((uint64_t)(v_i - 1u)) % 704u)] != 0u);
|
||||
}
|
||||
if ( ! v_flag) {
|
||||
v_c0 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u)))));
|
||||
v_flag = (((uint64_t)(((uint64_t)(((uint64_t)(((uint32_t)(((uint32_t)(256u)) * v_c0)))) - v_limit)) + 1240u)) >= 2480u);
|
||||
}
|
||||
if (v_flag) {
|
||||
if ((v_stride >= 4u) || ((v_stride >= 3u) && (v_sum == 0u))) {
|
||||
v_cnt = (((uint64_t)(v_sum + (v_stride / 2u))) / wuffs_base__u64__max(v_stride, 1u));
|
||||
if (v_cnt == 0u) {
|
||||
v_cnt = 1u;
|
||||
}
|
||||
if (v_sum == 0u) {
|
||||
v_cnt = 0u;
|
||||
}
|
||||
v_k = 0u;
|
||||
while (v_k < v_stride) {
|
||||
wuffs_brotli__metablock_builder__st32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(((uint64_t)(((uint64_t)(v_i - v_k)) - 1u)) * 4u)))), ((uint32_t)(v_cnt)));
|
||||
v_k += 1u;
|
||||
}
|
||||
}
|
||||
v_stride = 0u;
|
||||
v_sum = 0u;
|
||||
if (v_i < ((uint64_t)(v_length - 2u))) {
|
||||
v_c0 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u)))));
|
||||
v_c1 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(((uint64_t)(v_i + 1u)) * 4u)))));
|
||||
v_c2 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(((uint64_t)(v_i + 2u)) * 4u)))));
|
||||
v_limit = ((uint64_t)(((uint32_t)((((uint32_t)(((uint32_t)(256u)) * ((uint32_t)(((uint32_t)(v_c0 + v_c1)) + v_c2)))) / 3u) + 420u))));
|
||||
} else if (v_i < v_length) {
|
||||
v_c0 = wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u)))));
|
||||
v_limit = ((uint64_t)(((uint32_t)(((uint32_t)(256u)) * v_c0))));
|
||||
} else {
|
||||
v_limit = 0u;
|
||||
}
|
||||
}
|
||||
v_stride += 1u;
|
||||
if (v_i != v_length) {
|
||||
v_sum += ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_counts + ((uint64_t)(v_i * 4u)))))));
|
||||
if (v_stride >= 4u) {
|
||||
v_limit = (((uint64_t)(((uint64_t)(256u * v_sum)) + (v_stride / 2u))) / wuffs_base__u64__max(v_stride, 1u));
|
||||
}
|
||||
if (v_stride == 4u) {
|
||||
v_limit += 120u;
|
||||
}
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.sbv_scratch_len
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__sbv_scratch_len(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_length,
|
||||
uint64_t a_hs,
|
||||
uint64_t a_symbols_per_histogram,
|
||||
uint64_t a_max_histograms) {
|
||||
uint64_t v_nh = 0;
|
||||
uint64_t v_a = 0;
|
||||
uint64_t v_b = 0;
|
||||
uint64_t v_nb = 0;
|
||||
|
||||
if (a_length < 128u) {
|
||||
return 0u;
|
||||
}
|
||||
v_nh = wuffs_base__u64__sat_add((a_length / wuffs_base__u64__max(a_symbols_per_histogram, 1u)), 1u);
|
||||
v_nh = wuffs_base__u64__min(v_nh, a_max_histograms);
|
||||
v_a = wuffs_base__u64__sat_add(((uint64_t)(wuffs_base__u64__sat_add(v_nh, 1u) * a_hs)), ((uint64_t)(a_length * (wuffs_base__u64__sat_add(v_nh, 7u) >> 3u))));
|
||||
v_nb = a_length;
|
||||
v_b = wuffs_base__u64__sat_add(((uint64_t)(v_nb * wuffs_base__u64__sat_add(a_hs, 24u))), 1024u);
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_b, ((uint64_t)(wuffs_base__u64__min(v_nb, 64u) * a_hs)));
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_b, wuffs_base__u64__sat_add(49176u, ((uint64_t)(2u * a_hs))));
|
||||
v_nb = ((uint64_t)(64u * a_length));
|
||||
v_nb = wuffs_base__u64__min(v_nb, ((uint64_t)((a_length / 2u) * a_length)));
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_b, ((uint64_t)(wuffs_base__u64__sat_add(v_nb, 1u) * 24u)));
|
||||
return wuffs_base__u64__sat_add(a_length, wuffs_base__u64__max(v_a, v_b));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.cluster_scratch_len
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__cluster_scratch_len(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_hs) {
|
||||
uint64_t v_n = 0;
|
||||
uint64_t v_p = 0;
|
||||
|
||||
v_n = wuffs_base__u64__sat_add(wuffs_base__u64__sat_add(((uint64_t)(12u * a_in_size)), 49176u), a_hs);
|
||||
v_p = ((uint64_t)(64u * a_in_size));
|
||||
v_p = wuffs_base__u64__min(v_p, ((uint64_t)((a_in_size / 2u) * a_in_size)));
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_n, ((uint64_t)(wuffs_base__u64__sat_add(v_p, 1u) * 24u)));
|
||||
return wuffs_base__u64__sat_add(v_n, ((uint64_t)(a_in_size * a_hs)));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.workbuf_len_worst_case
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__metablock_builder__workbuf_len_worst_case(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_num_literals,
|
||||
uint64_t a_num_commands) {
|
||||
uint64_t v_l = 0;
|
||||
uint64_t v_c = 0;
|
||||
uint64_t v_n = 0;
|
||||
uint64_t v_split = 0;
|
||||
|
||||
v_l = wuffs_base__u64__min(a_num_literals, 4294967295u);
|
||||
v_c = wuffs_base__u64__min(a_num_commands, 4294967295u);
|
||||
v_n = wuffs_base__u64__sat_add(((uint64_t)(5u * wuffs_base__u64__max(v_l, 1u))), ((uint64_t)(10u * wuffs_base__u64__max(v_c, 1u))));
|
||||
v_split = wuffs_base__u64__sat_add(v_l, wuffs_brotli__metablock_builder__sbv_scratch_len(self,
|
||||
v_l,
|
||||
1040u,
|
||||
544u,
|
||||
100u));
|
||||
v_split = wuffs_base__u64__max(v_split, wuffs_base__u64__sat_add(((uint64_t)(2u * v_c)), wuffs_brotli__metablock_builder__sbv_scratch_len(self,
|
||||
v_c,
|
||||
2832u,
|
||||
530u,
|
||||
50u)));
|
||||
v_split = wuffs_base__u64__max(v_split, wuffs_base__u64__sat_add(((uint64_t)(2u * v_c)), wuffs_brotli__metablock_builder__sbv_scratch_len(self,
|
||||
v_c,
|
||||
2192u,
|
||||
544u,
|
||||
50u)));
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_n, wuffs_base__u64__max(v_split, 2192u));
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_n, 20078592u);
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_n, 19283968u);
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_n, wuffs_base__u64__max(wuffs_brotli__metablock_builder__cluster_scratch_len(self, 16384u, 1040u), wuffs_brotli__metablock_builder__cluster_scratch_len(self, 1024u, 2192u)));
|
||||
return v_n;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.gbs_histo_off
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -83753,6 +87351,260 @@ wuffs_brotli__metablock_tester__population_cost(
|
||||
return 0u;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.init_distance_params
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_tester__init_distance_params(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect,
|
||||
uint32_t a_large_window) {
|
||||
if (!self) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
wuffs_brotli__metablock_builder__init_distance_params(&self->private_data.f_mb, a_npostfix, a_ndirect, (a_large_window != 0u));
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.split_block
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_brotli__metablock_tester__split_block(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint32_t a_quality,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
if (!self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return wuffs_base__make_status(
|
||||
(self->private_impl.magic == WUFFS_BASE__DISABLED)
|
||||
? wuffs_base__error__disabled_by_previous_error
|
||||
: wuffs_base__error__initialize_not_called);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
wuffs_brotli__metablock_builder__split_block(&self->private_data.f_mb,
|
||||
a_workbuf,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
a_data,
|
||||
a_pos,
|
||||
a_mask);
|
||||
if (self->private_data.f_mb.private_impl.f_oom) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.cluster_histograms
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__cluster_histograms(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
uint32_t a_data_size,
|
||||
wuffs_base__slice_u8 a_in_histos,
|
||||
uint64_t a_in_size,
|
||||
uint64_t a_max_histograms,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t v_ds = 0;
|
||||
uint64_t v_hs = 0;
|
||||
uint64_t v_n = 0;
|
||||
uint64_t v_in_off = 0;
|
||||
uint64_t v_out_off = 0;
|
||||
uint64_t v_sym_off = 0;
|
||||
uint64_t v_out_size = 0;
|
||||
|
||||
if (a_data_size == 256u) {
|
||||
v_ds = 256u;
|
||||
} else if (a_data_size == 704u) {
|
||||
v_ds = 704u;
|
||||
} else if (a_data_size == 544u) {
|
||||
v_ds = 544u;
|
||||
} else {
|
||||
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);
|
||||
v_out_off = wuffs_brotli__metablock_builder__alloc(&self->private_data.f_mb, v_n);
|
||||
v_sym_off = wuffs_brotli__metablock_builder__alloc(&self->private_data.f_mb, ((uint64_t)(a_in_size * 4u)));
|
||||
if (self->private_data.f_mb.private_impl.f_oom || (v_n > ((uint64_t)(a_in_histos.len))) || (v_n > ((uint64_t)(a_workbuf.len)))) {
|
||||
return 18446744073709551615u;
|
||||
}
|
||||
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__slice_u8__subslice_j(a_workbuf, v_n), wuffs_base__slice_u8__subslice_j(a_in_histos, v_n));
|
||||
v_out_size = wuffs_brotli__metablock_builder__cluster_histograms(&self->private_data.f_mb,
|
||||
a_workbuf,
|
||||
v_ds,
|
||||
v_in_off,
|
||||
a_in_size,
|
||||
a_max_histograms,
|
||||
v_out_off,
|
||||
v_sym_off);
|
||||
if (self->private_data.f_mb.private_impl.f_oom) {
|
||||
return 18446744073709551615u;
|
||||
}
|
||||
self->private_data.f_mb.private_impl.f_histograms_off[0u] = v_out_off;
|
||||
self->private_data.f_mb.private_impl.f_context_map_off[0u] = v_sym_off;
|
||||
return v_out_size;
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.build_meta_block
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_brotli__metablock_tester__build_meta_block(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
wuffs_base__slice_u8 a_ringbuffer,
|
||||
uint64_t a_pos,
|
||||
uint64_t a_mask,
|
||||
uint8_t a_prev_byte,
|
||||
uint8_t a_prev_byte2,
|
||||
wuffs_base__slice_u8 a_cmds,
|
||||
uint64_t a_num_commands,
|
||||
uint32_t a_literal_context_mode,
|
||||
uint32_t a_quality,
|
||||
uint32_t a_large_window,
|
||||
uint32_t a_disable_literal_context_modeling,
|
||||
uint32_t a_npostfix,
|
||||
uint32_t a_ndirect) {
|
||||
if (!self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return wuffs_base__make_status(
|
||||
(self->private_impl.magic == WUFFS_BASE__DISABLED)
|
||||
? wuffs_base__error__disabled_by_previous_error
|
||||
: wuffs_base__error__initialize_not_called);
|
||||
}
|
||||
|
||||
wuffs_base__status v_status = wuffs_base__make_status(NULL);
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_literals = 0;
|
||||
|
||||
v_i = 0u;
|
||||
while (v_i < a_num_commands) {
|
||||
v_literals += ((uint64_t)(wuffs_brotli__metablock_builder__ld32(&self->private_data.f_mb, a_cmds, ((uint64_t)(v_i * 16u)))));
|
||||
v_i += 1u;
|
||||
}
|
||||
self->private_impl.f_worst_case = wuffs_brotli__metablock_builder__workbuf_len_worst_case(&self->private_data.f_mb, v_literals, a_num_commands);
|
||||
wuffs_brotli__metablock_builder__set_params(&self->private_data.f_mb, a_quality, (a_large_window != 0u), (a_disable_literal_context_modeling != 0u));
|
||||
wuffs_brotli__metablock_builder__init_distance_params(&self->private_data.f_mb, a_npostfix, a_ndirect, (a_large_window != 0u));
|
||||
v_status = wuffs_brotli__metablock_builder__build_meta_block(&self->private_data.f_mb,
|
||||
a_workbuf,
|
||||
a_ringbuffer,
|
||||
a_pos,
|
||||
a_mask,
|
||||
a_prev_byte,
|
||||
a_prev_byte2,
|
||||
a_cmds,
|
||||
a_num_commands,
|
||||
a_literal_context_mode);
|
||||
return wuffs_private_impl__status__ensure_not_a_suspension(v_status);
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.optimize_histograms
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct
|
||||
wuffs_brotli__metablock_tester__optimize_histograms(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
uint32_t a_num_distance_codes) {
|
||||
if (!self) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
wuffs_brotli__metablock_builder__optimize_histograms(&self->private_data.f_mb, a_workbuf, a_num_distance_codes);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.get
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__get(
|
||||
const wuffs_brotli__metablock_tester* self,
|
||||
uint32_t a_key) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
|
||||
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t v_c = 0;
|
||||
|
||||
if (a_key < 12u) {
|
||||
v_c = ((uint32_t)(((a_key / 4u) % 3u)));
|
||||
if ((a_key % 4u) == 0u) {
|
||||
return self->private_data.f_mb.private_impl.f_split_num_types[v_c];
|
||||
} else if ((a_key % 4u) == 1u) {
|
||||
return self->private_data.f_mb.private_impl.f_split_num_blocks[v_c];
|
||||
} else if ((a_key % 4u) == 2u) {
|
||||
return self->private_data.f_mb.private_impl.f_split_types_off[v_c];
|
||||
}
|
||||
return self->private_data.f_mb.private_impl.f_split_lengths_off[v_c];
|
||||
} else if (a_key < 24u) {
|
||||
v_c = ((uint32_t)((a_key % 3u)));
|
||||
if (a_key < 15u) {
|
||||
return self->private_data.f_mb.private_impl.f_context_map_off[v_c];
|
||||
} else if (a_key < 18u) {
|
||||
return self->private_data.f_mb.private_impl.f_context_map_size[v_c];
|
||||
} else if (a_key < 21u) {
|
||||
return self->private_data.f_mb.private_impl.f_histograms_off[v_c];
|
||||
}
|
||||
return self->private_data.f_mb.private_impl.f_histograms_size[v_c];
|
||||
} else if (a_key == 24u) {
|
||||
return ((uint64_t)(self->private_data.f_mb.private_impl.f_dist_postfix_bits));
|
||||
} else if (a_key == 25u) {
|
||||
return ((uint64_t)(self->private_data.f_mb.private_impl.f_dist_num_direct_codes));
|
||||
} else if (a_key == 26u) {
|
||||
return ((uint64_t)(self->private_data.f_mb.private_impl.f_dist_alphabet_size_max));
|
||||
} else if (a_key == 27u) {
|
||||
return ((uint64_t)(self->private_data.f_mb.private_impl.f_dist_alphabet_size_limit));
|
||||
} else if (a_key == 28u) {
|
||||
return self->private_data.f_mb.private_impl.f_dist_max_distance;
|
||||
} else if (a_key == 29u) {
|
||||
return self->private_data.f_mb.private_impl.f_wb_high;
|
||||
} else if (a_key == 30u) {
|
||||
return self->private_impl.f_worst_case;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.byte_at
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
|
||||
@@ -247,19 +247,1022 @@ test_bit_cost(int iters) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- InitDistanceParams
|
||||
|
||||
static void //
|
||||
test_init_distance_params(void) {
|
||||
uint32_t npostfix, ndirect_msb, lw;
|
||||
for (lw = 0; lw < 2; lw++) {
|
||||
for (npostfix = 0; npostfix <= 3; npostfix++) {
|
||||
for (ndirect_msb = 0; ndirect_msb < 16; ndirect_msb++) {
|
||||
uint32_t ndirect = ndirect_msb << npostfix;
|
||||
BrotliDistanceParams c;
|
||||
snprintf(g_config_name, sizeof(g_config_name),
|
||||
"InitDistanceParams npostfix=%u ndirect=%u lw=%u", npostfix,
|
||||
ndirect, lw);
|
||||
BrotliInitDistanceParams(&c, npostfix, ndirect, lw ? BROTLI_TRUE : 0);
|
||||
wuffs_brotli__metablock_tester__init_distance_params(g_tester, npostfix,
|
||||
ndirect, lw);
|
||||
CHECK_EQ("distance_postfix_bits", c.distance_postfix_bits,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 24));
|
||||
CHECK_EQ("num_direct_distance_codes", c.num_direct_distance_codes,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 25));
|
||||
CHECK_EQ("alphabet_size_max", c.alphabet_size_max,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 26));
|
||||
CHECK_EQ("alphabet_size_limit", c.alphabet_size_limit,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 27));
|
||||
CHECK_EQ("max_distance", c.max_distance,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 28));
|
||||
count(4, "InitDistanceParams");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- Test inputs
|
||||
|
||||
typedef struct {
|
||||
const char* name;
|
||||
uint8_t* data;
|
||||
size_t len;
|
||||
} file_t;
|
||||
|
||||
#define MAX_FILES 64
|
||||
static file_t g_files[MAX_FILES];
|
||||
static size_t g_num_files = 0;
|
||||
|
||||
static void //
|
||||
load_file(const char* path) {
|
||||
FILE* f;
|
||||
long n;
|
||||
if (g_num_files >= MAX_FILES) {
|
||||
return;
|
||||
}
|
||||
f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
return;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (n > 0) {
|
||||
uint8_t* d = malloc((size_t)n);
|
||||
if (d && (fread(d, 1, (size_t)n, f) == (size_t)n)) {
|
||||
g_files[g_num_files].name = path;
|
||||
g_files[g_num_files].data = d;
|
||||
g_files[g_num_files].len = (size_t)n;
|
||||
g_num_files++;
|
||||
} else {
|
||||
free(d);
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
// Data styles.
|
||||
enum {
|
||||
STYLE_RANDOM = 0,
|
||||
STYLE_IDENTICAL,
|
||||
STYLE_FILE,
|
||||
STYLE_UTF8,
|
||||
STYLE_FEW_SYMBOLS,
|
||||
STYLE_SHIFTING, // The byte distribution changes every few KiB.
|
||||
STYLE_BINARY, // Structured binary (little-endian integers).
|
||||
NUM_STYLES
|
||||
};
|
||||
|
||||
static const char* g_style_names[NUM_STYLES] = {
|
||||
"random", "identical", "file", "utf8", "few", "shifting", "binary",
|
||||
};
|
||||
|
||||
static const char* g_file_name = "";
|
||||
static uint32_t g_shift_max = 8192; // STYLE_SHIFTING's maximum segment size.
|
||||
static int g_dist_style = 0;
|
||||
static uint32_t g_dist_shift = 0;
|
||||
static uint32_t g_dist_residue = 0;
|
||||
|
||||
static void //
|
||||
fill_data(uint8_t* d, size_t n, int style) {
|
||||
size_t i;
|
||||
g_file_name = "";
|
||||
switch (style) {
|
||||
case STYLE_RANDOM:
|
||||
for (i = 0; i < n; i++) {
|
||||
d[i] = (uint8_t)rng64();
|
||||
}
|
||||
break;
|
||||
case STYLE_IDENTICAL: {
|
||||
uint8_t b = (uint8_t)rng64();
|
||||
memset(d, b, n);
|
||||
break;
|
||||
}
|
||||
case STYLE_FILE: {
|
||||
file_t* f = &g_files[rng_below((uint32_t)g_num_files)];
|
||||
size_t off = rng_below((uint32_t)f->len);
|
||||
g_file_name = f->name;
|
||||
for (i = 0; i < n; i++) {
|
||||
d[i] = f->data[(off + i) % f->len];
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STYLE_UTF8:
|
||||
for (i = 0; i < n;) {
|
||||
uint32_t r = rng_below(10);
|
||||
if ((r < 5) || (i + 4 > n)) {
|
||||
d[i++] = (uint8_t)(0x20 + rng_below(0x5F));
|
||||
} else if (r < 7) {
|
||||
d[i++] = (uint8_t)(0xC2 + rng_below(0x1E));
|
||||
d[i++] = (uint8_t)(0x80 + rng_below(0x40));
|
||||
} else if (r < 9) {
|
||||
d[i++] = (uint8_t)(0xE0 + rng_below(0x10));
|
||||
d[i++] = (uint8_t)(0x80 + rng_below(0x40));
|
||||
d[i++] = (uint8_t)(0x80 + rng_below(0x40));
|
||||
} else {
|
||||
d[i++] = (uint8_t)(0xF0 + rng_below(0x5));
|
||||
d[i++] = (uint8_t)(0x80 + rng_below(0x40));
|
||||
d[i++] = (uint8_t)(0x80 + rng_below(0x40));
|
||||
d[i++] = (uint8_t)(0x80 + rng_below(0x40));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case STYLE_FEW_SYMBOLS: {
|
||||
uint32_t k = 2 + rng_below(6);
|
||||
uint8_t base = (uint8_t)rng64();
|
||||
for (i = 0; i < n; i++) {
|
||||
d[i] = (uint8_t)(base + rng_below(k) * 3);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STYLE_SHIFTING: {
|
||||
uint32_t lo = 0, span = 1;
|
||||
size_t next = 0;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (i == next) {
|
||||
lo = rng_below(256);
|
||||
span = 1 + rng_below(64);
|
||||
next = i + 64 + rng_below(g_shift_max);
|
||||
}
|
||||
d[i] = (uint8_t)(lo + rng_below(span));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
uint32_t v = (uint32_t)rng64();
|
||||
for (i = 0; i + 4 <= n; i += 4) {
|
||||
v += rng_below(300);
|
||||
memcpy(d + i, &v, 4);
|
||||
}
|
||||
for (; i < n; i++) {
|
||||
d[i] = (uint8_t)rng64();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- Commands
|
||||
|
||||
typedef struct {
|
||||
uint8_t* rb; // The ring buffer, of rb_size = mask + 1 bytes (plus slack).
|
||||
size_t mask;
|
||||
size_t pos;
|
||||
size_t total; // The metablock length.
|
||||
Command* cmds;
|
||||
size_t num_cmds;
|
||||
size_t cmds_cap;
|
||||
uint8_t prev_byte;
|
||||
uint8_t prev_byte2;
|
||||
BrotliDistanceParams dist;
|
||||
bool large_window;
|
||||
} block_t;
|
||||
|
||||
static void //
|
||||
push_cmd(block_t* b, size_t insert_len, size_t copy_len, int delta,
|
||||
size_t distance_code) {
|
||||
if (b->num_cmds == b->cmds_cap) {
|
||||
b->cmds_cap = b->cmds_cap ? 2 * b->cmds_cap : 1024;
|
||||
b->cmds = realloc(b->cmds, b->cmds_cap * sizeof(Command));
|
||||
}
|
||||
if (copy_len == 0) {
|
||||
InitInsertCommand(&b->cmds[b->num_cmds++], insert_len);
|
||||
} else {
|
||||
InitCommand(&b->cmds[b->num_cmds++], &b->dist, insert_len, copy_len, delta,
|
||||
distance_code);
|
||||
}
|
||||
}
|
||||
|
||||
static size_t //
|
||||
random_distance(block_t* b) {
|
||||
uint32_t r = rng_below(16);
|
||||
size_t max = b->large_window ? 0x7FFFFFFC : ((1u << 24) - 16);
|
||||
if ((g_dist_style == 1) && (r < 12)) {
|
||||
// Distances with a fixed residue modulo (1 << g_dist_shift), which
|
||||
// favors npostfix > 0.
|
||||
return ((size_t)(1 + rng_below(r < 8 ? 64 : 100000)) << g_dist_shift) +
|
||||
g_dist_residue;
|
||||
} else if ((g_dist_style == 2) && (r < 12)) {
|
||||
// Small distances, which favor ndirect > 0.
|
||||
return 1 + rng_below(4 + rng_below(130));
|
||||
}
|
||||
if (r < 6) {
|
||||
return 1 + rng_below(64);
|
||||
} else if (r < 10) {
|
||||
return 1 + rng_below(4096);
|
||||
} else if (r < 13) {
|
||||
return 1 + rng_below(1u << 18);
|
||||
} else if (r < 15) {
|
||||
return 1 + rng_below((uint32_t)max);
|
||||
}
|
||||
return max - rng_below(1000);
|
||||
}
|
||||
|
||||
static size_t //
|
||||
random_insert_len(size_t remaining, int mode) {
|
||||
uint32_t r = rng_below(16);
|
||||
size_t n;
|
||||
if (mode == 1) { // Long inserts.
|
||||
n = rng_below(r < 8 ? 3000 : 70000);
|
||||
} else if (r < 8) {
|
||||
n = rng_below(6);
|
||||
} else if (r < 13) {
|
||||
n = rng_below(130);
|
||||
} else if (r < 15) {
|
||||
n = rng_below(2200);
|
||||
} else {
|
||||
n = rng_below(30000);
|
||||
}
|
||||
return n < remaining ? n : remaining;
|
||||
}
|
||||
|
||||
static size_t //
|
||||
random_copy_len(int mode) {
|
||||
uint32_t r = rng_below(16);
|
||||
(void)mode;
|
||||
if (r < 8) {
|
||||
return 2 + rng_below(8);
|
||||
} else if (r < 13) {
|
||||
return 2 + rng_below(134);
|
||||
} else if (r < 15) {
|
||||
return 2 + rng_below(2200);
|
||||
}
|
||||
return 2 + rng_below(100000);
|
||||
}
|
||||
|
||||
// make_synthetic_commands fills b->cmds with random (but valid) commands
|
||||
// whose insert and copy lengths add up to b->total.
|
||||
static void //
|
||||
make_synthetic_commands(block_t* b, int mode) {
|
||||
size_t remaining = b->total;
|
||||
b->num_cmds = 0;
|
||||
while (remaining > 0) {
|
||||
size_t ins = random_insert_len(remaining, mode);
|
||||
size_t copy;
|
||||
int delta = 0;
|
||||
size_t dcode;
|
||||
remaining -= ins;
|
||||
if (remaining < 2) {
|
||||
// A final insert-only command.
|
||||
push_cmd(b, ins + remaining, 0, 0, 0);
|
||||
remaining = 0;
|
||||
break;
|
||||
}
|
||||
copy = random_copy_len(mode);
|
||||
if (copy > remaining) {
|
||||
copy = remaining;
|
||||
}
|
||||
if ((rng_below(16) == 0) && (copy >= 4) && (copy <= 24)) {
|
||||
// A dictionary-like copy whose length code differs from its length.
|
||||
delta = (int)rng_below(9) - 4;
|
||||
if ((int)copy + delta < 2) {
|
||||
delta = 0;
|
||||
}
|
||||
}
|
||||
if (rng_below(4) == 0) {
|
||||
dcode = rng_below(16); // A short (distance cache) code.
|
||||
} else {
|
||||
dcode = random_distance(b) + 15;
|
||||
}
|
||||
push_cmd(b, ins, copy, delta, dcode);
|
||||
remaining -= copy;
|
||||
}
|
||||
}
|
||||
|
||||
// make_lz77_commands fills b->cmds with the commands of a simple greedy LZ77
|
||||
// parse of the metablock's bytes (with a 1-entry distance cache).
|
||||
static void //
|
||||
make_lz77_commands(block_t* b) {
|
||||
static uint32_t table[1 << 16];
|
||||
size_t i = 0, ins = 0, last_dist = 0;
|
||||
memset(table, 0xFF, sizeof(table));
|
||||
b->num_cmds = 0;
|
||||
while (i < b->total) {
|
||||
size_t cand_len = 0, cand_dist = 0;
|
||||
if (i + 4 <= b->total) {
|
||||
uint32_t v = 0;
|
||||
uint32_t h;
|
||||
size_t k;
|
||||
for (k = 0; k < 4; k++) {
|
||||
v |= (uint32_t)b->rb[(b->pos + i + k) & b->mask] << (8 * k);
|
||||
}
|
||||
h = (v * 0x1E35A7BDu) >> 16;
|
||||
if (table[h] != 0xFFFFFFFFu) {
|
||||
size_t j = table[h];
|
||||
size_t len = 0;
|
||||
while ((i + len < b->total) && (len < 1000) &&
|
||||
(b->rb[(b->pos + j + len) & b->mask] ==
|
||||
b->rb[(b->pos + i + len) & b->mask])) {
|
||||
len++;
|
||||
}
|
||||
if (len >= 4) {
|
||||
cand_len = len;
|
||||
cand_dist = i - j;
|
||||
}
|
||||
}
|
||||
table[h] = (uint32_t)i;
|
||||
}
|
||||
if (cand_len) {
|
||||
size_t dcode = (cand_dist == last_dist) ? 0 : cand_dist + 15;
|
||||
push_cmd(b, ins, cand_len, 0, dcode);
|
||||
last_dist = cand_dist;
|
||||
ins = 0;
|
||||
i += cand_len;
|
||||
} else {
|
||||
ins++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (ins > 0) {
|
||||
push_cmd(b, ins, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// make_short_commands fills b->cmds with many short commands.
|
||||
static void //
|
||||
make_short_commands(block_t* b) {
|
||||
size_t remaining = b->total;
|
||||
b->num_cmds = 0;
|
||||
while (remaining > 0) {
|
||||
size_t ins = rng_below(4);
|
||||
size_t copy = 2 + rng_below(6);
|
||||
if (ins > remaining) {
|
||||
ins = remaining;
|
||||
}
|
||||
remaining -= ins;
|
||||
if (remaining < copy) {
|
||||
push_cmd(b, ins + remaining, 0, 0, 0);
|
||||
break;
|
||||
}
|
||||
push_cmd(b, ins, copy, 0,
|
||||
rng_below(3) ? rng_below(16) : random_distance(b) + 15);
|
||||
remaining -= copy;
|
||||
}
|
||||
}
|
||||
|
||||
// make_block sets up the ring buffer (with the metablock's total bytes at
|
||||
// position pos, possibly wrapping around) and its commands.
|
||||
static void //
|
||||
make_block(block_t* b, size_t total, int style, int cmd_mode, bool wrap) {
|
||||
size_t rb_size = 1024;
|
||||
uint8_t* tmp;
|
||||
size_t i;
|
||||
while (rb_size < total + (wrap ? 0 : 1)) {
|
||||
rb_size *= 2;
|
||||
}
|
||||
if (!wrap && (rb_size < 2 * total)) {
|
||||
rb_size *= 2;
|
||||
}
|
||||
free(b->rb);
|
||||
b->rb = malloc(rb_size + 64);
|
||||
memset(b->rb, 0, rb_size + 64);
|
||||
b->mask = rb_size - 1;
|
||||
b->pos = wrap ? (rb_size - rng_below((uint32_t)total + 1)) & b->mask
|
||||
: rng_below((uint32_t)(rb_size - total));
|
||||
b->total = total;
|
||||
tmp = malloc(total + 1);
|
||||
fill_data(tmp, total, style);
|
||||
for (i = 0; i < total; i++) {
|
||||
b->rb[(b->pos + i) & b->mask] = tmp[i];
|
||||
}
|
||||
free(tmp);
|
||||
b->prev_byte = (uint8_t)rng64();
|
||||
b->prev_byte2 = (uint8_t)rng64();
|
||||
if (cmd_mode == 2) {
|
||||
make_lz77_commands(b);
|
||||
} else if (cmd_mode == 3) {
|
||||
make_short_commands(b);
|
||||
} else {
|
||||
make_synthetic_commands(b, cmd_mode);
|
||||
}
|
||||
}
|
||||
|
||||
// Coverage statistics.
|
||||
static uint64_t g_dist_params_seen[2][4][16];
|
||||
static uint64_t g_chosen_dist_params_seen[4][16];
|
||||
static uint64_t g_context_modes_seen[4];
|
||||
static uint64_t g_max_num_types[3];
|
||||
static uint64_t g_max_num_blocks[3];
|
||||
static uint64_t g_max_literals;
|
||||
static uint64_t g_max_commands;
|
||||
|
||||
// ---------------- Workbuf
|
||||
|
||||
static uint8_t* g_workbuf = NULL;
|
||||
static size_t g_workbuf_len = 0;
|
||||
static uint64_t g_max_workbuf_used = 0;
|
||||
|
||||
static void //
|
||||
ensure_workbuf(size_t n) {
|
||||
if (g_workbuf_len < n) {
|
||||
free(g_workbuf);
|
||||
g_workbuf = malloc(n);
|
||||
g_workbuf_len = n;
|
||||
if (!g_workbuf) {
|
||||
printf("out of memory\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t //
|
||||
ld32(const uint8_t* p) {
|
||||
uint32_t v;
|
||||
memcpy(&v, p, 4);
|
||||
return v;
|
||||
}
|
||||
|
||||
// ---------------- SplitBlock
|
||||
|
||||
static void //
|
||||
compare_block_split(const char* what, const BlockSplit* c, int cat) {
|
||||
size_t n;
|
||||
size_t i;
|
||||
uint64_t num_types = wuffs_brotli__metablock_tester__get(g_tester, 4 * cat);
|
||||
uint64_t num_blocks =
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 4 * cat + 1);
|
||||
uint64_t types_off =
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 4 * cat + 2);
|
||||
uint64_t lengths_off =
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 4 * cat + 3);
|
||||
char buf[128];
|
||||
snprintf(buf, sizeof(buf), "%s.num_types", what);
|
||||
CHECK_EQ(buf, c->num_types, num_types);
|
||||
snprintf(buf, sizeof(buf), "%s.num_blocks", what);
|
||||
CHECK_EQ(buf, c->num_blocks, num_blocks);
|
||||
n = c->num_blocks < num_blocks ? c->num_blocks : (size_t)num_blocks;
|
||||
for (i = 0; i < n; i++) {
|
||||
snprintf(buf, sizeof(buf), "%s.types[%zu]", what, i);
|
||||
CHECK_EQ(buf, c->types[i], g_workbuf[types_off + i]);
|
||||
snprintf(buf, sizeof(buf), "%s.lengths[%zu]", what, i);
|
||||
CHECK_EQ(buf, c->lengths[i], ld32(g_workbuf + lengths_off + 4 * i));
|
||||
if (g_num_mismatches > 1000) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static size_t //
|
||||
workbuf_guess(const block_t* b) {
|
||||
return ((size_t)64 << 20) + 64 * b->total + 64 * b->num_cmds;
|
||||
}
|
||||
|
||||
static void //
|
||||
test_split_block(block_t* b, int quality) {
|
||||
MemoryManager m;
|
||||
BrotliEncoderParams params;
|
||||
BlockSplit ls, cs, ds;
|
||||
wuffs_base__status st;
|
||||
size_t wlen = workbuf_guess(b);
|
||||
|
||||
memset(¶ms, 0, sizeof(params));
|
||||
params.quality = quality;
|
||||
BrotliInitMemoryManager(&m, 0, 0, 0);
|
||||
BrotliInitBlockSplit(&ls);
|
||||
BrotliInitBlockSplit(&cs);
|
||||
BrotliInitBlockSplit(&ds);
|
||||
BrotliSplitBlock(&m, b->cmds, b->num_cmds, b->rb, b->pos, b->mask, ¶ms,
|
||||
&ls, &cs, &ds);
|
||||
|
||||
while (true) {
|
||||
ensure_workbuf(wlen);
|
||||
st = wuffs_brotli__metablock_tester__split_block(
|
||||
g_tester,
|
||||
wuffs_base__make_slice_u8((uint8_t*)b->cmds,
|
||||
b->num_cmds * sizeof(Command)),
|
||||
b->num_cmds, wuffs_base__make_slice_u8(b->rb, b->mask + 1), b->pos,
|
||||
b->mask, (uint32_t)quality,
|
||||
wuffs_base__make_slice_u8(g_workbuf, g_workbuf_len));
|
||||
if (st.repr != wuffs_base__error__bad_workbuf_length) {
|
||||
break;
|
||||
}
|
||||
wlen *= 2;
|
||||
}
|
||||
if (st.repr) {
|
||||
printf("split_block: %s (%s)\n", st.repr, g_config_name);
|
||||
g_num_mismatches++;
|
||||
} else {
|
||||
uint64_t used = wuffs_brotli__metablock_tester__get(g_tester, 29);
|
||||
if (g_max_workbuf_used < used) {
|
||||
g_max_workbuf_used = used;
|
||||
}
|
||||
compare_block_split("literal_split", &ls, 0);
|
||||
compare_block_split("command_split", &cs, 1);
|
||||
compare_block_split("distance_split", &ds, 2);
|
||||
}
|
||||
BrotliDestroyBlockSplit(&m, &ls);
|
||||
BrotliDestroyBlockSplit(&m, &cs);
|
||||
BrotliDestroyBlockSplit(&m, &ds);
|
||||
}
|
||||
|
||||
// ---------------- BuildMetaBlock and OptimizeHistograms
|
||||
|
||||
static void //
|
||||
compare_histograms(const char* what, const void* c_histos, size_t hsize,
|
||||
uint64_t c_n, int cat) {
|
||||
uint64_t off = wuffs_brotli__metablock_tester__get(g_tester, 18 + cat);
|
||||
uint64_t n = wuffs_brotli__metablock_tester__get(g_tester, 21 + cat);
|
||||
size_t i;
|
||||
char buf[128];
|
||||
snprintf(buf, sizeof(buf), "%s.size", what);
|
||||
CHECK_EQ(buf, c_n, n);
|
||||
if (c_n != n) {
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < n; i++) {
|
||||
const uint8_t* cp = (const uint8_t*)c_histos + i * hsize;
|
||||
const uint8_t* wp = g_workbuf + off + i * hsize;
|
||||
size_t j;
|
||||
g_num_checks++;
|
||||
if (memcmp(cp, wp, hsize)) {
|
||||
for (j = 0; j < hsize; j += 4) {
|
||||
if (memcmp(cp + j, wp + j, 4)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "%s[%zu] at byte %zu", what, i, j);
|
||||
CHECK_EQ(buf, ld32(cp + j), ld32(wp + j));
|
||||
g_num_checks--;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void //
|
||||
compare_context_map(const char* what, const uint32_t* c_map, size_t c_n,
|
||||
int cat) {
|
||||
uint64_t off = wuffs_brotli__metablock_tester__get(g_tester, 12 + cat);
|
||||
uint64_t n = wuffs_brotli__metablock_tester__get(g_tester, 15 + cat);
|
||||
size_t i;
|
||||
char buf[128];
|
||||
snprintf(buf, sizeof(buf), "%s.size", what);
|
||||
CHECK_EQ(buf, c_n, n);
|
||||
if (c_n != n) {
|
||||
return;
|
||||
}
|
||||
g_num_checks++;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (c_map[i] != ld32(g_workbuf + off + 4 * i)) {
|
||||
snprintf(buf, sizeof(buf), "%s[%zu]", what, i);
|
||||
CHECK_EQ(buf, c_map[i], ld32(g_workbuf + off + 4 * i));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void //
|
||||
compare_meta_block_split(const MetaBlockSplit* mb, const char* suffix) {
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), "literal_histograms%s", suffix);
|
||||
compare_histograms(buf, mb->literal_histograms, sizeof(HistogramLiteral),
|
||||
mb->literal_histograms_size, 0);
|
||||
snprintf(buf, sizeof(buf), "command_histograms%s", suffix);
|
||||
compare_histograms(buf, mb->command_histograms, sizeof(HistogramCommand),
|
||||
mb->command_histograms_size, 1);
|
||||
snprintf(buf, sizeof(buf), "distance_histograms%s", suffix);
|
||||
compare_histograms(buf, mb->distance_histograms, sizeof(HistogramDistance),
|
||||
mb->distance_histograms_size, 2);
|
||||
}
|
||||
|
||||
static void //
|
||||
test_build_meta_block(block_t* b, int quality, bool disable_lcm,
|
||||
int context_mode) {
|
||||
MemoryManager m;
|
||||
BrotliEncoderParams params;
|
||||
MetaBlockSplit mb;
|
||||
wuffs_base__status st;
|
||||
size_t wlen = workbuf_guess(b);
|
||||
size_t cmds_size = b->num_cmds * sizeof(Command);
|
||||
Command* c_cmds = malloc(cmds_size + 1);
|
||||
Command* w_cmds = malloc(cmds_size + 1);
|
||||
size_t i;
|
||||
|
||||
memcpy(c_cmds, b->cmds, cmds_size);
|
||||
memset(¶ms, 0, sizeof(params));
|
||||
params.quality = quality;
|
||||
params.large_window = b->large_window ? BROTLI_TRUE : BROTLI_FALSE;
|
||||
params.disable_literal_context_modeling =
|
||||
disable_lcm ? BROTLI_TRUE : BROTLI_FALSE;
|
||||
params.dist = b->dist;
|
||||
BrotliInitMemoryManager(&m, 0, 0, 0);
|
||||
InitMetaBlockSplit(&mb);
|
||||
BrotliBuildMetaBlock(&m, b->rb, b->pos, b->mask, ¶ms, b->prev_byte,
|
||||
b->prev_byte2, c_cmds, b->num_cmds,
|
||||
(ContextType)context_mode, &mb);
|
||||
|
||||
while (true) {
|
||||
memcpy(w_cmds, b->cmds, cmds_size);
|
||||
ensure_workbuf(wlen);
|
||||
st = wuffs_brotli__metablock_tester__build_meta_block(
|
||||
g_tester, wuffs_base__make_slice_u8(g_workbuf, g_workbuf_len),
|
||||
wuffs_base__make_slice_u8(b->rb, b->mask + 1), b->pos, b->mask,
|
||||
b->prev_byte, b->prev_byte2,
|
||||
wuffs_base__make_slice_u8((uint8_t*)w_cmds, cmds_size), b->num_cmds,
|
||||
(uint32_t)context_mode, (uint32_t)quality, b->large_window ? 1 : 0,
|
||||
disable_lcm ? 1 : 0, b->dist.distance_postfix_bits,
|
||||
b->dist.num_direct_distance_codes);
|
||||
if (st.repr != wuffs_base__error__bad_workbuf_length) {
|
||||
break;
|
||||
}
|
||||
wlen *= 2;
|
||||
}
|
||||
if (st.repr) {
|
||||
printf("build_meta_block: %s (%s)\n", st.repr, g_config_name);
|
||||
g_num_mismatches++;
|
||||
goto done;
|
||||
}
|
||||
{
|
||||
uint64_t used = wuffs_brotli__metablock_tester__get(g_tester, 29);
|
||||
uint64_t worst = wuffs_brotli__metablock_tester__get(g_tester, 30);
|
||||
if (g_max_workbuf_used < used) {
|
||||
g_max_workbuf_used = used;
|
||||
}
|
||||
g_num_checks++;
|
||||
if (used > worst) {
|
||||
g_num_mismatches++;
|
||||
printf("workbuf used (%" PRIu64 ") > worst case (%" PRIu64 ") (%s)\n",
|
||||
used, worst, g_config_name);
|
||||
}
|
||||
}
|
||||
|
||||
CHECK_EQ("dist.distance_postfix_bits", params.dist.distance_postfix_bits,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 24));
|
||||
CHECK_EQ("dist.num_direct_distance_codes",
|
||||
params.dist.num_direct_distance_codes,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 25));
|
||||
CHECK_EQ("dist.alphabet_size_max", params.dist.alphabet_size_max,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 26));
|
||||
CHECK_EQ("dist.alphabet_size_limit", params.dist.alphabet_size_limit,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 27));
|
||||
CHECK_EQ("dist.max_distance", params.dist.max_distance,
|
||||
wuffs_brotli__metablock_tester__get(g_tester, 28));
|
||||
g_num_checks++;
|
||||
if (memcmp(c_cmds, w_cmds, cmds_size)) {
|
||||
for (i = 0; i < b->num_cmds; i++) {
|
||||
if (memcmp(&c_cmds[i], &w_cmds[i], sizeof(Command))) {
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), "cmds[%zu].dist_prefix_", i);
|
||||
CHECK_EQ(buf, c_cmds[i].dist_prefix_, w_cmds[i].dist_prefix_);
|
||||
snprintf(buf, sizeof(buf), "cmds[%zu].dist_extra_", i);
|
||||
CHECK_EQ(buf, c_cmds[i].dist_extra_, w_cmds[i].dist_extra_);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
g_chosen_dist_params_seen[params.dist.distance_postfix_bits & 3]
|
||||
[(params.dist.num_direct_distance_codes >>
|
||||
params.dist.distance_postfix_bits) & 15]++;
|
||||
g_context_modes_seen[context_mode & 3]++;
|
||||
if (g_max_num_types[0] < mb.literal_split.num_types) {
|
||||
g_max_num_types[0] = mb.literal_split.num_types;
|
||||
}
|
||||
if (g_max_num_types[1] < mb.command_split.num_types) {
|
||||
g_max_num_types[1] = mb.command_split.num_types;
|
||||
}
|
||||
if (g_max_num_types[2] < mb.distance_split.num_types) {
|
||||
g_max_num_types[2] = mb.distance_split.num_types;
|
||||
}
|
||||
if (g_max_num_blocks[0] < mb.literal_split.num_blocks) {
|
||||
g_max_num_blocks[0] = mb.literal_split.num_blocks;
|
||||
}
|
||||
if (g_max_num_blocks[1] < mb.command_split.num_blocks) {
|
||||
g_max_num_blocks[1] = mb.command_split.num_blocks;
|
||||
}
|
||||
if (g_max_num_blocks[2] < mb.distance_split.num_blocks) {
|
||||
g_max_num_blocks[2] = mb.distance_split.num_blocks;
|
||||
}
|
||||
if (g_max_commands < b->num_cmds) {
|
||||
g_max_commands = b->num_cmds;
|
||||
}
|
||||
{
|
||||
uint64_t nl = 0;
|
||||
for (i = 0; i < b->num_cmds; i++) {
|
||||
nl += b->cmds[i].insert_len_;
|
||||
}
|
||||
if (g_max_literals < nl) {
|
||||
g_max_literals = nl;
|
||||
}
|
||||
}
|
||||
}
|
||||
compare_block_split("mb.literal_split", &mb.literal_split, 0);
|
||||
compare_block_split("mb.command_split", &mb.command_split, 1);
|
||||
compare_block_split("mb.distance_split", &mb.distance_split, 2);
|
||||
compare_context_map("literal_context_map", mb.literal_context_map,
|
||||
mb.literal_context_map_size, 0);
|
||||
compare_context_map("distance_context_map", mb.distance_context_map,
|
||||
mb.distance_context_map_size, 2);
|
||||
compare_meta_block_split(&mb, "");
|
||||
|
||||
BrotliOptimizeHistograms(params.dist.alphabet_size_limit, &mb);
|
||||
wuffs_brotli__metablock_tester__optimize_histograms(
|
||||
g_tester, wuffs_base__make_slice_u8(g_workbuf, g_workbuf_len),
|
||||
params.dist.alphabet_size_limit);
|
||||
compare_meta_block_split(&mb, " (optimized)");
|
||||
|
||||
done:
|
||||
DestroyMetaBlockSplit(&m, &mb);
|
||||
free(c_cmds);
|
||||
free(w_cmds);
|
||||
}
|
||||
|
||||
// ---------------- ClusterHistograms
|
||||
|
||||
static void //
|
||||
test_cluster_histograms(int iters) {
|
||||
int it;
|
||||
for (it = 0; it < iters; it++) {
|
||||
int kind = (int)rng_below(3);
|
||||
size_t data_size = (kind == 0) ? 256 : (kind == 1) ? 704 : 544;
|
||||
size_t hsize = (kind == 0) ? sizeof(HistogramLiteral)
|
||||
: (kind == 1) ? sizeof(HistogramCommand)
|
||||
: sizeof(HistogramDistance);
|
||||
uint32_t r = rng_below(8);
|
||||
size_t in_size = (r < 4) ? 1 + rng_below(16)
|
||||
: (r < 7) ? 1 + rng_below(200)
|
||||
: 1 + rng_below(1100);
|
||||
size_t max_histograms = rng_below(4) ? 256 : 1 + rng_below(300);
|
||||
int style = (int)rng_below(7);
|
||||
int sharing = (int)rng_below(3);
|
||||
uint8_t* in = malloc(in_size * hsize);
|
||||
uint8_t* out = malloc(in_size * hsize);
|
||||
uint32_t* symbols = malloc(in_size * sizeof(uint32_t));
|
||||
size_t c_out_size = 0;
|
||||
uint64_t w_out_size;
|
||||
MemoryManager m;
|
||||
size_t i, j;
|
||||
snprintf(g_config_name, sizeof(g_config_name),
|
||||
"ClusterHistograms iter=%d kind=%d in_size=%zu max=%zu "
|
||||
"style=%d sharing=%d",
|
||||
it, kind, in_size, max_histograms, style, sharing);
|
||||
for (i = 0; i < in_size; i++) {
|
||||
uint32_t* data = (uint32_t*)(in + i * hsize);
|
||||
uint64_t total = 0;
|
||||
if ((sharing > 0) && (i > 0) && (rng_below(3) != 0)) {
|
||||
// Copy (and perturb) an earlier histogram, so that clustering has
|
||||
// something to merge.
|
||||
memcpy(data, in + rng_below((uint32_t)i) * hsize, data_size * 4);
|
||||
if (sharing == 2) {
|
||||
data[rng_below((uint32_t)data_size)] += rng_below(5);
|
||||
}
|
||||
} else if (rng_below(10) == 0) {
|
||||
memset(data, 0, data_size * 4); // An empty histogram.
|
||||
} else {
|
||||
fill_counts(data, data_size, style);
|
||||
}
|
||||
for (j = 0; j < data_size; j++) {
|
||||
total += data[j];
|
||||
}
|
||||
memcpy(in + i * hsize + data_size * 4, &total, 8);
|
||||
memset(in + i * hsize + data_size * 4 + 8, 0, 8);
|
||||
}
|
||||
BrotliInitMemoryManager(&m, 0, 0, 0);
|
||||
if (kind == 0) {
|
||||
BrotliClusterHistogramsLiteral(&m, (HistogramLiteral*)in, in_size,
|
||||
max_histograms, (HistogramLiteral*)out,
|
||||
&c_out_size, symbols);
|
||||
} else if (kind == 1) {
|
||||
BrotliClusterHistogramsCommand(&m, (HistogramCommand*)in, in_size,
|
||||
max_histograms, (HistogramCommand*)out,
|
||||
&c_out_size, symbols);
|
||||
} else {
|
||||
BrotliClusterHistogramsDistance(&m, (HistogramDistance*)in, in_size,
|
||||
max_histograms, (HistogramDistance*)out,
|
||||
&c_out_size, symbols);
|
||||
}
|
||||
ensure_workbuf(4 * in_size * hsize + ((size_t)16 << 20));
|
||||
w_out_size = wuffs_brotli__metablock_tester__cluster_histograms(
|
||||
g_tester, (uint32_t)data_size,
|
||||
wuffs_base__make_slice_u8(in, in_size * hsize), in_size,
|
||||
max_histograms, wuffs_base__make_slice_u8(g_workbuf, g_workbuf_len));
|
||||
CHECK_EQ("out_size", c_out_size, w_out_size);
|
||||
if (c_out_size == w_out_size) {
|
||||
uint64_t out_off = wuffs_brotli__metablock_tester__get(g_tester, 18);
|
||||
uint64_t sym_off = wuffs_brotli__metablock_tester__get(g_tester, 12);
|
||||
g_num_checks++;
|
||||
for (i = 0; i < c_out_size; i++) {
|
||||
if (memcmp(out + i * hsize, g_workbuf + out_off + i * hsize, hsize)) {
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), "out[%zu]", i);
|
||||
CHECK_EQ(buf, 0, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < in_size; i++) {
|
||||
if (symbols[i] != ld32(g_workbuf + sym_off + 4 * i)) {
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), "histogram_symbols[%zu]", i);
|
||||
CHECK_EQ(buf, symbols[i], ld32(g_workbuf + sym_off + 4 * i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
count(5 + kind, kind == 0 ? "ClusterHistogramsLiteral"
|
||||
: kind == 1 ? "ClusterHistogramsCommand"
|
||||
: "ClusterHistogramsDistance");
|
||||
free(in);
|
||||
free(out);
|
||||
free(symbols);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- Configurations
|
||||
|
||||
static block_t g_block;
|
||||
static uint32_t g_force_shift_max = 0;
|
||||
|
||||
|
||||
static void //
|
||||
random_dist_params(block_t* b) {
|
||||
uint32_t npostfix = rng_below(4);
|
||||
uint32_t ndirect = rng_below(16) << npostfix;
|
||||
if (rng_below(3) == 0) {
|
||||
npostfix = 0;
|
||||
ndirect = 0;
|
||||
}
|
||||
b->large_window = rng_below(4) == 0;
|
||||
BrotliInitDistanceParams(&b->dist, npostfix, ndirect,
|
||||
b->large_window ? BROTLI_TRUE : BROTLI_FALSE);
|
||||
}
|
||||
|
||||
static void //
|
||||
run_config(size_t total, int style, int cmd_mode, bool do_split,
|
||||
bool do_build) {
|
||||
bool wrap = rng_below(4) == 0;
|
||||
int quality = (rng_below(2) == 0) ? 10 : 11;
|
||||
bool disable_lcm = rng_below(4) == 0;
|
||||
int context_mode = (int)rng_below(4);
|
||||
if (rng_below(8) == 0) {
|
||||
quality = 4 + (int)rng_below(6);
|
||||
}
|
||||
g_shift_max = g_force_shift_max ? g_force_shift_max
|
||||
: rng_below(2) ? 8192
|
||||
: 64 + rng_below(2000);
|
||||
g_dist_style = (int)rng_below(3);
|
||||
g_dist_shift = 1 + rng_below(3);
|
||||
g_dist_residue = rng_below(1u << g_dist_shift);
|
||||
random_dist_params(&g_block);
|
||||
g_dist_params_seen[g_block.large_window ? 1 : 0]
|
||||
[g_block.dist.distance_postfix_bits]
|
||||
[g_block.dist.num_direct_distance_codes >>
|
||||
g_block.dist.distance_postfix_bits]++;
|
||||
make_block(&g_block, total, style, cmd_mode, wrap);
|
||||
snprintf(g_config_name, sizeof(g_config_name),
|
||||
"total=%zu style=%s%s%s cmds=%d(n=%zu) wrap=%d q=%d lw=%d "
|
||||
"npostfix=%u ndirect=%u nolcm=%d mode=%d rng=%016" PRIX64,
|
||||
total, g_style_names[style], *g_file_name ? ":" : "", g_file_name,
|
||||
cmd_mode, g_block.num_cmds, wrap, quality, g_block.large_window,
|
||||
g_block.dist.distance_postfix_bits,
|
||||
g_block.dist.num_direct_distance_codes, disable_lcm, context_mode,
|
||||
g_rng_state);
|
||||
if (do_split) {
|
||||
test_split_block(&g_block, quality);
|
||||
count(8, "SplitBlock");
|
||||
}
|
||||
if (do_build) {
|
||||
test_build_meta_block(&g_block, quality, disable_lcm, context_mode);
|
||||
count(9, "BuildMetaBlock+OptimizeHistograms");
|
||||
}
|
||||
}
|
||||
|
||||
static size_t //
|
||||
random_total(void) {
|
||||
uint32_t r = rng_below(16);
|
||||
if (r < 3) {
|
||||
return rng_below(130);
|
||||
} else if (r < 10) {
|
||||
return 130 + rng_below(3870);
|
||||
} else if (r < 15) {
|
||||
return 4000 + rng_below(36000);
|
||||
}
|
||||
return 40000 + rng_below(110000);
|
||||
}
|
||||
|
||||
// ---------------- Main
|
||||
|
||||
int //
|
||||
main(int argc, char** argv) {
|
||||
bool quick = (argc > 1) && !strcmp(argv[1], "-quick");
|
||||
bool quick = false;
|
||||
int n_bitcost = 200000, n_clusters = 3000, n_configs = 20000, n_large = 8;
|
||||
int large_off = 0, n_many_types = 0, n_edge = 64;
|
||||
int i;
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "-quick")) {
|
||||
quick = true;
|
||||
n_bitcost = 20000;
|
||||
n_clusters = 300;
|
||||
n_configs = 200;
|
||||
n_large = 1;
|
||||
} else if (!strncmp(argv[i], "-bitcost=", 9)) {
|
||||
n_bitcost = atoi(argv[i] + 9);
|
||||
} else if (!strncmp(argv[i], "-clusters=", 10)) {
|
||||
n_clusters = atoi(argv[i] + 10);
|
||||
} else if (!strncmp(argv[i], "-configs=", 9)) {
|
||||
n_configs = atoi(argv[i] + 9);
|
||||
} else if (!strncmp(argv[i], "-large=", 7)) {
|
||||
n_large = atoi(argv[i] + 7);
|
||||
} else if (!strncmp(argv[i], "-largeoff=", 10)) {
|
||||
large_off = atoi(argv[i] + 10);
|
||||
} else if (!strncmp(argv[i], "-manytypes=", 11)) {
|
||||
n_many_types = atoi(argv[i] + 11);
|
||||
} else if (!strncmp(argv[i], "-edge=", 6)) {
|
||||
n_edge = atoi(argv[i] + 6);
|
||||
} else if (!strncmp(argv[i], "-seed=", 6)) {
|
||||
g_rng_state = strtoull(argv[i] + 6, NULL, 0) | 1;
|
||||
}
|
||||
}
|
||||
(void)quick;
|
||||
g_tester = wuffs_brotli__metablock_tester__alloc();
|
||||
if (!g_tester) {
|
||||
printf("could not allocate the tester\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
test_bit_cost(quick ? 20000 : 200000);
|
||||
load_file("test/data/romeo.txt");
|
||||
load_file("test/data/midsummer.txt");
|
||||
load_file("test/data/pi.txt");
|
||||
load_file("test/data/abraca.txt");
|
||||
load_file("test/data/enwik5");
|
||||
load_file("test/data/archive.tar");
|
||||
load_file("test/data/hippopotamus.pgm");
|
||||
load_file("test/data/github-tags.json");
|
||||
load_file("test/data/json-things.formatted.json");
|
||||
load_file("test/data/256.bytes");
|
||||
if (g_num_files == 0) {
|
||||
printf("no test files (run from the Wuffs root directory)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
test_bit_cost(n_bitcost);
|
||||
test_init_distance_params();
|
||||
test_cluster_histograms(n_clusters);
|
||||
// Edge cases: no commands, one command, one literal, all identical bytes,
|
||||
// in every literal context mode.
|
||||
for (i = 0; i < n_edge; i++) {
|
||||
static const size_t totals[8] = {0, 1, 2, 5, 127, 128, 129, 3000};
|
||||
run_config(totals[i & 7], (i & 8) ? STYLE_IDENTICAL : STYLE_RANDOM,
|
||||
(i >> 4) % 3, true, true);
|
||||
}
|
||||
// Large configurations: at least 1 MiB of literals, and at least 100k
|
||||
// commands.
|
||||
for (i = large_off; i < large_off + n_large; i++) {
|
||||
static const int styles[4] = {STYLE_FILE, STYLE_RANDOM, STYLE_SHIFTING,
|
||||
STYLE_UTF8};
|
||||
if (i & 1) {
|
||||
// Many short commands.
|
||||
run_config((size_t)1 << 20, STYLE_FEW_SYMBOLS, 3, false, true);
|
||||
} else {
|
||||
run_config(((size_t)3 << 19) + rng_below(1 << 18), styles[(i / 2) & 3],
|
||||
1, false, true);
|
||||
}
|
||||
printf("large configuration %d done, %" PRIu64 " mismatches so far\n",
|
||||
i, g_num_mismatches);
|
||||
fflush(stdout);
|
||||
}
|
||||
// Inputs whose byte distribution changes every few hundred bytes, which
|
||||
// push the number of block types towards the 256 maximum (and the number
|
||||
// of FindBlocks histograms towards kMaxLiteralHistograms).
|
||||
for (i = 0; i < n_many_types; i++) {
|
||||
g_force_shift_max = 64 + rng_below(600);
|
||||
run_config((size_t)1 << (17 + rng_below(4)), STYLE_SHIFTING,
|
||||
(int)rng_below(2), true, true);
|
||||
g_force_shift_max = 0;
|
||||
printf("many-types configuration %d done, %" PRIu64
|
||||
" mismatches so far\n",
|
||||
i, g_num_mismatches);
|
||||
fflush(stdout);
|
||||
}
|
||||
{
|
||||
int n = n_configs;
|
||||
for (i = 0; i < n; i++) {
|
||||
int style = (int)rng_below(NUM_STYLES);
|
||||
int cmd_mode = (int)rng_below(3);
|
||||
run_config(random_total(), style, cmd_mode, rng_below(2) == 0, true);
|
||||
if ((i % 1000) == 999) {
|
||||
printf("%d configurations, %" PRIu64 " mismatches so far\n", i + 1,
|
||||
g_num_mismatches);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 64; i++) {
|
||||
if (g_count_names[i]) {
|
||||
@@ -267,6 +1270,34 @@ main(int argc, char** argv) {
|
||||
g_counts[i]);
|
||||
}
|
||||
}
|
||||
{
|
||||
int lw, np, nd, seen = 0, chosen = 0;
|
||||
for (lw = 0; lw < 2; lw++) {
|
||||
for (np = 0; np < 4; np++) {
|
||||
for (nd = 0; nd < 16; nd++) {
|
||||
seen += g_dist_params_seen[lw][np][nd] ? 1 : 0;
|
||||
if (lw == 0) {
|
||||
chosen += g_chosen_dist_params_seen[np][nd] ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("input (large_window, npostfix, ndirect) combinations: %d of 128\n",
|
||||
seen);
|
||||
printf("chosen (npostfix, ndirect) combinations: %d of 64\n", chosen);
|
||||
printf("context modes: %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
|
||||
g_context_modes_seen[0], g_context_modes_seen[1],
|
||||
g_context_modes_seen[2], g_context_modes_seen[3]);
|
||||
printf("max num_types (lit, cmd, dist): %" PRIu64 " %" PRIu64 " %" PRIu64
|
||||
"\n",
|
||||
g_max_num_types[0], g_max_num_types[1], g_max_num_types[2]);
|
||||
printf("max num_blocks (lit, cmd, dist): %" PRIu64 " %" PRIu64 " %" PRIu64
|
||||
"\n",
|
||||
g_max_num_blocks[0], g_max_num_blocks[1], g_max_num_blocks[2]);
|
||||
printf("max literals: %" PRIu64 ", max commands: %" PRIu64 "\n",
|
||||
g_max_literals, g_max_commands);
|
||||
}
|
||||
printf("max workbuf used: %" PRIu64 " bytes\n", g_max_workbuf_used);
|
||||
printf("%" PRIu64 " checks, %" PRIu64 " mismatches\n", g_num_checks,
|
||||
g_num_mismatches);
|
||||
free(g_tester);
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user