Skip to content

Commit

Permalink
Move big endian conversion into portability.h
Browse files Browse the repository at this point in the history
Also define a unique macro name to avoid conflicts with endian.h
  • Loading branch information
SLieve committed Jan 9, 2024
1 parent 2e4bda4 commit 9058881
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
Binary file added amalgamation_demo
Binary file not shown.
31 changes: 31 additions & 0 deletions include/roaring/portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,37 @@ static inline int roaring_hamming(uint64_t x) {
#endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#endif

// Host <-> big endian conversion.
#if CROARING_IS_BIG_ENDIAN
#define croaring_htobe64(x) (x)

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

#elif defined(__APPLE__) // CROARING_IS_BIG_ENDIAN
#include <libkern/OSByteOrder.h>
#define croaring_htobe64(x) OSSwapInt64(x)

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

#else // CROARING_IS_BIG_ENDIAN
// Gets compiled to bswap or equivalent on most compilers.
#define croaring_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 croaring_be64toh(x) croaring_htobe64(x)
// End of host <-> big endian conversion.

// Defines for the possible CROARING atomic implementations
#define CROARING_ATOMIC_IMPL_NONE 1
#define CROARING_ATOMIC_IMPL_CPP 2
Expand Down
35 changes: 3 additions & 32 deletions src/roaring64.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,11 @@
#include <roaring/art/art.h>
#include <roaring/containers/containers.h>
#include <roaring/roaring64.h>
#include <roaring/portability.h>
#include <stdarg.h>
#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
#include <libkern/OSByteOrder.h>
#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 Down Expand Up @@ -71,7 +42,7 @@ typedef struct roaring64_leaf_s leaf_t;
// 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 = htobe64(key);
uint64_t tmp = croaring_htobe64(key);
memcpy(high48_out, (uint8_t *)(&tmp), ART_KEY_BYTES);
return (uint16_t)key;
}
Expand All @@ -81,7 +52,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 betoh64(result) | low16;
return croaring_be64toh(result) | low16;
}

static inline uint64_t minimum(uint64_t a, uint64_t b) {
Expand Down

0 comments on commit 9058881

Please sign in to comment.