aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/bit_io/bitinput_impl.h
blob: b13fbef1012d8d3be87bfb30522d2557f7c9d49f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once

#include <util/generic/bitops.h>
#include <util/system/unaligned_mem.h>

namespace NBitIO {
    class TBitInputImpl {
        i64 RealStart;
        i64 Start;
        ui64 Length;
        ui64 BOffset;
        const ui32 FakeStart;
        char Fake[16];
        const i64 FStart;

    public:
        TBitInputImpl(const char* start, const char* end)
            : RealStart((i64)start)
            , Start((i64)start)
            , Length((end - start) << 3)
            , BOffset(0)
            , FakeStart(Length > 64 ? Length - 64 : 0)
            , FStart((i64)Fake - (FakeStart >> 3))
        {
            memcpy(Fake, (const char*)(RealStart + (FakeStart >> 3)), (Length - FakeStart) >> 3);
            Start = FakeStart ? RealStart : FStart;
        }

        ui64 GetBitLength() const {
            return Length;
        }

    protected:
        template <ui32 bits>
        Y_FORCE_INLINE bool ReadKImpl(ui64& result) {
            result = (ReadUnaligned<ui64>((const void*)(Start + (BOffset >> 3))) >> (BOffset & 7)) & Mask64(bits);
            BOffset += bits;
            if (BOffset < FakeStart)
                return true;
            if (BOffset > Length) {
                result = 0;
                BOffset -= bits;
                return false;
            }
            Start = FStart;
            return true;
        }

        Y_FORCE_INLINE bool ReadImpl(ui64& result, ui32 bits) {
            result = (ReadUnaligned<ui64>((const void*)(Start + (BOffset >> 3))) >> (BOffset & 7)) & MaskLowerBits(bits);
            BOffset += bits;
            if (BOffset < FakeStart)
                return true;
            if (Y_UNLIKELY(BOffset > Length)) {
                result = 0;
                BOffset -= bits;
                return false;
            }
            Start = FStart;
            return true;
        }

        Y_FORCE_INLINE bool EofImpl() const {
            return BOffset >= Length;
        }

        Y_FORCE_INLINE ui64 BitOffset() const {
            return BOffset;
        }

        Y_FORCE_INLINE bool Seek(i64 offset) {
            if (offset < 0 || offset > (i64)Length)
                return false;
            BOffset = offset;
            Start = BOffset < FakeStart ? RealStart : FStart;
            return true;
        }

    protected:
        template <ui64 bits, typename T>
        Y_FORCE_INLINE static void CopyToResultK(T& result, ui64 r64, ui64 skipbits) {
            result = (result & ~(Mask64(bits) << skipbits)) | (r64 << skipbits);
        }

        template <typename T>
        Y_FORCE_INLINE static void CopyToResult(T& result, ui64 r64, ui64 bits, ui64 skipbits) {
            result = (result & InverseMaskLowerBits(bits, skipbits)) | (r64 << skipbits);
        }

    public:
        template <ui64 bits>
        Y_FORCE_INLINE bool ReadWordsImpl(ui64& data) {
            data = 0;

            const ui64 haveMore = NthBit64(bits);
            const ui64 mask = Mask64(bits);
            ui64 current = 0;
            ui64 byteNo = 0;

            do {
                if (!ReadKImpl<bits + 1>(current))
                    return false;

                data |= (current & mask) << (byteNo++ * bits);
            } while (current & haveMore);

            return true;
        }
    };
}