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 | |
| 8 | #include "SkCpu.h" |
mtklein | eb85fd7 | 2016-04-21 10:34:41 -0700 | [diff] [blame] | 9 | #include "SkOnce.h" |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 10 | |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 11 | #if !defined(__has_include) |
| 12 | #define __has_include(x) 0 |
| 13 | #endif |
| 14 | |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 15 | #if defined(SK_CPU_X86) |
| 16 | #if defined(SK_BUILD_FOR_WIN32) |
| 17 | #include <intrin.h> |
| 18 | static void cpuid (uint32_t abcd[4]) { __cpuid ((int*)abcd, 1); } |
| 19 | static void cpuid7(uint32_t abcd[4]) { __cpuidex((int*)abcd, 7, 0); } |
| 20 | static uint64_t xgetbv(uint32_t xcr) { return _xgetbv(xcr); } |
| 21 | #else |
| 22 | #include <cpuid.h> |
| 23 | #if !defined(__cpuid_count) // Old Mac Clang doesn't have this defined. |
| 24 | #define __cpuid_count(eax, ecx, a, b, c, d) \ |
| 25 | __asm__("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(eax), "2"(ecx)) |
| 26 | #endif |
| 27 | static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); } |
| 28 | static void cpuid7(uint32_t abcd[4]) { |
| 29 | __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]); |
| 30 | } |
| 31 | static uint64_t xgetbv(uint32_t xcr) { |
| 32 | uint32_t eax, edx; |
| 33 | __asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); |
| 34 | return (uint64_t)(edx) << 32 | eax; |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | static uint32_t read_cpu_features() { |
| 39 | uint32_t features = 0; |
| 40 | uint32_t abcd[4] = {0,0,0,0}; |
| 41 | |
| 42 | // You might want to refer to http://www.sandpile.org/x86/cpuid.htm |
| 43 | |
| 44 | cpuid(abcd); |
| 45 | if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; } |
| 46 | if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; } |
| 47 | if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; } |
| 48 | if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; } |
| 49 | if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; } |
| 50 | if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; } |
| 51 | |
Mike Klein | c6a449d | 2017-02-28 20:15:26 -0500 | [diff] [blame] | 52 | if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE |
| 53 | && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled. |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 54 | if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; } |
| 55 | if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; } |
| 56 | if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; } |
| 57 | |
| 58 | cpuid7(abcd); |
| 59 | if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; } |
Mike Klein | 78d5a3b | 2016-09-30 10:48:01 -0400 | [diff] [blame] | 60 | if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; } |
| 61 | if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; } |
Mike Klein | c6a449d | 2017-02-28 20:15:26 -0500 | [diff] [blame] | 62 | |
| 63 | if ((xgetbv(0) & (7<<5)) == (7<<5)) { // All ZMM state bits enabled too. |
| 64 | if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; } |
| 65 | if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; } |
| 66 | if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; } |
| 67 | if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; } |
| 68 | if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; } |
| 69 | if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; } |
| 70 | if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; } |
| 71 | if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; } |
| 72 | } |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 73 | } |
| 74 | return features; |
| 75 | } |
| 76 | |
Mike Klein | c665fdd | 2017-06-06 10:43:01 -0400 | [diff] [blame] | 77 | #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>) |
Mike Klein | f44703a | 2016-12-12 10:29:38 -0500 | [diff] [blame] | 78 | #include <sys/auxv.h> |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 79 | |
| 80 | static uint32_t read_cpu_features() { |
Mike Klein | 04d488c | 2017-07-18 17:04:26 -0400 | [diff] [blame] | 81 | const uint32_t kHWCAP_CRC32 = (1<<7); |
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 | 04d488c | 2017-07-18 17:04:26 -0400 | [diff] [blame] | 85 | if (hwcaps & kHWCAP_CRC32) { features |= SkCpu::CRC32; } |
mtklein | f1b6030 | 2016-08-18 15:59:08 -0700 | [diff] [blame] | 86 | return features; |
| 87 | } |
| 88 | |
Mike Klein | c665fdd | 2017-06-06 10:43:01 -0400 | [diff] [blame] | 89 | #elif defined(SK_CPU_ARM32) && __has_include(<sys/auxv.h>) |
| 90 | // sys/auxv.h won't be present on NDK builds before API v21. |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 91 | #include <sys/auxv.h> |
| 92 | |
| 93 | static uint32_t read_cpu_features() { |
Mike Klein | 04d488c | 2017-07-18 17:04:26 -0400 | [diff] [blame] | 94 | const uint32_t kHWCAP_VFPv4 = (1<<16); |
Mike Klein | c665fdd | 2017-06-06 10:43:01 -0400 | [diff] [blame] | 95 | |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 96 | uint32_t features = 0; |
| 97 | uint32_t hwcaps = getauxval(AT_HWCAP); |
Mike Klein | 04d488c | 2017-07-18 17:04:26 -0400 | [diff] [blame] | 98 | if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON|SkCpu::NEON_FMA|SkCpu::VFP_FP16; } |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 99 | return features; |
| 100 | } |
| 101 | |
Mike Klein | 30ec0b3 | 2017-02-07 18:03:15 -0500 | [diff] [blame] | 102 | #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>) |
Mike Klein | 4ef8cb3 | 2017-01-12 11:36:46 -0500 | [diff] [blame] | 103 | #include <cpu-features.h> |
| 104 | |
| 105 | static uint32_t read_cpu_features() { |
| 106 | uint32_t features = 0; |
| 107 | uint64_t cpu_features = android_getCpuFeatures(); |
| 108 | if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) { features |= SkCpu::NEON; } |
| 109 | if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= SkCpu::NEON_FMA; } |
| 110 | if (cpu_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= SkCpu::VFP_FP16; } |
| 111 | return features; |
| 112 | } |
| 113 | |
mtklein | 4311f01 | 2016-04-19 14:00:13 -0700 | [diff] [blame] | 114 | #else |
| 115 | static uint32_t read_cpu_features() { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | #endif |
| 120 | |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 121 | uint32_t SkCpu::gCachedFeatures = 0; |
mtklein | eb85fd7 | 2016-04-21 10:34:41 -0700 | [diff] [blame] | 122 | |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 123 | void SkCpu::CacheRuntimeFeatures() { |
| 124 | static SkOnce once; |
| 125 | once([] { gCachedFeatures = read_cpu_features(); }); |
| 126 | } |