deflate_medium.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* deflate_medium.c -- The deflate_medium deflate strategy
  2. *
  3. * Copyright (C) 2013 Intel Corporation. All rights reserved.
  4. * Authors:
  5. * Arjan van de Ven <arjan@linux.intel.com>
  6. *
  7. * For conditions of distribution and use, see copyright notice in zlib.h
  8. */
  9. #ifndef NO_MEDIUM_STRATEGY
  10. #include "zbuild.h"
  11. #include "deflate.h"
  12. #include "deflate_p.h"
  13. #include "functable.h"
  14. struct match {
  15. uint16_t match_start;
  16. uint16_t match_length;
  17. uint16_t strstart;
  18. uint16_t orgstart;
  19. };
  20. static int emit_match(deflate_state *s, struct match match) {
  21. int bflush = 0;
  22. /* matches that are not long enough we need to emit as literals */
  23. if (match.match_length < WANT_MIN_MATCH) {
  24. while (match.match_length) {
  25. bflush += zng_tr_tally_lit(s, s->window[match.strstart]);
  26. s->lookahead--;
  27. match.strstart++;
  28. match.match_length--;
  29. }
  30. return bflush;
  31. }
  32. check_match(s, match.strstart, match.match_start, match.match_length);
  33. bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match.match_length - STD_MIN_MATCH);
  34. s->lookahead -= match.match_length;
  35. return bflush;
  36. }
  37. static void insert_match(deflate_state *s, struct match match) {
  38. if (UNLIKELY(s->lookahead <= (unsigned int)(match.match_length + WANT_MIN_MATCH)))
  39. return;
  40. /* string at strstart already in table */
  41. match.strstart++;
  42. match.match_length--;
  43. /* matches that are not long enough we need to emit as literals */
  44. if (LIKELY(match.match_length < WANT_MIN_MATCH - 1)) {
  45. if (UNLIKELY(match.match_length > 0)) {
  46. if (match.strstart >= match.orgstart) {
  47. if (match.strstart + match.match_length - 1 >= match.orgstart) {
  48. insert_string(s, match.strstart, match.match_length);
  49. } else {
  50. insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
  51. }
  52. match.strstart += match.match_length;
  53. match.match_length = 0;
  54. }
  55. }
  56. return;
  57. }
  58. /* Insert into hash table. */
  59. if (LIKELY(match.strstart >= match.orgstart)) {
  60. if (LIKELY(match.strstart + match.match_length - 1 >= match.orgstart)) {
  61. insert_string(s, match.strstart, match.match_length);
  62. } else {
  63. insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
  64. }
  65. } else if (match.orgstart < match.strstart + match.match_length) {
  66. insert_string(s, match.orgstart, match.strstart + match.match_length - match.orgstart);
  67. }
  68. match.strstart += match.match_length;
  69. match.match_length = 0;
  70. }
  71. static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
  72. Pos limit;
  73. unsigned char *match, *orig;
  74. int changed = 0;
  75. struct match c, n;
  76. /* step zero: sanity checks */
  77. if (current->match_length <= 1)
  78. return;
  79. if (UNLIKELY(current->match_length > 1 + next->match_start))
  80. return;
  81. if (UNLIKELY(current->match_length > 1 + next->strstart))
  82. return;
  83. match = s->window - current->match_length + 1 + next->match_start;
  84. orig = s->window - current->match_length + 1 + next->strstart;
  85. /* quick exit check.. if this fails then don't bother with anything else */
  86. if (LIKELY(*match != *orig))
  87. return;
  88. c = *current;
  89. n = *next;
  90. /* step one: try to move the "next" match to the left as much as possible */
  91. limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
  92. match = s->window + n.match_start - 1;
  93. orig = s->window + n.strstart - 1;
  94. while (*match == *orig) {
  95. if (UNLIKELY(c.match_length < 1))
  96. break;
  97. if (UNLIKELY(n.strstart <= limit))
  98. break;
  99. if (UNLIKELY(n.match_length >= 256))
  100. break;
  101. if (UNLIKELY(n.match_start <= 1))
  102. break;
  103. n.strstart--;
  104. n.match_start--;
  105. n.match_length++;
  106. c.match_length--;
  107. match--;
  108. orig--;
  109. changed++;
  110. }
  111. if (!changed)
  112. return;
  113. if (c.match_length <= 1 && n.match_length != 2) {
  114. n.orgstart++;
  115. *current = c;
  116. *next = n;
  117. } else {
  118. return;
  119. }
  120. }
  121. Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
  122. /* Align the first struct to start on a new cacheline, this allows us to fit both structs in one cacheline */
  123. ALIGNED_(16) struct match current_match;
  124. struct match next_match;
  125. /* For levels below 5, don't check the next position for a better match */
  126. int early_exit = s->level < 5;
  127. memset(&current_match, 0, sizeof(struct match));
  128. memset(&next_match, 0, sizeof(struct match));
  129. for (;;) {
  130. Pos hash_head = 0; /* head of the hash chain */
  131. int bflush = 0; /* set if current block must be flushed */
  132. int64_t dist;
  133. /* Make sure that we always have enough lookahead, except
  134. * at the end of the input file. We need STD_MAX_MATCH bytes
  135. * for the next match, plus WANT_MIN_MATCH bytes to insert the
  136. * string following the next current_match.
  137. */
  138. if (s->lookahead < MIN_LOOKAHEAD) {
  139. PREFIX(fill_window)(s);
  140. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  141. return need_more;
  142. }
  143. if (UNLIKELY(s->lookahead == 0))
  144. break; /* flush the current block */
  145. next_match.match_length = 0;
  146. }
  147. /* Insert the string window[strstart .. strstart+2] in the
  148. * dictionary, and set hash_head to the head of the hash chain:
  149. */
  150. /* If we already have a future match from a previous round, just use that */
  151. if (!early_exit && next_match.match_length > 0) {
  152. current_match = next_match;
  153. next_match.match_length = 0;
  154. } else {
  155. hash_head = 0;
  156. if (s->lookahead >= WANT_MIN_MATCH) {
  157. hash_head = quick_insert_string(s, s->strstart);
  158. }
  159. current_match.strstart = (uint16_t)s->strstart;
  160. current_match.orgstart = current_match.strstart;
  161. /* Find the longest match, discarding those <= prev_length.
  162. * At this point we have always match_length < WANT_MIN_MATCH
  163. */
  164. dist = (int64_t)s->strstart - hash_head;
  165. if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
  166. /* To simplify the code, we prevent matches with the string
  167. * of window index 0 (in particular we have to avoid a match
  168. * of the string with itself at the start of the input file).
  169. */
  170. current_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
  171. current_match.match_start = (uint16_t)s->match_start;
  172. if (UNLIKELY(current_match.match_length < WANT_MIN_MATCH))
  173. current_match.match_length = 1;
  174. if (UNLIKELY(current_match.match_start >= current_match.strstart)) {
  175. /* this can happen due to some restarts */
  176. current_match.match_length = 1;
  177. }
  178. } else {
  179. /* Set up the match to be a 1 byte literal */
  180. current_match.match_start = 0;
  181. current_match.match_length = 1;
  182. }
  183. }
  184. insert_match(s, current_match);
  185. /* now, look ahead one */
  186. if (LIKELY(!early_exit && s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
  187. s->strstart = current_match.strstart + current_match.match_length;
  188. hash_head = quick_insert_string(s, s->strstart);
  189. next_match.strstart = (uint16_t)s->strstart;
  190. next_match.orgstart = next_match.strstart;
  191. /* Find the longest match, discarding those <= prev_length.
  192. * At this point we have always match_length < WANT_MIN_MATCH
  193. */
  194. dist = (int64_t)s->strstart - hash_head;
  195. if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
  196. /* To simplify the code, we prevent matches with the string
  197. * of window index 0 (in particular we have to avoid a match
  198. * of the string with itself at the start of the input file).
  199. */
  200. next_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
  201. next_match.match_start = (uint16_t)s->match_start;
  202. if (UNLIKELY(next_match.match_start >= next_match.strstart)) {
  203. /* this can happen due to some restarts */
  204. next_match.match_length = 1;
  205. }
  206. if (next_match.match_length < WANT_MIN_MATCH)
  207. next_match.match_length = 1;
  208. else
  209. fizzle_matches(s, &current_match, &next_match);
  210. } else {
  211. /* Set up the match to be a 1 byte literal */
  212. next_match.match_start = 0;
  213. next_match.match_length = 1;
  214. }
  215. s->strstart = current_match.strstart;
  216. } else {
  217. next_match.match_length = 0;
  218. }
  219. /* now emit the current match */
  220. bflush = emit_match(s, current_match);
  221. /* move the "cursor" forward */
  222. s->strstart += current_match.match_length;
  223. if (UNLIKELY(bflush))
  224. FLUSH_BLOCK(s, 0);
  225. }
  226. s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
  227. if (flush == Z_FINISH) {
  228. FLUSH_BLOCK(s, 1);
  229. return finish_done;
  230. }
  231. if (UNLIKELY(s->sym_next))
  232. FLUSH_BLOCK(s, 0);
  233. return block_done;
  234. }
  235. #endif