Skip to content

Commit

Permalink
Improving portability and add missing externs.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Aug 9, 2016
1 parent e972914 commit fa0aec7
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 40 deletions.
2 changes: 1 addition & 1 deletion include/roaring/array_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/*
* good old binary search
*/
static inline int32_t binarySearch(const uint16_t *array, int32_t lenarray,
inline int32_t binarySearch(const uint16_t *array, int32_t lenarray,
uint16_t ikey) {
int32_t low = 0;
int32_t high = lenarray - 1;
Expand Down
4 changes: 2 additions & 2 deletions include/roaring/bitset_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ inline static uint64_t avx2_harley_seal_popcount256(const __m256i *data,
(uint64_t)(_mm256_extract_epi64(total, 3)); \
} \
static inline uint64_t avx2_harley_seal_popcount256andstore_##opname( \
const __m256i *restrict data1, const __m256i *restrict data2, \
__m256i *restrict out, const uint64_t size) { \
const __m256i *__restrict__ data1, const __m256i *__restrict__ data2, \
__m256i *__restrict__ out, const uint64_t size) { \
__m256i total = _mm256_setzero_si256(); \
__m256i ones = _mm256_setzero_si256(); \
__m256i twos = _mm256_setzero_si256(); \
Expand Down
2 changes: 1 addition & 1 deletion include/roaring/containers/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static inline bool array_container_remove(array_container_t *arr,
}

/* Check whether x is present. */
static inline bool array_container_contains(const array_container_t *arr,
inline bool array_container_contains(const array_container_t *arr,
uint16_t pos) {
return binarySearch(arr->array, arr->cardinality, pos) >= 0;
}
Expand Down
2 changes: 1 addition & 1 deletion include/roaring/containers/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static inline bool bitset_container_remove(bitset_container_t *bitset,
}

/* Get the value of the ith bit. */
static inline bool bitset_container_get(const bitset_container_t *bitset,
inline bool bitset_container_get(const bitset_container_t *bitset,
uint16_t pos) {
uint64_t word = bitset->array[pos >> 6];
const uint64_t p = pos;
Expand Down
24 changes: 3 additions & 21 deletions include/roaring/containers/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void *shared_container_extract_copy(shared_container_t *container,
uint8_t *typecode);

/* access to container underneath */
static const void *container_unwrap_shared(
inline const void *container_unwrap_shared(
const void *candidate_shared_container, uint8_t *type) {
if (*type == SHARED_CONTAINER_TYPE_CODE) {
*type =
Expand Down Expand Up @@ -357,25 +357,7 @@ static inline bool container_nonzero_cardinality(const void *container,
/**
* Recover memory from a container, requires a typecode
*/
static inline void container_free(void *container, uint8_t typecode) {
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
bitset_container_free((bitset_container_t *)container);
break;
case ARRAY_CONTAINER_TYPE_CODE:
array_container_free((array_container_t *)container);
break;
case RUN_CONTAINER_TYPE_CODE:
run_container_free((run_container_t *)container);
break;
case SHARED_CONTAINER_TYPE_CODE:
shared_container_free((shared_container_t *)container);
break;
default:
assert(false);
__builtin_unreachable();
}
}
void container_free(void *container, uint8_t typecode);

/**
* Convert a container to an array of values, requires a typecode as well as a
Expand Down Expand Up @@ -480,7 +462,7 @@ static inline void *container_remove(void *container, uint16_t val,
/**
* Check whether a value is in a container, requires a typecode
*/
static inline bool container_contains(const void *container, uint16_t val,
inline bool container_contains(const void *container, uint16_t val,
uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
Expand Down
4 changes: 2 additions & 2 deletions include/roaring/containers/run.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void recoverRoomAtIndex(run_container_t *run, uint16_t index) {
/**
* Good old binary search through rle data
*/
static inline int32_t interleavedBinarySearch(const rle16_t *array,
inline int32_t interleavedBinarySearch(const rle16_t *array,
int32_t lenarray, uint16_t ikey) {
int32_t low = 0;
int32_t high = lenarray - 1;
Expand Down Expand Up @@ -157,7 +157,7 @@ static inline bool run_container_remove(run_container_t *run, uint16_t pos) {
}

/* Check whether `pos' is present in `run'. */
static inline bool run_container_contains(const run_container_t *run,
inline bool run_container_contains(const run_container_t *run,
uint16_t pos) {
int32_t index = interleavedBinarySearch(run->runs, run->n_runs, pos);
if (index >= 0) return true;
Expand Down
4 changes: 4 additions & 0 deletions include/roaring/portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#error This code assumes 64-bit long longs (by use of the GCC intrinsics). Your system is not currently supported.
#endif

#if defined(_MSC_VER)
#define __restrict__ __restrict
#endif

// unless DISABLEAVX was defined, if we have AVX2 and BMI2, we enable AVX
#if (!defined(USEAVX)) && (!defined(DISABLEAVX)) && (defined(__AVX2__)) && \
(defined(__BMI2__))
Expand Down
2 changes: 1 addition & 1 deletion include/roaring/roaring.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t x);
/**
* Check if value x is present
*/
static inline bool roaring_bitmap_contains(const roaring_bitmap_t *r,
inline bool roaring_bitmap_contains(const roaring_bitmap_t *r,
uint32_t val) {
const uint16_t hb = val >> 16;
/*
Expand Down
6 changes: 3 additions & 3 deletions include/roaring/roaring_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void ra_free_without_containers(roaring_array_t *r);
/**
* Get the index corresponding to a 16-bit key
*/
static inline int32_t ra_get_index(roaring_array_t *ra, uint16_t x) {
inline int32_t ra_get_index(roaring_array_t *ra, uint16_t x) {
if ((ra->size == 0) || ra->keys[ra->size - 1] == x) return ra->size - 1;

return binarySearch(ra->keys, (int32_t)ra->size, x);
Expand All @@ -74,7 +74,7 @@ static inline int32_t ra_get_index(roaring_array_t *ra, uint16_t x) {
/**
* Retrieves the container at index i, filling in the typecode
*/
static inline void *ra_get_container_at_index(roaring_array_t *ra, uint16_t i,
inline void *ra_get_container_at_index(roaring_array_t *ra, uint16_t i,
uint8_t *typecode) {
*typecode = ra->typecodes[i];
return ra->containers[i];
Expand Down Expand Up @@ -154,7 +154,7 @@ void ra_set_container_at_index(roaring_array_t *ra, int32_t i, void *c,
*/
void extend_array(roaring_array_t *ra, uint32_t k);

static inline int32_t ra_get_size(roaring_array_t *ra) { return ra->size; }
inline int32_t ra_get_size(roaring_array_t *ra) { return ra->size; }

static inline int32_t ra_advance_until(roaring_array_t *ra, uint16_t x,
int32_t pos) {
Expand Down
10 changes: 6 additions & 4 deletions src/array_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <roaring/array_util.h>
#include <roaring/portability.h>
#include <roaring/utilasm.h>
extern inline int32_t binarySearch(const uint16_t *array, int32_t lenarray,
uint16_t ikey);

#ifdef IS_X64

Expand Down Expand Up @@ -996,14 +998,14 @@ static uint8_t uniqshuf[] = {

// write vector new, while omitting repeated values assuming that previously
// written vector was "old"
static inline int store_unique(__m128i old, __m128i new, uint16_t *output) {
__m128i vecTmp = _mm_alignr_epi8(new, old, 16 - 2);
static inline int store_unique(__m128i old, __m128i newval, uint16_t *output) {
__m128i vecTmp = _mm_alignr_epi8(newval, old, 16 - 2);
// lots of high latency instructions follow (optimize?)
int M = _mm_movemask_epi8(_mm_cmpeq_epi16(vecTmp, new));
int M = _mm_movemask_epi8(_mm_cmpeq_epi16(vecTmp, newval));
M = _pext_u32(M, 0x5555);
int numberofnewvalues = 8 - _mm_popcnt_u32(M);
__m128i key = _mm_lddqu_si128((const __m128i *)uniqshuf + M);
__m128i val = _mm_shuffle_epi8(new, key);
__m128i val = _mm_shuffle_epi8(newval, key);
_mm_storeu_si128((__m128i *)output, val);
return numberofnewvalues;
}
Expand Down
2 changes: 2 additions & 0 deletions src/containers/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdlib.h>
#include <roaring/containers/array.h>

extern inline bool array_container_contains(const array_container_t *arr,
uint16_t pos);
extern int array_container_cardinality(const array_container_t *array);
extern bool array_container_nonzero_cardinality(const array_container_t *array);
extern void array_container_clear(array_container_t *array);
Expand Down
2 changes: 1 addition & 1 deletion src/containers/bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern int bitset_container_cardinality(const bitset_container_t *bitset);
extern bool bitset_container_nonzero_cardinality(bitset_container_t *bitset);
extern void bitset_container_set(bitset_container_t *bitset, uint16_t pos);
extern void bitset_container_unset(bitset_container_t *bitset, uint16_t pos);
extern bool bitset_container_get(const bitset_container_t *bitset,
extern inline bool bitset_container_get(const bitset_container_t *bitset,
uint16_t pos);
extern int32_t bitset_container_serialized_size_in_bytes();
extern bool bitset_container_add(bitset_container_t *bitset, uint16_t pos);
Expand Down
25 changes: 24 additions & 1 deletion src/containers/containers.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

#include <roaring/containers/containers.h>

extern inline const void *container_unwrap_shared(
const void *candidate_shared_container, uint8_t *type);

extern const char *get_container_name(uint8_t typecode);

extern int container_get_cardinality(const void *container, uint8_t typecode);
Expand All @@ -17,6 +20,26 @@ extern void *container_ixor(void *c1, uint8_t type1, const void *c2,
extern void *container_iandnot(void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type);

void container_free(void *container, uint8_t typecode) {
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
bitset_container_free((bitset_container_t *)container);
break;
case ARRAY_CONTAINER_TYPE_CODE:
array_container_free((array_container_t *)container);
break;
case RUN_CONTAINER_TYPE_CODE:
run_container_free((run_container_t *)container);
break;
case SHARED_CONTAINER_TYPE_CODE:
shared_container_free((shared_container_t *)container);
break;
default:
assert(false);
__builtin_unreachable();
}
}

void container_printf(const void *container, uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
Expand Down Expand Up @@ -124,7 +147,7 @@ extern int container_to_uint32_array(uint32_t *output, const void *container,
extern void *container_add(void *container, uint16_t val, uint8_t typecode,
uint8_t *new_typecode);

extern bool container_contains(const void *container, uint16_t val,
extern inline bool container_contains(const void *container, uint16_t val,
uint8_t typecode);

extern void *container_clone(const void *container, uint8_t typecode);
Expand Down
4 changes: 4 additions & 0 deletions src/containers/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <x86intrin.h>
#endif

extern inline int32_t interleavedBinarySearch(const rle16_t *array,
int32_t lenarray, uint16_t ikey);
extern inline bool run_container_contains(const run_container_t *run,
uint16_t pos);
extern bool run_container_is_full(const run_container_t *run);
extern bool run_container_nonzero_cardinality(const run_container_t *r);
extern void run_container_clear(run_container_t *run);
Expand Down
2 changes: 2 additions & 0 deletions src/roaring.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <roaring/array_util.h>
#include <roaring/roaring_array.h>

extern inline bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val);

roaring_bitmap_t *roaring_bitmap_create() {
roaring_bitmap_t *ans =
(roaring_bitmap_t *)malloc(sizeof(roaring_bitmap_t));
Expand Down
6 changes: 4 additions & 2 deletions src/roaring_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
// Convention: [0,ra->size) all elements are initialized
// [ra->size, ra->allocation_size) is junk and contains nothing needing freeing

extern int32_t ra_get_size(roaring_array_t *ra);

extern inline int32_t ra_get_size(roaring_array_t *ra);
extern inline int32_t ra_get_index(roaring_array_t *ra, uint16_t x);
extern inline void *ra_get_container_at_index(roaring_array_t *ra,
uint16_t i, uint8_t *typecode);
#define INITIAL_CAPACITY 4

roaring_array_t *ra_create_with_capacity(uint32_t cap) {
Expand Down

0 comments on commit fa0aec7

Please sign in to comment.