match.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. .cfi_sections .debug_frame
  74. longest_match:
  75. .cfi_startproc
  76. /* Save registers that the compiler may be using, and adjust %esp to */
  77. /* make room for our stack frame. */
  78. pushl %ebp
  79. .cfi_def_cfa_offset 8
  80. .cfi_offset ebp, -8
  81. pushl %edi
  82. .cfi_def_cfa_offset 12
  83. pushl %esi
  84. .cfi_def_cfa_offset 16
  85. pushl %ebx
  86. .cfi_def_cfa_offset 20
  87. subl $LocalVarsSize, %esp
  88. .cfi_def_cfa_offset LocalVarsSize+20
  89. /* Retrieve the function arguments. %ecx will hold cur_match */
  90. /* throughout the entire function. %edx will hold the pointer to the */
  91. /* deflate_state structure during the function's setup (before */
  92. /* entering the main loop). */
  93. movl deflatestate(%esp), %edx
  94. movl curmatch(%esp), %ecx
  95. /* uInt wmask = s->w_mask; */
  96. /* unsigned chain_length = s->max_chain_length; */
  97. /* if (s->prev_length >= s->good_match) { */
  98. /* chain_length >>= 2; */
  99. /* } */
  100. movl dsPrevLen(%edx), %eax
  101. movl dsGoodMatch(%edx), %ebx
  102. cmpl %ebx, %eax
  103. movl dsWMask(%edx), %eax
  104. movl dsMaxChainLen(%edx), %ebx
  105. jl LastMatchGood
  106. shrl $2, %ebx
  107. LastMatchGood:
  108. /* chainlen is decremented once beforehand so that the function can */
  109. /* use the sign flag instead of the zero flag for the exit test. */
  110. /* It is then shifted into the high word, to make room for the wmask */
  111. /* value, which it will always accompany. */
  112. decl %ebx
  113. shll $16, %ebx
  114. orl %eax, %ebx
  115. movl %ebx, chainlenwmask(%esp)
  116. /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  117. movl dsNiceMatch(%edx), %eax
  118. movl dsLookahead(%edx), %ebx
  119. cmpl %eax, %ebx
  120. jl LookaheadLess
  121. movl %eax, %ebx
  122. LookaheadLess: movl %ebx, nicematch(%esp)
  123. /* register Bytef *scan = s->window + s->strstart; */
  124. movl dsWindow(%edx), %esi
  125. movl %esi, window(%esp)
  126. movl dsStrStart(%edx), %ebp
  127. lea (%esi,%ebp), %edi
  128. movl %edi, scan(%esp)
  129. /* Determine how many bytes the scan ptr is off from being */
  130. /* dword-aligned. */
  131. movl %edi, %eax
  132. negl %eax
  133. andl $3, %eax
  134. movl %eax, scanalign(%esp)
  135. /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  136. /* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  137. movl dsWSize(%edx), %eax
  138. subl $MIN_LOOKAHEAD, %eax
  139. subl %eax, %ebp
  140. jg LimitPositive
  141. xorl %ebp, %ebp
  142. LimitPositive:
  143. /* int best_len = s->prev_length; */
  144. movl dsPrevLen(%edx), %eax
  145. movl %eax, bestlen(%esp)
  146. /* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  147. addl %eax, %esi
  148. movl %esi, windowbestlen(%esp)
  149. /* register ush scan_start = *(ushf*)scan; */
  150. /* register ush scan_end = *(ushf*)(scan+best_len-1); */
  151. /* Posf *prev = s->prev; */
  152. movzwl (%edi), %ebx
  153. movl %ebx, scanstart(%esp)
  154. movzwl -1(%edi,%eax), %ebx
  155. movl %ebx, scanend(%esp)
  156. movl dsPrev(%edx), %edi
  157. /* Jump into the main loop. */
  158. movl chainlenwmask(%esp), %edx
  159. jmp LoopEntry
  160. .balign 16
  161. /* do {
  162. * match = s->window + cur_match;
  163. * if (*(ushf*)(match+best_len-1) != scan_end ||
  164. * *(ushf*)match != scan_start) continue;
  165. * [...]
  166. * } while ((cur_match = prev[cur_match & wmask]) > limit
  167. * && --chain_length != 0);
  168. *
  169. * Here is the inner loop of the function. The function will spend the
  170. * majority of its time in this loop, and majority of that time will
  171. * be spent in the first ten instructions.
  172. *
  173. * Within this loop:
  174. * %ebx = scanend
  175. * %ecx = curmatch
  176. * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
  177. * %esi = windowbestlen - i.e., (window + bestlen)
  178. * %edi = prev
  179. * %ebp = limit
  180. */
  181. LookupLoop:
  182. andl %edx, %ecx
  183. movzwl (%edi,%ecx,2), %ecx
  184. cmpl %ebp, %ecx
  185. jbe LeaveNow
  186. subl $0x00010000, %edx
  187. js LeaveNow
  188. LoopEntry: movzwl -1(%esi,%ecx), %eax
  189. cmpl %ebx, %eax
  190. jnz LookupLoop
  191. movl window(%esp), %eax
  192. movzwl (%eax,%ecx), %eax
  193. cmpl scanstart(%esp), %eax
  194. jnz LookupLoop
  195. /* Store the current value of chainlen. */
  196. movl %edx, chainlenwmask(%esp)
  197. /* Point %edi to the string under scrutiny, and %esi to the string we */
  198. /* are hoping to match it up with. In actuality, %esi and %edi are */
  199. /* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  200. /* initialized to -(MAX_MATCH_8 - scanalign). */
  201. movl window(%esp), %esi
  202. movl scan(%esp), %edi
  203. addl %ecx, %esi
  204. movl scanalign(%esp), %eax
  205. movl $(-MAX_MATCH_8), %edx
  206. lea MAX_MATCH_8(%edi,%eax), %edi
  207. lea MAX_MATCH_8(%esi,%eax), %esi
  208. /* Test the strings for equality, 8 bytes at a time. At the end,
  209. * adjust %edx so that it is offset to the exact byte that mismatched.
  210. *
  211. * We already know at this point that the first three bytes of the
  212. * strings match each other, and they can be safely passed over before
  213. * starting the compare loop. So what this code does is skip over 0-3
  214. * bytes, as much as necessary in order to dword-align the %edi
  215. * pointer. (%esi will still be misaligned three times out of four.)
  216. *
  217. * It should be confessed that this loop usually does not represent
  218. * much of the total running time. Replacing it with a more
  219. * straightforward "rep cmpsb" would not drastically degrade
  220. * performance.
  221. */
  222. LoopCmps:
  223. movl (%esi,%edx), %eax
  224. xorl (%edi,%edx), %eax
  225. jnz LeaveLoopCmps
  226. movl 4(%esi,%edx), %eax
  227. xorl 4(%edi,%edx), %eax
  228. jnz LeaveLoopCmps4
  229. addl $8, %edx
  230. jnz LoopCmps
  231. jmp LenMaximum
  232. LeaveLoopCmps4: addl $4, %edx
  233. LeaveLoopCmps: testl $0x0000FFFF, %eax
  234. jnz LenLower
  235. addl $2, %edx
  236. shrl $16, %eax
  237. LenLower: subb $1, %al
  238. adcl $0, %edx
  239. /* Calculate the length of the match. If it is longer than MAX_MATCH, */
  240. /* then automatically accept it as the best possible match and leave. */
  241. lea (%edi,%edx), %eax
  242. movl scan(%esp), %edi
  243. subl %edi, %eax
  244. cmpl $MAX_MATCH, %eax
  245. jge LenMaximum
  246. /* If the length of the match is not longer than the best match we */
  247. /* have so far, then forget it and return to the lookup loop. */
  248. movl deflatestate(%esp), %edx
  249. movl bestlen(%esp), %ebx
  250. cmpl %ebx, %eax
  251. jg LongerMatch
  252. movl windowbestlen(%esp), %esi
  253. movl dsPrev(%edx), %edi
  254. movl scanend(%esp), %ebx
  255. movl chainlenwmask(%esp), %edx
  256. jmp LookupLoop
  257. /* s->match_start = cur_match; */
  258. /* best_len = len; */
  259. /* if (len >= nice_match) break; */
  260. /* scan_end = *(ushf*)(scan+best_len-1); */
  261. LongerMatch: movl nicematch(%esp), %ebx
  262. movl %eax, bestlen(%esp)
  263. movl %ecx, dsMatchStart(%edx)
  264. cmpl %ebx, %eax
  265. jge LeaveNow
  266. movl window(%esp), %esi
  267. addl %eax, %esi
  268. movl %esi, windowbestlen(%esp)
  269. movzwl -1(%edi,%eax), %ebx
  270. movl dsPrev(%edx), %edi
  271. movl %ebx, scanend(%esp)
  272. movl chainlenwmask(%esp), %edx
  273. jmp LookupLoop
  274. /* Accept the current string, with the maximum possible length. */
  275. LenMaximum: movl deflatestate(%esp), %edx
  276. movl $MAX_MATCH, bestlen(%esp)
  277. movl %ecx, dsMatchStart(%edx)
  278. /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  279. /* return s->lookahead; */
  280. LeaveNow:
  281. movl deflatestate(%esp), %edx
  282. movl bestlen(%esp), %ebx
  283. movl dsLookahead(%edx), %eax
  284. cmpl %eax, %ebx
  285. jg LookaheadRet
  286. movl %ebx, %eax
  287. LookaheadRet:
  288. /* Restore the stack and return from whence we came. */
  289. addl $LocalVarsSize, %esp
  290. .cfi_def_cfa_offset 20
  291. popl %ebx
  292. .cfi_def_cfa_offset 16
  293. popl %esi
  294. .cfi_def_cfa_offset 12
  295. popl %edi
  296. .cfi_def_cfa_offset 8
  297. popl %ebp
  298. .cfi_def_cfa_offset 4
  299. .cfi_endproc
  300. match_init: ret