xref: /PHP-8.2/Zend/zend_cpuinfo.h (revision 67dd48fb)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 <laruence@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef ZEND_CPU_INFO_H
20 #define ZEND_CPU_INFO_H
21 
22 #include "zend.h"
23 
24 #define ZEND_CPU_EBX_MASK     (1<<30)
25 #define ZEND_CPU_EDX_MASK     (1U<<31)
26 
27 typedef enum _zend_cpu_feature {
28 	/* ECX */
29 	ZEND_CPU_FEATURE_SSE3			= (1<<0),
30 	ZEND_CPU_FEATURE_PCLMULQDQ		= (1<<1),
31 	ZEND_CPU_FEATURE_DTES64			= (1<<2),
32 	ZEND_CPU_FEATURE_MONITOR		= (1<<3),
33 	ZEND_CPU_FEATURE_DSCPL			= (1<<4),
34 	ZEND_CPU_FEATURE_VMX			= (1<<5),
35 	ZEND_CPU_FEATURE_SMX			= (1<<6),
36 	ZEND_CPU_FEATURE_EST			= (1<<7),
37 	ZEND_CPU_FEATURE_TM2			= (1<<8),
38 	ZEND_CPU_FEATURE_SSSE3			= (1<<9),
39 	ZEND_CPU_FEATURE_CID			= (1<<10),
40 	ZEND_CPU_FEATURE_SDBG			= (1<<11),
41 	ZEND_CPU_FEATURE_FMA			= (1<<12),
42 	ZEND_CPU_FEATURE_CX16			= (1<<13),
43 	ZEND_CPU_FEATURE_XTPR			= (1<<14),
44 	ZEND_CPU_FEATURE_PDCM			= (1<<15),
45 	/* reserved						= (1<<16),*/
46 	ZEND_CPU_FEATURE_PCID			= (1<<17),
47 	ZEND_CPU_FEATURE_DCA			= (1<<18),
48 	ZEND_CPU_FEATURE_SSE41			= (1<<19),
49 	ZEND_CPU_FEATURE_SSE42			= (1<<20),
50 	ZEND_CPU_FEATURE_X2APIC			= (1<<21),
51 	ZEND_CPU_FEATURE_MOVBE			= (1<<22),
52 	ZEND_CPU_FEATURE_POPCNT			= (1<<23),
53 	ZEND_CPU_FEATURE_TSC_DEADLINE 	= (1<<24),
54 	ZEND_CPU_FEATURE_AES			= (1<<25),
55 	ZEND_CPU_FEATURE_XSAVE			= (1<<26),
56 	ZEND_CPU_FEATURE_OSXSAVE		= (1<<27) ,
57 	ZEND_CPU_FEATURE_AVX			= (1<<28),
58 	ZEND_CPU_FEATURE_F16C			= (1<<29),
59 	/* intentionally don't support	= (1<<30) */
60 	/* intentionally don't support	= (1<<31) */
61 
62 	/* EBX */
63 	ZEND_CPU_FEATURE_AVX2			= (1<<5 | ZEND_CPU_EBX_MASK),
64 
65 	/* EDX */
66 	ZEND_CPU_FEATURE_FPU			= (1<<0 | ZEND_CPU_EDX_MASK),
67 	ZEND_CPU_FEATURE_VME			= (1<<1 | ZEND_CPU_EDX_MASK),
68 	ZEND_CPU_FEATURE_DE				= (1<<2 | ZEND_CPU_EDX_MASK),
69 	ZEND_CPU_FEATURE_PSE			= (1<<3 | ZEND_CPU_EDX_MASK),
70 	ZEND_CPU_FEATURE_TSC			= (1<<4 | ZEND_CPU_EDX_MASK),
71 	ZEND_CPU_FEATURE_MSR			= (1<<5 | ZEND_CPU_EDX_MASK),
72 	ZEND_CPU_FEATURE_PAE			= (1<<6 | ZEND_CPU_EDX_MASK),
73 	ZEND_CPU_FEATURE_MCE			= (1<<7 | ZEND_CPU_EDX_MASK),
74 	ZEND_CPU_FEATURE_CX8			= (1<<8 | ZEND_CPU_EDX_MASK),
75 	ZEND_CPU_FEATURE_APIC			= (1<<9 | ZEND_CPU_EDX_MASK),
76 	/* reserved						= (1<<10 | ZEND_CPU_EDX_MASK),*/
77 	ZEND_CPU_FEATURE_SEP			= (1<<11 | ZEND_CPU_EDX_MASK),
78 	ZEND_CPU_FEATURE_MTRR			= (1<<12 | ZEND_CPU_EDX_MASK),
79 	ZEND_CPU_FEATURE_PGE			= (1<<13 | ZEND_CPU_EDX_MASK),
80 	ZEND_CPU_FEATURE_MCA			= (1<<14 | ZEND_CPU_EDX_MASK),
81 	ZEND_CPU_FEATURE_CMOV			= (1<<15 | ZEND_CPU_EDX_MASK),
82 	ZEND_CPU_FEATURE_PAT			= (1<<16 | ZEND_CPU_EDX_MASK),
83 	ZEND_CPU_FEATURE_PSE36			= (1<<17 | ZEND_CPU_EDX_MASK),
84 	ZEND_CPU_FEATURE_PN				= (1<<18 | ZEND_CPU_EDX_MASK),
85 	ZEND_CPU_FEATURE_CLFLUSH		= (1<<19 | ZEND_CPU_EDX_MASK),
86 	/* reserved						= (1<<20 | ZEND_CPU_EDX_MASK),*/
87 	ZEND_CPU_FEATURE_DS				= (1<<21 | ZEND_CPU_EDX_MASK),
88 	ZEND_CPU_FEATURE_ACPI			= (1<<22 | ZEND_CPU_EDX_MASK),
89 	ZEND_CPU_FEATURE_MMX			= (1<<23 | ZEND_CPU_EDX_MASK),
90 	ZEND_CPU_FEATURE_FXSR			= (1<<24 | ZEND_CPU_EDX_MASK),
91 	ZEND_CPU_FEATURE_SSE			= (1<<25 | ZEND_CPU_EDX_MASK),
92 	ZEND_CPU_FEATURE_SSE2			= (1<<26 | ZEND_CPU_EDX_MASK),
93 	ZEND_CPU_FEATURE_SS				= (1<<27 | ZEND_CPU_EDX_MASK),
94 	ZEND_CPU_FEATURE_HT				= (1<<28 | ZEND_CPU_EDX_MASK),
95 	ZEND_CPU_FEATURE_TM				= (1<<29 | ZEND_CPU_EDX_MASK)
96 	/*intentionally don't support   = (1<<30 | ZEND_CPU_EDX_MASK)*/
97 	/*intentionally don't support   = (1<<31 | ZEND_CPU_EDX_MASK)*/
98 } zend_cpu_feature;
99 
100 void zend_cpu_startup(void);
101 ZEND_API int zend_cpu_supports(zend_cpu_feature feature);
102 
103 #ifndef __has_attribute
104 # define __has_attribute(x) 0
105 #endif
106 
107 /* Address sanitizer is incompatible with ifunc resolvers, so exclude the
108  * CPU support helpers from asan.
109  * See also https://github.com/google/sanitizers/issues/342. */
110 #if __has_attribute(no_sanitize_address)
111 # define ZEND_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
112 #else
113 # define ZEND_NO_SANITIZE_ADDRESS
114 #endif
115 
116 #if PHP_HAVE_BUILTIN_CPU_SUPPORTS
117 /* NOTE: you should use following inline function in
118  * resolver functions (ifunc), as it could be called
119  * before all PLT symbols are resolved. in other words,
120  * resolver functions should not depend on any external
121  * functions */
122 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_sse2(void)123 static inline int zend_cpu_supports_sse2(void) {
124 #if PHP_HAVE_BUILTIN_CPU_INIT
125 	__builtin_cpu_init();
126 #endif
127 	return __builtin_cpu_supports("sse2");
128 }
129 
130 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_sse3(void)131 static inline int zend_cpu_supports_sse3(void) {
132 #if PHP_HAVE_BUILTIN_CPU_INIT
133 	__builtin_cpu_init();
134 #endif
135 	return __builtin_cpu_supports("sse3");
136 }
137 
138 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_ssse3(void)139 static inline int zend_cpu_supports_ssse3(void) {
140 #if PHP_HAVE_BUILTIN_CPU_INIT
141 	__builtin_cpu_init();
142 #endif
143 	return __builtin_cpu_supports("ssse3");
144 }
145 
146 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_sse41(void)147 static inline int zend_cpu_supports_sse41(void) {
148 #if PHP_HAVE_BUILTIN_CPU_INIT
149 	__builtin_cpu_init();
150 #endif
151 	return __builtin_cpu_supports("sse4.1");
152 }
153 
154 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_sse42(void)155 static inline int zend_cpu_supports_sse42(void) {
156 #if PHP_HAVE_BUILTIN_CPU_INIT
157 	__builtin_cpu_init();
158 #endif
159 	return __builtin_cpu_supports("sse4.2");
160 }
161 
162 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_avx(void)163 static inline int zend_cpu_supports_avx(void) {
164 #if PHP_HAVE_BUILTIN_CPU_INIT
165 	__builtin_cpu_init();
166 #endif
167 	return __builtin_cpu_supports("avx");
168 }
169 
170 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_avx2(void)171 static inline int zend_cpu_supports_avx2(void) {
172 #if PHP_HAVE_BUILTIN_CPU_INIT
173 	__builtin_cpu_init();
174 #endif
175 	return __builtin_cpu_supports("avx2");
176 }
177 #else
178 
zend_cpu_supports_sse2(void)179 static inline int zend_cpu_supports_sse2(void) {
180 	return zend_cpu_supports(ZEND_CPU_FEATURE_SSE2);
181 }
182 
zend_cpu_supports_sse3(void)183 static inline int zend_cpu_supports_sse3(void) {
184 	return zend_cpu_supports(ZEND_CPU_FEATURE_SSE3);
185 }
186 
zend_cpu_supports_ssse3(void)187 static inline int zend_cpu_supports_ssse3(void) {
188 	return zend_cpu_supports(ZEND_CPU_FEATURE_SSSE3);
189 }
190 
zend_cpu_supports_sse41(void)191 static inline int zend_cpu_supports_sse41(void) {
192 	return zend_cpu_supports(ZEND_CPU_FEATURE_SSE41);
193 }
194 
zend_cpu_supports_sse42(void)195 static inline int zend_cpu_supports_sse42(void) {
196 	return zend_cpu_supports(ZEND_CPU_FEATURE_SSE42);
197 }
198 
zend_cpu_supports_avx(void)199 static inline int zend_cpu_supports_avx(void) {
200 	return zend_cpu_supports(ZEND_CPU_FEATURE_AVX);
201 }
202 
zend_cpu_supports_avx2(void)203 static inline int zend_cpu_supports_avx2(void) {
204 	return zend_cpu_supports(ZEND_CPU_FEATURE_AVX2);
205 }
206 #endif
207 
208 /* __builtin_cpu_supports has pclmul from gcc9 */
209 #if PHP_HAVE_BUILTIN_CPU_SUPPORTS && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000))
210 ZEND_NO_SANITIZE_ADDRESS
zend_cpu_supports_pclmul(void)211 static inline int zend_cpu_supports_pclmul(void) {
212 #if PHP_HAVE_BUILTIN_CPU_INIT
213 	__builtin_cpu_init();
214 #endif
215 	return __builtin_cpu_supports("pclmul");
216 }
217 #else
zend_cpu_supports_pclmul(void)218 static inline int zend_cpu_supports_pclmul(void) {
219 	return zend_cpu_supports(ZEND_CPU_FEATURE_PCLMULQDQ);
220 }
221 #endif
222 
223 #endif
224