PageRenderTime 65ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/core/SkCpu.cpp

https://github.com/android/platform_external_skia
C++ | 161 lines | 121 code | 24 blank | 16 comment | 47 complexity | c88a317dc4971975a1fa72e65d9954de MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, BSD-3-Clause, Apache-2.0
  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. #include "include/core/SkStream.h"
  8. #include "include/core/SkString.h"
  9. #include "include/private/SkOnce.h"
  10. #include "src/core/SkCpu.h"
  11. #if defined(SK_CPU_X86)
  12. #if defined(_MSC_VER)
  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. static uint32_t read_cpu_features() {
  34. uint32_t features = 0;
  35. uint32_t abcd[4] = {0,0,0,0};
  36. // You might want to refer to http://www.sandpile.org/x86/cpuid.htm
  37. cpuid(abcd);
  38. if (abcd[3] & (1<<25)) { features |= SkCpu:: SSE1; }
  39. if (abcd[3] & (1<<26)) { features |= SkCpu:: SSE2; }
  40. if (abcd[2] & (1<< 0)) { features |= SkCpu:: SSE3; }
  41. if (abcd[2] & (1<< 9)) { features |= SkCpu::SSSE3; }
  42. if (abcd[2] & (1<<19)) { features |= SkCpu::SSE41; }
  43. if (abcd[2] & (1<<20)) { features |= SkCpu::SSE42; }
  44. if ((abcd[2] & (3<<26)) == (3<<26) // XSAVE + OSXSAVE
  45. && (xgetbv(0) & (3<<1)) == (3<<1)) { // XMM and YMM state enabled.
  46. if (abcd[2] & (1<<28)) { features |= SkCpu:: AVX; }
  47. if (abcd[2] & (1<<29)) { features |= SkCpu::F16C; }
  48. if (abcd[2] & (1<<12)) { features |= SkCpu:: FMA; }
  49. cpuid7(abcd);
  50. if (abcd[1] & (1<<5)) { features |= SkCpu::AVX2; }
  51. if (abcd[1] & (1<<3)) { features |= SkCpu::BMI1; }
  52. if (abcd[1] & (1<<8)) { features |= SkCpu::BMI2; }
  53. if (abcd[1] & (1<<9)) { features |= SkCpu::ERMS; }
  54. if ((xgetbv(0) & (7<<5)) == (7<<5)) { // All ZMM state bits enabled too.
  55. if (abcd[1] & (1<<16)) { features |= SkCpu::AVX512F; }
  56. if (abcd[1] & (1<<17)) { features |= SkCpu::AVX512DQ; }
  57. if (abcd[1] & (1<<21)) { features |= SkCpu::AVX512IFMA; }
  58. if (abcd[1] & (1<<26)) { features |= SkCpu::AVX512PF; }
  59. if (abcd[1] & (1<<27)) { features |= SkCpu::AVX512ER; }
  60. if (abcd[1] & (1<<28)) { features |= SkCpu::AVX512CD; }
  61. if (abcd[1] & (1<<30)) { features |= SkCpu::AVX512BW; }
  62. if (abcd[1] & (1<<31)) { features |= SkCpu::AVX512VL; }
  63. }
  64. }
  65. return features;
  66. }
  67. #elif defined(SK_CPU_ARM64) && __has_include(<sys/auxv.h>)
  68. #include <sys/auxv.h>
  69. static uint32_t read_cpu_features() {
  70. const uint32_t kHWCAP_CRC32 = (1<< 7),
  71. kHWCAP_ASIMDHP = (1<<10);
  72. uint32_t features = 0;
  73. uint32_t hwcaps = getauxval(AT_HWCAP);
  74. if (hwcaps & kHWCAP_CRC32 ) { features |= SkCpu::CRC32; }
  75. if (hwcaps & kHWCAP_ASIMDHP) { features |= SkCpu::ASIMDHP; }
  76. // The Samsung Mongoose 3 core sets the ASIMDHP bit but doesn't support it.
  77. for (int core = 0; features & SkCpu::ASIMDHP; core++) {
  78. // These /sys files contain the core's MIDR_EL1 register, the source of
  79. // CPU {implementer, variant, part, revision} you'd see in /proc/cpuinfo.
  80. SkString path =
  81. SkStringPrintf("/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1", core);
  82. // Can't use SkData::MakeFromFileName() here, I think because /sys can't be mmap()'d.
  83. SkFILEStream midr_el1(path.c_str());
  84. if (!midr_el1.isValid()) {
  85. // This is our ordinary exit path.
  86. // If we ask for MIDR_EL1 from a core that doesn't exist, we've checked all cores.
  87. if (core == 0) {
  88. // On the other hand, if we can't read MIDR_EL1 from any core, assume the worst.
  89. features &= ~(SkCpu::ASIMDHP);
  90. }
  91. break;
  92. }
  93. const char kMongoose3[] = "0x00000000531f0020"; // 53 == Samsung.
  94. char buf[SK_ARRAY_COUNT(kMongoose3) - 1]; // No need for the terminating \0.
  95. if (SK_ARRAY_COUNT(buf) != midr_el1.read(buf, SK_ARRAY_COUNT(buf))
  96. || 0 == memcmp(kMongoose3, buf, SK_ARRAY_COUNT(buf))) {
  97. features &= ~(SkCpu::ASIMDHP);
  98. }
  99. }
  100. return features;
  101. }
  102. #elif defined(SK_CPU_ARM32) && __has_include(<sys/auxv.h>) && \
  103. (!defined(__ANDROID_API__) || __ANDROID_API__ >= 18)
  104. // sys/auxv.h will always be present in the Android NDK due to unified
  105. //headers, but getauxval is only defined for API >= 18.
  106. #include <sys/auxv.h>
  107. static uint32_t read_cpu_features() {
  108. const uint32_t kHWCAP_NEON = (1<<12);
  109. const uint32_t kHWCAP_VFPv4 = (1<<16);
  110. uint32_t features = 0;
  111. uint32_t hwcaps = getauxval(AT_HWCAP);
  112. if (hwcaps & kHWCAP_NEON ) {
  113. features |= SkCpu::NEON;
  114. if (hwcaps & kHWCAP_VFPv4) { features |= SkCpu::NEON_FMA|SkCpu::VFP_FP16; }
  115. }
  116. return features;
  117. }
  118. #elif defined(SK_CPU_ARM32) && __has_include(<cpu-features.h>)
  119. #include <cpu-features.h>
  120. static uint32_t read_cpu_features() {
  121. uint32_t features = 0;
  122. uint64_t cpu_features = android_getCpuFeatures();
  123. if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) { features |= SkCpu::NEON; }
  124. if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON_FMA) { features |= SkCpu::NEON_FMA; }
  125. if (cpu_features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) { features |= SkCpu::VFP_FP16; }
  126. return features;
  127. }
  128. #else
  129. static uint32_t read_cpu_features() {
  130. return 0;
  131. }
  132. #endif
  133. uint32_t SkCpu::gCachedFeatures = 0;
  134. void SkCpu::CacheRuntimeFeatures() {
  135. static SkOnce once;
  136. once([] { gCachedFeatures = read_cpu_features(); });
  137. }