gvmat64.S 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. ;uInt longest_match_x64(
  3. ; deflate_state *s,
  4. ; IPos cur_match); // current match
  5. ; gvmat64.S -- Asm portion of the optimized longest_match for 32 bits x86_64
  6. ; (AMD64 on Athlon 64, Opteron, Phenom
  7. ; and Intel EM64T on Pentium 4 with EM64T, Pentium D, Core 2 Duo, Core I5/I7)
  8. ; this file is translation from gvmat64.asm to GCC 4.x (for Linux, Mac XCode)
  9. ; Copyright (C) 1995-2010 Jean-loup Gailly, Brian Raiter and Gilles Vollant.
  10. ;
  11. ; File written by Gilles Vollant, by converting to assembly the longest_match
  12. ; from Jean-loup Gailly in deflate.c of zLib and infoZip zip.
  13. ; and by taking inspiration on asm686 with masm, optimised assembly code
  14. ; from Brian Raiter, written 1998
  15. ;
  16. ; This software is provided 'as-is', without any express or implied
  17. ; warranty. In no event will the authors be held liable for any damages
  18. ; arising from the use of this software.
  19. ;
  20. ; Permission is granted to anyone to use this software for any purpose,
  21. ; including commercial applications, and to alter it and redistribute it
  22. ; freely, subject to the following restrictions:
  23. ;
  24. ; 1. The origin of this software must not be misrepresented; you must not
  25. ; claim that you wrote the original software. If you use this software
  26. ; in a product, an acknowledgment in the product documentation would be
  27. ; appreciated but is not required.
  28. ; 2. Altered source versions must be plainly marked as such, and must not be
  29. ; misrepresented as being the original software
  30. ; 3. This notice may not be removed or altered from any source distribution.
  31. ;
  32. ; http://www.zlib.net
  33. ; http://www.winimage.com/zLibDll
  34. ; http://www.muppetlabs.com/~breadbox/software/assembly.html
  35. ;
  36. ; to compile this file for zLib, I use option:
  37. ; gcc -c -arch x86_64 gvmat64.S
  38. ;uInt longest_match(s, cur_match)
  39. ; deflate_state *s;
  40. ; IPos cur_match; // current match /
  41. ;
  42. ; with XCode for Mac, I had strange error with some jump on intel syntax
  43. ; this is why BEFORE_JMP and AFTER_JMP are used
  44. */
  45. #define BEFORE_JMP .att_syntax
  46. #define AFTER_JMP .intel_syntax noprefix
  47. #ifndef NO_UNDERLINE
  48. # define match_init _match_init
  49. # define longest_match _longest_match
  50. #endif
  51. .intel_syntax noprefix
  52. .globl match_init, longest_match
  53. .text
  54. longest_match:
  55. #define LocalVarsSize 96
  56. /*
  57. ; register used : rax,rbx,rcx,rdx,rsi,rdi,r8,r9,r10,r11,r12
  58. ; free register : r14,r15
  59. ; register can be saved : rsp
  60. */
  61. #define chainlenwmask (rsp + 8 - LocalVarsSize)
  62. #define nicematch (rsp + 16 - LocalVarsSize)
  63. #define save_rdi (rsp + 24 - LocalVarsSize)
  64. #define save_rsi (rsp + 32 - LocalVarsSize)
  65. #define save_rbx (rsp + 40 - LocalVarsSize)
  66. #define save_rbp (rsp + 48 - LocalVarsSize)
  67. #define save_r12 (rsp + 56 - LocalVarsSize)
  68. #define save_r13 (rsp + 64 - LocalVarsSize)
  69. #define save_r14 (rsp + 72 - LocalVarsSize)
  70. #define save_r15 (rsp + 80 - LocalVarsSize)
  71. /*
  72. ; all the +4 offsets are due to the addition of pending_buf_size (in zlib
  73. ; in the deflate_state structure since the asm code was first written
  74. ; (if you compile with zlib 1.0.4 or older, remove the +4).
  75. ; Note : these value are good with a 8 bytes boundary pack structure
  76. */
  77. #define MAX_MATCH 258
  78. #define MIN_MATCH 3
  79. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  80. /*
  81. ;;; Offsets for fields in the deflate_state structure. These numbers
  82. ;;; are calculated from the definition of deflate_state, with the
  83. ;;; assumption that the compiler will dword-align the fields. (Thus,
  84. ;;; changing the definition of deflate_state could easily cause this
  85. ;;; program to crash horribly, without so much as a warning at
  86. ;;; compile time. Sigh.)
  87. ; all the +zlib1222add offsets are due to the addition of fields
  88. ; in zlib in the deflate_state structure since the asm code was first written
  89. ; (if you compile with zlib 1.0.4 or older, use "zlib1222add equ (-4)").
  90. ; (if you compile with zlib between 1.0.5 and 1.2.2.1, use "zlib1222add equ 0").
  91. ; if you compile with zlib 1.2.2.2 or later , use "zlib1222add equ 8").
  92. */
  93. /* you can check the structure offset by running
  94. #include <stdlib.h>
  95. #include <stdio.h>
  96. #include "deflate.h"
  97. void print_depl()
  98. {
  99. deflate_state ds;
  100. deflate_state *s=&ds;
  101. printf("size pointer=%u\n",(int)sizeof(void*));
  102. printf("#define dsWSize %u\n",(int)(((char*)&(s->w_size))-((char*)s)));
  103. printf("#define dsWMask %u\n",(int)(((char*)&(s->w_mask))-((char*)s)));
  104. printf("#define dsWindow %u\n",(int)(((char*)&(s->window))-((char*)s)));
  105. printf("#define dsPrev %u\n",(int)(((char*)&(s->prev))-((char*)s)));
  106. printf("#define dsMatchLen %u\n",(int)(((char*)&(s->match_length))-((char*)s)));
  107. printf("#define dsPrevMatch %u\n",(int)(((char*)&(s->prev_match))-((char*)s)));
  108. printf("#define dsStrStart %u\n",(int)(((char*)&(s->strstart))-((char*)s)));
  109. printf("#define dsMatchStart %u\n",(int)(((char*)&(s->match_start))-((char*)s)));
  110. printf("#define dsLookahead %u\n",(int)(((char*)&(s->lookahead))-((char*)s)));
  111. printf("#define dsPrevLen %u\n",(int)(((char*)&(s->prev_length))-((char*)s)));
  112. printf("#define dsMaxChainLen %u\n",(int)(((char*)&(s->max_chain_length))-((char*)s)));
  113. printf("#define dsGoodMatch %u\n",(int)(((char*)&(s->good_match))-((char*)s)));
  114. printf("#define dsNiceMatch %u\n",(int)(((char*)&(s->nice_match))-((char*)s)));
  115. }
  116. */
  117. #define dsWSize 68
  118. #define dsWMask 76
  119. #define dsWindow 80
  120. #define dsPrev 96
  121. #define dsMatchLen 144
  122. #define dsPrevMatch 148
  123. #define dsStrStart 156
  124. #define dsMatchStart 160
  125. #define dsLookahead 164
  126. #define dsPrevLen 168
  127. #define dsMaxChainLen 172
  128. #define dsGoodMatch 188
  129. #define dsNiceMatch 192
  130. #define window_size [ rcx + dsWSize]
  131. #define WMask [ rcx + dsWMask]
  132. #define window_ad [ rcx + dsWindow]
  133. #define prev_ad [ rcx + dsPrev]
  134. #define strstart [ rcx + dsStrStart]
  135. #define match_start [ rcx + dsMatchStart]
  136. #define Lookahead [ rcx + dsLookahead] //; 0ffffffffh on infozip
  137. #define prev_length [ rcx + dsPrevLen]
  138. #define max_chain_length [ rcx + dsMaxChainLen]
  139. #define good_match [ rcx + dsGoodMatch]
  140. #define nice_match [ rcx + dsNiceMatch]
  141. /*
  142. ; windows:
  143. ; parameter 1 in rcx(deflate state s), param 2 in rdx (cur match)
  144. ; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and
  145. ; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp
  146. ;
  147. ; All registers must be preserved across the call, except for
  148. ; rax, rcx, rdx, r8, r9, r10, and r11, which are scratch.
  149. ;
  150. ; gcc on macosx-linux:
  151. ; see http://www.x86-64.org/documentation/abi-0.99.pdf
  152. ; param 1 in rdi, param 2 in rsi
  153. ; rbx, rsp, rbp, r12 to r15 must be preserved
  154. ;;; Save registers that the compiler may be using, and adjust esp to
  155. ;;; make room for our stack frame.
  156. ;;; Retrieve the function arguments. r8d will hold cur_match
  157. ;;; throughout the entire function. edx will hold the pointer to the
  158. ;;; deflate_state structure during the function's setup (before
  159. ;;; entering the main loop.
  160. ; ms: parameter 1 in rcx (deflate_state* s), param 2 in edx -> r8 (cur match)
  161. ; mac: param 1 in rdi, param 2 rsi
  162. ; this clear high 32 bits of r8, which can be garbage in both r8 and rdx
  163. */
  164. mov [save_rbx],rbx
  165. mov [save_rbp],rbp
  166. mov rcx,rdi
  167. mov r8d,esi
  168. mov [save_r12],r12
  169. mov [save_r13],r13
  170. mov [save_r14],r14
  171. mov [save_r15],r15
  172. //;;; uInt wmask = s->w_mask;
  173. //;;; unsigned chain_length = s->max_chain_length;
  174. //;;; if (s->prev_length >= s->good_match) {
  175. //;;; chain_length >>= 2;
  176. //;;; }
  177. mov edi, prev_length
  178. mov esi, good_match
  179. mov eax, WMask
  180. mov ebx, max_chain_length
  181. cmp edi, esi
  182. jl LastMatchGood
  183. shr ebx, 2
  184. LastMatchGood:
  185. //;;; chainlen is decremented once beforehand so that the function can
  186. //;;; use the sign flag instead of the zero flag for the exit test.
  187. //;;; It is then shifted into the high word, to make room for the wmask
  188. //;;; value, which it will always accompany.
  189. dec ebx
  190. shl ebx, 16
  191. or ebx, eax
  192. //;;; on zlib only
  193. //;;; if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  194. mov eax, nice_match
  195. mov [chainlenwmask], ebx
  196. mov r10d, Lookahead
  197. cmp r10d, eax
  198. cmovnl r10d, eax
  199. mov [nicematch],r10d
  200. //;;; register Bytef *scan = s->window + s->strstart;
  201. mov r10, window_ad
  202. mov ebp, strstart
  203. lea r13, [r10 + rbp]
  204. //;;; Determine how many bytes the scan ptr is off from being
  205. //;;; dword-aligned.
  206. mov r9,r13
  207. neg r13
  208. and r13,3
  209. //;;; IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  210. //;;; s->strstart - (IPos)MAX_DIST(s) : NIL;
  211. mov eax, window_size
  212. sub eax, MIN_LOOKAHEAD
  213. xor edi,edi
  214. sub ebp, eax
  215. mov r11d, prev_length
  216. cmovng ebp,edi
  217. //;;; int best_len = s->prev_length;
  218. //;;; Store the sum of s->window + best_len in esi locally, and in esi.
  219. lea rsi,[r10+r11]
  220. //;;; register ush scan_start = *(ushf*)scan;
  221. //;;; register ush scan_end = *(ushf*)(scan+best_len-1);
  222. //;;; Posf *prev = s->prev;
  223. movzx r12d,word ptr [r9]
  224. movzx ebx, word ptr [r9 + r11 - 1]
  225. mov rdi, prev_ad
  226. //;;; Jump into the main loop.
  227. mov edx, [chainlenwmask]
  228. cmp bx,word ptr [rsi + r8 - 1]
  229. jz LookupLoopIsZero
  230. LookupLoop1:
  231. and r8d, edx
  232. movzx r8d, word ptr [rdi + r8*2]
  233. cmp r8d, ebp
  234. jbe LeaveNow
  235. sub edx, 0x00010000
  236. BEFORE_JMP
  237. js LeaveNow
  238. AFTER_JMP
  239. LoopEntry1:
  240. cmp bx,word ptr [rsi + r8 - 1]
  241. BEFORE_JMP
  242. jz LookupLoopIsZero
  243. AFTER_JMP
  244. LookupLoop2:
  245. and r8d, edx
  246. movzx r8d, word ptr [rdi + r8*2]
  247. cmp r8d, ebp
  248. BEFORE_JMP
  249. jbe LeaveNow
  250. AFTER_JMP
  251. sub edx, 0x00010000
  252. BEFORE_JMP
  253. js LeaveNow
  254. AFTER_JMP
  255. LoopEntry2:
  256. cmp bx,word ptr [rsi + r8 - 1]
  257. BEFORE_JMP
  258. jz LookupLoopIsZero
  259. AFTER_JMP
  260. LookupLoop4:
  261. and r8d, edx
  262. movzx r8d, word ptr [rdi + r8*2]
  263. cmp r8d, ebp
  264. BEFORE_JMP
  265. jbe LeaveNow
  266. AFTER_JMP
  267. sub edx, 0x00010000
  268. BEFORE_JMP
  269. js LeaveNow
  270. AFTER_JMP
  271. LoopEntry4:
  272. cmp bx,word ptr [rsi + r8 - 1]
  273. BEFORE_JMP
  274. jnz LookupLoop1
  275. jmp LookupLoopIsZero
  276. AFTER_JMP
  277. /*
  278. ;;; do {
  279. ;;; match = s->window + cur_match;
  280. ;;; if (*(ushf*)(match+best_len-1) != scan_end ||
  281. ;;; *(ushf*)match != scan_start) continue;
  282. ;;; [...]
  283. ;;; } while ((cur_match = prev[cur_match & wmask]) > limit
  284. ;;; && --chain_length != 0);
  285. ;;;
  286. ;;; Here is the inner loop of the function. The function will spend the
  287. ;;; majority of its time in this loop, and majority of that time will
  288. ;;; be spent in the first ten instructions.
  289. ;;;
  290. ;;; Within this loop:
  291. ;;; ebx = scanend
  292. ;;; r8d = curmatch
  293. ;;; edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
  294. ;;; esi = windowbestlen - i.e., (window + bestlen)
  295. ;;; edi = prev
  296. ;;; ebp = limit
  297. */
  298. .balign 16
  299. LookupLoop:
  300. and r8d, edx
  301. movzx r8d, word ptr [rdi + r8*2]
  302. cmp r8d, ebp
  303. BEFORE_JMP
  304. jbe LeaveNow
  305. AFTER_JMP
  306. sub edx, 0x00010000
  307. BEFORE_JMP
  308. js LeaveNow
  309. AFTER_JMP
  310. LoopEntry:
  311. cmp bx,word ptr [rsi + r8 - 1]
  312. BEFORE_JMP
  313. jnz LookupLoop1
  314. AFTER_JMP
  315. LookupLoopIsZero:
  316. cmp r12w, word ptr [r10 + r8]
  317. BEFORE_JMP
  318. jnz LookupLoop1
  319. AFTER_JMP
  320. //;;; Store the current value of chainlen.
  321. mov [chainlenwmask], edx
  322. /*
  323. ;;; Point edi to the string under scrutiny, and esi to the string we
  324. ;;; are hoping to match it up with. In actuality, esi and edi are
  325. ;;; both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and edx is
  326. ;;; initialized to -(MAX_MATCH_8 - scanalign).
  327. */
  328. lea rsi,[r8+r10]
  329. mov rdx, 0xfffffffffffffef8 //; -(MAX_MATCH_8)
  330. lea rsi, [rsi + r13 + 0x0108] //;MAX_MATCH_8]
  331. lea rdi, [r9 + r13 + 0x0108] //;MAX_MATCH_8]
  332. prefetcht1 [rsi+rdx]
  333. prefetcht1 [rdi+rdx]
  334. /*
  335. ;;; Test the strings for equality, 8 bytes at a time. At the end,
  336. ;;; adjust rdx so that it is offset to the exact byte that mismatched.
  337. ;;;
  338. ;;; We already know at this point that the first three bytes of the
  339. ;;; strings match each other, and they can be safely passed over before
  340. ;;; starting the compare loop. So what this code does is skip over 0-3
  341. ;;; bytes, as much as necessary in order to dword-align the edi
  342. ;;; pointer. (rsi will still be misaligned three times out of four.)
  343. ;;;
  344. ;;; It should be confessed that this loop usually does not represent
  345. ;;; much of the total running time. Replacing it with a more
  346. ;;; straightforward "rep cmpsb" would not drastically degrade
  347. ;;; performance.
  348. */
  349. LoopCmps:
  350. mov rax, [rsi + rdx]
  351. xor rax, [rdi + rdx]
  352. jnz LeaveLoopCmps
  353. mov rax, [rsi + rdx + 8]
  354. xor rax, [rdi + rdx + 8]
  355. jnz LeaveLoopCmps8
  356. mov rax, [rsi + rdx + 8+8]
  357. xor rax, [rdi + rdx + 8+8]
  358. jnz LeaveLoopCmps16
  359. add rdx,8+8+8
  360. BEFORE_JMP
  361. jnz LoopCmps
  362. jmp LenMaximum
  363. AFTER_JMP
  364. LeaveLoopCmps16: add rdx,8
  365. LeaveLoopCmps8: add rdx,8
  366. LeaveLoopCmps:
  367. test eax, 0x0000FFFF
  368. jnz LenLower
  369. test eax,0xffffffff
  370. jnz LenLower32
  371. add rdx,4
  372. shr rax,32
  373. or ax,ax
  374. BEFORE_JMP
  375. jnz LenLower
  376. AFTER_JMP
  377. LenLower32:
  378. shr eax,16
  379. add rdx,2
  380. LenLower:
  381. sub al, 1
  382. adc rdx, 0
  383. //;;; Calculate the length of the match. If it is longer than MAX_MATCH,
  384. //;;; then automatically accept it as the best possible match and leave.
  385. lea rax, [rdi + rdx]
  386. sub rax, r9
  387. cmp eax, MAX_MATCH
  388. BEFORE_JMP
  389. jge LenMaximum
  390. AFTER_JMP
  391. /*
  392. ;;; If the length of the match is not longer than the best match we
  393. ;;; have so far, then forget it and return to the lookup loop.
  394. ;///////////////////////////////////
  395. */
  396. cmp eax, r11d
  397. jg LongerMatch
  398. lea rsi,[r10+r11]
  399. mov rdi, prev_ad
  400. mov edx, [chainlenwmask]
  401. BEFORE_JMP
  402. jmp LookupLoop
  403. AFTER_JMP
  404. /*
  405. ;;; s->match_start = cur_match;
  406. ;;; best_len = len;
  407. ;;; if (len >= nice_match) break;
  408. ;;; scan_end = *(ushf*)(scan+best_len-1);
  409. */
  410. LongerMatch:
  411. mov r11d, eax
  412. mov match_start, r8d
  413. cmp eax, [nicematch]
  414. BEFORE_JMP
  415. jge LeaveNow
  416. AFTER_JMP
  417. lea rsi,[r10+rax]
  418. movzx ebx, word ptr [r9 + rax - 1]
  419. mov rdi, prev_ad
  420. mov edx, [chainlenwmask]
  421. BEFORE_JMP
  422. jmp LookupLoop
  423. AFTER_JMP
  424. //;;; Accept the current string, with the maximum possible length.
  425. LenMaximum:
  426. mov r11d,MAX_MATCH
  427. mov match_start, r8d
  428. //;;; if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  429. //;;; return s->lookahead;
  430. LeaveNow:
  431. mov eax, Lookahead
  432. cmp r11d, eax
  433. cmovng eax, r11d
  434. //;;; Restore the stack and return from whence we came.
  435. // mov rsi,[save_rsi]
  436. // mov rdi,[save_rdi]
  437. mov rbx,[save_rbx]
  438. mov rbp,[save_rbp]
  439. mov r12,[save_r12]
  440. mov r13,[save_r13]
  441. mov r14,[save_r14]
  442. mov r15,[save_r15]
  443. ret 0
  444. //; please don't remove this string !
  445. //; Your can freely use gvmat64 in any free or commercial app
  446. //; but it is far better don't remove the string in the binary!
  447. // 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
  448. match_init:
  449. ret 0