Bug 1708243 - Part 3: Use actor messaging for tabs.detectLanguage, stop loading Messa...
[gecko.git] / mozglue / build / SSE.h
blob8a2e6682475416ce41207926a90e083d25c341bb
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 #ifdef HAVE_CPUID_H
231 MOZ_EXPORT uint64_t xgetbv(uint32_t xcr);
232 #endif
234 #if defined(MOZILLA_PRESUME_MMX)
235 # define MOZILLA_MAY_SUPPORT_MMX 1
236 inline bool supports_mmx() { return true; }
237 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
238 # if !(defined(_MSC_VER) && defined(_M_AMD64))
239 // Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for
240 // AMD64, since that compiler doesn't support MMX.
241 # define MOZILLA_MAY_SUPPORT_MMX 1
242 # endif
243 inline bool supports_mmx() { return sse_private::mmx_enabled; }
244 #else
245 inline bool supports_mmx() { return false; }
246 #endif
248 #if defined(MOZILLA_PRESUME_SSE)
249 # define MOZILLA_MAY_SUPPORT_SSE 1
250 inline bool supports_sse() { return true; }
251 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
252 # define MOZILLA_MAY_SUPPORT_SSE 1
253 inline bool supports_sse() { return sse_private::sse_enabled; }
254 #else
255 inline bool supports_sse() { return false; }
256 #endif
258 #if defined(MOZILLA_PRESUME_SSE2)
259 # define MOZILLA_MAY_SUPPORT_SSE2 1
260 inline bool supports_sse2() { return true; }
261 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
262 # define MOZILLA_MAY_SUPPORT_SSE2 1
263 inline bool supports_sse2() { return sse_private::sse2_enabled; }
264 #else
265 inline bool supports_sse2() { return false; }
266 #endif
268 #if defined(MOZILLA_PRESUME_SSE3)
269 # define MOZILLA_MAY_SUPPORT_SSE3 1
270 inline bool supports_sse3() { return true; }
271 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
272 # define MOZILLA_MAY_SUPPORT_SSE3 1
273 inline bool supports_sse3() { return sse_private::sse3_enabled; }
274 #else
275 inline bool supports_sse3() { return false; }
276 #endif
278 #if defined(MOZILLA_PRESUME_SSSE3)
279 # define MOZILLA_MAY_SUPPORT_SSSE3 1
280 inline bool supports_ssse3() { return true; }
281 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
282 # define MOZILLA_MAY_SUPPORT_SSSE3 1
283 inline bool supports_ssse3() { return sse_private::ssse3_enabled; }
284 #else
285 inline bool supports_ssse3() { return false; }
286 #endif
288 #if defined(MOZILLA_PRESUME_SSE4A)
289 # define MOZILLA_MAY_SUPPORT_SSE4A 1
290 inline bool supports_sse4a() { return true; }
291 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
292 # define MOZILLA_MAY_SUPPORT_SSE4A 1
293 inline bool supports_sse4a() { return sse_private::sse4a_enabled; }
294 #else
295 inline bool supports_sse4a() { return false; }
296 #endif
298 #if defined(MOZILLA_PRESUME_SSE4_1)
299 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
300 inline bool supports_sse4_1() { return true; }
301 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
302 # define MOZILLA_MAY_SUPPORT_SSE4_1 1
303 inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; }
304 #else
305 inline bool supports_sse4_1() { return false; }
306 #endif
308 #if defined(MOZILLA_PRESUME_SSE4_2)
309 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
310 inline bool supports_sse4_2() { return true; }
311 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
312 # define MOZILLA_MAY_SUPPORT_SSE4_2 1
313 inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; }
314 #else
315 inline bool supports_sse4_2() { return false; }
316 #endif
318 #if defined(MOZILLA_PRESUME_AVX)
319 # define MOZILLA_MAY_SUPPORT_AVX 1
320 inline bool supports_avx() { return true; }
321 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
322 # define MOZILLA_MAY_SUPPORT_AVX 1
323 inline bool supports_avx() { return sse_private::avx_enabled; }
324 #else
325 inline bool supports_avx() { return false; }
326 #endif
328 #if defined(MOZILLA_PRESUME_AVX2)
329 # define MOZILLA_MAY_SUPPORT_AVX2 1
330 inline bool supports_avx2() { return true; }
331 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
332 # define MOZILLA_MAY_SUPPORT_AVX2 1
333 inline bool supports_avx2() { return sse_private::avx2_enabled; }
334 #else
335 inline bool supports_avx2() { return false; }
336 #endif
338 #if defined(MOZILLA_PRESUME_AES)
339 # define MOZILLA_MAY_SUPPORT_AES 1
340 inline bool supports_aes() { return true; }
341 #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
342 # define MOZILLA_MAY_SUPPORT_AES 1
343 inline bool supports_aes() { return sse_private::aes_enabled; }
344 #else
345 inline bool supports_aes() { return false; }
346 #endif
348 } // namespace mozilla
350 #endif /* !defined(mozilla_SSE_h_) */