1- /* auto-generated on Ven 17 mai 2019 14:49:35 EDT. Do not edit! */
1+ /* auto-generated on Fri Aug 30 19:43:10 EDT 2019 . Do not edit! */
22#include "roaring.h"
33
44/* used for http://dmalloc.com/ Dmalloc - Debug Malloc Library */
@@ -515,6 +515,10 @@ int32_t intersect_vector16_cardinality(const uint16_t *__restrict__ A,
515515 return (int32_t )count ;
516516}
517517
518+ /////////
519+ // Warning:
520+ // This function may not be safe if A == C or B == C.
521+ /////////
518522int32_t difference_vector16 (const uint16_t * __restrict__ A , size_t s_a ,
519523 const uint16_t * __restrict__ B , size_t s_b ,
520524 uint16_t * C ) {
@@ -638,7 +642,16 @@ int32_t difference_vector16(const uint16_t *__restrict__ A, size_t s_a,
638642 }
639643 }
640644 if (i_a < s_a ) {
641- memmove (C + count , A + i_a , sizeof (uint16_t ) * (s_a - i_a ));
645+ if (C == A ) {
646+ assert (count <= i_a );
647+ if (count < i_a ) {
648+ memmove (C + count , A + i_a , sizeof (uint16_t ) * (s_a - i_a ));
649+ }
650+ } else {
651+ for (size_t i = 0 ; i < (s_a - i_a ); i ++ ) {
652+ C [count + i ] = A [i + i_a ];
653+ }
654+ }
642655 count += (int32_t )(s_a - i_a );
643656 }
644657 return count ;
@@ -1553,6 +1566,7 @@ static int uint16_compare(const void *a, const void *b) {
15531566}
15541567
15551568// a one-pass SSE union algorithm
1569+ // This function may not be safe if array1 == output or array2 == output.
15561570uint32_t union_vector16 (const uint16_t * __restrict__ array1 , uint32_t length1 ,
15571571 const uint16_t * __restrict__ array2 , uint32_t length2 ,
15581572 uint16_t * __restrict__ output ) {
@@ -3063,9 +3077,15 @@ void array_container_andnot(const array_container_t *array_1,
30633077 if (out -> capacity < array_1 -> cardinality )
30643078 array_container_grow (out , array_1 -> cardinality , false);
30653079#ifdef ROARING_VECTOR_OPERATIONS_ENABLED
3066- out -> cardinality =
3067- difference_vector16 (array_1 -> array , array_1 -> cardinality ,
3080+ if ((out != array_1 ) && (out != array_2 )) {
3081+ out -> cardinality =
3082+ difference_vector16 (array_1 -> array , array_1 -> cardinality ,
30683083 array_2 -> array , array_2 -> cardinality , out -> array );
3084+ } else {
3085+ out -> cardinality =
3086+ difference_uint16 (array_1 -> array , array_1 -> cardinality , array_2 -> array ,
3087+ array_2 -> cardinality , out -> array );
3088+ }
30693089#else
30703090 out -> cardinality =
30713091 difference_uint16 (array_1 -> array , array_1 -> cardinality , array_2 -> array ,
@@ -3420,7 +3440,7 @@ bitset_container_t *bitset_container_create(void) {
34203440 return NULL ;
34213441 }
34223442 // sizeof(__m256i) == 32
3423- bitset -> array = (uint64_t * )aligned_malloc (
3443+ bitset -> array = (uint64_t * )roaring_bitmap_aligned_malloc (
34243444 32 , sizeof (uint64_t ) * BITSET_CONTAINER_SIZE_IN_WORDS );
34253445 if (!bitset -> array ) {
34263446 free (bitset );
@@ -3469,7 +3489,7 @@ void bitset_container_add_from_range(bitset_container_t *bitset, uint32_t min,
34693489/* Free memory. */
34703490void bitset_container_free (bitset_container_t * bitset ) {
34713491 if (bitset -> array != NULL ) {// Jon Strabala reports that some tools complain otherwise
3472- aligned_free (bitset -> array );
3492+ roaring_bitmap_aligned_free (bitset -> array );
34733493 bitset -> array = NULL ; // pedantic
34743494 }
34753495 free (bitset );
@@ -3484,7 +3504,7 @@ bitset_container_t *bitset_container_clone(const bitset_container_t *src) {
34843504 return NULL ;
34853505 }
34863506 // sizeof(__m256i) == 32
3487- bitset -> array = (uint64_t * )aligned_malloc (
3507+ bitset -> array = (uint64_t * )roaring_bitmap_aligned_malloc (
34883508 32 , sizeof (uint64_t ) * BITSET_CONTAINER_SIZE_IN_WORDS );
34893509 if (!bitset -> array ) {
34903510 free (bitset );
@@ -3918,7 +3938,7 @@ void* bitset_container_deserialize(const char *buf, size_t buf_len) {
39183938 if ((ptr = (bitset_container_t * )malloc (sizeof (bitset_container_t ))) != NULL ) {
39193939 memcpy (ptr , buf , sizeof (bitset_container_t ));
39203940 // sizeof(__m256i) == 32
3921- ptr -> array = (uint64_t * ) aligned_malloc (32 , l );
3941+ ptr -> array = (uint64_t * ) roaring_bitmap_aligned_malloc (32 , l );
39223942 if (! ptr -> array ) {
39233943 free (ptr );
39243944 return NULL ;
@@ -4060,19 +4080,16 @@ uint16_t bitset_container_maximum(const bitset_container_t *container) {
40604080
40614081/* Returns the number of values equal or smaller than x */
40624082int bitset_container_rank (const bitset_container_t * container , uint16_t x ) {
4063- uint32_t x32 = x ;
4083+ // credit: aqrit
40644084 int sum = 0 ;
4065- uint32_t k = 0 ;
4066- for (; k + 63 <= x32 ; k += 64 ) {
4067- sum += hamming (container -> array [k / 64 ]);
4085+ int i = 0 ;
4086+ for (int end = x / 64 ; i < end ; i ++ ) {
4087+ sum += hamming (container -> array [i ]);
40684088 }
4069- // at this point, we have covered everything up to k, k not included.
4070- // we have that k < x, but not so large that k+63<=x
4071- // k is a power of 64
4072- int bitsleft = x32 - k + 1 ;// will be in [0,64)
4073- uint64_t leftoverword = container -> array [k / 64 ];// k / 64 should be within scope
4074- leftoverword = leftoverword << ((64 - bitsleft ) & 63 ); // need the "&63" otherwise we shift by 64 which is undefined.
4075- sum += hamming (leftoverword );
4089+ uint64_t lastword = container -> array [i ];
4090+ uint64_t lastpos = UINT64_C (1 ) << (x % 64 );
4091+ uint64_t mask = lastpos + lastpos - 1 ; // smear right
4092+ sum += hamming (lastword & mask );
40764093 return sum ;
40774094}
40784095
@@ -6252,7 +6269,7 @@ bool array_array_container_inplace_union(array_container_t *src_1,
62526269 return false; // not a bitset
62536270 } else {
62546271 memmove (src_1 -> array + src_2 -> cardinality , src_1 -> array , src_1 -> cardinality * sizeof (uint16_t ));
6255- src_1 -> cardinality = (int32_t )fast_union_uint16 (src_1 -> array + src_2 -> cardinality , src_1 -> cardinality ,
6272+ src_1 -> cardinality = (int32_t )union_uint16 (src_1 -> array + src_2 -> cardinality , src_1 -> cardinality ,
62566273 src_2 -> array , src_2 -> cardinality , src_1 -> array );
62576274 return false; // not a bitset
62586275 }
@@ -6324,7 +6341,7 @@ bool array_array_container_lazy_inplace_union(array_container_t *src_1,
63246341 return false; // not a bitset
63256342 } else {
63266343 memmove (src_1 -> array + src_2 -> cardinality , src_1 -> array , src_1 -> cardinality * sizeof (uint16_t ));
6327- src_1 -> cardinality = (int32_t )fast_union_uint16 (src_1 -> array + src_2 -> cardinality , src_1 -> cardinality ,
6344+ src_1 -> cardinality = (int32_t )union_uint16 (src_1 -> array + src_2 -> cardinality , src_1 -> cardinality ,
63286345 src_2 -> array , src_2 -> cardinality , src_1 -> array );
63296346 return false; // not a bitset
63306347 }
@@ -8712,8 +8729,8 @@ uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *ra,
87128729 range_end -- ; // make range_end inclusive
87138730 // now we have: 0 <= range_start <= range_end <= UINT32_MAX
87148731
8715- int minhb = range_start >> 16 ;
8716- int maxhb = range_end >> 16 ;
8732+ uint16_t minhb = range_start >> 16 ;
8733+ uint16_t maxhb = range_end >> 16 ;
87178734
87188735 uint64_t card = 0 ;
87198736
0 commit comments