Skip to content

Unify type juggling in math.c #12286

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 1 commit into from
Sep 24, 2023
Merged
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
57 changes: 26 additions & 31 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ static inline double php_round_helper(double value, int mode) {
}

return integral;
EMPTY_SWITCH_DEFAULT_CASE();
}

ZEND_UNREACHABLE();
}
/* }}} */

Expand Down Expand Up @@ -259,16 +258,16 @@ PHP_FUNCTION(abs)
Z_PARAM_NUMBER(value)
ZEND_PARSE_PARAMETERS_END();

if (Z_TYPE_P(value) == IS_DOUBLE) {
RETURN_DOUBLE(fabs(Z_DVAL_P(value)));
} else if (Z_TYPE_P(value) == IS_LONG) {
if (Z_LVAL_P(value) == ZEND_LONG_MIN) {
RETURN_DOUBLE(-(double)ZEND_LONG_MIN);
} else {
RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value));
}
} else {
ZEND_ASSERT(0 && "Unexpected type");
switch (Z_TYPE_P(value)) {
case IS_LONG:
if (Z_LVAL_P(value) == ZEND_LONG_MIN) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly as a follow-up wrapping this unlikely condition in UNEXPECTED() might make sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is a little odd anyway. For 32 bit versions it made sense, because doubles would be able to accurately represent 2147483648, but for 64 bit versions they are not capable of accurately representing 9223372036854775808. I wonder if this should throw instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now wrapped in UNEXPECTED, this actually results in slightly better assembly.

RETURN_DOUBLE(-(double)ZEND_LONG_MIN);
} else {
RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value));
}
case IS_DOUBLE:
RETURN_DOUBLE(fabs(Z_DVAL_P(value)));
EMPTY_SWITCH_DEFAULT_CASE();
}
}
/* }}} */
Expand All @@ -282,12 +281,12 @@ PHP_FUNCTION(ceil)
Z_PARAM_NUMBER(value)
ZEND_PARSE_PARAMETERS_END();

if (Z_TYPE_P(value) == IS_DOUBLE) {
RETURN_DOUBLE(ceil(Z_DVAL_P(value)));
} else if (Z_TYPE_P(value) == IS_LONG) {
RETURN_DOUBLE(zval_get_double(value));
} else {
ZEND_ASSERT(0 && "Unexpected type");
switch (Z_TYPE_P(value)) {
case IS_LONG:
RETURN_DOUBLE(zval_get_double(value));
case IS_DOUBLE:
RETURN_DOUBLE(ceil(Z_DVAL_P(value)));
EMPTY_SWITCH_DEFAULT_CASE();
}
}
/* }}} */
Expand All @@ -301,12 +300,12 @@ PHP_FUNCTION(floor)
Z_PARAM_NUMBER(value)
ZEND_PARSE_PARAMETERS_END();

if (Z_TYPE_P(value) == IS_DOUBLE) {
RETURN_DOUBLE(floor(Z_DVAL_P(value)));
} else if (Z_TYPE_P(value) == IS_LONG) {
RETURN_DOUBLE(zval_get_double(value));
} else {
ZEND_ASSERT(0 && "Unexpected type");
switch (Z_TYPE_P(value)) {
case IS_LONG:
RETURN_DOUBLE(zval_get_double(value));
case IS_DOUBLE:
RETURN_DOUBLE(floor(Z_DVAL_P(value)));
EMPTY_SWITCH_DEFAULT_CASE();
}
}
/* }}} */
Expand All @@ -318,7 +317,6 @@ PHP_FUNCTION(round)
int places = 0;
zend_long precision = 0;
zend_long mode = PHP_ROUND_HALF_UP;
double return_val;

ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_NUMBER(value)
Expand Down Expand Up @@ -350,17 +348,14 @@ PHP_FUNCTION(round)
case IS_LONG:
/* Simple case - long that doesn't need to be rounded. */
if (places >= 0) {
RETURN_DOUBLE((double) Z_LVAL_P(value));
RETURN_DOUBLE(zval_get_double(value));
}
ZEND_FALLTHROUGH;

case IS_DOUBLE:
return_val = (Z_TYPE_P(value) == IS_LONG) ? (double)Z_LVAL_P(value) : Z_DVAL_P(value);
return_val = _php_math_round(return_val, (int)places, (int)mode);
RETURN_DOUBLE(return_val);
break;
RETURN_DOUBLE(_php_math_round(zval_get_double(value), (int)places, (int)mode));

EMPTY_SWITCH_DEFAULT_CASE()
EMPTY_SWITCH_DEFAULT_CASE();
}
}
/* }}} */
Expand Down