1135669Scognet/* 2135669Scognet;uInt longest_match_x64( 3139735Simp; deflate_state *s, 4135669Scognet; IPos cur_match); // current match 5135669Scognet 6135669Scognet; gvmat64.S -- Asm portion of the optimized longest_match for 32 bits x86_64 7135669Scognet; (AMD64 on Athlon 64, Opteron, Phenom 8135669Scognet; and Intel EM64T on Pentium 4 with EM64T, Pentium D, Core 2 Duo, Core I5/I7) 9135669Scognet; this file is translation from gvmat64.asm to GCC 4.x (for Linux, Mac XCode) 10135669Scognet; Copyright (C) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant. 11135669Scognet; 12135669Scognet; File written by Gilles Vollant, by converting to assembly the longest_match 13135669Scognet; from Jean-loup Gailly in deflate.c of zLib and infoZip zip. 14135669Scognet; and by taking inspiration on asm686 with masm, optimised assembly code 15135669Scognet; from Brian Raiter, written 1998 16135669Scognet; 17135669Scognet; This software is provided 'as-is', without any express or implied 18135669Scognet; warranty. In no event will the authors be held liable for any damages 19135669Scognet; arising from the use of this software. 20135669Scognet; 21135669Scognet; Permission is granted to anyone to use this software for any purpose, 22135669Scognet; including commercial applications, and to alter it and redistribute it 23135669Scognet; freely, subject to the following restrictions: 24135669Scognet; 25135669Scognet; 1. The origin of this software must not be misrepresented; you must not 26135669Scognet; claim that you wrote the original software. If you use this software 27135669Scognet; in a product, an acknowledgment in the product documentation would be 28135669Scognet; appreciated but is not required. 29135669Scognet; 2. Altered source versions must be plainly marked as such, and must not be 30135669Scognet; misrepresented as being the original software 31135669Scognet; 3. This notice may not be removed or altered from any source distribution. 32135669Scognet; 33135669Scognet; http://www.zlib.net 34135669Scognet; http://www.winimage.com/zLibDll 35135669Scognet; http://www.muppetlabs.com/~breadbox/software/assembly.html 36135669Scognet; 37135669Scognet; to compile this file for zLib, I use option: 38135669Scognet; gcc -c -arch x86_64 gvmat64.S 39135669Scognet 40135669Scognet 41135669Scognet;uInt longest_match(s, cur_match) 42135669Scognet; deflate_state *s; 43135669Scognet; IPos cur_match; // current match / 44135669Scognet; 45135669Scognet; with XCode for Mac, I had strange error with some jump on intel syntax 46135669Scognet; this is why BEFORE_JMP and AFTER_JMP are used 47135669Scognet */ 48135669Scognet 49135669Scognet 50135669Scognet#define BEFORE_JMP .att_syntax 51135669Scognet#define AFTER_JMP .intel_syntax noprefix 52135669Scognet 53135669Scognet#ifndef NO_UNDERLINE 54135669Scognet# define match_init _match_init 55135669Scognet# define longest_match _longest_match 56135669Scognet#endif 57135669Scognet 58135669Scognet.intel_syntax noprefix 59135669Scognet 60135669Scognet.globl match_init, longest_match 61135669Scognet.text 62135669Scognetlongest_match: 63135669Scognet 64135669Scognet 65135669Scognet 66135669Scognet#define LocalVarsSize 96 67135669Scognet/* 68135669Scognet; register used : rax,rbx,rcx,rdx,rsi,rdi,r8,r9,r10,r11,r12 69135669Scognet; free register : r14,r15 70135669Scognet; register can be saved : rsp 71135669Scognet*/ 72135669Scognet 73135669Scognet#define chainlenwmask (rsp + 8 - LocalVarsSize) 74135669Scognet#define nicematch (rsp + 16 - LocalVarsSize) 75135669Scognet 76135669Scognet#define save_rdi (rsp + 24 - LocalVarsSize) 77135669Scognet#define save_rsi (rsp + 32 - LocalVarsSize) 78135669Scognet#define save_rbx (rsp + 40 - LocalVarsSize) 79135669Scognet#define save_rbp (rsp + 48 - LocalVarsSize) 80135669Scognet#define save_r12 (rsp + 56 - LocalVarsSize) 81135669Scognet#define save_r13 (rsp + 64 - LocalVarsSize) 82135669Scognet#define save_r14 (rsp + 72 - LocalVarsSize) 83135669Scognet#define save_r15 (rsp + 80 - LocalVarsSize) 84135669Scognet 85135669Scognet 86135669Scognet/* 87135669Scognet; all the +4 offsets are due to the addition of pending_buf_size (in zlib 88135669Scognet; in the deflate_state structure since the asm code was first written 89135669Scognet; (if you compile with zlib 1.0.4 or older, remove the +4). 90135669Scognet; Note : these value are good with a 8 bytes boundary pack structure 91135669Scognet*/ 92135669Scognet 93135669Scognet#define MAX_MATCH 258 94135669Scognet#define MIN_MATCH 3 95135669Scognet#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 96135669Scognet 97135669Scognet/* 98135669Scognet;;; Offsets for fields in the deflate_state structure. These numbers 99135669Scognet;;; are calculated from the definition of deflate_state, with the 100135669Scognet;;; assumption that the compiler will dword-align the fields. (Thus, 101135669Scognet;;; changing the definition of deflate_state could easily cause this 102135669Scognet;;; program to crash horribly, without so much as a warning at 103135669Scognet;;; compile time. Sigh.) 104135669Scognet 105135669Scognet; all the +zlib1222add offsets are due to the addition of fields 106135669Scognet; in zlib in the deflate_state structure since the asm code was first written 107135669Scognet; (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)"). 108135669Scognet; (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0"). 109135669Scognet; if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8"). 110135669Scognet*/ 111135669Scognet 112135669Scognet 113135669Scognet 114135669Scognet/* you can check the structure offset by running 115135669Scognet 116135669Scognet#include <stdlib.h> 117135669Scognet#include <stdio.h> 118135669Scognet#include "deflate.h" 119135669Scognet 120135669Scognetvoid print_depl() 121135669Scognet{ 122135669Scognetdeflate_state ds; 123135669Scognetdeflate_state *s=&ds; 124135669Scognetprintf("size pointer=%u\n",(int)sizeof(void*)); 125135669Scognet 126135669Scognetprintf("#define dsWSize %u\n",(int)(((char*)&(s->w_size))-((char*)s))); 127135669Scognetprintf("#define dsWMask %u\n",(int)(((char*)&(s->w_mask))-((char*)s))); 128135669Scognetprintf("#define dsWindow %u\n",(int)(((char*)&(s->window))-((char*)s))); 129135669Scognetprintf("#define dsPrev %u\n",(int)(((char*)&(s->prev))-((char*)s))); 130135669Scognetprintf("#define dsMatchLen %u\n",(int)(((char*)&(s->match_length))-((char*)s))); 131135669Scognetprintf("#define dsPrevMatch %u\n",(int)(((char*)&(s->prev_match))-((char*)s))); 132135669Scognetprintf("#define dsStrStart %u\n",(int)(((char*)&(s->strstart))-((char*)s))); 133135669Scognetprintf("#define dsMatchStart %u\n",(int)(((char*)&(s->match_start))-((char*)s))); 134135669Scognetprintf("#define dsLookahead %u\n",(int)(((char*)&(s->lookahead))-((char*)s))); 135135669Scognetprintf("#define dsPrevLen %u\n",(int)(((char*)&(s->prev_length))-((char*)s))); 136135669Scognetprintf("#define dsMaxChainLen %u\n",(int)(((char*)&(s->max_chain_length))-((char*)s))); 137135669Scognetprintf("#define dsGoodMatch %u\n",(int)(((char*)&(s->good_match))-((char*)s))); 138135669Scognetprintf("#define dsNiceMatch %u\n",(int)(((char*)&(s->nice_match))-((char*)s))); 139135669Scognet} 140135669Scognet*/ 141135669Scognet 142135669Scognet#define dsWSize 68 143135669Scognet#define dsWMask 76 144135669Scognet#define dsWindow 80 145135669Scognet#define dsPrev 96 146135669Scognet#define dsMatchLen 144 147135669Scognet#define dsPrevMatch 148 148135669Scognet#define dsStrStart 156 149135669Scognet#define dsMatchStart 160 150135669Scognet#define dsLookahead 164 151135669Scognet#define dsPrevLen 168 152135669Scognet#define dsMaxChainLen 172 153135669Scognet#define dsGoodMatch 188 154135669Scognet#define dsNiceMatch 192 155135669Scognet 156135669Scognet#define window_size [ rcx + dsWSize] 157135669Scognet#define WMask [ rcx + dsWMask] 158135669Scognet#define window_ad [ rcx + dsWindow] 159135669Scognet#define prev_ad [ rcx + dsPrev] 160135669Scognet#define strstart [ rcx + dsStrStart] 161135669Scognet#define match_start [ rcx + dsMatchStart] 162135669Scognet#define Lookahead [ rcx + dsLookahead] //; 0ffffffffh on infozip 163135669Scognet#define prev_length [ rcx + dsPrevLen] 164135669Scognet#define max_chain_length [ rcx + dsMaxChainLen] 165135669Scognet#define good_match [ rcx + dsGoodMatch] 166135669Scognet#define nice_match [ rcx + dsNiceMatch] 167135669Scognet 168135669Scognet/* 169135669Scognet; windows: 170135669Scognet; parameter 1 in rcx(deflate state s), param 2 in rdx (cur match) 171135669Scognet 172135669Scognet; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and 173135669Scognet; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp 174135669Scognet; 175135669Scognet; All registers must be preserved across the call, except for 176135669Scognet; rax, rcx, rdx, r8, r9, r10, and r11, which are scratch. 177135669Scognet 178135669Scognet; 179135669Scognet; gcc on macosx-linux: 180135669Scognet; see http://www.x86-64.org/documentation/abi-0.99.pdf 181135669Scognet; param 1 in rdi, param 2 in rsi 182135669Scognet; rbx, rsp, rbp, r12 to r15 must be preserved 183135669Scognet 184135669Scognet;;; Save registers that the compiler may be using, and adjust esp to 185135669Scognet;;; make room for our stack frame. 186135669Scognet 187135669Scognet 188135669Scognet;;; Retrieve the function arguments. r8d will hold cur_match 189135669Scognet;;; throughout the entire function. edx will hold the pointer to the 190135669Scognet;;; deflate_state structure during the function's setup (before 191135669Scognet;;; entering the main loop. 192135669Scognet 193135669Scognet; ms: parameter 1 in rcx (deflate_state* s), param 2 in edx -> r8 (cur match) 194135669Scognet; mac: param 1 in rdi, param 2 rsi 195135669Scognet; this clear high 32 bits of r8, which can be garbage in both r8 and rdx 196135669Scognet*/ 197135669Scognet mov [save_rbx],rbx 198135669Scognet mov [save_rbp],rbp 199135669Scognet 200135669Scognet 201135669Scognet mov rcx,rdi 202135669Scognet 203135669Scognet mov r8d,esi 204135669Scognet 205135669Scognet 206135669Scognet mov [save_r12],r12 207135669Scognet mov [save_r13],r13 208135669Scognet mov [save_r14],r14 209135669Scognet mov [save_r15],r15 210135669Scognet 211135669Scognet 212135669Scognet//;;; uInt wmask = s->w_mask; 213135669Scognet//;;; unsigned chain_length = s->max_chain_length; 214135669Scognet//;;; if (s->prev_length >= s->good_match) { 215135669Scognet//;;; chain_length >>= 2; 216135669Scognet//;;; } 217135669Scognet 218135669Scognet 219135669Scognet mov edi, prev_length 220135669Scognet mov esi, good_match 221135669Scognet mov eax, WMask 222135669Scognet mov ebx, max_chain_length 223135669Scognet cmp edi, esi 224135669Scognet jl LastMatchGood 225135669Scognet shr ebx, 2 226135669ScognetLastMatchGood: 227135669Scognet 228135669Scognet//;;; chainlen is decremented once beforehand so that the function can 229135669Scognet//;;; use the sign flag instead of the zero flag for the exit test. 230135669Scognet//;;; It is then shifted into the high word, to make room for the wmask 231135669Scognet//;;; value, which it will always accompany. 232135669Scognet 233135669Scognet dec ebx 234135669Scognet shl ebx, 16 235135669Scognet or ebx, eax 236135669Scognet 237135669Scognet//;;; on zlib only 238135669Scognet//;;; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; 239135669Scognet 240135669Scognet 241135669Scognet 242135669Scognet mov eax, nice_match 243135669Scognet mov [chainlenwmask], ebx 244135669Scognet mov r10d, Lookahead 245135669Scognet cmp r10d, eax 246135669Scognet cmovnl r10d, eax 247135669Scognet mov [nicematch],r10d 248135669Scognet 249135669Scognet 250135669Scognet 251135669Scognet//;;; register Bytef *scan = s->window + s->strstart; 252135669Scognet mov r10, window_ad 253135669Scognet mov ebp, strstart 254135669Scognet lea r13, [r10 + rbp] 255135669Scognet 256135669Scognet//;;; Determine how many bytes the scan ptr is off from being 257135669Scognet//;;; dword-aligned. 258135669Scognet 259135669Scognet mov r9,r13 260135669Scognet neg r13 261135669Scognet and r13,3 262135669Scognet 263135669Scognet//;;; IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 264135669Scognet//;;; s->strstart - (IPos)MAX_DIST(s) : NIL; 265135669Scognet 266135669Scognet 267135669Scognet mov eax, window_size 268135669Scognet sub eax, MIN_LOOKAHEAD 269 270 271 xor edi,edi 272 sub ebp, eax 273 274 mov r11d, prev_length 275 276 cmovng ebp,edi 277 278//;;; int best_len = s->prev_length; 279 280 281//;;; Store the sum of s->window + best_len in esi locally, and in esi. 282 283 lea rsi,[r10+r11] 284 285//;;; register ush scan_start = *(ushf*)scan; 286//;;; register ush scan_end = *(ushf*)(scan+best_len-1); 287//;;; Posf *prev = s->prev; 288 289 movzx r12d,word ptr [r9] 290 movzx ebx, word ptr [r9 + r11 - 1] 291 292 mov rdi, prev_ad 293 294//;;; Jump into the main loop. 295 296 mov edx, [chainlenwmask] 297 298 cmp bx,word ptr [rsi + r8 - 1] 299 jz LookupLoopIsZero 300 301 302 303LookupLoop1: 304 and r8d, edx 305 306 movzx r8d, word ptr [rdi + r8*2] 307 cmp r8d, ebp 308 jbe LeaveNow 309 310 311 312 sub edx, 0x00010000 313 BEFORE_JMP 314 js LeaveNow 315 AFTER_JMP 316 317LoopEntry1: 318 cmp bx,word ptr [rsi + r8 - 1] 319 BEFORE_JMP 320 jz LookupLoopIsZero 321 AFTER_JMP 322 323LookupLoop2: 324 and r8d, edx 325 326 movzx r8d, word ptr [rdi + r8*2] 327 cmp r8d, ebp 328 BEFORE_JMP 329 jbe LeaveNow 330 AFTER_JMP 331 sub edx, 0x00010000 332 BEFORE_JMP 333 js LeaveNow 334 AFTER_JMP 335 336LoopEntry2: 337 cmp bx,word ptr [rsi + r8 - 1] 338 BEFORE_JMP 339 jz LookupLoopIsZero 340 AFTER_JMP 341 342LookupLoop4: 343 and r8d, edx 344 345 movzx r8d, word ptr [rdi + r8*2] 346 cmp r8d, ebp 347 BEFORE_JMP 348 jbe LeaveNow 349 AFTER_JMP 350 sub edx, 0x00010000 351 BEFORE_JMP 352 js LeaveNow 353 AFTER_JMP 354 355LoopEntry4: 356 357 cmp bx,word ptr [rsi + r8 - 1] 358 BEFORE_JMP 359 jnz LookupLoop1 360 jmp LookupLoopIsZero 361 AFTER_JMP 362/* 363;;; do { 364;;; match = s->window + cur_match; 365;;; if (*(ushf*)(match+best_len-1) != scan_end || 366;;; *(ushf*)match != scan_start) continue; 367;;; [...] 368;;; } while ((cur_match = prev[cur_match & wmask]) > limit 369;;; && --chain_length != 0); 370;;; 371;;; Here is the inner loop of the function. The function will spend the 372;;; majority of its time in this loop, and majority of that time will 373;;; be spent in the first ten instructions. 374;;; 375;;; Within this loop: 376;;; ebx = scanend 377;;; r8d = curmatch 378;;; edx = chainlenwmask - i.e., ((chainlen << 16) | wmask) 379;;; esi = windowbestlen - i.e., (window + bestlen) 380;;; edi = prev 381;;; ebp = limit 382*/ 383.balign 16 384LookupLoop: 385 and r8d, edx 386 387 movzx r8d, word ptr [rdi + r8*2] 388 cmp r8d, ebp 389 BEFORE_JMP 390 jbe LeaveNow 391 AFTER_JMP 392 sub edx, 0x00010000 393 BEFORE_JMP 394 js LeaveNow 395 AFTER_JMP 396 397LoopEntry: 398 399 cmp bx,word ptr [rsi + r8 - 1] 400 BEFORE_JMP 401 jnz LookupLoop1 402 AFTER_JMP 403LookupLoopIsZero: 404 cmp r12w, word ptr [r10 + r8] 405 BEFORE_JMP 406 jnz LookupLoop1 407 AFTER_JMP 408 409 410//;;; Store the current value of chainlen. 411 mov [chainlenwmask], edx 412/* 413;;; Point edi to the string under scrutiny, and esi to the string we 414;;; are hoping to match it up with. In actuality, esi and edi are 415;;; both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and edx is 416;;; initialized to -(MAX_MATCH_8 - scanalign). 417*/ 418 lea rsi,[r8+r10] 419 mov rdx, 0xfffffffffffffef8 //; -(MAX_MATCH_8) 420 lea rsi, [rsi + r13 + 0x0108] //;MAX_MATCH_8] 421 lea rdi, [r9 + r13 + 0x0108] //;MAX_MATCH_8] 422 423 prefetcht1 [rsi+rdx] 424 prefetcht1 [rdi+rdx] 425 426/* 427;;; Test the strings for equality, 8 bytes at a time. At the end, 428;;; adjust rdx so that it is offset to the exact byte that mismatched. 429;;; 430;;; We already know at this point that the first three bytes of the 431;;; strings match each other, and they can be safely passed over before 432;;; starting the compare loop. So what this code does is skip over 0-3 433;;; bytes, as much as necessary in order to dword-align the edi 434;;; pointer. (rsi will still be misaligned three times out of four.) 435;;; 436;;; It should be confessed that this loop usually does not represent 437;;; much of the total running time. Replacing it with a more 438;;; straightforward "rep cmpsb" would not drastically degrade 439;;; performance. 440*/ 441 442LoopCmps: 443 mov rax, [rsi + rdx] 444 xor rax, [rdi + rdx] 445 jnz LeaveLoopCmps 446 447 mov rax, [rsi + rdx + 8] 448 xor rax, [rdi + rdx + 8] 449 jnz LeaveLoopCmps8 450 451 452 mov rax, [rsi + rdx + 8+8] 453 xor rax, [rdi + rdx + 8+8] 454 jnz LeaveLoopCmps16 455 456 add rdx,8+8+8 457 458 BEFORE_JMP 459 jnz LoopCmps 460 jmp LenMaximum 461 AFTER_JMP 462 463LeaveLoopCmps16: add rdx,8 464LeaveLoopCmps8: add rdx,8 465LeaveLoopCmps: 466 467 test eax, 0x0000FFFF 468 jnz LenLower 469 470 test eax,0xffffffff 471 472 jnz LenLower32 473 474 add rdx,4 475 shr rax,32 476 or ax,ax 477 BEFORE_JMP 478 jnz LenLower 479 AFTER_JMP 480 481LenLower32: 482 shr eax,16 483 add rdx,2 484 485LenLower: 486 sub al, 1 487 adc rdx, 0 488//;;; Calculate the length of the match. If it is longer than MAX_MATCH, 489//;;; then automatically accept it as the best possible match and leave. 490 491 lea rax, [rdi + rdx] 492 sub rax, r9 493 cmp eax, MAX_MATCH 494 BEFORE_JMP 495 jge LenMaximum 496 AFTER_JMP 497/* 498;;; If the length of the match is not longer than the best match we 499;;; have so far, then forget it and return to the lookup loop. 500;/////////////////////////////////// 501*/ 502 cmp eax, r11d 503 jg LongerMatch 504 505 lea rsi,[r10+r11] 506 507 mov rdi, prev_ad 508 mov edx, [chainlenwmask] 509 BEFORE_JMP 510 jmp LookupLoop 511 AFTER_JMP 512/* 513;;; s->match_start = cur_match; 514;;; best_len = len; 515;;; if (len >= nice_match) break; 516;;; scan_end = *(ushf*)(scan+best_len-1); 517*/ 518LongerMatch: 519 mov r11d, eax 520 mov match_start, r8d 521 cmp eax, [nicematch] 522 BEFORE_JMP 523 jge LeaveNow 524 AFTER_JMP 525 526 lea rsi,[r10+rax] 527 528 movzx ebx, word ptr [r9 + rax - 1] 529 mov rdi, prev_ad 530 mov edx, [chainlenwmask] 531 BEFORE_JMP 532 jmp LookupLoop 533 AFTER_JMP 534 535//;;; Accept the current string, with the maximum possible length. 536 537LenMaximum: 538 mov r11d,MAX_MATCH 539 mov match_start, r8d 540 541//;;; if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 542//;;; return s->lookahead; 543 544LeaveNow: 545 mov eax, Lookahead 546 cmp r11d, eax 547 cmovng eax, r11d 548 549 550 551//;;; Restore the stack and return from whence we came. 552 553 554// mov rsi,[save_rsi] 555// mov rdi,[save_rdi] 556 mov rbx,[save_rbx] 557 mov rbp,[save_rbp] 558 mov r12,[save_r12] 559 mov r13,[save_r13] 560 mov r14,[save_r14] 561 mov r15,[save_r15] 562 563 564 ret 0 565//; please don't remove this string ! 566//; Your can freely use gvmat64 in any free or commercial app 567//; but it is far better don't remove the string in the binary! 568 // db 0dh,0ah,"asm686 with masm, optimised assembly code from Brian Raiter, written 1998, converted to amd 64 by Gilles Vollant 2005",0dh,0ah,0 569 570 571match_init: 572 ret 0 573 574 575