18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci-*- linux-c -*-
48c2ecf20Sopenharmony_ci   drbd_receiver.c
58c2ecf20Sopenharmony_ci   This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci   Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
88c2ecf20Sopenharmony_ci   Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
98c2ecf20Sopenharmony_ci   Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifndef _DRBD_VLI_H
148c2ecf20Sopenharmony_ci#define _DRBD_VLI_H
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * At a granularity of 4KiB storage represented per bit,
188c2ecf20Sopenharmony_ci * and stroage sizes of several TiB,
198c2ecf20Sopenharmony_ci * and possibly small-bandwidth replication,
208c2ecf20Sopenharmony_ci * the bitmap transfer time can take much too long,
218c2ecf20Sopenharmony_ci * if transmitted in plain text.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * We try to reduce the transferred bitmap information
248c2ecf20Sopenharmony_ci * by encoding runlengths of bit polarity.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * We never actually need to encode a "zero" (runlengths are positive).
278c2ecf20Sopenharmony_ci * But then we have to store the value of the first bit.
288c2ecf20Sopenharmony_ci * The first bit of information thus shall encode if the first runlength
298c2ecf20Sopenharmony_ci * gives the number of set or unset bits.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * We assume that large areas are either completely set or unset,
328c2ecf20Sopenharmony_ci * which gives good compression with any runlength method,
338c2ecf20Sopenharmony_ci * even when encoding the runlength as fixed size 32bit/64bit integers.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Still, there may be areas where the polarity flips every few bits,
368c2ecf20Sopenharmony_ci * and encoding the runlength sequence of those areas with fix size
378c2ecf20Sopenharmony_ci * integers would be much worse than plaintext.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * We want to encode small runlength values with minimum code length,
408c2ecf20Sopenharmony_ci * while still being able to encode a Huge run of all zeros.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * Thus we need a Variable Length Integer encoding, VLI.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * For some cases, we produce more code bits than plaintext input.
458c2ecf20Sopenharmony_ci * We need to send incompressible chunks as plaintext, skip over them
468c2ecf20Sopenharmony_ci * and then see if the next chunk compresses better.
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * We don't care too much about "excellent" compression ratio for large
498c2ecf20Sopenharmony_ci * runlengths (all set/all clear): whether we achieve a factor of 100
508c2ecf20Sopenharmony_ci * or 1000 is not that much of an issue.
518c2ecf20Sopenharmony_ci * We do not want to waste too much on short runlengths in the "noisy"
528c2ecf20Sopenharmony_ci * parts of the bitmap, though.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * There are endless variants of VLI, we experimented with:
558c2ecf20Sopenharmony_ci *  * simple byte-based
568c2ecf20Sopenharmony_ci *  * various bit based with different code word length.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * To avoid yet an other configuration parameter (choice of bitmap compression
598c2ecf20Sopenharmony_ci * algorithm) which was difficult to explain and tune, we just chose the one
608c2ecf20Sopenharmony_ci * variant that turned out best in all test cases.
618c2ecf20Sopenharmony_ci * Based on real world usage patterns, with device sizes ranging from a few GiB
628c2ecf20Sopenharmony_ci * to several TiB, file server/mailserver/webserver/mysql/postgress,
638c2ecf20Sopenharmony_ci * mostly idle to really busy, the all time winner (though sometimes only
648c2ecf20Sopenharmony_ci * marginally better) is:
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * encoding is "visualised" as
698c2ecf20Sopenharmony_ci * __little endian__ bitstream, least significant bit first (left most)
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * this particular encoding is chosen so that the prefix code
728c2ecf20Sopenharmony_ci * starts as unary encoding the level, then modified so that
738c2ecf20Sopenharmony_ci * 10 levels can be described in 8bit, with minimal overhead
748c2ecf20Sopenharmony_ci * for the smaller levels.
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * Number of data bits follow fibonacci sequence, with the exception of the
778c2ecf20Sopenharmony_ci * last level (+1 data bit, so it makes 64bit total).  The only worse code when
788c2ecf20Sopenharmony_ci * encoding bit polarity runlength is 1 plain bits => 2 code bits.
798c2ecf20Sopenharmony_ciprefix    data bits                                    max val  Nº data bits
808c2ecf20Sopenharmony_ci0 x                                                         0x2            1
818c2ecf20Sopenharmony_ci10 x                                                        0x4            1
828c2ecf20Sopenharmony_ci110 xx                                                      0x8            2
838c2ecf20Sopenharmony_ci1110 xxx                                                   0x10            3
848c2ecf20Sopenharmony_ci11110 xxx xx                                               0x30            5
858c2ecf20Sopenharmony_ci111110 xx xxxxxx                                          0x130            8
868c2ecf20Sopenharmony_ci11111100  xxxxxxxx xxxxx                                 0x2130           13
878c2ecf20Sopenharmony_ci11111110  xxxxxxxx xxxxxxxx xxxxx                      0x202130           21
888c2ecf20Sopenharmony_ci11111101  xxxxxxxx xxxxxxxx xxxxxxxx  xxxxxxxx xx   0x400202130           34
898c2ecf20Sopenharmony_ci11111111  xxxxxxxx xxxxxxxx xxxxxxxx  xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 56
908c2ecf20Sopenharmony_ci * maximum encodable value: 0x100000400202130 == 2**56 + some */
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* compression "table":
938c2ecf20Sopenharmony_ci transmitted   x                                0.29
948c2ecf20Sopenharmony_ci as plaintext x                                  ........................
958c2ecf20Sopenharmony_ci             x                                   ........................
968c2ecf20Sopenharmony_ci            x                                    ........................
978c2ecf20Sopenharmony_ci           x    0.59                         0.21........................
988c2ecf20Sopenharmony_ci          x      ........................................................
998c2ecf20Sopenharmony_ci         x       .. c ...................................................
1008c2ecf20Sopenharmony_ci        x    0.44.. o ...................................................
1018c2ecf20Sopenharmony_ci       x .......... d ...................................................
1028c2ecf20Sopenharmony_ci      x  .......... e ...................................................
1038c2ecf20Sopenharmony_ci     X.............   ...................................................
1048c2ecf20Sopenharmony_ci    x.............. b ...................................................
1058c2ecf20Sopenharmony_ci2.0x............... i ...................................................
1068c2ecf20Sopenharmony_ci #X................ t ...................................................
1078c2ecf20Sopenharmony_ci #................. s ...........................  plain bits  ..........
1088c2ecf20Sopenharmony_ci-+-----------------------------------------------------------------------
1098c2ecf20Sopenharmony_ci 1             16              32                              64
1108c2ecf20Sopenharmony_ci*/
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* LEVEL: (total bits, prefix bits, prefix value),
1138c2ecf20Sopenharmony_ci * sorted ascending by number of total bits.
1148c2ecf20Sopenharmony_ci * The rest of the code table is calculated at compiletime from this. */
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/* fibonacci data 1, 1, ... */
1178c2ecf20Sopenharmony_ci#define VLI_L_1_1() do { \
1188c2ecf20Sopenharmony_ci	LEVEL( 2, 1, 0x00); \
1198c2ecf20Sopenharmony_ci	LEVEL( 3, 2, 0x01); \
1208c2ecf20Sopenharmony_ci	LEVEL( 5, 3, 0x03); \
1218c2ecf20Sopenharmony_ci	LEVEL( 7, 4, 0x07); \
1228c2ecf20Sopenharmony_ci	LEVEL(10, 5, 0x0f); \
1238c2ecf20Sopenharmony_ci	LEVEL(14, 6, 0x1f); \
1248c2ecf20Sopenharmony_ci	LEVEL(21, 8, 0x3f); \
1258c2ecf20Sopenharmony_ci	LEVEL(29, 8, 0x7f); \
1268c2ecf20Sopenharmony_ci	LEVEL(42, 8, 0xbf); \
1278c2ecf20Sopenharmony_ci	LEVEL(64, 8, 0xff); \
1288c2ecf20Sopenharmony_ci	} while (0)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/* finds a suitable level to decode the least significant part of in.
1318c2ecf20Sopenharmony_ci * returns number of bits consumed.
1328c2ecf20Sopenharmony_ci *
1338c2ecf20Sopenharmony_ci * BUG() for bad input, as that would mean a buggy code table. */
1348c2ecf20Sopenharmony_cistatic inline int vli_decode_bits(u64 *out, const u64 in)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	u64 adj = 1;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define LEVEL(t,b,v)					\
1398c2ecf20Sopenharmony_ci	do {						\
1408c2ecf20Sopenharmony_ci		if ((in & ((1 << b) -1)) == v) {	\
1418c2ecf20Sopenharmony_ci			*out = ((in & ((~0ULL) >> (64-t))) >> b) + adj;	\
1428c2ecf20Sopenharmony_ci			return t;			\
1438c2ecf20Sopenharmony_ci		}					\
1448c2ecf20Sopenharmony_ci		adj += 1ULL << (t - b);			\
1458c2ecf20Sopenharmony_ci	} while (0)
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	VLI_L_1_1();
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* NOT REACHED, if VLI_LEVELS code table is defined properly */
1508c2ecf20Sopenharmony_ci	BUG();
1518c2ecf20Sopenharmony_ci#undef LEVEL
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/* return number of code bits needed,
1558c2ecf20Sopenharmony_ci * or negative error number */
1568c2ecf20Sopenharmony_cistatic inline int __vli_encode_bits(u64 *out, const u64 in)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	u64 max = 0;
1598c2ecf20Sopenharmony_ci	u64 adj = 1;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	if (in == 0)
1628c2ecf20Sopenharmony_ci		return -EINVAL;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#define LEVEL(t,b,v) do {		\
1658c2ecf20Sopenharmony_ci		max += 1ULL << (t - b);	\
1668c2ecf20Sopenharmony_ci		if (in <= max) {	\
1678c2ecf20Sopenharmony_ci			if (out)	\
1688c2ecf20Sopenharmony_ci				*out = ((in - adj) << b) | v;	\
1698c2ecf20Sopenharmony_ci			return t;	\
1708c2ecf20Sopenharmony_ci		}			\
1718c2ecf20Sopenharmony_ci		adj = max + 1;		\
1728c2ecf20Sopenharmony_ci	} while (0)
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	VLI_L_1_1();
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return -EOVERFLOW;
1778c2ecf20Sopenharmony_ci#undef LEVEL
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#undef VLI_L_1_1
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* code from here down is independend of actually used bit code */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/*
1858c2ecf20Sopenharmony_ci * Code length is determined by some unique (e.g. unary) prefix.
1868c2ecf20Sopenharmony_ci * This encodes arbitrary bit length, not whole bytes: we have a bit-stream,
1878c2ecf20Sopenharmony_ci * not a byte stream.
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/* for the bitstream, we need a cursor */
1918c2ecf20Sopenharmony_cistruct bitstream_cursor {
1928c2ecf20Sopenharmony_ci	/* the current byte */
1938c2ecf20Sopenharmony_ci	u8 *b;
1948c2ecf20Sopenharmony_ci	/* the current bit within *b, nomalized: 0..7 */
1958c2ecf20Sopenharmony_ci	unsigned int bit;
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/* initialize cursor to point to first bit of stream */
1998c2ecf20Sopenharmony_cistatic inline void bitstream_cursor_reset(struct bitstream_cursor *cur, void *s)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	cur->b = s;
2028c2ecf20Sopenharmony_ci	cur->bit = 0;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/* advance cursor by that many bits; maximum expected input value: 64,
2068c2ecf20Sopenharmony_ci * but depending on VLI implementation, it may be more. */
2078c2ecf20Sopenharmony_cistatic inline void bitstream_cursor_advance(struct bitstream_cursor *cur, unsigned int bits)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	bits += cur->bit;
2108c2ecf20Sopenharmony_ci	cur->b = cur->b + (bits >> 3);
2118c2ecf20Sopenharmony_ci	cur->bit = bits & 7;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/* the bitstream itself knows its length */
2158c2ecf20Sopenharmony_cistruct bitstream {
2168c2ecf20Sopenharmony_ci	struct bitstream_cursor cur;
2178c2ecf20Sopenharmony_ci	unsigned char *buf;
2188c2ecf20Sopenharmony_ci	size_t buf_len;		/* in bytes */
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* for input stream:
2218c2ecf20Sopenharmony_ci	 * number of trailing 0 bits for padding
2228c2ecf20Sopenharmony_ci	 * total number of valid bits in stream: buf_len * 8 - pad_bits */
2238c2ecf20Sopenharmony_ci	unsigned int pad_bits;
2248c2ecf20Sopenharmony_ci};
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic inline void bitstream_init(struct bitstream *bs, void *s, size_t len, unsigned int pad_bits)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	bs->buf = s;
2298c2ecf20Sopenharmony_ci	bs->buf_len = len;
2308c2ecf20Sopenharmony_ci	bs->pad_bits = pad_bits;
2318c2ecf20Sopenharmony_ci	bitstream_cursor_reset(&bs->cur, bs->buf);
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic inline void bitstream_rewind(struct bitstream *bs)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	bitstream_cursor_reset(&bs->cur, bs->buf);
2378c2ecf20Sopenharmony_ci	memset(bs->buf, 0, bs->buf_len);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/* Put (at most 64) least significant bits of val into bitstream, and advance cursor.
2418c2ecf20Sopenharmony_ci * Ignores "pad_bits".
2428c2ecf20Sopenharmony_ci * Returns zero if bits == 0 (nothing to do).
2438c2ecf20Sopenharmony_ci * Returns number of bits used if successful.
2448c2ecf20Sopenharmony_ci *
2458c2ecf20Sopenharmony_ci * If there is not enough room left in bitstream,
2468c2ecf20Sopenharmony_ci * leaves bitstream unchanged and returns -ENOBUFS.
2478c2ecf20Sopenharmony_ci */
2488c2ecf20Sopenharmony_cistatic inline int bitstream_put_bits(struct bitstream *bs, u64 val, const unsigned int bits)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	unsigned char *b = bs->cur.b;
2518c2ecf20Sopenharmony_ci	unsigned int tmp;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (bits == 0)
2548c2ecf20Sopenharmony_ci		return 0;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if ((bs->cur.b + ((bs->cur.bit + bits -1) >> 3)) - bs->buf >= bs->buf_len)
2578c2ecf20Sopenharmony_ci		return -ENOBUFS;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	/* paranoia: strip off hi bits; they should not be set anyways. */
2608c2ecf20Sopenharmony_ci	if (bits < 64)
2618c2ecf20Sopenharmony_ci		val &= ~0ULL >> (64 - bits);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	*b++ |= (val & 0xff) << bs->cur.bit;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	for (tmp = 8 - bs->cur.bit; tmp < bits; tmp += 8)
2668c2ecf20Sopenharmony_ci		*b++ |= (val >> tmp) & 0xff;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	bitstream_cursor_advance(&bs->cur, bits);
2698c2ecf20Sopenharmony_ci	return bits;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci/* Fetch (at most 64) bits from bitstream into *out, and advance cursor.
2738c2ecf20Sopenharmony_ci *
2748c2ecf20Sopenharmony_ci * If more than 64 bits are requested, returns -EINVAL and leave *out unchanged.
2758c2ecf20Sopenharmony_ci *
2768c2ecf20Sopenharmony_ci * If there are less than the requested number of valid bits left in the
2778c2ecf20Sopenharmony_ci * bitstream, still fetches all available bits.
2788c2ecf20Sopenharmony_ci *
2798c2ecf20Sopenharmony_ci * Returns number of actually fetched bits.
2808c2ecf20Sopenharmony_ci */
2818c2ecf20Sopenharmony_cistatic inline int bitstream_get_bits(struct bitstream *bs, u64 *out, int bits)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	u64 val;
2848c2ecf20Sopenharmony_ci	unsigned int n;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (bits > 64)
2878c2ecf20Sopenharmony_ci		return -EINVAL;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (bs->cur.b + ((bs->cur.bit + bs->pad_bits + bits -1) >> 3) - bs->buf >= bs->buf_len)
2908c2ecf20Sopenharmony_ci		bits = ((bs->buf_len - (bs->cur.b - bs->buf)) << 3)
2918c2ecf20Sopenharmony_ci			- bs->cur.bit - bs->pad_bits;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (bits == 0) {
2948c2ecf20Sopenharmony_ci		*out = 0;
2958c2ecf20Sopenharmony_ci		return 0;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/* get the high bits */
2998c2ecf20Sopenharmony_ci	val = 0;
3008c2ecf20Sopenharmony_ci	n = (bs->cur.bit + bits + 7) >> 3;
3018c2ecf20Sopenharmony_ci	/* n may be at most 9, if cur.bit + bits > 64 */
3028c2ecf20Sopenharmony_ci	/* which means this copies at most 8 byte */
3038c2ecf20Sopenharmony_ci	if (n) {
3048c2ecf20Sopenharmony_ci		memcpy(&val, bs->cur.b+1, n - 1);
3058c2ecf20Sopenharmony_ci		val = le64_to_cpu(val) << (8 - bs->cur.bit);
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	/* we still need the low bits */
3098c2ecf20Sopenharmony_ci	val |= bs->cur.b[0] >> bs->cur.bit;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/* and mask out bits we don't want */
3128c2ecf20Sopenharmony_ci	val &= ~0ULL >> (64 - bits);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	bitstream_cursor_advance(&bs->cur, bits);
3158c2ecf20Sopenharmony_ci	*out = val;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	return bits;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci/* encodes @in as vli into @bs;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci * return values
3238c2ecf20Sopenharmony_ci *  > 0: number of bits successfully stored in bitstream
3248c2ecf20Sopenharmony_ci * -ENOBUFS @bs is full
3258c2ecf20Sopenharmony_ci * -EINVAL input zero (invalid)
3268c2ecf20Sopenharmony_ci * -EOVERFLOW input too large for this vli code (invalid)
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_cistatic inline int vli_encode_bits(struct bitstream *bs, u64 in)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	u64 code = code;
3318c2ecf20Sopenharmony_ci	int bits = __vli_encode_bits(&code, in);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (bits <= 0)
3348c2ecf20Sopenharmony_ci		return bits;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return bitstream_put_bits(bs, code, bits);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci#endif
340