/src/skia/src/core/SkCpu.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016 Google Inc. |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style license that can be |
5 | | * found in the LICENSE file. |
6 | | */ |
7 | | |
8 | | #include "include/core/SkStream.h" |
9 | | #include "include/core/SkString.h" |
10 | | #include "include/private/SkOnce.h" |
11 | | #include "src/core/SkCpu.h" |
12 | | |
13 | | #if defined(SK_CPU_X86) |
14 | | #if defined(_MSC_VER) |
15 | | #include <intrin.h> |
16 | | static void cpuid (uint32_t abcd[4]) { __cpuid ((int*)abcd, 1); } |
17 | | static void cpuid7(uint32_t abcd[4]) { __cpuidex((int*)abcd, 7, 0); } |
18 | | static uint64_t xgetbv(uint32_t xcr) { return _xgetbv(xcr); } |
19 | | #else |
20 | | #include <cpuid.h> |
21 | | #if !defined(__cpuid_count) // Old Mac Clang doesn't have this defined. |
22 | | #define __cpuid_count(eax, ecx, a, b, c, d) \ |
23 | | __asm__("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(eax), "2"(ecx)) |
24 | | #endif |
25 | 0 | static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); } |
26 | 0 | static void cpuid7(uint32_t abcd[4]) { |
27 | 0 | __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]); |
28 | 0 | } |
29 | 0 | static uint64_t xgetbv(uint32_t xcr) { |
30 | 0 | uint32_t eax, edx; |
31 | 0 | __asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); |
32 | 0 | return (uint64_t)(edx) << 32 | eax; |
33 | 0 | } |
34 | | #endif |
35 | | |
36 | 0 | static uint32_t read_cpu_features() { |
37 | 0 | uint32_t features = 0; |
38 | 0 | uint32_t abcd[4] = {0,0,0,0}; |
39 | | |
40 | | // You might want to refer to http://www.sandpile.org/x86/cpuid.htm |
41 | |
|
42 | 0 | cpuid(abcd); |
43 | 0 | if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; } |
44 | 0 | if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; } |
45 | 0 | if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; } |
46 | 0 | if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; } |
47 | 0 | if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; } |
48 | 0 | if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; } |
49 | |
|
50 | 0 | if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE |
51 | 0 | && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled. |
52 | 0 | if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; } |
53 | 0 | if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; } |
54 | 0 | if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; } |
55 | |
|
56 | 0 | cpuid7(abcd); |
57 | 0 | if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; } |
58 | 0 | if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; } |
59 | 0 | if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; } |
60 | 0 | if (abcd[1] & (1<<9)) { features |= SkCpu::ERMS; } |
61 | |
|
62 | 0 | if ((xgetbv(0) & (7<<5)) == (7<<5)) { // All ZMM state bits enabled too. |
63 | 0 | if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; } |
64 | 0 | if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; } |
65 | 0 | if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; } |
66 | 0 | if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; } |
67 | 0 | if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; } |
68 | 0 | if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; } |
69 | 0 | if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; } |
70 | 0 | if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; } |
71 | 0 | } |
72 | 0 | } |
73 | 0 | return features; |
74 | 0 | } |
75 | | |
76 | | #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>) |
77 | | #include <sys/auxv.h> |
78 | | |
79 | | static uint32_t read_cpu_features() { |
80 | | const uint32_t kHWCAP_CRC32 = (1<< 7), |
81 | | kHWCAP_ASIMDHP = (1<<10); |
82 | | |
83 | | uint32_t features = 0; |
84 | | uint32_t hwcaps = getauxval(AT_HWCAP); |
85 | | if (hwcaps & kHWCAP_CRC32 ) { features |= SkCpu::CRC32; } |
86 | | if (hwcaps & kHWCAP_ASIMDHP) { features |= SkCpu::ASIMDHP; } |
87 | | |
88 | | // The Samsung Mongoose 3 core sets the ASIMDHP bit but doesn't support it. |
89 | | for (int core = 0; features & SkCpu::ASIMDHP; core++) { |
90 | | // These /sys files contain the core's MIDR_EL1 register, the source of |
91 | | // CPU {implementer, variant, part, revision} you'd see in /proc/cpuinfo. |
92 | | SkString path = |
93 | | SkStringPrintf("/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1", core); |
94 | | |
95 | | // Can't use SkData::MakeFromFileName() here, I think because /sys can't be mmap()'d. |
96 | | SkFILEStream midr_el1(path.c_str()); |
97 | | if (!midr_el1.isValid()) { |
98 | | // This is our ordinary exit path. |
99 | | // If we ask for MIDR_EL1 from a core that doesn't exist, we've checked all cores. |
100 | | if (core == 0) { |
101 | | // On the other hand, if we can't read MIDR_EL1 from any core, assume the worst. |
102 | | features &= ~(SkCpu::ASIMDHP); |
103 | | } |
104 | | break; |
105 | | } |
106 | | |
107 | | const char kMongoose3[] = "0x00000000531f0020"; // 53 == Samsung. |
108 | | char buf[SK_ARRAY_COUNT(kMongoose3) - 1]; // No need for the terminating \0. |
109 | | |
110 | | if (SK_ARRAY_COUNT(buf) != midr_el1.read(buf, SK_ARRAY_COUNT(buf)) |
111 | | || 0 == memcmp(kMongoose3, buf, SK_ARRAY_COUNT(buf))) { |
112 | | features &= ~(SkCpu::ASIMDHP); |
113 | | } |
114 | | } |
115 | | return features; |
116 | | } |
117 | | |
118 | | #elif defined(SK_CPU_ARM32) && __has_include(<sys/auxv.h>) && \ |
119 | | (!defined(__ANDROID_API__) || __ANDROID_API__ >= 18) |
120 | | // sys/auxv.h will always be present in the Android NDK due to unified |
121 | | //headers, but getauxval is only defined for API >= 18. |
122 | | #include <sys/auxv.h> |
123 | | |
124 | | static uint32_t read_cpu_features() { |
125 | | const uint32_t kHWCAP_NEON = (1<<12); |
126 | | const uint32_t kHWCAP_VFPv4 = (1<<16); |
127 | | |
128 | | uint32_t features = 0; |
129 | | uint32_t hwcaps = getauxval(AT_HWCAP); |
130 | | if (hwcaps & kHWCAP_NEON ) { |
131 | | features |= SkCpu::NEON; |
132 | | if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON_FMA|SkCpu::VFP_FP16; } |
133 | | } |
134 | | return features; |
135 | | } |
136 | | |
137 | | #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>) |
138 | | #include <cpu-features.h> |
139 | | |
140 | | static uint32_t read_cpu_features() { |
141 | | uint32_t features = 0; |
142 | | uint64_t cpu_features = android_getCpuFeatures(); |
143 | | if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) { features |= SkCpu::NEON; } |
144 | | if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= SkCpu::NEON_FMA; } |
145 | | if (cpu_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= SkCpu::VFP_FP16; } |
146 | | return features; |
147 | | } |
148 | | |
149 | | #else |
150 | | static uint32_t read_cpu_features() { |
151 | | return 0; |
152 | | } |
153 | | |
154 | | #endif |
155 | | |
156 | | uint32_t SkCpu::gCachedFeatures = 0; |
157 | | |
158 | 0 | void SkCpu::CacheRuntimeFeatures() { |
159 | 0 | static SkOnce once; |
160 | 0 | once([] { gCachedFeatures = read_cpu_features(); }); |
161 | 0 | } |