michael@0: /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* compile-time and runtime tests for whether to use SSE instructions */ michael@0: michael@0: #ifndef mozilla_SSE_h_ michael@0: #define mozilla_SSE_h_ michael@0: michael@0: // for definition of MFBT_DATA michael@0: #include "mozilla/Types.h" michael@0: michael@0: /** michael@0: * The public interface of this header consists of a set of macros and michael@0: * functions for Intel CPU features. michael@0: * michael@0: * DETECTING ISA EXTENSIONS michael@0: * ======================== michael@0: * michael@0: * This header provides the following functions for determining whether the michael@0: * current CPU supports a particular instruction set extension: michael@0: * michael@0: * mozilla::supports_mmx michael@0: * mozilla::supports_sse michael@0: * mozilla::supports_sse2 michael@0: * mozilla::supports_sse3 michael@0: * mozilla::supports_ssse3 michael@0: * mozilla::supports_sse4a michael@0: * mozilla::supports_sse4_1 michael@0: * mozilla::supports_sse4_2 michael@0: * michael@0: * If you're writing code using inline assembly, you should guard it with a michael@0: * call to one of these functions. For instance: michael@0: * michael@0: * if (mozilla::supports_sse2()) { michael@0: * asm(" ... "); michael@0: * } michael@0: * else { michael@0: * ... michael@0: * } michael@0: * michael@0: * Note that these functions depend on cpuid intrinsics only available in gcc michael@0: * 4.3 or later and MSVC 8.0 (Visual C++ 2005) or later, so they return false michael@0: * in older compilers. (This could be fixed by replacing the code with inline michael@0: * assembly.) michael@0: * michael@0: * michael@0: * USING INTRINSICS michael@0: * ================ michael@0: * michael@0: * This header also provides support for coding using CPU intrinsics. michael@0: * michael@0: * For each mozilla::supports_abc function, we define a MOZILLA_MAY_SUPPORT_ABC michael@0: * macro which indicates that the target/compiler combination we're using is michael@0: * compatible with the ABC extension. For instance, x86_64 with MSVC 2003 is michael@0: * compatible with SSE2 but not SSE3, since although there exist x86_64 CPUs michael@0: * with SSE3 support, MSVC 2003 only supports through SSE2. michael@0: * michael@0: * Until gcc fixes #pragma target [1] [2] or our x86 builds require SSE2, michael@0: * you'll need to separate code using intrinsics into a file separate from your michael@0: * regular code. Here's the recommended pattern: michael@0: * michael@0: * #ifdef MOZILLA_MAY_SUPPORT_ABC michael@0: * namespace mozilla { michael@0: * namespace ABC { michael@0: * void foo(); michael@0: * } michael@0: * } michael@0: * #endif michael@0: * michael@0: * void foo() { michael@0: * #ifdef MOZILLA_MAY_SUPPORT_ABC michael@0: * if (mozilla::supports_abc()) { michael@0: * mozilla::ABC::foo(); // in a separate file michael@0: * return; michael@0: * } michael@0: * #endif michael@0: * michael@0: * foo_unvectorized(); michael@0: * } michael@0: * michael@0: * You'll need to define mozilla::ABC::foo() in a separate file and add the michael@0: * -mabc flag when using gcc. michael@0: * michael@0: * [1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39787 and michael@0: * [2] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41201 being fixed. michael@0: * michael@0: */ michael@0: michael@0: #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) michael@0: michael@0: #ifdef __MMX__ michael@0: // It's ok to use MMX instructions based on the -march option (or michael@0: // the default for x86_64 or for Intel Mac). michael@0: #define MOZILLA_PRESUME_MMX 1 michael@0: #endif michael@0: #ifdef __SSE__ michael@0: // It's ok to use SSE instructions based on the -march option (or michael@0: // the default for x86_64 or for Intel Mac). michael@0: #define MOZILLA_PRESUME_SSE 1 michael@0: #endif michael@0: #ifdef __SSE2__ michael@0: // It's ok to use SSE2 instructions based on the -march option (or michael@0: // the default for x86_64 or for Intel Mac). michael@0: #define MOZILLA_PRESUME_SSE2 1 michael@0: #endif michael@0: #ifdef __SSE3__ michael@0: // It's ok to use SSE3 instructions based on the -march option (or the michael@0: // default for Intel Mac). michael@0: #define MOZILLA_PRESUME_SSE3 1 michael@0: #endif michael@0: #ifdef __SSSE3__ michael@0: // It's ok to use SSSE3 instructions based on the -march option. michael@0: #define MOZILLA_PRESUME_SSSE3 1 michael@0: #endif michael@0: #ifdef __SSE4A__ michael@0: // It's ok to use SSE4A instructions based on the -march option. michael@0: #define MOZILLA_PRESUME_SSE4A 1 michael@0: #endif michael@0: #ifdef __SSE4_1__ michael@0: // It's ok to use SSE4.1 instructions based on the -march option. michael@0: #define MOZILLA_PRESUME_SSE4_1 1 michael@0: #endif michael@0: #ifdef __SSE4_2__ michael@0: // It's ok to use SSE4.2 instructions based on the -march option. michael@0: #define MOZILLA_PRESUME_SSE4_2 1 michael@0: #endif michael@0: michael@0: #ifdef HAVE_CPUID_H michael@0: #define MOZILLA_SSE_HAVE_CPUID_DETECTION michael@0: #endif michael@0: michael@0: #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64)) michael@0: michael@0: #define MOZILLA_SSE_HAVE_CPUID_DETECTION michael@0: michael@0: #if defined(_M_IX86_FP) michael@0: michael@0: #if _M_IX86_FP >= 1 michael@0: // It's ok to use SSE instructions based on the /arch option michael@0: #define MOZILLA_PRESUME_SSE michael@0: #endif michael@0: #if _M_IX86_FP >= 2 michael@0: // It's ok to use SSE2 instructions based on the /arch option michael@0: #define MOZILLA_PRESUME_SSE2 michael@0: #endif michael@0: michael@0: #elif defined(_M_AMD64) michael@0: // MSVC for AMD64 doesn't support MMX, so don't presume it here. michael@0: michael@0: // SSE is always available on AMD64. michael@0: #define MOZILLA_PRESUME_SSE michael@0: // SSE2 is always available on AMD64. michael@0: #define MOZILLA_PRESUME_SSE2 michael@0: #endif michael@0: michael@0: #elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__)) michael@0: // Sun Studio on x86 or amd64 michael@0: michael@0: #define MOZILLA_SSE_HAVE_CPUID_DETECTION michael@0: michael@0: #if defined(__x86_64__) michael@0: // MMX is always available on AMD64. michael@0: #define MOZILLA_PRESUME_MMX michael@0: // SSE is always available on AMD64. michael@0: #define MOZILLA_PRESUME_SSE michael@0: // SSE2 is always available on AMD64. michael@0: #define MOZILLA_PRESUME_SSE2 michael@0: #endif michael@0: michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: michael@0: namespace sse_private { michael@0: #if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #if !defined(MOZILLA_PRESUME_MMX) michael@0: extern bool MFBT_DATA mmx_enabled; michael@0: #endif michael@0: #if !defined(MOZILLA_PRESUME_SSE) michael@0: extern bool MFBT_DATA sse_enabled; michael@0: #endif michael@0: #if !defined(MOZILLA_PRESUME_SSE2) michael@0: extern bool MFBT_DATA sse2_enabled; michael@0: #endif michael@0: #if !defined(MOZILLA_PRESUME_SSE3) michael@0: extern bool MFBT_DATA sse3_enabled; michael@0: #endif michael@0: #if !defined(MOZILLA_PRESUME_SSSE3) michael@0: extern bool MFBT_DATA ssse3_enabled; michael@0: #endif michael@0: #if !defined(MOZILLA_PRESUME_SSE4A) michael@0: extern bool MFBT_DATA sse4a_enabled; michael@0: #endif michael@0: #if !defined(MOZILLA_PRESUME_SSE4_1) michael@0: extern bool MFBT_DATA sse4_1_enabled; michael@0: #endif michael@0: #if !defined(MOZILLA_PRESUME_SSE4_2) michael@0: extern bool MFBT_DATA sse4_2_enabled; michael@0: #endif michael@0: #endif michael@0: } michael@0: michael@0: #if defined(MOZILLA_PRESUME_MMX) michael@0: #define MOZILLA_MAY_SUPPORT_MMX 1 michael@0: inline bool supports_mmx() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #if !(defined(_MSC_VER) && defined(_M_AMD64)) michael@0: // Define MOZILLA_MAY_SUPPORT_MMX only if we're not on MSVC for michael@0: // AMD64, since that compiler doesn't support MMX. michael@0: #define MOZILLA_MAY_SUPPORT_MMX 1 michael@0: #endif michael@0: inline bool supports_mmx() { return sse_private::mmx_enabled; } michael@0: #else michael@0: inline bool supports_mmx() { return false; } michael@0: #endif michael@0: michael@0: #if defined(MOZILLA_PRESUME_SSE) michael@0: #define MOZILLA_MAY_SUPPORT_SSE 1 michael@0: inline bool supports_sse() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #define MOZILLA_MAY_SUPPORT_SSE 1 michael@0: inline bool supports_sse() { return sse_private::sse_enabled; } michael@0: #else michael@0: inline bool supports_sse() { return false; } michael@0: #endif michael@0: michael@0: #if defined(MOZILLA_PRESUME_SSE2) michael@0: #define MOZILLA_MAY_SUPPORT_SSE2 1 michael@0: inline bool supports_sse2() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #define MOZILLA_MAY_SUPPORT_SSE2 1 michael@0: inline bool supports_sse2() { return sse_private::sse2_enabled; } michael@0: #else michael@0: inline bool supports_sse2() { return false; } michael@0: #endif michael@0: michael@0: #if defined(MOZILLA_PRESUME_SSE3) michael@0: #define MOZILLA_MAY_SUPPORT_SSE3 1 michael@0: inline bool supports_sse3() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #define MOZILLA_MAY_SUPPORT_SSE3 1 michael@0: inline bool supports_sse3() { return sse_private::sse3_enabled; } michael@0: #else michael@0: inline bool supports_sse3() { return false; } michael@0: #endif michael@0: michael@0: #if defined(MOZILLA_PRESUME_SSSE3) michael@0: #define MOZILLA_MAY_SUPPORT_SSSE3 1 michael@0: inline bool supports_ssse3() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #define MOZILLA_MAY_SUPPORT_SSSE3 1 michael@0: inline bool supports_ssse3() { return sse_private::ssse3_enabled; } michael@0: #else michael@0: inline bool supports_ssse3() { return false; } michael@0: #endif michael@0: michael@0: #if defined(MOZILLA_PRESUME_SSE4A) michael@0: #define MOZILLA_MAY_SUPPORT_SSE4A 1 michael@0: inline bool supports_sse4a() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #define MOZILLA_MAY_SUPPORT_SSE4A 1 michael@0: inline bool supports_sse4a() { return sse_private::sse4a_enabled; } michael@0: #else michael@0: inline bool supports_sse4a() { return false; } michael@0: #endif michael@0: michael@0: #if defined(MOZILLA_PRESUME_SSE4_1) michael@0: #define MOZILLA_MAY_SUPPORT_SSE4_1 1 michael@0: inline bool supports_sse4_1() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #define MOZILLA_MAY_SUPPORT_SSE4_1 1 michael@0: inline bool supports_sse4_1() { return sse_private::sse4_1_enabled; } michael@0: #else michael@0: inline bool supports_sse4_1() { return false; } michael@0: #endif michael@0: michael@0: #if defined(MOZILLA_PRESUME_SSE4_2) michael@0: #define MOZILLA_MAY_SUPPORT_SSE4_2 1 michael@0: inline bool supports_sse4_2() { return true; } michael@0: #elif defined(MOZILLA_SSE_HAVE_CPUID_DETECTION) michael@0: #define MOZILLA_MAY_SUPPORT_SSE4_2 1 michael@0: inline bool supports_sse4_2() { return sse_private::sse4_2_enabled; } michael@0: #else michael@0: inline bool supports_sse4_2() { return false; } michael@0: #endif michael@0: michael@0: } michael@0: michael@0: #endif /* !defined(mozilla_SSE_h_) */