mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 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 | |
Mike Klein | d943029 | 2019-05-24 14:28:52 -0500 | [diff] [blame] | 8 | #include "include/core/SkStream.h" |
| 9 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/SkOnce.h" |
| 11 | #include "src/core/SkCpu.h" |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 12 | |
| 13 | #if defined(SK_CPU_X86) |
Mike Klein | 6f9a37f | 2020-02-11 09:44:48 -0600 | [diff] [blame] | 14 | #if defined(_MSC_VER) |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 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 | 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 Klein | c6a449d | 2017-02-28 20:15:26 -0500 | [diff] [blame] | 50 | if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE |
| 51 | && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled. |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 52 | 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 Klein | 78d5a3b | 2016-09-30 10:48:01 -0400 | [diff] [blame] | 58 | if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; } |
| 59 | if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; } |
Mike Klein | 7ac2be2 | 2020-11-05 09:38:53 -0600 | [diff] [blame] | 60 | if (abcd[1] & (1<<9)) { features |= SkCpu::ERMS; } |
Mike Klein | c6a449d | 2017-02-28 20:15:26 -0500 | [diff] [blame] | 61 | |
| 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 | } |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 72 | } |
| 73 | return features; |
| 74 | } |
| 75 | |
Mike Klein | c665fdd | 2017-06-06 10:43:01 -0400 | [diff] [blame] | 76 | #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>) |
Mike Klein | f44703a | 2016-12-12 10:29:38 -0500 | [diff] [blame] | 77 | #include <sys/auxv.h> |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 78 | |
| 79 | static uint32_t read_cpu_features() { |
Mike Klein | b468ddc | 2018-03-26 11:25:07 -0400 | [diff] [blame] | 80 | const uint32_t kHWCAP_CRC32 = (1<< 7), |
| 81 | kHWCAP_ASIMDHP = (1<<10); |
Mike Klein | c665fdd | 2017-06-06 10:43:01 -0400 | [diff] [blame] | 82 | |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 83 | uint32_t features = 0; |
Mike Klein | f44703a | 2016-12-12 10:29:38 -0500 | [diff] [blame] | 84 | uint32_t hwcaps = getauxval(AT_HWCAP); |
Mike Klein | b468ddc | 2018-03-26 11:25:07 -0400 | [diff] [blame] | 85 | if (hwcaps & kHWCAP_CRC32 ) { features |= SkCpu::CRC32; } |
| 86 | if (hwcaps & kHWCAP_ASIMDHP) { features |= SkCpu::ASIMDHP; } |
Mike Klein | d943029 | 2019-05-24 14:28:52 -0500 | [diff] [blame] | 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 | } |
mtklein | f1b6030 | 2016-08-18 15:59:08 -0700 | [diff] [blame] | 115 | return features; |
| 116 | } |
| 117 | |
bsheedy | 592c225 | 2017-11-28 11:06:47 -0800 | [diff] [blame] | 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. |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 122 | #include <sys/auxv.h> |
| 123 | |
| 124 | static uint32_t read_cpu_features() { |
Mike Klein | ca5d2cf | 2017-10-24 19:28:47 -0400 | [diff] [blame] | 125 | const uint32_t kHWCAP_NEON = (1<<12); |
Mike Klein | 04d488c | 2017-07-18 17:04:26 -0400 | [diff] [blame] | 126 | const uint32_t kHWCAP_VFPv4 = (1<<16); |
Mike Klein | c665fdd | 2017-06-06 10:43:01 -0400 | [diff] [blame] | 127 | |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 128 | uint32_t features = 0; |
| 129 | uint32_t hwcaps = getauxval(AT_HWCAP); |
Mike Klein | ca5d2cf | 2017-10-24 19:28:47 -0400 | [diff] [blame] | 130 | if (hwcaps & kHWCAP_NEON ) { |
| 131 | features |= SkCpu::NEON; |
| 132 | if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON_FMA|SkCpu::VFP_FP16; } |
| 133 | } |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 134 | return features; |
| 135 | } |
| 136 | |
Mike Klein | 30ec0b3 | 2017-02-07 18:03:15 -0500 | [diff] [blame] | 137 | #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>) |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 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 | |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 149 | #else |
| 150 | static uint32_t read_cpu_features() { |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | #endif |
| 155 | |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 156 | uint32_t SkCpu::gCachedFeatures = 0; |
mtklein | eb85fd7 | 2016-04-21 10:34:41 -0700 | [diff] [blame] | 157 | |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 158 | void SkCpu::CacheRuntimeFeatures() { |
| 159 | static SkOnce once; |
| 160 | once([] { gCachedFeatures = read_cpu_features(); }); |
| 161 | } |