Coverage Report

Created: 2026-08-25 19:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/util/golombrice.h
Line
Count
Source
1
// Copyright (c) 2018-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef BITCOIN_UTIL_GOLOMBRICE_H
6
#define BITCOIN_UTIL_GOLOMBRICE_H
7
8
#include <util/fastrange.h>
9
10
#include <streams.h>
11
12
#include <cstdint>
13
14
template <typename OStream>
15
void GolombRiceEncode(BitStreamWriter<OStream>& bitwriter, uint8_t P, uint64_t x)
16
54.1k
{
17
    // Write quotient as unary-encoded: q 1's followed by one 0.
18
54.1k
    uint64_t q = x >> P;
19
81.6k
    while (q > 0) {
  Branch (19:12): [True: 27.5k, False: 54.1k]
20
27.5k
        int nbits = q <= 64 ? static_cast<int>(q) : 64;
  Branch (20:21): [True: 27.5k, False: 3]
21
27.5k
        bitwriter.Write(~0ULL, nbits);
22
27.5k
        q -= nbits;
23
27.5k
    }
24
54.1k
    bitwriter.Write(0, 1);
25
26
    // Write the remainder in P bits. Since the remainder is just the bottom
27
    // P bits of x, there is no need to mask first.
28
54.1k
    bitwriter.Write(x, P);
29
54.1k
}
30
31
template <typename IStream>
32
uint64_t GolombRiceDecode(BitStreamReader<IStream>& bitreader, uint8_t P)
33
1.06M
{
34
    // Read unary-encoded quotient: q 1's followed by one 0.
35
1.06M
    uint64_t q = 0;
36
1.33M
    while (bitreader.Read(1) == 1) {
  Branch (36:12): [True: 269k, False: 1.06M]
37
269k
        ++q;
38
269k
    }
39
40
1.06M
    uint64_t r = bitreader.Read(P);
41
42
1.06M
    return (q << P) + r;
43
1.06M
}
44
45
#endif // BITCOIN_UTIL_GOLOMBRICE_H