]> granicus.if.org Git - libvpx/blob - vpx_ports/x86.h
Merge "Relocate memory operations for common code"
[libvpx] / vpx_ports / x86.h
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11
12 #ifndef VPX_PORTS_X86_H_
13 #define VPX_PORTS_X86_H_
14 #include <stdlib.h>
15 #include "vpx_config.h"
16 #include "vpx/vpx_integer.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 typedef enum {
23   VPX_CPU_UNKNOWN = -1,
24   VPX_CPU_AMD,
25   VPX_CPU_AMD_OLD,
26   VPX_CPU_CENTAUR,
27   VPX_CPU_CYRIX,
28   VPX_CPU_INTEL,
29   VPX_CPU_NEXGEN,
30   VPX_CPU_NSC,
31   VPX_CPU_RISE,
32   VPX_CPU_SIS,
33   VPX_CPU_TRANSMETA,
34   VPX_CPU_TRANSMETA_OLD,
35   VPX_CPU_UMC,
36   VPX_CPU_VIA,
37
38   VPX_CPU_LAST
39 }  vpx_cpu_t;
40
41 #if defined(__GNUC__) && __GNUC__ || defined(__ANDROID__)
42 #if ARCH_X86_64
43 #define cpuid(func, func2, ax, bx, cx, dx)\
44   __asm__ __volatile__ (\
45                         "cpuid           \n\t" \
46                         : "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) \
47                         : "a" (func), "c" (func2));
48 #else
49 #define cpuid(func, func2, ax, bx, cx, dx)\
50   __asm__ __volatile__ (\
51                         "mov %%ebx, %%edi   \n\t" \
52                         "cpuid              \n\t" \
53                         "xchg %%edi, %%ebx  \n\t" \
54                         : "=a" (ax), "=D" (bx), "=c" (cx), "=d" (dx) \
55                         : "a" (func), "c" (func2));
56 #endif
57 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) /* end __GNUC__ or __ANDROID__*/
58 #if ARCH_X86_64
59 #define cpuid(func, func2, ax, bx, cx, dx)\
60   asm volatile (\
61                 "xchg %rsi, %rbx \n\t" \
62                 "cpuid           \n\t" \
63                 "movl %ebx, %edi \n\t" \
64                 "xchg %rsi, %rbx \n\t" \
65                 : "=a" (ax), "=D" (bx), "=c" (cx), "=d" (dx) \
66                 : "a" (func), "c" (func2));
67 #else
68 #define cpuid(func, func2, ax, bx, cx, dx)\
69   asm volatile (\
70                 "pushl %ebx       \n\t" \
71                 "cpuid            \n\t" \
72                 "movl %ebx, %edi  \n\t" \
73                 "popl %ebx        \n\t" \
74                 : "=a" (ax), "=D" (bx), "=c" (cx), "=d" (dx) \
75                 : "a" (func), "c" (func2));
76 #endif
77 #else /* end __SUNPRO__ */
78 #if ARCH_X86_64
79 #if defined(_MSC_VER) && _MSC_VER > 1500
80 void __cpuidex(int CPUInfo[4], int info_type, int ecxvalue);
81 #pragma intrinsic(__cpuidex)
82 #define cpuid(func, func2, a, b, c, d) do {\
83     int regs[4];\
84     __cpuidex(regs, func, func2); \
85     a = regs[0];  b = regs[1];  c = regs[2];  d = regs[3];\
86   } while(0)
87 #else
88 void __cpuid(int CPUInfo[4], int info_type);
89 #pragma intrinsic(__cpuid)
90 #define cpuid(func, func2, a, b, c, d) do {\
91     int regs[4];\
92     __cpuid(regs, func); \
93     a = regs[0];  b = regs[1];  c = regs[2];  d = regs[3];\
94   } while (0)
95 #endif
96 #else
97 #define cpuid(func, func2, a, b, c, d)\
98   __asm mov eax, func\
99   __asm mov ecx, func2\
100   __asm cpuid\
101   __asm mov a, eax\
102   __asm mov b, ebx\
103   __asm mov c, ecx\
104   __asm mov d, edx
105 #endif
106 #endif /* end others */
107
108 // NaCl has no support for xgetbv or the raw opcode.
109 #if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__))
110 static INLINE uint64_t xgetbv(void) {
111   const uint32_t ecx = 0;
112   uint32_t eax, edx;
113   // Use the raw opcode for xgetbv for compatibility with older toolchains.
114   __asm__ volatile (
115     ".byte 0x0f, 0x01, 0xd0\n"
116     : "=a"(eax), "=d"(edx) : "c" (ecx));
117   return ((uint64_t)edx << 32) | eax;
118 }
119 #elif (defined(_M_X64) || defined(_M_IX86)) && \
120       defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219  // >= VS2010 SP1
121 #include <immintrin.h>
122 #define xgetbv() _xgetbv(0)
123 #elif defined(_MSC_VER) && defined(_M_IX86)
124 static INLINE uint64_t xgetbv(void) {
125   uint32_t eax_, edx_;
126   __asm {
127     xor ecx, ecx  // ecx = 0
128     // Use the raw opcode for xgetbv for compatibility with older toolchains.
129     __asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0
130     mov eax_, eax
131     mov edx_, edx
132   }
133   return ((uint64_t)edx_ << 32) | eax_;
134 }
135 #else
136 #define xgetbv() 0U  // no AVX for older x64 or unrecognized toolchains.
137 #endif
138
139 #define HAS_MMX     0x01
140 #define HAS_SSE     0x02
141 #define HAS_SSE2    0x04
142 #define HAS_SSE3    0x08
143 #define HAS_SSSE3   0x10
144 #define HAS_SSE4_1  0x20
145 #define HAS_AVX     0x40
146 #define HAS_AVX2    0x80
147 #ifndef BIT
148 #define BIT(n) (1<<n)
149 #endif
150
151 static INLINE int
152 x86_simd_caps(void) {
153   unsigned int flags = 0;
154   unsigned int mask = ~0;
155   unsigned int max_cpuid_val, reg_eax, reg_ebx, reg_ecx, reg_edx;
156   char *env;
157   (void)reg_ebx;
158
159   /* See if the CPU capabilities are being overridden by the environment */
160   env = getenv("VPX_SIMD_CAPS");
161
162   if (env && *env)
163     return (int)strtol(env, NULL, 0);
164
165   env = getenv("VPX_SIMD_CAPS_MASK");
166
167   if (env && *env)
168     mask = strtol(env, NULL, 0);
169
170   /* Ensure that the CPUID instruction supports extended features */
171   cpuid(0, 0, max_cpuid_val, reg_ebx, reg_ecx, reg_edx);
172
173   if (max_cpuid_val < 1)
174     return 0;
175
176   /* Get the standard feature flags */
177   cpuid(1, 0, reg_eax, reg_ebx, reg_ecx, reg_edx);
178
179   if (reg_edx & BIT(23)) flags |= HAS_MMX;
180
181   if (reg_edx & BIT(25)) flags |= HAS_SSE; /* aka xmm */
182
183   if (reg_edx & BIT(26)) flags |= HAS_SSE2; /* aka wmt */
184
185   if (reg_ecx & BIT(0)) flags |= HAS_SSE3;
186
187   if (reg_ecx & BIT(9)) flags |= HAS_SSSE3;
188
189   if (reg_ecx & BIT(19)) flags |= HAS_SSE4_1;
190
191   // bits 27 (OSXSAVE) & 28 (256-bit AVX)
192   if ((reg_ecx & (BIT(27) | BIT(28))) == (BIT(27) | BIT(28))) {
193     if ((xgetbv() & 0x6) == 0x6) {
194       flags |= HAS_AVX;
195
196       if (max_cpuid_val >= 7) {
197         /* Get the leaf 7 feature flags. Needed to check for AVX2 support */
198         cpuid(7, 0, reg_eax, reg_ebx, reg_ecx, reg_edx);
199
200         if (reg_ebx & BIT(5)) flags |= HAS_AVX2;
201       }
202     }
203   }
204
205   return flags & mask;
206 }
207
208 #if ARCH_X86_64 && defined(_MSC_VER)
209 unsigned __int64 __rdtsc(void);
210 #pragma intrinsic(__rdtsc)
211 #endif
212 static INLINE unsigned int
213 x86_readtsc(void) {
214 #if defined(__GNUC__) && __GNUC__
215   unsigned int tsc;
216   __asm__ __volatile__("rdtsc\n\t":"=a"(tsc):);
217   return tsc;
218 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
219   unsigned int tsc;
220   asm volatile("rdtsc\n\t":"=a"(tsc):);
221   return tsc;
222 #else
223 #if ARCH_X86_64
224   return (unsigned int)__rdtsc();
225 #else
226   __asm  rdtsc;
227 #endif
228 #endif
229 }
230
231
232 #if defined(__GNUC__) && __GNUC__
233 #define x86_pause_hint()\
234   __asm__ __volatile__ ("pause \n\t")
235 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
236 #define x86_pause_hint()\
237   asm volatile ("pause \n\t")
238 #else
239 #if ARCH_X86_64
240 #define x86_pause_hint()\
241   _mm_pause();
242 #else
243 #define x86_pause_hint()\
244   __asm pause
245 #endif
246 #endif
247
248 #if defined(__GNUC__) && __GNUC__
249 static void
250 x87_set_control_word(unsigned short mode) {
251   __asm__ __volatile__("fldcw %0" : : "m"(*&mode));
252 }
253 static unsigned short
254 x87_get_control_word(void) {
255   unsigned short mode;
256   __asm__ __volatile__("fstcw %0\n\t":"=m"(*&mode):);
257     return mode;
258 }
259 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
260 static void
261 x87_set_control_word(unsigned short mode) {
262   asm volatile("fldcw %0" : : "m"(*&mode));
263 }
264 static unsigned short
265 x87_get_control_word(void) {
266   unsigned short mode;
267   asm volatile("fstcw %0\n\t":"=m"(*&mode):);
268   return mode;
269 }
270 #elif ARCH_X86_64
271 /* No fldcw intrinsics on Windows x64, punt to external asm */
272 extern void           vpx_winx64_fldcw(unsigned short mode);
273 extern unsigned short vpx_winx64_fstcw(void);
274 #define x87_set_control_word vpx_winx64_fldcw
275 #define x87_get_control_word vpx_winx64_fstcw
276 #else
277 static void
278 x87_set_control_word(unsigned short mode) {
279   __asm { fldcw mode }
280 }
281 static unsigned short
282 x87_get_control_word(void) {
283   unsigned short mode;
284   __asm { fstcw mode }
285   return mode;
286 }
287 #endif
288
289 static INLINE unsigned int
290 x87_set_double_precision(void) {
291   unsigned int mode = x87_get_control_word();
292   x87_set_control_word((mode&~0x300) | 0x200);
293   return mode;
294 }
295
296
297 extern void vpx_reset_mmx_state(void);
298
299 #ifdef __cplusplus
300 }  // extern "C"
301 #endif
302
303 #endif  // VPX_PORTS_X86_H_