-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvarintChained.c
413 lines (371 loc) · 10.1 KB
/
varintChained.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
** This was originally extracted from util.c inside varintChained as of commit:
** "2015-09-01 [adf9fefb] — improvement in varintChainedVarintLen()."
**
*************************************************************************
*/
#include "varintChained.h"
#include "varint.h"
#include <stdarg.h>
/*
* ** Constants for the largest and smallest possible 64-bit signed integers.
* ** These macros are designed to work correctly on both 32-bit and 64-bit
* ** compilers.
* */
#define LARGEST_INT64 (0xffffffff | (((int64_t)0x7fffffff) << 32))
#define SMALLEST_INT64 (((int64_t)-1) - LARGEST_INT64)
/*
** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
** that can be stored in a u32 without loss of data. The value
** is 0x00000000ffffffff. But because of quirks of some compilers, we
** have to specify the value in the less intuitive manner shown:
*/
#define SQLITE_MAX_U32 ((((uint64_t)1) << 32) - 1)
/*
** The variable-length integer encoding is as follows:
**
** KEY:
** A = 0xxxxxxx 7 bits of data and one flag bit
** B = 1xxxxxxx 7 bits of data and one flag bit
** C = xxxxxxxx 8 bits of data
**
** 7 bits - A
** 14 bits - BA
** 21 bits - BBA
** 28 bits - BBBA
** 35 bits - BBBBA
** 42 bits - BBBBBA
** 49 bits - BBBBBBA
** 56 bits - BBBBBBBA
** 64 bits - BBBBBBBBC
*/
/*
** Write a 64-bit variable-length integer to memory starting at p[0].
** The length of data write will be between 1 and 9 bytes. The number
** of bytes written is returned.
**
** A variable-length integer consists of the lower 7 bits of each byte
** for all bytes that have the 8th bit set and one byte with the 8th
** bit clear. Except, if we get to the 9th byte, it stores the full
** 8 bits and is the last byte.
*/
static varintWidth __attribute__((noinline))
putVarint64(uint8_t *p, uint64_t v) {
int32_t i, j, n;
uint8_t buf[10];
if (v & (((uint64_t)0xff000000) << 32)) {
p[8] = (uint8_t)v;
v >>= 8;
for (i = 7; i >= 0; i--) {
p[i] = (uint8_t)((v & 0x7f) | 0x80);
v >>= 7;
}
return 9;
}
n = 0;
do {
buf[n++] = (uint8_t)((v & 0x7f) | 0x80);
v >>= 7;
} while (v != 0);
buf[0] &= 0x7f;
assert(n <= 9);
for (i = 0, j = n - 1; j >= 0; j--, i++) {
p[i] = buf[j];
}
return n;
}
varintWidth varintChainedPutVarint(uint8_t *p, uint64_t v) {
if (v <= 0x7f) {
p[0] = v & 0x7f;
return 1;
}
if (v <= 0x3fff) {
p[0] = ((v >> 7) & 0x7f) | 0x80;
p[1] = v & 0x7f;
return 2;
}
return putVarint64(p, v);
}
/*
** Bitmasks used by varintChainedGetVarint(). These precomputed constants
** are defined here rather than simply putting the constant expressions
** inline in order to work around bugs in the RVT compiler.
**
** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
**
** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
*/
#define SLOT_2_0 0x001fc07f
#define SLOT_4_2_0 0xf01fc07f
/*
** Read a 64-bit variable-length integer from memory starting at p[0].
** Return the number of bytes read. The value is stored in *v.
*/
varintWidth varintChainedGetVarint(const uint8_t *p, uint64_t *v) {
uint32_t a, b, s;
if (((int8_t *)p)[0] >= 0) {
*v = *p;
return 1;
}
if (((int8_t *)p)[1] >= 0) {
*v = ((uint32_t)(p[0] & 0x7f) << 7) | p[1];
return 2;
}
/* Verify that constants are precomputed correctly */
assert(SLOT_2_0 == ((0x7f << 14) | (0x7f)));
assert(SLOT_4_2_0 == ((0xfU << 28) | (0x7f << 14) | (0x7f)));
a = ((uint32_t)p[0]) << 14;
b = p[1];
p += 2;
a |= *p;
/* a: p0<<14 | p2 (unmasked) */
if (!(a & 0x80)) {
a &= SLOT_2_0;
b &= 0x7f;
b = b << 7;
a |= b;
*v = a;
return 3;
}
/* CSE1 from below */
a &= SLOT_2_0;
p++;
b = b << 14;
b |= *p;
/* b: p1<<14 | p3 (unmasked) */
if (!(b & 0x80)) {
b &= SLOT_2_0;
/* moved CSE1 up */
/* a &= (0x7f<<14)|(0x7f); */
a = a << 7;
a |= b;
*v = a;
return 4;
}
/* a: p0<<14 | p2 (masked) */
/* b: p1<<14 | p3 (unmasked) */
/* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
/* moved CSE1 up */
/* a &= (0x7f<<14)|(0x7f); */
b &= SLOT_2_0;
s = a;
/* s: p0<<14 | p2 (masked) */
p++;
a = a << 14;
a |= *p;
/* a: p0<<28 | p2<<14 | p4 (unmasked) */
if (!(a & 0x80)) {
/* we can skip these cause they were (effectively) done above in
* calc'ing s */
/* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
/* b &= (0x7f<<14)|(0x7f); */
b = b << 7;
a |= b;
s = s >> 18;
*v = ((uint64_t)s) << 32 | a;
return 5;
}
/* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
s = s << 7;
s |= b;
/* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
p++;
b = b << 14;
b |= *p;
/* b: p1<<28 | p3<<14 | p5 (unmasked) */
if (!(b & 0x80)) {
/* we can skip this cause it was (effectively) done above in calc'ing s
*/
/* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
a &= SLOT_2_0;
a = a << 7;
a |= b;
s = s >> 18;
*v = ((uint64_t)s) << 32 | a;
return 6;
}
p++;
a = a << 14;
a |= *p;
/* a: p2<<28 | p4<<14 | p6 (unmasked) */
if (!(a & 0x80)) {
a &= SLOT_4_2_0;
b &= SLOT_2_0;
b = b << 7;
a |= b;
s = s >> 11;
*v = ((uint64_t)s) << 32 | a;
return 7;
}
/* CSE2 from below */
a &= SLOT_2_0;
p++;
b = b << 14;
b |= *p;
/* b: p3<<28 | p5<<14 | p7 (unmasked) */
if (!(b & 0x80)) {
b &= SLOT_4_2_0;
/* moved CSE2 up */
/* a &= (0x7f<<14)|(0x7f); */
a = a << 7;
a |= b;
s = s >> 4;
*v = ((uint64_t)s) << 32 | a;
return 8;
}
p++;
a = a << 15;
a |= *p;
/* a: p4<<29 | p6<<15 | p8 (unmasked) */
/* moved CSE2 up */
/* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
b &= SLOT_2_0;
b = b << 8;
a |= b;
s = s << 4;
b = p[-4];
b &= 0x7f;
b = b >> 3;
s |= b;
*v = ((uint64_t)s) << 32 | a;
return 9;
}
/*
** Read a 32-bit variable-length integer from memory starting at p[0].
** Return the number of bytes read. The value is stored in *v.
**
** If the varint32_t stored in p[0] is larger than can fit in a 32-bit unsigned
** integer, then set *v to 0xffffffff.
**
** A MACRO version, getVarint32, is provided which inlines the
** single-byte case. All code should use the MACRO version as
** this function assumes the single-byte case has already been handled.
*/
varintWidth varintChainedGetVarint32(const uint8_t *p, uint32_t *v) {
uint32_t a, b;
/* The 1-byte case. Overwhelmingly the most common. Handled inline
** by the getVarin32() macro */
a = *p;
/* a: p0 (unmasked) */
#ifndef varintChained_getVarint32
if (!(a & 0x80)) {
/* Values between 0 and 127 */
*v = a;
return 1;
}
#endif
/* The 2-byte case */
p++;
b = *p;
/* b: p1 (unmasked) */
if (!(b & 0x80)) {
/* Values between 128 and 16383 */
a &= 0x7f;
a = a << 7;
*v = a | b;
return 2;
}
/* The 3-byte case */
p++;
a = a << 14;
a |= *p;
/* a: p0<<14 | p2 (unmasked) */
if (!(a & 0x80)) {
/* Values between 16384 and 2097151 */
a &= (0x7f << 14) | (0x7f);
b &= 0x7f;
b = b << 7;
*v = a | b;
return 3;
}
/* A 32-bit varint32_t is used to store size information in btrees.
** Objects are rarely larger than 2MiB limit of a 3-byte varint.
** A 3-byte varint32_t is sufficient, for example, to record the size
** of a 1048569-byte BLOB or string.
**
** We only unroll the first 1-, 2-, and 3- byte cases. The very
** rare larger cases can be handled by the slower 64-bit varint
** routine.
*/
#if 1
{
uint64_t v64;
uint8_t n;
p -= 2;
n = varintChainedGetVarint(p, &v64);
assert(n > 3 && n <= 9);
if ((v64 & SQLITE_MAX_U32) != v64) {
*v = 0xffffffff;
} else {
*v = (uint32_t)v64;
}
return n;
}
#else
/* For following code (kept for historical record only) shows an
** unrolling for the 3- and 4-byte varint32_t cases. This code is
** slightly faster, but it is also larger and much harder to test.
*/
p++;
b = b << 14;
b |= *p;
/* b: p1<<14 | p3 (unmasked) */
if (!(b & 0x80)) {
/* Values between 2097152 and 268435455 */
b &= (0x7f << 14) | (0x7f);
a &= (0x7f << 14) | (0x7f);
a = a << 7;
*v = a | b;
return 4;
}
p++;
a = a << 14;
a |= *p;
/* a: p0<<28 | p2<<14 | p4 (unmasked) */
if (!(a & 0x80)) {
/* Values between 268435456 and 34359738367 */
a &= SLOT_4_2_0;
b &= SLOT_4_2_0;
b = b << 7;
*v = a | b;
return 5;
}
/* We can only reach this point32_t when reading a corrupt database
** file. In that case we are not in any hurry. Use the (relatively
** slow) general-purpose varintChainedGetVarint() routine to extract the
** value. */
{
uint64_t v64;
uint8_t n;
p -= 4;
n = varintChainedGetVarint(p, &v64);
assert(n > 5 && n <= 9);
*v = (uint32_t)v64;
return n;
}
#endif
}
/*
** Return the number of bytes that will be needed to store the given
** 64-bit integer.
*/
varintWidth varintChainedVarintLen(uint64_t v) {
varintWidth i = VARINT_WIDTH_8B;
while (v >>= 7) {
i++;
}
/* The loop will create a 10 byte length for the extreme
* upper range of 64 bit integers, but our varints encode
* the last byte with 8 bits, so all 10 byte length
* calculations are actually 9 byte varints. */
return i > 9 ? 9 : i;
}