inffasx64.asm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. ; inffasx64.asm is a hand tuned assembler version of inffast.c - fast decoding
  2. ; version for AMD64 on Windows using Microsoft C compiler
  3. ;
  4. ; inffasx64.asm is automatically convert from AMD64 portion of inffas86.c
  5. ; inffasx64.asm is called by inffas8664.c, which contain more info.
  6. ; to compile this file, I use option
  7. ; ml64.exe /Flinffasx64 /c /Zi inffasx64.asm
  8. ; with Microsoft Macro Assembler (x64) for AMD64
  9. ;
  10. ; This file compile with Microsoft Macro Assembler (x64) for AMD64
  11. ;
  12. ; ml64.exe is given with Visual Studio 2005/2008/2010 and Windows WDK
  13. ;
  14. ; (you can get Windows WDK with ml64 for AMD64 from
  15. ; http://www.microsoft.com/whdc/Devtools/wdk/default.mspx for low price)
  16. ;
  17. .code
  18. inffas8664fnc PROC
  19. ; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and
  20. ; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp
  21. ;
  22. ; All registers must be preserved across the call, except for
  23. ; rax, rcx, rdx, r8, r-9, r10, and r11, which are scratch.
  24. mov [rsp-8],rsi
  25. mov [rsp-16],rdi
  26. mov [rsp-24],r12
  27. mov [rsp-32],r13
  28. mov [rsp-40],r14
  29. mov [rsp-48],r15
  30. mov [rsp-56],rbx
  31. mov rax,rcx
  32. mov [rax+8], rbp ; /* save regs rbp and rsp */
  33. mov [rax], rsp
  34. mov rsp, rax ; /* make rsp point to &ar */
  35. mov rsi, [rsp+16] ; /* rsi = in */
  36. mov rdi, [rsp+32] ; /* rdi = out */
  37. mov r9, [rsp+24] ; /* r9 = last */
  38. mov r10, [rsp+48] ; /* r10 = end */
  39. mov rbp, [rsp+64] ; /* rbp = lcode */
  40. mov r11, [rsp+72] ; /* r11 = dcode */
  41. mov rdx, [rsp+80] ; /* rdx = hold */
  42. mov ebx, [rsp+88] ; /* ebx = bits */
  43. mov r12d, [rsp+100] ; /* r12d = lmask */
  44. mov r13d, [rsp+104] ; /* r13d = dmask */
  45. ; /* r14d = len */
  46. ; /* r15d = dist */
  47. cld
  48. cmp r10, rdi
  49. je L_one_time ; /* if only one decode left */
  50. cmp r9, rsi
  51. jne L_do_loop
  52. L_one_time:
  53. mov r8, r12 ; /* r8 = lmask */
  54. cmp bl, 32
  55. ja L_get_length_code_one_time
  56. lodsd ; /* eax = *(uint *)in++ */
  57. mov cl, bl ; /* cl = bits, needs it for shifting */
  58. add bl, 32 ; /* bits += 32 */
  59. shl rax, cl
  60. or rdx, rax ; /* hold |= *((uint *)in)++ << bits */
  61. jmp L_get_length_code_one_time
  62. ALIGN 4
  63. L_while_test:
  64. cmp r10, rdi
  65. jbe L_break_loop
  66. cmp r9, rsi
  67. jbe L_break_loop
  68. L_do_loop:
  69. mov r8, r12 ; /* r8 = lmask */
  70. cmp bl, 32
  71. ja L_get_length_code ; /* if (32 < bits) */
  72. lodsd ; /* eax = *(uint *)in++ */
  73. mov cl, bl ; /* cl = bits, needs it for shifting */
  74. add bl, 32 ; /* bits += 32 */
  75. shl rax, cl
  76. or rdx, rax ; /* hold |= *((uint *)in)++ << bits */
  77. L_get_length_code:
  78. and r8, rdx ; /* r8 &= hold */
  79. mov eax, [rbp+r8*4] ; /* eax = lcode[hold & lmask] */
  80. mov cl, ah ; /* cl = this.bits */
  81. sub bl, ah ; /* bits -= this.bits */
  82. shr rdx, cl ; /* hold >>= this.bits */
  83. test al, al
  84. jnz L_test_for_length_base ; /* if (op != 0) 45.7% */
  85. mov r8, r12 ; /* r8 = lmask */
  86. shr eax, 16 ; /* output this.val char */
  87. stosb
  88. L_get_length_code_one_time:
  89. and r8, rdx ; /* r8 &= hold */
  90. mov eax, [rbp+r8*4] ; /* eax = lcode[hold & lmask] */
  91. L_dolen:
  92. mov cl, ah ; /* cl = this.bits */
  93. sub bl, ah ; /* bits -= this.bits */
  94. shr rdx, cl ; /* hold >>= this.bits */
  95. test al, al
  96. jnz L_test_for_length_base ; /* if (op != 0) 45.7% */
  97. shr eax, 16 ; /* output this.val char */
  98. stosb
  99. jmp L_while_test
  100. ALIGN 4
  101. L_test_for_length_base:
  102. mov r14d, eax ; /* len = this */
  103. shr r14d, 16 ; /* len = this.val */
  104. mov cl, al
  105. test al, 16
  106. jz L_test_for_second_level_length ; /* if ((op & 16) == 0) 8% */
  107. and cl, 15 ; /* op &= 15 */
  108. jz L_decode_distance ; /* if (!op) */
  109. L_add_bits_to_len:
  110. sub bl, cl
  111. xor eax, eax
  112. inc eax
  113. shl eax, cl
  114. dec eax
  115. and eax, edx ; /* eax &= hold */
  116. shr rdx, cl
  117. add r14d, eax ; /* len += hold & mask[op] */
  118. L_decode_distance:
  119. mov r8, r13 ; /* r8 = dmask */
  120. cmp bl, 32
  121. ja L_get_distance_code ; /* if (32 < bits) */
  122. lodsd ; /* eax = *(uint *)in++ */
  123. mov cl, bl ; /* cl = bits, needs it for shifting */
  124. add bl, 32 ; /* bits += 32 */
  125. shl rax, cl
  126. or rdx, rax ; /* hold |= *((uint *)in)++ << bits */
  127. L_get_distance_code:
  128. and r8, rdx ; /* r8 &= hold */
  129. mov eax, [r11+r8*4] ; /* eax = dcode[hold & dmask] */
  130. L_dodist:
  131. mov r15d, eax ; /* dist = this */
  132. shr r15d, 16 ; /* dist = this.val */
  133. mov cl, ah
  134. sub bl, ah ; /* bits -= this.bits */
  135. shr rdx, cl ; /* hold >>= this.bits */
  136. mov cl, al ; /* cl = this.op */
  137. test al, 16 ; /* if ((op & 16) == 0) */
  138. jz L_test_for_second_level_dist
  139. and cl, 15 ; /* op &= 15 */
  140. jz L_check_dist_one
  141. L_add_bits_to_dist:
  142. sub bl, cl
  143. xor eax, eax
  144. inc eax
  145. shl eax, cl
  146. dec eax ; /* (1 << op) - 1 */
  147. and eax, edx ; /* eax &= hold */
  148. shr rdx, cl
  149. add r15d, eax ; /* dist += hold & ((1 << op) - 1) */
  150. L_check_window:
  151. mov r8, rsi ; /* save in so from can use it's reg */
  152. mov rax, rdi
  153. sub rax, [rsp+40] ; /* nbytes = out - beg */
  154. cmp eax, r15d
  155. jb L_clip_window ; /* if (dist > nbytes) 4.2% */
  156. mov ecx, r14d ; /* ecx = len */
  157. mov rsi, rdi
  158. sub rsi, r15 ; /* from = out - dist */
  159. sar ecx, 1
  160. jnc L_copy_two ; /* if len % 2 == 0 */
  161. rep movsw
  162. mov al, [rsi]
  163. mov [rdi], al
  164. inc rdi
  165. mov rsi, r8 ; /* move in back to %rsi, toss from */
  166. jmp L_while_test
  167. L_copy_two:
  168. rep movsw
  169. mov rsi, r8 ; /* move in back to %rsi, toss from */
  170. jmp L_while_test
  171. ALIGN 4
  172. L_check_dist_one:
  173. cmp r15d, 1 ; /* if dist 1, is a memset */
  174. jne L_check_window
  175. cmp [rsp+40], rdi ; /* if out == beg, outside window */
  176. je L_check_window
  177. mov ecx, r14d ; /* ecx = len */
  178. mov al, [rdi-1]
  179. mov ah, al
  180. sar ecx, 1
  181. jnc L_set_two
  182. mov [rdi], al
  183. inc rdi
  184. L_set_two:
  185. rep stosw
  186. jmp L_while_test
  187. ALIGN 4
  188. L_test_for_second_level_length:
  189. test al, 64
  190. jnz L_test_for_end_of_block ; /* if ((op & 64) != 0) */
  191. xor eax, eax
  192. inc eax
  193. shl eax, cl
  194. dec eax
  195. and eax, edx ; /* eax &= hold */
  196. add eax, r14d ; /* eax += len */
  197. mov eax, [rbp+rax*4] ; /* eax = lcode[val+(hold&mask[op])]*/
  198. jmp L_dolen
  199. ALIGN 4
  200. L_test_for_second_level_dist:
  201. test al, 64
  202. jnz L_invalid_distance_code ; /* if ((op & 64) != 0) */
  203. xor eax, eax
  204. inc eax
  205. shl eax, cl
  206. dec eax
  207. and eax, edx ; /* eax &= hold */
  208. add eax, r15d ; /* eax += dist */
  209. mov eax, [r11+rax*4] ; /* eax = dcode[val+(hold&mask[op])]*/
  210. jmp L_dodist
  211. ALIGN 4
  212. L_clip_window:
  213. mov ecx, eax ; /* ecx = nbytes */
  214. mov eax, [rsp+92] ; /* eax = wsize, prepare for dist cmp */
  215. neg ecx ; /* nbytes = -nbytes */
  216. cmp eax, r15d
  217. jb L_invalid_distance_too_far ; /* if (dist > wsize) */
  218. add ecx, r15d ; /* nbytes = dist - nbytes */
  219. cmp dword ptr [rsp+96], 0
  220. jne L_wrap_around_window ; /* if (write != 0) */
  221. mov rsi, [rsp+56] ; /* from = window */
  222. sub eax, ecx ; /* eax -= nbytes */
  223. add rsi, rax ; /* from += wsize - nbytes */
  224. mov eax, r14d ; /* eax = len */
  225. cmp r14d, ecx
  226. jbe L_do_copy ; /* if (nbytes >= len) */
  227. sub eax, ecx ; /* eax -= nbytes */
  228. rep movsb
  229. mov rsi, rdi
  230. sub rsi, r15 ; /* from = &out[ -dist ] */
  231. jmp L_do_copy
  232. ALIGN 4
  233. L_wrap_around_window:
  234. mov eax, [rsp+96] ; /* eax = write */
  235. cmp ecx, eax
  236. jbe L_contiguous_in_window ; /* if (write >= nbytes) */
  237. mov esi, [rsp+92] ; /* from = wsize */
  238. add rsi, [rsp+56] ; /* from += window */
  239. add rsi, rax ; /* from += write */
  240. sub rsi, rcx ; /* from -= nbytes */
  241. sub ecx, eax ; /* nbytes -= write */
  242. mov eax, r14d ; /* eax = len */
  243. cmp eax, ecx
  244. jbe L_do_copy ; /* if (nbytes >= len) */
  245. sub eax, ecx ; /* len -= nbytes */
  246. rep movsb
  247. mov rsi, [rsp+56] ; /* from = window */
  248. mov ecx, [rsp+96] ; /* nbytes = write */
  249. cmp eax, ecx
  250. jbe L_do_copy ; /* if (nbytes >= len) */
  251. sub eax, ecx ; /* len -= nbytes */
  252. rep movsb
  253. mov rsi, rdi
  254. sub rsi, r15 ; /* from = out - dist */
  255. jmp L_do_copy
  256. ALIGN 4
  257. L_contiguous_in_window:
  258. mov rsi, [rsp+56] ; /* rsi = window */
  259. add rsi, rax
  260. sub rsi, rcx ; /* from += write - nbytes */
  261. mov eax, r14d ; /* eax = len */
  262. cmp eax, ecx
  263. jbe L_do_copy ; /* if (nbytes >= len) */
  264. sub eax, ecx ; /* len -= nbytes */
  265. rep movsb
  266. mov rsi, rdi
  267. sub rsi, r15 ; /* from = out - dist */
  268. jmp L_do_copy ; /* if (nbytes >= len) */
  269. ALIGN 4
  270. L_do_copy:
  271. mov ecx, eax ; /* ecx = len */
  272. rep movsb
  273. mov rsi, r8 ; /* move in back to %esi, toss from */
  274. jmp L_while_test
  275. L_test_for_end_of_block:
  276. test al, 32
  277. jz L_invalid_literal_length_code
  278. mov dword ptr [rsp+116], 1
  279. jmp L_break_loop_with_status
  280. L_invalid_literal_length_code:
  281. mov dword ptr [rsp+116], 2
  282. jmp L_break_loop_with_status
  283. L_invalid_distance_code:
  284. mov dword ptr [rsp+116], 3
  285. jmp L_break_loop_with_status
  286. L_invalid_distance_too_far:
  287. mov dword ptr [rsp+116], 4
  288. jmp L_break_loop_with_status
  289. L_break_loop:
  290. mov dword ptr [rsp+116], 0
  291. L_break_loop_with_status:
  292. ; /* put in, out, bits, and hold back into ar and pop esp */
  293. mov [rsp+16], rsi ; /* in */
  294. mov [rsp+32], rdi ; /* out */
  295. mov [rsp+88], ebx ; /* bits */
  296. mov [rsp+80], rdx ; /* hold */
  297. mov rax, [rsp] ; /* restore rbp and rsp */
  298. mov rbp, [rsp+8]
  299. mov rsp, rax
  300. mov rsi,[rsp-8]
  301. mov rdi,[rsp-16]
  302. mov r12,[rsp-24]
  303. mov r13,[rsp-32]
  304. mov r14,[rsp-40]
  305. mov r15,[rsp-48]
  306. mov rbx,[rsp-56]
  307. ret 0
  308. ; :
  309. ; : "m" (ar)
  310. ; : "memory", "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",
  311. ; "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"
  312. ; );
  313. inffas8664fnc ENDP
  314. ;_TEXT ENDS
  315. END