SkCpu.cpp revision f44703a87f532b3f593d91605d66d52c6bbc45c9
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(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, abcd+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 if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; } 56 if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; } 57 } 58 return features; 59 } 60 61#elif defined(SK_CPU_ARM64) && defined(SK_BUILD_FOR_ANDROID) 62 #include <asm/hwcap.h> 63 #include <sys/auxv.h> 64 65 static uint32_t read_cpu_features() { 66 uint32_t features = 0; 67 uint32_t hwcaps = getauxval(AT_HWCAP); 68 if (hwcaps & HWCAP_CRC32) { features |= SkCpu::CRC32; } 69 return features; 70 } 71 72#else 73 static uint32_t read_cpu_features() { 74 return 0; 75 } 76 77#endif 78 79uint32_t SkCpu::gCachedFeatures = 0; 80 81void SkCpu::CacheRuntimeFeatures() { 82 static SkOnce once; 83 once([] { gCachedFeatures = read_cpu_features(); }); 84} 85