annotate src/zlib-1.2.8/contrib/asm686/match.S @ 81:7029a4916348

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