Bug 1860823 [wpt PR 42716] - Update wpt metadata, a=testonly
[gecko.git] / mozglue / misc / SSE.h
blob0b87366a80433b26ecc7a30ce07f0d14355227ca
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 __AES__
142 // It's ok to use AES instructions based on the -march option.
143 # define MOZILLA_PRESUME_AES 1
144 # endif
146 # ifdef HAVE_CPUID_H
147 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
148 # endif
150 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
152 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
154 # if defined(_M_IX86_FP)
156 # if _M_IX86_FP >= 1
157 // It's ok to use SSE instructions based on the /arch option
158 # define MOZILLA_PRESUME_SSE
159 # endif
160 # if _M_IX86_FP >= 2
161 // It's ok to use SSE2 instructions based on the /arch option
162 # define MOZILLA_PRESUME_SSE2
163 # endif
165 # elif defined(_M_AMD64)
166 // MSVC for AMD64 doesn't support MMX, so don't presume it here.
168 // SSE is always available on AMD64.
169 # define MOZILLA_PRESUME_SSE
170 // SSE2 is always available on AMD64.
171 # define MOZILLA_PRESUME_SSE2
172 # endif
174 #elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__))
175 // Sun Studio on x86 or amd64
177 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
179 # if defined(__x86_64__)
180 // MMX is always available on AMD64.
181 # define MOZILLA_PRESUME_MMX
182 // SSE is always available on AMD64.
183 # define MOZILLA_PRESUME_SSE
184 // SSE2 is always available on AMD64.
185 # define MOZILLA_PRESUME_SSE2
186 # endif
188 #endif
190 namespace mozilla {
192 namespace sse_private {
193 #if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
194 # if !defined(MOZILLA_PRESUME_MMX)
195 extern bool MFBT_DATA mmx_enabled;
196 # endif
197 # if !defined(MOZILLA_PRESUME_SSE)
198 extern bool MFBT_DATA sse_enabled;
199 # endif
200 # if !defined(MOZILLA_PRESUME_SSE2)
201 extern bool MFBT_DATA sse2_enabled;
202 # endif
203 # if !defined(MOZILLA_PRESUME_SSE3)
204 extern bool MFBT_DATA sse3_enabled;
205 # endif
206 # if !defined(MOZILLA_PRESUME_SSSE3)
207 extern bool MFBT_DATA ssse3_enabled;
208 # endif
209 # if !defined(MOZILLA_PRESUME_SSE4A)
210 extern bool MFBT_DATA sse4a_enabled;
211 # endif
212 # if !defined(MOZILLA_PRESUME_SSE4_1)
213 extern bool MFBT_DATA sse4_1_enabled;
214 # endif
215 # if !defined(MOZILLA_PRESUME_SSE4_2)
216 extern bool MFBT_DATA sse4_2_enabled;
217 # endif
218 # if !defined(MOZILLA_PRESUME_FMA3)
219 extern bool MFBT_DATA fma3_enabled;
220 # endif
221 # if !defined(MOZILLA_PRESUME_AVX)
222 extern bool MFBT_DATA avx_enabled;
223 # endif
224 # if !defined(MOZILLA_PRESUME_AVX2)
225 extern bool MFBT_DATA avx2_enabled;
226 # endif
227 # if !defined(MOZILLA_PRESUME_AES)
228 extern bool MFBT_DATA aes_enabled;
229 # endif
230 extern bool MFBT_DATA has_constant_tsc;
232 #endif
233 } // namespace sse_private
235 #ifdef HAVE_CPUID_H
236 MOZ_EXPORT uint64_t xgetbv(uint32_t xcr);
237 #endif
239 #if defined(MOZILLA_PRESUME_MMX)
240 # define MOZILLA_MAY_SUPPORT_MMX 1
241 inline bool supports_mmx() { return true; }
242 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
243 # if !(defined(_MSC_VER) && defined(_M_AMD64))
244 // Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for
245 // AMD64, since that compiler doesn't support MMX.
246 # define MOZILLA_MAY_SUPPORT_MMX 1
247 # endif
248 inline bool supports_mmx() { return sse_private::mmx_enabled; }
249 #else
250 inline bool supports_mmx() { return false; }
251 #endif
253 #if defined(MOZILLA_PRESUME_SSE)
254 # define MOZILLA_MAY_SUPPORT_SSE 1
255 inline bool supports_sse() { return true; }
256 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
257 # define MOZILLA_MAY_SUPPORT_SSE 1
258 inline bool supports_sse() { return sse_private::sse_enabled; }
259 #else
260 inline bool supports_sse() { return false; }
261 #endif
263 #if defined(MOZILLA_PRESUME_SSE2)
264 # define MOZILLA_MAY_SUPPORT_SSE2 1
265 inline bool supports_sse2() { return true; }
266 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
267 # define MOZILLA_MAY_SUPPORT_SSE2 1
268 inline bool supports_sse2() { return sse_private::sse2_enabled; }
269 #else
270 inline bool supports_sse2() { return false; }
271 #endif
273 #if defined(MOZILLA_PRESUME_SSE3)
274 # define MOZILLA_MAY_SUPPORT_SSE3 1
275 inline bool supports_sse3() { return true; }
276 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
277 # define MOZILLA_MAY_SUPPORT_SSE3 1
278 inline bool supports_sse3() { return sse_private::sse3_enabled; }
279 #else
280 inline bool supports_sse3() { return false; }
281 #endif
283 #if defined(MOZILLA_PRESUME_SSSE3)
284 # define MOZILLA_MAY_SUPPORT_SSSE3 1
285 inline bool supports_ssse3() { return true; }
286 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
287 # define MOZILLA_MAY_SUPPORT_SSSE3 1
288 inline bool supports_ssse3() { return sse_private::ssse3_enabled; }
289 #else
290 inline bool supports_ssse3() { return false; }
291 #endif
293 #if defined(MOZILLA_PRESUME_SSE4A)
294 # define MOZILLA_MAY_SUPPORT_SSE4A 1
295 inline bool supports_sse4a() { return true; }
296 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
297 # define MOZILLA_MAY_SUPPORT_SSE4A 1
298 inline bool supports_sse4a() { return sse_private::sse4a_enabled; }
299 #else
300 inline bool supports_sse4a() { return false; }
301 #endif
303 #if defined(MOZILLA_PRESUME_SSE4_1)
304 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
305 inline bool supports_sse4_1() { return true; }
306 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
307 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
308 inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; }
309 #else
310 inline bool supports_sse4_1() { return false; }
311 #endif
313 #if defined(MOZILLA_PRESUME_SSE4_2)
314 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
315 inline bool supports_sse4_2() { return true; }
316 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
317 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
318 inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; }
319 #else
320 inline bool supports_sse4_2() { return false; }
321 #endif
323 #if defined(MOZILLA_PRESUME_FMA3)
324 # define MOZILLA_MAY_SUPPORT_FMA3 1
325 inline bool supports_fma3() { return true; }
326 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
327 # define MOZILLA_MAY_SUPPORT_FMA3 1
328 inline bool supports_fma3() { return sse_private::fma3_enabled; }
329 #else
330 inline bool supports_fma3() { return false; }
331 #endif
333 #if defined(MOZILLA_PRESUME_AVX)
334 # define MOZILLA_MAY_SUPPORT_AVX 1
335 inline bool supports_avx() { return true; }
336 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
337 # define MOZILLA_MAY_SUPPORT_AVX 1
338 inline bool supports_avx() { return sse_private::avx_enabled; }
339 #else
340 inline bool supports_avx() { return false; }
341 #endif
343 #if defined(MOZILLA_PRESUME_AVX2)
344 # define MOZILLA_MAY_SUPPORT_AVX2 1
345 inline bool supports_avx2() { return true; }
346 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
347 # define MOZILLA_MAY_SUPPORT_AVX2 1
348 inline bool supports_avx2() { return sse_private::avx2_enabled; }
349 #else
350 inline bool supports_avx2() { return false; }
351 #endif
353 #if defined(MOZILLA_PRESUME_AES)
354 # define MOZILLA_MAY_SUPPORT_AES 1
355 inline bool supports_aes() { return true; }
356 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
357 # define MOZILLA_MAY_SUPPORT_AES 1
358 inline bool supports_aes() { return sse_private::aes_enabled; }
359 #else
360 inline bool supports_aes() { return false; }
361 #endif
363 #ifdef MOZILLA_SSE_HAVE_CPUID_DETECTION
364 inline bool has_constant_tsc() { return sse_private::has_constant_tsc; }
365 #else
366 inline bool has_constant_tsc() { return false; }
367 #endif
369 } // namespace mozilla
371 #endif /* !defined(mozilla_SSE_h_) */