php-src/Zend/zend_cpuinfo.c

88 lines
2.6 KiB
C
Raw Normal View History

2018-01-16 06:44:06 +00:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) 2018-2018 Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Xinchen Hui <xinchen.h@zend.com> |
+----------------------------------------------------------------------+
*/
#include "zend_cpuinfo.h"
typedef struct _zend_cpu_info {
uint32_t eax;
uint32_t ebx;
uint32_t ecx;
uint32_t edx;
uint32_t initialized;
} zend_cpu_info;
static zend_cpu_info cpuinfo = {0};
2018-01-16 06:44:06 +00:00
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
#include <cpuid.h>
static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
__cpuid_count(func, subfunc, cpuinfo->eax, cpuinfo->ebx, cpuinfo->ecx, cpuinfo->edx);
2018-01-16 06:44:06 +00:00
}
#elif defined(ZEND_WIN32)
# include <intrin.h>
static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
2018-01-16 09:56:46 +00:00
int regs[4];
__cpuidex(regs, func, subfunc);
cpuinfo->eax = regs[0];
cpuinfo->ebx = regs[1];
cpuinfo->ecx = regs[2];
cpuinfo->edx = regs[3];
2018-01-16 06:44:06 +00:00
}
#else
static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
cpuinfo->eax = 0;
2018-01-16 06:44:06 +00:00
}
#endif
void zend_cpu_startup(void)
{
2018-01-16 06:44:06 +00:00
if (!cpuinfo.initialized) {
zend_cpu_info ebx;
2018-01-16 06:44:06 +00:00
cpuinfo.initialized = 1;
__zend_cpuid(0, 0, &cpuinfo);
2018-01-16 06:44:06 +00:00
if (cpuinfo.eax == 0) {
return;
2018-01-16 06:44:06 +00:00
}
__zend_cpuid(1, 0, &cpuinfo);
/* for avx2 */
__zend_cpuid(7, 0, &ebx);
cpuinfo.ebx = ebx.ebx;
2018-01-16 06:44:06 +00:00
}
}
ZEND_API int zend_cpu_supports(zend_cpu_feature feature) {
2018-01-16 06:44:06 +00:00
if (feature & ZEND_CPU_EDX_MASK) {
return (cpuinfo.edx & (feature & ~ZEND_CPU_EDX_MASK));
} else if (feature & ZEND_CPU_EBX_MASK) {
return (cpuinfo.ebx & (feature & ~ZEND_CPU_EBX_MASK));
2018-01-16 06:44:06 +00:00
} else {
return (cpuinfo.ecx & feature);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/