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 TUNGSTEN GRAPHICS 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 const uint8_t* data
= vlc
->inputs
[0];
95 unsigned len
= vlc
->sizes
[0];
98 assert(vlc
->num_inputs
);
100 vlc
->bytes_left
-= len
;
102 /* align the data pointer */
103 while (len
&& pointer_to_uintptr(data
) & 3) {
104 vlc
->buffer
|= (uint64_t)*data
<< (24 + vlc
->invalid_bits
);
107 vlc
->invalid_bits
-= 8;
110 vlc
->end
= data
+ len
;
118 * fill the bit buffer, so that at least 32 bits are valid
121 vl_vlc_fillbits(struct vl_vlc
*vlc
)
125 /* as long as the buffer needs to be filled */
126 while (vlc
->invalid_bits
> 0) {
127 unsigned bytes_left
= vlc
->end
- vlc
->data
;
129 /* if this input is depleted */
130 if (bytes_left
== 0) {
133 /* go on to next input */
134 vl_vlc_next_input(vlc
);
136 /* or give up since we don't have anymore inputs */
139 } else if (bytes_left
>= 4) {
141 /* enough bytes in buffer, read in a whole dword */
142 uint64_t value
= *(const uint32_t*)vlc
->data
;
144 #ifndef PIPE_ARCH_BIG_ENDIAN
145 value
= util_bswap32(value
);
148 vlc
->buffer
|= value
<< vlc
->invalid_bits
;
150 vlc
->invalid_bits
-= 32;
152 /* buffer is now definitely filled up avoid the loop test */
155 } else while (vlc
->data
< vlc
->end
) {
157 /* not enough bytes left in buffer, read single bytes */
158 vlc
->buffer
|= (uint64_t)*vlc
->data
<< (24 + vlc
->invalid_bits
);
160 vlc
->invalid_bits
-= 8;
166 * initialize vlc structure and start reading from first input buffer
169 vl_vlc_init(struct vl_vlc
*vlc
, unsigned num_inputs
,
170 const void *const *inputs
, const unsigned *sizes
)
178 vlc
->invalid_bits
= 32;
179 vlc
->num_inputs
= num_inputs
;
180 vlc
->inputs
= inputs
;
184 for (i
= 0; i
< num_inputs
; ++i
)
185 vlc
->bytes_left
+= sizes
[i
];
187 vl_vlc_next_input(vlc
);
188 vl_vlc_fillbits(vlc
);
189 vl_vlc_fillbits(vlc
);
193 * number of bits still valid in bit buffer
195 static INLINE
unsigned
196 vl_vlc_valid_bits(struct vl_vlc
*vlc
)
198 return 32 - vlc
->invalid_bits
;
202 * number of bits left over all inbut buffers
204 static INLINE
unsigned
205 vl_vlc_bits_left(struct vl_vlc
*vlc
)
207 signed bytes_left
= vlc
->end
- vlc
->data
;
208 bytes_left
+= vlc
->bytes_left
;
209 return bytes_left
* 8 + vl_vlc_valid_bits(vlc
);
213 * get num_bits from bit buffer without removing them
215 static INLINE
unsigned
216 vl_vlc_peekbits(struct vl_vlc
*vlc
, unsigned num_bits
)
218 assert(vl_vlc_valid_bits(vlc
) >= num_bits
|| vlc
->data
>= vlc
->end
);
219 return vlc
->buffer
>> (64 - num_bits
);
223 * remove num_bits from bit buffer
226 vl_vlc_eatbits(struct vl_vlc
*vlc
, unsigned num_bits
)
228 assert(vl_vlc_valid_bits(vlc
) >= num_bits
);
230 vlc
->buffer
<<= num_bits
;
231 vlc
->invalid_bits
+= num_bits
;
235 * get num_bits from bit buffer with removing them
237 static INLINE
unsigned
238 vl_vlc_get_uimsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
242 assert(vl_vlc_valid_bits(vlc
) >= num_bits
);
244 value
= vlc
->buffer
>> (64 - num_bits
);
245 vl_vlc_eatbits(vlc
, num_bits
);
251 * treat num_bits as signed value and remove them from bit buffer
254 vl_vlc_get_simsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
258 assert(vl_vlc_valid_bits(vlc
) >= num_bits
);
260 value
= ((int64_t)vlc
->buffer
) >> (64 - num_bits
);
261 vl_vlc_eatbits(vlc
, num_bits
);
267 * lookup a value and length in a decompressed table
270 vl_vlc_get_vlclbf(struct vl_vlc
*vlc
, const struct vl_vlc_entry
*tbl
, unsigned num_bits
)
272 tbl
+= vl_vlc_peekbits(vlc
, num_bits
);
273 vl_vlc_eatbits(vlc
, tbl
->length
);
278 #endif /* vl_vlc_h */