/* LEVEL: (total bits, prefix bits, prefix value), *sortedascendingbynumberoftotalbits.
* The rest of the code table is calculated at compiletime from this. */
/* finds a suitable level to decode the least significant part of in. *returnsnumberofbitsconsumed. *
* BUG() for bad input, as that would mean a buggy code table. */ staticinlineint vli_decode_bits(u64 *out, const u64 in)
{
u64 adj = 1;
#define LEVEL(t,b,v) \ do { \ if ((in & ((1 << b) -1)) == v) { \
*out = ((in & ((~0ULL) >> (64-t))) >> b) + adj; \ return t; \
} \
adj += 1ULL << (t - b); \
} while (0)
VLI_L_1_1();
/* NOT REACHED, if VLI_LEVELS code table is defined properly */
BUG(); #undef LEVEL
}
/* return number of code bits needed,
* or negative error number */ staticinlineint __vli_encode_bits(u64 *out, const u64 in)
{
u64 max = 0;
u64 adj = 1;
if (in == 0) return -EINVAL;
#define LEVEL(t,b,v) do { \
max += 1ULL << (t - b); \ if (in <= max) { \ if (out) \
*out = ((in - adj) << b) | v; \ return t; \
} \
adj = max + 1; \
} while (0)
VLI_L_1_1();
return -EOVERFLOW; #undef LEVEL
}
#undef VLI_L_1_1
/* code from here down is independend of actually used bit code */
/* for the bitstream, we need a cursor */ struct bitstream_cursor { /* the current byte */
u8 *b; /* the current bit within *b, nomalized: 0..7 */ unsignedint bit;
};
/* initialize cursor to point to first bit of stream */ staticinlinevoid bitstream_cursor_reset(struct bitstream_cursor *cur, void *s)
{
cur->b = s;
cur->bit = 0;
}
/* advance cursor by that many bits; maximum expected input value: 64,
* but depending on VLI implementation, it may be more. */ staticinlinevoid bitstream_cursor_advance(struct bitstream_cursor *cur, unsignedint bits)
{
bits += cur->bit;
cur->b = cur->b + (bits >> 3);
cur->bit = bits & 7;
}
/* the bitstream itself knows its length */ struct bitstream { struct bitstream_cursor cur; unsignedchar *buf;
size_t buf_len; /* in bytes */
/* for input stream: *numberoftrailing0bitsforpadding
* total number of valid bits in stream: buf_len * 8 - pad_bits */ unsignedint pad_bits;
};
/* get the high bits */
val = 0;
n = (bs->cur.bit + bits + 7) >> 3; /* n may be at most 9, if cur.bit + bits > 64 */ /* which means this copies at most 8 byte */ if (n) {
memcpy(&val, bs->cur.b+1, n - 1);
val = le64_to_cpu(val) << (8 - bs->cur.bit);
}
/* we still need the low bits */
val |= bs->cur.b[0] >> bs->cur.bit;
/* and mask out bits we don't want */
val &= ~0ULL >> (64 - bits);
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.