Coverage Report

Created: 2026-08-17 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/croaring/src/isadetection.c
Line
Count
Source
1
2
/* From
3
https://github.com/endorno/pytorch/blob/master/torch/lib/TH/generic/simd/simd.h
4
Highly modified.
5
6
Copyright (c) 2016-     Facebook, Inc            (Adam Paszke)
7
Copyright (c) 2014-     Facebook, Inc            (Soumith Chintala)
8
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
9
Copyright (c) 2012-2014 Deepmind Technologies    (Koray Kavukcuoglu)
10
Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
11
Copyright (c) 2011-2013 NYU                      (Clement Farabet)
12
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou,
13
Iain Melvin, Jason Weston) Copyright (c) 2006      Idiap Research Institute
14
(Samy Bengio) Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert,
15
Samy Bengio, Johnny Mariethoz)
16
17
All rights reserved.
18
19
Redistribution and use in source and binary forms, with or without
20
modification, are permitted provided that the following conditions are met:
21
22
1. Redistributions of source code must retain the above copyright
23
   notice, this list of conditions and the following disclaimer.
24
25
2. Redistributions in binary form must reproduce the above copyright
26
   notice, this list of conditions and the following disclaimer in the
27
   documentation and/or other materials provided with the distribution.
28
29
3. Neither the names of Facebook, Deepmind Technologies, NYU, NEC Laboratories
30
America and IDIAP Research Institute nor the names of its contributors may be
31
   used to endorse or promote products derived from this software without
32
   specific prior written permission.
33
34
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44
POSSIBILITY OF SUCH DAMAGE.
45
*/
46
47
#include <stdbool.h>
48
#include <stdint.h>
49
#include <stdlib.h>
50
51
// Binaries produced by Visual Studio 19.38 with solely AVX2 routines
52
// can compile to AVX-512 thus causing crashes on non-AVX-512 systems.
53
// This appears to affect VS 17.8 and 17.9. We disable AVX-512 and AVX2
54
// on these systems. It seems that ClangCL is not affected.
55
// https://github.com/RoaringBitmap/CRoaring/pull/603
56
#ifndef __clang__
57
#if _MSC_VER == 1938
58
#define ROARING_DISABLE_AVX 1
59
#endif  // _MSC_VER == 1938
60
#endif  // __clang__
61
62
#ifdef __FILC__
63
#include <stdfil.h>
64
#endif
65
66
// We need portability.h to be included first, see
67
// https://github.com/RoaringBitmap/CRoaring/issues/394
68
#include <roaring/portability.h>
69
#if CROARING_REGULAR_VISUAL_STUDIO
70
#include <intrin.h>
71
#elif (defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID)) || \
72
    defined(__FILC__)
73
#include <cpuid.h>
74
#endif  // CROARING_REGULAR_VISUAL_STUDIO
75
#include <roaring/isadetection.h>
76
77
#if CROARING_IS_X64
78
#ifndef CROARING_COMPILER_SUPPORTS_AVX512
79
#error "CROARING_COMPILER_SUPPORTS_AVX512 needs to be defined."
80
#endif  // CROARING_COMPILER_SUPPORTS_AVX512
81
#endif
82
83
#ifdef __cplusplus
84
extern "C" {
85
namespace roaring {
86
namespace internal {
87
#endif
88
enum croaring_instruction_set {
89
    CROARING_DEFAULT = 0x0,
90
    CROARING_NEON = 0x1,
91
    CROARING_AVX2 = 0x4,
92
    CROARING_SSE42 = 0x8,
93
    CROARING_PCLMULQDQ = 0x10,
94
    CROARING_BMI1 = 0x20,
95
    CROARING_BMI2 = 0x40,
96
    CROARING_ALTIVEC = 0x80,
97
    CROARING_AVX512F = 0x100,
98
    CROARING_AVX512DQ = 0x200,
99
    CROARING_AVX512BW = 0x400,
100
    CROARING_AVX512VBMI2 = 0x800,
101
    CROARING_AVX512BITALG = 0x1000,
102
    CROARING_AVX512VPOPCNTDQ = 0x2000,
103
    CROARING_UNINITIALIZED = 0x8000
104
};
105
106
#if CROARING_COMPILER_SUPPORTS_AVX512
107
unsigned int CROARING_AVX512_REQUIRED =
108
    (CROARING_AVX512F | CROARING_AVX512DQ | CROARING_AVX512BW |
109
     CROARING_AVX512VBMI2 | CROARING_AVX512BITALG | CROARING_AVX512VPOPCNTDQ);
110
#endif
111
112
#if CROARING_IS_X64  // x64
113
114
static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx,
115
2
                         uint32_t *edx) {
116
#if CROARING_REGULAR_VISUAL_STUDIO
117
    int cpu_info[4];
118
    __cpuidex(cpu_info, *eax, *ecx);
119
    *eax = cpu_info[0];
120
    *ebx = cpu_info[1];
121
    *ecx = cpu_info[2];
122
    *edx = cpu_info[3];
123
#elif (defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID)) || \
124
    defined(__FILC__)
125
    uint32_t level = *eax;
126
    __get_cpuid(level, eax, ebx, ecx, edx);
127
#else
128
2
    uint32_t a = *eax, b, c = *ecx, d;
129
2
    __asm__("cpuid\n\t" : "+a"(a), "=b"(b), "+c"(c), "=d"(d));
130
2
    *eax = a;
131
2
    *ebx = b;
132
2
    *ecx = c;
133
2
    *edx = d;
134
2
#endif
135
2
}
136
137
1
static inline uint64_t xgetbv(void) {
138
#if defined(_MSC_VER)
139
    return _xgetbv(0);
140
#elif defined(__FILC__)
141
    return zxgetbv();
142
#else
143
1
    uint32_t xcr0_lo, xcr0_hi;
144
1
    __asm__("xgetbv\n\t" : "=a"(xcr0_lo), "=d"(xcr0_hi) : "c"(0));
145
1
    return xcr0_lo | ((uint64_t)xcr0_hi << 32);
146
1
#endif
147
1
}
148
149
/**
150
 * This is a relatively expensive function but it will get called at most
151
 * *once* per compilation units. Normally, the CRoaring library is built
152
 * as one compilation unit.
153
 */
154
1
static inline uint32_t dynamic_croaring_detect_supported_architectures(void) {
155
1
    uint32_t eax, ebx, ecx, edx;
156
1
    uint32_t host_isa = 0x0;
157
    // Can be found on Intel ISA Reference for CPUID
158
1
    static uint32_t cpuid_avx2_bit =
159
1
        1 << 5;  ///< @private Bit 5 of EBX for EAX=0x7
160
1
    static uint32_t cpuid_bmi1_bit =
161
1
        1 << 3;  ///< @private bit 3 of EBX for EAX=0x7
162
1
    static uint32_t cpuid_bmi2_bit =
163
1
        1 << 8;  ///< @private bit 8 of EBX for EAX=0x7
164
1
    static uint32_t cpuid_avx512f_bit =
165
1
        1 << 16;  ///< @private bit 16 of EBX for EAX=0x7
166
1
    static uint32_t cpuid_avx512dq_bit =
167
1
        1 << 17;  ///< @private bit 17 of EBX for EAX=0x7
168
1
    static uint32_t cpuid_avx512bw_bit =
169
1
        1 << 30;  ///< @private bit 30 of EBX for EAX=0x7
170
1
    static uint32_t cpuid_avx512vbmi2_bit =
171
1
        1 << 6;  ///< @private bit 6 of ECX for EAX=0x7
172
1
    static uint32_t cpuid_avx512bitalg_bit =
173
1
        1 << 12;  ///< @private bit 12 of ECX for EAX=0x7
174
1
    static uint32_t cpuid_avx512vpopcntdq_bit =
175
1
        1 << 14;  ///< @private bit 14 of ECX for EAX=0x7
176
1
    static uint64_t cpuid_avx256_saved = 1 << 2;  ///< @private bit 2 = AVX
177
1
    static uint64_t cpuid_avx512_saved =
178
1
        7 << 5;  ///< @private bits 5,6,7 = opmask, ZMM_hi256, hi16_ZMM
179
1
    static uint32_t cpuid_sse42_bit =
180
1
        1 << 20;  ///< @private bit 20 of ECX for EAX=0x1
181
1
    static uint32_t cpuid_osxsave =
182
1
        (1 << 26) | (1 << 27);  ///< @private bits 26+27 of ECX for EAX=0x1
183
1
    static uint32_t cpuid_pclmulqdq_bit =
184
1
        1 << 1;  ///< @private bit  1 of ECX for EAX=0x1
185
186
    // EBX for EAX=0x1
187
1
    eax = 0x1;
188
1
    ecx = 0x0;
189
1
    cpuid(&eax, &ebx, &ecx, &edx);
190
191
1
    if (ecx & cpuid_sse42_bit) {
192
1
        host_isa |= CROARING_SSE42;
193
1
    } else {
194
0
        return host_isa;  // everything after is redundant
195
0
    }
196
197
1
    if (ecx & cpuid_pclmulqdq_bit) {
198
1
        host_isa |= CROARING_PCLMULQDQ;
199
1
    }
200
201
1
    if ((ecx & cpuid_osxsave) != cpuid_osxsave) {
202
0
        return host_isa;
203
0
    }
204
205
    // xgetbv for checking if the OS saves registers
206
1
    uint64_t xcr0 = xgetbv();
207
208
1
    if ((xcr0 & cpuid_avx256_saved) == 0) {
209
0
        return host_isa;
210
0
    }
211
212
    // ECX for EAX=0x7
213
1
    eax = 0x7;
214
1
    ecx = 0x0;
215
1
    cpuid(&eax, &ebx, &ecx, &edx);
216
1
    if (ebx & cpuid_avx2_bit) {
217
1
        host_isa |= CROARING_AVX2;
218
1
    }
219
1
    if (ebx & cpuid_bmi1_bit) {
220
1
        host_isa |= CROARING_BMI1;
221
1
    }
222
223
1
    if (ebx & cpuid_bmi2_bit) {
224
1
        host_isa |= CROARING_BMI2;
225
1
    }
226
227
1
    if (!((xcr0 & cpuid_avx512_saved) == cpuid_avx512_saved)) {
228
1
        return host_isa;
229
1
    }
230
231
0
    if (ebx & cpuid_avx512f_bit) {
232
0
        host_isa |= CROARING_AVX512F;
233
0
    }
234
235
0
    if (ebx & cpuid_avx512bw_bit) {
236
0
        host_isa |= CROARING_AVX512BW;
237
0
    }
238
239
0
    if (ebx & cpuid_avx512dq_bit) {
240
0
        host_isa |= CROARING_AVX512DQ;
241
0
    }
242
243
0
    if (ecx & cpuid_avx512vbmi2_bit) {
244
0
        host_isa |= CROARING_AVX512VBMI2;
245
0
    }
246
247
0
    if (ecx & cpuid_avx512bitalg_bit) {
248
0
        host_isa |= CROARING_AVX512BITALG;
249
0
    }
250
251
0
    if (ecx & cpuid_avx512vpopcntdq_bit) {
252
0
        host_isa |= CROARING_AVX512VPOPCNTDQ;
253
0
    }
254
255
0
    return host_isa;
256
1
}
257
258
#endif  // end SIMD extension detection code
259
260
#if CROARING_IS_X64  // x64
261
262
#if CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_CPP
263
static inline uint32_t croaring_detect_supported_architectures(void) {
264
    // thread-safe as per the C++11 standard.
265
    static uint32_t buffer = dynamic_croaring_detect_supported_architectures();
266
    return buffer;
267
}
268
#elif CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_C
269
2
static uint32_t croaring_detect_supported_architectures(void) {
270
    // we use an atomic for thread safety
271
2
    static _Atomic uint32_t buffer = CROARING_UNINITIALIZED;
272
2
    if (buffer == CROARING_UNINITIALIZED) {
273
        // atomicity is sufficient
274
1
        buffer = dynamic_croaring_detect_supported_architectures();
275
1
    }
276
2
    return buffer;
277
2
}
278
#else
279
// If we do not have atomics, we do the best we can.
280
static inline uint32_t croaring_detect_supported_architectures(void) {
281
    static uint32_t buffer = CROARING_UNINITIALIZED;
282
    if (buffer == CROARING_UNINITIALIZED) {
283
        buffer = dynamic_croaring_detect_supported_architectures();
284
    }
285
    return buffer;
286
}
287
#endif  // CROARING_C_ATOMIC
288
289
#ifdef ROARING_DISABLE_AVX
290
291
int croaring_hardware_support(void) { return 0; }
292
293
#elif defined(__AVX512F__) && defined(__AVX512DQ__) &&   \
294
    defined(__AVX512BW__) && defined(__AVX512VBMI2__) && \
295
    defined(__AVX512BITALG__) && defined(__AVX512VPOPCNTDQ__)
296
int croaring_hardware_support(void) {
297
    return ROARING_SUPPORTS_AVX2 | ROARING_SUPPORTS_AVX512;
298
}
299
#elif defined(__AVX2__)
300
301
int croaring_hardware_support(void) {
302
    static
303
#if CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_C
304
        _Atomic
305
#endif
306
        int support = 0xFFFFFFF;
307
    if (support == 0xFFFFFFF) {
308
        bool avx512_support = false;
309
#if CROARING_COMPILER_SUPPORTS_AVX512
310
        avx512_support =
311
            ((croaring_detect_supported_architectures() &
312
              CROARING_AVX512_REQUIRED) == CROARING_AVX512_REQUIRED);
313
#endif
314
        support = ROARING_SUPPORTS_AVX2 |
315
                  (avx512_support ? ROARING_SUPPORTS_AVX512 : 0);
316
    }
317
    return support;
318
}
319
#else
320
321
10.1k
int croaring_hardware_support(void) {
322
10.1k
    static
323
10.1k
#if CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_C
324
10.1k
        _Atomic
325
10.1k
#endif
326
10.1k
        int support = 0xFFFFFFF;
327
10.1k
    if (support == 0xFFFFFFF) {
328
1
        bool has_avx2 = (croaring_detect_supported_architectures() &
329
1
                         CROARING_AVX2) == CROARING_AVX2;
330
1
        bool has_avx512 = false;
331
1
#if CROARING_COMPILER_SUPPORTS_AVX512
332
1
        has_avx512 = (croaring_detect_supported_architectures() &
333
1
                      CROARING_AVX512_REQUIRED) == CROARING_AVX512_REQUIRED;
334
1
#endif  // CROARING_COMPILER_SUPPORTS_AVX512
335
1
        support = (has_avx2 ? ROARING_SUPPORTS_AVX2 : 0) |
336
1
                  (has_avx512 ? ROARING_SUPPORTS_AVX512 : 0);
337
1
    }
338
10.1k
    return support;
339
10.1k
}
340
#endif
341
342
#endif  // CROARING_IS_X64 // x64
343
#ifdef __cplusplus
344
}
345
}
346
}  // extern "C" { namespace roaring { namespace internal {
347
#endif