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