xref: /openbmc/linux/drivers/block/drbd/drbd_vli.h (revision b411b3637fa71fce9cf2acf0639009500f5892fe)
1*b411b363SPhilipp Reisner /*
2*b411b363SPhilipp Reisner -*- linux-c -*-
3*b411b363SPhilipp Reisner    drbd_receiver.c
4*b411b363SPhilipp Reisner    This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5*b411b363SPhilipp Reisner 
6*b411b363SPhilipp Reisner    Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7*b411b363SPhilipp Reisner    Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8*b411b363SPhilipp Reisner    Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9*b411b363SPhilipp Reisner 
10*b411b363SPhilipp Reisner    drbd is free software; you can redistribute it and/or modify
11*b411b363SPhilipp Reisner    it under the terms of the GNU General Public License as published by
12*b411b363SPhilipp Reisner    the Free Software Foundation; either version 2, or (at your option)
13*b411b363SPhilipp Reisner    any later version.
14*b411b363SPhilipp Reisner 
15*b411b363SPhilipp Reisner    drbd is distributed in the hope that it will be useful,
16*b411b363SPhilipp Reisner    but WITHOUT ANY WARRANTY; without even the implied warranty of
17*b411b363SPhilipp Reisner    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*b411b363SPhilipp Reisner    GNU General Public License for more details.
19*b411b363SPhilipp Reisner 
20*b411b363SPhilipp Reisner    You should have received a copy of the GNU General Public License
21*b411b363SPhilipp Reisner    along with drbd; see the file COPYING.  If not, write to
22*b411b363SPhilipp Reisner    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23*b411b363SPhilipp Reisner  */
24*b411b363SPhilipp Reisner 
25*b411b363SPhilipp Reisner #ifndef _DRBD_VLI_H
26*b411b363SPhilipp Reisner #define _DRBD_VLI_H
27*b411b363SPhilipp Reisner 
28*b411b363SPhilipp Reisner /*
29*b411b363SPhilipp Reisner  * At a granularity of 4KiB storage represented per bit,
30*b411b363SPhilipp Reisner  * and stroage sizes of several TiB,
31*b411b363SPhilipp Reisner  * and possibly small-bandwidth replication,
32*b411b363SPhilipp Reisner  * the bitmap transfer time can take much too long,
33*b411b363SPhilipp Reisner  * if transmitted in plain text.
34*b411b363SPhilipp Reisner  *
35*b411b363SPhilipp Reisner  * We try to reduce the transfered bitmap information
36*b411b363SPhilipp Reisner  * by encoding runlengths of bit polarity.
37*b411b363SPhilipp Reisner  *
38*b411b363SPhilipp Reisner  * We never actually need to encode a "zero" (runlengths are positive).
39*b411b363SPhilipp Reisner  * But then we have to store the value of the first bit.
40*b411b363SPhilipp Reisner  * The first bit of information thus shall encode if the first runlength
41*b411b363SPhilipp Reisner  * gives the number of set or unset bits.
42*b411b363SPhilipp Reisner  *
43*b411b363SPhilipp Reisner  * We assume that large areas are either completely set or unset,
44*b411b363SPhilipp Reisner  * which gives good compression with any runlength method,
45*b411b363SPhilipp Reisner  * even when encoding the runlength as fixed size 32bit/64bit integers.
46*b411b363SPhilipp Reisner  *
47*b411b363SPhilipp Reisner  * Still, there may be areas where the polarity flips every few bits,
48*b411b363SPhilipp Reisner  * and encoding the runlength sequence of those areas with fix size
49*b411b363SPhilipp Reisner  * integers would be much worse than plaintext.
50*b411b363SPhilipp Reisner  *
51*b411b363SPhilipp Reisner  * We want to encode small runlength values with minimum code length,
52*b411b363SPhilipp Reisner  * while still being able to encode a Huge run of all zeros.
53*b411b363SPhilipp Reisner  *
54*b411b363SPhilipp Reisner  * Thus we need a Variable Length Integer encoding, VLI.
55*b411b363SPhilipp Reisner  *
56*b411b363SPhilipp Reisner  * For some cases, we produce more code bits than plaintext input.
57*b411b363SPhilipp Reisner  * We need to send incompressible chunks as plaintext, skip over them
58*b411b363SPhilipp Reisner  * and then see if the next chunk compresses better.
59*b411b363SPhilipp Reisner  *
60*b411b363SPhilipp Reisner  * We don't care too much about "excellent" compression ratio for large
61*b411b363SPhilipp Reisner  * runlengths (all set/all clear): whether we achieve a factor of 100
62*b411b363SPhilipp Reisner  * or 1000 is not that much of an issue.
63*b411b363SPhilipp Reisner  * We do not want to waste too much on short runlengths in the "noisy"
64*b411b363SPhilipp Reisner  * parts of the bitmap, though.
65*b411b363SPhilipp Reisner  *
66*b411b363SPhilipp Reisner  * There are endless variants of VLI, we experimented with:
67*b411b363SPhilipp Reisner  *  * simple byte-based
68*b411b363SPhilipp Reisner  *  * various bit based with different code word length.
69*b411b363SPhilipp Reisner  *
70*b411b363SPhilipp Reisner  * To avoid yet an other configuration parameter (choice of bitmap compression
71*b411b363SPhilipp Reisner  * algorithm) which was difficult to explain and tune, we just chose the one
72*b411b363SPhilipp Reisner  * variant that turned out best in all test cases.
73*b411b363SPhilipp Reisner  * Based on real world usage patterns, with device sizes ranging from a few GiB
74*b411b363SPhilipp Reisner  * to several TiB, file server/mailserver/webserver/mysql/postgress,
75*b411b363SPhilipp Reisner  * mostly idle to really busy, the all time winner (though sometimes only
76*b411b363SPhilipp Reisner  * marginally better) is:
77*b411b363SPhilipp Reisner  */
78*b411b363SPhilipp Reisner 
79*b411b363SPhilipp Reisner /*
80*b411b363SPhilipp Reisner  * encoding is "visualised" as
81*b411b363SPhilipp Reisner  * __little endian__ bitstream, least significant bit first (left most)
82*b411b363SPhilipp Reisner  *
83*b411b363SPhilipp Reisner  * this particular encoding is chosen so that the prefix code
84*b411b363SPhilipp Reisner  * starts as unary encoding the level, then modified so that
85*b411b363SPhilipp Reisner  * 10 levels can be described in 8bit, with minimal overhead
86*b411b363SPhilipp Reisner  * for the smaller levels.
87*b411b363SPhilipp Reisner  *
88*b411b363SPhilipp Reisner  * Number of data bits follow fibonacci sequence, with the exception of the
89*b411b363SPhilipp Reisner  * last level (+1 data bit, so it makes 64bit total).  The only worse code when
90*b411b363SPhilipp Reisner  * encoding bit polarity runlength is 1 plain bits => 2 code bits.
91*b411b363SPhilipp Reisner prefix    data bits                                    max val  Nº data bits
92*b411b363SPhilipp Reisner 0 x                                                         0x2            1
93*b411b363SPhilipp Reisner 10 x                                                        0x4            1
94*b411b363SPhilipp Reisner 110 xx                                                      0x8            2
95*b411b363SPhilipp Reisner 1110 xxx                                                   0x10            3
96*b411b363SPhilipp Reisner 11110 xxx xx                                               0x30            5
97*b411b363SPhilipp Reisner 111110 xx xxxxxx                                          0x130            8
98*b411b363SPhilipp Reisner 11111100  xxxxxxxx xxxxx                                 0x2130           13
99*b411b363SPhilipp Reisner 11111110  xxxxxxxx xxxxxxxx xxxxx                      0x202130           21
100*b411b363SPhilipp Reisner 11111101  xxxxxxxx xxxxxxxx xxxxxxxx  xxxxxxxx xx   0x400202130           34
101*b411b363SPhilipp Reisner 11111111  xxxxxxxx xxxxxxxx xxxxxxxx  xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 56
102*b411b363SPhilipp Reisner  * maximum encodable value: 0x100000400202130 == 2**56 + some */
103*b411b363SPhilipp Reisner 
104*b411b363SPhilipp Reisner /* compression "table":
105*b411b363SPhilipp Reisner  transmitted   x                                0.29
106*b411b363SPhilipp Reisner  as plaintext x                                  ........................
107*b411b363SPhilipp Reisner              x                                   ........................
108*b411b363SPhilipp Reisner             x                                    ........................
109*b411b363SPhilipp Reisner            x    0.59                         0.21........................
110*b411b363SPhilipp Reisner           x      ........................................................
111*b411b363SPhilipp Reisner          x       .. c ...................................................
112*b411b363SPhilipp Reisner         x    0.44.. o ...................................................
113*b411b363SPhilipp Reisner        x .......... d ...................................................
114*b411b363SPhilipp Reisner       x  .......... e ...................................................
115*b411b363SPhilipp Reisner      X.............   ...................................................
116*b411b363SPhilipp Reisner     x.............. b ...................................................
117*b411b363SPhilipp Reisner 2.0x............... i ...................................................
118*b411b363SPhilipp Reisner  #X................ t ...................................................
119*b411b363SPhilipp Reisner  #................. s ...........................  plain bits  ..........
120*b411b363SPhilipp Reisner -+-----------------------------------------------------------------------
121*b411b363SPhilipp Reisner  1             16              32                              64
122*b411b363SPhilipp Reisner */
123*b411b363SPhilipp Reisner 
124*b411b363SPhilipp Reisner /* LEVEL: (total bits, prefix bits, prefix value),
125*b411b363SPhilipp Reisner  * sorted ascending by number of total bits.
126*b411b363SPhilipp Reisner  * The rest of the code table is calculated at compiletime from this. */
127*b411b363SPhilipp Reisner 
128*b411b363SPhilipp Reisner /* fibonacci data 1, 1, ... */
129*b411b363SPhilipp Reisner #define VLI_L_1_1() do { \
130*b411b363SPhilipp Reisner 	LEVEL( 2, 1, 0x00); \
131*b411b363SPhilipp Reisner 	LEVEL( 3, 2, 0x01); \
132*b411b363SPhilipp Reisner 	LEVEL( 5, 3, 0x03); \
133*b411b363SPhilipp Reisner 	LEVEL( 7, 4, 0x07); \
134*b411b363SPhilipp Reisner 	LEVEL(10, 5, 0x0f); \
135*b411b363SPhilipp Reisner 	LEVEL(14, 6, 0x1f); \
136*b411b363SPhilipp Reisner 	LEVEL(21, 8, 0x3f); \
137*b411b363SPhilipp Reisner 	LEVEL(29, 8, 0x7f); \
138*b411b363SPhilipp Reisner 	LEVEL(42, 8, 0xbf); \
139*b411b363SPhilipp Reisner 	LEVEL(64, 8, 0xff); \
140*b411b363SPhilipp Reisner 	} while (0)
141*b411b363SPhilipp Reisner 
142*b411b363SPhilipp Reisner /* finds a suitable level to decode the least significant part of in.
143*b411b363SPhilipp Reisner  * returns number of bits consumed.
144*b411b363SPhilipp Reisner  *
145*b411b363SPhilipp Reisner  * BUG() for bad input, as that would mean a buggy code table. */
146*b411b363SPhilipp Reisner static inline int vli_decode_bits(u64 *out, const u64 in)
147*b411b363SPhilipp Reisner {
148*b411b363SPhilipp Reisner 	u64 adj = 1;
149*b411b363SPhilipp Reisner 
150*b411b363SPhilipp Reisner #define LEVEL(t,b,v)					\
151*b411b363SPhilipp Reisner 	do {						\
152*b411b363SPhilipp Reisner 		if ((in & ((1 << b) -1)) == v) {	\
153*b411b363SPhilipp Reisner 			*out = ((in & ((~0ULL) >> (64-t))) >> b) + adj;	\
154*b411b363SPhilipp Reisner 			return t;			\
155*b411b363SPhilipp Reisner 		}					\
156*b411b363SPhilipp Reisner 		adj += 1ULL << (t - b);			\
157*b411b363SPhilipp Reisner 	} while (0)
158*b411b363SPhilipp Reisner 
159*b411b363SPhilipp Reisner 	VLI_L_1_1();
160*b411b363SPhilipp Reisner 
161*b411b363SPhilipp Reisner 	/* NOT REACHED, if VLI_LEVELS code table is defined properly */
162*b411b363SPhilipp Reisner 	BUG();
163*b411b363SPhilipp Reisner #undef LEVEL
164*b411b363SPhilipp Reisner }
165*b411b363SPhilipp Reisner 
166*b411b363SPhilipp Reisner /* return number of code bits needed,
167*b411b363SPhilipp Reisner  * or negative error number */
168*b411b363SPhilipp Reisner static inline int __vli_encode_bits(u64 *out, const u64 in)
169*b411b363SPhilipp Reisner {
170*b411b363SPhilipp Reisner 	u64 max = 0;
171*b411b363SPhilipp Reisner 	u64 adj = 1;
172*b411b363SPhilipp Reisner 
173*b411b363SPhilipp Reisner 	if (in == 0)
174*b411b363SPhilipp Reisner 		return -EINVAL;
175*b411b363SPhilipp Reisner 
176*b411b363SPhilipp Reisner #define LEVEL(t,b,v) do {		\
177*b411b363SPhilipp Reisner 		max += 1ULL << (t - b);	\
178*b411b363SPhilipp Reisner 		if (in <= max) {	\
179*b411b363SPhilipp Reisner 			if (out)	\
180*b411b363SPhilipp Reisner 				*out = ((in - adj) << b) | v;	\
181*b411b363SPhilipp Reisner 			return t;	\
182*b411b363SPhilipp Reisner 		}			\
183*b411b363SPhilipp Reisner 		adj = max + 1;		\
184*b411b363SPhilipp Reisner 	} while (0)
185*b411b363SPhilipp Reisner 
186*b411b363SPhilipp Reisner 	VLI_L_1_1();
187*b411b363SPhilipp Reisner 
188*b411b363SPhilipp Reisner 	return -EOVERFLOW;
189*b411b363SPhilipp Reisner #undef LEVEL
190*b411b363SPhilipp Reisner }
191*b411b363SPhilipp Reisner 
192*b411b363SPhilipp Reisner #undef VLI_L_1_1
193*b411b363SPhilipp Reisner 
194*b411b363SPhilipp Reisner /* code from here down is independend of actually used bit code */
195*b411b363SPhilipp Reisner 
196*b411b363SPhilipp Reisner /*
197*b411b363SPhilipp Reisner  * Code length is determined by some unique (e.g. unary) prefix.
198*b411b363SPhilipp Reisner  * This encodes arbitrary bit length, not whole bytes: we have a bit-stream,
199*b411b363SPhilipp Reisner  * not a byte stream.
200*b411b363SPhilipp Reisner  */
201*b411b363SPhilipp Reisner 
202*b411b363SPhilipp Reisner /* for the bitstream, we need a cursor */
203*b411b363SPhilipp Reisner struct bitstream_cursor {
204*b411b363SPhilipp Reisner 	/* the current byte */
205*b411b363SPhilipp Reisner 	u8 *b;
206*b411b363SPhilipp Reisner 	/* the current bit within *b, nomalized: 0..7 */
207*b411b363SPhilipp Reisner 	unsigned int bit;
208*b411b363SPhilipp Reisner };
209*b411b363SPhilipp Reisner 
210*b411b363SPhilipp Reisner /* initialize cursor to point to first bit of stream */
211*b411b363SPhilipp Reisner static inline void bitstream_cursor_reset(struct bitstream_cursor *cur, void *s)
212*b411b363SPhilipp Reisner {
213*b411b363SPhilipp Reisner 	cur->b = s;
214*b411b363SPhilipp Reisner 	cur->bit = 0;
215*b411b363SPhilipp Reisner }
216*b411b363SPhilipp Reisner 
217*b411b363SPhilipp Reisner /* advance cursor by that many bits; maximum expected input value: 64,
218*b411b363SPhilipp Reisner  * but depending on VLI implementation, it may be more. */
219*b411b363SPhilipp Reisner static inline void bitstream_cursor_advance(struct bitstream_cursor *cur, unsigned int bits)
220*b411b363SPhilipp Reisner {
221*b411b363SPhilipp Reisner 	bits += cur->bit;
222*b411b363SPhilipp Reisner 	cur->b = cur->b + (bits >> 3);
223*b411b363SPhilipp Reisner 	cur->bit = bits & 7;
224*b411b363SPhilipp Reisner }
225*b411b363SPhilipp Reisner 
226*b411b363SPhilipp Reisner /* the bitstream itself knows its length */
227*b411b363SPhilipp Reisner struct bitstream {
228*b411b363SPhilipp Reisner 	struct bitstream_cursor cur;
229*b411b363SPhilipp Reisner 	unsigned char *buf;
230*b411b363SPhilipp Reisner 	size_t buf_len;		/* in bytes */
231*b411b363SPhilipp Reisner 
232*b411b363SPhilipp Reisner 	/* for input stream:
233*b411b363SPhilipp Reisner 	 * number of trailing 0 bits for padding
234*b411b363SPhilipp Reisner 	 * total number of valid bits in stream: buf_len * 8 - pad_bits */
235*b411b363SPhilipp Reisner 	unsigned int pad_bits;
236*b411b363SPhilipp Reisner };
237*b411b363SPhilipp Reisner 
238*b411b363SPhilipp Reisner static inline void bitstream_init(struct bitstream *bs, void *s, size_t len, unsigned int pad_bits)
239*b411b363SPhilipp Reisner {
240*b411b363SPhilipp Reisner 	bs->buf = s;
241*b411b363SPhilipp Reisner 	bs->buf_len = len;
242*b411b363SPhilipp Reisner 	bs->pad_bits = pad_bits;
243*b411b363SPhilipp Reisner 	bitstream_cursor_reset(&bs->cur, bs->buf);
244*b411b363SPhilipp Reisner }
245*b411b363SPhilipp Reisner 
246*b411b363SPhilipp Reisner static inline void bitstream_rewind(struct bitstream *bs)
247*b411b363SPhilipp Reisner {
248*b411b363SPhilipp Reisner 	bitstream_cursor_reset(&bs->cur, bs->buf);
249*b411b363SPhilipp Reisner 	memset(bs->buf, 0, bs->buf_len);
250*b411b363SPhilipp Reisner }
251*b411b363SPhilipp Reisner 
252*b411b363SPhilipp Reisner /* Put (at most 64) least significant bits of val into bitstream, and advance cursor.
253*b411b363SPhilipp Reisner  * Ignores "pad_bits".
254*b411b363SPhilipp Reisner  * Returns zero if bits == 0 (nothing to do).
255*b411b363SPhilipp Reisner  * Returns number of bits used if successful.
256*b411b363SPhilipp Reisner  *
257*b411b363SPhilipp Reisner  * If there is not enough room left in bitstream,
258*b411b363SPhilipp Reisner  * leaves bitstream unchanged and returns -ENOBUFS.
259*b411b363SPhilipp Reisner  */
260*b411b363SPhilipp Reisner static inline int bitstream_put_bits(struct bitstream *bs, u64 val, const unsigned int bits)
261*b411b363SPhilipp Reisner {
262*b411b363SPhilipp Reisner 	unsigned char *b = bs->cur.b;
263*b411b363SPhilipp Reisner 	unsigned int tmp;
264*b411b363SPhilipp Reisner 
265*b411b363SPhilipp Reisner 	if (bits == 0)
266*b411b363SPhilipp Reisner 		return 0;
267*b411b363SPhilipp Reisner 
268*b411b363SPhilipp Reisner 	if ((bs->cur.b + ((bs->cur.bit + bits -1) >> 3)) - bs->buf >= bs->buf_len)
269*b411b363SPhilipp Reisner 		return -ENOBUFS;
270*b411b363SPhilipp Reisner 
271*b411b363SPhilipp Reisner 	/* paranoia: strip off hi bits; they should not be set anyways. */
272*b411b363SPhilipp Reisner 	if (bits < 64)
273*b411b363SPhilipp Reisner 		val &= ~0ULL >> (64 - bits);
274*b411b363SPhilipp Reisner 
275*b411b363SPhilipp Reisner 	*b++ |= (val & 0xff) << bs->cur.bit;
276*b411b363SPhilipp Reisner 
277*b411b363SPhilipp Reisner 	for (tmp = 8 - bs->cur.bit; tmp < bits; tmp += 8)
278*b411b363SPhilipp Reisner 		*b++ |= (val >> tmp) & 0xff;
279*b411b363SPhilipp Reisner 
280*b411b363SPhilipp Reisner 	bitstream_cursor_advance(&bs->cur, bits);
281*b411b363SPhilipp Reisner 	return bits;
282*b411b363SPhilipp Reisner }
283*b411b363SPhilipp Reisner 
284*b411b363SPhilipp Reisner /* Fetch (at most 64) bits from bitstream into *out, and advance cursor.
285*b411b363SPhilipp Reisner  *
286*b411b363SPhilipp Reisner  * If more than 64 bits are requested, returns -EINVAL and leave *out unchanged.
287*b411b363SPhilipp Reisner  *
288*b411b363SPhilipp Reisner  * If there are less than the requested number of valid bits left in the
289*b411b363SPhilipp Reisner  * bitstream, still fetches all available bits.
290*b411b363SPhilipp Reisner  *
291*b411b363SPhilipp Reisner  * Returns number of actually fetched bits.
292*b411b363SPhilipp Reisner  */
293*b411b363SPhilipp Reisner static inline int bitstream_get_bits(struct bitstream *bs, u64 *out, int bits)
294*b411b363SPhilipp Reisner {
295*b411b363SPhilipp Reisner 	u64 val;
296*b411b363SPhilipp Reisner 	unsigned int n;
297*b411b363SPhilipp Reisner 
298*b411b363SPhilipp Reisner 	if (bits > 64)
299*b411b363SPhilipp Reisner 		return -EINVAL;
300*b411b363SPhilipp Reisner 
301*b411b363SPhilipp Reisner 	if (bs->cur.b + ((bs->cur.bit + bs->pad_bits + bits -1) >> 3) - bs->buf >= bs->buf_len)
302*b411b363SPhilipp Reisner 		bits = ((bs->buf_len - (bs->cur.b - bs->buf)) << 3)
303*b411b363SPhilipp Reisner 			- bs->cur.bit - bs->pad_bits;
304*b411b363SPhilipp Reisner 
305*b411b363SPhilipp Reisner 	if (bits == 0) {
306*b411b363SPhilipp Reisner 		*out = 0;
307*b411b363SPhilipp Reisner 		return 0;
308*b411b363SPhilipp Reisner 	}
309*b411b363SPhilipp Reisner 
310*b411b363SPhilipp Reisner 	/* get the high bits */
311*b411b363SPhilipp Reisner 	val = 0;
312*b411b363SPhilipp Reisner 	n = (bs->cur.bit + bits + 7) >> 3;
313*b411b363SPhilipp Reisner 	/* n may be at most 9, if cur.bit + bits > 64 */
314*b411b363SPhilipp Reisner 	/* which means this copies at most 8 byte */
315*b411b363SPhilipp Reisner 	if (n) {
316*b411b363SPhilipp Reisner 		memcpy(&val, bs->cur.b+1, n - 1);
317*b411b363SPhilipp Reisner 		val = le64_to_cpu(val) << (8 - bs->cur.bit);
318*b411b363SPhilipp Reisner 	}
319*b411b363SPhilipp Reisner 
320*b411b363SPhilipp Reisner 	/* we still need the low bits */
321*b411b363SPhilipp Reisner 	val |= bs->cur.b[0] >> bs->cur.bit;
322*b411b363SPhilipp Reisner 
323*b411b363SPhilipp Reisner 	/* and mask out bits we don't want */
324*b411b363SPhilipp Reisner 	val &= ~0ULL >> (64 - bits);
325*b411b363SPhilipp Reisner 
326*b411b363SPhilipp Reisner 	bitstream_cursor_advance(&bs->cur, bits);
327*b411b363SPhilipp Reisner 	*out = val;
328*b411b363SPhilipp Reisner 
329*b411b363SPhilipp Reisner 	return bits;
330*b411b363SPhilipp Reisner }
331*b411b363SPhilipp Reisner 
332*b411b363SPhilipp Reisner /* encodes @in as vli into @bs;
333*b411b363SPhilipp Reisner 
334*b411b363SPhilipp Reisner  * return values
335*b411b363SPhilipp Reisner  *  > 0: number of bits successfully stored in bitstream
336*b411b363SPhilipp Reisner  * -ENOBUFS @bs is full
337*b411b363SPhilipp Reisner  * -EINVAL input zero (invalid)
338*b411b363SPhilipp Reisner  * -EOVERFLOW input too large for this vli code (invalid)
339*b411b363SPhilipp Reisner  */
340*b411b363SPhilipp Reisner static inline int vli_encode_bits(struct bitstream *bs, u64 in)
341*b411b363SPhilipp Reisner {
342*b411b363SPhilipp Reisner 	u64 code = code;
343*b411b363SPhilipp Reisner 	int bits = __vli_encode_bits(&code, in);
344*b411b363SPhilipp Reisner 
345*b411b363SPhilipp Reisner 	if (bits <= 0)
346*b411b363SPhilipp Reisner 		return bits;
347*b411b363SPhilipp Reisner 
348*b411b363SPhilipp Reisner 	return bitstream_put_bits(bs, code, bits);
349*b411b363SPhilipp Reisner }
350*b411b363SPhilipp Reisner 
351*b411b363SPhilipp Reisner #endif
352