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) (1u << (n))
170 static INLINE
int x86_simd_caps(void) {
171 unsigned int flags
= 0;
172 unsigned int mask
= ~0u;
173 unsigned int max_cpuid_val
, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
;
176 /* See if the CPU capabilities are being overridden by the environment */
177 env
= getenv("AOM_SIMD_CAPS");
179 if (env
&& *env
) return (int)strtol(env
, NULL
, 0);
181 env
= getenv("AOM_SIMD_CAPS_MASK");
183 if (env
&& *env
) mask
= (unsigned int)strtoul(env
, NULL
, 0);
185 /* Ensure that the CPUID instruction supports extended features */
186 cpuid(0, 0, max_cpuid_val
, reg_ebx
, reg_ecx
, reg_edx
);
188 if (max_cpuid_val
< 1) return 0;
190 /* Get the standard feature flags */
191 cpuid(1, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
193 if (reg_edx
& BIT(23)) flags
|= HAS_MMX
;
195 if (reg_edx
& BIT(25)) flags
|= HAS_SSE
; /* aka xmm */
197 if (reg_edx
& BIT(26)) flags
|= HAS_SSE2
; /* aka wmt */
199 if (reg_ecx
& BIT(0)) flags
|= HAS_SSE3
;
201 if (reg_ecx
& BIT(9)) flags
|= HAS_SSSE3
;
203 if (reg_ecx
& BIT(19)) flags
|= HAS_SSE4_1
;
205 if (reg_ecx
& BIT(20)) flags
|= HAS_SSE4_2
;
207 // bits 27 (OSXSAVE) & 28 (256-bit AVX)
208 if ((reg_ecx
& (BIT(27) | BIT(28))) == (BIT(27) | BIT(28))) {
209 // Check for OS-support of YMM state. Necessary for AVX and AVX2.
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
;
222 (void)reg_eax
; // Avoid compiler warning on unused-but-set variable.
227 // Fine-Grain Measurement Functions
229 // If you are timing a small region of code, access the timestamp counter
232 // unsigned int start = x86_tsc_start();
234 // unsigned int end = x86_tsc_end();
235 // unsigned int diff = end - start;
237 // The start/end functions introduce a few more instructions than using
238 // x86_readtsc directly, but prevent the CPU's out-of-order execution from
239 // affecting the measurement (by having earlier/later instructions be evaluated
240 // in the time interval). See the white paper, "How to Benchmark Code
241 // Execution Times on Intel(R) IA-32 and IA-64 Instruction Set Architectures" by
242 // Gabriele Paoloni for more information.
244 // If you are timing a large function (CPU time > a couple of seconds), use
245 // x86_readtsc64 to read the timestamp counter in a 64-bit integer. The
246 // out-of-order leakage that can occur is minimal compared to total runtime.
247 static INLINE
unsigned int x86_readtsc(void) {
248 #if defined(__GNUC__) && __GNUC__
250 __asm__
__volatile__("rdtsc\n\t" : "=a"(tsc
) :);
252 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
254 asm volatile("rdtsc\n\t" : "=a"(tsc
) :);
258 return (unsigned int)__rdtsc();
264 // 64-bit CPU cycle counter
265 static INLINE
uint64_t x86_readtsc64(void) {
266 #if defined(__GNUC__) && __GNUC__
268 __asm__
__volatile__("rdtsc" : "=a"(lo
), "=d"(hi
));
269 return ((uint64_t)hi
<< 32) | lo
;
270 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
272 asm volatile("rdtsc\n\t" : "=a"(lo
), "=d"(hi
));
273 return ((uint64_t)hi
<< 32) | lo
;
276 return (uint64_t)__rdtsc();
283 // 32-bit CPU cycle counter with a partial fence against out-of-order execution.
284 static INLINE
unsigned int x86_readtscp(void) {
285 #if defined(__GNUC__) && __GNUC__
287 __asm__
__volatile__("rdtscp\n\t" : "=a"(tscp
) :);
289 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
291 asm volatile("rdtscp\n\t" : "=a"(tscp
) :);
293 #elif defined(_MSC_VER)
295 return (unsigned int)__rdtscp(&ui
);
298 return (unsigned int)__rdtscp();
305 static INLINE
unsigned int x86_tsc_start(void) {
306 unsigned int reg_eax
, reg_ebx
, reg_ecx
, reg_edx
;
307 // This call should not be removed. See function notes above.
308 cpuid(0, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
309 // Avoid compiler warnings on unused-but-set variables.
314 return x86_readtsc();
317 static INLINE
unsigned int x86_tsc_end(void) {
318 uint32_t v
= x86_readtscp();
319 unsigned int reg_eax
, reg_ebx
, reg_ecx
, reg_edx
;
320 // This call should not be removed. See function notes above.
321 cpuid(0, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
322 // Avoid compiler warnings on unused-but-set variables.
330 #if defined(__GNUC__) && __GNUC__
331 #define x86_pause_hint() __asm__ __volatile__("pause \n\t")
332 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
333 #define x86_pause_hint() asm volatile("pause \n\t")
336 #define x86_pause_hint() _mm_pause();
338 #define x86_pause_hint() __asm pause
342 #if defined(__GNUC__) && __GNUC__
343 static void x87_set_control_word(unsigned short mode
) {
344 __asm__
__volatile__("fldcw %0" : : "m"(*&mode
));
346 static unsigned short x87_get_control_word(void) {
348 __asm__
__volatile__("fstcw %0\n\t" : "=m"(*&mode
) :);
351 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
352 static void x87_set_control_word(unsigned short mode
) {
353 asm volatile("fldcw %0" : : "m"(*&mode
));
355 static unsigned short x87_get_control_word(void) {
357 asm volatile("fstcw %0\n\t" : "=m"(*&mode
) :);
361 /* No fldcw intrinsics on Windows x64, punt to external asm */
362 extern void aom_winx64_fldcw(unsigned short mode
);
363 extern unsigned short aom_winx64_fstcw(void);
364 #define x87_set_control_word aom_winx64_fldcw
365 #define x87_get_control_word aom_winx64_fstcw
367 static void x87_set_control_word(unsigned short mode
) {
370 static unsigned short x87_get_control_word(void) {
377 static INLINE
unsigned int x87_set_double_precision(void) {
378 unsigned int mode
= x87_get_control_word();
379 // Intel 64 and IA-32 Architectures Developer's Manual: Vol. 1
380 // https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdf
381 // 8.1.5.2 Precision Control Field
382 // Bits 8 and 9 (0x300) of the x87 FPU Control Word ("Precision Control")
383 // determine the number of bits used in floating point calculations. To match
384 // later SSE instructions restrict x87 operations to Double Precision (0x200).
385 // Precision PC Field
386 // Single Precision (24-Bits) 00B
388 // Double Precision (53-Bits) 10B
389 // Extended Precision (64-Bits) 11B
390 x87_set_control_word((mode
& ~0x300) | 0x200);
398 #endif // AOM_AOM_PORTS_X86_H_