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> |
| 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 // You might want to refer to http://www.sandpile.org/x86/cpuid.htm |
| 39 |
| 40 cpuid(abcd); |
| 41 if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; } |
| 42 if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; } |
| 43 if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; } |
| 44 if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; } |
| 45 if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; } |
| 46 if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; } |
| 47 |
| 48 if ((abcd[2] & (3<<26)) == (3<<26) && (xgetbv(0) & 6) == 6) { // XSAVE
+ OSXSAVE |
| 49 if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; } |
| 50 if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; } |
| 51 if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; } |
| 52 |
| 53 cpuid7(abcd); |
| 54 if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; } |
| 55 } |
| 56 return features; |
| 57 } |
| 58 |
| 59 #elif defined(SK_CPU_ARM32) && \ |
| 60 defined(SK_BUILD_FOR_ANDROID) && \ |
| 61 !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 62 #include <cpu-features.h> |
| 63 |
| 64 static uint32_t read_cpu_features() { |
| 65 uint32_t features = 0; |
| 66 |
| 67 uint64_t android_features = android_getCpuFeatures(); |
| 68 if (android_features & ANDROID_CPU_ARM_FEATURE_NEON ) { features |= S
kCpu::NEON ; } |
| 69 if (android_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= S
kCpu::NEON_FMA; } |
| 70 if (android_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= S
kCpu::VFP_FP16; } |
| 71 return features; |
| 72 } |
| 73 |
| 74 #else |
| 75 static uint32_t read_cpu_features() { |
| 76 return 0; |
| 77 } |
| 78 |
| 79 #endif |
| 80 |
| 81 #if defined(__GNUC__) || defined(__clang__) |
| 82 SK_DECLARE_STATIC_ONCE_PTR(uint32_t, gCachedCpuFeatures); |
| 83 uint32_t SkCpu::RuntimeCpuFeatures() { |
| 84 return *gCachedCpuFeatures.get([]{ return new uint32_t{read_cpu_features
()}; }); |
| 85 } |
| 86 |
| 87 #else |
| 88 const uint32_t SkCpu::gCachedCpuFeatures = read_cpu_features(); |
| 89 |
| 90 #endif |
OLD | NEW |