OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The LibYuv Project Authors. All rights reserved. | 2 * Copyright 2011 The LibYuv Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "libyuv/cpu_id.h" | 11 #include "libyuv/cpu_id.h" |
12 | 12 |
13 #if defined(_MSC_VER) && !defined(__clang__) | 13 #if (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__) |
14 #include <intrin.h> // For __cpuidex() | 14 #include <intrin.h> // For __cpuidex() |
15 #endif | 15 #endif |
16 #if !defined(__pnacl__) && !defined(__CLR_VER) && \ | 16 #if !defined(__pnacl__) && !defined(__CLR_VER) && \ |
17 !defined(__native_client__) && \ | 17 !defined(__native_client__) && (defined(_M_IX86) || defined(_M_X64)) && \ |
18 defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219) && \ | 18 defined(_MSC_VER) && !defined(__clang__) && (_MSC_FULL_VER >= 160040219) |
19 (defined(_M_IX86) || defined(_M_X64)) | |
20 #include <immintrin.h> // For _xgetbv() | 19 #include <immintrin.h> // For _xgetbv() |
21 #endif | 20 #endif |
22 | 21 |
23 #if !defined(__native_client__) | 22 #if !defined(__native_client__) |
24 #include <stdlib.h> // For getenv() | 23 #include <stdlib.h> // For getenv() |
25 #endif | 24 #endif |
26 | 25 |
27 // For ArmCpuCaps() but unittested on all platforms | 26 // For ArmCpuCaps() but unittested on all platforms |
28 #include <stdio.h> | 27 #include <stdio.h> |
29 #include <string.h> | 28 #include <string.h> |
30 | 29 |
31 #include "libyuv/basic_types.h" // For CPU_X86 | 30 #include "libyuv/basic_types.h" // For CPU_X86 |
32 | 31 |
33 #ifdef __cplusplus | 32 #ifdef __cplusplus |
34 namespace libyuv { | 33 namespace libyuv { |
35 extern "C" { | 34 extern "C" { |
36 #endif | 35 #endif |
37 | 36 |
38 // For functions that use the stack and have runtime checks for overflow, | 37 // For functions that use the stack and have runtime checks for overflow, |
39 // use SAFEBUFFERS to avoid additional check. | 38 // use SAFEBUFFERS to avoid additional check. |
40 #if defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219) | 39 #if (defined(_MSC_VER) && !defined(__clang__)) && (_MSC_FULL_VER >= 160040219) |
41 #define SAFEBUFFERS __declspec(safebuffers) | 40 #define SAFEBUFFERS __declspec(safebuffers) |
42 #else | 41 #else |
43 #define SAFEBUFFERS | 42 #define SAFEBUFFERS |
44 #endif | 43 #endif |
45 | 44 |
46 // Low level cpuid for X86. Returns zeros on other CPUs. | 45 // Low level cpuid for X86. |
47 #if !defined(__pnacl__) && !defined(__CLR_VER) && \ | 46 #if (defined(_M_IX86) || defined(_M_X64) || \ |
48 (defined(_M_IX86) || defined(_M_X64) || \ | 47 defined(__i386__) || defined(__x86_64__)) && \ |
49 defined(__i386__) || defined(__x86_64__)) | 48 !defined(__pnacl__) && !defined(__CLR_VER) |
50 LIBYUV_API | 49 LIBYUV_API |
51 void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) { | 50 void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) { |
52 #if defined(_MSC_VER) && !defined(__clang__) | 51 #if (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__) |
| 52 // Visual C version uses intrinsic or inline x86 assembly. |
53 #if (_MSC_FULL_VER >= 160040219) | 53 #if (_MSC_FULL_VER >= 160040219) |
54 __cpuidex((int*)(cpu_info), info_eax, info_ecx); | 54 __cpuidex((int*)(cpu_info), info_eax, info_ecx); |
55 #endif | 55 #elif defined(_M_IX86) |
56 #if defined(_M_IX86) | |
57 __asm { | 56 __asm { |
58 mov eax, info_eax | 57 mov eax, info_eax |
59 mov ecx, info_ecx | 58 mov ecx, info_ecx |
60 mov edi, cpu_info | 59 mov edi, cpu_info |
61 cpuid | 60 cpuid |
62 mov [edi], eax | 61 mov [edi], eax |
63 mov [edi + 4], ebx | 62 mov [edi + 4], ebx |
64 mov [edi + 8], ecx | 63 mov [edi + 8], ecx |
65 mov [edi + 12], edx | 64 mov [edi + 12], edx |
66 } | 65 } |
67 #else | 66 #else |
68 if (info_ecx == 0) { | 67 if (info_ecx == 0) { |
69 __cpuid((int*)(cpu_info), info_eax); | 68 __cpuid((int*)(cpu_info), info_eax); |
70 } else { | 69 } else { |
71 cpu_info[3] = cpu_info[2] = cpu_info[1] = cpu_info[0] = 0; | 70 cpu_info[3] = cpu_info[2] = cpu_info[1] = cpu_info[0] = 0; |
72 } | 71 } |
73 #endif | 72 #endif |
74 #else // defined(_MSC_VER) | 73 // GCC version uses inline x86 assembly. |
| 74 #else // (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__) |
75 uint32 info_ebx, info_edx; | 75 uint32 info_ebx, info_edx; |
76 asm volatile ( // NOLINT | 76 asm volatile ( // NOLINT |
77 #if defined( __i386__) && defined(__PIC__) | 77 #if defined( __i386__) && defined(__PIC__) |
78 // Preserve ebx for fpic 32 bit. | 78 // Preserve ebx for fpic 32 bit. |
79 "mov %%ebx, %%edi \n" | 79 "mov %%ebx, %%edi \n" |
80 "cpuid \n" | 80 "cpuid \n" |
81 "xchg %%edi, %%ebx \n" | 81 "xchg %%edi, %%ebx \n" |
82 : "=D" (info_ebx), | 82 : "=D" (info_ebx), |
83 #else | 83 #else |
84 "cpuid \n" | 84 "cpuid \n" |
85 : "=b" (info_ebx), | 85 : "=b" (info_ebx), |
86 #endif // defined( __i386__) && defined(__PIC__) | 86 #endif // defined( __i386__) && defined(__PIC__) |
87 "+a" (info_eax), "+c" (info_ecx), "=d" (info_edx)); | 87 "+a" (info_eax), "+c" (info_ecx), "=d" (info_edx)); |
88 cpu_info[0] = info_eax; | 88 cpu_info[0] = info_eax; |
89 cpu_info[1] = info_ebx; | 89 cpu_info[1] = info_ebx; |
90 cpu_info[2] = info_ecx; | 90 cpu_info[2] = info_ecx; |
91 cpu_info[3] = info_edx; | 91 cpu_info[3] = info_edx; |
92 #endif // defined(_MSC_VER) | 92 #endif // (defined(_MSC_VER) && !defined(__clang__)) && !defined(__clang__) |
93 } | 93 } |
94 | 94 #else // (defined(_M_IX86) || defined(_M_X64) ... |
95 #if !defined(__native_client__) | |
96 #define HAS_XGETBV | |
97 // X86 CPUs have xgetbv to detect OS saves high parts of ymm registers. | |
98 int TestOsSaveYmm() { | |
99 uint32 xcr0 = 0u; | |
100 #if defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219) | |
101 xcr0 = (uint32)(_xgetbv(0)); // VS2010 SP1 required. | |
102 #endif | |
103 #if defined(_M_IX86) && defined(_MSC_VER) | |
104 __asm { | |
105 xor ecx, ecx // xcr 0 | |
106 _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0 // For VS2010 and earlier. | |
107 mov xcr0, eax | |
108 } | |
109 #endif | |
110 #if defined(__i386__) || defined(__x86_64__) | |
111 asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx"); | |
112 #endif // defined(_MSC_VER) | |
113 return((xcr0 & 6) == 6); // Is ymm saved? | |
114 } | |
115 #endif // !defined(__native_client__) | |
116 #else | |
117 LIBYUV_API | 95 LIBYUV_API |
118 void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) { | 96 void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) { |
119 cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0; | 97 cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0; |
120 } | 98 } |
121 #endif | 99 #endif |
122 | 100 |
| 101 // TODO(fbarchard): Enable xgetbv when validator supports it. |
| 102 #if (defined(_M_IX86) || defined(_M_X64) || \ |
| 103 defined(__i386__) || defined(__x86_64__)) && \ |
| 104 !defined(__pnacl__) && !defined(__CLR_VER) && !defined(__native_client__) |
| 105 #define HAS_XGETBV |
| 106 // X86 CPUs have xgetbv to detect OS saves high parts of ymm registers. |
| 107 int TestOsSaveYmm() { |
| 108 uint32 xcr0 = 0u; |
| 109 #if (defined(_MSC_VER) && !defined(__clang__)) && (_MSC_FULL_VER >= 160040219) |
| 110 xcr0 = (uint32)(_xgetbv(0)); // VS2010 SP1 required. |
| 111 #elif defined(_M_IX86) && defined(_MSC_VER) && !defined(__clang__) |
| 112 __asm { |
| 113 xor ecx, ecx // xcr 0 |
| 114 _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0 // For VS2010 and earlier. |
| 115 mov xcr0, eax |
| 116 } |
| 117 #elif defined(__i386__) || defined(__x86_64__) |
| 118 asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx"); |
| 119 #endif // defined(__i386__) || defined(__x86_64__) |
| 120 return((xcr0 & 6) == 6); // Is ymm saved? |
| 121 } |
| 122 #endif // defined(_M_IX86) || defined(_M_X64) .. |
| 123 |
123 // based on libvpx arm_cpudetect.c | 124 // based on libvpx arm_cpudetect.c |
124 // For Arm, but public to allow testing on any CPU | 125 // For Arm, but public to allow testing on any CPU |
125 LIBYUV_API SAFEBUFFERS | 126 LIBYUV_API SAFEBUFFERS |
126 int ArmCpuCaps(const char* cpuinfo_name) { | 127 int ArmCpuCaps(const char* cpuinfo_name) { |
127 char cpuinfo_line[512]; | 128 char cpuinfo_line[512]; |
128 FILE* f = fopen(cpuinfo_name, "r"); | 129 FILE* f = fopen(cpuinfo_name, "r"); |
129 if (!f) { | 130 if (!f) { |
130 // Assume Neon if /proc/cpuinfo is unavailable. | 131 // Assume Neon if /proc/cpuinfo is unavailable. |
131 // This will occur for Chrome sandbox for Pepper or Render process. | 132 // This will occur for Chrome sandbox for Pepper or Render process. |
132 return kCpuHasNEON; | 133 return kCpuHasNEON; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 | 298 |
298 LIBYUV_API | 299 LIBYUV_API |
299 void MaskCpuFlags(int enable_flags) { | 300 void MaskCpuFlags(int enable_flags) { |
300 cpu_info_ = InitCpuFlags() & enable_flags; | 301 cpu_info_ = InitCpuFlags() & enable_flags; |
301 } | 302 } |
302 | 303 |
303 #ifdef __cplusplus | 304 #ifdef __cplusplus |
304 } // extern "C" | 305 } // extern "C" |
305 } // namespace libyuv | 306 } // namespace libyuv |
306 #endif | 307 #endif |
OLD | NEW |