cannam@128: /* cannam@128: * match.S -- optimized version of longest_match() cannam@128: * based on the similar work by Gilles Vollant, and Brian Raiter, written 1998 cannam@128: * cannam@128: * This is free software; you can redistribute it and/or modify it cannam@128: * under the terms of the BSD License. Use by owners of Che Guevarra cannam@128: * parafernalia is prohibited, where possible, and highly discouraged cannam@128: * elsewhere. cannam@128: */ cannam@128: cannam@128: #ifndef NO_UNDERLINE cannam@128: # define match_init _match_init cannam@128: # define longest_match _longest_match cannam@128: #endif cannam@128: cannam@128: #define scanend ebx cannam@128: #define scanendw bx cannam@128: #define chainlenwmask edx /* high word: current chain len low word: s->wmask */ cannam@128: #define curmatch rsi cannam@128: #define curmatchd esi cannam@128: #define windowbestlen r8 cannam@128: #define scanalign r9 cannam@128: #define scanalignd r9d cannam@128: #define window r10 cannam@128: #define bestlen r11 cannam@128: #define bestlend r11d cannam@128: #define scanstart r12d cannam@128: #define scanstartw r12w cannam@128: #define scan r13 cannam@128: #define nicematch r14d cannam@128: #define limit r15 cannam@128: #define limitd r15d cannam@128: #define prev rcx cannam@128: cannam@128: /* cannam@128: * The 258 is a "magic number, not a parameter -- changing it cannam@128: * breaks the hell loose cannam@128: */ cannam@128: #define MAX_MATCH (258) cannam@128: #define MIN_MATCH (3) cannam@128: #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1) cannam@128: #define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7) cannam@128: cannam@128: /* stack frame offsets */ cannam@128: #define LocalVarsSize (112) cannam@128: #define _chainlenwmask ( 8-LocalVarsSize)(%rsp) cannam@128: #define _windowbestlen (16-LocalVarsSize)(%rsp) cannam@128: #define save_r14 (24-LocalVarsSize)(%rsp) cannam@128: #define save_rsi (32-LocalVarsSize)(%rsp) cannam@128: #define save_rbx (40-LocalVarsSize)(%rsp) cannam@128: #define save_r12 (56-LocalVarsSize)(%rsp) cannam@128: #define save_r13 (64-LocalVarsSize)(%rsp) cannam@128: #define save_r15 (80-LocalVarsSize)(%rsp) cannam@128: cannam@128: cannam@128: .globl match_init, longest_match cannam@128: cannam@128: /* cannam@128: * On AMD64 the first argument of a function (in our case -- the pointer to cannam@128: * deflate_state structure) is passed in %rdi, hence our offsets below are cannam@128: * all off of that. cannam@128: */ cannam@128: cannam@128: /* you can check the structure offset by running cannam@128: cannam@128: #include cannam@128: #include cannam@128: #include "deflate.h" cannam@128: cannam@128: void print_depl() cannam@128: { cannam@128: deflate_state ds; cannam@128: deflate_state *s=&ds; cannam@128: printf("size pointer=%u\n",(int)sizeof(void*)); cannam@128: cannam@128: printf("#define dsWSize (%3u)(%%rdi)\n",(int)(((char*)&(s->w_size))-((char*)s))); cannam@128: printf("#define dsWMask (%3u)(%%rdi)\n",(int)(((char*)&(s->w_mask))-((char*)s))); cannam@128: printf("#define dsWindow (%3u)(%%rdi)\n",(int)(((char*)&(s->window))-((char*)s))); cannam@128: printf("#define dsPrev (%3u)(%%rdi)\n",(int)(((char*)&(s->prev))-((char*)s))); cannam@128: printf("#define dsMatchLen (%3u)(%%rdi)\n",(int)(((char*)&(s->match_length))-((char*)s))); cannam@128: printf("#define dsPrevMatch (%3u)(%%rdi)\n",(int)(((char*)&(s->prev_match))-((char*)s))); cannam@128: printf("#define dsStrStart (%3u)(%%rdi)\n",(int)(((char*)&(s->strstart))-((char*)s))); cannam@128: printf("#define dsMatchStart (%3u)(%%rdi)\n",(int)(((char*)&(s->match_start))-((char*)s))); cannam@128: printf("#define dsLookahead (%3u)(%%rdi)\n",(int)(((char*)&(s->lookahead))-((char*)s))); cannam@128: printf("#define dsPrevLen (%3u)(%%rdi)\n",(int)(((char*)&(s->prev_length))-((char*)s))); cannam@128: printf("#define dsMaxChainLen (%3u)(%%rdi)\n",(int)(((char*)&(s->max_chain_length))-((char*)s))); cannam@128: printf("#define dsGoodMatch (%3u)(%%rdi)\n",(int)(((char*)&(s->good_match))-((char*)s))); cannam@128: printf("#define dsNiceMatch (%3u)(%%rdi)\n",(int)(((char*)&(s->nice_match))-((char*)s))); cannam@128: } cannam@128: cannam@128: */ cannam@128: cannam@128: cannam@128: /* cannam@128: to compile for XCode 3.2 on MacOSX x86_64 cannam@128: - run "gcc -g -c -DXCODE_MAC_X64_STRUCTURE amd64-match.S" cannam@128: */ cannam@128: cannam@128: cannam@128: #ifndef CURRENT_LINX_XCODE_MAC_X64_STRUCTURE cannam@128: #define dsWSize ( 68)(%rdi) cannam@128: #define dsWMask ( 76)(%rdi) cannam@128: #define dsWindow ( 80)(%rdi) cannam@128: #define dsPrev ( 96)(%rdi) cannam@128: #define dsMatchLen (144)(%rdi) cannam@128: #define dsPrevMatch (148)(%rdi) cannam@128: #define dsStrStart (156)(%rdi) cannam@128: #define dsMatchStart (160)(%rdi) cannam@128: #define dsLookahead (164)(%rdi) cannam@128: #define dsPrevLen (168)(%rdi) cannam@128: #define dsMaxChainLen (172)(%rdi) cannam@128: #define dsGoodMatch (188)(%rdi) cannam@128: #define dsNiceMatch (192)(%rdi) cannam@128: cannam@128: #else cannam@128: cannam@128: #ifndef STRUCT_OFFSET cannam@128: # define STRUCT_OFFSET (0) cannam@128: #endif cannam@128: cannam@128: cannam@128: #define dsWSize ( 56 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsWMask ( 64 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsWindow ( 72 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsPrev ( 88 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsMatchLen (136 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsPrevMatch (140 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsStrStart (148 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsMatchStart (152 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsLookahead (156 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsPrevLen (160 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsMaxChainLen (164 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsGoodMatch (180 + STRUCT_OFFSET)(%rdi) cannam@128: #define dsNiceMatch (184 + STRUCT_OFFSET)(%rdi) cannam@128: cannam@128: #endif cannam@128: cannam@128: cannam@128: cannam@128: cannam@128: .text cannam@128: cannam@128: /* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */ cannam@128: cannam@128: longest_match: cannam@128: /* cannam@128: * Retrieve the function arguments. %curmatch will hold cur_match cannam@128: * throughout the entire function (passed via rsi on amd64). cannam@128: * rdi will hold the pointer to the deflate_state (first arg on amd64) cannam@128: */ cannam@128: mov %rsi, save_rsi cannam@128: mov %rbx, save_rbx cannam@128: mov %r12, save_r12 cannam@128: mov %r13, save_r13 cannam@128: mov %r14, save_r14 cannam@128: mov %r15, save_r15 cannam@128: cannam@128: /* uInt wmask = s->w_mask; */ cannam@128: /* unsigned chain_length = s->max_chain_length; */ cannam@128: /* if (s->prev_length >= s->good_match) { */ cannam@128: /* chain_length >>= 2; */ cannam@128: /* } */ cannam@128: cannam@128: movl dsPrevLen, %eax cannam@128: movl dsGoodMatch, %ebx cannam@128: cmpl %ebx, %eax cannam@128: movl dsWMask, %eax cannam@128: movl dsMaxChainLen, %chainlenwmask cannam@128: jl LastMatchGood cannam@128: shrl $2, %chainlenwmask cannam@128: LastMatchGood: cannam@128: cannam@128: /* chainlen is decremented once beforehand so that the function can */ cannam@128: /* use the sign flag instead of the zero flag for the exit test. */ cannam@128: /* It is then shifted into the high word, to make room for the wmask */ cannam@128: /* value, which it will always accompany. */ cannam@128: cannam@128: decl %chainlenwmask cannam@128: shll $16, %chainlenwmask cannam@128: orl %eax, %chainlenwmask cannam@128: cannam@128: /* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */ cannam@128: cannam@128: movl dsNiceMatch, %eax cannam@128: movl dsLookahead, %ebx cannam@128: cmpl %eax, %ebx cannam@128: jl LookaheadLess cannam@128: movl %eax, %ebx cannam@128: LookaheadLess: movl %ebx, %nicematch cannam@128: cannam@128: /* register Bytef *scan = s->window + s->strstart; */ cannam@128: cannam@128: mov dsWindow, %window cannam@128: movl dsStrStart, %limitd cannam@128: lea (%limit, %window), %scan cannam@128: cannam@128: /* Determine how many bytes the scan ptr is off from being */ cannam@128: /* dword-aligned. */ cannam@128: cannam@128: mov %scan, %scanalign cannam@128: negl %scanalignd cannam@128: andl $3, %scanalignd cannam@128: cannam@128: /* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */ cannam@128: /* s->strstart - (IPos)MAX_DIST(s) : NIL; */ cannam@128: cannam@128: movl dsWSize, %eax cannam@128: subl $MIN_LOOKAHEAD, %eax cannam@128: xorl %ecx, %ecx cannam@128: subl %eax, %limitd cannam@128: cmovng %ecx, %limitd cannam@128: cannam@128: /* int best_len = s->prev_length; */ cannam@128: cannam@128: movl dsPrevLen, %bestlend cannam@128: cannam@128: /* Store the sum of s->window + best_len in %windowbestlen locally, and in memory. */ cannam@128: cannam@128: lea (%window, %bestlen), %windowbestlen cannam@128: mov %windowbestlen, _windowbestlen cannam@128: cannam@128: /* register ush scan_start = *(ushf*)scan; */ cannam@128: /* register ush scan_end = *(ushf*)(scan+best_len-1); */ cannam@128: /* Posf *prev = s->prev; */ cannam@128: cannam@128: movzwl (%scan), %scanstart cannam@128: movzwl -1(%scan, %bestlen), %scanend cannam@128: mov dsPrev, %prev cannam@128: cannam@128: /* Jump into the main loop. */ cannam@128: cannam@128: movl %chainlenwmask, _chainlenwmask cannam@128: jmp LoopEntry cannam@128: cannam@128: .balign 16 cannam@128: cannam@128: /* do { cannam@128: * match = s->window + cur_match; cannam@128: * if (*(ushf*)(match+best_len-1) != scan_end || cannam@128: * *(ushf*)match != scan_start) continue; cannam@128: * [...] cannam@128: * } while ((cur_match = prev[cur_match & wmask]) > limit cannam@128: * && --chain_length != 0); cannam@128: * cannam@128: * Here is the inner loop of the function. The function will spend the cannam@128: * majority of its time in this loop, and majority of that time will cannam@128: * be spent in the first ten instructions. cannam@128: */ cannam@128: LookupLoop: cannam@128: andl %chainlenwmask, %curmatchd cannam@128: movzwl (%prev, %curmatch, 2), %curmatchd cannam@128: cmpl %limitd, %curmatchd cannam@128: jbe LeaveNow cannam@128: subl $0x00010000, %chainlenwmask cannam@128: js LeaveNow cannam@128: LoopEntry: cmpw -1(%windowbestlen, %curmatch), %scanendw cannam@128: jne LookupLoop cannam@128: cmpw %scanstartw, (%window, %curmatch) cannam@128: jne LookupLoop cannam@128: cannam@128: /* Store the current value of chainlen. */ cannam@128: movl %chainlenwmask, _chainlenwmask cannam@128: cannam@128: /* %scan is the string under scrutiny, and %prev to the string we */ cannam@128: /* are hoping to match it up with. In actuality, %esi and %edi are */ cannam@128: /* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */ cannam@128: /* initialized to -(MAX_MATCH_8 - scanalign). */ cannam@128: cannam@128: mov $(-MAX_MATCH_8), %rdx cannam@128: lea (%curmatch, %window), %windowbestlen cannam@128: lea MAX_MATCH_8(%windowbestlen, %scanalign), %windowbestlen cannam@128: lea MAX_MATCH_8(%scan, %scanalign), %prev cannam@128: cannam@128: /* the prefetching below makes very little difference... */ cannam@128: prefetcht1 (%windowbestlen, %rdx) cannam@128: prefetcht1 (%prev, %rdx) cannam@128: cannam@128: /* cannam@128: * Test the strings for equality, 8 bytes at a time. At the end, cannam@128: * adjust %rdx so that it is offset to the exact byte that mismatched. cannam@128: * cannam@128: * It should be confessed that this loop usually does not represent cannam@128: * much of the total running time. Replacing it with a more cannam@128: * straightforward "rep cmpsb" would not drastically degrade cannam@128: * performance -- unrolling it, for example, makes no difference. cannam@128: */ cannam@128: cannam@128: #undef USE_SSE /* works, but is 6-7% slower, than non-SSE... */ cannam@128: cannam@128: LoopCmps: cannam@128: #ifdef USE_SSE cannam@128: /* Preload the SSE registers */ cannam@128: movdqu (%windowbestlen, %rdx), %xmm1 cannam@128: movdqu (%prev, %rdx), %xmm2 cannam@128: pcmpeqb %xmm2, %xmm1 cannam@128: movdqu 16(%windowbestlen, %rdx), %xmm3 cannam@128: movdqu 16(%prev, %rdx), %xmm4 cannam@128: pcmpeqb %xmm4, %xmm3 cannam@128: movdqu 32(%windowbestlen, %rdx), %xmm5 cannam@128: movdqu 32(%prev, %rdx), %xmm6 cannam@128: pcmpeqb %xmm6, %xmm5 cannam@128: movdqu 48(%windowbestlen, %rdx), %xmm7 cannam@128: movdqu 48(%prev, %rdx), %xmm8 cannam@128: pcmpeqb %xmm8, %xmm7 cannam@128: cannam@128: /* Check the comparisions' results */ cannam@128: pmovmskb %xmm1, %rax cannam@128: notw %ax cannam@128: bsfw %ax, %ax cannam@128: jnz LeaveLoopCmps cannam@128: cannam@128: /* this is the only iteration of the loop with a possibility of having cannam@128: incremented rdx by 0x108 (each loop iteration add 16*4 = 0x40 cannam@128: and (0x40*4)+8=0x108 */ cannam@128: add $8, %rdx cannam@128: jz LenMaximum cannam@128: add $8, %rdx cannam@128: cannam@128: cannam@128: pmovmskb %xmm3, %rax cannam@128: notw %ax cannam@128: bsfw %ax, %ax cannam@128: jnz LeaveLoopCmps cannam@128: cannam@128: cannam@128: add $16, %rdx cannam@128: cannam@128: cannam@128: pmovmskb %xmm5, %rax cannam@128: notw %ax cannam@128: bsfw %ax, %ax cannam@128: jnz LeaveLoopCmps cannam@128: cannam@128: add $16, %rdx cannam@128: cannam@128: cannam@128: pmovmskb %xmm7, %rax cannam@128: notw %ax cannam@128: bsfw %ax, %ax cannam@128: jnz LeaveLoopCmps cannam@128: cannam@128: add $16, %rdx cannam@128: cannam@128: jmp LoopCmps cannam@128: LeaveLoopCmps: add %rax, %rdx cannam@128: #else cannam@128: mov (%windowbestlen, %rdx), %rax cannam@128: xor (%prev, %rdx), %rax cannam@128: jnz LeaveLoopCmps cannam@128: cannam@128: mov 8(%windowbestlen, %rdx), %rax cannam@128: xor 8(%prev, %rdx), %rax cannam@128: jnz LeaveLoopCmps8 cannam@128: cannam@128: mov 16(%windowbestlen, %rdx), %rax cannam@128: xor 16(%prev, %rdx), %rax cannam@128: jnz LeaveLoopCmps16 cannam@128: cannam@128: add $24, %rdx cannam@128: jnz LoopCmps cannam@128: jmp LenMaximum cannam@128: # if 0 cannam@128: /* cannam@128: * This three-liner is tantalizingly simple, but bsf is a slow instruction, cannam@128: * and the complicated alternative down below is quite a bit faster. Sad... cannam@128: */ cannam@128: cannam@128: LeaveLoopCmps: bsf %rax, %rax /* find the first non-zero bit */ cannam@128: shrl $3, %eax /* divide by 8 to get the byte */ cannam@128: add %rax, %rdx cannam@128: # else cannam@128: LeaveLoopCmps16: cannam@128: add $8, %rdx cannam@128: LeaveLoopCmps8: cannam@128: add $8, %rdx cannam@128: LeaveLoopCmps: testl $0xFFFFFFFF, %eax /* Check the first 4 bytes */ cannam@128: jnz Check16 cannam@128: add $4, %rdx cannam@128: shr $32, %rax cannam@128: Check16: testw $0xFFFF, %ax cannam@128: jnz LenLower cannam@128: add $2, %rdx cannam@128: shrl $16, %eax cannam@128: LenLower: subb $1, %al cannam@128: adc $0, %rdx cannam@128: # endif cannam@128: #endif cannam@128: cannam@128: /* Calculate the length of the match. If it is longer than MAX_MATCH, */ cannam@128: /* then automatically accept it as the best possible match and leave. */ cannam@128: cannam@128: lea (%prev, %rdx), %rax cannam@128: sub %scan, %rax cannam@128: cmpl $MAX_MATCH, %eax cannam@128: jge LenMaximum cannam@128: cannam@128: /* If the length of the match is not longer than the best match we */ cannam@128: /* have so far, then forget it and return to the lookup loop. */ cannam@128: cannam@128: cmpl %bestlend, %eax cannam@128: jg LongerMatch cannam@128: mov _windowbestlen, %windowbestlen cannam@128: mov dsPrev, %prev cannam@128: movl _chainlenwmask, %edx cannam@128: jmp LookupLoop cannam@128: cannam@128: /* s->match_start = cur_match; */ cannam@128: /* best_len = len; */ cannam@128: /* if (len >= nice_match) break; */ cannam@128: /* scan_end = *(ushf*)(scan+best_len-1); */ cannam@128: cannam@128: LongerMatch: cannam@128: movl %eax, %bestlend cannam@128: movl %curmatchd, dsMatchStart cannam@128: cmpl %nicematch, %eax cannam@128: jge LeaveNow cannam@128: cannam@128: lea (%window, %bestlen), %windowbestlen cannam@128: mov %windowbestlen, _windowbestlen cannam@128: cannam@128: movzwl -1(%scan, %rax), %scanend cannam@128: mov dsPrev, %prev cannam@128: movl _chainlenwmask, %chainlenwmask cannam@128: jmp LookupLoop cannam@128: cannam@128: /* Accept the current string, with the maximum possible length. */ cannam@128: cannam@128: LenMaximum: cannam@128: movl $MAX_MATCH, %bestlend cannam@128: movl %curmatchd, dsMatchStart cannam@128: cannam@128: /* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */ cannam@128: /* return s->lookahead; */ cannam@128: cannam@128: LeaveNow: cannam@128: movl dsLookahead, %eax cannam@128: cmpl %eax, %bestlend cannam@128: cmovngl %bestlend, %eax cannam@128: LookaheadRet: cannam@128: cannam@128: /* Restore the registers and return from whence we came. */ cannam@128: cannam@128: mov save_rsi, %rsi cannam@128: mov save_rbx, %rbx cannam@128: mov save_r12, %r12 cannam@128: mov save_r13, %r13 cannam@128: mov save_r14, %r14 cannam@128: mov save_r15, %r15 cannam@128: cannam@128: ret cannam@128: cannam@128: match_init: ret