| 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 |
static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); } |
| 25 |
static void cpuid7(uint32_t abcd[4]) { |
| 26 |
__cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]); |
| 27 |
} |
| 28 |
static uint64_t xgetbv(uint32_t xcr) { |
| 29 |
uint32_t eax, edx; |
| 30 |
__asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); |
| 31 |
return (uint64_t)(edx) << 32 | eax; |
| 32 |
} |
| 33 |
#endif |
| 34 |
|
| 35 |
static uint32_t read_cpu_features() { |
| 36 |
uint32_t features = 0; |
| 37 |
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 |
cpuid(abcd); |
| 42 |
if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; } |
| 43 |
if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; } |
| 44 |
if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; } |
| 45 |
if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; } |
| 46 |
if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; } |
| 47 |
if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; } |
| 48 |
|
| 49 |
if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE |
| 50 |
&& (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled. |
| 51 |
if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; } |
| 52 |
if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; } |
| 53 |
if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; } |
| 54 |
|
| 55 |
cpuid7(abcd); |
| 56 |
if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; } |
| 57 |
if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; } |
| 58 |
if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; } |
| 59 |
if (abcd[1] & (1<<9)) { features |= SkCpu::ERMS; } |
| 60 |
|
| 61 |
if ((xgetbv(0) & (7<<5)) == (7<<5)) { // All ZMM state bits enabled too. |
| 62 |
if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; } |
| 63 |
if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; } |
| 64 |
if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; } |
| 65 |
if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; } |
| 66 |
if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; } |
| 67 |
if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; } |
| 68 |
if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; } |
| 69 |
if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; } |
| 70 |
} |
| 71 |
} |
| 72 |
return features; |
| 73 |
} |
| 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 |
void SkCpu::CacheRuntimeFeatures() { |
| 95 |
static SkOnce once; |
| 96 |
once([] { gCachedFeatures = read_cpu_features(); }); |
| 97 |
} |