match.S 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* match.S -- x86 assembly version of the zlib longest_match() function.
  2. * Optimized for the Intel 686 chips (PPro and later).
  3. *
  4. * Copyright (C) 1998, 2007 Brian Raiter <breadbox@muppetlabs.com>
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the author be held liable for any damages
  8. * arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute it
  12. * freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must not
  15. * claim that you wrote the original software. If you use this software
  16. * in a product, an acknowledgment in the product documentation would be
  17. * appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. #ifndef NO_UNDERLINE
  23. #define match_init _match_init
  24. #define longest_match _longest_match
  25. #endif
  26. #define MAX_MATCH (258)
  27. #define MIN_MATCH (3)
  28. #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  29. #define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
  30. /* stack frame offsets */
  31. #define chainlenwmask 0 /* high word: current chain len */
  32. /* low word: s->wmask */
  33. #define window 4 /* local copy of s->window */
  34. #define windowbestlen 8 /* s->window + bestlen */
  35. #define scanstart 16 /* first two bytes of string */
  36. #define scanend 12 /* last two bytes of string */
  37. #define scanalign 20 /* dword-misalignment of string */
  38. #define nicematch 24 /* a good enough match size */
  39. #define bestlen 28 /* size of best match so far */
  40. #define scan 32 /* ptr to string wanting match */
  41. #define LocalVarsSize (36)
  42. /* saved ebx 36 */
  43. /* saved edi 40 */
  44. /* saved esi 44 */
  45. /* saved ebp 48 */
  46. /* return address 52 */
  47. #define deflatestate 56 /* the function arguments */
  48. #define curmatch 60
  49. /* All the +zlib1222add offsets are due to the addition of fields
  50. * in zlib in the deflate_state structure since the asm code was first written
  51. * (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)").
  52. * (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0").
  53. * if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8").
  54. */
  55. #define zlib1222add (8)
  56. #define dsWSize (36+zlib1222add)
  57. #define dsWMask (44+zlib1222add)
  58. #define dsWindow (48+zlib1222add)
  59. #define dsPrev (56+zlib1222add)
  60. #define dsMatchLen (88+zlib1222add)
  61. #define dsPrevMatch (92+zlib1222add)
  62. #define dsStrStart (100+zlib1222add)
  63. #define dsMatchStart (104+zlib1222add)
  64. #define dsLookahead (108+zlib1222add)
  65. #define dsPrevLen (112+zlib1222add)
  66. #define dsMaxChainLen (116+zlib1222add)
  67. #define dsGoodMatch (132+zlib1222add)
  68. #define dsNiceMatch (136+zlib1222add)
  69. .file "match.S"
  70. .globl match_init, longest_match
  71. .text
  72. /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
  73. longest_match:
  74. /* Save registers that the compiler may be using, and adjust %esp to */
  75. /* make room for our stack frame. */
  76. pushl %ebp
  77. pushl %edi
  78. pushl %esi
  79. pushl %ebx
  80. subl $LocalVarsSize, %esp
  81. /* Retrieve the function arguments. %ecx will hold cur_match */
  82. /* throughout the entire function. %edx will hold the pointer to the */
  83. /* deflate_state structure during the function's setup (before */
  84. /* entering the main loop). */
  85. movl deflatestate(%esp), %edx
  86. movl curmatch(%esp), %ecx
  87. /* uInt wmask = s->w_mask; */
  88. /* unsigned chain_length = s->max_chain_length; */
  89. /* if (s->prev_length >= s->good_match) { */
  90. /* chain_length >>= 2; */
  91. /* } */
  92. movl dsPrevLen(%edx), %eax
  93. movl dsGoodMatch(%edx), %ebx
  94. cmpl %ebx, %eax
  95. movl dsWMask(%edx), %eax
  96. movl dsMaxChainLen(%edx), %ebx
  97. jl LastMatchGood
  98. shrl $2, %ebx
  99. LastMatchGood:
  100. /* chainlen is decremented once beforehand so that the function can */
  101. /* use the sign flag instead of the zero flag for the exit test. */
  102. /* It is then shifted into the high word, to make room for the wmask */
  103. /* value, which it will always accompany. */
  104. decl %ebx
  105. shll $16, %ebx
  106. orl %eax, %ebx
  107. movl %ebx, chainlenwmask(%esp)
  108. /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  109. movl dsNiceMatch(%edx), %eax
  110. movl dsLookahead(%edx), %ebx
  111. cmpl %eax, %ebx
  112. jl LookaheadLess
  113. movl %eax, %ebx
  114. LookaheadLess: movl %ebx, nicematch(%esp)
  115. /* register Bytef *scan = s->window + s->strstart; */
  116. movl dsWindow(%edx), %esi
  117. movl %esi, window(%esp)
  118. movl dsStrStart(%edx), %ebp
  119. lea (%esi,%ebp), %edi
  120. movl %edi, scan(%esp)
  121. /* Determine how many bytes the scan ptr is off from being */
  122. /* dword-aligned. */
  123. movl %edi, %eax
  124. negl %eax
  125. andl $3, %eax
  126. movl %eax, scanalign(%esp)
  127. /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  128. /* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  129. movl dsWSize(%edx), %eax
  130. subl $MIN_LOOKAHEAD, %eax
  131. subl %eax, %ebp
  132. jg LimitPositive
  133. xorl %ebp, %ebp
  134. LimitPositive:
  135. /* int best_len = s->prev_length; */
  136. movl dsPrevLen(%edx), %eax
  137. movl %eax, bestlen(%esp)
  138. /* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  139. addl %eax, %esi
  140. movl %esi, windowbestlen(%esp)
  141. /* register ush scan_start = *(ushf*)scan; */
  142. /* register ush scan_end = *(ushf*)(scan+best_len-1); */
  143. /* Posf *prev = s->prev; */
  144. movzwl (%edi), %ebx
  145. movl %ebx, scanstart(%esp)
  146. movzwl -1(%edi,%eax), %ebx
  147. movl %ebx, scanend(%esp)
  148. movl dsPrev(%edx), %edi
  149. /* Jump into the main loop. */
  150. movl chainlenwmask(%esp), %edx
  151. jmp LoopEntry
  152. .balign 16
  153. /* do {
  154. * match = s->window + cur_match;
  155. * if (*(ushf*)(match+best_len-1) != scan_end ||
  156. * *(ushf*)match != scan_start) continue;
  157. * [...]
  158. * } while ((cur_match = prev[cur_match & wmask]) > limit
  159. * && --chain_length != 0);
  160. *
  161. * Here is the inner loop of the function. The function will spend the
  162. * majority of its time in this loop, and majority of that time will
  163. * be spent in the first ten instructions.
  164. *
  165. * Within this loop:
  166. * %ebx = scanend
  167. * %ecx = curmatch
  168. * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
  169. * %esi = windowbestlen - i.e., (window + bestlen)
  170. * %edi = prev
  171. * %ebp = limit
  172. */
  173. LookupLoop:
  174. andl %edx, %ecx
  175. movzwl (%edi,%ecx,2), %ecx
  176. cmpl %ebp, %ecx
  177. jbe LeaveNow
  178. subl $0x00010000, %edx
  179. js LeaveNow
  180. LoopEntry: movzwl -1(%esi,%ecx), %eax
  181. cmpl %ebx, %eax
  182. jnz LookupLoop
  183. movl window(%esp), %eax
  184. movzwl (%eax,%ecx), %eax
  185. cmpl scanstart(%esp), %eax
  186. jnz LookupLoop
  187. /* Store the current value of chainlen. */
  188. movl %edx, chainlenwmask(%esp)
  189. /* Point %edi to the string under scrutiny, and %esi to the string we */
  190. /* are hoping to match it up with. In actuality, %esi and %edi are */
  191. /* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  192. /* initialized to -(MAX_MATCH_8 - scanalign). */
  193. movl window(%esp), %esi
  194. movl scan(%esp), %edi
  195. addl %ecx, %esi
  196. movl scanalign(%esp), %eax
  197. movl $(-MAX_MATCH_8), %edx
  198. lea MAX_MATCH_8(%edi,%eax), %edi
  199. lea MAX_MATCH_8(%esi,%eax), %esi
  200. /* Test the strings for equality, 8 bytes at a time. At the end,
  201. * adjust %edx so that it is offset to the exact byte that mismatched.
  202. *
  203. * We already know at this point that the first three bytes of the
  204. * strings match each other, and they can be safely passed over before
  205. * starting the compare loop. So what this code does is skip over 0-3
  206. * bytes, as much as necessary in order to dword-align the %edi
  207. * pointer. (%esi will still be misaligned three times out of four.)
  208. *
  209. * It should be confessed that this loop usually does not represent
  210. * much of the total running time. Replacing it with a more
  211. * straightforward "rep cmpsb" would not drastically degrade
  212. * performance.
  213. */
  214. LoopCmps:
  215. movl (%esi,%edx), %eax
  216. xorl (%edi,%edx), %eax
  217. jnz LeaveLoopCmps
  218. movl 4(%esi,%edx), %eax
  219. xorl 4(%edi,%edx), %eax
  220. jnz LeaveLoopCmps4
  221. addl $8, %edx
  222. jnz LoopCmps
  223. jmp LenMaximum
  224. LeaveLoopCmps4: addl $4, %edx
  225. LeaveLoopCmps: testl $0x0000FFFF, %eax
  226. jnz LenLower
  227. addl $2, %edx
  228. shrl $16, %eax
  229. LenLower: subb $1, %al
  230. adcl $0, %edx
  231. /* Calculate the length of the match. If it is longer than MAX_MATCH, */
  232. /* then automatically accept it as the best possible match and leave. */
  233. lea (%edi,%edx), %eax
  234. movl scan(%esp), %edi
  235. subl %edi, %eax
  236. cmpl $MAX_MATCH, %eax
  237. jge LenMaximum
  238. /* If the length of the match is not longer than the best match we */
  239. /* have so far, then forget it and return to the lookup loop. */
  240. movl deflatestate(%esp), %edx
  241. movl bestlen(%esp), %ebx
  242. cmpl %ebx, %eax
  243. jg LongerMatch
  244. movl windowbestlen(%esp), %esi
  245. movl dsPrev(%edx), %edi
  246. movl scanend(%esp), %ebx
  247. movl chainlenwmask(%esp), %edx
  248. jmp LookupLoop
  249. /* s->match_start = cur_match; */
  250. /* best_len = len; */
  251. /* if (len >= nice_match) break; */
  252. /* scan_end = *(ushf*)(scan+best_len-1); */
  253. LongerMatch: movl nicematch(%esp), %ebx
  254. movl %eax, bestlen(%esp)
  255. movl %ecx, dsMatchStart(%edx)
  256. cmpl %ebx, %eax
  257. jge LeaveNow
  258. movl window(%esp), %esi
  259. addl %eax, %esi
  260. movl %esi, windowbestlen(%esp)
  261. movzwl -1(%edi,%eax), %ebx
  262. movl dsPrev(%edx), %edi
  263. movl %ebx, scanend(%esp)
  264. movl chainlenwmask(%esp), %edx
  265. jmp LookupLoop
  266. /* Accept the current string, with the maximum possible length. */
  267. LenMaximum: movl deflatestate(%esp), %edx
  268. movl $MAX_MATCH, bestlen(%esp)
  269. movl %ecx, dsMatchStart(%edx)
  270. /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  271. /* return s->lookahead; */
  272. LeaveNow:
  273. movl deflatestate(%esp), %edx
  274. movl bestlen(%esp), %ebx
  275. movl dsLookahead(%edx), %eax
  276. cmpl %eax, %ebx
  277. jg LookaheadRet
  278. movl %ebx, %eax
  279. LookaheadRet:
  280. /* Restore the stack and return from whence we came. */
  281. addl $LocalVarsSize, %esp
  282. popl %ebx
  283. popl %esi
  284. popl %edi
  285. popl %ebp
  286. match_init: ret