/src/skia/src/core/SkCpu.cpp
Line | Count | Source (jump to first uncovered line) |
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 "src/core/SkCpu.h" |
9 | | |
10 | | #include "include/private/base/SkOnce.h" |
11 | | |
12 | | #if defined(SK_CPU_X86) |
13 | | #if defined(_MSC_VER) |
14 | | #include <intrin.h> |
15 | | static void cpuid (uint32_t abcd[4]) { __cpuid ((int*)abcd, 1); } |
16 | | static void cpuid7(uint32_t abcd[4]) { __cpuidex((int*)abcd, 7, 0); } |
17 | | static uint64_t xgetbv(uint32_t xcr) { return _xgetbv(xcr); } |
18 | | #else |
19 | | #include <cpuid.h> |
20 | | #if !defined(__cpuid_count) // Old Mac Clang doesn't have this defined. |
21 | | #define __cpuid_count(eax, ecx, a, b, c, d) \ |
22 | | __asm__("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(eax), "2"(ecx)) |
23 | | #endif |
24 | 0 | static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); } |
25 | 0 | static void cpuid7(uint32_t abcd[4]) { |
26 | 0 | __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]); |
27 | 0 | } |
28 | 0 | static uint64_t xgetbv(uint32_t xcr) { |
29 | 0 | uint32_t eax, edx; |
30 | 0 | __asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); |
31 | 0 | return (uint64_t)(edx) << 32 | eax; |
32 | 0 | } |
33 | | #endif |
34 | | |
35 | 0 | static uint32_t read_cpu_features() { |
36 | 0 | uint32_t features = 0; |
37 | 0 | uint32_t abcd[4] = {0,0,0,0}; |
38 | | |
39 | | // You might want to refer to http://www.sandpile.org/x86/cpuid.htm |
40 | |
|
41 | 0 | cpuid(abcd); |
42 | 0 | if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; } |
43 | 0 | if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; } |
44 | 0 | if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; } |
45 | 0 | if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; } |
46 | 0 | if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; } |
47 | 0 | if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; } |
48 | |
|
49 | 0 | if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE |
50 | 0 | && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled. |
51 | 0 | if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; } |
52 | 0 | if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; } |
53 | 0 | if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; } |
54 | |
|
55 | 0 | cpuid7(abcd); |
56 | 0 | if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; } |
57 | 0 | if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; } |
58 | 0 | if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; } |
59 | 0 | if (abcd[1] & (1<<9)) { features |= SkCpu::ERMS; } |
60 | |
|
61 | 0 | if ((xgetbv(0) & (7<<5)) == (7<<5)) { // All ZMM state bits enabled too. |
62 | 0 | if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; } |
63 | 0 | if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; } |
64 | 0 | if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; } |
65 | 0 | if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; } |
66 | 0 | if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; } |
67 | 0 | if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; } |
68 | 0 | if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; } |
69 | 0 | if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; } |
70 | 0 | } |
71 | 0 | } |
72 | 0 | return features; |
73 | 0 | } |
74 | | #elif defined(SK_CPU_LOONGARCH) |
75 | | #include <sys/auxv.h> |
76 | | static uint32_t read_cpu_features(void) |
77 | | { |
78 | | uint64_t features = 0; |
79 | | uint64_t hwcap = getauxval(AT_HWCAP); |
80 | | |
81 | | if (hwcap & HWCAP_LOONGARCH_LSX) { features |= SkCpu::LOONGARCH_SX; } |
82 | | if (hwcap & HWCAP_LOONGARCH_LASX) { features |= SkCpu::LOONGARCH_ASX; } |
83 | | |
84 | | return features; |
85 | | } |
86 | | #else |
87 | | static uint32_t read_cpu_features() { |
88 | | return 0; |
89 | | } |
90 | | #endif |
91 | | |
92 | | uint32_t SkCpu::gCachedFeatures = 0; |
93 | | |
94 | 0 | void SkCpu::CacheRuntimeFeatures() { |
95 | 0 | static SkOnce once; |
96 | 0 | once([] { gCachedFeatures = read_cpu_features(); }); |
97 | 0 | } |