cpu_id.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2011 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef INCLUDE_LIBYUV_CPU_ID_H_
  11. #define INCLUDE_LIBYUV_CPU_ID_H_
  12. #include "libyuv/basic_types.h"
  13. #ifdef __cplusplus
  14. namespace libyuv {
  15. extern "C" {
  16. #endif
  17. // Internal flag to indicate cpuid requires initialization.
  18. static const int kCpuInitialized = 0x1;
  19. // These flags are only valid on ARM processors.
  20. static const int kCpuHasARM = 0x2;
  21. static const int kCpuHasNEON = 0x4;
  22. // 0x8 reserved for future ARM flag.
  23. // These flags are only valid on x86 processors.
  24. static const int kCpuHasX86 = 0x10;
  25. static const int kCpuHasSSE2 = 0x20;
  26. static const int kCpuHasSSSE3 = 0x40;
  27. static const int kCpuHasSSE41 = 0x80;
  28. static const int kCpuHasSSE42 = 0x100; // unused at this time.
  29. static const int kCpuHasAVX = 0x200;
  30. static const int kCpuHasAVX2 = 0x400;
  31. static const int kCpuHasERMS = 0x800;
  32. static const int kCpuHasFMA3 = 0x1000;
  33. static const int kCpuHasF16C = 0x2000;
  34. static const int kCpuHasGFNI = 0x4000;
  35. static const int kCpuHasAVX512BW = 0x8000;
  36. static const int kCpuHasAVX512VL = 0x10000;
  37. static const int kCpuHasAVX512VBMI = 0x20000;
  38. static const int kCpuHasAVX512VBMI2 = 0x40000;
  39. static const int kCpuHasAVX512VBITALG = 0x80000;
  40. static const int kCpuHasAVX512VPOPCNTDQ = 0x100000;
  41. // These flags are only valid on MIPS processors.
  42. static const int kCpuHasMIPS = 0x200000;
  43. static const int kCpuHasDSPR2 = 0x400000;
  44. static const int kCpuHasMSA = 0x800000;
  45. // Optional init function. TestCpuFlag does an auto-init.
  46. // Returns cpu_info flags.
  47. LIBYUV_API
  48. int InitCpuFlags(void);
  49. // Detect CPU has SSE2 etc.
  50. // Test_flag parameter should be one of kCpuHas constants above.
  51. // Returns non-zero if instruction set is detected
  52. static __inline int TestCpuFlag(int test_flag) {
  53. LIBYUV_API extern int cpu_info_;
  54. #ifdef __ATOMIC_RELAXED
  55. int cpu_info = __atomic_load_n(&cpu_info_, __ATOMIC_RELAXED);
  56. #else
  57. int cpu_info = cpu_info_;
  58. #endif
  59. return (!cpu_info ? InitCpuFlags() : cpu_info) & test_flag;
  60. }
  61. // Internal function for parsing /proc/cpuinfo.
  62. LIBYUV_API
  63. int ArmCpuCaps(const char* cpuinfo_name);
  64. // For testing, allow CPU flags to be disabled.
  65. // ie MaskCpuFlags(~kCpuHasSSSE3) to disable SSSE3.
  66. // MaskCpuFlags(-1) to enable all cpu specific optimizations.
  67. // MaskCpuFlags(1) to disable all cpu specific optimizations.
  68. // MaskCpuFlags(0) to reset state so next call will auto init.
  69. // Returns cpu_info flags.
  70. LIBYUV_API
  71. int MaskCpuFlags(int enable_flags);
  72. // Low level cpuid for X86. Returns zeros on other CPUs.
  73. // eax is the info type that you want.
  74. // ecx is typically the cpu number, and should normally be zero.
  75. LIBYUV_API
  76. void CpuId(int eax, int ecx, int* cpu_info);
  77. #ifdef __cplusplus
  78. } // extern "C"
  79. } // namespace libyuv
  80. #endif
  81. #endif // INCLUDE_LIBYUV_CPU_ID_H_