annotate src/zlib-1.2.8/contrib/asm686/match.S @ 169:223a55898ab9 tip default

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