summaryrefslogtreecommitdiff
path: root/src/include/intpack.i
blob: 9cc6acc2d7edebaafd95ceaa34a93a710336f843 (plain)
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
/*-
 * Copyright (c) 2014-2017 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

/*
 * Variable-length integer encoding.
 * We need up to 64 bits, signed and unsigned.  Further, we want the packed
 * representation to have the same lexicographic ordering as the integer
 * values.  This avoids the need for special-purpose comparison code.
 *
 * Try hard to keep small values small (up to ~2 bytes): that gives the biggest
 * benefit for common cases storing small values.  After that, just encode the
 * length in the first byte: we could squeeze in a couple of extra bits, but
 * the marginal benefit is small, and we want this code to be relatively
 * easy to implement in client code or scripting APIs.
 *
 * First byte  | Next |                        |
 * byte        | bytes| Min Value              | Max Value
 * ------------+------+------------------------+--------------------------------
 * [00 00xxxx] | free | N/A                    | N/A
 * [00 01llll] | llll | -2^64                  | -2^13 - 2^6
 * [00 1xxxxx] | 1    | -2^13 - 2^6            | -2^6 - 1
 * [01 xxxxxx] | 0    | -2^6                   | -1
 * [10 xxxxxx] | 0    | 0                      | 2^6 - 1
 * [11 0xxxxx] | 1    | 2^6                    | 2^13 + 2^6 - 1
 * [11 10llll] | llll | 2^13 + 2^6             | 2^64 - 1
 * [11 11xxxx] | free | N/A                    | N/A
 */

#define	NEG_MULTI_MARKER (uint8_t)0x10
#define	NEG_2BYTE_MARKER (uint8_t)0x20
#define	NEG_1BYTE_MARKER (uint8_t)0x40
#define	POS_1BYTE_MARKER (uint8_t)0x80
#define	POS_2BYTE_MARKER (uint8_t)0xc0
#define	POS_MULTI_MARKER (uint8_t)0xe0

#define	NEG_1BYTE_MIN (-(1 << 6))
#define	NEG_2BYTE_MIN (-(1 << 13) + NEG_1BYTE_MIN)
#define	POS_1BYTE_MAX ((1 << 6) - 1)
#define	POS_2BYTE_MAX ((1 << 13) + POS_1BYTE_MAX)

/* Extract bits <start> to <end> from a value (counting from LSB == 0). */
#define	GET_BITS(x, start, end)                                         \
	(((uint64_t)(x) & ((1U << (start)) - 1U)) >> (end))

/*
 * Size checks: return ENOMEM if not enough room when writing, EINVAL if the
 * length is wrong when reading (presumably the value is corrupted).
 */
#define	WT_SIZE_CHECK_PACK(l, maxl)					\
	WT_RET_TEST((maxl) != 0 && (size_t)(l) > (maxl), ENOMEM)
#define	WT_SIZE_CHECK_UNPACK(l, maxl)					\
	WT_RET_TEST((maxl) != 0 && (size_t)(l) > (maxl), EINVAL)

/* Count the leading zero bytes. */
#if defined(__GNUC__)
#define	WT_LEADING_ZEROS(x, i)						\
	((i) = ((x) == 0) ? (int)sizeof(x) : __builtin_clzll(x) >> 3)
#elif defined(_MSC_VER)
#define	WT_LEADING_ZEROS(x, i)	do {					\
	if ((x) == 0) (i) = (int)sizeof(x);				\
	else  { 							\
		unsigned long __index;					\
		_BitScanReverse64(&__index, x);				\
		__index = 63 ^ __index;					\
		(i) = (int)(__index >> 3); }				\
	} while (0)
#else
#define	WT_LEADING_ZEROS(x, i) do {					\
	uint64_t __x = (x);						\
	uint64_t __m = (uint64_t)0xff << 56;				\
	for ((i) = 0; !(__x & __m) && (i) != 8; (i)++)			\
		__m >>= 8;						\
} while (0)
#endif

/*
 * __wt_vpack_posint --
 *	Packs a positive variable-length integer in the specified location.
 */
static inline int
__wt_vpack_posint(uint8_t **pp, size_t maxlen, uint64_t x)
{
	uint8_t *p;
	int len, lz, shift;

	WT_LEADING_ZEROS(x, lz);
	len = (int)sizeof(x) - lz;
	WT_SIZE_CHECK_PACK(len + 1, maxlen);
	p = *pp;

	/* There are four bits we can use in the first byte. */
	*p++ |= (len & 0xf);

	for (shift = (len - 1) << 3; len != 0; --len, shift -= 8)
		*p++ = (uint8_t)(x >> shift);

	*pp = p;
	return (0);
}

/*
 * __wt_vpack_negint --
 *	Packs a negative variable-length integer in the specified location.
 */
static inline int
__wt_vpack_negint(uint8_t **pp, size_t maxlen, uint64_t x)
{
	uint8_t *p;
	int len, lz, shift;

	WT_LEADING_ZEROS(~x, lz);
	len = (int)sizeof(x) - lz;
	WT_SIZE_CHECK_PACK(len + 1, maxlen);
	p = *pp;

	/*
	 * There are four size bits we can use in the first byte.
	 * For negative numbers, we store the number of leading 0xff bytes
	 * to maintain ordering (if this is not obvious, it may help to
	 * remember that -1 is the largest negative number).
	 */
	*p++ |= (lz & 0xf);

	for (shift = (len - 1) << 3; len != 0; shift -= 8, --len)
		*p++ = (uint8_t)(x >> shift);

	*pp = p;
	return (0);
}

/*
 * __wt_vunpack_posint --
 *	Reads a variable-length positive integer from the specified location.
 */
static inline int
__wt_vunpack_posint(const uint8_t **pp, size_t maxlen, uint64_t *retp)
{
	uint64_t x;
	uint8_t len;
	const uint8_t *p;

	/* There are four length bits in the first byte. */
	p = *pp;
	len = (*p++ & 0xf);
	WT_SIZE_CHECK_UNPACK(len + 1, maxlen);

	for (x = 0; len != 0; --len)
		x = (x << 8) | *p++;

	*retp = x;
	*pp = p;
	return (0);
}

/*
 * __wt_vunpack_negint --
 *	Reads a variable-length negative integer from the specified location.
 */
static inline int
__wt_vunpack_negint(const uint8_t **pp, size_t maxlen, uint64_t *retp)
{
	uint64_t x;
	uint8_t len;
	const uint8_t *p;

	/* There are four length bits in the first byte. */
	p = *pp;
	len = (int)sizeof(x) - (*p++ & 0xf);
	WT_SIZE_CHECK_UNPACK(len + 1, maxlen);

	for (x = UINT64_MAX; len != 0; --len)
		x = (x << 8) | *p++;

	*retp = x;
	*pp = p;
	return (0);
}

/*
 * __wt_vpack_uint --
 *	Variable-sized packing for unsigned integers
 */
static inline int
__wt_vpack_uint(uint8_t **pp, size_t maxlen, uint64_t x)
{
	uint8_t *p;

	WT_SIZE_CHECK_PACK(1, maxlen);
	p = *pp;
	if (x <= POS_1BYTE_MAX)
		*p++ = POS_1BYTE_MARKER | GET_BITS(x, 6, 0);
	else if (x <= POS_2BYTE_MAX) {
		WT_SIZE_CHECK_PACK(2, maxlen);
		x -= POS_1BYTE_MAX + 1;
		*p++ = POS_2BYTE_MARKER | GET_BITS(x, 13, 8);
		*p++ = GET_BITS(x, 8, 0);
	} else if (x == POS_2BYTE_MAX + 1) {
		/*
		 * This is a special case where we could store the value with
		 * just a single byte, but we append a zero byte so that the
		 * encoding doesn't get shorter for this one value.
		 */
		*p++ = POS_MULTI_MARKER | 0x1;
		*p++ = 0;
	} else {
		x -= POS_2BYTE_MAX + 1;
		*p = POS_MULTI_MARKER;
		return (__wt_vpack_posint(pp, maxlen, x));
	}

	*pp = p;
	return (0);
}

/*
 * __wt_vpack_int --
 *	Variable-sized packing for signed integers
 */
static inline int
__wt_vpack_int(uint8_t **pp, size_t maxlen, int64_t x)
{
	uint8_t *p;

	WT_SIZE_CHECK_PACK(1, maxlen);
	p = *pp;
	if (x < NEG_2BYTE_MIN) {
		*p = NEG_MULTI_MARKER;
		return (__wt_vpack_negint(pp, maxlen, (uint64_t)x));
	}
	if (x < NEG_1BYTE_MIN) {
		WT_SIZE_CHECK_PACK(2, maxlen);
		x -= NEG_2BYTE_MIN;
		*p++ = NEG_2BYTE_MARKER | GET_BITS(x, 13, 8);
		*p++ = GET_BITS(x, 8, 0);
	} else if (x < 0) {
		x -= NEG_1BYTE_MIN;
		*p++ = NEG_1BYTE_MARKER | GET_BITS(x, 6, 0);
	} else
		/* For non-negative values, use the unsigned code above. */
		return (__wt_vpack_uint(pp, maxlen, (uint64_t)x));

	*pp = p;
	return (0);
}

/*
 * __wt_vunpack_uint --
 *	Variable-sized unpacking for unsigned integers
 */
static inline int
__wt_vunpack_uint(const uint8_t **pp, size_t maxlen, uint64_t *xp)
{
	const uint8_t *p;

	WT_SIZE_CHECK_UNPACK(1, maxlen);
	p = *pp;
	switch (*p & 0xf0) {
	case POS_1BYTE_MARKER:
	case POS_1BYTE_MARKER | 0x10:
	case POS_1BYTE_MARKER | 0x20:
	case POS_1BYTE_MARKER | 0x30:
		*xp = GET_BITS(*p, 6, 0);
		p += 1;
		break;
	case POS_2BYTE_MARKER:
	case POS_2BYTE_MARKER | 0x10:
		WT_SIZE_CHECK_UNPACK(2, maxlen);
		*xp = GET_BITS(*p++, 5, 0) << 8;
		*xp |= *p++;
		*xp += POS_1BYTE_MAX + 1;
		break;
	case POS_MULTI_MARKER:
		WT_RET(__wt_vunpack_posint(pp, maxlen, xp));
		*xp += POS_2BYTE_MAX + 1;
		return (0);
	default:
		return (EINVAL);
	}

	*pp = p;
	return (0);
}

/*
 * __wt_vunpack_int --
 *	Variable-sized packing for signed integers
 */
static inline int
__wt_vunpack_int(const uint8_t **pp, size_t maxlen, int64_t *xp)
{
	const uint8_t *p;

	WT_SIZE_CHECK_UNPACK(1, maxlen);
	p = *pp;
	switch (*p & 0xf0) {
	case NEG_MULTI_MARKER:
		WT_RET(__wt_vunpack_negint(pp, maxlen, (uint64_t *)xp));
		return (0);
	case NEG_2BYTE_MARKER:
	case NEG_2BYTE_MARKER | 0x10:
		WT_SIZE_CHECK_UNPACK(2, maxlen);
		*xp = (int64_t)(GET_BITS(*p++, 5, 0) << 8);
		*xp |= *p++;
		*xp += NEG_2BYTE_MIN;
		break;
	case NEG_1BYTE_MARKER:
	case NEG_1BYTE_MARKER | 0x10:
	case NEG_1BYTE_MARKER | 0x20:
	case NEG_1BYTE_MARKER | 0x30:
		*xp = NEG_1BYTE_MIN + (int64_t)GET_BITS(*p, 6, 0);
		p += 1;
		break;
	default:
		/* Identical to the unsigned case. */
		return (__wt_vunpack_uint(pp, maxlen, (uint64_t *)xp));
	}

	*pp = p;
	return (0);
}

/*
 * __wt_vsize_posint --
 *	Return the packed size of a positive variable-length integer.
 */
static inline size_t
__wt_vsize_posint(uint64_t x)
{
	int lz;

	WT_LEADING_ZEROS(x, lz);
	return ((size_t)(WT_INTPACK64_MAXSIZE - lz));
}

/*
 * __wt_vsize_negint --
 *	Return the packed size of a negative variable-length integer.
 */
static inline size_t
__wt_vsize_negint(uint64_t x)
{
	int lz;

	WT_LEADING_ZEROS(~x, lz);
	return (size_t)(WT_INTPACK64_MAXSIZE - lz);
}

/*
 * __wt_vsize_uint --
 *	Return the packed size of an unsigned integer.
 */
static inline size_t
__wt_vsize_uint(uint64_t x)
{
	if (x <= POS_1BYTE_MAX)
		return (1);
	if (x <= POS_2BYTE_MAX + 1)
		return (2);
	x -= POS_2BYTE_MAX + 1;
	return (__wt_vsize_posint(x));
}

/*
 * __wt_vsize_int --
 *	Return the packed size of a signed integer.
 */
static inline size_t
__wt_vsize_int(int64_t x)
{
	if (x < NEG_2BYTE_MIN)
		return (__wt_vsize_negint((uint64_t)x));
	if (x < NEG_1BYTE_MIN)
		return (2);
	if (x < 0)
		return (1);
	/* For non-negative values, use the unsigned code above. */
	return (__wt_vsize_uint((uint64_t)x));
}