blob: 8c184487148039b5ab57b6b23e3aaddcbb7990dd [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
James Zerne1cbb132018-08-22 14:10:36 -070012#ifndef AOM_AOM_PORTS_X86_H_
13#define AOM_AOM_PORTS_X86_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014#include <stdlib.h>
15
16#if defined(_MSC_VER)
17#include <intrin.h> /* For __cpuidex, __rdtsc */
18#endif
19
Yaowu Xuf883b422016-08-30 14:01:10 -070020#include "aom/aom_integer.h"
Tom Finegan60e653d2018-05-22 11:34:58 -070021#include "config/aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27typedef enum {
Yaowu Xuf883b422016-08-30 14:01:10 -070028 AOM_CPU_UNKNOWN = -1,
29 AOM_CPU_AMD,
30 AOM_CPU_AMD_OLD,
31 AOM_CPU_CENTAUR,
32 AOM_CPU_CYRIX,
33 AOM_CPU_INTEL,
34 AOM_CPU_NEXGEN,
35 AOM_CPU_NSC,
36 AOM_CPU_RISE,
37 AOM_CPU_SIS,
38 AOM_CPU_TRANSMETA,
39 AOM_CPU_TRANSMETA_OLD,
40 AOM_CPU_UMC,
41 AOM_CPU_VIA,
Yaowu Xuc27fc142016-08-22 16:08:15 -070042
Yaowu Xuf883b422016-08-30 14:01:10 -070043 AOM_CPU_LAST
44} aom_cpu_t;
Yaowu Xuc27fc142016-08-22 16:08:15 -070045
46#if defined(__GNUC__) && __GNUC__ || defined(__ANDROID__)
47#if ARCH_X86_64
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));
52#else
53#define cpuid(func, func2, ax, bx, cx, dx) \
54 __asm__ __volatile__( \
55 "mov %%ebx, %%edi \n\t" \
56 "cpuid \n\t" \
57 "xchg %%edi, %%ebx \n\t" \
58 : "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
59 : "a"(func), "c"(func2));
60#endif
61#elif defined(__SUNPRO_C) || \
62 defined(__SUNPRO_CC) /* end __GNUC__ or __ANDROID__*/
63#if ARCH_X86_64
64#define cpuid(func, func2, ax, bx, cx, dx) \
65 asm volatile( \
66 "xchg %rsi, %rbx \n\t" \
67 "cpuid \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));
72#else
73#define cpuid(func, func2, ax, bx, cx, dx) \
74 asm volatile( \
75 "pushl %ebx \n\t" \
76 "cpuid \n\t" \
77 "movl %ebx, %edi \n\t" \
78 "popl %ebx \n\t" \
79 : "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
80 : "a"(func), "c"(func2));
81#endif
82#else /* end __SUNPRO__ */
83#if ARCH_X86_64
84#if defined(_MSC_VER) && _MSC_VER > 1500
85#define cpuid(func, func2, a, b, c, d) \
86 do { \
87 int regs[4]; \
88 __cpuidex(regs, func, func2); \
89 a = regs[0]; \
90 b = regs[1]; \
91 c = regs[2]; \
92 d = regs[3]; \
93 } while (0)
94#else
95#define cpuid(func, func2, a, b, c, d) \
96 do { \
97 int regs[4]; \
98 __cpuid(regs, func); \
99 a = regs[0]; \
100 b = regs[1]; \
101 c = regs[2]; \
102 d = regs[3]; \
103 } while (0)
104#endif
105#else
106/* clang-format off */
107#define cpuid(func, func2, a, b, c, d) \
108 __asm mov eax, func \
109 __asm mov ecx, func2 \
110 __asm cpuid \
111 __asm mov a, eax \
112 __asm mov b, ebx \
113 __asm mov c, ecx \
114 __asm mov d, edx
115#endif
116/* clang-format on */
117#endif /* end others */
118
119// NaCl has no support for xgetbv or the raw opcode.
120#if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__))
121static INLINE uint64_t xgetbv(void) {
122 const uint32_t ecx = 0;
123 uint32_t eax, edx;
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)
127 : "c"(ecx));
128 return ((uint64_t)edx << 32) | eax;
129}
clang-format67948d32016-09-07 22:40:40 -0700130#elif (defined(_M_X64) || defined(_M_IX86)) && defined(_MSC_FULL_VER) && \
Yaowu Xuc27fc142016-08-22 16:08:15 -0700131 _MSC_FULL_VER >= 160040219 // >= VS2010 SP1
132#include <immintrin.h>
133#define xgetbv() _xgetbv(0)
134#elif defined(_MSC_VER) && defined(_M_IX86)
135static INLINE uint64_t xgetbv(void) {
136 uint32_t eax_, edx_;
137 __asm {
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
141 mov eax_, eax
142 mov edx_, edx
143 }
144 return ((uint64_t)edx_ << 32) | eax_;
145}
146#else
147#define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains.
148#endif
149
150#if defined(_MSC_VER) && _MSC_VER >= 1700
151#include <windows.h>
152#if WINAPI_FAMILY_PARTITION(WINAPI_FAMILY_APP)
153#define getenv(x) NULL
154#endif
155#endif
156
157#define HAS_MMX 0x01
158#define HAS_SSE 0x02
159#define HAS_SSE2 0x04
160#define HAS_SSE3 0x08
161#define HAS_SSSE3 0x10
162#define HAS_SSE4_1 0x20
163#define HAS_AVX 0x40
164#define HAS_AVX2 0x80
PENGBINffda3772018-02-26 17:36:37 +0800165#define HAS_SSE4_2 0x100
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166#ifndef BIT
167#define BIT(n) (1 << n)
168#endif
169
170static 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;
174 char *env;
175 (void)reg_ebx;
176
177 /* See if the CPU capabilities are being overridden by the environment */
Yaowu Xuf883b422016-08-30 14:01:10 -0700178 env = getenv("AOM_SIMD_CAPS");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179
180 if (env && *env) return (int)strtol(env, NULL, 0);
181
Yaowu Xuf883b422016-08-30 14:01:10 -0700182 env = getenv("AOM_SIMD_CAPS_MASK");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183
184 if (env && *env) mask = (unsigned int)strtoul(env, NULL, 0);
185
186 /* Ensure that the CPUID instruction supports extended features */
187 cpuid(0, 0, max_cpuid_val, reg_ebx, reg_ecx, reg_edx);
188
189 if (max_cpuid_val < 1) return 0;
190
191 /* Get the standard feature flags */
192 cpuid(1, 0, reg_eax, reg_ebx, reg_ecx, reg_edx);
193
194 if (reg_edx & BIT(23)) flags |= HAS_MMX;
195
196 if (reg_edx & BIT(25)) flags |= HAS_SSE; /* aka xmm */
197
198 if (reg_edx & BIT(26)) flags |= HAS_SSE2; /* aka wmt */
199
200 if (reg_ecx & BIT(0)) flags |= HAS_SSE3;
201
202 if (reg_ecx & BIT(9)) flags |= HAS_SSSE3;
203
204 if (reg_ecx & BIT(19)) flags |= HAS_SSE4_1;
205
PENGBINffda3772018-02-26 17:36:37 +0800206 if (reg_ecx & BIT(20)) flags |= HAS_SSE4_2;
207
Yaowu Xuc27fc142016-08-22 16:08:15 -0700208 // bits 27 (OSXSAVE) & 28 (256-bit AVX)
209 if ((reg_ecx & (BIT(27) | BIT(28))) == (BIT(27) | BIT(28))) {
210 if ((xgetbv() & 0x6) == 0x6) {
211 flags |= HAS_AVX;
212
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);
216
217 if (reg_ebx & BIT(5)) flags |= HAS_AVX2;
218 }
219 }
220 }
221
222 return flags & mask;
223}
224
elliottk6e05c6e2018-12-05 11:43:09 -0800225// Fine-Grain Measurement Functions
226//
227// If you are a timing a small region of code, access the timestamp counter
228// (TSC) via:
229//
230// unsigned int start = x86_tsc_start();
231// ...
232// unsigned int end = x86_tsc_end();
233// unsigned int diff = end - start;
234//
235// The start/end functions introduce a few more instructions than using
236// x86_readtsc directly, but prevent the CPU's out-of-order execution from
237// affecting the measurement (by having earlier/later instructions be evaluated
238// in the time interval). See the white paper, "How to Benchmark Code
239// Execution Times on IntelĀ® IA-32 and IA-64 Instruction Set Architectures" by
240// Gabriele Paoloni for more information.
241//
242// If you are timing a large function (CPU time > a couple of seconds), use
243// x86_readtsc64 to read the timestamp counter in a 64-bit integer. The
244// out-of-order leakage that can occur is minimal compared to total runtime.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245static INLINE unsigned int x86_readtsc(void) {
246#if defined(__GNUC__) && __GNUC__
247 unsigned int tsc;
248 __asm__ __volatile__("rdtsc\n\t" : "=a"(tsc) :);
249 return tsc;
250#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
251 unsigned int tsc;
252 asm volatile("rdtsc\n\t" : "=a"(tsc) :);
253 return tsc;
254#else
255#if ARCH_X86_64
256 return (unsigned int)__rdtsc();
257#else
258 __asm rdtsc;
259#endif
260#endif
261}
262// 64-bit CPU cycle counter
263static INLINE uint64_t x86_readtsc64(void) {
264#if defined(__GNUC__) && __GNUC__
265 uint32_t hi, lo;
266 __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
267 return ((uint64_t)hi << 32) | lo;
268#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
269 uint_t hi, lo;
270 asm volatile("rdtsc\n\t" : "=a"(lo), "=d"(hi));
271 return ((uint64_t)hi << 32) | lo;
272#else
273#if ARCH_X86_64
274 return (uint64_t)__rdtsc();
275#else
276 __asm rdtsc;
277#endif
278#endif
279}
280
elliottk6e05c6e2018-12-05 11:43:09 -0800281// 32-bit CPU cycle counter with a partial fence against out-of-order execution.
282static INLINE unsigned int x86_readtscp(void) {
283#if defined(__GNUC__) && __GNUC__
284 unsigned int tscp;
285 __asm__ __volatile__("rdtscp\n\t" : "=a"(tscp) :);
286 return tscp;
287#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
288 unsigned int tscp;
289 asm volatile("rdtscp\n\t" : "=a"(tscp) :);
290 return tscp;
Wan-Teh Chang34611bd2018-12-20 13:39:09 -0800291#elif defined(_MSC_VER)
292 unsigned int ui;
293 return (unsigned int)__rdtscp(&ui);
elliottk6e05c6e2018-12-05 11:43:09 -0800294#else
295#if ARCH_X86_64
296 return (unsigned int)__rdtscp();
297#else
298 __asm rdtscp;
299#endif
300#endif
301}
302
303static INLINE unsigned int x86_tsc_start(void) {
Wan-Teh Chang3273e6a2018-12-21 09:26:04 -0800304 unsigned int reg_eax, reg_ebx, reg_ecx, reg_edx;
305 cpuid(0, 0, reg_eax, reg_ebx, reg_ecx, reg_edx);
elliottk6e05c6e2018-12-05 11:43:09 -0800306 return x86_readtsc();
307}
308
309static INLINE unsigned int x86_tsc_end(void) {
310 uint32_t v = x86_readtscp();
Wan-Teh Chang3273e6a2018-12-21 09:26:04 -0800311 unsigned int reg_eax, reg_ebx, reg_ecx, reg_edx;
312 cpuid(0, 0, reg_eax, reg_ebx, reg_ecx, reg_edx);
elliottk6e05c6e2018-12-05 11:43:09 -0800313 return v;
314}
315
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316#if defined(__GNUC__) && __GNUC__
317#define x86_pause_hint() __asm__ __volatile__("pause \n\t")
318#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
319#define x86_pause_hint() asm volatile("pause \n\t")
320#else
321#if ARCH_X86_64
322#define x86_pause_hint() _mm_pause();
323#else
324#define x86_pause_hint() __asm pause
325#endif
326#endif
327
328#if defined(__GNUC__) && __GNUC__
329static void x87_set_control_word(unsigned short mode) {
330 __asm__ __volatile__("fldcw %0" : : "m"(*&mode));
331}
332static unsigned short x87_get_control_word(void) {
333 unsigned short mode;
334 __asm__ __volatile__("fstcw %0\n\t" : "=m"(*&mode) :);
335 return mode;
336}
337#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
338static void x87_set_control_word(unsigned short mode) {
339 asm volatile("fldcw %0" : : "m"(*&mode));
340}
341static unsigned short x87_get_control_word(void) {
342 unsigned short mode;
343 asm volatile("fstcw %0\n\t" : "=m"(*&mode) :);
344 return mode;
345}
346#elif ARCH_X86_64
347/* No fldcw intrinsics on Windows x64, punt to external asm */
Yaowu Xuf883b422016-08-30 14:01:10 -0700348extern void aom_winx64_fldcw(unsigned short mode);
349extern unsigned short aom_winx64_fstcw(void);
350#define x87_set_control_word aom_winx64_fldcw
351#define x87_get_control_word aom_winx64_fstcw
Yaowu Xuc27fc142016-08-22 16:08:15 -0700352#else
353static void x87_set_control_word(unsigned short mode) {
354 __asm { fldcw mode }
355}
356static unsigned short x87_get_control_word(void) {
357 unsigned short mode;
358 __asm { fstcw mode }
359 return mode;
360}
361#endif
362
363static INLINE unsigned int x87_set_double_precision(void) {
364 unsigned int mode = x87_get_control_word();
365 x87_set_control_word((mode & ~0x300) | 0x200);
366 return mode;
367}
368
Yaowu Xuf883b422016-08-30 14:01:10 -0700369extern void aom_reset_mmx_state(void);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700370
371#ifdef __cplusplus
372} // extern "C"
373#endif
374
James Zerne1cbb132018-08-22 14:10:36 -0700375#endif // AOM_AOM_PORTS_X86_H_