Permalink
Browse files

Fixed floating point mathematic speed degradation (Christian)

  • Loading branch information...
1 parent d3b4270 commit 31c0af245e7601f3c3c870c20a57e9cf8634bb7b Dmitry Stogov committed Mar 18, 2009
Showing with 93 additions and 65 deletions.
  1. +1 −1 Zend/Makefile.am
  2. +4 −0 Zend/Zend.dsp
  3. +1 −0 Zend/zend.c
  4. +5 −0 Zend/zend_execute_API.c
  5. +73 −9 Zend/zend_float.h
  6. +2 −0 Zend/zend_globals.h
  7. +0 −39 Zend/zend_operators.c
  8. +1 −5 Zend/zend_strtod.c
  9. +1 −1 configure.in
  10. +3 −9 ext/standard/math.c
  11. +2 −1 win32/build/config.w32
View
@@ -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@
View
@@ -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
View
@@ -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;
}
/* }}} */
@@ -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;
}
/* }}} */
View
@@ -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 */
View
@@ -254,6 +254,8 @@ struct _zend_executor_globals {
zend_bool active;
+ void *saved_fpu_cw;
+
void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};
View
@@ -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,31 +753,23 @@ 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);
}
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;
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,31 +815,23 @@ 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);
}
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:
@@ -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:
Oops, something went wrong. Retry.

0 comments on commit 31c0af2

Please sign in to comment.