diff options
| author | datibbaw <datibbaw@php.net> | 2013-11-19 15:36:06 +0800 |
|---|---|---|
| committer | Bob Weinand <bobwei9@hotmail.com> | 2014-02-06 14:41:21 +0100 |
| commit | aff56f3c4539869910cf2778cf0ece2d8c2dd671 (patch) | |
| tree | e98c54de8656d0684fba10018abadbba81a4feb4 /Zend/zend_operators.c | |
| parent | df76fd8997ef503c5fee479c7349c47996a0c781 (diff) | |
| download | php-git-aff56f3c4539869910cf2778cf0ece2d8c2dd671.tar.gz | |
add T_POW (**) operator
Fixed recognition of the operator
Added opcode, still doing multiply instead of pow()
opcode now always returns int(42)
The right answer, but always a float
Yanked code from pow() implementation.
Should not handle negative long as exponent ourselves
Added test cases from pow()
Moved precedence higher than '~'
Added GMP operator overloading
Added ZEND_ASSIGN_POW (**=) operator.
Added pow() as a language construct.
Adjusted test cases for changed precedence.
Reduced pow() to shell function around ZEND_API pow_function()
Reduced test case to only contain edge cases
Added overloading test case
Moved unary minus above T_POW
Revert "Added pow() as a language construct."
Bad bad bad idea.
This reverts commit f60b98cf7a8371233d800a6faa286ddba4432d02.
Reverted unary minus behaviour due to previous revert.
Convert arrays to int(0)
Exponent with array as a base becomes int(0)
Rebase against master
Fixed tokenizer test case
Diffstat (limited to 'Zend/zend_operators.c')
| -rw-r--r-- | Zend/zend_operators.c | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index 3434694f4c..03dbb15264 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -963,6 +963,87 @@ ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ * } /* }}} */ +ZEND_API int pow_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) +{ + zval op1_copy, op2_copy; + int converted = 0; + + while (1) { + switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { + case TYPE_PAIR(IS_LONG, IS_LONG): + if (Z_LVAL_P(op2) >= 0) { + long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2); + + if (i == 0) { + ZVAL_LONG(result, 1L); + return SUCCESS; + } else if (l2 == 0) { + ZVAL_LONG(result, 0); + return SUCCESS; + } + + while (i >= 1) { + long overflow; + double dval = 0.0; + + if (i % 2) { + --i; + ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow); + if (overflow) { + ZVAL_DOUBLE(result, dval * pow(l2, i)); + return SUCCESS; + } + } else { + i /= 2; + ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow); + if (overflow) { + ZVAL_DOUBLE(result, (double)l1 * pow(dval, i)); + return SUCCESS; + } + } + } + /* i == 0 */ + ZVAL_LONG(result, l1); + } else { + ZVAL_DOUBLE(result, pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2))); + } + return SUCCESS; + + case TYPE_PAIR(IS_LONG, IS_DOUBLE): + ZVAL_DOUBLE(result, pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2))); + return SUCCESS; + + case TYPE_PAIR(IS_DOUBLE, IS_LONG): + ZVAL_DOUBLE(result, pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2))); + return SUCCESS; + + case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE): + ZVAL_DOUBLE(result, pow(Z_DVAL_P(op1), Z_DVAL_P(op2))); + return SUCCESS; + + default: + if (!converted) { + ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW); + + if (Z_TYPE_P(op1) == IS_ARRAY) { + ZVAL_LONG(op1, 0); + } else { + zendi_convert_scalar_to_number(op1, op1_copy, result); + } + if (Z_TYPE_P(op2) == IS_ARRAY) { + ZVAL_LONG(op2, 0); + } else { + zendi_convert_scalar_to_number(op2, op2_copy, result); + } + converted = 1; + } else { + zend_error(E_ERROR, "Unsupported operand types"); + return FAILURE; + } + } + } +} + ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ { zval op1_copy, op2_copy; |
