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 */
11 // for definition of MFBT_DATA
12 #include "mozilla/Types.h"
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()) {
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
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
76 * #ifdef MOZILLA_MAY_SUPPORT_ABC
77 * if (mozilla::supports_abc()) {
78 * mozilla::ABC::foo(); // in a separate file
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__))
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
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
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
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
117 // It's ok to use SSSE3 instructions based on the -march option.
118 # define MOZILLA_PRESUME_SSSE3 1
121 // It's ok to use SSE4A instructions based on the -march option.
122 # define MOZILLA_PRESUME_SSE4A 1
125 // It's ok to use SSE4.1 instructions based on the -march option.
126 # define MOZILLA_PRESUME_SSE4_1 1
129 // It's ok to use SSE4.2 instructions based on the -march option.
130 # define MOZILLA_PRESUME_SSE4_2 1
133 // It's ok to use AVX instructions based on the -march option.
134 # define MOZILLA_PRESUME_AVX 1
137 // It's ok to use AVX instructions based on the -march option.
138 # define MOZILLA_PRESUME_AVX2 1
141 // It's ok to use AES instructions based on the -march option.
142 # define MOZILLA_PRESUME_AES 1
146 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
149 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
151 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
153 # if defined(_M_IX86_FP)
156 // It's ok to use SSE instructions based on the /arch option
157 # define MOZILLA_PRESUME_SSE
160 // It's ok to use SSE2 instructions based on the /arch option
161 # define MOZILLA_PRESUME_SSE2
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
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
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
;
196 # if !defined(MOZILLA_PRESUME_SSE)
197 extern bool MFBT_DATA sse_enabled
;
199 # if !defined(MOZILLA_PRESUME_SSE2)
200 extern bool MFBT_DATA sse2_enabled
;
202 # if !defined(MOZILLA_PRESUME_SSE3)
203 extern bool MFBT_DATA sse3_enabled
;
205 # if !defined(MOZILLA_PRESUME_SSSE3)
206 extern bool MFBT_DATA ssse3_enabled
;
208 # if !defined(MOZILLA_PRESUME_SSE4A)
209 extern bool MFBT_DATA sse4a_enabled
;
211 # if !defined(MOZILLA_PRESUME_SSE4_1)
212 extern bool MFBT_DATA sse4_1_enabled
;
214 # if !defined(MOZILLA_PRESUME_SSE4_2)
215 extern bool MFBT_DATA sse4_2_enabled
;
217 # if !defined(MOZILLA_PRESUME_AVX)
218 extern bool MFBT_DATA avx_enabled
;
220 # if !defined(MOZILLA_PRESUME_AVX2)
221 extern bool MFBT_DATA avx2_enabled
;
223 # if !defined(MOZILLA_PRESUME_AES)
224 extern bool MFBT_DATA aes_enabled
;
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
239 inline bool supports_mmx() { return sse_private::mmx_enabled
; }
241 inline bool supports_mmx() { return false; }
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
; }
251 inline bool supports_sse() { return false; }
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
; }
261 inline bool supports_sse2() { return false; }
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
; }
271 inline bool supports_sse3() { return false; }
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
; }
281 inline bool supports_ssse3() { return false; }
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
; }
291 inline bool supports_sse4a() { return false; }
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
; }
301 inline bool supports_sse4_1() { return false; }
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
; }
311 inline bool supports_sse4_2() { return false; }
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
; }
321 inline bool supports_avx() { return false; }
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
; }
331 inline bool supports_avx2() { return false; }
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
; }
341 inline bool supports_aes() { return false; }
344 } // namespace mozilla
346 #endif /* !defined(mozilla_SSE_h_) */