Skip to content

Integer semantics #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Zend/tests/int_special_values.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Conversion of special float values to int
--FILE--
<?php
$values = [
0.0,
INF,
-INF,
1 / INF,
-1 / INF, // Negative zero,
NAN
];

foreach($values as $value) {
var_dump($value);
var_dump((int)$value);
echo PHP_EOL;
}
?>
--EXPECT--
float(0)
int(0)

float(INF)
int(0)

float(-INF)
int(0)

float(0)
int(0)

float(-0)
int(0)

float(NAN)
int(0)
24 changes: 24 additions & 0 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,18 @@ ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /
op1_lval = Z_LVAL_P(op1);
}

/* prevent wrapping quirkiness on some processors where << 64 + x == << x */
if (Z_LVAL_P(op2) >= SIZEOF_ZEND_LONG * 8) {
ZVAL_LONG(result, 0);
return SUCCESS;
}

if (Z_LVAL_P(op2) < 0) {
zend_error(E_WARNING, "Bit shift by negative number");
ZVAL_FALSE(result);
return FAILURE;
}

ZVAL_LONG(result, op1_lval << Z_LVAL_P(op2));
return SUCCESS;
}
Expand All @@ -1469,6 +1481,18 @@ ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
op1_lval = Z_LVAL_P(op1);
}

/* prevent wrapping quirkiness on some processors where >> 64 + x == >> x */
if (Z_LVAL_P(op2) >= SIZEOF_ZEND_LONG * 8) {
ZVAL_LONG(result, (Z_LVAL_P(op1) < 0) ? -1 : 0);
return SUCCESS;
}

if (Z_LVAL_P(op2) < 0) {
zend_error(E_WARNING, "Bit shift by negative number");
ZVAL_FALSE(result);
return FAILURE;
}

ZVAL_LONG(result, op1_lval >> Z_LVAL_P(op2));
return SUCCESS;
}
Expand Down
17 changes: 14 additions & 3 deletions Zend/zend_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,20 @@ ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, cons
END_EXTERN_C()

#if ZEND_DVAL_TO_LVAL_CAST_OK
# define zend_dval_to_lval(d) ((zend_long) (d))
static zend_always_inline zend_long zend_dval_to_lval(double d)
{
if (EXPECTED(zend_finite(d)) && EXPECTED(!zend_isnan(d))) {
return (zend_long)d;
} else {
return 0;
}
}
#elif SIZEOF_ZEND_LONG == 4
static zend_always_inline zend_long zend_dval_to_lval(double d)
{
if (d > ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
return 0;
} else if (d > ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
double two_pow_32 = pow(2., 32.),
dmod;

Expand All @@ -93,8 +102,10 @@ static zend_always_inline zend_long zend_dval_to_lval(double d)
#else
static zend_always_inline zend_long zend_dval_to_lval(double d)
{
if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
return 0;
/* >= as (double)ZEND_LONG_MAX is outside signed range */
if (d >= ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
} else if (d >= ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
double two_pow_64 = pow(2., 64.),
dmod;

Expand Down
Loading