Skip to content

Don't evaluate GMP comparison multiple times #18321

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
Apr 14, 2025
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
6 changes: 4 additions & 2 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ static int gmp_compare(zval *op1, zval *op2) /* {{{ */
return ZEND_UNCOMPARABLE;
}

return ZEND_THREEWAY_COMPARE(mpz_cmp(gmp_op1, gmp_op2), 0);
int ret = mpz_cmp(gmp_op1, gmp_op2); /* avoid multiple evaluations */
return ZEND_THREEWAY_COMPARE(ret, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

is it somehow possible to put this into the macro itself?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not without relying on non-portable GCC extensions.

}
/* }}} */

Expand Down Expand Up @@ -1422,7 +1423,8 @@ ZEND_FUNCTION(gmp_cmp)
GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_b)
ZEND_PARSE_PARAMETERS_END();

RETURN_LONG(ZEND_THREEWAY_COMPARE(mpz_cmp(gmpnum_a, gmpnum_b), 0));
int ret = mpz_cmp(gmpnum_a, gmpnum_b); /* avoid multiple evaluations */
RETURN_LONG(ZEND_THREEWAY_COMPARE(ret, 0));
}
/* }}} */

Expand Down
Loading