PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/gfx/skia/skia/src/core/SkCpu.cpp

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