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