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 "util/u_math.h"
44
const void *const *inputs;
45
const unsigned *sizes;
55
struct vl_vlc_compressed
58
struct vl_vlc_entry entry;
62
* initalize and decompress a lookup table
65
vl_vlc_init_table(struct vl_vlc_entry *dst, unsigned dst_size, const struct vl_vlc_compressed *src, unsigned src_size)
67
unsigned i, bits = util_logbase2(dst_size);
69
assert(dst && dst_size);
70
assert(src && src_size);
72
for (i=0;i<dst_size;++i) {
77
for(; src_size > 0; --src_size, ++src) {
78
for(i = 0; i < (1u << (bits - src->entry.length)); ++i)
79
dst[src->bitcode >> (16 - bits) | i] = src->entry;
84
* switch over to next input buffer
87
vl_vlc_next_input(struct vl_vlc *vlc)
89
unsigned len = vlc->sizes[0];
92
assert(vlc->bytes_left);
94
if (len < vlc->bytes_left)
95
vlc->bytes_left -= len;
97
len = vlc->bytes_left;
101
vlc->data = vlc->inputs[0];
102
vlc->end = vlc->data + len;
109
* align the data pointer to the next dword
112
vl_vlc_align_data_ptr(struct vl_vlc *vlc)
114
/* align the data pointer */
115
while (vlc->data != vlc->end && ((uintptr_t)vlc->data) & 3) {
116
vlc->buffer |= (uint64_t)*vlc->data << (24 + vlc->invalid_bits);
118
vlc->invalid_bits -= 8;
123
* fill the bit buffer, so that at least 32 bits are valid
126
vl_vlc_fillbits(struct vl_vlc *vlc)
130
/* as long as the buffer needs to be filled */
131
while (vlc->invalid_bits > 0) {
132
unsigned bytes_left = vlc->end - vlc->data;
134
/* if this input is depleted */
135
if (bytes_left == 0) {
137
if (vlc->bytes_left) {
138
/* go on to next input */
139
vl_vlc_next_input(vlc);
140
vl_vlc_align_data_ptr(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
#if !UTIL_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->inputs = inputs;
189
for (i = 0; i < num_inputs; ++i)
190
vlc->bytes_left += sizes[i];
192
if (vlc->bytes_left) {
193
vl_vlc_next_input(vlc);
194
vl_vlc_align_data_ptr(vlc);
195
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
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 == ~0u || (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 != ~0u) {
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 != ~0u) {
334
vl_vlc_align_data_ptr(vlc);
342
* remove num_bits bits starting at pos from the bitbuffer
345
vl_vlc_removebits(struct vl_vlc *vlc, unsigned pos, unsigned num_bits)
347
uint64_t lo = (vlc->buffer & (~0UL >> (pos + num_bits))) << num_bits;
348
uint64_t hi = (vlc->buffer & (~0UL << (64 - pos)));
349
vlc->buffer = lo | hi;
350
vlc->invalid_bits += num_bits;
354
* limit the number of bits left for fetching
357
vl_vlc_limit(struct vl_vlc *vlc, unsigned bits_left)
359
assert(bits_left <= vl_vlc_bits_left(vlc));
361
vl_vlc_fillbits(vlc);
362
if (bits_left < vl_vlc_valid_bits(vlc)) {
363
vlc->invalid_bits = 32 - bits_left;
364
vlc->buffer &= ~0L << (vlc->invalid_bits + 32);
365
vlc->end = vlc->data;
368
assert((bits_left - vl_vlc_valid_bits(vlc)) % 8 == 0);
369
vlc->bytes_left = (bits_left - vl_vlc_valid_bits(vlc)) / 8;
370
if (vlc->bytes_left < (vlc->end - vlc->data)) {
371
vlc->end = vlc->data + vlc->bytes_left;
374
vlc->bytes_left -= vlc->end - vlc->data;
378
#endif /* vl_vlc_h */