diff options
author | Dmitry Stogov <dmitry@php.net> | 2009-03-18 10:18:10 +0000 |
---|---|---|
committer | Dmitry Stogov <dmitry@php.net> | 2009-03-18 10:18:10 +0000 |
commit | 31c0af245e7601f3c3c870c20a57e9cf8634bb7b (patch) | |
tree | 38d551086ead5a5d7c84f70154028b712de5550e /Zend | |
parent | d3b42700a0279bfcd58414799b81cd332d1fa08f (diff) | |
download | php-git-31c0af245e7601f3c3c870c20a57e9cf8634bb7b.tar.gz |
Fixed floating point mathematic speed degradation (Christian)
Diffstat (limited to 'Zend')
-rw-r--r-- | Zend/Makefile.am | 2 | ||||
-rw-r--r-- | Zend/Zend.dsp | 4 | ||||
-rw-r--r-- | Zend/zend.c | 1 | ||||
-rw-r--r-- | Zend/zend_execute_API.c | 5 | ||||
-rw-r--r-- | Zend/zend_float.h | 82 | ||||
-rw-r--r-- | Zend/zend_globals.h | 2 | ||||
-rw-r--r-- | Zend/zend_operators.c | 39 | ||||
-rw-r--r-- | Zend/zend_strtod.c | 6 |
8 files changed, 87 insertions, 54 deletions
diff --git a/Zend/Makefile.am b/Zend/Makefile.am index def7a91cc3..219d2d3fc9 100644 --- a/Zend/Makefile.am +++ b/Zend/Makefile.am @@ -17,7 +17,7 @@ libZend_la_SOURCES=\ zend_objects_API.c zend_ts_hash.c zend_stream.c \ zend_default_classes.c \ zend_iterators.c zend_interfaces.c zend_exceptions.c \ - zend_strtod.c zend_closures.c + zend_strtod.c zend_closures.c zend_float.c libZend_la_LDFLAGS = libZend_la_LIBADD = @ZEND_EXTRA_LIBS@ diff --git a/Zend/Zend.dsp b/Zend/Zend.dsp index 0c6400fb1d..0a7ed77534 100644 --- a/Zend/Zend.dsp +++ b/Zend/Zend.dsp @@ -155,6 +155,10 @@ SOURCE=.\zend_extensions.c # End Source File
# Begin Source File
+SOURCE=.\zend_float.c
+# End Source File
+# Begin Source File
+
SOURCE=.\zend_hash.c
# End Source File
# Begin Source File
diff --git a/Zend/zend.c b/Zend/zend.c index af2ffaba43..44f2c4a9cb 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -549,6 +549,7 @@ static void executor_globals_ctor(zend_executor_globals *executor_globals TSRMLS EG(current_execute_data) = NULL; EG(current_module) = NULL; EG(exit_status) = 0; + EG(saved_fpu_cw) = NULL; EG(active) = 0; } /* }}} */ diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index 7e3bce21d8..99c8dc3638 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -123,6 +123,8 @@ static int clean_non_persistent_class_full(zend_class_entry **ce TSRMLS_DC) /* { void init_executor(TSRMLS_D) /* {{{ */ { + zend_init_fpu(TSRMLS_C); + INIT_ZVAL(EG(uninitialized_zval)); /* trick to make uninitialized_zval never be modified, passed by ref, etc. */ Z_ADDREF(EG(uninitialized_zval)); @@ -331,6 +333,9 @@ void shutdown_executor(TSRMLS_D) /* {{{ */ FREE_HASHTABLE(EG(in_autoload)); } } zend_end_try(); + + zend_shutdown_fpu(TSRMLS_C); + EG(active) = 0; } /* }}} */ diff --git a/Zend/zend_float.h b/Zend/zend_float.h index 591832ebe8..fdd248bdf3 100644 --- a/Zend/zend_float.h +++ b/Zend/zend_float.h @@ -21,10 +21,12 @@ #ifndef ZEND_FLOAT_H #define ZEND_FLOAT_H -#define ZEND_FLOAT_DECLARE XPFPA_DECLARE -#define ZEND_FLOAT_ENSURE() XPFPA_SWITCH_DOUBLE() -#define ZEND_FLOAT_RESTORE() XPFPA_RESTORE() -#define ZEND_FLOAT_RETURN(val) XPFPA_RETURN_DOUBLE(val) +/* + Define functions for FP initialization and de-initialization. +*/ +extern ZEND_API void zend_init_fpu(TSRMLS_D); +extern ZEND_API void zend_shutdown_fpu(TSRMLS_D); +extern ZEND_API void zend_ensure_fpu_mode(TSRMLS_D); /* Copy of the contents of xpfpa.h (which is under public domain) See http://wiki.php.net/rfc/rounding for details. @@ -45,7 +47,7 @@ For further details, please visit: http://www.christian-seiler.de/projekte/fpmath/ - Version: 20081026 */ + Version: 20090317 */ /* Implementation notes: @@ -69,11 +71,8 @@ /* MSVC detection (MSVC people usually don't use autoconf) */ #ifdef _MSC_VER # if _MSC_VER >= 1500 - /* Disable it, it slowdowns the floating operation more than - anything else, by a factor 3 (using Bench.php (mandel and - mandel2 for example)*/ /* Visual C++ 2008 or higher, supports _controlfp_s */ - /*# define HAVE__CONTROLFP_S */ +# define HAVE__CONTROLFP_S # else /* Visual C++ (up to 2005), supports _controlfp */ # define HAVE__CONTROLFP @@ -87,6 +86,19 @@ /* float.h defines _controlfp_s */ # include <float.h> +# define XPFPA_HAVE_CW 1 +# define XPFPA_CW_DATATYPE \ + unsigned int + +# define XPFPA_STORE_CW(vptr) do { \ + _controlfp_s((unsigned int *)(vptr), 0, 0); \ + } while (0) + +# define XPFPA_RESTORE_CW(vptr) do { \ + unsigned int _xpfpa_fpu_cw; \ + _controlfp_s(&_xpfpa_fpu_cw, *((unsigned int *)(vptr)), _MCW_PC); \ + } while (0) + # define XPFPA_DECLARE \ unsigned int _xpfpa_fpu_oldcw, _xpfpa_fpu_cw; @@ -141,6 +153,18 @@ # define XPFPA_DECLARE \ unsigned int _xpfpa_fpu_oldcw; +# define XPFPA_HAVE_CW 1 +# define XPFPA_CW_DATATYPE \ + unsigned int + +# define XPFPA_STORE_CW(vptr) do { \ + *((unsigned int *)(vptr)) = _controlfp(0, 0); \ + } while (0) + +# define XPFPA_RESTORE_CW(vptr) do { \ + _controlfp(*((unsigned int *)(vptr)), _MCW_PC); \ + } while (0) + # define XPFPA_SWITCH_DOUBLE() do { \ _xpfpa_fpu_oldcw = _controlfp(0, 0); \ _controlfp(_PC_53, _MCW_PC); \ @@ -188,6 +212,18 @@ # define XPFPA_DECLARE \ fpu_control_t _xpfpa_fpu_oldcw, _xpfpa_fpu_cw; +# define XPFPA_HAVE_CW 1 +# define XPFPA_CW_DATATYPE \ + fpu_control_t + +# define XPFPA_STORE_CW(vptr) do { \ + _FPU_GETCW((*((fpu_control_t *)(vptr)))); \ + } while (0) + +# define XPFPA_RESTORE_CW(vptr) do { \ + _FPU_SETCW((*((fpu_control_t *)(vptr)))); \ + } while (0) + # define XPFPA_SWITCH_DOUBLE() do { \ _FPU_GETCW(_xpfpa_fpu_oldcw); \ _xpfpa_fpu_cw = (_xpfpa_fpu_oldcw & ~_FPU_EXTENDED & ~_FPU_SINGLE) | _FPU_DOUBLE; \ @@ -235,6 +271,18 @@ # define XPFPA_DECLARE \ fp_prec_t _xpfpa_fpu_oldprec; +# define XPFPA_HAVE_CW 1 +# define XPFPA_CW_DATATYPE \ + fp_prec_t + +# define XPFPA_STORE_CW(vptr) do { \ + *((fp_prec_t *)(vptr)) = fpgetprec(); \ + } while (0) + +# define XPFPA_RESTORE_CW(vptr) do { \ + fpsetprec(*((fp_prec_t *)(vptr))); \ + } while (0) + # define XPFPA_SWITCH_DOUBLE() do { \ _xpfpa_fpu_oldprec = fpgetprec(); \ fpsetprec(FP_PD); \ @@ -298,6 +346,18 @@ # define XPFPA_DECLARE \ unsigned int _xpfpa_fpu_oldcw, _xpfpa_fpu_cw; +# define XPFPA_HAVE_CW 1 +# define XPFPA_CW_DATATYPE \ + unsigned int + +# define XPFPA_STORE_CW(vptr) do { \ + __asm__ __volatile__ ("fnstcw %0" : "=m" (*((unsigned int *)(vptr)))); \ + } while (0) + +# define XPFPA_RESTORE_CW(vptr) do { \ + __asm__ __volatile__ ("fldcw %0" : : "m" (*((unsigned int *)(vptr)))); \ + } while (0) + # define XPFPA_SWITCH_DOUBLE() do { \ __asm__ __volatile__ ("fnstcw %0" : "=m" (*&_xpfpa_fpu_oldcw)); \ _xpfpa_fpu_cw = (_xpfpa_fpu_oldcw & ~0x100) | 0x200; \ @@ -345,6 +405,10 @@ generated code will behave as planned. */ # define XPFPA_DECLARE /* NOP */ +# define XPFPA_HAVE_CW 0 +# define XPFPA_CW_DATATYPE unsigned int +# define XPFPA_STORE_CW(variable) /* NOP */ +# define XPFPA_RESTORE_CW(variable) /* NOP */ # define XPFPA_SWITCH_DOUBLE() /* NOP */ # define XPFPA_SWITCH_SINGLE() /* NOP */ # define XPFPA_SWITCH_DOUBLE_EXTENDED() /* NOP */ diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index 51baf773ef..546a392fde 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -254,6 +254,8 @@ struct _zend_executor_globals { zend_bool active; + void *saved_fpu_cw; + void *reserved[ZEND_MAX_RESERVED_RESOURCES]; }; diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index d78e0ece4c..ce1afff8d0 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -30,7 +30,6 @@ #include "zend_multiply.h" #include "zend_strtod.h" #include "zend_exceptions.h" -#include "zend_float.h" #define LONG_SIGN_MASK (1L << (8*sizeof(long)-1)) @@ -742,7 +741,6 @@ ZEND_API void multi_convert_to_string_ex(int argc, ...) ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) { - ZEND_FLOAT_DECLARE zval op1_copy, op2_copy; int converted = 0; @@ -755,9 +753,7 @@ ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) if ((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK) && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (lval & LONG_SIGN_MASK)) { - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2)); - ZEND_FLOAT_RESTORE(); } else { ZVAL_LONG(result, lval); } @@ -765,21 +761,15 @@ ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) } case TYPE_PAIR(IS_LONG, IS_DOUBLE): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_DOUBLE, IS_LONG): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2))); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_ARRAY, IS_ARRAY): { @@ -813,7 +803,6 @@ ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) { - ZEND_FLOAT_DECLARE zval op1_copy, op2_copy; int converted = 0; @@ -826,9 +815,7 @@ ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) if ((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK) && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (lval & LONG_SIGN_MASK)) { - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2)); - ZEND_FLOAT_RESTORE(); } else { ZVAL_LONG(result, lval); } @@ -836,21 +823,15 @@ ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) } case TYPE_PAIR(IS_LONG, IS_DOUBLE): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_DOUBLE, IS_LONG): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2))); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; default: @@ -869,7 +850,6 @@ ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) { - ZEND_FLOAT_DECLARE zval op1_copy, op2_copy; int converted = 0; @@ -878,29 +858,21 @@ ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) case TYPE_PAIR(IS_LONG, IS_LONG): { long overflow; - ZEND_FLOAT_ENSURE(); ZEND_SIGNED_MULTIPLY_LONG(Z_LVAL_P(op1),Z_LVAL_P(op2), Z_LVAL_P(result),Z_DVAL_P(result),overflow); - ZEND_FLOAT_RESTORE(); Z_TYPE_P(result) = overflow ? IS_DOUBLE : IS_LONG; return SUCCESS; } case TYPE_PAIR(IS_LONG, IS_DOUBLE): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_DOUBLE, IS_LONG): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2))); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE): - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; default: @@ -918,7 +890,6 @@ ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) { - ZEND_FLOAT_DECLARE zval op1_copy, op2_copy; int converted = 0; @@ -931,17 +902,13 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) return FAILURE; /* division by zero */ } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == LONG_MIN) { /* Prevent overflow error/crash */ - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, (double) LONG_MIN / -1); - ZEND_FLOAT_RESTORE(); return SUCCESS; } if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */ ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2)); } else { - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2)); - ZEND_FLOAT_RESTORE(); } return SUCCESS; @@ -951,9 +918,7 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) ZVAL_BOOL(result, 0); return FAILURE; /* division by zero */ } - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_LONG, IS_DOUBLE): @@ -962,9 +927,7 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) ZVAL_BOOL(result, 0); return FAILURE; /* division by zero */ } - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE): @@ -973,9 +936,7 @@ ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) ZVAL_BOOL(result, 0); return FAILURE; /* division by zero */ } - ZEND_FLOAT_ENSURE(); ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2)); - ZEND_FLOAT_RESTORE(); return SUCCESS; default: diff --git a/Zend/zend_strtod.c b/Zend/zend_strtod.c index 4a3e976d25..bc2ca97b19 100644 --- a/Zend/zend_strtod.c +++ b/Zend/zend_strtod.c @@ -93,7 +93,6 @@ #include <zend_operators.h> #include <zend_strtod.h> -#include <zend_float.h> #ifdef ZTS #include <TSRM.h> @@ -2033,7 +2032,6 @@ ret1: ZEND_API double zend_strtod (CONST char *s00, char **se) { - ZEND_FLOAT_DECLARE int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; CONST char *s, *s0, *s1; @@ -2046,8 +2044,6 @@ ZEND_API double zend_strtod (CONST char *s00, char **se) CONST char decimal_point = '.'; - ZEND_FLOAT_ENSURE(); - sign = nz0 = nz = 0; value(rv) = 0.; @@ -2578,7 +2574,7 @@ ret: } _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex); - ZEND_FLOAT_RETURN(result); + return result; } ZEND_API double zend_hex_strtod(const char *str, char **endptr) |