summaryrefslogtreecommitdiff
path: root/src/include/bitstring.i
blob: 4f788a625b5bc45184b7ebdc8955c16b9ee6c6a8 (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
/*-
 * Copyright (c) 2008-2012 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/*-
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Paul Vixie.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: src/sys/sys/bitstring.h,v 1.5 2005/01/07 02:29:23 imp Exp $
 */

				/* byte of the bitstring bit is in */
#define	__bit_byte(bit)	((bit) >> 3)

				/* mask for the bit within its byte */
#define	__bit_mask(bit)	(1 << ((bit) & 0x7))

/*
 * __bitstr_size --
 *	Return the bytes in a bitstring of nbits.
 */
static inline uint32_t
__bitstr_size(uint32_t nbits)
{
	return (((nbits) + 7) >> 3);
}

/*
 * __bit_alloc --
 *	Allocate a bitstring.
 */
static inline int
__bit_alloc(WT_SESSION_IMPL *session, uint32_t nbits, void *retp)
{
	return (__wt_calloc(
	    session, (size_t)__bitstr_size(nbits), sizeof(uint8_t), retp));
}

/*
 * __bit_test --
 *	Test one bit in name.
 */
static inline int
__bit_test(uint8_t *bitf, uint32_t bit)
{
	return (bitf[__bit_byte(bit)] & __bit_mask(bit) ? 1 : 0);
}

/*
 * __bit_set --
 *	Set one bit in name.
 */
static inline void
__bit_set(uint8_t *bitf, uint32_t bit)
{
	bitf[__bit_byte(bit)] |= __bit_mask(bit);
}

/*
 * __bit_clear --
 *	Clear one bit in name.
 */
static inline void
__bit_clear(uint8_t *bitf, uint32_t bit)
{
	bitf[__bit_byte(bit)] &= ~__bit_mask(bit);
}

#ifdef __NOT_CURRENTLY_USED
/*
 * __bit_nclr --
 *	Clear bits start-to-stop in name.
 */
static inline void
__bit_nclr(uint8_t *bitf, uint32_t start, uint32_t stop)
{
	uint32_t startbyte, stopbyte;

	startbyte = __bit_byte(start);
	stopbyte = __bit_byte(stop);

	if (startbyte == stopbyte)
		bitf[startbyte] &=
		    ((0xff >> (8 - (start & 0x7))) |
		    (0xff << ((stop & 0x7) + 1)));
	else {
		bitf[startbyte] &= 0xff >> (8 - (start & 0x7));
		while (++startbyte < stopbyte)
			bitf[startbyte] = 0;
		bitf[stopbyte] &= 0xff << ((stop & 0x7) + 1);
	}
}
#endif

/*
 * __bit_nset --
 *	Set bits start-to-stop in name.
 */
static inline void
__bit_nset(uint8_t *bitf, uint32_t start, uint32_t stop)
{
	uint32_t startbyte, stopbyte;

	startbyte = __bit_byte(start);
	stopbyte = __bit_byte(stop);
	if (startbyte == stopbyte)
		bitf[startbyte] |=
		    ((0xff << (start & 0x7)) & (0xff >> (7 - (stop & 0x7))));
	else {
		bitf[startbyte] |= 0xff << (start & 0x7);
		while (++startbyte < stopbyte)
			bitf[startbyte] = 0xff;
		bitf[stopbyte] |= 0xff >> (7 - (stop & 0x7));
	}
}

/*
 * __bit_ffc --
 *	Find first clear bit in name, return 0 on success, -1 on no bit clear.
 */
static inline int
__bit_ffc(uint8_t *bitf, uint32_t nbits, uint32_t *retp)
{
	uint8_t lb;
	uint32_t byte, stopbyte, value;

	value = 0;              /* -Wuninitialized */

	if (nbits == 0)
		return (-1);

	for (byte = 0,
	    stopbyte = __bit_byte(nbits - 1); byte <= stopbyte; ++byte)
		if (bitf[byte] != 0xff) {
			value = byte << 3;
			for (lb = bitf[byte]; lb & 0x01; ++value, lb >>= 1)
				;
			break;
		}

	if (byte > stopbyte || value >= nbits)
		return (-1);

	*retp = value;
	return (0);
}

#ifdef __NOT_CURRENTLY_USED
/*
 * __bit_ffs --
 *	Find first set bit in name, return 0 on success, -1 on no bit set.
 */
static inline int
__bit_ffs(uint8_t *bitf, uint32_t nbits, uint32_t *retp)
{
	uint8_t lb;
	uint32_t byte, stopbyte, value;

	if (nbits == 0)
		return (-1);

	for (byte = 0,
	    stopbyte = __bit_byte(nbits - 1); byte <= stopbyte; ++byte)
		if (bitf[byte] != 0) {
			value = byte << 3;
			for (lb = bitf[byte]; !(lb & 0x01); ++value, lb >>= 1)
				;
			break;
		}

	if (byte > stopbyte || value >= nbits)
		return (-1);

	*retp = value;
	return (0);
}
#endif

/*
 * __bit_getv --
 *	Return a fixed-length column store bit-field value.
 */
static inline uint8_t
__bit_getv(uint8_t *bitf, uint32_t entry, uint8_t width)
{
	uint8_t value;
	uint32_t bit;

#define	__BIT_GET(len, mask)						\
	case len:							\
		if (__bit_test(bitf, bit))				\
			value |= mask;					\
		++bit							\
		/* FALLTHROUGH */

	value = 0;
	bit = entry * width;

	/*
	 * Fast-path single bytes, do repeated tests for the rest: we could
	 * slice-and-dice instead, but the compiler is probably going to do
	 * a better job than I will.
	 */
	switch (width) {
	case 8:
		return (bitf[__bit_byte(bit)]);
	__BIT_GET(7, 0x40);
	__BIT_GET(6, 0x20);
	__BIT_GET(5, 0x10);
	__BIT_GET(4, 0x08);
	__BIT_GET(3, 0x04);
	__BIT_GET(2, 0x02);
	__BIT_GET(1, 0x01);
	}
	return (value);
}

/*
 * __bit_getv_recno --
 *	Return a record number's bit-field value.
 */
static inline uint8_t
__bit_getv_recno(WT_PAGE *page, uint64_t recno, uint8_t width)
{
	return (__bit_getv(page->u.col_fix.bitf,
	    (uint32_t)(recno - page->u.col_fix.recno), width));
}

/*
 * __bit_setv --
 *	Set a fixed-length column store bit-field value.
 */
static inline void
__bit_setv(uint8_t *bitf, uint32_t entry, uint8_t width, uint8_t value)
{
	uint32_t bit;

#define	__BIT_SET(len, mask)						\
	case len:							\
		if (value & (mask))					\
			__bit_set(bitf, bit);				\
		else							\
			__bit_clear(bitf, bit);				\
		++bit							\
		/* FALLTHROUGH */

	bit = entry * width;

	/*
	 * Fast-path single bytes, do repeated tests for the rest: we could
	 * slice-and-dice instead, but the compiler is probably going to do
	 * a better job than I will.
	 */
	switch (width) {
	case 8:
		bitf[__bit_byte(bit)] = value;
		return;
	__BIT_SET(7, 0x40);
	__BIT_SET(6, 0x20);
	__BIT_SET(5, 0x10);
	__BIT_SET(4, 0x08);
	__BIT_SET(3, 0x04);
	__BIT_SET(2, 0x02);
	__BIT_SET(1, 0x01);
	}
}

/*
 * __bit_setv_recno --
 *	Set a record number's bit-field value.
 */
static inline void
__bit_setv_recno(WT_PAGE *page, uint64_t recno, uint8_t width, uint8_t value)
{
	return (__bit_setv(page->u.col_fix.bitf,
	    (uint32_t)(recno - page->u.col_fix.recno), width, value));
}