forked from grundprinzip/bitcompressedvector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcv.h
More file actions
701 lines (545 loc) · 18.4 KB
/
bcv.h
File metadata and controls
701 lines (545 loc) · 18.4 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#ifndef BCV_BCV_H
#define BCV_BCV_H
#include <algorithm>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
// SSE requirements
#include <emmintrin.h>
#include <tmmintrin.h>
#include <smmintrin.h>
#define CACHE_LINE_SIZE 64
#ifndef NDEBUG
#define DEBUG(msg) std::cout << msg << std::endl;
#define DEBUG_M128(m) std::cout << (uint64_t) _mm_extract_epi64(m, 0) << " " << (uint64_t) _mm_extract_epi64(m, 1) << std::endl;
#else
#define DEBUG(msg)
#endif
#include <assert.h>
#include "decompress.h"
#include "decompress2.h"
/*
This class provides a numeric bit compressed vector.
Basically it provides a drop-in replacement for the standard std::vector. However,
the number of bits allocated per value cannot be changed afterwards.
*/
template<typename T, uint64_t B>
class BitCompressedVector
{
public:
typedef T value_type;
typedef T& value_type_ref;
typedef T* value_type_ptr;
typedef BitCompressedVector<T,B> vector_type;
/*
* Constructor
*/
explicit BitCompressedVector(size_t size=0): _reserved(0), _size(0), _data(0), _allocated_blocks(0)
{
reserve(size);
// If the size is set to a value large 0 we assume
// a repeated initialization
if (size > 0)
_size = size;
}
/* copy constructor */
BitCompressedVector(const BitCompressedVector& other): _reserved(other._reserved),
_size(other._size), _allocated_blocks(other._allocated_blocks), _data(nullptr)
{
_data = _allocate(_allocated_blocks);
memcpy(_data, other._data, sizeof(data_t) * _allocated_blocks);
}
/* assignemnt operator */
vector_type& operator=(BitCompressedVector other)
{
std::swap(_reserved, other._reserved);
std::swap(_size, other._size);
std::swap(_allocated_blocks, other._allocated_blocks);
std::swap(_data, other._data);
return *this;
}
/* Destructor */
~BitCompressedVector()
{
free(_data);
}
/*
* Returns the used size of the vector
*/
inline uint64_t size() const
{
return _size;
}
/*
* Returns the reserved size of the vector
*/
inline uint64_t capacity() const
{
return _reserved;
}
/*
* Original get method based on the index
*/
inline value_type get(const size_t index) const;
/*
This method returns a list of extracted values from the vector.The number
of elements is variadic end depends on the number of elements inside a
single block.
Typicallay we try to extract at least a single cache line
This operation is unchecked and will not throw an out of range exception
in case the access is illegal
*/
inline void mget(const size_t index, value_type_ptr data, size_t *actual) const;
/*
* Set method to set a value
*
* This method will throw an out of range exception in case the access is illegal
*/
inline void set(const size_t index, const value_type v);
/*
* Append a new value to the vector
*/
inline void push_back(const value_type v)
{
if (_size + 1 > _reserved)
reserve((_size + 1) * 2);
// Increase the size
set(_size++, v);
}
/*
* Perform a resize of the vector.
*
* Currently, the size can only be increased not decreased.
*/
inline void reserve(size_t size)
{
if (_blocks(size) > _allocated_blocks)
{
data_t *newMemory = _allocate(_blocks(size));
// Copy the data from the old data partition to the new partition
memcpy(newMemory, _data, sizeof(data_t) * _allocated_blocks);
// Allocate memory
_allocated_blocks = _blocks(size);
_reserved = _allocated_blocks * _width / B;
// Swap pointers
std::swap(_data,newMemory);
free(newMemory);
}
}
/*
This small class is a simple proxy class that let's us handle reference
values to indizes in the bitvector without actually having a direct reference
*/
struct BitVectorProxy
{
size_t _index;
BitCompressedVector<T, B> *_vector;
BitVectorProxy(size_t idx, BitCompressedVector<T, B> *v): _index(idx), _vector(v)
{}
// Implicit conversion operator used for rvalues of T
inline operator const T () const
{
return _vector->get(_index);
}
// Usins the Proxy to set the value using the subscript as an lvalue
inline BitVectorProxy& operator= (const T& rvalue)
{
_vector->set(_index, rvalue);
return *this;
}
};
/*
* Shortcut method for get(size_t index)
*/
inline const BitVectorProxy operator[] (const size_t index) const
{
return BitVectorProxy(index, this);
}
inline BitVectorProxy operator[] (const size_t index)
{
return BitVectorProxy(index, this);
}
private:
// data type
typedef uint64_t data_t;
// Width determines the number of bits used to encode the block data type
static const uint8_t _width = sizeof(data_t) * 8;
// Pointer to the data, aligned
data_t *_data __attribute__((aligned(16))) ;
size_t _reserved;
size_t _size;
size_t _allocated_blocks;
// get the position of an index inside the list of data values
inline size_t _getPos(size_t index, size_t width=_width) const
{
return (index * B) / width;
}
// get the offset of an index inside a block
inline size_t _getOffset(size_t index, size_t base) const
{
return (index * B) - base;
}
// returns the offset mask for any given index
inline data_t buildMask(size_t index) const
{
return (index * B) % _width;
}
// returns the number of blocks required for the number of
// elements to store
inline size_t _blocks(size_t elements) const
{
// Simple ceil implementation
return ((elements * B) + _width - 1 )/ _width;
}
/*
* Allocate a number of blocks and zero out the memory
*/
inline data_t* _allocate(size_t blocks) const
{
data_t *newMemory = nullptr;
posix_memalign((void**) &newMemory, 16, blocks * sizeof(data_t));
memset(newMemory, 0, blocks * sizeof(data_t));
return newMemory;
}
public:
data_t* getData(){ return _data; }
};
/**
*/
template<typename T, uint64_t B>
void BitCompressedVector<T, B>::set(const size_t index, const value_type v)
{
if (index >= _size)
throw std::out_of_range("Access not permitted");
// Allocate new memory if required
uint64_t pos = _getPos(index);
uint64_t offset = _getOffset(index, pos * _width);
uint64_t bounds = _width - offset;
uint64_t mask, baseMask;
baseMask = (1ull << B) - 1ull;
mask = ~(baseMask << offset);
_data[pos] &= mask;
_data[pos] = _data[pos] | ((uint64_t) v << offset);
if (bounds < B)
{
mask = ~(baseMask << offset); // we have a an overflow here thatswhy we do not need to care about the original stuff
_data[pos + 1] &= mask; // clear bits
_data[pos + 1] |= v >> bounds; // set bits and shift by the number of bits we already inserted
}
}
/**
*/
template<typename T, uint64_t B>
typename BitCompressedVector<T, B>::value_type BitCompressedVector<T, B>::get(const size_t index) const
{
value_type result;
register uint64_t mask;
register uint64_t pos = _getPos(index);
register uint64_t offset = _getOffset(index, pos * _width);
register uint64_t bounds = _width - offset; // This is almost static expression, that could be handled with a switch case
mask = (1ull << B) - 1;
register data_t block = _data[pos];
block >>= offset;
result = (mask & block);
if (bounds < B)
{
offset = B - bounds;
mask = (1ull << offset) - 1;
result |= (mask & _data[pos + 1]) << bounds;
}
return result;
}
/**
*/
template<typename T, uint64_t B>
void BitCompressedVector<T, B>::mget(const size_t index, value_type_ptr data, size_t *actual) const
{
//assert(128 % index == 0);
BitCompression<B>::decompress_large(((const __m128i*) _data) + _getPos(index, 128), data, actual);
}
/**************************************************************************************************************/
template<typename T, uint64_t B>
class BitCompressedVectorVertical
{
public:
typedef T value_type;
typedef T& value_type_ref;
typedef T* value_type_ptr;
typedef BitCompressedVectorVertical<T,B> vector_type;
/*
* Constructor
*/
BitCompressedVectorVertical(size_t size=0): _reserved(size), _size(0), _allocated_blocks(0), _data(nullptr)
{
reserve(size);
// If the size is set to a value large 0 we assume
// a repeated initialization
if (size > 0)
_size = size;
}
/* copy constructor */
BitCompressedVectorVertical(const BitCompressedVectorVertical& other):
_reserved(other._reserved), _size(other._size), _allocated_blocks(other._allocated_blocks),
_data(nullptr)
{
_data = _allocate(_allocated_blocks);
memcpy(_data, other._data, sizeof(data_t) * _allocated_blocks);
}
/* assignemnt operator */
vector_type& operator=(BitCompressedVectorVertical other)
{
std::swap(_reserved, other._reserved);
std::swap(_size, other._size);
std::swap(_allocated_blocks, other._allocated_blocks);
std::swap(_data, other._data);
return *this;
}
~BitCompressedVectorVertical()
{
free(_data);
}
/*
* Original get method based on the index
*/
inline value_type get(const size_t index) const;
/*
* Returns the used size of the vector
*/
inline uint64_t size() const
{
return _size;
}
/*
* Returns the reserved size of the vector
*/
inline uint64_t capacity() const
{
return _reserved;
}
/*
* Append a new value to the vector
*/
inline void push_back(const value_type v)
{
if (_size + 1 > _reserved)
reserve((_size + 1) * 2);
// Increase the size
set(_size++, v);
}
/*
* Perform a resize of the vector.
*
* Currently, the size can only be increased not decreased.
*/
inline void reserve(size_t size)
{
if (_blocks(size) > _allocated_blocks)
{
data_t *newMemory = _allocate(_blocks(size));
// Copy the data from the old data partition to the new partition
memcpy(newMemory, _data, sizeof(data_t) * _allocated_blocks);
// Allocate memory
_allocated_blocks = _blocks(size);
_reserved = _allocated_blocks * _width / B;
// Swap pointers
_data = newMemory;
}
}
/*
This method returns a list of extracted values from the vector.The number
of elements is variadic end depends on the number of elements inside a
single block.
Typicallay we try to extract at least a single cache line
*/
inline void mget(const size_t index, value_type_ptr __restrict__ data, size_t* __restrict__ actual) const;
/*
* Set method to set a value
*/
inline void set(const size_t index, const value_type v);
inline void cmp_eq_bv(const size_t index, const value_type v, value_type_ptr __restrict__ data, size_t* __restrict__ actual) const;
/*
This small class is a simple proxy class that let's us handle reference
values to indizes in the bitvector without actually having a direct reference
*/
struct BitVectorProxy
{
size_t _index;
BitCompressedVectorVertical<T, B> *_vector;
BitVectorProxy(size_t idx, BitCompressedVectorVertical<T, B> *v): _index(idx), _vector(v)
{}
// Implicit conversion operator used for rvalues of T
inline operator const T () const
{
return _vector->get(_index);
}
// Usins the Proxy to set the value using the subscript as an lvalue
inline BitVectorProxy& operator= (const T& rvalue)
{
_vector->set(_index, rvalue);
return *this;
}
};
/*
* Shortcut method for get(size_t index)
*/
inline const BitVectorProxy operator[] (const size_t index) const
{
return BitVectorProxy(index, this);
}
inline BitVectorProxy operator[] (const size_t index)
{
return BitVectorProxy(index, this);
}
private:
// data type
typedef __m128i data_t;
// Width determines the number of bits used to encode the block data type
static const uint8_t _width = sizeof(data_t) * 8;
// How many elements are we storing in a 32bit small block
static const uint8_t _extracts = 4;
static const uint8_t _extract_bits = 32;
static const uint8_t _elements_per_small_block = (sizeof(uint32_t)*8) / B;
static const uint8_t _elements_per_large_block = _extracts * _elements_per_small_block;
// Pointer to the data, aligned
data_t *_data __attribute__((aligned(16))) ;
size_t _reserved;
size_t _size;
size_t _allocated_blocks;
// returns the number of blocks required for the number of
// elements to store
inline size_t _blocks(size_t elements) const
{
// Simple ceil implementation
return ((elements * B) + _width - 1 )/ _width;
}
/*
* Allocate a number of blocks and zero out the memory
*/
inline data_t* _allocate(size_t blocks) const
{
data_t *newMemory = nullptr;
posix_memalign((void**) &newMemory, 16, blocks * sizeof(data_t));
memset(newMemory, 0, blocks * sizeof(data_t));
return newMemory;
}
public:
data_t* getData(){ return _data; }
};
/**
*/
template<typename T, uint64_t B>
void BitCompressedVectorVertical<T, B>::set(const size_t index, const value_type v)
{
if (index >= _size)
throw std::out_of_range("Index out of range");
register size_t block = (index / _extracts) * B / _extract_bits;
register size_t in_block = index % _extracts;
register size_t offset_in_block = ((index / _extracts) * B) % _extract_bits;
register __m128i in = _data[block];
// Shuffle the mask
register __m128i mask = {static_cast<uint32_t>((static_cast<int32_t>(1) << B) - 1) << (offset_in_block), 0};
register __m128i shuffled_mask;
switch(in_block)
{
case 0: shuffled_mask = mask; break;
case 1: shuffled_mask = _mm_shuffle_epi32(mask, 243); break;
case 2: shuffled_mask = _mm_shuffle_epi32(mask, 975); break;
case 3: shuffled_mask = _mm_shuffle_epi32(mask, 3903); break;
}
// Shuffle the value
in = _mm_and_si128(in, ~shuffled_mask);
// Shuffle the value
__m128i out = {static_cast<uint32_t>(v) << (offset_in_block), 0};
switch(in_block)
{
case 0: break;
case 1: out = _mm_shuffle_epi32(out, 243); break;
case 2: out = _mm_shuffle_epi32(out, 975); break;
case 3: out = _mm_shuffle_epi32(out, 3903); break;
}
in = _mm_or_si128(in, out);
_mm_store_si128(_data + block, in);
if (_extract_bits - offset_in_block < B)
{
size_t diff = (_extract_bits - offset_in_block);
mask = __m128i{((1ll << B) - 1) >> diff, 0};
switch(in_block)
{
case 0: mask = mask; break;
case 1: mask = _mm_shuffle_epi32(mask, 243); break;
case 2: mask = _mm_shuffle_epi32(mask, 975); break;
case 3: mask = _mm_shuffle_epi32(mask, 3903); break;
}
in = _data[block + 1];
in = _mm_and_si128(in, ~mask);
out = __m128i{v >> diff, 0};
switch(in_block)
{
case 0: break;
case 1: out = _mm_shuffle_epi32(out, 243); break;
case 2: out = _mm_shuffle_epi32(out, 975); break;
case 3: out = _mm_shuffle_epi32(out, 3903); break;
}
_mm_store_si128(_data + block + 1, _mm_or_si128(in, out));
}
}
/**
*/
template<typename T, uint64_t B>
typename BitCompressedVectorVertical<T, B>::value_type BitCompressedVectorVertical<T, B>::get(const size_t index) const
{
// Shuffle masks
static const int _shuffle2 = 1;
static const int _shuffle3 = 2;
static const int _shuffle4 = 3;
register size_t block = (index / _extracts) * B / _extract_bits;
register size_t in_block = index % _extracts;
register size_t offset_in_block = ((index / _extracts) * B) % _extract_bits;
static int32_t mask = (1 << B) - 1;
uint32_t part;
switch(in_block)
{
case 0: part = _mm_cvtsi128_si32(_data[block]); break;
case 1: part = _mm_cvtsi128_si32(_mm_shuffle_epi32(_data[block], _shuffle2)); break;
case 2: part = _mm_cvtsi128_si32(_mm_shuffle_epi32(_data[block], _shuffle3)); break;
case 3: part = _mm_cvtsi128_si32(_mm_shuffle_epi32(_data[block], _shuffle4)); break;
}
part >>= offset_in_block;
part &= mask;
if (_extract_bits - offset_in_block < B)
{
// Get the block + 1
int32_t upper;
switch(in_block)
{
case 0: upper = _mm_cvtsi128_si32(_data[block+1]); break;
case 1: upper = _mm_cvtsi128_si32(_mm_shuffle_epi32(_data[block+1], _shuffle2)); break;
case 2: upper = _mm_cvtsi128_si32(_mm_shuffle_epi32(_data[block+1], _shuffle3)); break;
case 3: upper = _mm_cvtsi128_si32(_mm_shuffle_epi32(_data[block+1], _shuffle4)); break;
}
size_t diff = _extract_bits - offset_in_block;
upper = (upper << diff) & mask;
part |= upper;
}
return part;
}
/**
*/
template<typename T, uint64_t B>
void BitCompressedVectorVertical<T, B>::mget(const size_t index, value_type_ptr __restrict__ data, size_t* __restrict__ actual) const
{
register size_t block = (index / _extracts) * B / _extract_bits;
VerticalBitCompression<B>::decompress(_data + block, data, actual);
}
template<typename T, uint64_t B>
inline void BitCompressedVectorVertical<T,B>::cmp_eq_bv(const size_t index, const value_type cmp, value_type_ptr __restrict__ data, size_t* __restrict__ actual) const
{
register size_t block = (index / _extracts) * B / _extract_bits;
VerticalBitCompression<B>::cmp_eq(_data + block, cmp, data, actual);
}
#endif // BCV_BCV_H