Home | History | Annotate | Download | only in core
      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_WIN)
     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)         // XSAVE + OSXSAVE
     49              && (xgetbv(0) & (3<<1)) == (3<<1)) {  // XMM and YMM state enabled.
     50             if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; }
     51             if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; }
     52             if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; }
     53 
     54             cpuid7(abcd);
     55             if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; }
     56             if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; }
     57             if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; }
     58 
     59             if ((xgetbv(0) & (7<<5)) == (7<<5)) {  // All ZMM state bits enabled too.
     60                 if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; }
     61                 if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; }
     62                 if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; }
     63                 if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; }
     64                 if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; }
     65                 if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; }
     66                 if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; }
     67                 if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; }
     68             }
     69         }
     70         return features;
     71     }
     72 
     73 #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>)
     74     #include <sys/auxv.h>
     75 
     76     static uint32_t read_cpu_features() {
     77         const uint32_t kHWCAP_CRC32   = (1<< 7),
     78                        kHWCAP_ASIMDHP = (1<<10);
     79 
     80         uint32_t features = 0;
     81         uint32_t hwcaps = getauxval(AT_HWCAP);
     82         if (hwcaps & kHWCAP_CRC32  ) { features |= SkCpu::CRC32; }
     83         if (hwcaps & kHWCAP_ASIMDHP) { features |= SkCpu::ASIMDHP; }
     84         return features;
     85     }
     86 
     87 #elif defined(SK_CPU_ARM32) && __has_include(<sys/auxv.h>) && \
     88     (!defined(__ANDROID_API__) || __ANDROID_API__ >= 18)
     89     // sys/auxv.h will always be present in the Android NDK due to unified
     90     //headers, but getauxval is only defined for API >= 18.
     91     #include <sys/auxv.h>
     92 
     93     static uint32_t read_cpu_features() {
     94         const uint32_t kHWCAP_NEON  = (1<<12);
     95         const uint32_t kHWCAP_VFPv4 = (1<<16);
     96 
     97         uint32_t features = 0;
     98         uint32_t hwcaps = getauxval(AT_HWCAP);
     99         if (hwcaps & kHWCAP_NEON ) {
    100             features |= SkCpu::NEON;
    101             if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON_FMA|SkCpu::VFP_FP16; }
    102         }
    103         return features;
    104     }
    105 
    106 #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>)
    107     #include <cpu-features.h>
    108 
    109     static uint32_t read_cpu_features() {
    110         uint32_t features = 0;
    111         uint64_t cpu_features = android_getCpuFeatures();
    112         if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON)     { features |= SkCpu::NEON; }
    113         if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= SkCpu::NEON_FMA; }
    114         if (cpu_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= SkCpu::VFP_FP16; }
    115         return features;
    116     }
    117 
    118 #else
    119     static uint32_t read_cpu_features() {
    120         return 0;
    121     }
    122 
    123 #endif
    124 
    125 uint32_t SkCpu::gCachedFeatures = 0;
    126 
    127 void SkCpu::CacheRuntimeFeatures() {
    128     static SkOnce once;
    129     once([] { gCachedFeatures = read_cpu_features(); });
    130 }
    131