summaryrefslogtreecommitdiffstats
path: root/compat/zlib/contrib/masmx64/inffasx64.asm
blob: b5d93a272abc46d4676b4a0f5ced980bbbd2bbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
; inffasx64.asm is a hand tuned assembler version of inffast.c - fast decoding

; version for AMD64 on Windows using Microsoft C compiler

;

; inffasx64.asm is automatically convert from AMD64 portion of inffas86.c

; inffasx64.asm is called by inffas8664.c, which contain more info.



; to compile this file, I use option

;   ml64.exe /Flinffasx64 /c /Zi inffasx64.asm

;   with Microsoft Macro Assembler (x64) for AMD64

;

;   ml64.exe is given with Visual Studio 2005, Windows 2003 server DDK

;

;   (you can get Windows 2003 server DDK with ml64 and cl.exe for AMD64 from 

;      http://www.microsoft.com/whdc/devtools/ddk/default.mspx for low price)

;


.code
inffas8664fnc PROC

; see http://weblogs.asp.net/oldnewthing/archive/2004/01/14/58579.aspx and

; http://msdn.microsoft.com/library/en-us/kmarch/hh/kmarch/64bitAMD_8e951dd2-ee77-4728-8702-55ce4b5dd24a.xml.asp

;

; All registers must be preserved across the call, except for

;   rax, rcx, rdx, r8, r-9, r10, and r11, which are scratch.



	mov [rsp-8],rsi
	mov [rsp-16],rdi
	mov [rsp-24],r12
	mov [rsp-32],r13
	mov [rsp-40],r14
	mov [rsp-48],r15
	mov [rsp-56],rbx

	mov rax,rcx

	mov	[rax+8], rbp       ; /* save regs rbp and rsp */

	mov	[rax], rsp

	mov	rsp, rax          ; /* make rsp point to &ar */


	mov	rsi, [rsp+16]      ; /* rsi  = in */

	mov	rdi, [rsp+32]      ; /* rdi  = out */

	mov	r9, [rsp+24]       ; /* r9   = last */

	mov	r10, [rsp+48]      ; /* r10  = end */

	mov	rbp, [rsp+64]      ; /* rbp  = lcode */

	mov	r11, [rsp+72]      ; /* r11  = dcode */

	mov	rdx, [rsp+80]      ; /* rdx  = hold */

	mov	ebx, [rsp+88]      ; /* ebx  = bits */

	mov	r12d, [rsp+100]    ; /* r12d = lmask */

	mov	r13d, [rsp+104]    ; /* r13d = dmask */

                                          ; /* r14d = len */

                                          ; /* r15d = dist */



	cld
	cmp	r10, rdi
	je	L_one_time           ; /* if only one decode left */

	cmp	r9, rsi

    jne L_do_loop


L_one_time:
	mov	r8, r12           ; /* r8 = lmask */

	cmp	bl, 32
	ja	L_get_length_code_one_time

	lodsd                         ; /* eax = *(uint *)in++ */

	mov	cl, bl            ; /* cl = bits, needs it for shifting */

	add	bl, 32             ; /* bits += 32 */

	shl	rax, cl
	or	rdx, rax          ; /* hold |= *((uint *)in)++ << bits */

	jmp	L_get_length_code_one_time

ALIGN 4
L_while_test:
	cmp	r10, rdi
	jbe	L_break_loop
	cmp	r9, rsi
	jbe	L_break_loop

L_do_loop:
	mov	r8, r12           ; /* r8 = lmask */

	cmp	bl, 32
	ja	L_get_length_code    ; /* if (32 < bits) */


	lodsd                         ; /* eax = *(uint *)in++ */

	mov	cl, bl            ; /* cl = bits, needs it for shifting */

	add	bl, 32             ; /* bits += 32 */

	shl	rax, cl
	or	rdx, rax          ; /* hold |= *((uint *)in)++ << bits */


L_get_length_code:
	and	r8, rdx            ; /* r8 &= hold */

	mov	eax, [rbp+r8*4]  ; /* eax = lcode[hold & lmask] */


	mov	cl, ah            ; /* cl = this.bits */

	sub	bl, ah            ; /* bits -= this.bits */

	shr	rdx, cl           ; /* hold >>= this.bits */


	test	al, al
	jnz	L_test_for_length_base ; /* if (op != 0) 45.7% */


	mov	r8, r12            ; /* r8 = lmask */

	shr	eax, 16            ; /* output this.val char */

	stosb

L_get_length_code_one_time:
	and	r8, rdx            ; /* r8 &= hold */

	mov	eax, [rbp+r8*4] ; /* eax = lcode[hold & lmask] */


L_dolen:
	mov	cl, ah            ; /* cl = this.bits */

	sub	bl, ah            ; /* bits -= this.bits */

	shr	rdx, cl           ; /* hold >>= this.bits */


	test	al, al
	jnz	L_test_for_length_base ; /* if (op != 0) 45.7% */


	shr	eax, 16            ; /* output this.val char */

	stosb
	jmp	L_while_test

ALIGN 4
L_test_for_length_base:
	mov	r14d, eax         ; /* len = this */

	shr	r14d, 16           ; /* len = this.val */

	mov	cl, al

	test	al, 16
	jz	L_test_for_second_level_length ; /* if ((op & 16) == 0) 8% */

	and	cl, 15             ; /* op &= 15 */

	jz	L_decode_distance    ; /* if (!op) */


L_add_bits_to_len:
	sub	bl, cl
	xor	eax, eax
	inc	eax
	shl	eax, cl
	dec	eax
	and	eax, edx          ; /* eax &= hold */

	shr	rdx, cl
	add	r14d, eax         ; /* len += hold & mask[op] */


L_decode_distance:
	mov	r8, r13           ; /* r8 = dmask */

	cmp	bl, 32
	ja	L_get_distance_code  ; /* if (32 < bits) */


	lodsd                         ; /* eax = *(uint *)in++ */

	mov	cl, bl            ; /* cl = bits, needs it for shifting */

	add	bl, 32             ; /* bits += 32 */

	shl	rax, cl
	or	rdx, rax          ; /* hold |= *((uint *)in)++ << bits */


L_get_distance_code:
	and	r8, rdx           ; /* r8 &= hold */

	mov	eax, [r11+r8*4] ; /* eax = dcode[hold & dmask] */


L_dodist:
	mov	r15d, eax         ; /* dist = this */

	shr	r15d, 16           ; /* dist = this.val */

	mov	cl, ah
	sub	bl, ah            ; /* bits -= this.bits */

	shr	rdx, cl           ; /* hold >>= this.bits */

	mov	cl, al            ; /* cl = this.op */


	test	al, 16             ; /* if ((op & 16) == 0) */

	jz	L_test_for_second_level_dist
	and	cl, 15             ; /* op &= 15 */

	jz	L_check_dist_one

L_add_bits_to_dist:
	sub	bl, cl
	xor	eax, eax
	inc	eax
	shl	eax, cl
	dec	eax                 ; /* (1 << op) - 1 */

	and	eax, edx          ; /* eax &= hold */

	shr	rdx, cl
	add	r15d, eax         ; /* dist += hold & ((1 << op) - 1) */


L_check_window:
	mov	r8, rsi           ; /* save in so from can use it's reg */

	mov	rax, rdi
	sub	rax, [rsp+40]      ; /* nbytes = out - beg */


	cmp	eax, r15d
	jb	L_clip_window        ; /* if (dist > nbytes) 4.2% */


	mov	ecx, r14d         ; /* ecx = len */

	mov	rsi, rdi
	sub	rsi, r15          ; /* from = out - dist */


	sar	ecx, 1
	jnc	L_copy_two           ; /* if len % 2 == 0 */


	rep     movsw
	mov	al, [rsi]
	mov	[rdi], al
	inc	rdi

	mov	rsi, r8           ; /* move in back to %rsi, toss from */

	jmp	L_while_test

L_copy_two:
	rep     movsw
	mov	rsi, r8           ; /* move in back to %rsi, toss from */

	jmp	L_while_test

ALIGN 4
L_check_dist_one:
	cmp	r15d, 1            ; /* if dist 1, is a memset */

	jne	L_check_window
	cmp	[rsp+40], rdi      ; /* if out == beg, outside window */

	je	L_check_window

	mov	ecx, r14d         ; /* ecx = len */

	mov	al, [rdi-1]
	mov	ah, al

	sar	ecx, 1
	jnc	L_set_two
	mov	[rdi], al
	inc	rdi

L_set_two:
	rep     stosw
	jmp	L_while_test

ALIGN 4
L_test_for_second_level_length:
	test	al, 64
	jnz	L_test_for_end_of_block ; /* if ((op & 64) != 0) */


	xor	eax, eax
	inc	eax
	shl	eax, cl
	dec	eax
	and	eax, edx         ; /* eax &= hold */

	add	eax, r14d        ; /* eax += len */

	mov	eax, [rbp+rax*4] ; /* eax = lcode[val+(hold&mask[op])]*/

	jmp	L_dolen

ALIGN 4
L_test_for_second_level_dist:
	test	al, 64
	jnz	L_invalid_distance_code ; /* if ((op & 64) != 0) */


	xor	eax, eax
	inc	eax
	shl	eax, cl
	dec	eax
	and	eax, edx         ; /* eax &= hold */

	add	eax, r15d        ; /* eax += dist */

	mov	eax, [r11+rax*4] ; /* eax = dcode[val+(hold&mask[op])]*/

	jmp	L_dodist

ALIGN 4
L_clip_window:
	mov	ecx, eax         ; /* ecx = nbytes */

	mov	eax, [rsp+92]     ; /* eax = wsize, prepare for dist cmp */

	neg	ecx                ; /* nbytes = -nbytes */


	cmp	eax, r15d
	jb	L_invalid_distance_too_far ; /* if (dist > wsize) */


	add	ecx, r15d         ; /* nbytes = dist - nbytes */

	cmp	dword ptr [rsp+96], 0
	jne	L_wrap_around_window ; /* if (write != 0) */


	mov	rsi, [rsp+56]     ; /* from  = window */

	sub	eax, ecx         ; /* eax  -= nbytes */

	add	rsi, rax         ; /* from += wsize - nbytes */


	mov	eax, r14d        ; /* eax = len */

	cmp	r14d, ecx
	jbe	L_do_copy           ; /* if (nbytes >= len) */


	sub	eax, ecx         ; /* eax -= nbytes */

	rep     movsb
	mov	rsi, rdi
	sub	rsi, r15         ; /* from = &out[ -dist ] */

	jmp	L_do_copy

ALIGN 4
L_wrap_around_window:
	mov	eax, [rsp+96]     ; /* eax = write */

	cmp	ecx, eax
	jbe	L_contiguous_in_window ; /* if (write >= nbytes) */


	mov	esi, [rsp+92]     ; /* from  = wsize */

	add	rsi, [rsp+56]     ; /* from += window */

	add	rsi, rax         ; /* from += write */

	sub	rsi, rcx         ; /* from -= nbytes */

	sub	ecx, eax         ; /* nbytes -= write */


	mov	eax, r14d        ; /* eax = len */

	cmp	eax, ecx
	jbe	L_do_copy           ; /* if (nbytes >= len) */


	sub	eax, ecx         ; /* len -= nbytes */

	rep     movsb
	mov	rsi, [rsp+56]     ; /* from = window */

	mov	ecx, [rsp+96]     ; /* nbytes = write */

	cmp	eax, ecx
	jbe	L_do_copy           ; /* if (nbytes >= len) */


	sub	eax, ecx         ; /* len -= nbytes */

	rep     movsb
	mov	rsi, rdi
	sub	rsi, r15         ; /* from = out - dist */

	jmp	L_do_copy

ALIGN 4
L_contiguous_in_window:
	mov	rsi, [rsp+56]     ; /* rsi = window */

	add	rsi, rax
	sub	rsi, rcx         ; /* from += write - nbytes */


	mov	eax, r14d        ; /* eax = len */

	cmp	eax, ecx
	jbe	L_do_copy           ; /* if (nbytes >= len) */


	sub	eax, ecx         ; /* len -= nbytes */

	rep     movsb
	mov	rsi, rdi
	sub	rsi, r15         ; /* from = out - dist */

	jmp	L_do_copy           ; /* if (nbytes >= len) */


ALIGN 4
L_do_copy:
	mov	ecx, eax         ; /* ecx = len */

	rep     movsb

	mov	rsi, r8          ; /* move in back to %esi, toss from */

	jmp	L_while_test

L_test_for_end_of_block:
	test	al, 32
	jz	L_invalid_literal_length_code
	mov	dword ptr [rsp+116], 1
	jmp	L_break_loop_with_status

L_invalid_literal_length_code:
	mov	dword ptr [rsp+116], 2
	jmp	L_break_loop_with_status

L_invalid_distance_code:
	mov	dword ptr [rsp+116], 3
	jmp	L_break_loop_with_status

L_invalid_distance_too_far:
	mov	dword ptr [rsp+116], 4
	jmp	L_break_loop_with_status

L_break_loop:
	mov	dword ptr [rsp+116], 0

L_break_loop_with_status:
; /* put in, out, bits, and hold back into ar and pop esp */

	mov	[rsp+16], rsi     ; /* in */

	mov	[rsp+32], rdi     ; /* out */

	mov	[rsp+88], ebx     ; /* bits */

	mov	[rsp+80], rdx     ; /* hold */


	mov	rax, [rsp]       ; /* restore rbp and rsp */

	mov	rbp, [rsp+8]
	mov	rsp, rax



	mov rsi,[rsp-8]
	mov rdi,[rsp-16]
	mov r12,[rsp-24]
	mov r13,[rsp-32]
	mov r14,[rsp-40]
	mov r15,[rsp-48]
	mov rbx,[rsp-56]
	
    ret 0
;          :

;          : "m" (ar)

;          : "memory", "%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi",

;            "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"

;    );


inffas8664fnc 	ENDP
;_TEXT	ENDS

END