zend_cpuinfo.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2018-2018 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Xinchen Hui <xinchen.h@zend.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "zend_cpuinfo.h"
  19. typedef struct _zend_cpu_info {
  20. uint32_t eax;
  21. uint32_t ebx;
  22. uint32_t ecx;
  23. uint32_t edx;
  24. uint32_t initialized;
  25. } zend_cpu_info;
  26. static zend_cpu_info cpuinfo = {0};
  27. #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  28. # if defined(HAVE_CPUID_H) && defined(HAVE_CPUID_COUNT)
  29. # include <cpuid.h>
  30. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  31. __cpuid_count(func, subfunc, cpuinfo->eax, cpuinfo->ebx, cpuinfo->ecx, cpuinfo->edx);
  32. }
  33. # else
  34. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  35. #if defined(__i386__) && (defined(__pic__) || defined(__PIC__))
  36. /* PIC on i386 uses %ebx, so preserve it. */
  37. __asm__ __volatile__ (
  38. "pushl %%ebx\n"
  39. "cpuid\n"
  40. "mov %%ebx,%1\n"
  41. "popl %%ebx"
  42. : "=a"(cpuinfo->eax), "=r"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
  43. : "a"(func), "c"(subfunc)
  44. );
  45. #else
  46. __asm__ __volatile__ (
  47. "cpuid"
  48. : "=a"(cpuinfo->eax), "=b"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
  49. : "a"(func), "c"(subfunc)
  50. );
  51. #endif
  52. }
  53. # endif
  54. #elif defined(ZEND_WIN32) && !defined(__clang__)
  55. # include <intrin.h>
  56. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  57. int regs[4];
  58. __cpuidex(regs, func, subfunc);
  59. cpuinfo->eax = regs[0];
  60. cpuinfo->ebx = regs[1];
  61. cpuinfo->ecx = regs[2];
  62. cpuinfo->edx = regs[3];
  63. }
  64. #else
  65. static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
  66. cpuinfo->eax = 0;
  67. }
  68. #endif
  69. void zend_cpu_startup(void)
  70. {
  71. if (!cpuinfo.initialized) {
  72. zend_cpu_info ebx;
  73. int max_feature;
  74. cpuinfo.initialized = 1;
  75. __zend_cpuid(0, 0, &cpuinfo);
  76. max_feature = cpuinfo.eax;
  77. if (max_feature == 0) {
  78. return;
  79. }
  80. __zend_cpuid(1, 0, &cpuinfo);
  81. /* for avx2 */
  82. if (max_feature >= 7) {
  83. __zend_cpuid(7, 0, &ebx);
  84. cpuinfo.ebx = ebx.ebx;
  85. } else {
  86. cpuinfo.ebx = 0;
  87. }
  88. }
  89. }
  90. ZEND_API int zend_cpu_supports(zend_cpu_feature feature) {
  91. if (feature & ZEND_CPU_EDX_MASK) {
  92. return (cpuinfo.edx & (feature & ~ZEND_CPU_EDX_MASK));
  93. } else if (feature & ZEND_CPU_EBX_MASK) {
  94. return (cpuinfo.ebx & (feature & ~ZEND_CPU_EBX_MASK));
  95. } else {
  96. return (cpuinfo.ecx & feature);
  97. }
  98. }
  99. /*
  100. * Local variables:
  101. * tab-width: 4
  102. * c-basic-offset: 4
  103. * indent-tabs-mode: t
  104. * End:
  105. */