Bug 1523562 [wpt PR 14417] - Update RTCPeerConnection-helper.js, a=testonly
[gecko.git] / mozglue / build / SSE.h
blobbd618c4680b1bed653e329d4b60cca9387fcb865
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
36 * If you're writing code using inline assembly, you should guard it with a
37 * call to one of these functions. For instance:
39 * if (mozilla::supports_sse2()) {
40 * asm(" ... ");
41 * }
42 * else {
43 * ...
44 * }
46 * Note that these functions depend on cpuid intrinsics only available in gcc
47 * 4.3 or later and MSVC 8.0 (Visual C++ 2005) or later, so they return false
48 * in older compilers. (This could be fixed by replacing the code with inline
49 * assembly.)
52 * USING INTRINSICS
53 * ================
55 * This header also provides support for coding using CPU intrinsics.
57 * For each mozilla::supports_abc function, we define a MOZILLA_MAY_SUPPORT_ABC
58 * macro which indicates that the target/compiler combination we're using is
59 * compatible with the ABC extension. For instance, x86_64 with MSVC 2003 is
60 * compatible with SSE2 but not SSE3, since although there exist x86_64 CPUs
61 * with SSE3 support, MSVC 2003 only supports through SSE2.
63 * Until gcc fixes #pragma target [1] [2] or our x86 builds require SSE2,
64 * you'll need to separate code using intrinsics into a file separate from your
65 * regular code. Here's the recommended pattern:
67 * #ifdef MOZILLA_MAY_SUPPORT_ABC
68 * namespace mozilla {
69 * namespace ABC {
70 * void foo();
71 * }
72 * }
73 * #endif
75 * void foo() {
76 * #ifdef MOZILLA_MAY_SUPPORT_ABC
77 * if (mozilla::supports_abc()) {
78 * mozilla::ABC::foo(); // in a separate file
79 * return;
80 * }
81 * #endif
83 * foo_unvectorized();
84 * }
86 * You'll need to define mozilla::ABC::foo() in a separate file and add the
87 * -mabc flag when using gcc.
89 * [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39787 and
90 * [2] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41201 being fixed.
94 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
96 # ifdef __MMX__
97 // It's ok to use MMX instructions based on the -march option (or
98 // the default for x86_64 or for Intel Mac).
99 # define MOZILLA_PRESUME_MMX 1
100 # endif
101 # ifdef __SSE__
102 // It's ok to use SSE instructions based on the -march option (or
103 // the default for x86_64 or for Intel Mac).
104 # define MOZILLA_PRESUME_SSE 1
105 # endif
106 # ifdef __SSE2__
107 // It's ok to use SSE2 instructions based on the -march option (or
108 // the default for x86_64 or for Intel Mac).
109 # define MOZILLA_PRESUME_SSE2 1
110 # endif
111 # ifdef __SSE3__
112 // It's ok to use SSE3 instructions based on the -march option (or the
113 // default for Intel Mac).
114 # define MOZILLA_PRESUME_SSE3 1
115 # endif
116 # ifdef __SSSE3__
117 // It's ok to use SSSE3 instructions based on the -march option.
118 # define MOZILLA_PRESUME_SSSE3 1
119 # endif
120 # ifdef __SSE4A__
121 // It's ok to use SSE4A instructions based on the -march option.
122 # define MOZILLA_PRESUME_SSE4A 1
123 # endif
124 # ifdef __SSE4_1__
125 // It's ok to use SSE4.1 instructions based on the -march option.
126 # define MOZILLA_PRESUME_SSE4_1 1
127 # endif
128 # ifdef __SSE4_2__
129 // It's ok to use SSE4.2 instructions based on the -march option.
130 # define MOZILLA_PRESUME_SSE4_2 1
131 # endif
132 # ifdef __AVX__
133 // It's ok to use AVX instructions based on the -march option.
134 # define MOZILLA_PRESUME_AVX 1
135 # endif
136 # ifdef __AVX2__
137 // It's ok to use AVX instructions based on the -march option.
138 # define MOZILLA_PRESUME_AVX2 1
139 # endif
140 # ifdef __AES__
141 // It's ok to use AES instructions based on the -march option.
142 # define MOZILLA_PRESUME_AES 1
143 # endif
145 # ifdef HAVE_CPUID_H
146 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
147 # endif
149 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
151 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
153 # if defined(_M_IX86_FP)
155 # if _M_IX86_FP >= 1
156 // It's ok to use SSE instructions based on the /arch option
157 # define MOZILLA_PRESUME_SSE
158 # endif
159 # if _M_IX86_FP >= 2
160 // It's ok to use SSE2 instructions based on the /arch option
161 # define MOZILLA_PRESUME_SSE2
162 # endif
164 # elif defined(_M_AMD64)
165 // MSVC for AMD64 doesn't support MMX, so don't presume it here.
167 // SSE is always available on AMD64.
168 # define MOZILLA_PRESUME_SSE
169 // SSE2 is always available on AMD64.
170 # define MOZILLA_PRESUME_SSE2
171 # endif
173 #elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__))
174 // Sun Studio on x86 or amd64
176 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
178 # if defined(__x86_64__)
179 // MMX is always available on AMD64.
180 # define MOZILLA_PRESUME_MMX
181 // SSE is always available on AMD64.
182 # define MOZILLA_PRESUME_SSE
183 // SSE2 is always available on AMD64.
184 # define MOZILLA_PRESUME_SSE2
185 # endif
187 #endif
189 namespace mozilla {
191 namespace sse_private {
192 #if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
193 # if !defined(MOZILLA_PRESUME_MMX)
194 extern bool MFBT_DATA mmx_enabled;
195 # endif
196 # if !defined(MOZILLA_PRESUME_SSE)
197 extern bool MFBT_DATA sse_enabled;
198 # endif
199 # if !defined(MOZILLA_PRESUME_SSE2)
200 extern bool MFBT_DATA sse2_enabled;
201 # endif
202 # if !defined(MOZILLA_PRESUME_SSE3)
203 extern bool MFBT_DATA sse3_enabled;
204 # endif
205 # if !defined(MOZILLA_PRESUME_SSSE3)
206 extern bool MFBT_DATA ssse3_enabled;
207 # endif
208 # if !defined(MOZILLA_PRESUME_SSE4A)
209 extern bool MFBT_DATA sse4a_enabled;
210 # endif
211 # if !defined(MOZILLA_PRESUME_SSE4_1)
212 extern bool MFBT_DATA sse4_1_enabled;
213 # endif
214 # if !defined(MOZILLA_PRESUME_SSE4_2)
215 extern bool MFBT_DATA sse4_2_enabled;
216 # endif
217 # if !defined(MOZILLA_PRESUME_AVX)
218 extern bool MFBT_DATA avx_enabled;
219 # endif
220 # if !defined(MOZILLA_PRESUME_AVX2)
221 extern bool MFBT_DATA avx2_enabled;
222 # endif
223 # if !defined(MOZILLA_PRESUME_AES)
224 extern bool MFBT_DATA aes_enabled;
225 # endif
227 #endif
228 } // namespace sse_private
230 #if defined(MOZILLA_PRESUME_MMX)
231 # define MOZILLA_MAY_SUPPORT_MMX 1
232 inline bool supports_mmx() { return true; }
233 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
234 # if !(defined(_MSC_VER) && defined(_M_AMD64))
235 // Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for
236 // AMD64, since that compiler doesn't support MMX.
237 # define MOZILLA_MAY_SUPPORT_MMX 1
238 # endif
239 inline bool supports_mmx() { return sse_private::mmx_enabled; }
240 #else
241 inline bool supports_mmx() { return false; }
242 #endif
244 #if defined(MOZILLA_PRESUME_SSE)
245 # define MOZILLA_MAY_SUPPORT_SSE 1
246 inline bool supports_sse() { return true; }
247 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
248 # define MOZILLA_MAY_SUPPORT_SSE 1
249 inline bool supports_sse() { return sse_private::sse_enabled; }
250 #else
251 inline bool supports_sse() { return false; }
252 #endif
254 #if defined(MOZILLA_PRESUME_SSE2)
255 # define MOZILLA_MAY_SUPPORT_SSE2 1
256 inline bool supports_sse2() { return true; }
257 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
258 # define MOZILLA_MAY_SUPPORT_SSE2 1
259 inline bool supports_sse2() { return sse_private::sse2_enabled; }
260 #else
261 inline bool supports_sse2() { return false; }
262 #endif
264 #if defined(MOZILLA_PRESUME_SSE3)
265 # define MOZILLA_MAY_SUPPORT_SSE3 1
266 inline bool supports_sse3() { return true; }
267 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
268 # define MOZILLA_MAY_SUPPORT_SSE3 1
269 inline bool supports_sse3() { return sse_private::sse3_enabled; }
270 #else
271 inline bool supports_sse3() { return false; }
272 #endif
274 #if defined(MOZILLA_PRESUME_SSSE3)
275 # define MOZILLA_MAY_SUPPORT_SSSE3 1
276 inline bool supports_ssse3() { return true; }
277 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
278 # define MOZILLA_MAY_SUPPORT_SSSE3 1
279 inline bool supports_ssse3() { return sse_private::ssse3_enabled; }
280 #else
281 inline bool supports_ssse3() { return false; }
282 #endif
284 #if defined(MOZILLA_PRESUME_SSE4A)
285 # define MOZILLA_MAY_SUPPORT_SSE4A 1
286 inline bool supports_sse4a() { return true; }
287 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
288 # define MOZILLA_MAY_SUPPORT_SSE4A 1
289 inline bool supports_sse4a() { return sse_private::sse4a_enabled; }
290 #else
291 inline bool supports_sse4a() { return false; }
292 #endif
294 #if defined(MOZILLA_PRESUME_SSE4_1)
295 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
296 inline bool supports_sse4_1() { return true; }
297 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
298 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
299 inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; }
300 #else
301 inline bool supports_sse4_1() { return false; }
302 #endif
304 #if defined(MOZILLA_PRESUME_SSE4_2)
305 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
306 inline bool supports_sse4_2() { return true; }
307 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
308 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
309 inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; }
310 #else
311 inline bool supports_sse4_2() { return false; }
312 #endif
314 #if defined(MOZILLA_PRESUME_AVX)
315 # define MOZILLA_MAY_SUPPORT_AVX 1
316 inline bool supports_avx() { return true; }
317 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
318 # define MOZILLA_MAY_SUPPORT_AVX 1
319 inline bool supports_avx() { return sse_private::avx_enabled; }
320 #else
321 inline bool supports_avx() { return false; }
322 #endif
324 #if defined(MOZILLA_PRESUME_AVX2)
325 # define MOZILLA_MAY_SUPPORT_AVX2 1
326 inline bool supports_avx2() { return true; }
327 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
328 # define MOZILLA_MAY_SUPPORT_AVX2 1
329 inline bool supports_avx2() { return sse_private::avx2_enabled; }
330 #else
331 inline bool supports_avx2() { return false; }
332 #endif
334 #if defined(MOZILLA_PRESUME_AES)
335 # define MOZILLA_MAY_SUPPORT_AES 1
336 inline bool supports_aes() { return true; }
337 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
338 # define MOZILLA_MAY_SUPPORT_AES 1
339 inline bool supports_aes() { return sse_private::aes_enabled; }
340 #else
341 inline bool supports_aes() { return false; }
342 #endif
344 } // namespace mozilla
346 #endif /* !defined(mozilla_SSE_h_) */