1 /* vim: set shiftwidth=4 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 */
10 #include "mozilla/Attributes.h"
13 // cpuid.h is available on gcc 4.3 and higher on i386 and x86_64
15 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
16 // MSVC 2005 or newer on x86-32 or x86-64
22 // SSE.h has parallel #ifs which declare MOZILLA_SSE_HAVE_CPUID_DETECTION.
23 // We can't declare these functions in the header file, however, because
24 // <intrin.h> conflicts with <windows.h> on MSVC 2005, and some files want to
25 // include both SSE.h and <windows.h>.
29 enum CPUIDRegister
{ eax
= 0, ebx
= 1, ecx
= 2, edx
= 3 };
31 static bool has_cpuid_bits(unsigned int level
, CPUIDRegister reg
,
34 unsigned int eax
, ebx
, ecx
, edx
;
35 unsigned max
= __get_cpuid_max(level
& 0x80000000u
, nullptr);
36 if (level
> max
) return false;
37 __cpuid_count(level
, 0, eax
, ebx
, ecx
, edx
);
42 return (regs
[reg
] & bits
) == bits
;
45 static bool has_cpuid_bits_ex(unsigned int level
, CPUIDRegister reg
,
48 unsigned int eax
, ebx
, ecx
, edx
;
49 unsigned max
= __get_cpuid_max(level
& 0x80000000u
, nullptr);
50 if (level
> max
) return false;
51 __cpuid_count(level
, 1, eax
, ebx
, ecx
, edx
);
56 return (regs
[reg
] & bits
) == bits
;
59 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
61 enum CPUIDRegister
{ eax
= 0, ebx
= 1, ecx
= 2, edx
= 3 };
63 static bool has_cpuid_bits(unsigned int level
, CPUIDRegister reg
,
65 // Check that the level in question is supported.
67 __cpuid_ex(regs
, level
& 0x80000000u
, 1);
68 if (unsigned(regs
[0]) < level
) return false;
70 // "The __cpuid intrinsic clears the ECX register before calling the cpuid
72 __cpuid_ex(regs
, level
, 1);
73 return (unsigned(regs
[reg
]) & bits
) == bits
;
76 #elif (defined(__GNUC__) || defined(__SUNPRO_CC)) && \
77 (defined(__i386) || defined(__x86_64__))
79 enum CPUIDRegister
{ eax
= 0, ebx
= 1, ecx
= 2, edx
= 3 };
82 static void moz_cpuid(int CPUInfo
[4], int InfoType
) {
83 asm("xchg %esi, %ebx\n"
84 "xor %ecx, %ecx\n" // ecx is the sub-leaf (we only ever need 0)
87 "movl %ebx, 4(%edi)\n"
88 "movl %ecx, 8(%edi)\n"
89 "movl %edx, 12(%edi)\n"
92 : "a"(InfoType
), // %eax
94 : "%ecx", "%edx", "%esi");
96 static void moz_cpuid_ex(int CPUInfo
[4], int InfoType
) {
97 asm("xchg %esi, %ebx\n"
100 "movl %eax, (%edi)\n"
101 "movl %ebx, 4(%edi)\n"
102 "movl %ecx, 8(%edi)\n"
103 "movl %edx, 12(%edi)\n"
106 : "a"(InfoType
), // %eax
108 : "%ecx", "%edx", "%esi");
111 static void moz_cpuid(int CPUInfo
[4], int InfoType
) {
112 asm("xchg %rsi, %rbx\n"
113 "xor %ecx, %ecx\n" // ecx is the sub-leaf (we only ever need 0)
115 "movl %eax, (%rdi)\n"
116 "movl %ebx, 4(%rdi)\n"
117 "movl %ecx, 8(%rdi)\n"
118 "movl %edx, 12(%rdi)\n"
121 : "a"(InfoType
), // %eax
123 : "%ecx", "%edx", "%rsi");
125 static void moz_cpuid_ex(int CPUInfo
[4], int InfoType
) {
126 asm("xchg %rsi, %rbx\n"
129 "movl %eax, (%rdi)\n"
130 "movl %ebx, 4(%rdi)\n"
131 "movl %ecx, 8(%rdi)\n"
132 "movl %edx, 12(%rdi)\n"
135 : "a"(InfoType
), // %eax
137 : "%ecx", "%edx", "%rsi");
141 static bool has_cpuid_bits(unsigned int level
, CPUIDRegister reg
,
143 // Check that the level in question is supported.
144 volatile int regs
[4];
145 moz_cpuid((int*)regs
, level
& 0x80000000u
);
146 if (unsigned(regs
[0]) < level
) return false;
148 moz_cpuid((int*)regs
, level
);
149 return (unsigned(regs
[reg
]) & bits
) == bits
;
152 static bool has_cpuid_bits_ex(unsigned int level
, CPUIDRegister reg
,
154 // Check that the level in question is supported.
155 volatile int regs
[4];
156 moz_cpuid_ex((int*)regs
, level
& 0x80000000u
);
157 if (unsigned(regs
[0]) < level
) return false;
159 moz_cpuid_ex((int*)regs
, level
);
160 return (unsigned(regs
[reg
]) & bits
) == bits
;
163 #endif // end CPUID declarations
169 namespace sse_private
{
171 #if defined(MOZILLA_SSE_HAVE_CPUID_DETECTION)
173 # if !defined(MOZILLA_PRESUME_MMX)
174 MOZ_RUNINIT
bool mmx_enabled
= has_cpuid_bits(1u, edx
, (1u << 23));
177 # if !defined(MOZILLA_PRESUME_SSE)
178 MOZ_RUNINIT
bool sse_enabled
= has_cpuid_bits(1u, edx
, (1u << 25));
181 # if !defined(MOZILLA_PRESUME_SSE2)
182 MOZ_RUNINIT
bool sse2_enabled
= has_cpuid_bits(1u, edx
, (1u << 26));
185 # if !defined(MOZILLA_PRESUME_SSE3)
186 MOZ_RUNINIT
bool sse3_enabled
= has_cpuid_bits(1u, ecx
, (1u << 0));
189 # if !defined(MOZILLA_PRESUME_SSSE3)
190 MOZ_RUNINIT
bool ssse3_enabled
= has_cpuid_bits(1u, ecx
, (1u << 9));
193 # if !defined(MOZILLA_PRESUME_SSE4A)
194 MOZ_RUNINIT
bool sse4a_enabled
= has_cpuid_bits(0x80000001u
, ecx
, (1u << 6));
197 # if !defined(MOZILLA_PRESUME_SSE4_1)
198 MOZ_RUNINIT
bool sse4_1_enabled
= has_cpuid_bits(1u, ecx
, (1u << 19));
201 # if !defined(MOZILLA_PRESUME_SSE4_2)
202 MOZ_RUNINIT
bool sse4_2_enabled
= has_cpuid_bits(1u, ecx
, (1u << 20));
205 # if !defined(MOZILLA_PRESUME_FMA3)
206 MOZ_RUNINIT
bool fma3_enabled
= has_cpuid_bits(1u, ecx
, (1u << 12));
209 # if !defined(MOZILLA_PRESUME_AVX) || !defined(MOZILLA_PRESUME_AVX2)
210 static bool has_avx() {
211 # if defined(MOZILLA_PRESUME_AVX)
214 const unsigned AVX
= 1u << 28;
215 const unsigned OSXSAVE
= 1u << 27;
216 const unsigned XSAVE
= 1u << 26;
218 const unsigned XMM_STATE
= 1u << 1;
219 const unsigned YMM_STATE
= 1u << 2;
220 const unsigned AVX_STATE
= XMM_STATE
| YMM_STATE
;
222 return has_cpuid_bits(1u, ecx
, AVX
| OSXSAVE
| XSAVE
) &&
223 // ensure the OS supports XSAVE of YMM registers
224 (xgetbv(0) & AVX_STATE
) == AVX_STATE
;
225 # endif // MOZILLA_PRESUME_AVX
227 # endif // !MOZILLA_PRESUME_AVX || !MOZILLA_PRESUME_AVX2
229 # if !defined(MOZILLA_PRESUME_AVX)
230 MOZ_RUNINIT
bool avx_enabled
= has_avx();
233 # if !defined(MOZILLA_PRESUME_AVX2)
234 MOZ_RUNINIT
bool avx2_enabled
= has_avx() && has_cpuid_bits(7u, ebx
, (1u << 5));
237 # if !defined(MOZILLA_PRESUME_AVXVNNI)
238 MOZ_RUNINIT
bool avxvnni_enabled
= has_cpuid_bits_ex(7u, eax
, (1u << 4));
241 # if !defined(MOZILLA_PRESUME_AES)
242 MOZ_RUNINIT
bool aes_enabled
= has_cpuid_bits(1u, ecx
, (1u << 25));
245 MOZ_RUNINIT
bool has_constant_tsc
= has_cpuid_bits(0x80000007u
, edx
, (1u << 8));
249 } // namespace sse_private
253 uint64_t xgetbv(uint32_t xcr
) {
255 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(eax
), "=d"(edx
) : "c"(xcr
));
256 return (uint64_t)(edx
) << 32 | eax
;
261 } // namespace mozilla