2010-05-18 19:58:33 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
2010-05-18 19:58:33 +04:00
|
|
|
*/
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#ifndef AOM_PORTS_X86_H_
|
|
|
|
#define AOM_PORTS_X86_H_
|
2010-05-18 19:58:33 +04:00
|
|
|
#include <stdlib.h>
|
2016-06-22 21:44:47 +03:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
2016-08-11 04:23:43 +03:00
|
|
|
#include <intrin.h> /* For __cpuidex, __rdtsc */
|
2016-06-22 21:44:47 +03:00
|
|
|
#endif
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "aom_config.h"
|
|
|
|
#include "aom/aom_integer.h"
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
typedef enum {
|
2016-08-31 00:01:10 +03:00
|
|
|
AOM_CPU_UNKNOWN = -1,
|
|
|
|
AOM_CPU_AMD,
|
|
|
|
AOM_CPU_AMD_OLD,
|
|
|
|
AOM_CPU_CENTAUR,
|
|
|
|
AOM_CPU_CYRIX,
|
|
|
|
AOM_CPU_INTEL,
|
|
|
|
AOM_CPU_NEXGEN,
|
|
|
|
AOM_CPU_NSC,
|
|
|
|
AOM_CPU_RISE,
|
|
|
|
AOM_CPU_SIS,
|
|
|
|
AOM_CPU_TRANSMETA,
|
|
|
|
AOM_CPU_TRANSMETA_OLD,
|
|
|
|
AOM_CPU_UMC,
|
|
|
|
AOM_CPU_VIA,
|
|
|
|
|
|
|
|
AOM_CPU_LAST
|
|
|
|
} aom_cpu_t;
|
2010-10-13 01:55:31 +04:00
|
|
|
|
2013-04-26 17:00:24 +04:00
|
|
|
#if defined(__GNUC__) && __GNUC__ || defined(__ANDROID__)
|
2010-05-18 19:58:33 +04:00
|
|
|
#if ARCH_X86_64
|
2016-08-11 04:23:43 +03:00
|
|
|
#define cpuid(func, func2, ax, bx, cx, dx) \
|
|
|
|
__asm__ __volatile__("cpuid \n\t" \
|
|
|
|
: "=a"(ax), "=b"(bx), "=c"(cx), "=d"(dx) \
|
|
|
|
: "a"(func), "c"(func2));
|
2010-05-18 19:58:33 +04:00
|
|
|
#else
|
2016-08-11 04:23:43 +03:00
|
|
|
#define cpuid(func, func2, ax, bx, cx, dx) \
|
|
|
|
__asm__ __volatile__( \
|
|
|
|
"mov %%ebx, %%edi \n\t" \
|
|
|
|
"cpuid \n\t" \
|
|
|
|
"xchg %%edi, %%ebx \n\t" \
|
|
|
|
: "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
|
|
|
|
: "a"(func), "c"(func2));
|
2010-05-18 19:58:33 +04:00
|
|
|
#endif
|
2016-08-11 04:23:43 +03:00
|
|
|
#elif defined(__SUNPRO_C) || \
|
|
|
|
defined(__SUNPRO_CC) /* end __GNUC__ or __ANDROID__*/
|
2012-11-03 02:39:14 +04:00
|
|
|
#if ARCH_X86_64
|
2016-08-11 04:23:43 +03:00
|
|
|
#define cpuid(func, func2, ax, bx, cx, dx) \
|
|
|
|
asm volatile( \
|
|
|
|
"xchg %rsi, %rbx \n\t" \
|
|
|
|
"cpuid \n\t" \
|
|
|
|
"movl %ebx, %edi \n\t" \
|
|
|
|
"xchg %rsi, %rbx \n\t" \
|
|
|
|
: "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
|
|
|
|
: "a"(func), "c"(func2));
|
2012-11-03 02:39:14 +04:00
|
|
|
#else
|
2016-08-11 04:23:43 +03:00
|
|
|
#define cpuid(func, func2, ax, bx, cx, dx) \
|
|
|
|
asm volatile( \
|
|
|
|
"pushl %ebx \n\t" \
|
|
|
|
"cpuid \n\t" \
|
|
|
|
"movl %ebx, %edi \n\t" \
|
|
|
|
"popl %ebx \n\t" \
|
|
|
|
: "=a"(ax), "=D"(bx), "=c"(cx), "=d"(dx) \
|
|
|
|
: "a"(func), "c"(func2));
|
2012-11-03 02:39:14 +04:00
|
|
|
#endif
|
2013-04-26 17:00:24 +04:00
|
|
|
#else /* end __SUNPRO__ */
|
2010-05-18 19:58:33 +04:00
|
|
|
#if ARCH_X86_64
|
2013-11-23 04:45:56 +04:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER > 1500
|
2016-08-11 04:23:43 +03:00
|
|
|
#define cpuid(func, func2, a, b, c, d) \
|
|
|
|
do { \
|
|
|
|
int regs[4]; \
|
|
|
|
__cpuidex(regs, func, func2); \
|
|
|
|
a = regs[0]; \
|
|
|
|
b = regs[1]; \
|
|
|
|
c = regs[2]; \
|
|
|
|
d = regs[3]; \
|
|
|
|
} while (0)
|
2010-05-18 19:58:33 +04:00
|
|
|
#else
|
2016-08-11 04:23:43 +03:00
|
|
|
#define cpuid(func, func2, a, b, c, d) \
|
|
|
|
do { \
|
|
|
|
int regs[4]; \
|
|
|
|
__cpuid(regs, func); \
|
|
|
|
a = regs[0]; \
|
|
|
|
b = regs[1]; \
|
|
|
|
c = regs[2]; \
|
|
|
|
d = regs[3]; \
|
2013-11-23 04:45:56 +04:00
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
#else
|
2016-08-11 04:35:20 +03:00
|
|
|
/* clang-format off */
|
|
|
|
#define cpuid(func, func2, a, b, c, d) \
|
|
|
|
__asm mov eax, func \
|
|
|
|
__asm mov ecx, func2 \
|
|
|
|
__asm cpuid \
|
|
|
|
__asm mov a, eax \
|
|
|
|
__asm mov b, ebx \
|
|
|
|
__asm mov c, ecx \
|
|
|
|
__asm mov d, edx
|
2010-05-18 19:58:33 +04:00
|
|
|
#endif
|
2016-08-11 04:35:20 +03:00
|
|
|
/* clang-format on */
|
2013-04-26 17:00:24 +04:00
|
|
|
#endif /* end others */
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2015-01-15 09:51:49 +03:00
|
|
|
// NaCl has no support for xgetbv or the raw opcode.
|
|
|
|
#if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__))
|
|
|
|
static INLINE uint64_t xgetbv(void) {
|
|
|
|
const uint32_t ecx = 0;
|
|
|
|
uint32_t eax, edx;
|
|
|
|
// Use the raw opcode for xgetbv for compatibility with older toolchains.
|
2016-08-11 04:23:43 +03:00
|
|
|
__asm__ volatile(".byte 0x0f, 0x01, 0xd0\n"
|
|
|
|
: "=a"(eax), "=d"(edx)
|
|
|
|
: "c"(ecx));
|
2015-01-15 09:51:49 +03:00
|
|
|
return ((uint64_t)edx << 32) | eax;
|
|
|
|
}
|
2016-09-08 08:40:40 +03:00
|
|
|
#elif (defined(_M_X64) || defined(_M_IX86)) && defined(_MSC_FULL_VER) && \
|
2016-08-11 04:23:43 +03:00
|
|
|
_MSC_FULL_VER >= 160040219 // >= VS2010 SP1
|
2015-01-15 09:51:49 +03:00
|
|
|
#include <immintrin.h>
|
|
|
|
#define xgetbv() _xgetbv(0)
|
|
|
|
#elif defined(_MSC_VER) && defined(_M_IX86)
|
|
|
|
static INLINE uint64_t xgetbv(void) {
|
|
|
|
uint32_t eax_, edx_;
|
|
|
|
__asm {
|
|
|
|
xor ecx, ecx // ecx = 0
|
|
|
|
// Use the raw opcode for xgetbv for compatibility with older toolchains.
|
|
|
|
__asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0
|
|
|
|
mov eax_, eax
|
|
|
|
mov edx_, edx
|
|
|
|
}
|
|
|
|
return ((uint64_t)edx_ << 32) | eax_;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains.
|
|
|
|
#endif
|
|
|
|
|
2015-08-08 21:10:44 +03:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1700
|
2015-07-28 17:37:09 +03:00
|
|
|
#include <windows.h>
|
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_FAMILY_APP)
|
|
|
|
#define getenv(x) NULL
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-08-11 04:23:43 +03:00
|
|
|
#define HAS_MMX 0x01
|
|
|
|
#define HAS_SSE 0x02
|
|
|
|
#define HAS_SSE2 0x04
|
|
|
|
#define HAS_SSE3 0x08
|
|
|
|
#define HAS_SSSE3 0x10
|
|
|
|
#define HAS_SSE4_1 0x20
|
|
|
|
#define HAS_AVX 0x40
|
|
|
|
#define HAS_AVX2 0x80
|
2010-05-18 19:58:33 +04:00
|
|
|
#ifndef BIT
|
2016-08-11 04:23:43 +03:00
|
|
|
#define BIT(n) (1 << n)
|
2010-05-18 19:58:33 +04:00
|
|
|
#endif
|
|
|
|
|
2016-08-11 04:23:43 +03:00
|
|
|
static INLINE int x86_simd_caps(void) {
|
2012-07-14 02:21:29 +04:00
|
|
|
unsigned int flags = 0;
|
|
|
|
unsigned int mask = ~0;
|
2015-04-25 05:21:53 +03:00
|
|
|
unsigned int max_cpuid_val, reg_eax, reg_ebx, reg_ecx, reg_edx;
|
2012-07-14 02:21:29 +04:00
|
|
|
char *env;
|
|
|
|
(void)reg_ebx;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
/* See if the CPU capabilities are being overridden by the environment */
|
2016-08-31 00:01:10 +03:00
|
|
|
env = getenv("AOM_SIMD_CAPS");
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-11 04:23:43 +03:00
|
|
|
if (env && *env) return (int)strtol(env, NULL, 0);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
env = getenv("AOM_SIMD_CAPS_MASK");
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-11 04:23:43 +03:00
|
|
|
if (env && *env) mask = (unsigned int)strtoul(env, NULL, 0);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
/* Ensure that the CPUID instruction supports extended features */
|
2015-04-25 05:21:53 +03:00
|
|
|
cpuid(0, 0, max_cpuid_val, reg_ebx, reg_ecx, reg_edx);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-11 04:23:43 +03:00
|
|
|
if (max_cpuid_val < 1) return 0;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
/* Get the standard feature flags */
|
2013-11-20 08:11:57 +04:00
|
|
|
cpuid(1, 0, reg_eax, reg_ebx, reg_ecx, reg_edx);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (reg_edx & BIT(23)) flags |= HAS_MMX;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (reg_edx & BIT(25)) flags |= HAS_SSE; /* aka xmm */
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (reg_edx & BIT(26)) flags |= HAS_SSE2; /* aka wmt */
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2013-10-29 19:48:12 +04:00
|
|
|
if (reg_ecx & BIT(0)) flags |= HAS_SSE3;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2013-10-29 19:48:12 +04:00
|
|
|
if (reg_ecx & BIT(9)) flags |= HAS_SSSE3;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
if (reg_ecx & BIT(19)) flags |= HAS_SSE4_1;
|
2010-10-27 16:45:24 +04:00
|
|
|
|
2015-01-15 09:51:49 +03:00
|
|
|
// bits 27 (OSXSAVE) & 28 (256-bit AVX)
|
2015-01-24 01:13:51 +03:00
|
|
|
if ((reg_ecx & (BIT(27) | BIT(28))) == (BIT(27) | BIT(28))) {
|
2015-01-15 09:51:49 +03:00
|
|
|
if ((xgetbv() & 0x6) == 0x6) {
|
|
|
|
flags |= HAS_AVX;
|
2013-10-29 19:48:12 +04:00
|
|
|
|
2015-04-25 05:21:53 +03:00
|
|
|
if (max_cpuid_val >= 7) {
|
|
|
|
/* Get the leaf 7 feature flags. Needed to check for AVX2 support */
|
|
|
|
cpuid(7, 0, reg_eax, reg_ebx, reg_ecx, reg_edx);
|
2013-11-20 08:11:57 +04:00
|
|
|
|
2015-04-25 05:21:53 +03:00
|
|
|
if (reg_ebx & BIT(5)) flags |= HAS_AVX2;
|
|
|
|
}
|
2015-01-15 09:51:49 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 19:48:12 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
return flags & mask;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2016-04-22 01:59:05 +03:00
|
|
|
// Note:
|
|
|
|
// 32-bit CPU cycle counter is light-weighted for most function performance
|
|
|
|
// measurement. For large function (CPU time > a couple of seconds), 64-bit
|
|
|
|
// counter should be used.
|
|
|
|
// 32-bit CPU cycle counter
|
2016-08-11 04:23:43 +03:00
|
|
|
static INLINE unsigned int x86_readtsc(void) {
|
2010-05-18 19:58:33 +04:00
|
|
|
#if defined(__GNUC__) && __GNUC__
|
2012-07-14 02:21:29 +04:00
|
|
|
unsigned int tsc;
|
2016-08-11 04:23:43 +03:00
|
|
|
__asm__ __volatile__("rdtsc\n\t" : "=a"(tsc) :);
|
2012-07-14 02:21:29 +04:00
|
|
|
return tsc;
|
2012-11-03 02:39:14 +04:00
|
|
|
#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
|
|
|
unsigned int tsc;
|
2016-08-11 04:23:43 +03:00
|
|
|
asm volatile("rdtsc\n\t" : "=a"(tsc) :);
|
2012-11-03 02:39:14 +04:00
|
|
|
return tsc;
|
2010-05-18 19:58:33 +04:00
|
|
|
#else
|
|
|
|
#if ARCH_X86_64
|
2012-11-03 02:39:14 +04:00
|
|
|
return (unsigned int)__rdtsc();
|
2010-05-18 19:58:33 +04:00
|
|
|
#else
|
2016-08-11 04:23:43 +03:00
|
|
|
__asm rdtsc;
|
2010-05-18 19:58:33 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-22 01:59:05 +03:00
|
|
|
// 64-bit CPU cycle counter
|
2016-08-11 04:23:43 +03:00
|
|
|
static INLINE uint64_t x86_readtsc64(void) {
|
2016-04-22 01:59:05 +03:00
|
|
|
#if defined(__GNUC__) && __GNUC__
|
|
|
|
uint32_t hi, lo;
|
|
|
|
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
|
|
|
|
return ((uint64_t)hi << 32) | lo;
|
|
|
|
#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
|
|
|
uint_t hi, lo;
|
|
|
|
asm volatile("rdtsc\n\t" : "=a"(lo), "=d"(hi));
|
|
|
|
return ((uint64_t)hi << 32) | lo;
|
|
|
|
#else
|
|
|
|
#if ARCH_X86_64
|
|
|
|
return (uint64_t)__rdtsc();
|
|
|
|
#else
|
2016-08-11 04:23:43 +03:00
|
|
|
__asm rdtsc;
|
2016-04-22 01:59:05 +03:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#if defined(__GNUC__) && __GNUC__
|
2016-08-11 04:23:43 +03:00
|
|
|
#define x86_pause_hint() __asm__ __volatile__("pause \n\t")
|
2012-11-03 02:39:14 +04:00
|
|
|
#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
2016-08-11 04:23:43 +03:00
|
|
|
#define x86_pause_hint() asm volatile("pause \n\t")
|
2010-05-18 19:58:33 +04:00
|
|
|
#else
|
|
|
|
#if ARCH_X86_64
|
2016-08-11 04:23:43 +03:00
|
|
|
#define x86_pause_hint() _mm_pause();
|
2010-05-18 19:58:33 +04:00
|
|
|
#else
|
2016-08-11 04:23:43 +03:00
|
|
|
#define x86_pause_hint() __asm pause
|
2010-05-18 19:58:33 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && __GNUC__
|
2016-08-11 04:23:43 +03:00
|
|
|
static void x87_set_control_word(unsigned short mode) {
|
2013-06-18 08:58:00 +04:00
|
|
|
__asm__ __volatile__("fldcw %0" : : "m"(*&mode));
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2016-08-11 04:23:43 +03:00
|
|
|
static unsigned short x87_get_control_word(void) {
|
2012-07-14 02:21:29 +04:00
|
|
|
unsigned short mode;
|
2016-08-11 04:23:43 +03:00
|
|
|
__asm__ __volatile__("fstcw %0\n\t" : "=m"(*&mode) :);
|
|
|
|
return mode;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2012-05-02 21:14:27 +04:00
|
|
|
#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
2016-08-11 04:23:43 +03:00
|
|
|
static void x87_set_control_word(unsigned short mode) {
|
2013-06-18 08:58:00 +04:00
|
|
|
asm volatile("fldcw %0" : : "m"(*&mode));
|
2012-11-03 02:39:14 +04:00
|
|
|
}
|
2016-08-11 04:23:43 +03:00
|
|
|
static unsigned short x87_get_control_word(void) {
|
2012-11-03 02:39:14 +04:00
|
|
|
unsigned short mode;
|
2016-08-11 04:23:43 +03:00
|
|
|
asm volatile("fstcw %0\n\t" : "=m"(*&mode) :);
|
2012-11-03 02:39:14 +04:00
|
|
|
return mode;
|
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
#elif ARCH_X86_64
|
|
|
|
/* No fldcw intrinsics on Windows x64, punt to external asm */
|
2016-08-31 00:01:10 +03:00
|
|
|
extern void aom_winx64_fldcw(unsigned short mode);
|
|
|
|
extern unsigned short aom_winx64_fstcw(void);
|
|
|
|
#define x87_set_control_word aom_winx64_fldcw
|
|
|
|
#define x87_get_control_word aom_winx64_fstcw
|
2010-05-18 19:58:33 +04:00
|
|
|
#else
|
2016-08-11 04:23:43 +03:00
|
|
|
static void x87_set_control_word(unsigned short mode) {
|
2012-07-14 02:21:29 +04:00
|
|
|
__asm { fldcw mode }
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2016-08-11 04:23:43 +03:00
|
|
|
static unsigned short x87_get_control_word(void) {
|
2012-07-14 02:21:29 +04:00
|
|
|
unsigned short mode;
|
|
|
|
__asm { fstcw mode }
|
|
|
|
return mode;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-11 04:23:43 +03:00
|
|
|
static INLINE unsigned int x87_set_double_precision(void) {
|
2014-08-13 03:51:07 +04:00
|
|
|
unsigned int mode = x87_get_control_word();
|
2016-08-11 04:23:43 +03:00
|
|
|
x87_set_control_word((mode & ~0x300) | 0x200);
|
2012-07-14 02:21:29 +04:00
|
|
|
return mode;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
extern void aom_reset_mmx_state(void);
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#endif // AOM_PORTS_X86_H_
|