2 * @brief Classes to encode/decode a bitstream.
4 /* Copyright (C) 2004,2005,2006,2008,2013,2014,2016,2017,2018 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
24 #include "bitstream.h"
26 #include <xapian/types.h>
33 // Find the position of the most significant set bit counting from 1 with
34 // 0 being returned if no bits are set (similar to how ffs() reports the least
35 // significant set bit).
38 highest_order_bit(T mask
)
41 return mask
? sizeof(T
) * 8 - do_clz(mask
) : 0;
43 // Table of results for 8 bit inputs.
44 static const unsigned char hob_tab
[256] = {
45 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
46 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
47 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
48 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
49 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
50 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
51 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
52 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
53 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
54 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
55 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
56 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
57 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
59 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
60 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
64 if constexpr(sizeof(T
) > 4) {
65 if (mask
>= 0x100000000ul
) {
70 if (mask
>= 0x10000u
) {
78 return result
+ hob_tab
[mask
];
84 /// Shift left that's safe for shifts wider than the type.
85 template<typename T
, typename U
>
86 static constexpr inline
87 T
safe_shl(T x
, U shift
)
89 return (shift
>= sizeof(T
) * 8 ? 0 : x
<< shift
);
93 BitWriter::encode(Xapian::termpos value
, Xapian::termpos outof
)
95 Assert(value
< outof
);
96 unsigned bits
= highest_order_bit(outof
- Xapian::termpos(1));
97 // If the top bit of (outof - Xapian::termpos(1)) is set then
98 // the shift will shift the bit out and give zero and the
99 // subtraction will result in an unsigned overflow.
100 const Xapian::termpos spare
=
101 UNSIGNED_OVERFLOW_OK(safe_shl(Xapian::termpos(1), bits
) - outof
);
103 /* If we have spare values, we can use one fewer bit to encode some
104 * values. We shorten the values in the middle of the range, as
105 * testing (on positional data) shows this works best. "Managing
106 * Gigabytes" suggests reversing this for the lowest level and encoding
107 * the end values of the range shorter, which is contrary to our
108 * testing (MG is talking about posting lists, which probably have
109 * different characteristics).
111 * For example, if outof is 11, the codes emitted are:
126 * Note the LSB comes first in the bitstream, so these codes need to be
127 * suffix-free to be decoded.
129 const Xapian::termpos mid_start
= (outof
- spare
) / 2;
130 if (value
>= mid_start
+ spare
) {
131 value
= (value
- (mid_start
+ spare
)) |
132 (Xapian::termpos(1) << (bits
- 1));
133 } else if (value
>= mid_start
) {
138 if (bits
+ n_bits
> sizeof(acc
) * 8) {
139 // We need to write more bits than there's empty room for in
140 // the accumulator. So we arrange to shift out 8 bits, then
141 // adjust things so we're adding 8 fewer bits.
142 Assert(bits
<= sizeof(acc
) * 8);
143 acc
|= (value
<< n_bits
);
149 acc
|= (value
<< n_bits
);
151 while (n_bits
>= 8) {
159 BitWriter::encode_interpolative(const Xapian::VecCOW
<Xapian::termpos
>& pos
,
162 // "Interpolative code" - for an algorithm description, see "Managing
163 // Gigabytes" - pages 126-127 in the second edition. You can probably
164 // view those pages in google books.
166 const Xapian::termpos mid
= j
+ (k
- j
) / 2;
167 // Encode one out of (pos[k] - pos[j] + 1) values
168 // (less some at either end because we must be able to fit
169 // all the intervening pos in)
170 const Xapian::termpos outof
= pos
[k
] - pos
[j
] + j
- k
+ 1;
171 const Xapian::termpos lowest
= pos
[j
] + mid
- j
;
172 encode(pos
[mid
] - lowest
, outof
);
173 encode_interpolative(pos
, j
, mid
);
179 BitReader::decode(Xapian::termpos outof
, bool force
)
182 Assert(force
== di_current
.is_initialized());
183 Xapian::termpos bits
= highest_order_bit(outof
- Xapian::termpos(1));
184 // If the top bit of (outof - Xapian::termpos(1)) is set then
185 // the shift will shift the bit out and give zero and the
186 // subtraction will result in an unsigned overflow.
187 const Xapian::termpos spare
=
188 UNSIGNED_OVERFLOW_OK(safe_shl(Xapian::termpos(1), bits
) - outof
);
189 const Xapian::termpos mid_start
= (outof
- spare
) / 2;
192 pos
= read_bits(bits
- 1);
193 if (pos
< mid_start
) {
194 if (read_bits(1)) pos
+= mid_start
+ spare
;
197 pos
= read_bits(bits
);
204 BitReader::read_bits(int count
)
206 Xapian::termpos result
;
207 if (count
> int(sizeof(acc
) * 8 - 7)) {
208 // If we need more than 7 bits less than fit in acc do the read in two
209 // goes to ensure that we don't overflow acc. This is a little more
210 // conservative than it needs to be, but such large values will
211 // inevitably be rare (because you can't fit very many of them into
212 // the full Xapian::termpos range).
213 Assert(count
<= int(sizeof(acc
) * 8));
214 const size_t half_the_bits
= sizeof(acc
) * 4;
215 result
= read_bits(half_the_bits
);
216 return result
| (read_bits(count
- half_the_bits
) << half_the_bits
);
218 while (n_bits
< count
) {
220 acc
|= Xapian::termpos(static_cast<unsigned char>(*p
++)) << n_bits
;
223 result
= acc
& ((Xapian::termpos(1) << count
) - Xapian::termpos(1));
230 BitReader::decode_interpolative(int j
, int k
,
231 Xapian::termpos pos_j
, Xapian::termpos pos_k
)
233 Assert(!di_current
.is_initialized());
234 di_stack
.reserve(highest_order_bit(pos_k
- pos_j
));
235 di_current
.set_j(j
, pos_j
);
236 di_current
.set_k(k
, pos_k
);
240 BitReader::decode_interpolative_next()
242 Assert(di_current
.is_initialized());
243 while (!di_stack
.empty() || di_current
.is_next()) {
244 if (!di_current
.is_next()) {
245 Xapian::termpos pos_ret
= di_current
.pos_k
;
246 di_current
= di_stack
.back();
248 int mid
= (di_current
.j
+ di_current
.k
) / 2;
249 di_current
.set_j(mid
, pos_ret
);
252 di_stack
.push_back(di_current
);
253 int mid
= (di_current
.j
+ di_current
.k
) / 2;
254 Xapian::termpos pos_mid
= decode(di_current
.outof(), true) +
255 (di_current
.pos_j
+ mid
- di_current
.j
);
256 di_current
.set_k(mid
, pos_mid
);
258 #ifdef XAPIAN_ASSERTIONS
261 return di_current
.pos_k
;