]> granicus.if.org Git - php/commitdiff
Added ZEND_API zend_cpu_supports
authorXinchen Hui <laruence@gmail.com>
Tue, 16 Jan 2018 06:44:06 +0000 (14:44 +0800)
committerXinchen Hui <laruence@gmail.com>
Tue, 16 Jan 2018 06:53:00 +0000 (14:53 +0800)
UPGRADING.INTERNALS
Zend/Makefile.am
Zend/zend_cpuinfo.c [new file with mode: 0644]
configure.ac
ext/standard/string.c
win32/build/config.w32

index bf3a63b9b34a446fa942f9283485ea7ae0bda573..45143ead7c5de263c0f4503df819cd8c7231083f 100644 (file)
@@ -13,6 +13,7 @@ PHP 7.3 INTERNALS UPGRADE NOTES
   j. cast_object() with _IS_NUMBER
   k. zend_fcall_info_cache.initialized
   l. php_hrtime_current()
+  m. zend_cpu_supports()
 
 2. Build system changes
   a. Unix build system changes
@@ -99,6 +100,8 @@ PHP 7.3 INTERNALS UPGRADE NOTES
   l. php_hrtime_current() delivers the number of nanoseconds since an uncertain
      point in the past.
 
+  m. zend_cpu_supports() determines if a feature is supported by current cpu.
+
 ========================
 2. Build system changes
 ========================
index 5f7f25df677e05f5eb0564ef5750503cfcc2386d..dc62bf681c49729a30b40cb22991f9d8f061785c 100644 (file)
@@ -18,7 +18,7 @@ libZend_la_SOURCES=\
        zend_default_classes.c \
        zend_iterators.c zend_interfaces.c zend_exceptions.c \
        zend_strtod.c zend_closures.c zend_float.c zend_string.c zend_signal.c \
-       zend_generators.c zend_virtual_cwd.c zend_ast.c zend_smart_str.c
+       zend_generators.c zend_virtual_cwd.c zend_ast.c zend_smart_str.c zend_cpuinfo.c
 
 libZend_la_CFLAGS = -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1
 libZend_la_LDFLAGS =
diff --git a/Zend/zend_cpuinfo.c b/Zend/zend_cpuinfo.c
new file mode 100644 (file)
index 0000000..9defda0
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+   +----------------------------------------------------------------------+
+   | 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.h"
+#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;
+
+#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
+static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
+       __asm__ __volatile__ (
+               "cpuid"
+               : "=a"(cpuinfo.eax), "=b"(cpuinfo.ebx), "=c"(cpuinfo.ecx), "=d"(cpuinfo.edx)
+               : "a"(func), "c"(subfunc)
+       );
+}
+#elif defined(ZEND_WIN32)
+# include <intrin.h>
+static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
+       __cpuidex(&cpuinfo, func, subfunc)
+}
+#else
+static void __zend_cpuid(uint32_t func, uint32_t subfunc) {
+       cpuinfo.eax = 0;
+}
+#endif
+
+ZEND_API int zend_cpu_supports(zend_cpu_feature feature) {
+       if (!cpuinfo.initialized) {
+               cpuinfo.initialized = 1;
+               __zend_cpuid(0, 0);
+               if (cpuinfo.eax == 0) {
+                       return 0;
+               }
+               __zend_cpuid(1, 0);
+       }
+       if (feature & ZEND_CPU_EDX_MASK) {
+               return (cpuinfo.edx & (feature & ~ZEND_CPU_EDX_MASK));
+       } else {
+               return (cpuinfo.ecx & feature);
+       }
+}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ */
index 9c033fafbcad4948d2c4ad8066663e58f16b7b91..0d3c83ee1b44b4cf9ab9dad7baa741db3a6650a6 100644 (file)
@@ -566,8 +566,6 @@ dnl Check __builtin_ssubl_overflow
 PHP_CHECK_BUILTIN_SSUBL_OVERFLOW
 dnl Check __builtin_ssubll_overflow
 PHP_CHECK_BUILTIN_SSUBLL_OVERFLOW
-dnl Check __builtin_cpu_init
-PHP_CHECK_BUILTIN_CPU_INIT
 
 dnl Check for members of the stat structure
 AC_STRUCT_ST_BLKSIZE
@@ -1488,7 +1486,7 @@ PHP_ADD_SOURCES(Zend, \
     zend_iterators.c zend_interfaces.c zend_exceptions.c zend_strtod.c zend_gc.c \
     zend_closures.c zend_float.c zend_string.c zend_signal.c zend_generators.c \
     zend_virtual_cwd.c zend_ast.c zend_objects.c zend_object_handlers.c zend_objects_API.c \
-    zend_default_classes.c zend_inheritance.c zend_smart_str.c, \
+    zend_default_classes.c zend_inheritance.c zend_smart_str.c zend_cpuinfo.c, \
        -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
 
 dnl Selectively disable optimization due to high RAM usage during
index 2abd2c796a18a0bb7b1f52c72fb57a04b29a29b7..05512786a86b3f5f0efd47d788fd1c7601544988 100644 (file)
@@ -3865,10 +3865,12 @@ PHPAPI zend_string *php_addcslashes(zend_string *str, int should_free, char *wha
 /* }}} */
 
 /* {{{ php_addslashes */
-#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && HAVE_FUNC_ATTRIBUTE_IFUNC && HAVE_FUNC_ATTRIBUTE_TARGET && HAVE_NMMINTRIN_H
+#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && \
+    defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(HAVE_NMMINTRIN_H)
 
 #include <nmmintrin.h>
 #include "Zend/zend_bitset.h"
+#include "Zend/zend_cpuinfo.h"
 
 PHPAPI zend_string *php_addslashes(zend_string *str, int should_free) __attribute__((ifunc("resolve_addslashes")));
 
@@ -3877,12 +3879,9 @@ zend_string *php_addslashes_default(zend_string *str, int should_free);
 
 /* {{{ resolve_addslashes */
 static void *resolve_addslashes() {
-#if PHP_HAVE_BUILTIN_CPU_INIT
-       __builtin_cpu_init();
-       if (__builtin_cpu_supports("sse4.2")) {
+       if (zend_cpu_supports(ZEND_CPU_FEATURE_SSE42)) {
                return php_addslashes_sse4;
        }
-#endif
        return  php_addslashes_default;
 }
 /* }}} */
index 71cf49156f84f69ff151ffa0a015b04d3de87525..a043ddbb205734e0d820f670b72298007873ee04 100644 (file)
@@ -234,7 +234,7 @@ ADD_SOURCES("Zend", "zend_language_parser.c zend_language_scanner.c \
        zend_object_handlers.c zend_objects_API.c \
        zend_default_classes.c zend_execute.c zend_strtod.c zend_gc.c zend_closures.c \
        zend_float.c zend_string.c zend_generators.c zend_virtual_cwd.c zend_ast.c \
-       zend_inheritance.c zend_smart_str.c");
+       zend_inheritance.c zend_smart_str.c zend_cpuinfo.c");
 
 ADD_FLAG("CFLAGS_BD_ZEND", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");