2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
12 #ifndef AOM_AOM_PORTS_X86_H_
13 #define AOM_AOM_PORTS_X86_H_
17 #include <intrin.h> /* For __cpuidex, __rdtsc */
20 #include "aom/aom_integer.h"
21 #include "config/aom_config.h"
39 AOM_CPU_TRANSMETA_OLD
,
46 #if defined(__GNUC__) && __GNUC__ || defined(__ANDROID__)
48 #define cpuid(func, func2, ax, bx, cx, dx) \
49 __asm__ __volatile__("cpuid \n\t" \
50 : "=a"(ax), "=b"(bx), "=c"(cx), "=d"(dx) \
51 : "a"(func), "c"(func2));
53 #define cpuid(func, func2, ax, bx, cx, dx) \
54 __asm__ __volatile__( \
55 "mov %%ebx, %%edi \n\t" \
57 "xchg %%edi, %%ebx \n\t" \
58 : "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
59 : "a"(func), "c"(func2));
61 #elif defined(__SUNPRO_C) || \
62 defined(__SUNPRO_CC) /* end __GNUC__ or __ANDROID__*/
64 #define cpuid(func, func2, ax, bx, cx, dx) \
66 "xchg %rsi, %rbx \n\t" \
68 "movl %ebx, %edi \n\t" \
69 "xchg %rsi, %rbx \n\t" \
70 : "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
71 : "a"(func), "c"(func2));
73 #define cpuid(func, func2, ax, bx, cx, dx) \
77 "movl %ebx, %edi \n\t" \
79 : "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
80 : "a"(func), "c"(func2));
82 #else /* end __SUNPRO__ */
84 #if defined(_MSC_VER) && _MSC_VER > 1500
85 #define cpuid(func, func2, a, b, c, d) \
88 __cpuidex(regs, func, func2); \
95 #define cpuid(func, func2, a, b, c, d) \
98 __cpuid(regs, func); \
106 /* clang-format off */
107 #define cpuid(func, func2, a, b, c, d) \
108 __asm mov eax, func \
109 __asm mov ecx, func2 \
116 /* clang-format on */
117 #endif /* end others */
119 // NaCl has no support for xgetbv or the raw opcode.
120 #if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__))
121 static INLINE
uint64_t xgetbv(void) {
122 const uint32_t ecx
= 0;
124 // Use the raw opcode for xgetbv for compatibility with older toolchains.
125 __asm__
volatile(".byte 0x0f, 0x01, 0xd0\n"
126 : "=a"(eax
), "=d"(edx
)
128 return ((uint64_t)edx
<< 32) | eax
;
130 #elif (defined(_M_X64) || defined(_M_IX86)) && defined(_MSC_FULL_VER) && \
131 _MSC_FULL_VER >= 160040219 // >= VS2010 SP1
132 #include <immintrin.h>
133 #define xgetbv() _xgetbv(0)
134 #elif defined(_MSC_VER) && defined(_M_IX86)
135 static INLINE
uint64_t xgetbv(void) {
138 xor ecx
, ecx
// ecx = 0
139 // Use the raw opcode for xgetbv for compatibility with older toolchains.
140 __asm _emit
0x0f __asm _emit
0x01 __asm _emit
0xd0
144 return ((uint64_t)edx_
<< 32) | eax_
;
147 #define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains.
150 #if defined(_MSC_VER) && _MSC_VER >= 1700
152 #if WINAPI_FAMILY_PARTITION(WINAPI_FAMILY_APP)
153 #define getenv(x) NULL
159 #define HAS_SSE2 0x04
160 #define HAS_SSE3 0x08
161 #define HAS_SSSE3 0x10
162 #define HAS_SSE4_1 0x20
164 #define HAS_AVX2 0x80
165 #define HAS_SSE4_2 0x100
167 #define BIT(n) (1 << n)
170 static INLINE
int x86_simd_caps(void) {
171 unsigned int flags
= 0;
172 unsigned int mask
= ~0;
173 unsigned int max_cpuid_val
, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
;
177 /* See if the CPU capabilities are being overridden by the environment */
178 env
= getenv("AOM_SIMD_CAPS");
180 if (env
&& *env
) return (int)strtol(env
, NULL
, 0);
182 env
= getenv("AOM_SIMD_CAPS_MASK");
184 if (env
&& *env
) mask
= (unsigned int)strtoul(env
, NULL
, 0);
186 /* Ensure that the CPUID instruction supports extended features */
187 cpuid(0, 0, max_cpuid_val
, reg_ebx
, reg_ecx
, reg_edx
);
189 if (max_cpuid_val
< 1) return 0;
191 /* Get the standard feature flags */
192 cpuid(1, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
194 if (reg_edx
& BIT(23)) flags
|= HAS_MMX
;
196 if (reg_edx
& BIT(25)) flags
|= HAS_SSE
; /* aka xmm */
198 if (reg_edx
& BIT(26)) flags
|= HAS_SSE2
; /* aka wmt */
200 if (reg_ecx
& BIT(0)) flags
|= HAS_SSE3
;
202 if (reg_ecx
& BIT(9)) flags
|= HAS_SSSE3
;
204 if (reg_ecx
& BIT(19)) flags
|= HAS_SSE4_1
;
206 if (reg_ecx
& BIT(20)) flags
|= HAS_SSE4_2
;
208 // bits 27 (OSXSAVE) & 28 (256-bit AVX)
209 if ((reg_ecx
& (BIT(27) | BIT(28))) == (BIT(27) | BIT(28))) {
210 if ((xgetbv() & 0x6) == 0x6) {
213 if (max_cpuid_val
>= 7) {
214 /* Get the leaf 7 feature flags. Needed to check for AVX2 support */
215 cpuid(7, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
217 if (reg_ebx
& BIT(5)) flags
|= HAS_AVX2
;
226 // 32-bit CPU cycle counter is light-weighted for most function performance
227 // measurement. For large function (CPU time > a couple of seconds), 64-bit
228 // counter should be used.
229 // 32-bit CPU cycle counter
230 static INLINE
unsigned int x86_readtsc(void) {
231 #if defined(__GNUC__) && __GNUC__
233 __asm__
__volatile__("rdtsc\n\t" : "=a"(tsc
) :);
235 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
237 asm volatile("rdtsc\n\t" : "=a"(tsc
) :);
241 return (unsigned int)__rdtsc();
247 // 64-bit CPU cycle counter
248 static INLINE
uint64_t x86_readtsc64(void) {
249 #if defined(__GNUC__) && __GNUC__
251 __asm__
__volatile__("rdtsc" : "=a"(lo
), "=d"(hi
));
252 return ((uint64_t)hi
<< 32) | lo
;
253 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
255 asm volatile("rdtsc\n\t" : "=a"(lo
), "=d"(hi
));
256 return ((uint64_t)hi
<< 32) | lo
;
259 return (uint64_t)__rdtsc();
266 #if defined(__GNUC__) && __GNUC__
267 #define x86_pause_hint() __asm__ __volatile__("pause \n\t")
268 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
269 #define x86_pause_hint() asm volatile("pause \n\t")
272 #define x86_pause_hint() _mm_pause();
274 #define x86_pause_hint() __asm pause
278 #if defined(__GNUC__) && __GNUC__
279 static void x87_set_control_word(unsigned short mode
) {
280 __asm__
__volatile__("fldcw %0" : : "m"(*&mode
));
282 static unsigned short x87_get_control_word(void) {
284 __asm__
__volatile__("fstcw %0\n\t" : "=m"(*&mode
) :);
287 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
288 static void x87_set_control_word(unsigned short mode
) {
289 asm volatile("fldcw %0" : : "m"(*&mode
));
291 static unsigned short x87_get_control_word(void) {
293 asm volatile("fstcw %0\n\t" : "=m"(*&mode
) :);
297 /* No fldcw intrinsics on Windows x64, punt to external asm */
298 extern void aom_winx64_fldcw(unsigned short mode
);
299 extern unsigned short aom_winx64_fstcw(void);
300 #define x87_set_control_word aom_winx64_fldcw
301 #define x87_get_control_word aom_winx64_fstcw
303 static void x87_set_control_word(unsigned short mode
) {
306 static unsigned short x87_get_control_word(void) {
313 static INLINE
unsigned int x87_set_double_precision(void) {
314 unsigned int mode
= x87_get_control_word();
315 x87_set_control_word((mode
& ~0x300) | 0x200);
319 extern void aom_reset_mmx_state(void);
325 #endif // AOM_AOM_PORTS_X86_H_