+ lzms_encode_lz_explicit_offset_match(c, longest_len,
+ c->matches[num_matches - 1].offset);
+
+ c->optimum[0].state = cur_optimum_ptr->state;
+
+ lzms_update_main_state(&c->optimum[0].state, 1);
+ lzms_update_match_state(&c->optimum[0].state, 0);
+ lzms_update_lz_match_state(&c->optimum[0].state, 0);
+
+ c->optimum[0].state.lru.upcoming_offset =
+ c->matches[num_matches - 1].offset;
+
+ lzms_update_lz_lru_queue(&c->optimum[0].state.lru);
+ goto begin;
+ }
+
+ /* If reaching any positions for the first time,
+ * initialize their costs to "infinity". */
+ while (end_optimum_ptr < cur_optimum_ptr + longest_len)
+ (++end_optimum_ptr)->cost = MC_INFINITE_COST;
+
+ /* Consider coding an explicit offset match. */
+ lzms_consider_lz_explicit_offset_matches(c, cur_optimum_ptr,
+ c->matches, num_matches);
+ } else {
+ /* No matches found. The only choice at this position
+ * is to code a literal. */
+
+ if (end_optimum_ptr == cur_optimum_ptr)
+ (++end_optimum_ptr)->cost = MC_INFINITE_COST;
+ }
+
+ /* Consider coding a literal.
+
+ * To avoid an extra unpredictable brench, actually checking the
+ * preferability of coding a literal is integrated into the
+ * adaptive state update code below. */
+ literal = *window_ptr++;
+ cost = cur_optimum_ptr->cost +
+ lzms_literal_cost(c, literal, &cur_optimum_ptr->state);
+
+ /* Advance to the next position. */
+ cur_optimum_ptr++;
+
+ /* The lowest-cost path to the current position is now known.
+ * Finalize the adaptive state that results from taking this
+ * lowest-cost path. */
+
+ if (cost < cur_optimum_ptr->cost) {
+ /* Literal */
+ cur_optimum_ptr->cost = cost;
+ cur_optimum_ptr->mc_item_data = ((u64)literal << MC_OFFSET_SHIFT) | 1;
+
+ cur_optimum_ptr->state = (cur_optimum_ptr - 1)->state;
+
+ lzms_update_main_state(&cur_optimum_ptr->state, 0);
+
+ cur_optimum_ptr->state.lru.upcoming_offset = 0;
+ } else {
+ /* LZ match */
+ len = cur_optimum_ptr->mc_item_data & MC_LEN_MASK;
+ offset_data = cur_optimum_ptr->mc_item_data >> MC_OFFSET_SHIFT;
+
+ cur_optimum_ptr->state = (cur_optimum_ptr - len)->state;
+
+ lzms_update_main_state(&cur_optimum_ptr->state, 1);
+ lzms_update_match_state(&cur_optimum_ptr->state, 0);
+
+ if (offset_data >= LZMS_NUM_RECENT_OFFSETS) {
+
+ /* Explicit offset LZ match */
+
+ lzms_update_lz_match_state(&cur_optimum_ptr->state, 0);
+
+ cur_optimum_ptr->state.lru.upcoming_offset =
+ offset_data - LZMS_OFFSET_OFFSET;
+ } else {
+ /* Repeat offset LZ match */
+
+ lzms_update_lz_match_state(&cur_optimum_ptr->state, 1);
+ lzms_update_lz_repeat_match_state(&cur_optimum_ptr->state,
+ offset_data);
+
+ cur_optimum_ptr->state.lru.upcoming_offset =
+ cur_optimum_ptr->state.lru.recent_offsets[offset_data];
+
+ for (i = offset_data; i < LZMS_NUM_RECENT_OFFSETS; i++)
+ cur_optimum_ptr->state.lru.recent_offsets[i] =
+ cur_optimum_ptr->state.lru.recent_offsets[i + 1];
+ }
+ }
+
+ lzms_update_lz_lru_queue(&cur_optimum_ptr->state.lru);
+
+ /*
+ * This loop will terminate when either of the following
+ * conditions is true:
+ *
+ * (1) cur_optimum_ptr == end_optimum_ptr
+ *
+ * There are no paths that extend beyond the current
+ * position. In this case, any path to a later position
+ * must pass through the current position, so we can go
+ * ahead and choose the list of items that led to this
+ * position.
+ *
+ * (2) cur_optimum_ptr == c->optimum_end
+ *
+ * This bounds the number of times the algorithm can step
+ * forward before it is guaranteed to start choosing items.
+ * This limits the memory usage. It also guarantees that
+ * the parser will not go too long without updating the
+ * probability tables.
+ *
+ * Note: no check for end-of-window is needed because
+ * end-of-window will trigger condition (1).
+ */
+ if (cur_optimum_ptr == end_optimum_ptr ||
+ cur_optimum_ptr == c->optimum_end)
+ {
+ c->optimum[0].state = cur_optimum_ptr->state;
+ break;
+ }