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"
31 %include "lz0a_const.asm"
32 %include "data_struct2.asm"
33 %include "bitbuf2.asm"
34 %include "huffman.asm"
35 %include "igzip_compare_types.asm"
38 %include "reg_sizes.asm"
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69 %define m_bit_count r11
75 %define file_start r13
79 %define hufftables r15
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 f_end_i_mem_offset equ 0 ; local variable (8 bytes)
86 ; void isal_deflate_finish ( isal_zstream *stream )
87 ; arg 1: rcx: addr of stream
88 global isal_deflate_finish_01
89 isal_deflate_finish_01:
90 PUSH_ALL rbx, rsi, rdi, rbp, r12, r13, r14, r15
93 %ifidn __OUTPUT_FORMAT__, elf64
99 ; state->bitbuf.set_buf(stream->next_out, stream->avail_out);
100 mov m_out_buf, [stream + _next_out]
101 mov [stream + _internal_state_bitbuf_m_out_start], m_out_buf
102 mov tmp1 %+ d, [stream + _avail_out]
106 mov [stream + _internal_state_bitbuf_m_out_end], tmp1
108 mov m_bits, [stream + _internal_state_bitbuf_m_bits]
109 mov m_bit_count %+ d, [stream + _internal_state_bitbuf_m_bit_count]
111 mov hufftables, [stream + _hufftables]
113 ; f_i = state->b_bytes_processed;
114 ; f_end_i = state->b_bytes_valid;
115 mov f_i %+ d, [stream + _internal_state_b_bytes_processed]
116 mov f_end_i %+ d, [stream + _internal_state_b_bytes_valid]
118 ; f_i += (uint32_t)(state->buffer - state->file_start);
119 ; f_end_i += (uint32_t)(state->buffer - state->file_start);
120 mov file_start, [stream + _internal_state_file_start]
121 lea tmp1, [stream + _internal_state_buffer]
125 mov [rsp + f_end_i_mem_offset], f_end_i
126 ; for (f_i = f_start_i; f_i < f_end_i; f_i++) {
130 mov tmp1 %+ d, [file_start + f_i]
133 ; if (state->bitbuf.is_full()) {
134 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
137 ; hash = compute_hash(state->file_start + f_i) & HASH_MASK;
138 compute_hash hash, tmp1
139 and hash %+ d, HASH_MASK
141 ; f_index = state->head[hash];
142 movzx f_index %+ d, word [stream + _internal_state_head + 2 * hash]
144 ; state->head[hash] = (uint16_t) f_i;
145 mov [stream + _internal_state_head + 2 * hash], f_i %+ w
147 ; dist = f_i - f_index; // mod 64k
148 mov dist %+ d, f_i %+ d
149 sub dist %+ d, f_index %+ d
150 and dist %+ d, 0xFFFF
152 ; if ((dist-1) <= (D-1)) {
153 mov tmp1 %+ d, dist %+ d
158 ; len = f_end_i - f_i;
159 mov tmp4, [rsp + f_end_i_mem_offset]
162 ; if (len > 258) len = 258;
166 ; len = compare(state->file_start + f_i,
167 ; state->file_start + f_i - dist, len);
168 lea tmp1, [file_start + f_i]
171 compare tmp4, tmp1, tmp2, len, tmp3
173 ; if (len >= SHORTEST_MATCH) {
174 cmp len, SHORTEST_MATCH
177 ;; encode as dist/len
179 ; get_dist_code(dist, &code2, &code_len2);
180 get_dist_code dist, code2, code_len2, hufftables ;; clobbers dist, rcx
182 ; get_len_code(len, &code, &code_len);
183 get_len_code len, code, rcx, hufftables ;; rcx is code_len
187 ; code_len2 += code_len
189 shlx code2, code2, rcx
196 ; for (k = f_i+1, f_i += len-1; k <= f_i; k++) {
197 lea tmp3, [f_i + 1] ; tmp3 <= k
199 %ifdef LIMIT_HASH_UPDATE
200 ; only update hash twice
202 ; hash = compute_hash(state->file_start + k) & HASH_MASK;
203 mov tmp6 %+ d, [file_start + tmp3]
204 compute_hash hash, tmp6
205 and hash %+ d, HASH_MASK
206 ; state->head[hash] = k;
207 mov [stream + _internal_state_head + 2 * hash], tmp3 %+ w
211 ; hash = compute_hash(state->file_start + k) & HASH_MASK;
212 mov tmp6 %+ d, [file_start + tmp3]
213 compute_hash hash, tmp6
214 and hash %+ d, HASH_MASK
215 ; state->head[hash] = k;
216 mov [stream + _internal_state_head + 2 * hash], tmp3 %+ w
220 ; hash = compute_hash(state->file_start + k) & HASH_MASK;
221 mov tmp6 %+ d, [file_start + tmp3]
222 compute_hash hash, tmp6
223 and hash %+ d, HASH_MASK
224 ; state->head[hash] = k;
225 mov [stream + _internal_state_head + 2 * hash], tmp3 %+ w
231 mov tmp1 %+ d, [file_start + f_i]
233 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf, tmp5
236 cmp f_i, [rsp + f_end_i_mem_offset]
241 mov tmp1 %+ d, [file_start + f_i + 1]
243 ; get_lit_code(state->file_start[f_i], &code2, &code_len2);
244 movzx tmp5, byte [file_start + f_i]
245 get_lit_code tmp5, code2, code_len2, hufftables
247 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf, tmp5
251 cmp f_i, [rsp + f_end_i_mem_offset]
256 ; if ((f_i >= f_end_i) && ! state->bitbuf.is_full()) {
257 cmp f_i, [rsp + f_end_i_mem_offset]
259 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
262 cmp dword [stream + _end_of_stream], 1
264 cmp dword [stream + _internal_state_left_over], 0
268 ; get_lit_code(256, &code2, &code_len2);
269 get_lit_code 256, code2, code_len2, hufftables
271 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf, tmp1
273 mov dword [stream + _internal_state_has_eob], 1
274 cmp dword [stream + _end_of_stream], 1
276 ; state->state = ZSTATE_TRL;
277 mov dword [stream + _internal_state_state], ZSTATE_TRL
281 ; state->state = ZSTATE_SYNC_FLUSH;
282 mov dword [stream + _internal_state_state], ZSTATE_SYNC_FLUSH
286 ; state->b_bytes_processed = f_i - (state->buffer - state->file_start);
287 add f_i, [stream + _internal_state_file_start]
289 sub f_i, _internal_state_buffer
290 mov [stream + _internal_state_b_bytes_processed], f_i %+ d
292 ; // update output buffer
293 ; stream->next_out = state->bitbuf.buffer_ptr();
294 mov [stream + _next_out], m_out_buf
295 ; len = state->bitbuf.buffer_used();
296 sub m_out_buf, [stream + _internal_state_bitbuf_m_out_start]
298 ; stream->avail_out -= len;
299 sub [stream + _avail_out], m_out_buf %+ d
300 ; stream->total_out += len;
301 add [stream + _total_out], m_out_buf %+ d
303 mov [stream + _internal_state_bitbuf_m_bits], m_bits
304 mov [stream + _internal_state_bitbuf_m_bit_count], m_bit_count %+ d