blob: 89d69be6251f27649a4b3e4cdf14849e3f1e28d4 (
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
|
#ifndef BIT_IO_INL_H_
#error "Direct inclusion of this file is not allowed, include bit_io.h"
// For the sake of sane code completion.
#include "bit_io.h"
#endif
#include <util/system/compiler.h>
#include <util/system/unaligned_mem.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
inline TBitWriter::TBitWriter(char* ptr)
: Ptr_(ptr)
{ }
Y_FORCE_INLINE void TBitWriter::WriteBits(ui32 value, int width)
{
// Flush a whole 32-bit word at once (a single unaligned store) instead of
// looping over individual bytes with a data-dependent trip count.
Accumulator_ = (Accumulator_ << width) | value;
BitCount_ += width;
if (BitCount_ >= 32) {
BitCount_ -= 32;
ui32 word = __builtin_bswap32(static_cast<ui32>(Accumulator_ >> BitCount_));
WriteUnaligned<ui32>(Ptr_, word);
Ptr_ += sizeof(word);
}
}
inline char* TBitWriter::Finish()
{
while (BitCount_ >= 8) {
BitCount_ -= 8;
*Ptr_++ = static_cast<char>((Accumulator_ >> BitCount_) & 0xff);
}
if (BitCount_ > 0) {
*Ptr_++ = static_cast<char>((Accumulator_ << (8 - BitCount_)) & 0xff);
BitCount_ = 0;
}
Accumulator_ = 0;
return Ptr_;
}
////////////////////////////////////////////////////////////////////////////////
inline TBitReader::TBitReader(const char* ptr)
: Ptr_(reinterpret_cast<const ui8*>(ptr))
{ }
Y_FORCE_INLINE ui32 TBitReader::ReadBits(int width)
{
while (BitCount_ < width) {
Accumulator_ = (Accumulator_ << 8) | *Ptr_++;
BitCount_ += 8;
}
BitCount_ -= width;
return static_cast<ui32>((Accumulator_ >> BitCount_) & ((1ull << width) - 1));
}
inline const char* TBitReader::Finish()
{
return reinterpret_cast<const char*>(Ptr_);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|