Coverage Report

Created: 2026-05-30 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/croaring/include/roaring/portability.h
Line
Count
Source
1
/*
2
 * portability.h
3
 *
4
 * This header centralizes compiler-, platform-, and architecture-specific
5
 * portability definitions used throughout CRoaring. It provides feature
6
 * detection, calling-convention and attribute macros, intrinsic and inline
7
 * assembly enablement, endianness helpers, alignment annotations, atomic
8
 * reference-count support, and other low-level compatibility glue.
9
 *
10
 * The goal is to keep these conditional definitions in one place so the rest
11
 * of the codebase can rely on a more uniform interface across GCC, Clang,
12
 * MSVC, x86/x64, ARM/NEON, and other supported environments.
13
 */
14
15
/**
16
 * All macros should be prefixed with either CROARING or ROARING.
17
 * The library uses both ROARING_...
18
 * as well as CROARING_ as prefixes. The ROARING_ prefix is for
19
 * macros that are provided by the build system or that are closely
20
 * related to the format. The header macros may also use ROARING_.
21
 * The CROARING_ prefix is for internal macros that a user is unlikely
22
 * to ever interact with.
23
 */
24
25
#ifndef CROARING_INCLUDE_PORTABILITY_H_
26
#define CROARING_INCLUDE_PORTABILITY_H_
27
28
// Users who need _GNU_SOURCE should define it?
29
// #ifndef _GNU_SOURCE
30
// #define _GNU_SOURCE 1
31
// #endif  // _GNU_SOURCE
32
#ifndef __STDC_FORMAT_MACROS
33
#define __STDC_FORMAT_MACROS 1
34
#endif  // __STDC_FORMAT_MACROS
35
36
#ifdef _MSC_VER
37
#define CROARING_VISUAL_STUDIO 1
38
/**
39
 * We want to differentiate carefully between
40
 * clang under visual studio and regular visual
41
 * studio.
42
 */
43
#ifdef __clang__
44
// clang under visual studio
45
#define CROARING_CLANG_VISUAL_STUDIO 1
46
#else
47
// just regular visual studio (best guess)
48
#define CROARING_REGULAR_VISUAL_STUDIO 1
49
#endif  // __clang__
50
#endif  // _MSC_VER
51
#ifndef CROARING_VISUAL_STUDIO
52
#define CROARING_VISUAL_STUDIO 0
53
#endif
54
#ifndef CROARING_CLANG_VISUAL_STUDIO
55
#define CROARING_CLANG_VISUAL_STUDIO 0
56
#endif
57
#ifndef CROARING_REGULAR_VISUAL_STUDIO
58
#define CROARING_REGULAR_VISUAL_STUDIO 0
59
#endif
60
61
#include <stdbool.h>
62
#include <stdint.h>
63
#include <stdlib.h>  // will provide posix_memalign with _POSIX_C_SOURCE as defined above
64
#ifdef __GLIBC__
65
#include <malloc.h>  // this should never be needed but there are some reports that it is needed.
66
#endif
67
// alignas/alignof are keywords in C++ and in C23+, where <stdalign.h> is
68
// deprecated. Only include it for C11..C17.
69
#if !defined(__cplusplus) && \
70
    (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 202311L)
71
#include <stdalign.h>
72
#endif
73
74
#ifdef __cplusplus
75
extern "C" {  // portability definitions are in global scope, not a namespace
76
#endif
77
78
#if defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ != 8
79
#error This code assumes  64-bit long longs (by use of the GCC intrinsics). Your system is not currently supported.
80
#endif
81
82
#if CROARING_REGULAR_VISUAL_STUDIO
83
#ifndef __restrict__
84
#define __restrict__ __restrict
85
#endif  // __restrict__
86
#endif  // CROARING_REGULAR_VISUAL_STUDIO
87
88
#if defined(__x86_64__) || defined(_M_X64)
89
// we have an x64 processor
90
#define CROARING_IS_X64 1
91
92
#if defined(_MSC_VER) && (_MSC_VER < 1910)
93
// Old visual studio systems won't support AVX2 well.
94
#undef CROARING_IS_X64
95
#endif
96
97
#if defined(__clang_major__) && (__clang_major__ <= 8) && !defined(__AVX2__)
98
// Older versions of clang have a bug affecting us
99
// https://stackoverflow.com/questions/57228537/how-does-one-use-pragma-clang-attribute-push-with-c-namespaces
100
#undef CROARING_IS_X64
101
#endif
102
103
#ifdef ROARING_DISABLE_X64
104
#undef CROARING_IS_X64
105
#endif
106
// we include the intrinsic header
107
#if !CROARING_REGULAR_VISUAL_STUDIO
108
/* Non-Microsoft C/C++-compatible compiler */
109
#include <x86intrin.h>  // on some recent GCC, this will declare posix_memalign
110
111
#if CROARING_CLANG_VISUAL_STUDIO
112
113
/**
114
 * You are not supposed, normally, to include these
115
 * headers directly. Instead you should either include intrin.h
116
 * or x86intrin.h. However, when compiling with clang
117
 * under Windows (i.e., when _MSC_VER is set), these headers
118
 * only get included *if* the corresponding features are detected
119
 * from macros:
120
 * e.g., if __AVX2__ is set... in turn,  we normally set these
121
 * macros by compiling against the corresponding architecture
122
 * (e.g., arch:AVX2, -mavx2, etc.) which compiles the whole
123
 * software with these advanced instructions. These headers would
124
 * normally guard against such usage, but we carefully included
125
 * <x86intrin.h>  (or <intrin.h>) before, so the headers
126
 * are fooled.
127
 */
128
// To avoid reordering imports:
129
// clang-format off
130
#include <bmiintrin.h>   // for _blsr_u64
131
#include <lzcntintrin.h> // for  __lzcnt64
132
#include <immintrin.h>   // for most things (AVX2, AVX512, _popcnt64)
133
#include <smmintrin.h>
134
#include <tmmintrin.h>
135
#include <avxintrin.h>
136
#include <avx2intrin.h>
137
#include <wmmintrin.h>
138
#if _MSC_VER >= 1920
139
// Important: we need the AVX-512 headers:
140
#include <avx512fintrin.h>
141
#include <avx512dqintrin.h>
142
#include <avx512cdintrin.h>
143
#include <avx512bwintrin.h>
144
#include <avx512vlintrin.h>
145
#include <avx512vbmiintrin.h>
146
#include <avx512vbmi2intrin.h>
147
#include <avx512vpopcntdqintrin.h>
148
// clang-format on
149
#endif  // _MSC_VER >= 1920
150
// unfortunately, we may not get _blsr_u64, but, thankfully, clang
151
// has it as a macro.
152
#ifndef _blsr_u64
153
// we roll our own
154
#define _blsr_u64(n) ((n - 1) & n)
155
#endif  //  _blsr_u64
156
#endif  // SIMDJSON_CLANG_VISUAL_STUDIO
157
158
#endif  // CROARING_REGULAR_VISUAL_STUDIO
159
#endif  // defined(__x86_64__) || defined(_M_X64)
160
161
#if !defined(CROARING_USENEON) && !defined(DISABLENEON) && defined(__ARM_NEON)
162
#define CROARING_USENEON
163
#endif
164
#if defined(CROARING_USENEON)
165
#include <arm_neon.h>
166
#endif
167
168
#if defined(__e2k__)
169
// we have an e2k (Elbrus-2000) processor
170
#define CROARING_IS_E2K 1
171
#endif
172
173
#if !CROARING_REGULAR_VISUAL_STUDIO && !defined(CROARING_IS_E2K) && !__FILC__
174
/* Non-Microsoft C/C++-compatible compiler, assumes that it supports inline
175
 * assembly */
176
#define CROARING_INLINE_ASM 1
177
#endif  // _MSC_VER
178
179
#if CROARING_REGULAR_VISUAL_STUDIO
180
/* Microsoft C/C++-compatible compiler */
181
#include <intrin.h>
182
183
#ifndef __clang__  // if one compiles with MSVC *with* clang, then these
184
                   // intrinsics are defined!!!
185
#define CROARING_INTRINSICS 1
186
// sadly there is no way to check whether we are missing these intrinsics
187
// specifically.
188
189
/* wrappers for Visual Studio built-ins that look like gcc built-ins
190
 * __builtin_ctzll */
191
/** result might be undefined when input_num is zero */
192
inline int roaring_trailing_zeroes(unsigned long long input_num) {
193
    unsigned long index;
194
#ifdef _WIN64  // highly recommended!!!
195
    _BitScanForward64(&index, input_num);
196
#else   // if we must support 32-bit Windows
197
    if ((uint32_t)input_num != 0) {
198
        _BitScanForward(&index, (uint32_t)input_num);
199
    } else {
200
        _BitScanForward(&index, (uint32_t)(input_num >> 32));
201
        index += 32;
202
    }
203
#endif  // _WIN64
204
    return index;
205
}
206
207
/* wrappers for Visual Studio built-ins that look like gcc built-ins
208
 * __builtin_clzll */
209
/** result might be undefined when input_num is zero */
210
inline int roaring_leading_zeroes(unsigned long long input_num) {
211
    unsigned long index;
212
#ifdef _WIN64  // highly recommended!!!
213
    _BitScanReverse64(&index, input_num);
214
#else   // if we must support 32-bit Windows
215
    if (input_num > 0xFFFFFFFF) {
216
        _BitScanReverse(&index, (uint32_t)(input_num >> 32));
217
        index += 32;
218
    } else {
219
        _BitScanReverse(&index, (uint32_t)(input_num));
220
    }
221
#endif  // _WIN64
222
    return 63 - index;
223
}
224
225
/* Use #define so this is effective even under /Ob0 (no inline) */
226
#define roaring_unreachable __assume(0)
227
#endif  // __clang__
228
229
#endif  // CROARING_REGULAR_VISUAL_STUDIO
230
231
#ifndef CROARING_INTRINSICS
232
#define CROARING_INTRINSICS 1
233
0
#define roaring_unreachable __builtin_unreachable()
234
/** result might be undefined when input_num is zero */
235
0
inline int roaring_trailing_zeroes(unsigned long long input_num) {
236
0
    return __builtin_ctzll(input_num);
237
0
}
238
/** result might be undefined when input_num is zero */
239
0
inline int roaring_leading_zeroes(unsigned long long input_num) {
240
0
    return __builtin_clzll(input_num);
241
0
}
242
#endif
243
244
#if CROARING_REGULAR_VISUAL_STUDIO
245
#define ALIGNED(x) __declspec(align(x))
246
#elif defined(__GNUC__) || defined(__clang__)
247
#define ALIGNED(x) __attribute__((aligned(x)))
248
#else
249
#warning "Warning. Unrecognized compiler."
250
#define ALIGNED(x)
251
#endif
252
253
#if defined(__GNUC__) || defined(__clang__)
254
#define CROARING_WARN_UNUSED __attribute__((warn_unused_result))
255
#else
256
#define CROARING_WARN_UNUSED
257
#endif
258
259
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
260
261
#ifdef CROARING_USENEON
262
// we can always compute the popcount fast.
263
#elif (defined(_M_ARM) || defined(_M_ARM64)) && \
264
    ((defined(_WIN64) || defined(_WIN32)) &&    \
265
     defined(CROARING_REGULAR_VISUAL_STUDIO) && \
266
     CROARING_REGULAR_VISUAL_STUDIO)
267
// we will need this function:
268
static inline int roaring_hamming_backup(uint64_t x) {
269
    uint64_t c1 = UINT64_C(0x5555555555555555);
270
    uint64_t c2 = UINT64_C(0x3333333333333333);
271
    uint64_t c4 = UINT64_C(0x0F0F0F0F0F0F0F0F);
272
    x -= (x >> 1) & c1;
273
    x = ((x >> 2) & c2) + (x & c2);
274
    x = (x + (x >> 4)) & c4;
275
    x *= UINT64_C(0x0101010101010101);
276
    return x >> 56;
277
}
278
#endif
279
280
0
static inline int roaring_hamming(uint64_t x) {
281
#if defined(_WIN64) && defined(CROARING_REGULAR_VISUAL_STUDIO) && \
282
    CROARING_REGULAR_VISUAL_STUDIO
283
#ifdef CROARING_USENEON
284
    return vaddv_u8(vcnt_u8(vcreate_u8(input_num)));
285
#elif defined(_M_ARM64)
286
    return roaring_hamming_backup(x);
287
    // (int) _CountOneBits64(x); is unavailable
288
#else   // _M_ARM64
289
    return (int)__popcnt64(x);
290
#endif  // _M_ARM64
291
#elif defined(_WIN32) && defined(CROARING_REGULAR_VISUAL_STUDIO) && \
292
    CROARING_REGULAR_VISUAL_STUDIO
293
#ifdef _M_ARM
294
    return roaring_hamming_backup(x);
295
    // _CountOneBits is unavailable
296
#else   // _M_ARM
297
    return (int)__popcnt((unsigned int)x) +
298
           (int)__popcnt((unsigned int)(x >> 32));
299
#endif  // _M_ARM
300
#else
301
0
    return __builtin_popcountll(x);
302
0
#endif
303
0
}
Unexecuted instantiation: croaring_fuzzer.c:roaring_hamming
Unexecuted instantiation: array_util.c:roaring_hamming
Unexecuted instantiation: containers.c:roaring_hamming
Unexecuted instantiation: run.c:roaring_hamming
Unexecuted instantiation: roaring.c:roaring_hamming
Unexecuted instantiation: roaring64.c:roaring_hamming
Unexecuted instantiation: roaring_array.c:roaring_hamming
Unexecuted instantiation: isadetection.c:roaring_hamming
Unexecuted instantiation: bitset_util.c:roaring_hamming
Unexecuted instantiation: art.c:roaring_hamming
Unexecuted instantiation: bitset.c:roaring_hamming
Unexecuted instantiation: array.c:roaring_hamming
Unexecuted instantiation: convert.c:roaring_hamming
Unexecuted instantiation: mixed_intersection.c:roaring_hamming
Unexecuted instantiation: mixed_union.c:roaring_hamming
Unexecuted instantiation: mixed_equal.c:roaring_hamming
Unexecuted instantiation: mixed_subset.c:roaring_hamming
Unexecuted instantiation: mixed_negation.c:roaring_hamming
Unexecuted instantiation: mixed_xor.c:roaring_hamming
Unexecuted instantiation: mixed_andnot.c:roaring_hamming
304
305
#ifndef UINT64_C
306
#define UINT64_C(c) (c##ULL)
307
#endif  // UINT64_C
308
309
#ifndef UINT32_C
310
#define UINT32_C(c) (c##UL)
311
#endif  // UINT32_C
312
313
#ifdef __cplusplus
314
}  // extern "C" {
315
#endif  // __cplusplus
316
317
// this is almost standard?
318
#undef STRINGIFY_IMPLEMENTATION_
319
#undef STRINGIFY
320
#define STRINGIFY_IMPLEMENTATION_(a) #a
321
#define STRINGIFY(a) STRINGIFY_IMPLEMENTATION_(a)
322
323
// Our fast kernels require 64-bit systems.
324
//
325
// On 32-bit x86, we lack 64-bit popcnt, lzcnt, blsr instructions.
326
// Furthermore, the number of SIMD registers is reduced.
327
//
328
// On 32-bit ARM, we would have smaller registers.
329
//
330
// The library should still have the fallback kernel. It is
331
// slower, but it should run everywhere.
332
333
//
334
// Enable valid runtime implementations, and select
335
// CROARING_BUILTIN_IMPLEMENTATION
336
//
337
338
// We are going to use runtime dispatch.
339
#if CROARING_IS_X64
340
#ifdef __clang__
341
// clang does not have GCC push pop
342
// warning: clang attribute push can't be used within a namespace in clang up
343
// til 8.0 so CROARING_TARGET_REGION and CROARING_UNTARGET_REGION must be
344
// *outside* of a namespace.
345
#define CROARING_TARGET_REGION(T)                                      \
346
    _Pragma(STRINGIFY(clang attribute push(__attribute__((target(T))), \
347
                                           apply_to = function)))
348
#define CROARING_UNTARGET_REGION _Pragma("clang attribute pop")
349
#elif defined(__GNUC__)
350
// GCC is easier
351
#define CROARING_TARGET_REGION(T) \
352
    _Pragma("GCC push_options") _Pragma(STRINGIFY(GCC target(T)))
353
#define CROARING_UNTARGET_REGION _Pragma("GCC pop_options")
354
#endif  // clang then gcc
355
356
#endif  // CROARING_IS_X64
357
358
// Default target region macros don't do anything.
359
#ifndef CROARING_TARGET_REGION
360
#define CROARING_TARGET_REGION(T)
361
#define CROARING_UNTARGET_REGION
362
#endif
363
364
#define CROARING_TARGET_AVX2 \
365
    CROARING_TARGET_REGION("avx2,bmi,pclmul,lzcnt,popcnt")
366
#define CROARING_TARGET_AVX512                                         \
367
    CROARING_TARGET_REGION(                                            \
368
        "avx2,bmi,bmi2,pclmul,lzcnt,popcnt,avx512f,avx512dq,avx512bw," \
369
        "avx512vbmi2,avx512bitalg,avx512vpopcntdq")
370
#define CROARING_UNTARGET_AVX2 CROARING_UNTARGET_REGION
371
#define CROARING_UNTARGET_AVX512 CROARING_UNTARGET_REGION
372
373
#ifdef __AVX2__
374
// No need for runtime dispatching.
375
// It is unnecessary and harmful to old clang to tag regions.
376
#undef CROARING_TARGET_AVX2
377
#define CROARING_TARGET_AVX2
378
#undef CROARING_UNTARGET_AVX2
379
#define CROARING_UNTARGET_AVX2
380
#endif
381
382
#if defined(__AVX512F__) && defined(__AVX512DQ__) && defined(__AVX512BW__) && \
383
    defined(__AVX512VBMI2__) && defined(__AVX512BITALG__) &&                  \
384
    defined(__AVX512VPOPCNTDQ__)
385
// No need for runtime dispatching.
386
// It is unnecessary and harmful to old clang to tag regions.
387
#undef CROARING_TARGET_AVX512
388
#define CROARING_TARGET_AVX512
389
#undef CROARING_UNTARGET_AVX512
390
#define CROARING_UNTARGET_AVX512
391
#endif
392
393
// Allow unaligned memory access
394
#if defined(__GNUC__) || defined(__clang__)
395
#define CROARING_ALLOW_UNALIGNED __attribute__((no_sanitize("alignment")))
396
#else
397
#define CROARING_ALLOW_UNALIGNED
398
#endif
399
400
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
401
#define CROARING_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
402
#elif defined(_WIN32)
403
#define CROARING_IS_BIG_ENDIAN 0
404
#else
405
#if defined(__APPLE__) || \
406
    defined(__FreeBSD__)  // defined __BYTE_ORDER__ && defined
407
                          // __ORDER_BIG_ENDIAN__
408
#include <machine/endian.h>
409
#elif defined(sun) || \
410
    defined(__sun)  // defined(__APPLE__) || defined(__FreeBSD__)
411
#include <sys/byteorder.h>
412
#else  // defined(__APPLE__) || defined(__FreeBSD__)
413
414
#ifdef __has_include
415
#if __has_include(<endian.h>)
416
#include <endian.h>
417
#endif  //__has_include(<endian.h>)
418
#endif  //__has_include
419
420
#endif  // defined(__APPLE__) || defined(__FreeBSD__)
421
422
#ifndef !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__)
423
#define CROARING_IS_BIG_ENDIAN 0
424
#endif
425
426
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
427
#define CROARING_IS_BIG_ENDIAN 0
428
#else  // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
429
#define CROARING_IS_BIG_ENDIAN 1
430
#endif  // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
431
#endif
432
433
// Host <-> big endian conversion.
434
#if CROARING_IS_BIG_ENDIAN
435
#define croaring_htobe64(x) (x)
436
437
#elif defined(_WIN32) || defined(_WIN64)  // CROARING_IS_BIG_ENDIAN
438
#include <stdlib.h>
439
#define croaring_htobe64(x) _byteswap_uint64(x)
440
441
#elif defined(__APPLE__)  // CROARING_IS_BIG_ENDIAN
442
#include <libkern/OSByteOrder.h>
443
#define croaring_htobe64(x) OSSwapInt64(x)
444
445
#elif defined(__has_include) && \
446
    __has_include(              \
447
        <byteswap.h>)  && (defined(__linux__) || defined(__FreeBSD__))  // CROARING_IS_BIG_ENDIAN
448
#include <byteswap.h>
449
#if defined(__linux__)
450
0
#define croaring_htobe64(x) bswap_64(x)
451
#elif defined(__FreeBSD__)
452
#define croaring_htobe64(x) bswap64(x)
453
#else
454
#warning "Unknown platform, report as an error"
455
#endif
456
457
#else  // CROARING_IS_BIG_ENDIAN
458
// Gets compiled to bswap or equivalent on most compilers.
459
#define croaring_htobe64(x)                                                    \
460
    (((x & 0x00000000000000FFULL) << 56) |                                     \
461
     ((x & 0x000000000000FF00ULL) << 40) |                                     \
462
     ((x & 0x0000000000FF0000ULL) << 24) |                                     \
463
     ((x & 0x00000000FF000000ULL) << 8) | ((x & 0x000000FF00000000ULL) >> 8) | \
464
     ((x & 0x0000FF0000000000ULL) >> 24) |                                     \
465
     ((x & 0x00FF000000000000ULL) >> 40) |                                     \
466
     ((x & 0xFF00000000000000ULL) >> 56))
467
#endif  // CROARING_IS_BIG_ENDIAN
468
0
#define croaring_be64toh(x) croaring_htobe64(x)
469
// End of host <-> big endian conversion.
470
471
// Host <-> little-endian conversion helpers.
472
//
473
// The CRoaring "portable" serialization format (and the regular
474
// roaring_bitmap_serialize / Roaring64Map::write formats which build on it)
475
// is defined to be little-endian on the wire. Code that reads or writes
476
// multi-byte integers to such buffers must convert between host and
477
// little-endian byte order. On little-endian hosts these are no-ops; on
478
// big-endian hosts they swap bytes.
479
//
480
// The "frozen" format is intentionally non-portable and uses native byte
481
// order; it must not use these helpers.
482
#if CROARING_IS_BIG_ENDIAN
483
484
static inline uint16_t croaring_bswap16(uint16_t x) {
485
    return (uint16_t)((x << 8) | (x >> 8));
486
}
487
488
static inline uint32_t croaring_bswap32(uint32_t x) {
489
    return ((x & 0x000000FFU) << 24) | ((x & 0x0000FF00U) << 8) |
490
           ((x & 0x00FF0000U) >> 8) | ((x & 0xFF000000U) >> 24);
491
}
492
493
static inline uint64_t croaring_bswap64(uint64_t x) {
494
    return ((x & 0x00000000000000FFULL) << 56) |
495
           ((x & 0x000000000000FF00ULL) << 40) |
496
           ((x & 0x0000000000FF0000ULL) << 24) |
497
           ((x & 0x00000000FF000000ULL) << 8) |
498
           ((x & 0x000000FF00000000ULL) >> 8) |
499
           ((x & 0x0000FF0000000000ULL) >> 24) |
500
           ((x & 0x00FF000000000000ULL) >> 40) |
501
           ((x & 0xFF00000000000000ULL) >> 56);
502
}
503
504
#define croaring_htole16(x) croaring_bswap16(x)
505
#define croaring_htole32(x) croaring_bswap32(x)
506
#define croaring_htole64(x) croaring_bswap64(x)
507
508
#else  // CROARING_IS_BIG_ENDIAN
509
510
0
#define croaring_htole16(x) (x)
511
0
#define croaring_htole32(x) (x)
512
0
#define croaring_htole64(x) (x)
513
514
#endif  // CROARING_IS_BIG_ENDIAN
515
516
0
#define croaring_letoh16(x) croaring_htole16(x)
517
0
#define croaring_letoh32(x) croaring_htole32(x)
518
0
#define croaring_letoh64(x) croaring_htole64(x)
519
520
// Defines for the possible CROARING atomic implementations
521
#define CROARING_ATOMIC_IMPL_NONE 1
522
#define CROARING_ATOMIC_IMPL_CPP 2
523
#define CROARING_ATOMIC_IMPL_C 3
524
#define CROARING_ATOMIC_IMPL_C_WINDOWS 4
525
526
// If the use has forced a specific implementation, use that, otherwise,
527
// figure out the best implementation we can use.
528
#if !defined(CROARING_ATOMIC_IMPL)
529
#if defined(__cplusplus) && __cplusplus >= 201103L
530
#ifdef __has_include
531
#if __has_include(<atomic>)
532
#define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_CPP
533
#endif  //__has_include(<atomic>)
534
#else
535
// We lack __has_include to check:
536
#define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_CPP
537
#endif  //__has_include
538
#elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
539
#define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_C
540
#elif CROARING_REGULAR_VISUAL_STUDIO
541
// https://www.technetworkhub.com/c11-atomics-in-visual-studio-2022-version-17/
542
#define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_C_WINDOWS
543
#endif
544
#endif  // !defined(CROARING_ATOMIC_IMPL)
545
546
#if CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_C
547
#include <stdatomic.h>
548
typedef _Atomic(uint32_t) croaring_refcount_t;
549
550
0
static inline void croaring_refcount_inc(croaring_refcount_t *val) {
551
    // Increasing the reference counter can always be done with
552
    // memory_order_relaxed: New references to an object can only be formed from
553
    // an existing reference, and passing an existing reference from one thread
554
    // to another must already provide any required synchronization.
555
0
    atomic_fetch_add_explicit(val, 1, memory_order_relaxed);
556
0
}
Unexecuted instantiation: croaring_fuzzer.c:croaring_refcount_inc
Unexecuted instantiation: array_util.c:croaring_refcount_inc
Unexecuted instantiation: containers.c:croaring_refcount_inc
Unexecuted instantiation: run.c:croaring_refcount_inc
Unexecuted instantiation: roaring.c:croaring_refcount_inc
Unexecuted instantiation: roaring64.c:croaring_refcount_inc
Unexecuted instantiation: roaring_array.c:croaring_refcount_inc
Unexecuted instantiation: isadetection.c:croaring_refcount_inc
Unexecuted instantiation: bitset_util.c:croaring_refcount_inc
Unexecuted instantiation: art.c:croaring_refcount_inc
Unexecuted instantiation: bitset.c:croaring_refcount_inc
Unexecuted instantiation: array.c:croaring_refcount_inc
Unexecuted instantiation: convert.c:croaring_refcount_inc
Unexecuted instantiation: mixed_intersection.c:croaring_refcount_inc
Unexecuted instantiation: mixed_union.c:croaring_refcount_inc
Unexecuted instantiation: mixed_equal.c:croaring_refcount_inc
Unexecuted instantiation: mixed_subset.c:croaring_refcount_inc
Unexecuted instantiation: mixed_negation.c:croaring_refcount_inc
Unexecuted instantiation: mixed_xor.c:croaring_refcount_inc
Unexecuted instantiation: mixed_andnot.c:croaring_refcount_inc
557
558
0
static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
559
    // It is important to enforce any possible access to the object in one
560
    // thread (through an existing reference) to happen before deleting the
561
    // object in a different thread. This is achieved by a "release" operation
562
    // after dropping a reference (any access to the object through this
563
    // reference must obviously happened before), and an "acquire" operation
564
    // before deleting the object.
565
0
    bool is_zero = atomic_fetch_sub_explicit(val, 1, memory_order_release) == 1;
566
0
    if (is_zero) {
567
0
        atomic_thread_fence(memory_order_acquire);
568
0
    }
569
0
    return is_zero;
570
0
}
Unexecuted instantiation: croaring_fuzzer.c:croaring_refcount_dec
Unexecuted instantiation: array_util.c:croaring_refcount_dec
Unexecuted instantiation: containers.c:croaring_refcount_dec
Unexecuted instantiation: run.c:croaring_refcount_dec
Unexecuted instantiation: roaring.c:croaring_refcount_dec
Unexecuted instantiation: roaring64.c:croaring_refcount_dec
Unexecuted instantiation: roaring_array.c:croaring_refcount_dec
Unexecuted instantiation: isadetection.c:croaring_refcount_dec
Unexecuted instantiation: bitset_util.c:croaring_refcount_dec
Unexecuted instantiation: art.c:croaring_refcount_dec
Unexecuted instantiation: bitset.c:croaring_refcount_dec
Unexecuted instantiation: array.c:croaring_refcount_dec
Unexecuted instantiation: convert.c:croaring_refcount_dec
Unexecuted instantiation: mixed_intersection.c:croaring_refcount_dec
Unexecuted instantiation: mixed_union.c:croaring_refcount_dec
Unexecuted instantiation: mixed_equal.c:croaring_refcount_dec
Unexecuted instantiation: mixed_subset.c:croaring_refcount_dec
Unexecuted instantiation: mixed_negation.c:croaring_refcount_dec
Unexecuted instantiation: mixed_xor.c:croaring_refcount_dec
Unexecuted instantiation: mixed_andnot.c:croaring_refcount_dec
571
572
0
static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
573
0
    return atomic_load_explicit(val, memory_order_relaxed);
574
0
}
Unexecuted instantiation: croaring_fuzzer.c:croaring_refcount_get
Unexecuted instantiation: array_util.c:croaring_refcount_get
Unexecuted instantiation: containers.c:croaring_refcount_get
Unexecuted instantiation: run.c:croaring_refcount_get
Unexecuted instantiation: roaring.c:croaring_refcount_get
Unexecuted instantiation: roaring64.c:croaring_refcount_get
Unexecuted instantiation: roaring_array.c:croaring_refcount_get
Unexecuted instantiation: isadetection.c:croaring_refcount_get
Unexecuted instantiation: bitset_util.c:croaring_refcount_get
Unexecuted instantiation: art.c:croaring_refcount_get
Unexecuted instantiation: bitset.c:croaring_refcount_get
Unexecuted instantiation: array.c:croaring_refcount_get
Unexecuted instantiation: convert.c:croaring_refcount_get
Unexecuted instantiation: mixed_intersection.c:croaring_refcount_get
Unexecuted instantiation: mixed_union.c:croaring_refcount_get
Unexecuted instantiation: mixed_equal.c:croaring_refcount_get
Unexecuted instantiation: mixed_subset.c:croaring_refcount_get
Unexecuted instantiation: mixed_negation.c:croaring_refcount_get
Unexecuted instantiation: mixed_xor.c:croaring_refcount_get
Unexecuted instantiation: mixed_andnot.c:croaring_refcount_get
575
#elif CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_CPP
576
#include <atomic>
577
typedef std::atomic<uint32_t> croaring_refcount_t;
578
579
static inline void croaring_refcount_inc(croaring_refcount_t *val) {
580
    val->fetch_add(1, std::memory_order_relaxed);
581
}
582
583
static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
584
    // See above comments on the c11 atomic implementation for memory ordering
585
    bool is_zero = val->fetch_sub(1, std::memory_order_release) == 1;
586
    if (is_zero) {
587
        std::atomic_thread_fence(std::memory_order_acquire);
588
    }
589
    return is_zero;
590
}
591
592
static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
593
    return val->load(std::memory_order_relaxed);
594
}
595
#elif CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_C_WINDOWS
596
#include <intrin.h>
597
#pragma intrinsic(_InterlockedIncrement)
598
#pragma intrinsic(_InterlockedDecrement)
599
600
// _InterlockedIncrement and _InterlockedDecrement take a (signed) long, and
601
// overflow is defined to wrap, so we can pretend it is a uint32_t for our case
602
typedef volatile long croaring_refcount_t;
603
604
static inline void croaring_refcount_inc(croaring_refcount_t *val) {
605
    _InterlockedIncrement(val);
606
}
607
608
static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
609
    return _InterlockedDecrement(val) == 0;
610
}
611
612
static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
613
    // Per
614
    // https://learn.microsoft.com/en-us/windows/win32/sync/interlocked-variable-access
615
    // > Simple reads and writes to properly-aligned 32-bit variables are atomic
616
    // > operations. In other words, you will not end up with only one portion
617
    // > of the variable updated; all bits are updated in an atomic fashion.
618
    return *val;
619
}
620
#elif CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_NONE
621
#include <assert.h>
622
typedef uint32_t croaring_refcount_t;
623
624
static inline void croaring_refcount_inc(croaring_refcount_t *val) {
625
    *val += 1;
626
}
627
628
static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
629
    assert(*val > 0);
630
    *val -= 1;
631
    return val == 0;
632
}
633
634
static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
635
    return *val;
636
}
637
#else
638
#error "Unknown atomic implementation"
639
#endif
640
641
#if defined(__GNUC__) || defined(__clang__)
642
#define CROARING_DEPRECATED __attribute__((deprecated))
643
#elif defined(_MSC_VER)
644
#define CROARING_DEPRECATED __declspec(deprecated)
645
#else
646
#define CROARING_DEPRECATED
647
#endif  // defined(__GNUC__) || defined(__clang__)
648
649
// We want to initialize structs to zero portably (C and C++), without
650
// warnings. We can do mystruct s = CROARING_ZERO_INITIALIZER;
651
#if __cplusplus
652
#define CROARING_ZERO_INITIALIZER \
653
    {}
654
#else
655
#define CROARING_ZERO_INITIALIZER \
656
0
    { 0 }
657
#endif
658
659
#if defined(__cplusplus)
660
#define CROARING_STATIC_ASSERT(x, y) static_assert(x, y)
661
#else
662
0
#define CROARING_STATIC_ASSERT(x, y) _Static_assert(x, y)
663
#endif
664
665
// We need portability.h to be included first,
666
// but we also always want isadetection.h to be
667
// included (right after).
668
// See https://github.com/RoaringBitmap/CRoaring/issues/394
669
// There is no scenario where we want portability.h to
670
// be included, but not isadetection.h: the latter is a
671
// strict requirement.
672
#include <roaring/isadetection.h>  // include it last!
673
#endif                             /* INCLUDE_PORTABILITY_H_ */