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"
32 ; Assumes m_out_buf is a register
35 ; write_bits_always m_bits, m_bit_count, code, count, m_out_buf, tmp1
36 %macro write_bits_always 6
38 %define %%m_bit_count %2
41 %define %%m_out_buf %5
45 shlx %%code, %%code, %%m_bit_count
47 mov rcx, %%m_bit_count
51 add %%m_bit_count, %%count
53 movnti [%%m_out_buf], %%m_bits
54 mov rcx, %%m_bit_count
55 shr rcx, 3 ; rcx = bytes
57 shl rcx, 3 ; rcx = bits
58 sub %%m_bit_count, rcx
60 shrx %%m_bits, %%m_bits, rcx
66 ; Assumes m_out_buf is a register
69 ; write_bits_safe m_bits, m_bit_count, code, count, m_out_buf, tmp1
70 %macro write_bits_safe 6
72 %define %%m_bit_count %2
75 %define %%m_out_buf %5
80 shlx %%tmp1, %%tmp1, %%m_bit_count
82 mov rcx, %%m_bit_count
86 add %%m_bit_count, %%count
90 movnti [%%m_out_buf], %%m_bits
93 sub rcx, %%m_bit_count
96 shrx %%m_bits, %%m_bits, rcx
103 ; Assumes m_out_buf is a register
105 ;; check_space num_bits, m_bits, m_bit_count, m_out_buf, tmp1
107 %define %%num_bits %1
109 %define %%m_bit_count %3
110 %define %%m_out_buf %4
114 sub %%tmp1, %%m_bit_count
115 cmp %%tmp1, %%num_bits
118 ; if (63 - m_bit_count < num_bits)
119 movnti [%%m_out_buf], %%m_bits
120 mov rcx, %%m_bit_count
121 shr rcx, 3 ; rcx = bytes
123 shl rcx, 3 ; rcx = bits
124 sub %%m_bit_count, rcx
126 shrx %%m_bits, %%m_bits, rcx
135 ; write_bits_unsafe m_bits, m_bit_count, code, count
136 %macro write_bits_unsafe 4
138 %define %%m_bit_count %2
142 shlx %%code, %%code, %%m_bit_count
144 mov rcx, %%m_bit_count
148 add %%m_bit_count, %%count
151 ; pad_to_byte m_bit_count, extra_bits
153 %define %%m_bit_count %1
154 %define %%extra_bits %2
156 mov %%extra_bits, %%m_bit_count
159 add %%m_bit_count, %%extra_bits
162 ; Assumes m_out_buf is a memory reference
163 ; flush m_bits, m_bit_count, m_out_buf, tmp1
166 %define %%m_bit_count %2
167 %define %%m_out_buf %3
170 test %%m_bit_count, %%m_bit_count
171 jz %%bit_count_is_zero
173 mov %%tmp1, %%m_out_buf
174 movnti [%%tmp1], %%m_bits
177 shr %%m_bit_count, 3 ; bytes
178 add %%tmp1, %%m_bit_count
179 mov %%m_out_buf, %%tmp1
182 xor %%m_bits, %%m_bits
183 xor %%m_bit_count, %%m_bit_count
188 %define %%m_bit_count %2
191 %define %%m_out_buf %5
195 write_bits_safe %%m_bits, %%m_bit_count, %%code, %%count, %%m_out_buf, %%tmp1
197 write_bits_always %%m_bits, %%m_bit_count, %%code, %%count, %%m_out_buf, %%tmp1
199 ; state->bitbuf.check_space(code_len2);
200 check_space %%count, %%m_bits, %%m_bit_count, %%m_out_buf, %%tmp1
201 ; state->bitbuf.write_bits(code2, code_len2);
202 write_bits_unsafe %%m_bits, %%m_bit_count, %%code, %%count
203 ; code2 is clobbered, rcx is clobbered
210 movnti [%%addr], %%data