Skip to content

Commit 3a3e049

Browse files
committed
Added ZEND_API zend_cpu_supports
1 parent 022e029 commit 3a3e049

File tree

6 files changed

+83
-10
lines changed

6 files changed

+83
-10
lines changed

Diff for: UPGRADING.INTERNALS

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ PHP 7.3 INTERNALS UPGRADE NOTES
1313
j. cast_object() with _IS_NUMBER
1414
k. zend_fcall_info_cache.initialized
1515
l. php_hrtime_current()
16+
m. zend_cpu_supports()
1617

1718
2. Build system changes
1819
a. Unix build system changes
@@ -99,6 +100,8 @@ PHP 7.3 INTERNALS UPGRADE NOTES
99100
l. php_hrtime_current() delivers the number of nanoseconds since an uncertain
100101
point in the past.
101102

103+
m. zend_cpu_supports() determines if a feature is supported by current cpu.
104+
102105
========================
103106
2. Build system changes
104107
========================

Diff for: Zend/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ libZend_la_SOURCES=\
1818
zend_default_classes.c \
1919
zend_iterators.c zend_interfaces.c zend_exceptions.c \
2020
zend_strtod.c zend_closures.c zend_float.c zend_string.c zend_signal.c \
21-
zend_generators.c zend_virtual_cwd.c zend_ast.c zend_smart_str.c
21+
zend_generators.c zend_virtual_cwd.c zend_ast.c zend_smart_str.c zend_cpuinfo.c
2222

2323
libZend_la_CFLAGS = -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1
2424
libZend_la_LDFLAGS =

Diff for: Zend/zend_cpuinfo.c

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
19+
#include "zend.h"
20+
#include "zend_cpuinfo.h"
21+
22+
typedef struct _zend_cpu_info {
23+
uint32_t eax;
24+
uint32_t ebx;
25+
uint32_t ecx;
26+
uint32_t edx;
27+
uint32_t initialized;
28+
} zend_cpu_info;
29+
30+
static zend_cpu_info cpuinfo;
31+
32+
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
33+
static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
34+
__asm__ __volatile__ (
35+
"cpuid"
36+
: "=a"(cpuinfo.eax), "=b"(cpuinfo.ebx), "=c"(cpuinfo.ecx), "=d"(cpuinfo.edx)
37+
: "a"(func), "c"(subfunc)
38+
);
39+
}
40+
#elif defined(ZEND_WIN32)
41+
# include <intrin.h>
42+
static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
43+
__cpuidex(&cpuinfo, func, subfunc)
44+
}
45+
#else
46+
static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
47+
cpuinfo.eax = 0;
48+
}
49+
#endif
50+
51+
ZEND_API int zend_cpu_supports(zend_cpu_feature feature) {
52+
if (!cpuinfo.initialized) {
53+
cpuinfo.initialized = 1;
54+
__zend_cpuid(0, 0);
55+
if (cpuinfo.eax == 0) {
56+
return 0;
57+
}
58+
__zend_cpuid(1, 0);
59+
}
60+
if (feature & ZEND_CPU_EDX_MASK) {
61+
return (cpuinfo.edx & (feature & ~ZEND_CPU_EDX_MASK));
62+
} else {
63+
return (cpuinfo.ecx & feature);
64+
}
65+
}
66+
67+
/*
68+
* Local variables:
69+
* tab-width: 4
70+
* c-basic-offset: 4
71+
* indent-tabs-mode: t
72+
* End:
73+
*/

Diff for: configure.ac

+1-3
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,6 @@ dnl Check __builtin_ssubl_overflow
566566
PHP_CHECK_BUILTIN_SSUBL_OVERFLOW
567567
dnl Check __builtin_ssubll_overflow
568568
PHP_CHECK_BUILTIN_SSUBLL_OVERFLOW
569-
dnl Check __builtin_cpu_init
570-
PHP_CHECK_BUILTIN_CPU_INIT
571569

572570
dnl Check for members of the stat structure
573571
AC_STRUCT_ST_BLKSIZE
@@ -1488,7 +1486,7 @@ PHP_ADD_SOURCES(Zend, \
14881486
zend_iterators.c zend_interfaces.c zend_exceptions.c zend_strtod.c zend_gc.c \
14891487
zend_closures.c zend_float.c zend_string.c zend_signal.c zend_generators.c \
14901488
zend_virtual_cwd.c zend_ast.c zend_objects.c zend_object_handlers.c zend_objects_API.c \
1491-
zend_default_classes.c zend_inheritance.c zend_smart_str.c, \
1489+
zend_default_classes.c zend_inheritance.c zend_smart_str.c zend_cpuinfo.c, \
14921490
-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
14931491

14941492
dnl Selectively disable optimization due to high RAM usage during

Diff for: ext/standard/string.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -3865,10 +3865,12 @@ PHPAPI zend_string *php_addcslashes(zend_string *str, int should_free, char *wha
38653865
/* }}} */
38663866

38673867
/* {{{ php_addslashes */
3868-
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && HAVE_FUNC_ATTRIBUTE_IFUNC && HAVE_FUNC_ATTRIBUTE_TARGET && HAVE_NMMINTRIN_H
3868+
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && \
3869+
defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(HAVE_NMMINTRIN_H)
38693870

38703871
#include <nmmintrin.h>
38713872
#include "Zend/zend_bitset.h"
3873+
#include "Zend/zend_cpuinfo.h"
38723874

38733875
PHPAPI zend_string *php_addslashes(zend_string *str, int should_free) __attribute__((ifunc("resolve_addslashes")));
38743876

@@ -3877,12 +3879,9 @@ zend_string *php_addslashes_default(zend_string *str, int should_free);
38773879

38783880
/* {{{ resolve_addslashes */
38793881
static void *resolve_addslashes() {
3880-
#if PHP_HAVE_BUILTIN_CPU_INIT
3881-
__builtin_cpu_init();
3882-
if (__builtin_cpu_supports("sse4.2")) {
3882+
if (zend_cpu_supports(ZEND_CPU_FEATURE_SSE42)) {
38833883
return php_addslashes_sse4;
38843884
}
3885-
#endif
38863885
return php_addslashes_default;
38873886
}
38883887
/* }}} */

Diff for: win32/build/config.w32

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ ADD_SOURCES("Zend", "zend_language_parser.c zend_language_scanner.c \
234234
zend_object_handlers.c zend_objects_API.c \
235235
zend_default_classes.c zend_execute.c zend_strtod.c zend_gc.c zend_closures.c \
236236
zend_float.c zend_string.c zend_generators.c zend_virtual_cwd.c zend_ast.c \
237-
zend_inheritance.c zend_smart_str.c");
237+
zend_inheritance.c zend_smart_str.c zend_cpuinfo.c");
238238

239239
ADD_FLAG("CFLAGS_BD_ZEND", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
240240

0 commit comments

Comments
 (0)