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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72 %define m_bit_count r11
77 %define file_start r13
81 %define hufftables r15
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 f_end_i_mem_offset equ 0 ; local variable (8 bytes)
88 ; void isal_deflate_finish ( isal_zstream *stream )
89 ; arg 1: rcx: addr of stream
90 global isal_deflate_finish_01
91 isal_deflate_finish_01:
92 PUSH_ALL rbx, rsi, rdi, rbp, r12, r13, r14, r15
95 %ifidn __OUTPUT_FORMAT__, elf64
101 ; state->bitbuf.set_buf(stream->next_out, stream->avail_out);
102 mov m_out_buf, [stream + _next_out]
103 mov [stream + _internal_state_bitbuf_m_out_start], m_out_buf
104 mov tmp1 %+ d, [stream + _avail_out]
108 mov [stream + _internal_state_bitbuf_m_out_end], tmp1
110 mov m_bits, [stream + _internal_state_bitbuf_m_bits]
111 mov m_bit_count %+ d, [stream + _internal_state_bitbuf_m_bit_count]
113 mov hufftables, [stream + _hufftables]
115 mov file_start, [stream + _next_in]
117 mov f_i %+ d, dword [stream + _total_in]
120 mov f_end_i %+ d, dword [stream + _avail_in]
123 sub f_end_i, LAST_BYTES_COUNT
124 mov [rsp + f_end_i_mem_offset], f_end_i
125 ; for (f_i = f_start_i; f_i < f_end_i; f_i++) {
129 mov curr_data %+ d, [file_start + f_i]
131 cmp byte [stream + _internal_state_has_hist], IGZIP_NO_HIST
132 jne skip_write_first_byte
134 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
136 mov hmask1 %+ d, dword [stream + _internal_state_hash_mask]
137 compute_hash hash, curr_data
138 and hash %+ d, hmask1 %+ d
139 mov [stream + _internal_state_head + 2 * hash], f_i %+ w
140 mov byte [stream + _internal_state_has_hist], IGZIP_HIST
143 skip_write_first_byte:
146 mov tmp3 %+ d, dword [stream + _internal_state_dist_mask]
147 mov hmask1 %+ d, dword [stream + _internal_state_hash_mask]
148 ; if (state->bitbuf.is_full()) {
149 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
152 ; hash = compute_hash(state->file_start + f_i) & hash_mask;
153 mov curr_data %+ d, [file_start + f_i]
154 compute_hash hash, curr_data
155 and hash %+ d, hmask1 %+ d
157 ; f_index = state->head[hash];
158 movzx f_index %+ d, word [stream + _internal_state_head + 2 * hash]
160 ; state->head[hash] = (uint16_t) f_i;
161 mov [stream + _internal_state_head + 2 * hash], f_i %+ w
163 ; dist = f_i - f_index; // mod 64k
164 mov dist %+ d, f_i %+ d
165 sub dist %+ d, f_index %+ d
166 and dist %+ d, 0xFFFF
168 ; if ((dist-1) <= (D-1)) {
169 mov tmp1 %+ d, dist %+ d
171 cmp tmp1 %+ d, tmp3 %+ d
174 ; len = f_end_i - f_i;
175 mov tmp4, [rsp + f_end_i_mem_offset]
177 add tmp4, LAST_BYTES_COUNT
179 ; if (len > 258) len = 258;
183 ; len = compare(state->file_start + f_i,
184 ; state->file_start + f_i - dist, len);
185 lea tmp1, [file_start + f_i]
188 compare tmp4, tmp1, tmp2, len, tmp3
190 ; if (len >= SHORTEST_MATCH) {
191 cmp len, SHORTEST_MATCH
194 ;; encode as dist/len
196 ; get_dist_code(dist, &code2, &code_len2);
198 get_dist_code dist, code2, code_len2, hufftables ;; clobbers dist, rcx
200 ; get_len_code(len, &code, &code_len);
201 get_len_code len, code, rcx, hufftables ;; rcx is code_len
203 mov hmask2 %+ d, dword [stream + _internal_state_hash_mask]
206 ; code_len2 += code_len
207 SHLX code2, code2, rcx
211 ; for (k = f_i+1, f_i += len-1; k <= f_i; k++) {
212 lea tmp3, [f_i + 1] ; tmp3 <= k
214 cmp f_i, [rsp + f_end_i_mem_offset]
217 ; only update hash twice
219 ; hash = compute_hash(state->file_start + k) & hash_mask;
220 mov tmp6 %+ d, dword [file_start + tmp3]
221 compute_hash hash, tmp6
222 and hash %+ d, hmask2 %+ d
223 ; state->head[hash] = k;
224 mov [stream + _internal_state_head + 2 * hash], tmp3 %+ w
228 ; hash = compute_hash(state->file_start + k) & hash_mask;
229 mov tmp6 %+ d, dword [file_start + tmp3]
230 compute_hash hash, tmp6
231 and hash %+ d, hmask2 %+ d
232 ; state->head[hash] = k;
233 mov [stream + _internal_state_head + 2 * hash], tmp3 %+ w
236 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf
239 cmp f_i, [rsp + f_end_i_mem_offset]
244 ; get_lit_code(state->file_start[f_i], &code2, &code_len2);
245 movzx tmp5, byte [file_start + f_i]
246 get_lit_code tmp5, code2, code_len2, hufftables
248 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf
252 cmp f_i, [rsp + f_end_i_mem_offset]
256 mov f_end_i, [rsp + f_end_i_mem_offset]
257 add f_end_i, LAST_BYTES_COUNT
258 mov [rsp + f_end_i_mem_offset], f_end_i
259 ; if ((f_i >= f_end_i) && ! state->bitbuf.is_full()) {
265 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
267 movzx tmp5, byte [file_start + f_i]
268 get_lit_code tmp5, code2, code_len2, hufftables
269 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf
272 cmp f_i, [rsp + f_end_i_mem_offset]
276 cmp m_out_buf, [stream + _internal_state_bitbuf_m_out_end]
279 ; get_lit_code(256, &code2, &code_len2);
280 get_lit_code 256, code2, code_len2, hufftables
282 write_bits m_bits, m_bit_count, code2, code_len2, m_out_buf
284 mov byte [stream + _internal_state_has_eob], 1
285 cmp word [stream + _end_of_stream], 1
287 ; state->state = ZSTATE_TRL;
288 mov dword [stream + _internal_state_state], ZSTATE_TRL
292 ; state->state = ZSTATE_SYNC_FLUSH;
293 mov dword [stream + _internal_state_state], ZSTATE_SYNC_FLUSH
298 ;; Update input buffer
299 mov f_end_i, [rsp + f_end_i_mem_offset]
300 mov [stream + _total_in], f_i %+ d
302 mov [stream + _next_in], file_start
304 mov [stream + _avail_in], f_end_i %+ d
306 ;; Update output buffer
307 mov [stream + _next_out], m_out_buf
308 ; len = state->bitbuf.buffer_used();
309 sub m_out_buf, [stream + _internal_state_bitbuf_m_out_start]
311 ; stream->avail_out -= len;
312 sub [stream + _avail_out], m_out_buf %+ d
313 ; stream->total_out += len;
314 add [stream + _total_out], m_out_buf %+ d
316 mov [stream + _internal_state_bitbuf_m_bits], m_bits
317 mov [stream + _internal_state_bitbuf_m_bit_count], m_bit_count %+ d