| 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 | 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 | |
| 50 | if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE |
| 51 | && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled. |
| 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; } |
| 58 | if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; } |
| 59 | if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; } |
| 60 | |
| 61 | if ((xgetbv(0) & (7<<5)) == (7<<5)) { // All ZMM state bits enabled too. |
| 62 | if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; } |
| 63 | if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; } |
| 64 | if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; } |
| 65 | if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; } |
| 66 | if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; } |
| 67 | if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; } |
| 68 | if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; } |
| 69 | if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; } |
| 70 | } |
| 71 | } |
| 72 | return features; |
| 73 | } |
| 74 | |
| 75 | #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>) |
| 76 | #include <sys/auxv.h> |
| 77 | |
| 78 | static uint32_t read_cpu_features() { |
| 79 | const uint32_t kHWCAP_CRC32 = (1<< 7), |
| 80 | kHWCAP_ASIMDHP = (1<<10); |
| 81 | |
| 82 | uint32_t features = 0; |
| 83 | uint32_t hwcaps = getauxval(AT_HWCAP); |
| 84 | if (hwcaps & kHWCAP_CRC32 ) { features |= SkCpu::CRC32; } |
| 85 | if (hwcaps & kHWCAP_ASIMDHP) { features |= SkCpu::ASIMDHP; } |
| 86 | |
| 87 | // The Samsung Mongoose 3 core sets the ASIMDHP bit but doesn't support it. |
| 88 | for (int core = 0; features & SkCpu::ASIMDHP; core++) { |
| 89 | // These /sys files contain the core's MIDR_EL1 register, the source of |
| 90 | // CPU {implementer, variant, part, revision} you'd see in /proc/cpuinfo. |
| 91 | SkString path = |
| 92 | SkStringPrintf("/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1" , core); |
| 93 | |
| 94 | // Can't use SkData::MakeFromFileName() here, I think because /sys can't be mmap()'d. |
| 95 | SkFILEStream midr_el1(path.c_str()); |
| 96 | if (!midr_el1.isValid()) { |
| 97 | // This is our ordinary exit path. |
| 98 | // If we ask for MIDR_EL1 from a core that doesn't exist, we've checked all cores. |
| 99 | if (core == 0) { |
| 100 | // On the other hand, if we can't read MIDR_EL1 from any core, assume the worst. |
| 101 | features &= ~(SkCpu::ASIMDHP); |
| 102 | } |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | const char kMongoose3[] = "0x00000000531f0020" ; // 53 == Samsung. |
| 107 | char buf[SK_ARRAY_COUNT(kMongoose3) - 1]; // No need for the terminating \0. |
| 108 | |
| 109 | if (SK_ARRAY_COUNT(buf) != midr_el1.read(buf, SK_ARRAY_COUNT(buf)) |
| 110 | || 0 == memcmp(kMongoose3, buf, SK_ARRAY_COUNT(buf))) { |
| 111 | features &= ~(SkCpu::ASIMDHP); |
| 112 | } |
| 113 | } |
| 114 | return features; |
| 115 | } |
| 116 | |
| 117 | #elif defined(SK_CPU_ARM32) && __has_include(<sys/auxv.h>) && \ |
| 118 | (!defined(__ANDROID_API__) || __ANDROID_API__ >= 18) |
| 119 | // sys/auxv.h will always be present in the Android NDK due to unified |
| 120 | //headers, but getauxval is only defined for API >= 18. |
| 121 | #include <sys/auxv.h> |
| 122 | |
| 123 | static uint32_t read_cpu_features() { |
| 124 | const uint32_t kHWCAP_NEON = (1<<12); |
| 125 | const uint32_t kHWCAP_VFPv4 = (1<<16); |
| 126 | |
| 127 | uint32_t features = 0; |
| 128 | uint32_t hwcaps = getauxval(AT_HWCAP); |
| 129 | if (hwcaps & kHWCAP_NEON ) { |
| 130 | features |= SkCpu::NEON; |
| 131 | if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON_FMA|SkCpu::VFP_FP16; } |
| 132 | } |
| 133 | return features; |
| 134 | } |
| 135 | |
| 136 | #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>) |
| 137 | #include <cpu-features.h> |
| 138 | |
| 139 | static uint32_t read_cpu_features() { |
| 140 | uint32_t features = 0; |
| 141 | uint64_t cpu_features = android_getCpuFeatures(); |
| 142 | if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) { features |= SkCpu::NEON; } |
| 143 | if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= SkCpu::NEON_FMA; } |
| 144 | if (cpu_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= SkCpu::VFP_FP16; } |
| 145 | return features; |
| 146 | } |
| 147 | |
| 148 | #else |
| 149 | static uint32_t read_cpu_features() { |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | #endif |
| 154 | |
| 155 | uint32_t SkCpu::gCachedFeatures = 0; |
| 156 | |
| 157 | void SkCpu::CacheRuntimeFeatures() { |
| 158 | static SkOnce once; |
| 159 | once([] { gCachedFeatures = read_cpu_features(); }); |
| 160 | } |
| 161 | |