Skip to content

Commit

Permalink
Fixed floating point mathematic speed degradation (Christian)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstogov committed Mar 18, 2009
1 parent d3b4270 commit 31c0af2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Zend/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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@
Expand Down
4 changes: 4 additions & 0 deletions Zend/Zend.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/* }}} */
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
/* }}} */
Expand Down
82 changes: 73 additions & 9 deletions Zend/zend_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -45,7 +47,7 @@
For further details, please visit:
http://www.christian-seiler.de/projekte/fpmath/
Version: 20081026 */
Version: 20090317 */

/*
Implementation notes:
Expand All @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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; \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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; \
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ struct _zend_executor_globals {

zend_bool active;

void *saved_fpu_cw;

void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};

Expand Down
39 changes: 0 additions & 39 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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;

Expand All @@ -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): {
Expand Down Expand Up @@ -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;

Expand All @@ -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:
Expand All @@ -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;

Expand All @@ -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:
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 31c0af2

Please sign in to comment.