1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitops.h>
4 #include <asm/unaligned.h>
8 int bch2_varint_encode(u8 *out, u64 v)
10 unsigned bits = fls64(v|1);
11 unsigned bytes = DIV_ROUND_UP(bits, 7);
13 if (likely(bytes < 9)) {
15 v |= ~(~0 << (bytes - 1));
21 put_unaligned_le64(v, out);
25 int bch2_varint_decode(const u8 *in, const u8 *end, u64 *out)
27 u64 v = get_unaligned_le64(in);
28 unsigned bytes = ffz(v & 255) + 1;
30 if (unlikely(in + bytes > end))
33 if (likely(bytes < 9)) {
35 v &= ~(~0ULL << (7 * bytes));
37 v = get_unaligned_le64(++in);