OLD | NEW |
---|---|
(Empty) | |
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 "SkOncePtr.h" | |
10 | |
11 #if defined(SK_CPU_X86) | |
12 #if defined(SK_BUILD_FOR_WIN32) | |
13 #include <intrin.h> | |
14 static void cpuid (uint32_t abcd[4]) { __cpuid ((int*)abcd, 1); } | |
15 static void cpuid7(uint32_t abcd[4]) { __cpuidex((int*)abcd, 7, 0); } | |
16 static uint64_t xgetbv(uint32_t xcr) { return _xgetbv(xcr); } | |
17 #else | |
18 #include <cpuid.h> | |
herb_g
2016/04/14 15:38:34
If you have a link to the docs that describe cpuid
mtklein
2016/04/14 15:52:28
Done, below.
| |
19 #if !defined(__cpuid_count) // Old Mac Clang doesn't have this defined. | |
20 #define __cpuid_count(eax, ecx, a, b, c, d) \ | |
21 __asm__("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(eax), "2"(ecx)) | |
22 #endif | |
23 static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, ab cd+2, abcd+3); } | |
24 static void cpuid7(uint32_t abcd[4]) { | |
25 __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]); | |
26 } | |
27 static uint64_t xgetbv(uint32_t xcr) { | |
28 uint32_t eax, edx; | |
29 __asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); | |
30 return (uint64_t)(edx) << 32 | eax; | |
31 } | |
32 #endif | |
33 | |
34 static uint32_t read_cpu_features() { | |
35 uint32_t features = 0; | |
36 uint32_t abcd[4] = {0,0,0,0}; | |
37 | |
38 cpuid(abcd); | |
39 if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; } | |
f(malita)
2016/04/14 15:40:35
It's a bummer we can't use the cpuid.h macros inst
mtklein
2016/04/14 15:52:28
Yeah, we could use them when we have cpuid.h, but
| |
40 if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; } | |
41 if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; } | |
42 if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; } | |
43 if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; } | |
44 if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; } | |
45 | |
46 if ((abcd[2] & (3<<26)) == (3<<26) && (xgetbv(0) & 6) == 6) { // XSAVE + OSXSAVE | |
47 if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; } | |
48 if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; } | |
49 if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; } | |
50 | |
51 cpuid7(abcd); | |
52 if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; } | |
53 } | |
54 return features; | |
55 } | |
56 | |
57 #elif defined(SK_CPU_ARM32) && \ | |
58 defined(SK_BUILD_FOR_ANDROID) && \ | |
59 !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) | |
60 #include <cpu-features.h> | |
61 | |
62 static uint32_t read_cpu_features() { | |
63 uint32_t features = 0; | |
64 | |
65 uint64_t android_features = android_getCpuFeatures(); | |
66 if (android_features & ANDROID_CPU_ARM_FEATURE_NEON ) { features |= S kCpu::NEON ; } | |
67 if (android_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= S kCpu::NEON_FMA; } | |
68 if (android_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= S kCpu::VFP_FP16; } | |
69 return features; | |
70 } | |
71 | |
72 #else | |
73 static uint32_t read_cpu_features() { | |
74 return 0; | |
75 } | |
76 | |
77 #endif | |
78 | |
79 #if defined(__GNUC__) || defined(__clang__) | |
80 SK_DECLARE_STATIC_ONCE_PTR(uint32_t, gCachedCpuFeatures); | |
81 uint32_t SkCpu::RuntimeCpuFeatures() { | |
82 return *gCachedCpuFeatures.get([]{ return new uint32_t{read_cpu_features ()}; }); | |
83 } | |
84 | |
85 #else | |
86 const uint32_t SkCpu::gCachedCpuFeatures = read_cpu_features(); | |
87 | |
88 #endif | |
OLD | NEW |