cbits 0.1.1
High-performance BitVector C-API & Python binding
Loading...
Searching...
No Matches
compat.h
Go to the documentation of this file.
1
14#ifndef CBITS_COMPAT_H
15#define CBITS_COMPAT_H
16
17#include <stdint.h>
18#include <stddef.h>
19#include <stdlib.h>
20
21#if defined(__x86_64__) || defined(_M_X64)
22 #if defined(_MSC_VER)
23 #include <immintrin.h>
24 #else
25 #include <x86intrin.h>
26 #endif
27#endif
28
29#ifdef _MSC_VER
30 #ifdef _M_IX86
31 #pragma intrinsic(__popcnt)
32 #elif defined(_M_X64) || defined(_M_AMD64)
33 #pragma intrinsic(__popcnt)
34 #pragma intrinsic(__popcnt64)
35 #endif
36#endif
37
38/* Aligned malloc / free */
39#if defined(_MSC_VER)
40 #include <malloc.h>
41 #include <errno.h>
42
43static inline int
44posix_memalign(void **memptr, size_t alignment, size_t size)
45{
46 void *p = _aligned_malloc(size, alignment);
47 if (!p) {
48 return ENOMEM;
49 }
50 *memptr = p;
51 return 0;
52}
53
61static inline void
62cbits_free_aligned(void *ptr)
63{
64 _aligned_free(ptr);
65}
66#else
67
75static inline void
77{
78 free(ptr);
79}
80#endif
81
91static inline void *
92cbits_malloc_aligned(size_t size, size_t align)
93{
94 void *p = NULL;
95 if (posix_memalign(&p, align, size) != 0) {
96 return NULL;
97 }
98 return p;
99}
100
101/* Prefetch */
102
108static inline void
109cbits_prefetch(const void *ptr)
110{
111#if defined(_MSC_VER)
112 _mm_prefetch((const char *) ptr, _MM_HINT_T0);
113#else
114 __builtin_prefetch(ptr, 0, 1);
115#endif
116}
117
118/* Popcount */
119
126static inline uint64_t
128{
129#if defined(_MSC_VER)
130 #if defined(_M_IX86) || defined(_M_ARM)
131 return (uint64_t) __popcnt((uint32_t) x) +
132 (uint64_t) __popcnt((uint32_t) (x >> 32));
133 #else
134 return (uint64_t) __popcnt64(x);
135 #endif
136#else
137 return (uint64_t) __builtin_popcountll(x);
138#endif
139}
140
148extern uint64_t (*cbits_popcount_block_ptr)(const uint64_t *ptr);
149
159uint64_t
160cbits_popcount_block_fallback(const uint64_t *ptr);
161
162#if defined(__x86_64__) || defined(_M_X64)
174uint64_t
175cbits_popcount_block_avx2(const uint64_t *ptr);
176
188uint64_t
189cbits_popcount_block_avx512(const uint64_t *ptr);
190#endif
191
198static inline uint64_t
199cbits_popcount_block(const uint64_t *ptr)
200{
201 return cbits_popcount_block_ptr(ptr);
202}
212void
214
215#endif /* CBITS_COMPAT_H */
static uint64_t cbits_popcount64(uint64_t x)
Count bits set in a 64-bit word.
Definition compat.h:127
uint64_t cbits_popcount_block_fallback(const uint64_t *ptr)
Fallback popcount block implementation.
Definition compat_dispatch.c:16
void init_cpu_dispatch(void)
Constructor to initialize popcount dispatch pointer.
Definition compat_dispatch.c:96
static void * cbits_malloc_aligned(size_t size, size_t align)
Allocate aligned memory.
Definition compat.h:92
static void cbits_prefetch(const void *ptr)
Prefetch a cache line at ptr into L1.
Definition compat.h:109
uint64_t(* cbits_popcount_block_ptr)(const uint64_t *ptr)
Dispatch pointer for block popcount.
Definition compat_dispatch.c:25
static uint64_t cbits_popcount_block(const uint64_t *ptr)
Inline wrapper that calls the current dispatch pointer.
Definition compat.h:199
static void cbits_free_aligned(void *ptr)
Free aligned memory.
Definition compat.h:76