+ return lzms_get_lz_match_cost_nolen(ctx, state, offset) +
+ lzms_get_length_cost(&ctx->length_encoder, length);
+}
+
+static inline u32
+lzms_repsearch(const u8 * const strptr, const u32 bytes_remaining,
+ const struct lzms_lz_lru_queues *queue, u32 *offset_ret)
+{
+ u32 len;
+ unsigned slot = 0;
+
+ len = lz_repsearch(strptr, bytes_remaining, UINT32_MAX,
+ queue->recent_offsets, LZMS_NUM_RECENT_OFFSETS, &slot);
+ *offset_ret = queue->recent_offsets[slot];
+ return len;
+}
+
+
+static struct lz_match
+lzms_match_chooser_reverse_list(struct lzms_compressor *ctx, unsigned cur_pos)
+{
+ unsigned prev_link, saved_prev_link;
+ unsigned prev_match_offset, saved_prev_match_offset;
+
+ ctx->optimum_end_idx = cur_pos;
+
+ saved_prev_link = ctx->optimum[cur_pos].prev.link;
+ saved_prev_match_offset = ctx->optimum[cur_pos].prev.match_offset;
+
+ do {
+ prev_link = saved_prev_link;
+ prev_match_offset = saved_prev_match_offset;
+
+ saved_prev_link = ctx->optimum[prev_link].prev.link;
+ saved_prev_match_offset = ctx->optimum[prev_link].prev.match_offset;
+
+ ctx->optimum[prev_link].next.link = cur_pos;
+ ctx->optimum[prev_link].next.match_offset = prev_match_offset;
+
+ cur_pos = prev_link;
+ } while (cur_pos != 0);
+
+ ctx->optimum_cur_idx = ctx->optimum[0].next.link;
+
+ return (struct lz_match)
+ { .len = ctx->optimum_cur_idx,
+ .offset = ctx->optimum[0].next.match_offset,
+ };
+}
+
+/* This is similar to lzx_choose_near_optimal_item() in lzx-compress.c.
+ * Read that one if you want to understand it. */
+static struct lz_match
+lzms_get_near_optimal_item(struct lzms_compressor *ctx)
+{
+ u32 num_matches;
+ struct lz_match *matches;
+ struct lz_match match;
+ u32 longest_len;
+ u32 longest_rep_len;
+ u32 longest_rep_offset;
+ unsigned cur_pos;
+ unsigned end_pos;
+ struct lzms_adaptive_state initial_state;
+
+ if (ctx->optimum_cur_idx != ctx->optimum_end_idx) {
+ match.len = ctx->optimum[ctx->optimum_cur_idx].next.link -
+ ctx->optimum_cur_idx;
+ match.offset = ctx->optimum[ctx->optimum_cur_idx].next.match_offset;
+
+ ctx->optimum_cur_idx = ctx->optimum[ctx->optimum_cur_idx].next.link;
+ return match;
+ }
+
+ ctx->optimum_cur_idx = 0;
+ ctx->optimum_end_idx = 0;
+
+ if (lz_mf_get_position(ctx->mf) >= LZMS_MAX_INIT_RECENT_OFFSET) {
+ longest_rep_len = lzms_repsearch(lz_mf_get_window_ptr(ctx->mf),
+ lz_mf_get_bytes_remaining(ctx->mf),
+ &ctx->lru.lz, &longest_rep_offset);
+ } else {
+ longest_rep_len = 0;
+ }
+
+ if (longest_rep_len >= ctx->params.nice_match_length) {
+ lzms_skip_bytes(ctx, longest_rep_len);
+ return (struct lz_match) {
+ .len = longest_rep_len,
+ .offset = longest_rep_offset,
+ };
+ }
+
+ num_matches = lzms_get_matches(ctx, &matches);
+
+ if (num_matches) {
+ longest_len = matches[num_matches - 1].len;
+ if (longest_len >= ctx->params.nice_match_length) {
+ lzms_skip_bytes(ctx, longest_len - 1);
+ return matches[num_matches - 1];
+ }
+ } else {
+ longest_len = 1;
+ }
+
+ initial_state.lru = ctx->lru.lz;
+ initial_state.main_state = ctx->main_range_encoder.state;
+ initial_state.match_state = ctx->match_range_encoder.state;
+ initial_state.lz_match_state = ctx->lz_match_range_encoder.state;
+ for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS - 1; i++)
+ initial_state.lz_repeat_match_state[i] = ctx->lz_repeat_match_range_encoders[i].state;
+
+ ctx->optimum[1].state = initial_state;
+ ctx->optimum[1].cost = lzms_get_literal_cost(ctx,
+ &ctx->optimum[1].state,
+ *(lz_mf_get_window_ptr(ctx->mf) - 1));
+ ctx->optimum[1].prev.link = 0;
+
+ for (u32 i = 0, len = 2; i < num_matches; i++) {
+ u32 offset = matches[i].offset;
+ struct lzms_adaptive_state state;
+ u32 position_cost;
+
+ state = initial_state;
+ position_cost = 0;
+ position_cost += lzms_get_lz_match_cost_nolen(ctx, &state, offset);
+
+ do {
+ u32 cost;
+
+ cost = position_cost;
+ cost += lzms_get_length_cost(&ctx->length_encoder, len);
+
+ ctx->optimum[len].state = state;
+ ctx->optimum[len].prev.link = 0;
+ ctx->optimum[len].prev.match_offset = offset;
+ ctx->optimum[len].cost = cost;
+ } while (++len <= matches[i].len);
+ }
+ end_pos = longest_len;
+
+ if (longest_rep_len) {
+ struct lzms_adaptive_state state;
+ u32 cost;
+
+ while (end_pos < longest_rep_len)
+ ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
+
+ state = initial_state;
+ cost = lzms_get_lz_match_cost(ctx,
+ &state,
+ longest_rep_len,
+ longest_rep_offset);
+ if (cost <= ctx->optimum[longest_rep_len].cost) {
+ ctx->optimum[longest_rep_len].state = state;
+ ctx->optimum[longest_rep_len].prev.link = 0;
+ ctx->optimum[longest_rep_len].prev.match_offset = longest_rep_offset;
+ ctx->optimum[longest_rep_len].cost = cost;
+ }
+ }
+
+ cur_pos = 0;
+ for (;;) {
+ u32 cost;
+ struct lzms_adaptive_state state;
+
+ cur_pos++;
+
+ if (cur_pos == end_pos || cur_pos == ctx->params.optim_array_length)
+ return lzms_match_chooser_reverse_list(ctx, cur_pos);
+
+ if (lz_mf_get_position(ctx->mf) >= LZMS_MAX_INIT_RECENT_OFFSET) {
+ longest_rep_len = lzms_repsearch(lz_mf_get_window_ptr(ctx->mf),
+ lz_mf_get_bytes_remaining(ctx->mf),
+ &ctx->optimum[cur_pos].state.lru,
+ &longest_rep_offset);
+ } else {
+ longest_rep_len = 0;
+ }
+
+ if (longest_rep_len >= ctx->params.nice_match_length) {
+ match = lzms_match_chooser_reverse_list(ctx, cur_pos);
+
+ ctx->optimum[cur_pos].next.match_offset = longest_rep_offset;
+ ctx->optimum[cur_pos].next.link = cur_pos + longest_rep_len;
+ ctx->optimum_end_idx = cur_pos + longest_rep_len;
+
+ lzms_skip_bytes(ctx, longest_rep_len);
+
+ return match;
+ }
+
+ num_matches = lzms_get_matches(ctx, &matches);
+
+ if (num_matches) {
+ longest_len = matches[num_matches - 1].len;
+ if (longest_len >= ctx->params.nice_match_length) {
+ match = lzms_match_chooser_reverse_list(ctx, cur_pos);
+
+ ctx->optimum[cur_pos].next.match_offset =
+ matches[num_matches - 1].offset;
+ ctx->optimum[cur_pos].next.link = cur_pos + longest_len;
+ ctx->optimum_end_idx = cur_pos + longest_len;
+
+ lzms_skip_bytes(ctx, longest_len - 1);
+
+ return match;
+ }
+ } else {
+ longest_len = 1;
+ }
+
+ while (end_pos < cur_pos + longest_len)
+ ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
+
+ state = ctx->optimum[cur_pos].state;
+ cost = ctx->optimum[cur_pos].cost +
+ lzms_get_literal_cost(ctx,
+ &state,
+ *(lz_mf_get_window_ptr(ctx->mf) - 1));
+ if (cost < ctx->optimum[cur_pos + 1].cost) {
+ ctx->optimum[cur_pos + 1].state = state;
+ ctx->optimum[cur_pos + 1].cost = cost;
+ ctx->optimum[cur_pos + 1].prev.link = cur_pos;
+ }
+
+ for (u32 i = 0, len = 2; i < num_matches; i++) {
+ u32 offset = matches[i].offset;
+ struct lzms_adaptive_state state;
+ u32 position_cost;
+
+ state = ctx->optimum[cur_pos].state;
+ position_cost = ctx->optimum[cur_pos].cost;
+ position_cost += lzms_get_lz_match_cost_nolen(ctx, &state, offset);
+
+ do {
+ u32 cost;
+
+ cost = position_cost;
+ cost += lzms_get_length_cost(&ctx->length_encoder, len);
+
+ if (cost < ctx->optimum[cur_pos + len].cost) {
+ ctx->optimum[cur_pos + len].state = state;
+ ctx->optimum[cur_pos + len].prev.link = cur_pos;
+ ctx->optimum[cur_pos + len].prev.match_offset = offset;
+ ctx->optimum[cur_pos + len].cost = cost;
+ }
+ } while (++len <= matches[i].len);
+ }
+
+ if (longest_rep_len >= ctx->params.min_match_length) {
+
+ while (end_pos < cur_pos + longest_rep_len)
+ ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
+
+ state = ctx->optimum[cur_pos].state;
+
+ cost = ctx->optimum[cur_pos].cost +
+ lzms_get_lz_match_cost(ctx,
+ &state,
+ longest_rep_len,
+ longest_rep_offset);
+ if (cost <= ctx->optimum[cur_pos + longest_rep_len].cost) {
+ ctx->optimum[cur_pos + longest_rep_len].state =
+ state;
+ ctx->optimum[cur_pos + longest_rep_len].prev.link =
+ cur_pos;
+ ctx->optimum[cur_pos + longest_rep_len].prev.match_offset =
+ longest_rep_offset;
+ ctx->optimum[cur_pos + longest_rep_len].cost =
+ cost;
+ }
+ }
+ }