blob: a88f49802ff84bfac937488ca59276b6fb190fde [file] [log] [blame]
mtklein4311f012016-04-19 14:00:13 -07001/*
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
Mike Kleind9430292019-05-24 14:28:52 -05008#include "include/core/SkStream.h"
9#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkOnce.h"
11#include "src/core/SkCpu.h"
mtklein4311f012016-04-19 14:00:13 -070012
13#if defined(SK_CPU_X86)
Mike Klein6f9a37f2020-02-11 09:44:48 -060014 #if defined(_MSC_VER)
mtklein4311f012016-04-19 14:00:13 -070015 #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 static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); }
26 static void cpuid7(uint32_t abcd[4]) {
27 __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]);
28 }
29 static uint64_t xgetbv(uint32_t xcr) {
30 uint32_t eax, edx;
31 __asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
32 return (uint64_t)(edx) << 32 | eax;
33 }
34 #endif
35
36 static uint32_t read_cpu_features() {
37 uint32_t features = 0;
38 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 cpuid(abcd);
43 if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; }
44 if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; }
45 if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; }
46 if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; }
47 if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; }
48 if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; }
49
Mike Kleinc6a449d2017-02-28 20:15:26 -050050 if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE
51 && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled.
mtklein4311f012016-04-19 14:00:13 -070052 if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; }
53 if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; }
54 if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; }
55
56 cpuid7(abcd);
57 if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; }
Mike Klein78d5a3b2016-09-30 10:48:01 -040058 if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; }
59 if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; }
Mike Klein7ac2be22020-11-05 09:38:53 -060060 if (abcd[1] & (1<<9)) { features |= SkCpu::ERMS; }
Mike Kleinc6a449d2017-02-28 20:15:26 -050061
62 if ((xgetbv(0) & (7<<5)) == (7<<5)) { // All ZMM state bits enabled too.
63 if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; }
64 if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; }
65 if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; }
66 if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; }
67 if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; }
68 if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; }
69 if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; }
70 if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; }
71 }
mtklein4311f012016-04-19 14:00:13 -070072 }
73 return features;
74 }
75
Mike Kleinc665fdd2017-06-06 10:43:01 -040076#elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>)
Mike Kleinf44703a2016-12-12 10:29:38 -050077 #include <sys/auxv.h>
mtklein4311f012016-04-19 14:00:13 -070078
79 static uint32_t read_cpu_features() {
Mike Kleinb468ddc2018-03-26 11:25:07 -040080 const uint32_t kHWCAP_CRC32 = (1<< 7),
81 kHWCAP_ASIMDHP = (1<<10);
Mike Kleinc665fdd2017-06-06 10:43:01 -040082
mtklein4311f012016-04-19 14:00:13 -070083 uint32_t features = 0;
Mike Kleinf44703a2016-12-12 10:29:38 -050084 uint32_t hwcaps = getauxval(AT_HWCAP);
Mike Kleinb468ddc2018-03-26 11:25:07 -040085 if (hwcaps & kHWCAP_CRC32 ) { features |= SkCpu::CRC32; }
86 if (hwcaps & kHWCAP_ASIMDHP) { features |= SkCpu::ASIMDHP; }
Mike Kleind9430292019-05-24 14:28:52 -050087
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 }
mtkleinf1b60302016-08-18 15:59:08 -0700115 return features;
116 }
117
bsheedy592c2252017-11-28 11:06:47 -0800118#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.
Mike Klein4ef8cb32017-01-12 11:36:46 -0500122 #include <sys/auxv.h>
123
124 static uint32_t read_cpu_features() {
Mike Kleinca5d2cf2017-10-24 19:28:47 -0400125 const uint32_t kHWCAP_NEON = (1<<12);
Mike Klein04d488c2017-07-18 17:04:26 -0400126 const uint32_t kHWCAP_VFPv4 = (1<<16);
Mike Kleinc665fdd2017-06-06 10:43:01 -0400127
Mike Klein4ef8cb32017-01-12 11:36:46 -0500128 uint32_t features = 0;
129 uint32_t hwcaps = getauxval(AT_HWCAP);
Mike Kleinca5d2cf2017-10-24 19:28:47 -0400130 if (hwcaps & kHWCAP_NEON ) {
131 features |= SkCpu::NEON;
132 if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON_FMA|SkCpu::VFP_FP16; }
133 }
Mike Klein4ef8cb32017-01-12 11:36:46 -0500134 return features;
135 }
136
Mike Klein30ec0b32017-02-07 18:03:15 -0500137#elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>)
Mike Klein4ef8cb32017-01-12 11:36:46 -0500138 #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
mtklein4311f012016-04-19 14:00:13 -0700149#else
150 static uint32_t read_cpu_features() {
151 return 0;
152 }
153
154#endif
155
mtklein5608e2e2016-07-11 09:59:21 -0700156uint32_t SkCpu::gCachedFeatures = 0;
mtkleineb85fd72016-04-21 10:34:41 -0700157
mtklein5608e2e2016-07-11 09:59:21 -0700158void SkCpu::CacheRuntimeFeatures() {
159 static SkOnce once;
160 once([] { gCachedFeatures = read_cpu_features(); });
161}