2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #ifndef VPX_PORTS_X86_H_
13 #define VPX_PORTS_X86_H_
15 #include "vpx_config.h"
33 VPX_CPU_TRANSMETA_OLD
,
40 #if defined(__GNUC__) && __GNUC__ || defined(__ANDROID__)
42 #define cpuid(func, func2, ax, bx, cx, dx)\
43 __asm__ __volatile__ (\
45 : "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) \
46 : "a" (func), "c" (func2));
48 #define cpuid(func, func2, ax, bx, cx, dx)\
49 __asm__ __volatile__ (\
50 "mov %%ebx, %%edi \n\t" \
52 "xchg %%edi, %%ebx \n\t" \
53 : "=a" (ax), "=D" (bx), "=c" (cx), "=d" (dx) \
54 : "a" (func), "c" (func2));
56 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) /* end __GNUC__ or __ANDROID__*/
58 #define cpuid(func, func2, ax, bx, cx, dx)\
60 "xchg %rsi, %rbx \n\t" \
62 "movl %ebx, %edi \n\t" \
63 "xchg %rsi, %rbx \n\t" \
64 : "=a" (ax), "=D" (bx), "=c" (cx), "=d" (dx) \
65 : "a" (func), "c" (func2));
67 #define cpuid(func, func2, ax, bx, cx, dx)\
71 "movl %ebx, %edi \n\t" \
73 : "=a" (ax), "=D" (bx), "=c" (cx), "=d" (dx) \
74 : "a" (func), "c" (func2));
76 #else /* end __SUNPRO__ */
78 #if defined(_MSC_VER) && _MSC_VER > 1500
79 void __cpuidex(int CPUInfo
[4], int info_type
, int ecxvalue
);
80 #pragma intrinsic(__cpuidex)
81 #define cpuid(func, func2, a, b, c, d) do {\
83 __cpuidex(regs, func, func2); \
84 a = regs[0]; b = regs[1]; c = regs[2]; d = regs[3];\
87 void __cpuid(int CPUInfo
[4], int info_type
);
88 #pragma intrinsic(__cpuid)
89 #define cpuid(func, func2, a, b, c, d) do {\
91 __cpuid(regs, func); \
92 a = regs[0]; b = regs[1]; c = regs[2]; d = regs[3];\
96 #define cpuid(func, func2, a, b, c, d)\
105 #endif /* end others */
109 #define HAS_SSE2 0x04
110 #define HAS_SSE3 0x08
111 #define HAS_SSSE3 0x10
112 #define HAS_SSE4_1 0x20
114 #define HAS_AVX2 0x80
116 #define BIT(n) (1<<n)
120 x86_simd_caps(void) {
121 unsigned int flags
= 0;
122 unsigned int mask
= ~0;
123 unsigned int reg_eax
, reg_ebx
, reg_ecx
, reg_edx
;
127 /* See if the CPU capabilities are being overridden by the environment */
128 env
= getenv("VPX_SIMD_CAPS");
131 return (int)strtol(env
, NULL
, 0);
133 env
= getenv("VPX_SIMD_CAPS_MASK");
136 mask
= strtol(env
, NULL
, 0);
138 /* Ensure that the CPUID instruction supports extended features */
139 cpuid(0, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
144 /* Get the standard feature flags */
145 cpuid(1, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
147 if (reg_edx
& BIT(23)) flags
|= HAS_MMX
;
149 if (reg_edx
& BIT(25)) flags
|= HAS_SSE
; /* aka xmm */
151 if (reg_edx
& BIT(26)) flags
|= HAS_SSE2
; /* aka wmt */
153 if (reg_ecx
& BIT(0)) flags
|= HAS_SSE3
;
155 if (reg_ecx
& BIT(9)) flags
|= HAS_SSSE3
;
157 if (reg_ecx
& BIT(19)) flags
|= HAS_SSE4_1
;
159 if (reg_ecx
& BIT(28)) flags
|= HAS_AVX
;
161 /* Get the leaf 7 feature flags. Needed to check for AVX2 support */
164 cpuid(7, 0, reg_eax
, reg_ebx
, reg_ecx
, reg_edx
);
166 if (reg_ebx
& BIT(5)) flags
|= HAS_AVX2
;
171 #if ARCH_X86_64 && defined(_MSC_VER)
172 unsigned __int64
__rdtsc(void);
173 #pragma intrinsic(__rdtsc)
175 static INLINE
unsigned int
177 #if defined(__GNUC__) && __GNUC__
179 __asm__
__volatile__("rdtsc\n\t":"=a"(tsc
):);
181 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
183 asm volatile("rdtsc\n\t":"=a"(tsc
):);
187 return (unsigned int)__rdtsc();
195 #if defined(__GNUC__) && __GNUC__
196 #define x86_pause_hint()\
197 __asm__ __volatile__ ("pause \n\t")
198 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
199 #define x86_pause_hint()\
200 asm volatile ("pause \n\t")
203 #define x86_pause_hint()\
206 #define x86_pause_hint()\
211 #if defined(__GNUC__) && __GNUC__
213 x87_set_control_word(unsigned short mode
) {
214 __asm__
__volatile__("fldcw %0" : : "m"(*&mode
));
216 static unsigned short
217 x87_get_control_word(void) {
219 __asm__
__volatile__("fstcw %0\n\t":"=m"(*&mode
):);
222 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
224 x87_set_control_word(unsigned short mode
) {
225 asm volatile("fldcw %0" : : "m"(*&mode
));
227 static unsigned short
228 x87_get_control_word(void) {
230 asm volatile("fstcw %0\n\t":"=m"(*&mode
):);
234 /* No fldcw intrinsics on Windows x64, punt to external asm */
235 extern void vpx_winx64_fldcw(unsigned short mode
);
236 extern unsigned short vpx_winx64_fstcw(void);
237 #define x87_set_control_word vpx_winx64_fldcw
238 #define x87_get_control_word vpx_winx64_fstcw
241 x87_set_control_word(unsigned short mode
) {
244 static unsigned short
245 x87_get_control_word(void) {
252 static INLINE
unsigned int
253 x87_set_double_precision(void) {
254 unsigned int mode
= x87_get_control_word();
255 x87_set_control_word((mode
&~0x300) | 0x200);
260 extern void vpx_reset_mmx_state(void);
266 #endif // VPX_PORTS_X86_H_