1 /**************************************************************************
3 * Copyright 2011 Christian König.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * Functions for fast bitwise access to multiple probably unaligned input buffers
35 #include "pipe/p_compiler.h"
37 #include "util/u_math.h"
38 #include "util/u_pointer.h"
39 #include "util/u_debug.h"
49 const void *const *inputs
;
50 const unsigned *sizes
;
60 struct vl_vlc_compressed
63 struct vl_vlc_entry entry
;
67 * initalize and decompress a lookup table
70 vl_vlc_init_table(struct vl_vlc_entry
*dst
, unsigned dst_size
, const struct vl_vlc_compressed
*src
, unsigned src_size
)
72 unsigned i
, bits
= util_logbase2(dst_size
);
74 assert(dst
&& dst_size
);
75 assert(src
&& src_size
);
77 for (i
=0;i
<dst_size
;++i
) {
82 for(; src_size
> 0; --src_size
, ++src
) {
83 for(i
=0; i
<(1 << (bits
- src
->entry
.length
)); ++i
)
84 dst
[src
->bitcode
>> (16 - bits
) | i
] = src
->entry
;
89 * switch over to next input buffer
92 vl_vlc_next_input(struct vl_vlc
*vlc
)
94 unsigned len
= vlc
->sizes
[0];
97 assert(vlc
->num_inputs
);
99 vlc
->bytes_left
-= len
;
101 vlc
->data
= vlc
->inputs
[0];
102 vlc
->end
= vlc
->data
+ len
;
110 * align the data pointer to the next dword
113 vl_vlc_align_data_ptr(struct vl_vlc
*vlc
)
115 /* align the data pointer */
116 while (vlc
->data
!= vlc
->end
&& pointer_to_uintptr(vlc
->data
) & 3) {
117 vlc
->buffer
|= (uint64_t)*vlc
->data
<< (24 + vlc
->invalid_bits
);
119 vlc
->invalid_bits
-= 8;
124 * fill the bit buffer, so that at least 32 bits are valid
127 vl_vlc_fillbits(struct vl_vlc
*vlc
)
131 /* as long as the buffer needs to be filled */
132 while (vlc
->invalid_bits
> 0) {
133 unsigned bytes_left
= vlc
->end
- vlc
->data
;
135 /* if this input is depleted */
136 if (bytes_left
== 0) {
139 /* go on to next input */
140 vl_vlc_next_input(vlc
);
142 /* or give up since we don't have anymore inputs */
145 } else if (bytes_left
>= 4) {
147 /* enough bytes in buffer, read in a whole dword */
148 uint64_t value
= *(const uint32_t*)vlc
->data
;
150 #ifndef PIPE_ARCH_BIG_ENDIAN
151 value
= util_bswap32(value
);
154 vlc
->buffer
|= value
<< vlc
->invalid_bits
;
156 vlc
->invalid_bits
-= 32;
158 /* buffer is now definitely filled up avoid the loop test */
161 } else while (vlc
->data
< vlc
->end
) {
163 /* not enough bytes left in buffer, read single bytes */
164 vlc
->buffer
|= (uint64_t)*vlc
->data
<< (24 + vlc
->invalid_bits
);
166 vlc
->invalid_bits
-= 8;
172 * initialize vlc structure and start reading from first input buffer
175 vl_vlc_init(struct vl_vlc
*vlc
, unsigned num_inputs
,
176 const void *const *inputs
, const unsigned *sizes
)
184 vlc
->invalid_bits
= 32;
185 vlc
->num_inputs
= num_inputs
;
186 vlc
->inputs
= inputs
;
190 for (i
= 0; i
< num_inputs
; ++i
)
191 vlc
->bytes_left
+= sizes
[i
];
193 vl_vlc_next_input(vlc
);
194 vl_vlc_align_data_ptr(vlc
);
195 vl_vlc_fillbits(vlc
);
196 vl_vlc_fillbits(vlc
);
200 * number of bits still valid in bit buffer
202 static INLINE
unsigned
203 vl_vlc_valid_bits(struct vl_vlc
*vlc
)
205 return 32 - vlc
->invalid_bits
;
209 * number of bits left over all inbut buffers
211 static INLINE
unsigned
212 vl_vlc_bits_left(struct vl_vlc
*vlc
)
214 signed bytes_left
= vlc
->end
- vlc
->data
;
215 bytes_left
+= vlc
->bytes_left
;
216 return bytes_left
* 8 + vl_vlc_valid_bits(vlc
);
220 * get num_bits from bit buffer without removing them
222 static INLINE
unsigned
223 vl_vlc_peekbits(struct vl_vlc
*vlc
, unsigned num_bits
)
225 assert(vl_vlc_valid_bits(vlc
) >= num_bits
|| vlc
->data
>= vlc
->end
);
226 return vlc
->buffer
>> (64 - num_bits
);
230 * remove num_bits from bit buffer
233 vl_vlc_eatbits(struct vl_vlc
*vlc
, unsigned num_bits
)
235 assert(vl_vlc_valid_bits(vlc
) >= num_bits
);
237 vlc
->buffer
<<= num_bits
;
238 vlc
->invalid_bits
+= num_bits
;
242 * get num_bits from bit buffer with removing them
244 static INLINE
unsigned
245 vl_vlc_get_uimsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
249 assert(vl_vlc_valid_bits(vlc
) >= num_bits
);
251 value
= vlc
->buffer
>> (64 - num_bits
);
252 vl_vlc_eatbits(vlc
, num_bits
);
258 * treat num_bits as signed value and remove them from bit buffer
261 vl_vlc_get_simsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
265 assert(vl_vlc_valid_bits(vlc
) >= num_bits
);
267 value
= ((int64_t)vlc
->buffer
) >> (64 - num_bits
);
268 vl_vlc_eatbits(vlc
, num_bits
);
274 * lookup a value and length in a decompressed table
277 vl_vlc_get_vlclbf(struct vl_vlc
*vlc
, const struct vl_vlc_entry
*tbl
, unsigned num_bits
)
279 tbl
+= vl_vlc_peekbits(vlc
, num_bits
);
280 vl_vlc_eatbits(vlc
, tbl
->length
);
285 * fast forward search for a specific byte value
287 static INLINE boolean
288 vl_vlc_search_byte(struct vl_vlc
*vlc
, unsigned num_bits
, uint8_t value
)
290 /* make sure we are on a byte boundary */
291 assert((vl_vlc_valid_bits(vlc
) % 8) == 0);
292 assert(num_bits
== ~0 || (num_bits
% 8) == 0);
294 /* deplete the bit buffer */
295 while (vl_vlc_valid_bits(vlc
) > 0) {
297 if (vl_vlc_peekbits(vlc
, 8) == value
) {
298 vl_vlc_fillbits(vlc
);
302 vl_vlc_eatbits(vlc
, 8);
304 if (num_bits
!= ~0) {
311 /* deplete the byte buffers */
314 /* if this input is depleted */
315 if (vlc
->data
== vlc
->end
) {
317 /* go on to next input */
318 vl_vlc_next_input(vlc
);
320 /* or give up since we don't have anymore inputs */
324 if (*vlc
->data
== value
) {
325 vl_vlc_align_data_ptr(vlc
);
326 vl_vlc_fillbits(vlc
);
331 if (num_bits
!= ~0) {
334 vl_vlc_align_data_ptr(vlc
);
341 #endif /* vl_vlc_h */