12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
32940b44dSPeter Avalos /// \file lzma_encoder_optimum_fast.c
42940b44dSPeter Avalos //
52940b44dSPeter Avalos // Author: Igor Pavlov
62940b44dSPeter Avalos //
72940b44dSPeter Avalos // This file has been put into the public domain.
82940b44dSPeter Avalos // You can do whatever you want with this file.
92940b44dSPeter Avalos //
102940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
112940b44dSPeter Avalos
122940b44dSPeter Avalos #include "lzma_encoder_private.h"
1315ab8c86SJohn Marino #include "memcmplen.h"
142940b44dSPeter Avalos
152940b44dSPeter Avalos
162940b44dSPeter Avalos #define change_pair(small_dist, big_dist) \
172940b44dSPeter Avalos (((big_dist) >> 7) > (small_dist))
182940b44dSPeter Avalos
192940b44dSPeter Avalos
202940b44dSPeter Avalos extern void
lzma_lzma_optimum_fast(lzma_lzma1_encoder * restrict coder,lzma_mf * restrict mf,uint32_t * restrict back_res,uint32_t * restrict len_res)21*46a2189dSzrj lzma_lzma_optimum_fast(lzma_lzma1_encoder *restrict coder,
22*46a2189dSzrj lzma_mf *restrict mf,
232940b44dSPeter Avalos uint32_t *restrict back_res, uint32_t *restrict len_res)
242940b44dSPeter Avalos {
252940b44dSPeter Avalos const uint32_t nice_len = mf->nice_len;
262940b44dSPeter Avalos
272940b44dSPeter Avalos uint32_t len_main;
282940b44dSPeter Avalos uint32_t matches_count;
292940b44dSPeter Avalos if (mf->read_ahead == 0) {
302940b44dSPeter Avalos len_main = mf_find(mf, &matches_count, coder->matches);
312940b44dSPeter Avalos } else {
322940b44dSPeter Avalos assert(mf->read_ahead == 1);
332940b44dSPeter Avalos len_main = coder->longest_match_length;
342940b44dSPeter Avalos matches_count = coder->matches_count;
352940b44dSPeter Avalos }
362940b44dSPeter Avalos
372940b44dSPeter Avalos const uint8_t *buf = mf_ptr(mf) - 1;
382940b44dSPeter Avalos const uint32_t buf_avail = my_min(mf_avail(mf) + 1, MATCH_LEN_MAX);
392940b44dSPeter Avalos
402940b44dSPeter Avalos if (buf_avail < 2) {
412940b44dSPeter Avalos // There's not enough input left to encode a match.
422940b44dSPeter Avalos *back_res = UINT32_MAX;
432940b44dSPeter Avalos *len_res = 1;
442940b44dSPeter Avalos return;
452940b44dSPeter Avalos }
462940b44dSPeter Avalos
472940b44dSPeter Avalos // Look for repeated matches; scan the previous four match distances
482940b44dSPeter Avalos uint32_t rep_len = 0;
492940b44dSPeter Avalos uint32_t rep_index = 0;
502940b44dSPeter Avalos
5115ab8c86SJohn Marino for (uint32_t i = 0; i < REPS; ++i) {
522940b44dSPeter Avalos // Pointer to the beginning of the match candidate
532940b44dSPeter Avalos const uint8_t *const buf_back = buf - coder->reps[i] - 1;
542940b44dSPeter Avalos
552940b44dSPeter Avalos // If the first two bytes (2 == MATCH_LEN_MIN) do not match,
562940b44dSPeter Avalos // this rep is not useful.
572940b44dSPeter Avalos if (not_equal_16(buf, buf_back))
582940b44dSPeter Avalos continue;
592940b44dSPeter Avalos
602940b44dSPeter Avalos // The first two bytes matched.
612940b44dSPeter Avalos // Calculate the length of the match.
6215ab8c86SJohn Marino const uint32_t len = lzma_memcmplen(
6315ab8c86SJohn Marino buf, buf_back, 2, buf_avail);
642940b44dSPeter Avalos
652940b44dSPeter Avalos // If we have found a repeated match that is at least
662940b44dSPeter Avalos // nice_len long, return it immediately.
672940b44dSPeter Avalos if (len >= nice_len) {
682940b44dSPeter Avalos *back_res = i;
692940b44dSPeter Avalos *len_res = len;
702940b44dSPeter Avalos mf_skip(mf, len - 1);
712940b44dSPeter Avalos return;
722940b44dSPeter Avalos }
732940b44dSPeter Avalos
742940b44dSPeter Avalos if (len > rep_len) {
752940b44dSPeter Avalos rep_index = i;
762940b44dSPeter Avalos rep_len = len;
772940b44dSPeter Avalos }
782940b44dSPeter Avalos }
792940b44dSPeter Avalos
802940b44dSPeter Avalos // We didn't find a long enough repeated match. Encode it as a normal
812940b44dSPeter Avalos // match if the match length is at least nice_len.
822940b44dSPeter Avalos if (len_main >= nice_len) {
8315ab8c86SJohn Marino *back_res = coder->matches[matches_count - 1].dist + REPS;
842940b44dSPeter Avalos *len_res = len_main;
852940b44dSPeter Avalos mf_skip(mf, len_main - 1);
862940b44dSPeter Avalos return;
872940b44dSPeter Avalos }
882940b44dSPeter Avalos
892940b44dSPeter Avalos uint32_t back_main = 0;
902940b44dSPeter Avalos if (len_main >= 2) {
912940b44dSPeter Avalos back_main = coder->matches[matches_count - 1].dist;
922940b44dSPeter Avalos
932940b44dSPeter Avalos while (matches_count > 1 && len_main ==
942940b44dSPeter Avalos coder->matches[matches_count - 2].len + 1) {
952940b44dSPeter Avalos if (!change_pair(coder->matches[
962940b44dSPeter Avalos matches_count - 2].dist,
972940b44dSPeter Avalos back_main))
982940b44dSPeter Avalos break;
992940b44dSPeter Avalos
1002940b44dSPeter Avalos --matches_count;
1012940b44dSPeter Avalos len_main = coder->matches[matches_count - 1].len;
1022940b44dSPeter Avalos back_main = coder->matches[matches_count - 1].dist;
1032940b44dSPeter Avalos }
1042940b44dSPeter Avalos
1052940b44dSPeter Avalos if (len_main == 2 && back_main >= 0x80)
1062940b44dSPeter Avalos len_main = 1;
1072940b44dSPeter Avalos }
1082940b44dSPeter Avalos
1092940b44dSPeter Avalos if (rep_len >= 2) {
1102940b44dSPeter Avalos if (rep_len + 1 >= len_main
1112940b44dSPeter Avalos || (rep_len + 2 >= len_main
1122940b44dSPeter Avalos && back_main > (UINT32_C(1) << 9))
1132940b44dSPeter Avalos || (rep_len + 3 >= len_main
1142940b44dSPeter Avalos && back_main > (UINT32_C(1) << 15))) {
1152940b44dSPeter Avalos *back_res = rep_index;
1162940b44dSPeter Avalos *len_res = rep_len;
1172940b44dSPeter Avalos mf_skip(mf, rep_len - 1);
1182940b44dSPeter Avalos return;
1192940b44dSPeter Avalos }
1202940b44dSPeter Avalos }
1212940b44dSPeter Avalos
1222940b44dSPeter Avalos if (len_main < 2 || buf_avail <= 2) {
1232940b44dSPeter Avalos *back_res = UINT32_MAX;
1242940b44dSPeter Avalos *len_res = 1;
1252940b44dSPeter Avalos return;
1262940b44dSPeter Avalos }
1272940b44dSPeter Avalos
1282940b44dSPeter Avalos // Get the matches for the next byte. If we find a better match,
1292940b44dSPeter Avalos // the current byte is encoded as a literal.
1302940b44dSPeter Avalos coder->longest_match_length = mf_find(mf,
1312940b44dSPeter Avalos &coder->matches_count, coder->matches);
1322940b44dSPeter Avalos
1332940b44dSPeter Avalos if (coder->longest_match_length >= 2) {
1342940b44dSPeter Avalos const uint32_t new_dist = coder->matches[
1352940b44dSPeter Avalos coder->matches_count - 1].dist;
1362940b44dSPeter Avalos
1372940b44dSPeter Avalos if ((coder->longest_match_length >= len_main
1382940b44dSPeter Avalos && new_dist < back_main)
1392940b44dSPeter Avalos || (coder->longest_match_length == len_main + 1
1402940b44dSPeter Avalos && !change_pair(back_main, new_dist))
1412940b44dSPeter Avalos || (coder->longest_match_length > len_main + 1)
1422940b44dSPeter Avalos || (coder->longest_match_length + 1 >= len_main
1432940b44dSPeter Avalos && len_main >= 3
1442940b44dSPeter Avalos && change_pair(new_dist, back_main))) {
1452940b44dSPeter Avalos *back_res = UINT32_MAX;
1462940b44dSPeter Avalos *len_res = 1;
1472940b44dSPeter Avalos return;
1482940b44dSPeter Avalos }
1492940b44dSPeter Avalos }
1502940b44dSPeter Avalos
1512940b44dSPeter Avalos // In contrast to LZMA SDK, dictionary could not have been moved
1522940b44dSPeter Avalos // between mf_find() calls, thus it is safe to just increment
1532940b44dSPeter Avalos // the old buf pointer instead of recalculating it with mf_ptr().
1542940b44dSPeter Avalos ++buf;
1552940b44dSPeter Avalos
15615ab8c86SJohn Marino const uint32_t limit = my_max(2, len_main - 1);
1572940b44dSPeter Avalos
15815ab8c86SJohn Marino for (uint32_t i = 0; i < REPS; ++i) {
15915ab8c86SJohn Marino if (memcmp(buf, buf - coder->reps[i] - 1, limit) == 0) {
1602940b44dSPeter Avalos *back_res = UINT32_MAX;
1612940b44dSPeter Avalos *len_res = 1;
1622940b44dSPeter Avalos return;
1632940b44dSPeter Avalos }
1642940b44dSPeter Avalos }
1652940b44dSPeter Avalos
16615ab8c86SJohn Marino *back_res = back_main + REPS;
1672940b44dSPeter Avalos *len_res = len_main;
1682940b44dSPeter Avalos mf_skip(mf, len_main - 2);
1692940b44dSPeter Avalos return;
1702940b44dSPeter Avalos }
171