/* Generate a "word" with platform-dependent size whose bytes all contain the *value'b'.
*/ static forceinline size_t repeat_byte(u8 b)
{
size_t v;
v = b;
v |= v << 8;
v |= v << 16;
v |= v << ((WORDBYTES == 8) ? 32 : 0); return v;
}
/* Structure that encapsulates a block of in-memory data being interpreted as a *streamofbits,optionallywithinterwovenliteralbytes.Bitsareassumed *tobestoredinlittleendian16-bitcodingunits,withthebitsorderedhigh *tolow.
*/ struct input_bitstream {
/* Bits that have been read from the input buffer. The bits are *left-justified;thenextbitisalwaysbit31.
*/
u32 bitbuf;
/* Number of bits currently held in @bitbuf. */
u32 bitsleft;
/* Pointer to the next byte to be retrieved from the input buffer. */ const u8 *next;
/* Pointer to just past the end of the input buffer. */ const u8 *end;
};
/* Initialize a bitstream to read from the specified input buffer. */ static forceinline void init_input_bitstream(struct input_bitstream *is, constvoid *buffer, u32 size)
{
is->bitbuf = 0;
is->bitsleft = 0;
is->next = buffer;
is->end = is->next + size;
}
/* Ensure the bit buffer variable for the bitstream contains at least @num_bits *bits.Followingthis,bitstream_peek_bits()and/orbitstream_remove_bits() *maybecalledonthebitstreamtopeekorremoveupto@num_bitsbits.Note *that@num_bitsmustbe<=16.
*/ static forceinline void bitstream_ensure_bits(struct input_bitstream *is,
u32 num_bits)
{ if (is->bitsleft < num_bits) { if (is->end - is->next >= 2) {
is->bitbuf |= (u32)get_unaligned_le16(is->next)
<< (16 - is->bitsleft);
is->next += 2;
}
is->bitsleft += 16;
}
}
/* Return the next @num_bits bits from the bitstream, without removing them. *Theremustbeatleast@num_bitsremaininginthebuffervariable,froma *previouscalltobitstream_ensure_bits().
*/ static forceinline u32
bitstream_peek_bits(conststruct input_bitstream *is, const u32 num_bits)
{ return (is->bitbuf >> 1) >> (sizeof(is->bitbuf) * 8 - num_bits - 1);
}
/* Remove @num_bits from the bitstream. There must be at least @num_bits *remaininginthebuffervariable,fromapreviouscallto *bitstream_ensure_bits().
*/ static forceinline void
bitstream_remove_bits(struct input_bitstream *is, u32 num_bits)
{
is->bitbuf <<= num_bits;
is->bitsleft -= num_bits;
}
/* Remove and return @num_bits bits from the bitstream. There must be at least *@num_bitsremaininginthebuffervariable,fromapreviouscallto *bitstream_ensure_bits().
*/ static forceinline u32
bitstream_pop_bits(struct input_bitstream *is, u32 num_bits)
{
u32 bits = bitstream_peek_bits(is, num_bits);
/* Read and return the next @num_bits bits from the bitstream. */ static forceinline u32
bitstream_read_bits(struct input_bitstream *is, u32 num_bits)
{
bitstream_ensure_bits(is, num_bits); return bitstream_pop_bits(is, num_bits);
}
/* Read and return the next literal byte embedded in the bitstream. */ static forceinline u8
bitstream_read_byte(struct input_bitstream *is)
{ if (unlikely(is->end == is->next)) return0; return *is->next++;
}
/* Read and return the next 16-bit integer embedded in the bitstream. */ static forceinline u16
bitstream_read_u16(struct input_bitstream *is)
{
u16 v;
if (unlikely(is->end - is->next < 2)) return0;
v = get_unaligned_le16(is->next);
is->next += 2; return v;
}
/* Read and return the next 32-bit integer embedded in the bitstream. */ static forceinline u32
bitstream_read_u32(struct input_bitstream *is)
{
u32 v;
if (unlikely(is->end - is->next < 4)) return0;
v = get_unaligned_le32(is->next);
is->next += 4; return v;
}
/* Read into @dst_buffer an array of literal bytes embedded in the bitstream. *Returneitherapointertothebytepastthelastwritten,orNULLifthe *readoverflowstheinputbuffer.
*/ static forceinline void *bitstream_read_bytes(struct input_bitstream *is, void *dst_buffer, size_t count)
{ if ((size_t)(is->end - is->next) < count) return NULL;
memcpy(dst_buffer, is->next, count);
is->next += count; return (u8 *)dst_buffer + count;
}
/* Align the input bitstream on a coding-unit boundary. */ static forceinline void bitstream_align(struct input_bitstream *is)
{
is->bitsleft = 0;
is->bitbuf = 0;
}
/* Reads and returns the next Huffman-encoded symbol from a bitstream. If the *inputdataisexhausted,theHuffmansymbolisdecodedasifthemissingbits *areallzeroes.
*/ static forceinline u32 read_huffsym(struct input_bitstream *istream, const u16 decode_table[],
u32 table_bits,
u32 max_codeword_len)
{
u32 entry;
u32 key_bits;
bitstream_ensure_bits(istream, max_codeword_len);
/* Index the decode table by the next table_bits bits of the input. */
key_bits = bitstream_peek_bits(istream, table_bits);
entry = decode_table[key_bits]; if (entry < 0xC000) { /* Fast case: The decode table directly provided the *symbolandcodewordlength.Thelow11bitsarethe *symbol,andthehigh5bitsarethecodewordlength.
*/
bitstream_remove_bits(istream, entry >> 11); return entry & 0x7FF;
} /* Slow case: The codeword for the symbol is longer than *table_bits,sothesymboldoesnothaveanentry *directlyinthefirst(1<<table_bits)entriesofthe *decodetable.Traversetheappropriatebinarytree *bit-by-bittodecodethesymbol.
*/
bitstream_remove_bits(istream, table_bits); do {
key_bits = (entry & 0x3FFF) + bitstream_pop_bits(istream, 1);
} while ((entry = decode_table[key_bits]) >= 0xC000); return entry;
}
if (offset >= WORDBYTES) { /* The source and destination words don't overlap. */
/* To improve branch prediction, one iteration of this *loopisunrolled.Mostmatchesareshortandwill *failthefirstcheck.Butifthatcheckpasses,then *itbecomesincreasinglikelythatthematchislong *andwe'llneedtocontinuecopying.
*/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.