-
Notifications
You must be signed in to change notification settings - Fork 24
/
ProbMap.hpp
151 lines (124 loc) · 3.87 KB
/
ProbMap.hpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* MCM file compressor
Copyright (C) 2016, Google Inc.
Authors: Mathieu Chartier
LICENSE
This file is part of the MCM file compressor.
MCM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MCM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MCM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROB_MAP_HPP_
#define PROB_MAP_HPP_
template <class Predictor, size_t kProbs>
class DynamicProbMap {
public:
Predictor probs_[kProbs];
size_t GetUpdater(size_t bit) const {
return bit;
}
// Get stretched prob.
int GetP(size_t index) const {
return probs_[index].getP();
}
template <typename Table>
void Update(size_t index, size_t bit_updater, const Table& table, size_t update = 9) {
probs_[index].update(bit_updater, update);
}
template <typename Table>
void SetP(size_t index, int p, Table& t) {
probs_[index].setP(p);
}
template <typename Table>
void UpdateAll(Table& table) {}
template <typename Table>
int GetSTP(size_t index, Table& t) const {
return t.st(GetP(index));
}
};
// Keeps track of stretched probabilities.
// format is: <learn:8><prob:32><stp:16>
template <size_t kProbs>
class FastAdaptiveProbMap {
public:
static constexpr size_t kPShift = 31;
static constexpr size_t kProbBits = 12;
uint64_t probs_[kProbs];
static constexpr size_t kLearnShift = 32;
static constexpr size_t kSTPShift = kLearnShift + 8;
public:
size_t GetUpdater(size_t bit) const {
return (bit << kPShift);
}
template <typename Table>
ALWAYS_INLINE void SetP(size_t index, int p, Table& t) {
Set(index, p << (kPShift - kProbBits), 9, t.st(p));
}
template <typename Table>
ALWAYS_INLINE void Update(size_t index, size_t bit_updater, const Table& table, size_t dummy = 0) {
uint64_t p = probs_[index];
p >>= 16;
const uint32_t lower = static_cast<uint32_t>(p);
const uint8_t learn = static_cast<uint8_t>(p >> kLearnShift);
p += (bit_updater - lower) >> learn;
const uint16_t st = table.st(lower >> (kPShift - kProbBits));
p = (p << 16) | st;
probs_[index] = p;
}
ALWAYS_INLINE int GetP(size_t index) const {
uint32_t p = probs_[index] >> 16;
return p >> (kPShift - kProbBits);
}
template <typename Table>
ALWAYS_INLINE int GetSTP(size_t index, const Table& table) const {
return static_cast<int16_t>(static_cast<uint16_t>(probs_[index]));
}
void SetLearn(size_t index, size_t learn) {
auto p = probs_[index];
p &= 0xFFFFFFFFFFFF;
p |= (learn << 48);
probs_[index] = p;
}
private:
ALWAYS_INLINE void Set(size_t index, uint32_t p, uint8_t learn, int16_t stp) {
uint64_t acc = learn;
acc = (acc << 32) | p;
acc = (acc << 16) | static_cast<uint16_t>(stp);
probs_[index] = acc;
}
};
template <class Predictor, size_t kProbs>
class FastProbMap {
Predictor probs_[kProbs];
int stp_[kProbs];
public:
// Get stretched prob.
int GetP(size_t index) const {
return probs_[index].getP();
}
template <typename Table>
void UpdateAll(Table& table) {
for (size_t i = 0; i < kProbs; ++i) {
stp_[i] = table.st(GetP(i));
}
}
void Update(size_t index, size_t bit, size_t update = 9) {
probs_[index].update(bit, update);
}
template <typename Table>
void SetP(size_t index, int p, Table& t) {
probs_[index].setP(p);
stp_[index] = t.st(p);
}
template <typename Table>
int GetSTP(size_t index, Table& t) const {
return stp_[index];
}
};
#endif