Bug 1842999 - Part 25: Support testing elements are present in resizable typed arrays...
[gecko.git] / mozglue / misc / SSE.h
blobd7c7e4ae973acaf23224980faaaa5cdb4aff352c
1 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* compile-time and runtime tests for whether to use SSE instructions */
8 #ifndef mozilla_SSE_h_
9 #define mozilla_SSE_h_
11 // for definition of MFBT_DATA
12 #include "mozilla/Types.h"
14 /**
15 * The public interface of this header consists of a set of macros and
16 * functions for Intel CPU features.
18 * DETECTING ISA EXTENSIONS
19 * ========================
21 * This header provides the following functions for determining whether the
22 * current CPU supports a particular instruction set extension:
24 * mozilla::supports_mmx
25 * mozilla::supports_sse
26 * mozilla::supports_sse2
27 * mozilla::supports_sse3
28 * mozilla::supports_ssse3
29 * mozilla::supports_sse4a
30 * mozilla::supports_sse4_1
31 * mozilla::supports_sse4_2
32 * mozilla::supports_avx
33 * mozilla::supports_avx2
34 * mozilla::supports_aes
35 * mozilla::has_constant_tsc
37 * If you're writing code using inline assembly, you should guard it with a
38 * call to one of these functions. For instance:
40 * if (mozilla::supports_sse2()) {
41 * asm(" ... ");
42 * }
43 * else {
44 * ...
45 * }
47 * Note that these functions depend on cpuid intrinsics only available in gcc
48 * 4.3 or later and MSVC 8.0 (Visual C++ 2005) or later, so they return false
49 * in older compilers. (This could be fixed by replacing the code with inline
50 * assembly.)
53 * USING INTRINSICS
54 * ================
56 * This header also provides support for coding using CPU intrinsics.
58 * For each mozilla::supports_abc function, we define a MOZILLA_MAY_SUPPORT_ABC
59 * macro which indicates that the target/compiler combination we're using is
60 * compatible with the ABC extension. For instance, x86_64 with MSVC 2003 is
61 * compatible with SSE2 but not SSE3, since although there exist x86_64 CPUs
62 * with SSE3 support, MSVC 2003 only supports through SSE2.
64 * Until gcc fixes #pragma target [1] [2] or our x86 builds require SSE2,
65 * you'll need to separate code using intrinsics into a file separate from your
66 * regular code. Here's the recommended pattern:
68 * #ifdef MOZILLA_MAY_SUPPORT_ABC
69 * namespace mozilla {
70 * namespace ABC {
71 * void foo();
72 * }
73 * }
74 * #endif
76 * void foo() {
77 * #ifdef MOZILLA_MAY_SUPPORT_ABC
78 * if (mozilla::supports_abc()) {
79 * mozilla::ABC::foo(); // in a separate file
80 * return;
81 * }
82 * #endif
84 * foo_unvectorized();
85 * }
87 * You'll need to define mozilla::ABC::foo() in a separate file and add the
88 * -mabc flag when using gcc.
90 * [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39787 and
91 * [2] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41201 being fixed.
95 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
97 # ifdef __MMX__
98 // It's ok to use MMX instructions based on the -march option (or
99 // the default for x86_64 or for Intel Mac).
100 # define MOZILLA_PRESUME_MMX 1
101 # endif
102 # ifdef __SSE__
103 // It's ok to use SSE instructions based on the -march option (or
104 // the default for x86_64 or for Intel Mac).
105 # define MOZILLA_PRESUME_SSE 1
106 # endif
107 # ifdef __SSE2__
108 // It's ok to use SSE2 instructions based on the -march option (or
109 // the default for x86_64 or for Intel Mac).
110 # define MOZILLA_PRESUME_SSE2 1
111 # endif
112 # ifdef __SSE3__
113 // It's ok to use SSE3 instructions based on the -march option (or the
114 // default for Intel Mac).
115 # define MOZILLA_PRESUME_SSE3 1
116 # endif
117 # ifdef __SSSE3__
118 // It's ok to use SSSE3 instructions based on the -march option.
119 # define MOZILLA_PRESUME_SSSE3 1
120 # endif
121 # ifdef __SSE4A__
122 // It's ok to use SSE4A instructions based on the -march option.
123 # define MOZILLA_PRESUME_SSE4A 1
124 # endif
125 # ifdef __SSE4_1__
126 // It's ok to use SSE4.1 instructions based on the -march option.
127 # define MOZILLA_PRESUME_SSE4_1 1
128 # endif
129 # ifdef __SSE4_2__
130 // It's ok to use SSE4.2 instructions based on the -march option.
131 # define MOZILLA_PRESUME_SSE4_2 1
132 # endif
133 # ifdef __AVX__
134 // It's ok to use AVX instructions based on the -march option.
135 # define MOZILLA_PRESUME_AVX 1
136 # endif
137 # ifdef __AVX2__
138 // It's ok to use AVX instructions based on the -march option.
139 # define MOZILLA_PRESUME_AVX2 1
140 # endif
141 # ifdef __AVXVNNI__
142 // It's ok to use AVX instructions based on the -march option.
143 # define MOZILLA_PRESUME_AVXVNNI 1
144 # endif
145 # ifdef __AES__
146 // It's ok to use AES instructions based on the -march option.
147 # define MOZILLA_PRESUME_AES 1
148 # endif
150 # ifdef HAVE_CPUID_H
151 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
152 # endif
154 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
156 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
158 # if defined(_M_IX86_FP)
160 # if _M_IX86_FP >= 1
161 // It's ok to use SSE instructions based on the /arch option
162 # define MOZILLA_PRESUME_SSE
163 # endif
164 # if _M_IX86_FP >= 2
165 // It's ok to use SSE2 instructions based on the /arch option
166 # define MOZILLA_PRESUME_SSE2
167 # endif
169 # elif defined(_M_AMD64)
170 // MSVC for AMD64 doesn't support MMX, so don't presume it here.
172 // SSE is always available on AMD64.
173 # define MOZILLA_PRESUME_SSE
174 // SSE2 is always available on AMD64.
175 # define MOZILLA_PRESUME_SSE2
176 # endif
178 #elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__))
179 // Sun Studio on x86 or amd64
181 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
183 # if defined(__x86_64__)
184 // MMX is always available on AMD64.
185 # define MOZILLA_PRESUME_MMX
186 // SSE is always available on AMD64.
187 # define MOZILLA_PRESUME_SSE
188 // SSE2 is always available on AMD64.
189 # define MOZILLA_PRESUME_SSE2
190 # endif
192 #endif
194 namespace mozilla {
196 namespace sse_private {
197 #if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
198 # if !defined(MOZILLA_PRESUME_MMX)
199 extern bool MFBT_DATA mmx_enabled;
200 # endif
201 # if !defined(MOZILLA_PRESUME_SSE)
202 extern bool MFBT_DATA sse_enabled;
203 # endif
204 # if !defined(MOZILLA_PRESUME_SSE2)
205 extern bool MFBT_DATA sse2_enabled;
206 # endif
207 # if !defined(MOZILLA_PRESUME_SSE3)
208 extern bool MFBT_DATA sse3_enabled;
209 # endif
210 # if !defined(MOZILLA_PRESUME_SSSE3)
211 extern bool MFBT_DATA ssse3_enabled;
212 # endif
213 # if !defined(MOZILLA_PRESUME_SSE4A)
214 extern bool MFBT_DATA sse4a_enabled;
215 # endif
216 # if !defined(MOZILLA_PRESUME_SSE4_1)
217 extern bool MFBT_DATA sse4_1_enabled;
218 # endif
219 # if !defined(MOZILLA_PRESUME_SSE4_2)
220 extern bool MFBT_DATA sse4_2_enabled;
221 # endif
222 # if !defined(MOZILLA_PRESUME_FMA3)
223 extern bool MFBT_DATA fma3_enabled;
224 # endif
225 # if !defined(MOZILLA_PRESUME_AVX)
226 extern bool MFBT_DATA avx_enabled;
227 # endif
228 # if !defined(MOZILLA_PRESUME_AVX2)
229 extern bool MFBT_DATA avx2_enabled;
230 # endif
231 # if !defined(MOZILLA_PRESUME_AVXVNNI)
232 extern bool MFBT_DATA avxvnni_enabled;
233 # endif
234 # if !defined(MOZILLA_PRESUME_AES)
235 extern bool MFBT_DATA aes_enabled;
236 # endif
237 extern bool MFBT_DATA has_constant_tsc;
239 #endif
240 } // namespace sse_private
242 #ifdef HAVE_CPUID_H
243 MOZ_EXPORT uint64_t xgetbv(uint32_t xcr);
244 #endif
246 #if defined(MOZILLA_PRESUME_MMX)
247 # define MOZILLA_MAY_SUPPORT_MMX 1
248 inline bool supports_mmx() { return true; }
249 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
250 # if !(defined(_MSC_VER) && defined(_M_AMD64))
251 // Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for
252 // AMD64, since that compiler doesn't support MMX.
253 # define MOZILLA_MAY_SUPPORT_MMX 1
254 # endif
255 inline bool supports_mmx() { return sse_private::mmx_enabled; }
256 #else
257 inline bool supports_mmx() { return false; }
258 #endif
260 #if defined(MOZILLA_PRESUME_SSE)
261 # define MOZILLA_MAY_SUPPORT_SSE 1
262 inline bool supports_sse() { return true; }
263 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
264 # define MOZILLA_MAY_SUPPORT_SSE 1
265 inline bool supports_sse() { return sse_private::sse_enabled; }
266 #else
267 inline bool supports_sse() { return false; }
268 #endif
270 #if defined(MOZILLA_PRESUME_SSE2)
271 # define MOZILLA_MAY_SUPPORT_SSE2 1
272 inline bool supports_sse2() { return true; }
273 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
274 # define MOZILLA_MAY_SUPPORT_SSE2 1
275 inline bool supports_sse2() { return sse_private::sse2_enabled; }
276 #else
277 inline bool supports_sse2() { return false; }
278 #endif
280 #if defined(MOZILLA_PRESUME_SSE3)
281 # define MOZILLA_MAY_SUPPORT_SSE3 1
282 inline bool supports_sse3() { return true; }
283 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
284 # define MOZILLA_MAY_SUPPORT_SSE3 1
285 inline bool supports_sse3() { return sse_private::sse3_enabled; }
286 #else
287 inline bool supports_sse3() { return false; }
288 #endif
290 #if defined(MOZILLA_PRESUME_SSSE3)
291 # define MOZILLA_MAY_SUPPORT_SSSE3 1
292 inline bool supports_ssse3() { return true; }
293 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
294 # define MOZILLA_MAY_SUPPORT_SSSE3 1
295 inline bool supports_ssse3() { return sse_private::ssse3_enabled; }
296 #else
297 inline bool supports_ssse3() { return false; }
298 #endif
300 #if defined(MOZILLA_PRESUME_SSE4A)
301 # define MOZILLA_MAY_SUPPORT_SSE4A 1
302 inline bool supports_sse4a() { return true; }
303 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
304 # define MOZILLA_MAY_SUPPORT_SSE4A 1
305 inline bool supports_sse4a() { return sse_private::sse4a_enabled; }
306 #else
307 inline bool supports_sse4a() { return false; }
308 #endif
310 #if defined(MOZILLA_PRESUME_SSE4_1)
311 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
312 inline bool supports_sse4_1() { return true; }
313 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
314 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
315 inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; }
316 #else
317 inline bool supports_sse4_1() { return false; }
318 #endif
320 #if defined(MOZILLA_PRESUME_SSE4_2)
321 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
322 inline bool supports_sse4_2() { return true; }
323 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
324 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
325 inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; }
326 #else
327 inline bool supports_sse4_2() { return false; }
328 #endif
330 #if defined(MOZILLA_PRESUME_FMA3)
331 # define MOZILLA_MAY_SUPPORT_FMA3 1
332 inline bool supports_fma3() { return true; }
333 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
334 # define MOZILLA_MAY_SUPPORT_FMA3 1
335 inline bool supports_fma3() { return sse_private::fma3_enabled; }
336 #else
337 inline bool supports_fma3() { return false; }
338 #endif
340 #if defined(MOZILLA_PRESUME_AVX)
341 # define MOZILLA_MAY_SUPPORT_AVX 1
342 inline bool supports_avx() { return true; }
343 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
344 # define MOZILLA_MAY_SUPPORT_AVX 1
345 inline bool supports_avx() { return sse_private::avx_enabled; }
346 #else
347 inline bool supports_avx() { return false; }
348 #endif
350 #if defined(MOZILLA_PRESUME_AVX2)
351 # define MOZILLA_MAY_SUPPORT_AVX2 1
352 inline bool supports_avx2() { return true; }
353 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
354 # define MOZILLA_MAY_SUPPORT_AVX2 1
355 inline bool supports_avx2() { return sse_private::avx2_enabled; }
356 #else
357 inline bool supports_avx2() { return false; }
358 #endif
360 #if defined(MOZILLA_PRESUME_AVXVNNI)
361 # define MOZILLA_MAY_SUPPORT_AVXVNNI 1
362 inline bool supports_avxvnni() { return true; }
363 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
364 # define MOZILLA_MAY_SUPPORT_AVXVNNI 1
365 inline bool supports_avxvnni() { return sse_private::avxvnni_enabled; }
366 #else
367 inline bool supports_avxvnni() { return false; }
368 #endif
370 #if defined(MOZILLA_PRESUME_AES)
371 # define MOZILLA_MAY_SUPPORT_AES 1
372 inline bool supports_aes() { return true; }
373 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
374 # define MOZILLA_MAY_SUPPORT_AES 1
375 inline bool supports_aes() { return sse_private::aes_enabled; }
376 #else
377 inline bool supports_aes() { return false; }
378 #endif
380 #ifdef MOZILLA_SSE_HAVE_CPUID_DETECTION
381 inline bool has_constant_tsc() { return sse_private::has_constant_tsc; }
382 #else
383 inline bool has_constant_tsc() { return false; }
384 #endif
386 } // namespace mozilla
388 #endif /* !defined(mozilla_SSE_h_) */