Lines Matching +full:max +full:- +full:cur

1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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
79 prefix data bits max val Nº data bits
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()
158 u64 max = 0; in __vli_encode_bits() local
162 return -EINVAL; in __vli_encode_bits()
165 max += 1ULL << (t - b); \ in __vli_encode_bits()
166 if (in <= max) { \ in __vli_encode_bits()
168 *out = ((in - adj) << b) | v; \ in __vli_encode_bits()
171 adj = max + 1; \ 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,
199 static inline void bitstream_cursor_reset(struct bitstream_cursor *cur, void *s) in bitstream_cursor_reset() argument
201 cur->b = s; in bitstream_cursor_reset()
202 cur->bit = 0; in bitstream_cursor_reset()
207 static inline void bitstream_cursor_advance(struct bitstream_cursor *cur, unsigned int bits) in bitstream_cursor_advance() argument
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()
216 struct bitstream_cursor cur; member
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()
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()
274 * If more than 64 bits are requested, returns -EINVAL and leave *out unchanged.
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()
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)