]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/igzip/huffman.asm
fbe402ce6c0c9627a804769bada35285119d3cd5
[ceph.git] / ceph / src / isa-l / igzip / huffman.asm
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3 ;
4 ; Redistribution and use in source and binary forms, with or without
5 ; modification, are permitted provided that the following conditions
6 ; are met:
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
12 ; distribution.
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.
16 ;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29
30 %include "options.asm"
31 %include "lz0a_const.asm"
32
33 ; Macros for doing Huffman Encoding
34
35 %ifdef LONGER_HUFFTABLE
36 %if (D > 8192)
37 %error History D is larger than 8K, cannot use %LONGER_HUFFTABLE
38 % error
39 %else
40 %define DIST_TABLE_SIZE 8192
41 %define DECODE_OFFSET 26
42 %endif
43 %else
44 %define DIST_TABLE_SIZE 1024
45 %define DECODE_OFFSET 20
46 %endif
47
48 %define LEN_TABLE_SIZE 256
49 %define LIT_TABLE_SIZE 257
50
51 %define DIST_TABLE_START (IGZIP_MAX_DEF_HDR_SIZE + 8)
52 %define DIST_TABLE_OFFSET (DIST_TABLE_START + - 4 * 1)
53 %define LEN_TABLE_OFFSET (DIST_TABLE_START + DIST_TABLE_SIZE * 4 - 4*3)
54 %define LIT_TABLE_OFFSET (DIST_TABLE_START + 4 * DIST_TABLE_SIZE + 4 * LEN_TABLE_SIZE)
55 %define LIT_TABLE_SIZES_OFFSET (LIT_TABLE_OFFSET + 2 * LIT_TABLE_SIZE)
56 %define DCODE_TABLE_OFFSET (LIT_TABLE_SIZES_OFFSET + LIT_TABLE_SIZE + 1 - DECODE_OFFSET * 2)
57 %define DCODE_TABLE_SIZE_OFFSET (DCODE_TABLE_OFFSET + 2 * 30 - DECODE_OFFSET)
58 ;; /** @brief Holds the huffman tree used to huffman encode the input stream **/
59 ;; struct isal_hufftables {
60 ;; // deflate huffman tree header
61 ;; uint8_t deflate_huff_hdr[IGZIP_MAX_DEF_HDR_SIZE];
62 ;;
63 ;; //!< Number of whole bytes in deflate_huff_hdr
64 ;; uint32_t deflate_huff_hdr_count;
65 ;;
66 ;; //!< Number of bits in the partial byte in header
67 ;; uint32_t deflate_huff_hdr_extra_bits;
68 ;;
69 ;; //!< bits 7:0 are the code length, bits 31:8 are the code
70 ;; uint32_t dist_table[DIST_TABLE_SIZE];
71 ;;
72 ;; //!< bits 7:0 are the code length, bits 31:8 are the code
73 ;; uint32_t len_table[LEN_TABLE_SIZE];
74 ;;
75 ;; //!< bits 3:0 are the code length, bits 15:4 are the code
76 ;; uint16_t lit_table[LIT_TABLE_SIZE];
77 ;;
78 ;; //!< bits 3:0 are the code length, bits 15:4 are the code
79 ;; uint16_t dcodes[30 - DECODE_OFFSET];
80
81 ;; };
82
83
84 %ifdef LONGER_HUFFTABLE
85 ; Uses RCX, clobbers dist
86 ; get_dist_code dist, code, len
87 %macro get_dist_code 4
88 %define %%dist %1 ; 64-bit IN
89 %define %%code %2d ; 32-bit OUT
90 %define %%len %3d ; 32-bit OUT
91 %define %%hufftables %4 ; address of the hufftable
92
93 mov %%len, [%%hufftables + DIST_TABLE_OFFSET + 4*%%dist ]
94 mov %%code, %%len
95 and %%len, 0x1F;
96 shr %%code, 5
97 %endm
98
99 %macro get_packed_dist_code 3
100 %define %%dist %1 ; 64-bit IN
101 %define %%code_len %2d ; 32-bit OUT
102 %define %%hufftables %3 ; address of the hufftable
103 mov %%code_len, [%%hufftables + DIST_TABLE_OFFSET + 4*%%dist ]
104 %endm
105
106 %macro unpack_dist_code 2
107 %define %%code %1d ; 32-bit OUT
108 %define %%len %2d ; 32-bit OUT
109
110 mov %%len, %%code
111 and %%len, 0x1F;
112 shr %%code, 5
113 %endm
114
115 %else
116 ; Assumes (dist != 0)
117 ; Uses RCX, clobbers dist
118 ; void compute_dist_code dist, code, len
119 %macro compute_dist_code 4
120 %define %%dist %1d ; IN, clobbered
121 %define %%distq %1
122 %define %%code %2 ; OUT
123 %define %%len %3 ; OUT
124 %define %%hufftables %4
125
126 dec %%dist
127 bsr ecx, %%dist ; ecx = msb = bsr(dist)
128 dec ecx ; ecx = num_extra_bits = msb - N
129 mov %%code, 1
130 shl %%code, CL
131 dec %%code ; code = ((1 << num_extra_bits) - 1)
132 and %%code, %%dist ; code = extra_bits
133 shr %%dist, CL ; dist >>= num_extra_bits
134 lea %%dist, [%%dist + 2*ecx] ; dist = sym = dist + num_extra_bits*2
135 mov %%len, ecx ; len = num_extra_bits
136 movzx ecx, byte [hufftables + DCODE_TABLE_SIZE_OFFSET + %%distq WRT_OPT]
137 movzx %%dist, word [hufftables + DCODE_TABLE_OFFSET + 2 * %%distq WRT_OPT]
138 shl %%code, CL ; code = extra_bits << (sym & 0xF)
139 or %%code, %%dist ; code = (sym >> 4) | (extra_bits << (sym & 0xF))
140 add %%len, ecx ; len = num_extra_bits + (sym & 0xF)
141 %endm
142
143 ; Uses RCX, clobbers dist
144 ; get_dist_code dist, code, len
145 %macro get_dist_code 4
146 %define %%dist %1d ; 32-bit IN, clobbered
147 %define %%distq %1 ; 64-bit IN, clobbered
148 %define %%code %2d ; 32-bit OUT
149 %define %%len %3d ; 32-bit OUT
150 %define %%hufftables %4
151
152 cmp %%dist, DIST_TABLE_SIZE
153 jg %%do_compute
154 mov %%len, [hufftables + DIST_TABLE_OFFSET + 4*%%distq WRT_OPT]
155 mov %%code, %%len
156 and %%len, 0x1F;
157 shr %%code, 5
158 jmp %%done
159 %%do_compute:
160 compute_dist_code %%distq, %%code, %%len, %%hufftables
161 %%done:
162 %endm
163
164 %macro get_packed_dist_code 3
165 %define %%dist %1 ; 64-bit IN
166 %define %%code_len %2d ; 32-bit OUT
167 %define %%hufftables %3 ; address of the hufftable
168 %endm
169
170 %endif
171
172
173 ; "len" can be same register as "length"
174 ; get_len_code length, code, len
175 %macro get_len_code 4
176 %define %%length %1 ; 64-bit IN
177 %define %%code %2d ; 32-bit OUT
178 %define %%len %3d ; 32-bit OUT
179 %define %%hufftables %4
180
181 mov %%len, [%%hufftables + LEN_TABLE_OFFSET + 4 * %%length]
182 mov %%code, %%len
183 and %%len, 0x1F
184 shr %%code, 5
185 %endm
186
187
188 %macro get_lit_code 4
189 %define %%lit %1 ; 64-bit IN or CONST
190 %define %%code %2d ; 32-bit OUT
191 %define %%len %3d ; 32-bit OUT
192 %define %%hufftables %4
193
194 movzx %%len, byte [%%hufftables + LIT_TABLE_SIZES_OFFSET + %%lit]
195 movzx %%code, word [%%hufftables + LIT_TABLE_OFFSET + 2 * %%lit]
196
197 %endm
198
199
200 ;; Compute hash of first 3 bytes of data
201 %macro compute_hash 2
202 %define %%result %1d ; 32-bit reg
203 %define %%data %2d ; 32-bit reg (low byte not clobbered)
204
205 and %%data, 0x00FFFFFF
206 xor %%result, %%result
207 crc32 %%result, %%data
208 %endm