1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 ; Redistribution and use in source and binary forms, with or without
5 ; modification, are permitted provided that the following conditions
7 ; * Redistributions of source code must retain the above copyright
8 ; notice, this list of conditions and the following disclaimer.
9 ; * Redistributions in binary form must reproduce the above copyright
10 ; notice, this list of conditions and the following disclaimer in
11 ; the documentation and/or other materials provided with the
13 ; * Neither the name of Intel Corporation nor the names of its
14 ; contributors may be used to endorse or promote products derived
15 ; from this software without specific prior written permission.
17 ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 ; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 ; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 ; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 ; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 ; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 %include "options.asm"
35 %include "lz0a_const.asm"
36 %include "data_struct2.asm"
37 %include "bitbuf2.asm"
38 %include "huffman.asm"
39 %include "igzip_compare_types.asm"
41 %include "reg_sizes.asm"
46 %define MOVDQA vmovdqa
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
67 %define b_bytes_valid rax
85 %define file_start rdi
87 %define m_bit_count rbp
110 %define hufftables r15
112 %define crc_0 xmm0 ; in/out: crc state
113 %define crc_1 xmm1 ; in/out: crc state
114 %define crc_2 xmm2 ; in/out: crc state
115 %define crc_3 xmm3 ; in/out: crc state
116 %define crc_fold xmm4 ; in: (loaded from fold_4)
118 %define xtmp0 xmm5 ; tmp
119 %define xtmp1 xmm6 ; tmp
120 %define xtmp2 xmm7 ; tmp
121 %define xtmp3 xmm8 ; tmp
122 %define xtmp4 xmm9 ; tmp
124 %define ytmp0 ymm5 ; tmp
125 %define ytmp1 ymm6 ; tmp
128 %define vtmp0 ymm5 ; tmp
129 %define vtmp1 ymm6 ; tmp
130 %define vtmp2 ymm7 ; tmp
131 %define vtmp3 ymm8 ; tmp
132 %define vtmp4 ymm9 ; tmp
134 %define vtmp0 xmm5 ; tmp
135 %define vtmp1 xmm6 ; tmp
136 %define vtmp2 xmm7 ; tmp
137 %define vtmp3 xmm8 ; tmp
138 %define vtmp4 xmm9 ; tmp
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 %define b_bytes_processed f_i
146 blen_mem_offset equ 0 ; local variable (8 bytes)
147 in_buf_mem_offset equ 8
148 f_end_i_mem_offset equ 16
149 empty_buffer_flag equ 24
150 gpr_save_mem_offset equ 32 ; gpr save area (8*8 bytes)
151 xmm_save_mem_offset equ 32 + 8*8 ; xmm save area (4*16 bytes) (16 byte aligned)
152 stack_size equ 4*8 + 8*8 + 4*16 + 8
153 ;;; 8 because stack address is odd multiple of 8 after a function call and
154 ;;; we want it aligned to 16 bytes
156 ; void isal_deflate_body ( isal_zstream *stream )
157 ; arg 1: rcx: addr of stream
158 global isal_deflate_body_ %+ ARCH
159 isal_deflate_body_ %+ ARCH %+ :
160 %ifidn __OUTPUT_FORMAT__, elf64
164 ;; do nothing if (avail_in == 0)
165 cmp dword [rcx + _avail_in], 0
168 ;; Set stream's next state
169 mov rdx, ZSTATE_FLUSH_READ_BUFFER
171 cmp dword [rcx + _end_of_stream], 0
173 cmp dword [rcx + _flush], _NO_FLUSH
175 mov dword [rcx + _internal_state_state], eax
188 mov [rsp + gpr_save_mem_offset + 0*8], rbx
189 mov [rsp + gpr_save_mem_offset + 1*8], rsi
190 mov [rsp + gpr_save_mem_offset + 2*8], rdi
191 mov [rsp + gpr_save_mem_offset + 3*8], rbp
192 mov [rsp + gpr_save_mem_offset + 4*8], r12
193 mov [rsp + gpr_save_mem_offset + 5*8], r13
194 mov [rsp + gpr_save_mem_offset + 6*8], r14
195 mov [rsp + gpr_save_mem_offset + 7*8], r15
196 MOVDQA [rsp + xmm_save_mem_offset + 0*16], xmm6
197 MOVDQA [rsp + xmm_save_mem_offset + 1*16], xmm7
198 MOVDQA [rsp + xmm_save_mem_offset + 2*16], xmm8
199 MOVDQA [rsp + xmm_save_mem_offset + 3*16], xmm9
203 MOVDQA crc_0, [stream + _internal_state_crc + 0*16]
204 MOVDQA crc_1, [stream + _internal_state_crc + 1*16]
205 MOVDQA crc_2, [stream + _internal_state_crc + 2*16]
206 MOVDQA crc_3, [stream + _internal_state_crc + 3*16]
207 MOVDQA crc_fold, [fold_4]
208 mov dword [stream + _internal_state_has_eob], 0
210 ; state->bitbuf.set_buf(stream->next_out, stream->avail_out);
211 mov m_out_buf, [stream + _next_out]
212 mov [stream + _internal_state_bitbuf_m_out_start], m_out_buf
213 mov tmp1 %+ d, [stream + _avail_out]
217 mov [stream + _internal_state_bitbuf_m_out_end], tmp1
219 mov m_bits, [stream + _internal_state_bitbuf_m_bits]
220 mov m_bit_count %+ d, [stream + _internal_state_bitbuf_m_bit_count]
222 mov hufftables, [stream + _hufftables]
223 ; in_buf = stream->next_in
224 mov in_buf, [stream + _next_in]
225 mov blen %+ d, [stream + _avail_in]
227 mov dword [rsp + empty_buffer_flag], 0
228 cmp dword [stream + _flush], _FULL_FLUSH
229 sete byte [rsp + empty_buffer_flag]
230 cmp dword [stream + _internal_state_b_bytes_processed], 0
231 sete byte [rsp + empty_buffer_flag + 1]
234 MARK __Compute_X_ %+ ARCH
236 ; x = D + LA - (state->b_bytes_valid - state->b_bytes_processed);
237 mov b_bytes_valid %+ d, [stream + _internal_state_b_bytes_valid]
238 mov b_bytes_processed %+ d, [stream + _internal_state_b_bytes_processed]
239 lea x, [b_bytes_processed + D + LA]
246 ; if (blen < D) x = blen;
250 ;; process x bytes starting at in_buf
252 ;; If there isn't enough room, shift buffer down
253 ; if (x > BSIZE - state->b_bytes_valid) {
255 sub tmp1, b_bytes_valid
259 ; if (state->b_bytes_processed < state->b_bytes_valid - LA) {
260 mov tmp1, b_bytes_valid
262 cmp b_bytes_processed, tmp1
265 ;; We need to move an odd amount, skip move for this copy of loop
267 mov [rsp + blen_mem_offset], blen
270 MARK __shift_data_down_ %+ ARCH
272 ; offset = state->b_bytes_valid - (D + LA);
273 mov tmp4, b_bytes_valid
275 ; copy_D_LA(state->buffer, state->buffer + offset);
276 lea tmp1, [stream + _internal_state_buffer]
277 lea tmp2, [tmp1 + tmp4]
278 copy_D_LA tmp1, tmp2, tmp3, vtmp0, vtmp1, vtmp2, vtmp3
281 ; state->file_start -= offset;
282 sub [stream + _internal_state_file_start], tmp4
283 ; state->b_bytes_processed -= offset;
284 sub b_bytes_processed, tmp4
285 mov b_bytes_valid, D + LA
287 MARK __copy_in_ %+ ARCH
291 mov [rsp + blen_mem_offset], blen
293 ; copy_in(state->buffer + state->b_bytes_valid, in_buf, x);
294 lea tmp1, [stream + _internal_state_buffer + b_bytes_valid]
299 COPY_IN_CRC tmp1, tmp2, tmp3, tmp4, crc_0, crc_1, crc_2, crc_3, crc_fold, \
300 xtmp0, xtmp1, xtmp2, xtmp3, xtmp4
304 MARK __prepare_loop_ %+ ARCH
306 mov [rsp + in_buf_mem_offset], in_buf
307 ; state->b_bytes_valid += x;
309 mov [stream + _internal_state_b_bytes_valid], b_bytes_valid %+ d
311 ; f_end_i = state->b_bytes_valid - LA;
312 %ifnidn f_end_i, b_bytes_valid
313 mov f_end_i, b_bytes_valid
316 ; if (f_end_i <= 0) continue;
320 ; f_start_i = state->b_bytes_processed;
321 ;; f_i and b_bytes_processed are same register, just store b_bytes_proc
322 mov [stream + _internal_state_b_bytes_processed], b_bytes_processed %+ d
324 ; f_start_i += (uint32_t)(state->buffer - state->file_start);
325 mov file_start, [stream + _internal_state_file_start]
326 lea tmp1, [stream + _internal_state_buffer]
330 mov [rsp + f_end_i_mem_offset], f_end_i
332 ; for (f_i = f_start_i; f_i < f_end_i; f_i++) {
336 MARK __misc_compute_hash_lookup_ %+ ARCH
337 mov curr_data %+ d, [file_start + f_i]
339 cmp dword [rsp + empty_buffer_flag], 0
342 mov curr_data2, curr_data
344 compute_hash hash, curr_data
351 xor hash2 %+ d, hash2 %+ d
352 crc32 hash2 %+ d, curr_data2 %+ d
354 ; hash = compute_hash(state->file_start + f_i) & HASH_MASK;
355 and hash %+ d, HASH_MASK
356 and hash2 %+ d, HASH_MASK
358 ; if (state->bitbuf.is_full()) {
359 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
366 lea tmp1, [file_start + f_i]
369 mov dist %+ w, f_i %+ w
370 sub dist %+ w, word [stream + _internal_state_head + 2 * hash]
372 ; state->head[hash] = (uint16_t) f_i;
373 mov [stream + _internal_state_head + 2 * hash], f_i %+ w
377 mov dist2 %+ w, f_i %+ w
378 sub dist2 %+ w, word [stream + _internal_state_head + 2 * hash2]
381 ; state->head[hash2] = (uint16_t) f_i;
382 mov [stream + _internal_state_head + 2 * hash2], f_i %+ w
388 ; if ((dist-1) < (D-1)) {
394 cmp dist2 %+ d, (D-1)
398 MARK __compare_ %+ ARCH
399 ; len = compare258(state->file_start + f_i,
400 ; state->file_start + f_i - dist);
402 ;; Specutively load distance code (except for when large windows are used)
403 get_packed_dist_code dist, code2, hufftables
405 ;; Check for long len/dist match (>7) with first literal
415 lea tmp1, [file_start + f_i]
419 ;; Specutively load distance code (except for when large windows are used)
420 get_packed_dist_code dist2, code4, hufftables
422 ;; Check for len/dist match (>7) with second literal
428 ;; Check for len/dist match for first literal
430 jz len_dist_lit_huffman_pre
434 jmp len_dist_huffman_pre
437 MARK __len_dist_lit_huffman_ %+ ARCH
438 len_dist_lit_huffman_pre:
439 movzx tmp1, curr_data %+ b
440 get_lit_code tmp1, code3, code_len3, hufftables
442 ;; Specutively load the code for the first literal
443 movzx tmp1, curr_data %+ b
444 get_lit_code tmp1, code3, rcx, hufftables
446 ;; Check for len/dist match for first literal
448 jz len_dist_huffman_pre
450 ;; Specutively load the code for the second literal
453 get_lit_code curr_data, code2, code_len2, hufftables
459 ;; Check for len/dist match for second literal
463 MARK __len_dist_lit_huffman_ %+ ARCH
464 len_dist_lit_huffman_pre:
470 len_dist_lit_huffman:
471 %ifndef LONGER_HUFFTABLE
473 get_dist_code tmp4, code4, code_len2, hufftables ;; clobbers dist, rcx
475 unpack_dist_code code4, code_len2
477 get_len_code len2, code, rcx, hufftables ;; rcx is code_len
480 shlx code4, code4, rcx
490 shlx code4, code4, rcx
498 ;; Setup for updating hash
499 lea tmp3, [f_i + 1] ; tmp3 <= k
502 ; hash = compute_hash(state->file_start + k) & HASH_MASK;
503 mov tmp5 %+ d, [file_start + tmp3]
507 compute_hash hash, tmp5
508 and hash %+ d, HASH_MASK
510 ; state->head[hash] = k;
511 mov [stream + _internal_state_head + 2 * hash], tmp3 %+ w
515 jmp update_hash_for_symbol
516 ;; encode as dist/len
518 MARK __len_dist_huffman_ %+ ARCH
519 len_dist_huffman_pre:
525 ; get_dist_code(dist, &code2, &code_len2);
526 %ifndef LONGER_HUFFTABLE
527 mov tmp3, dist ; since code2 and dist are rbx
528 get_dist_code tmp3, code2, code_len2, hufftables ;; clobbers dist, rcx
530 unpack_dist_code code2, code_len2
532 ; get_len_code(len, &code, &code_len);
533 get_len_code len, code, rcx, hufftables ;; rcx is code_len
537 ; code_len2 += code_len
539 shlx code2, code2, rcx
546 ;; Setup for updateing hash
547 lea tmp3, [f_i + 2] ; tmp3 <= k
549 mov tmp7 %+ d, [file_start + tmp3]
551 MARK __update_hash_for_symbol_ %+ ARCH
552 update_hash_for_symbol:
553 mov curr_data %+ d, [file_start + f_i]
554 mov curr_data2, curr_data
555 compute_hash hash, curr_data
556 %ifdef LIMIT_HASH_UPDATE
557 ; only update hash twice, first hash was already calculated.
559 ; hash = compute_hash(state->file_start + k) & HASH_MASK;
560 compute_hash hash2, tmp7
561 and hash2 %+ d, HASH_MASK
562 ; state->head[hash] = k;
563 mov [stream + _internal_state_head + 2 * hash2], tmp3 %+ w
567 ; hash = compute_hash(state->file_start + k) & HASH_MASK;
568 mov tmp7 %+ d, [file_start + tmp3]
569 compute_hash hash2, tmp7
570 and hash2 %+ d, HASH_MASK
571 ; state->head[hash] = k;
572 mov [stream + _internal_state_head + 2 * hash2], tmp3 %+ w
579 MARK __write_len_dist_bits_ %+ ARCH
580 mov f_end_i, [rsp + f_end_i_mem_offset]
581 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf, tmp3
589 MARK __write_lit_bits_ %+ ARCH
592 movzx tmp1, curr_data %+ b
593 get_lit_code tmp1, code3, rcx, hufftables
597 get_lit_code curr_data, code2, code_len2, hufftables
599 ;; Calculate code associated with both literals
600 shlx code2, code2, rcx
605 mov f_end_i, [rsp + f_end_i_mem_offset]
607 mov curr_data %+ d, [file_start + f_i]
608 mov curr_data2, curr_data
610 compute_hash hash, curr_data
612 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf, tmp3
619 MARK __end_loops_ %+ ARCH
622 ; state->b_bytes_processed = f_i - (state->buffer - state->file_start);
623 add f_i, [stream + _internal_state_file_start]
625 sub f_i, _internal_state_buffer
626 mov [stream + _internal_state_b_bytes_processed], f_i %+ d
630 mov blen, [rsp + blen_mem_offset]
631 mov in_buf, [rsp + in_buf_mem_offset]
636 ;; update input buffer
637 ; stream->total_in += (uint32_t)(in_buf - stream->next_in); // bytes copied
638 mov tmp1 %+ d, [stream + _total_in]
639 mov in_buf, [rsp + in_buf_mem_offset]
641 sub tmp1, [stream + _next_in]
642 mov [stream + _total_in], tmp1 %+ d
644 mov [stream + _next_in], in_buf
645 mov [stream + _avail_in], blen %+ d
650 ;; Set stream's next state
651 mov tmp1, ZSTATE_FLUSH_READ_BUFFER
652 mov tmp5, ZSTATE_BODY
653 cmp dword [stream + _end_of_stream], 0
655 cmp dword [stream + _flush], _NO_FLUSH
657 mov dword [stream + _internal_state_state], tmp5 %+ d
659 mov [stream + _next_out], m_out_buf
660 ; offset = state->bitbuf.buffer_used();
661 sub m_out_buf, [stream + _internal_state_bitbuf_m_out_start]
662 sub [stream + _avail_out], m_out_buf %+ d
663 add [stream + _total_out], m_out_buf %+ d
665 mov [stream + _internal_state_bitbuf_m_bits], m_bits
666 mov [stream + _internal_state_bitbuf_m_bit_count], m_bit_count %+ d
669 MOVDQA [stream + _internal_state_crc + 0*16], crc_0
670 MOVDQA [stream + _internal_state_crc + 1*16], crc_1
671 MOVDQA [stream + _internal_state_crc + 2*16], crc_2
672 MOVDQA [stream + _internal_state_crc + 3*16], crc_3
674 mov rbx, [rsp + gpr_save_mem_offset + 0*8]
675 mov rsi, [rsp + gpr_save_mem_offset + 1*8]
676 mov rdi, [rsp + gpr_save_mem_offset + 2*8]
677 mov rbp, [rsp + gpr_save_mem_offset + 3*8]
678 mov r12, [rsp + gpr_save_mem_offset + 4*8]
679 mov r13, [rsp + gpr_save_mem_offset + 5*8]
680 mov r14, [rsp + gpr_save_mem_offset + 6*8]
681 mov r15, [rsp + gpr_save_mem_offset + 7*8]
682 MOVDQA xmm6, [rsp + xmm_save_mem_offset + 0*16]
683 MOVDQA xmm7, [rsp + xmm_save_mem_offset + 1*16]
684 MOVDQA xmm8, [rsp + xmm_save_mem_offset + 2*16]
685 MOVDQA xmm9, [rsp + xmm_save_mem_offset + 3*16]
695 MARK __bitbuf_full_ %+ ARCH
697 mov blen, [rsp + blen_mem_offset]
698 ; state->b_bytes_processed = f_i - (state->buffer - state->file_start);
699 add f_i, [stream + _internal_state_file_start]
701 sub f_i, _internal_state_buffer
702 mov [stream + _internal_state_b_bytes_processed], f_i %+ d
705 MARK __compare_loops_ %+ ARCH
707 %if (COMPARE_TYPE == 1)
708 compare250 tmp1, tmp2, len, tmp3
709 %elif (COMPARE_TYPE == 2)
710 compare250_x tmp1, tmp2, len, tmp3, xtmp0, xtmp1
711 %elif (COMPARE_TYPE == 3)
712 compare250_y tmp1, tmp2, len, tmp3, ytmp0, ytmp1
714 %error Unknown Compare type COMPARE_TYPE
720 %if (COMPARE_TYPE == 1)
721 compare250 tmp1, tmp2, len2, tmp3
722 %elif (COMPARE_TYPE == 2)
723 compare250_x tmp1, tmp2, len2, tmp3, xtmp0, xtmp1
724 %elif (COMPARE_TYPE == 3)
725 compare250_y tmp1, tmp2, len2, tmp3, ytmp0, ytmp1
727 %error Unknown Compare type COMPARE_TYPE
731 get_lit_code curr_data, code3, code_len3, hufftables
732 jmp len_dist_lit_huffman
734 MARK __write_first_byte_ %+ ARCH
736 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
739 mov dword [rsp + empty_buffer_flag], 0
740 compute_hash hash, curr_data
741 and hash %+ d, HASH_MASK
742 mov [stream + _internal_state_head + 2 * hash], f_i %+ w
744 get_lit_code curr_data, code2, code_len2, hufftables
751 %endif ;; ifndef TEST