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(__has_include)
     12     #define __has_include(x) 0
     13 #endif
     14 
     15 #if defined(SK_CPU_X86)
     16     #if defined(SK_BUILD_FOR_WIN)
     17         #include <intrin.h>
     18         static void cpuid (uint32_t abcd[4]) { __cpuid  ((int*)abcd, 1);    }
     19         static void cpuid7(uint32_t abcd[4]) { __cpuidex((int*)abcd, 7, 0); }
     20         static uint64_t xgetbv(uint32_t xcr) { return _xgetbv(xcr); }
     21     #else
     22         #include <cpuid.h>
     23         #if !defined(__cpuid_count)  // Old Mac Clang doesn't have this defined.
     24             #define  __cpuid_count(eax, ecx, a, b, c, d) \
     25                 __asm__("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(eax), "2"(ecx))
     26         #endif
     27         static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); }
     28         static void cpuid7(uint32_t abcd[4]) {
     29             __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]);
     30         }
     31         static uint64_t xgetbv(uint32_t xcr) {
     32             uint32_t eax, edx;
     33             __asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
     34             return (uint64_t)(edx) << 32 | eax;
     35         }
     36     #endif
     37 
     38     static uint32_t read_cpu_features() {
     39         uint32_t features = 0;
     40         uint32_t abcd[4] = {0,0,0,0};
     41 
     42         // You might want to refer to http://www.sandpile.org/x86/cpuid.htm
     43 
     44         cpuid(abcd);
     45         if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; }
     46         if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; }
     47         if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; }
     48         if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; }
     49         if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; }
     50         if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; }
     51 
     52         if ((abcd[2] & (3<<26)) == (3<<26)         // XSAVE + OSXSAVE
     53              && (xgetbv(0) & (3<<1)) == (3<<1)) {  // XMM and YMM state enabled.
     54             if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; }
     55             if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; }
     56             if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; }
     57 
     58             cpuid7(abcd);
     59             if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; }
     60             if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; }
     61             if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; }
     62 
     63             if ((xgetbv(0) & (7<<5)) == (7<<5)) {  // All ZMM state bits enabled too.
     64                 if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; }
     65                 if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; }
     66                 if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; }
     67                 if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; }
     68                 if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; }
     69                 if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; }
     70                 if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; }
     71                 if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; }
     72             }
     73         }
     74         return features;
     75     }
     76 
     77 #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>)
     78     #include <sys/auxv.h>
     79 
     80     static uint32_t read_cpu_features() {
     81         const uint32_t kHWCAP_CRC32 = (1<<7);
     82 
     83         uint32_t features = 0;
     84         uint32_t hwcaps = getauxval(AT_HWCAP);
     85         if (hwcaps & kHWCAP_CRC32) { features |= SkCpu::CRC32; }
     86         return features;
     87     }
     88 
     89 #elif defined(SK_CPU_ARM32) && __has_include(<sys/auxv.h>) && \
     90     (!defined(__ANDROID_API__) || __ANDROID_API__ >= 18)
     91     // sys/auxv.h will always be present in the Android NDK due to unified
     92     //headers, but getauxval is only defined for API >= 18.
     93     #include <sys/auxv.h>
     94 
     95     static uint32_t read_cpu_features() {
     96         const uint32_t kHWCAP_NEON  = (1<<12);
     97         const uint32_t kHWCAP_VFPv4 = (1<<16);
     98 
     99         uint32_t features = 0;
    100         uint32_t hwcaps = getauxval(AT_HWCAP);
    101         if (hwcaps & kHWCAP_NEON ) {
    102             features |= SkCpu::NEON;
    103             if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON_FMA|SkCpu::VFP_FP16; }
    104         }
    105         return features;
    106     }
    107 
    108 #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>)
    109     #include <cpu-features.h>
    110 
    111     static uint32_t read_cpu_features() {
    112         uint32_t features = 0;
    113         uint64_t cpu_features = android_getCpuFeatures();
    114         if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON)     { features |= SkCpu::NEON; }
    115         if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= SkCpu::NEON_FMA; }
    116         if (cpu_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= SkCpu::VFP_FP16; }
    117         return features;
    118     }
    119 
    120 #else
    121     static uint32_t read_cpu_features() {
    122         return 0;
    123     }
    124 
    125 #endif
    126 
    127 uint32_t SkCpu::gCachedFeatures = 0;
    128 
    129 void SkCpu::CacheRuntimeFeatures() {
    130     static SkOnce once;
    131     once([] { gCachedFeatures = read_cpu_features(); });
    132 }
    133