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
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()) {
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
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
77 * #ifdef MOZILLA_MAY_SUPPORT_ABC
78 * if (mozilla::supports_abc()) {
79 * mozilla::ABC::foo(); // in a separate file
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__))
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
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
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
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
118 // It's ok to use SSSE3 instructions based on the -march option.
119 # define MOZILLA_PRESUME_SSSE3 1
122 // It's ok to use SSE4A instructions based on the -march option.
123 # define MOZILLA_PRESUME_SSE4A 1
126 // It's ok to use SSE4.1 instructions based on the -march option.
127 # define MOZILLA_PRESUME_SSE4_1 1
130 // It's ok to use SSE4.2 instructions based on the -march option.
131 # define MOZILLA_PRESUME_SSE4_2 1
134 // It's ok to use AVX instructions based on the -march option.
135 # define MOZILLA_PRESUME_AVX 1
138 // It's ok to use AVX instructions based on the -march option.
139 # define MOZILLA_PRESUME_AVX2 1
142 // It's ok to use AES instructions based on the -march option.
143 # define MOZILLA_PRESUME_AES 1
147 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
150 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
152 # define MOZILLA_SSE_HAVE_CPUID_DETECTION
154 # if defined(_M_IX86_FP)
157 // It's ok to use SSE instructions based on the /arch option
158 # define MOZILLA_PRESUME_SSE
161 // It's ok to use SSE2 instructions based on the /arch option
162 # define MOZILLA_PRESUME_SSE2
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
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
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
;
197 # if !defined(MOZILLA_PRESUME_SSE)
198 extern bool MFBT_DATA sse_enabled
;
200 # if !defined(MOZILLA_PRESUME_SSE2)
201 extern bool MFBT_DATA sse2_enabled
;
203 # if !defined(MOZILLA_PRESUME_SSE3)
204 extern bool MFBT_DATA sse3_enabled
;
206 # if !defined(MOZILLA_PRESUME_SSSE3)
207 extern bool MFBT_DATA ssse3_enabled
;
209 # if !defined(MOZILLA_PRESUME_SSE4A)
210 extern bool MFBT_DATA sse4a_enabled
;
212 # if !defined(MOZILLA_PRESUME_SSE4_1)
213 extern bool MFBT_DATA sse4_1_enabled
;
215 # if !defined(MOZILLA_PRESUME_SSE4_2)
216 extern bool MFBT_DATA sse4_2_enabled
;
218 # if !defined(MOZILLA_PRESUME_FMA3)
219 extern bool MFBT_DATA fma3_enabled
;
221 # if !defined(MOZILLA_PRESUME_AVX)
222 extern bool MFBT_DATA avx_enabled
;
224 # if !defined(MOZILLA_PRESUME_AVX2)
225 extern bool MFBT_DATA avx2_enabled
;
227 # if !defined(MOZILLA_PRESUME_AES)
228 extern bool MFBT_DATA aes_enabled
;
230 extern bool MFBT_DATA has_constant_tsc
;
233 } // namespace sse_private
236 MOZ_EXPORT
uint64_t xgetbv(uint32_t xcr
);
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
248 inline bool supports_mmx() { return sse_private::mmx_enabled
; }
250 inline bool supports_mmx() { return false; }
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
; }
260 inline bool supports_sse() { return false; }
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
; }
270 inline bool supports_sse2() { return false; }
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
; }
280 inline bool supports_sse3() { return false; }
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
; }
290 inline bool supports_ssse3() { return false; }
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
; }
300 inline bool supports_sse4a() { return false; }
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
; }
310 inline bool supports_sse4_1() { return false; }
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
; }
320 inline bool supports_sse4_2() { return false; }
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
; }
330 inline bool supports_fma3() { return false; }
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
; }
340 inline bool supports_avx() { return false; }
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
; }
350 inline bool supports_avx2() { return false; }
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
; }
360 inline bool supports_aes() { return false; }
363 #ifdef MOZILLA_SSE_HAVE_CPUID_DETECTION
364 inline bool has_constant_tsc() { return sse_private::has_constant_tsc
; }
366 inline bool has_constant_tsc() { return false; }
369 } // namespace mozilla
371 #endif /* !defined(mozilla_SSE_h_) */