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