• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "SkOnce.h"
10 
11 #if !defined(__has_include)
12     #define __has_include(x) 0
13 #endif
14 
15 #if defined(SK_CPU_X86)
16     #if defined(SK_BUILD_FOR_WIN32)
17         #include <intrin.h>
cpuid(uint32_t abcd[4])18         static void cpuid (uint32_t abcd[4]) { __cpuid  ((int*)abcd, 1);    }
cpuid7(uint32_t abcd[4])19         static void cpuid7(uint32_t abcd[4]) { __cpuidex((int*)abcd, 7, 0); }
xgetbv(uint32_t xcr)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
cpuid(uint32_t abcd[4])27         static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); }
cpuid7(uint32_t abcd[4])28         static void cpuid7(uint32_t abcd[4]) {
29             __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]);
30         }
xgetbv(uint32_t xcr)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 
read_cpu_features()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 
52         if ((abcd[2] & (3<<26)) == (3<<26)         // XSAVE + OSXSAVE
53              && (xgetbv(0) & (3<<1)) == (3<<1)) {  // XMM and YMM state enabled.
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; }
60             if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; }
61             if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; }
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             }
73         }
74         return features;
75     }
76 
77 #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>)
78     #include <sys/auxv.h>
79 
read_cpu_features()80     static uint32_t read_cpu_features() {
81         const uint32_t HWCAP_CRC32 = (1<<7);
82 
83         uint32_t features = 0;
84         uint32_t hwcaps = getauxval(AT_HWCAP);
85         if (hwcaps & HWCAP_CRC32) { features |= SkCpu::CRC32; }
86         return features;
87     }
88 
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.
91     #include <sys/auxv.h>
92 
read_cpu_features()93     static uint32_t read_cpu_features() {
94         const uint32_t HWCAP_VFPv4 = (1<<16);
95 
96         uint32_t features = 0;
97         uint32_t hwcaps = getauxval(AT_HWCAP);
98         if (hwcaps & HWCAP_VFPv4) { features |= SkCpu::NEON|SkCpu::NEON_FMA|SkCpu::VFP_FP16; }
99         return features;
100     }
101 
102 #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>)
103     #include <cpu-features.h>
104 
read_cpu_features()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 
114 #else
read_cpu_features()115     static uint32_t read_cpu_features() {
116         return 0;
117     }
118 
119 #endif
120 
121 uint32_t SkCpu::gCachedFeatures = 0;
122 
CacheRuntimeFeatures()123 void SkCpu::CacheRuntimeFeatures() {
124     static SkOnce once;
125     once([] { gCachedFeatures = read_cpu_features(); });
126 }
127