/src/croaring/include/roaring/isadetection.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* From |
2 | | https://github.com/endorno/pytorch/blob/master/torch/lib/TH/generic/simd/simd.h |
3 | | Highly modified. |
4 | | |
5 | | Copyright (c) 2016- Facebook, Inc (Adam Paszke) |
6 | | Copyright (c) 2014- Facebook, Inc (Soumith Chintala) |
7 | | Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) |
8 | | Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu) |
9 | | Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu) |
10 | | Copyright (c) 2011-2013 NYU (Clement Farabet) |
11 | | Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, |
12 | | Iain Melvin, Jason Weston) Copyright (c) 2006 Idiap Research Institute |
13 | | (Samy Bengio) Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, |
14 | | Samy Bengio, Johnny Mariethoz) |
15 | | |
16 | | All rights reserved. |
17 | | |
18 | | Redistribution and use in source and binary forms, with or without |
19 | | modification, are permitted provided that the following conditions are met: |
20 | | |
21 | | 1. Redistributions of source code must retain the above copyright |
22 | | notice, this list of conditions and the following disclaimer. |
23 | | |
24 | | 2. Redistributions in binary form must reproduce the above copyright |
25 | | notice, this list of conditions and the following disclaimer in the |
26 | | documentation and/or other materials provided with the distribution. |
27 | | |
28 | | 3. Neither the names of Facebook, Deepmind Technologies, NYU, NEC Laboratories |
29 | | America and IDIAP Research Institute nor the names of its contributors may be |
30 | | used to endorse or promote products derived from this software without |
31 | | specific prior written permission. |
32 | | |
33 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
34 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
35 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
36 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
37 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
38 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
39 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
40 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
41 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
42 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
43 | | POSSIBILITY OF SUCH DAMAGE. |
44 | | */ |
45 | | |
46 | | #ifndef ROARING_ISADETECTION_H |
47 | | #define ROARING_ISADETECTION_H |
48 | | |
49 | | // isadetection.h does not define any macro (except for ROARING_ISADETECTION_H). |
50 | | |
51 | | #include <stdint.h> |
52 | | #include <stdbool.h> |
53 | | #include <stdlib.h> |
54 | | |
55 | | // We need portability.h to be included first, see |
56 | | // https://github.com/RoaringBitmap/CRoaring/issues/394 |
57 | | #include <roaring/portability.h> |
58 | | #if CROARING_REGULAR_VISUAL_STUDIO |
59 | | #include <intrin.h> |
60 | | #elif defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID) |
61 | | #include <cpuid.h> |
62 | | #endif // CROARING_REGULAR_VISUAL_STUDIO |
63 | | |
64 | | |
65 | | enum croaring_instruction_set { |
66 | | CROARING_DEFAULT = 0x0, |
67 | | CROARING_NEON = 0x1, |
68 | | CROARING_AVX2 = 0x4, |
69 | | CROARING_SSE42 = 0x8, |
70 | | CROARING_PCLMULQDQ = 0x10, |
71 | | CROARING_BMI1 = 0x20, |
72 | | CROARING_BMI2 = 0x40, |
73 | | CROARING_ALTIVEC = 0x80, |
74 | | CROARING_UNINITIALIZED = 0x8000 |
75 | | }; |
76 | | |
77 | | #if defined(__PPC64__) |
78 | | |
79 | | //static inline uint32_t dynamic_croaring_detect_supported_architectures() { |
80 | | // return CROARING_ALTIVEC; |
81 | | //} |
82 | | |
83 | | #elif defined(__arm__) || defined(__aarch64__) // incl. armel, armhf, arm64 |
84 | | |
85 | | #if defined(__ARM_NEON) |
86 | | |
87 | | //static inline uint32_t dynamic_croaring_detect_supported_architectures() { |
88 | | // return CROARING_NEON; |
89 | | //} |
90 | | |
91 | | #else // ARM without NEON |
92 | | |
93 | | //static inline uint32_t dynamic_croaring_detect_supported_architectures() { |
94 | | // return CROARING_DEFAULT; |
95 | | //} |
96 | | |
97 | | #endif |
98 | | |
99 | | #elif defined(__x86_64__) || defined(_M_AMD64) // x64 |
100 | | |
101 | | |
102 | | |
103 | | |
104 | | static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, |
105 | 0 | uint32_t *edx) { |
106 | |
|
107 | | #if CROARING_REGULAR_VISUAL_STUDIO |
108 | | int cpu_info[4]; |
109 | | __cpuid(cpu_info, *eax); |
110 | | *eax = cpu_info[0]; |
111 | | *ebx = cpu_info[1]; |
112 | | *ecx = cpu_info[2]; |
113 | | *edx = cpu_info[3]; |
114 | | #elif defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID) |
115 | | uint32_t level = *eax; |
116 | | __get_cpuid(level, eax, ebx, ecx, edx); |
117 | | #else |
118 | 0 | uint32_t a = *eax, b, c = *ecx, d; |
119 | 0 | __asm__("cpuid\n\t" : "+a"(a), "=b"(b), "+c"(c), "=d"(d)); |
120 | 0 | *eax = a; |
121 | 0 | *ebx = b; |
122 | 0 | *ecx = c; |
123 | 0 | *edx = d; |
124 | 0 | #endif |
125 | 0 | } Unexecuted instantiation: roaring.c:cpuid Unexecuted instantiation: roaring_array.c:cpuid Unexecuted instantiation: array_util.c:cpuid Unexecuted instantiation: array.c:cpuid Unexecuted instantiation: bitset.c:cpuid Unexecuted instantiation: containers.c:cpuid Unexecuted instantiation: convert.c:cpuid Unexecuted instantiation: mixed_intersection.c:cpuid Unexecuted instantiation: mixed_union.c:cpuid Unexecuted instantiation: mixed_equal.c:cpuid Unexecuted instantiation: mixed_subset.c:cpuid Unexecuted instantiation: mixed_negation.c:cpuid Unexecuted instantiation: mixed_xor.c:cpuid Unexecuted instantiation: mixed_andnot.c:cpuid Unexecuted instantiation: run.c:cpuid Unexecuted instantiation: bitset_util.c:cpuid |
126 | | |
127 | 0 | static inline uint32_t dynamic_croaring_detect_supported_architectures() { |
128 | 0 | uint32_t eax, ebx, ecx, edx; |
129 | 0 | uint32_t host_isa = 0x0; |
130 | | // Can be found on Intel ISA Reference for CPUID |
131 | 0 | static uint32_t cpuid_avx2_bit = 1 << 5; ///< @private Bit 5 of EBX for EAX=0x7 |
132 | 0 | static uint32_t cpuid_bmi1_bit = 1 << 3; ///< @private bit 3 of EBX for EAX=0x7 |
133 | 0 | static uint32_t cpuid_bmi2_bit = 1 << 8; ///< @private bit 8 of EBX for EAX=0x7 |
134 | 0 | static uint32_t cpuid_sse42_bit = 1 << 20; ///< @private bit 20 of ECX for EAX=0x1 |
135 | 0 | static uint32_t cpuid_pclmulqdq_bit = 1 << 1; ///< @private bit 1 of ECX for EAX=0x1 |
136 | | // ECX for EAX=0x7 |
137 | 0 | eax = 0x7; |
138 | 0 | ecx = 0x0; |
139 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
140 | 0 | if (ebx & cpuid_avx2_bit) { |
141 | 0 | host_isa |= CROARING_AVX2; |
142 | 0 | } |
143 | 0 | if (ebx & cpuid_bmi1_bit) { |
144 | 0 | host_isa |= CROARING_BMI1; |
145 | 0 | } |
146 | |
|
147 | 0 | if (ebx & cpuid_bmi2_bit) { |
148 | 0 | host_isa |= CROARING_BMI2; |
149 | 0 | } |
150 | | |
151 | | // EBX for EAX=0x1 |
152 | 0 | eax = 0x1; |
153 | 0 | cpuid(&eax, &ebx, &ecx, &edx); |
154 | |
|
155 | 0 | if (ecx & cpuid_sse42_bit) { |
156 | 0 | host_isa |= CROARING_SSE42; |
157 | 0 | } |
158 | |
|
159 | 0 | if (ecx & cpuid_pclmulqdq_bit) { |
160 | 0 | host_isa |= CROARING_PCLMULQDQ; |
161 | 0 | } |
162 | |
|
163 | 0 | return host_isa; |
164 | 0 | } Unexecuted instantiation: roaring.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: roaring_array.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: array_util.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: array.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: bitset.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: containers.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: convert.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: mixed_intersection.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: mixed_union.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: mixed_equal.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: mixed_subset.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: mixed_negation.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: mixed_xor.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: mixed_andnot.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: run.c:dynamic_croaring_detect_supported_architectures Unexecuted instantiation: bitset_util.c:dynamic_croaring_detect_supported_architectures |
165 | | #else // fallback |
166 | | |
167 | | |
168 | | //static inline uint32_t dynamic_croaring_detect_supported_architectures() { |
169 | | // return CROARING_DEFAULT; |
170 | | //} |
171 | | |
172 | | |
173 | | #endif // end SIMD extension detection code |
174 | | |
175 | | |
176 | | #if defined(__x86_64__) || defined(_M_AMD64) // x64 |
177 | | |
178 | | #if defined(__cplusplus) |
179 | | static inline uint32_t croaring_detect_supported_architectures() { |
180 | | // thread-safe as per the C++11 standard. |
181 | | static uint32_t buffer = dynamic_croaring_detect_supported_architectures(); |
182 | | return buffer; |
183 | | } |
184 | | #elif CROARING_VISUAL_STUDIO |
185 | | // Visual Studio does not support C11 atomics. |
186 | | static inline uint32_t croaring_detect_supported_architectures() { |
187 | | static int buffer = CROARING_UNINITIALIZED; |
188 | | if (buffer == CROARING_UNINITIALIZED) { |
189 | | buffer = dynamic_croaring_detect_supported_architectures(); |
190 | | } |
191 | | return buffer; |
192 | | } |
193 | | #else // CROARING_VISUAL_STUDIO |
194 | | #include <stdatomic.h> |
195 | 0 | static inline uint32_t croaring_detect_supported_architectures() { |
196 | | // we use an atomic for thread safety |
197 | 0 | static _Atomic uint32_t buffer = CROARING_UNINITIALIZED; |
198 | 0 | if (buffer == CROARING_UNINITIALIZED) { |
199 | | // atomicity is sufficient |
200 | 0 | buffer = dynamic_croaring_detect_supported_architectures(); |
201 | 0 | } |
202 | 0 | return buffer; |
203 | 0 | } Unexecuted instantiation: roaring.c:croaring_detect_supported_architectures Unexecuted instantiation: roaring_array.c:croaring_detect_supported_architectures Unexecuted instantiation: array_util.c:croaring_detect_supported_architectures Unexecuted instantiation: array.c:croaring_detect_supported_architectures Unexecuted instantiation: bitset.c:croaring_detect_supported_architectures Unexecuted instantiation: containers.c:croaring_detect_supported_architectures Unexecuted instantiation: convert.c:croaring_detect_supported_architectures Unexecuted instantiation: mixed_intersection.c:croaring_detect_supported_architectures Unexecuted instantiation: mixed_union.c:croaring_detect_supported_architectures Unexecuted instantiation: mixed_equal.c:croaring_detect_supported_architectures Unexecuted instantiation: mixed_subset.c:croaring_detect_supported_architectures Unexecuted instantiation: mixed_negation.c:croaring_detect_supported_architectures Unexecuted instantiation: mixed_xor.c:croaring_detect_supported_architectures Unexecuted instantiation: mixed_andnot.c:croaring_detect_supported_architectures Unexecuted instantiation: run.c:croaring_detect_supported_architectures Unexecuted instantiation: bitset_util.c:croaring_detect_supported_architectures |
204 | | #endif // CROARING_REGULAR_VISUAL_STUDIO |
205 | | |
206 | | #ifdef ROARING_DISABLE_AVX |
207 | | static inline bool croaring_avx2() { |
208 | | return false; |
209 | | } |
210 | | #elif defined(__AVX2__) |
211 | | static inline bool croaring_avx2() { |
212 | | return true; |
213 | | } |
214 | | #else |
215 | 0 | static inline bool croaring_avx2() { |
216 | 0 | return (croaring_detect_supported_architectures() & CROARING_AVX2) == CROARING_AVX2; |
217 | 0 | } Unexecuted instantiation: roaring.c:croaring_avx2 Unexecuted instantiation: roaring_array.c:croaring_avx2 Unexecuted instantiation: array_util.c:croaring_avx2 Unexecuted instantiation: array.c:croaring_avx2 Unexecuted instantiation: bitset.c:croaring_avx2 Unexecuted instantiation: containers.c:croaring_avx2 Unexecuted instantiation: convert.c:croaring_avx2 Unexecuted instantiation: mixed_intersection.c:croaring_avx2 Unexecuted instantiation: mixed_union.c:croaring_avx2 Unexecuted instantiation: mixed_equal.c:croaring_avx2 Unexecuted instantiation: mixed_subset.c:croaring_avx2 Unexecuted instantiation: mixed_negation.c:croaring_avx2 Unexecuted instantiation: mixed_xor.c:croaring_avx2 Unexecuted instantiation: mixed_andnot.c:croaring_avx2 Unexecuted instantiation: run.c:croaring_avx2 Unexecuted instantiation: bitset_util.c:croaring_avx2 |
218 | | #endif |
219 | | |
220 | | |
221 | | #else // defined(__x86_64__) || defined(_M_AMD64) // x64 |
222 | | |
223 | | //static inline bool croaring_avx2() { |
224 | | // return false; |
225 | | //} |
226 | | |
227 | | //static inline uint32_t croaring_detect_supported_architectures() { |
228 | | // // no runtime dispatch |
229 | | // return dynamic_croaring_detect_supported_architectures(); |
230 | | //} |
231 | | #endif // defined(__x86_64__) || defined(_M_AMD64) // x64 |
232 | | |
233 | | #endif // ROARING_ISADETECTION_H |