Skip to content

Commit

Permalink
Attempt to fix test in win32
Browse files Browse the repository at this point in the history
  • Loading branch information
SLieve committed Jan 1, 2024
1 parent d09bafd commit 5f340b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
55 changes: 32 additions & 23 deletions src/roaring64.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@
#include <stdint.h>
#include <string.h>

#if CROARING_IS_BIG_ENDIAN
#define htobe64(x) (x)

#elif defined(_WIN32) || defined(_WIN64) // CROARING_IS_BIG_ENDIAN
#include <stdlib.h>
#define htobe64(x) _byteswap_uint64(x)

#elif defined(__APPLE__) // CROARING_IS_BIG_ENDIAN
#define htobe64(x) OSSwapInt64(x)

#elif defined(__has_include) && \
__has_include(<byteswap.h>) // CROARING_IS_BIG_ENDIAN
#include <byteswap.h>
#define htobe64(x) __bswap_64(x)

#else // CROARING_IS_BIG_ENDIAN
// Gets compiled to bswap or equivalent on most compilers.
#define htobe64(x) \
(((x & 0x00000000000000FFULL) << 56) | \
((x & 0x000000000000FF00ULL) << 40) | \
((x & 0x0000000000FF0000ULL) << 24) | \
((x & 0x00000000FF000000ULL) << 8) | \
((x & 0x000000FF00000000ULL) >> 8) | \
((x & 0x0000FF0000000000ULL) >> 24) | \
((x & 0x00FF000000000000ULL) >> 40) | \
((x & 0xFF00000000000000ULL) >> 56))
#endif // CROARING_IS_BIG_ENDIAN

#define betoh64(x) htobe64(x)

#ifdef __cplusplus
using namespace ::roaring::internal;

Expand All @@ -22,31 +52,10 @@ namespace api64 {
// TODO: Serialization.
// TODO: Error on failed allocation.

// Returns the uint64 represented in big endian format, so that the underlying
// bytes can be used in keys in the ART.
static inline uint64_t htobe(uint64_t x) {
#if CROARING_IS_BIG_ENDIAN
return x;
#else
// Gets compiled to bswap or equivalent on most compilers.
return ((x & 0x00000000000000FFULL) << 56) |
((x & 0x000000000000FF00ULL) << 40) |
((x & 0x0000000000FF0000ULL) << 24) |
((x & 0x00000000FF000000ULL) << 8) |
((x & 0x000000FF00000000ULL) >> 8) |
((x & 0x0000FF0000000000ULL) >> 24) |
((x & 0x00FF000000000000ULL) >> 40) |
((x & 0xFF00000000000000ULL) >> 56);
#endif // CROARING_IS_BIG_ENDIAN
}

// Returns the uint64 represented in host format.
static inline uint64_t betoh(uint64_t x) { return htobe(x); }

// Splits the given uint64 key into high 48 bit and low 16 bit components.
// Expects high48_out to be of length ART_KEY_BYTES.
static inline uint16_t split_key(uint64_t key, uint8_t high48_out[]) {
uint64_t tmp = htobe(key);
uint64_t tmp = htobe64(key);
memcpy(high48_out, (uint8_t *)(&tmp), ART_KEY_BYTES);
return (uint16_t)key;
}
Expand All @@ -56,7 +65,7 @@ static inline uint16_t split_key(uint64_t key, uint8_t high48_out[]) {
static inline uint64_t combine_key(const uint8_t high48[], uint16_t low16) {
uint64_t result = 0;
memcpy((uint8_t *)(&result), high48, ART_KEY_BYTES);
return betoh(result) | low16;
return betoh64(result) | low16;
}

static inline uint64_t minimum(uint64_t a, uint64_t b) {
Expand Down
2 changes: 1 addition & 1 deletion tests/roaring64_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ DEFINE_TEST(test_of_ptr) {
}

DEFINE_TEST(test_of) {
roaring64_bitmap_t* r = roaring64_bitmap_of(3, 1, 20000, 500000);
roaring64_bitmap_t* r = roaring64_bitmap_of(3, 1ULL, 20000ULL, 500000ULL);
assert_true(roaring64_bitmap_contains(r, 1));
assert_true(roaring64_bitmap_contains(r, 20000));
assert_true(roaring64_bitmap_contains(r, 500000));
Expand Down

0 comments on commit 5f340b5

Please sign in to comment.