Lines Matching +full:left +full:- +full:most
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 -*- linux-c -*-
7 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
8 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
9 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
19 * and possibly small-bandwidth replication,
55 * * simple byte-based
69 * __little endian__ bitstream, least significant bit first (left most)
108 -+-----------------------------------------------------------------------
140 if ((in & ((1 << b) -1)) == v) { \ in vli_decode_bits()
141 *out = ((in & ((~0ULL) >> (64-t))) >> b) + adj; \ in vli_decode_bits()
144 adj += 1ULL << (t - b); \ in vli_decode_bits()
162 return -EINVAL; in __vli_encode_bits()
165 max += 1ULL << (t - b); \ in __vli_encode_bits()
168 *out = ((in - adj) << b) | v; \ in __vli_encode_bits()
176 return -EOVERFLOW; in __vli_encode_bits()
186 * This encodes arbitrary bit length, not whole bytes: we have a bit-stream,
201 cur->b = s; in bitstream_cursor_reset()
202 cur->bit = 0; in bitstream_cursor_reset()
209 bits += cur->bit; in bitstream_cursor_advance()
210 cur->b = cur->b + (bits >> 3); in bitstream_cursor_advance()
211 cur->bit = bits & 7; in bitstream_cursor_advance()
222 * total number of valid bits in stream: buf_len * 8 - pad_bits */
228 bs->buf = s; in bitstream_init()
229 bs->buf_len = len; in bitstream_init()
230 bs->pad_bits = pad_bits; in bitstream_init()
231 bitstream_cursor_reset(&bs->cur, bs->buf); in bitstream_init()
236 bitstream_cursor_reset(&bs->cur, bs->buf); in bitstream_rewind()
237 memset(bs->buf, 0, bs->buf_len); in bitstream_rewind()
240 /* Put (at most 64) least significant bits of val into bitstream, and advance cursor.
245 * If there is not enough room left in bitstream,
246 * leaves bitstream unchanged and returns -ENOBUFS.
250 unsigned char *b = bs->cur.b; in bitstream_put_bits()
256 if ((bs->cur.b + ((bs->cur.bit + bits -1) >> 3)) - bs->buf >= bs->buf_len) in bitstream_put_bits()
257 return -ENOBUFS; in bitstream_put_bits()
261 val &= ~0ULL >> (64 - bits); in bitstream_put_bits()
263 *b++ |= (val & 0xff) << bs->cur.bit; in bitstream_put_bits()
265 for (tmp = 8 - bs->cur.bit; tmp < bits; tmp += 8) in bitstream_put_bits()
268 bitstream_cursor_advance(&bs->cur, bits); in bitstream_put_bits()
272 /* Fetch (at most 64) bits from bitstream into *out, and advance cursor.
274 * If more than 64 bits are requested, returns -EINVAL and leave *out unchanged.
276 * If there are less than the requested number of valid bits left in the
287 return -EINVAL; in bitstream_get_bits()
289 if (bs->cur.b + ((bs->cur.bit + bs->pad_bits + bits -1) >> 3) - bs->buf >= bs->buf_len) in bitstream_get_bits()
290 bits = ((bs->buf_len - (bs->cur.b - bs->buf)) << 3) in bitstream_get_bits()
291 - bs->cur.bit - bs->pad_bits; in bitstream_get_bits()
300 n = (bs->cur.bit + bits + 7) >> 3; in bitstream_get_bits()
301 /* n may be at most 9, if cur.bit + bits > 64 */ in bitstream_get_bits()
302 /* which means this copies at most 8 byte */ in bitstream_get_bits()
304 memcpy(&val, bs->cur.b+1, n - 1); in bitstream_get_bits()
305 val = le64_to_cpu(val) << (8 - bs->cur.bit); in bitstream_get_bits()
309 val |= bs->cur.b[0] >> bs->cur.bit; in bitstream_get_bits()
312 val &= ~0ULL >> (64 - bits); in bitstream_get_bits()
314 bitstream_cursor_advance(&bs->cur, bits); in bitstream_get_bits()
324 * -ENOBUFS @bs is full
325 * -EINVAL input zero (invalid)
326 * -EOVERFLOW input too large for this vli code (invalid)