- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathzend_cpuinfo.c
153 lines (139 loc) · 4.71 KB
/
zend_cpuinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) 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"
typedefstruct_zend_cpu_info {
uint32_teax;
uint32_tebx;
uint32_tecx;
uint32_tedx;
uint32_tinitialized;
} zend_cpu_info;
staticzend_cpu_infocpuinfo= {0};
#if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
# if defined(HAVE_CPUID_H) && defined(HAVE_CPUID_COUNT) /* use cpuid.h functions */
# include<cpuid.h>
staticvoid__zend_cpuid(uint32_tfunc, uint32_tsubfunc, zend_cpu_info*cpuinfo) {
__cpuid_count(func, subfunc, cpuinfo->eax, cpuinfo->ebx, cpuinfo->ecx, cpuinfo->edx);
}
# else/* use inline asm */
staticvoid__zend_cpuid(uint32_tfunc, uint32_tsubfunc, zend_cpu_info*cpuinfo) {
# if defined(__i386__) && (defined(__pic__) || defined(__PIC__))
/* PIC on i386 uses %ebx, so preserve it. */
__asm__ __volatile__ (
"pushl %%ebx\n"
"cpuid\n"
"mov %%ebx,%1\n"
"popl %%ebx"
: "=a"(cpuinfo->eax), "=r"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
: "a"(func), "c"(subfunc)
);
# else
__asm__ __volatile__ (
"cpuid"
: "=a"(cpuinfo->eax), "=b"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
: "a"(func), "c"(subfunc)
);
# endif
}
# endif
#elif defined(_MSC_VER) && !defined(__clang__) && (defined(_M_X64) || defined(_M_IX86)) /* use MSVC __cpuidex intrin */
# include<intrin.h>
staticvoid__zend_cpuid(uint32_tfunc, uint32_tsubfunc, zend_cpu_info*cpuinfo) {
intregs[4];
__cpuidex(regs, func, subfunc);
cpuinfo->eax=regs[0];
cpuinfo->ebx=regs[1];
cpuinfo->ecx=regs[2];
cpuinfo->edx=regs[3];
}
#else/* fall back to zero */
staticvoid__zend_cpuid(uint32_tfunc, uint32_tsubfunc, zend_cpu_info*cpuinfo) {
cpuinfo->eax=0;
}
#endif
#if defined(__i386__) || defined(__x86_64__) || defined(_M_X64) || defined(_M_IX86)
/* Function based on compiler-rt implementation. */
staticunsignedget_xcr0_eax(void) {
# if defined(__GNUC__) || defined(__clang__)
// Check xgetbv; this uses a .byte sequence instead of the instruction
// directly because older assemblers do not include support for xgetbv and
// there is no easy way to conditionally compile based on the assembler used.
unsignedeax, edx;
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(0));
returneax;
# elif defined(ZEND_WIN32) && defined(_XCR_XFEATURE_ENABLED_MASK)
return_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
# else
return0;
# endif
}
staticboolis_avx_supported(void) {
if (!(cpuinfo.ecx&ZEND_CPU_FEATURE_AVX)) {
/* No support for AVX */
return0;
}
if (!(cpuinfo.ecx&ZEND_CPU_FEATURE_OSXSAVE)) {
/* The operating system does not support XSAVE. */
return0;
}
if ((get_xcr0_eax() &0x6) !=0x6) {
/* XCR0 SSE and AVX bits must be set. */
return0;
}
return1;
}
#else
staticboolis_avx_supported(void) {
return0;
}
#endif
voidzend_cpu_startup(void)
{
if (!cpuinfo.initialized) {
zend_cpu_infoebx;
intmax_feature;
cpuinfo.initialized=1;
__zend_cpuid(0, 0, &cpuinfo);
max_feature=cpuinfo.eax;
if (max_feature==0) {
return;
}
__zend_cpuid(1, 0, &cpuinfo);
/* for avx2 */
if (max_feature >= 7) {
__zend_cpuid(7, 0, &ebx);
cpuinfo.ebx=ebx.ebx;
} else {
cpuinfo.ebx=0;
}
if (!is_avx_supported()) {
cpuinfo.edx &= ~ZEND_CPU_FEATURE_AVX;
cpuinfo.ebx &= ~(ZEND_CPU_FEATURE_AVX2& ~ZEND_CPU_EBX_MASK);
}
}
}
ZEND_APIintzend_cpu_supports(zend_cpu_featurefeature) {
ZEND_ASSERT(cpuinfo.initialized);
if (feature&ZEND_CPU_EDX_MASK) {
return (cpuinfo.edx& (feature& ~ZEND_CPU_EDX_MASK));
} elseif (feature&ZEND_CPU_EBX_MASK) {
return (cpuinfo.ebx& (feature& ~ZEND_CPU_EBX_MASK));
} else {
return (cpuinfo.ecx&feature);
}
}