blob: 0ab45e5b4e34df7d7e26520772e3bcd6c87c928e [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
8#include "SkCpu.h"
mtkleineb85fd72016-04-21 10:34:41 -07009#include "SkOnce.h"
mtklein4311f012016-04-19 14:00:13 -070010
Mike Klein4ef8cb32017-01-12 11:36:46 -050011#if !defined(__has_include)
12 #define __has_include(x) 0
13#endif
14
mtklein4311f012016-04-19 14:00:13 -070015#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 Kleinc6a449d2017-02-28 20:15:26 -050052 if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE
53 && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled.
mtklein4311f012016-04-19 14:00:13 -070054 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 Klein78d5a3b2016-09-30 10:48:01 -040060 if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; }
61 if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; }
Mike Kleinc6a449d2017-02-28 20:15:26 -050062
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 }
mtklein4311f012016-04-19 14:00:13 -070073 }
74 return features;
75 }
76
Mike Kleinc665fdd2017-06-06 10:43:01 -040077#elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>)
Mike Kleinf44703a2016-12-12 10:29:38 -050078 #include <sys/auxv.h>
mtklein4311f012016-04-19 14:00:13 -070079
80 static uint32_t read_cpu_features() {
Mike Klein04d488c2017-07-18 17:04:26 -040081 const uint32_t kHWCAP_CRC32 = (1<<7);
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 Klein04d488c2017-07-18 17:04:26 -040085 if (hwcaps & kHWCAP_CRC32) { features |= SkCpu::CRC32; }
mtkleinf1b60302016-08-18 15:59:08 -070086 return features;
87 }
88
Mike Kleinc665fdd2017-06-06 10:43:01 -040089#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 Klein4ef8cb32017-01-12 11:36:46 -050091 #include <sys/auxv.h>
92
93 static uint32_t read_cpu_features() {
Mike Klein04d488c2017-07-18 17:04:26 -040094 const uint32_t kHWCAP_VFPv4 = (1<<16);
Mike Kleinc665fdd2017-06-06 10:43:01 -040095
Mike Klein4ef8cb32017-01-12 11:36:46 -050096 uint32_t features = 0;
97 uint32_t hwcaps = getauxval(AT_HWCAP);
Mike Klein04d488c2017-07-18 17:04:26 -040098 if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON|SkCpu::NEON_FMA|SkCpu::VFP_FP16; }
Mike Klein4ef8cb32017-01-12 11:36:46 -050099 return features;
100 }
101
Mike Klein30ec0b32017-02-07 18:03:15 -0500102#elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>)
Mike Klein4ef8cb32017-01-12 11:36:46 -0500103 #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
mtklein4311f012016-04-19 14:00:13 -0700114#else
115 static uint32_t read_cpu_features() {
116 return 0;
117 }
118
119#endif
120
mtklein5608e2e2016-07-11 09:59:21 -0700121uint32_t SkCpu::gCachedFeatures = 0;
mtkleineb85fd72016-04-21 10:34:41 -0700122
mtklein5608e2e2016-07-11 09:59:21 -0700123void SkCpu::CacheRuntimeFeatures() {
124 static SkOnce once;
125 once([] { gCachedFeatures = read_cpu_features(); });
126}