Skip to content

round: Abolished the CPU rounding mode change and modified it to a different logic. #13435

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 2 commits into from
Mar 8, 2024
Merged
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ static inline double php_round_helper(double integral, double value, double expo
* mode. For the specifics of the algorithm, see http://wiki.php.net/rfc/rounding
*/
PHPAPI double _php_math_round(double value, int places, int mode) {
double exponent;
double tmp_value;
double exponent, tmp_value, adjusted_value;
int cpu_round_mode;

if (!zend_finite(value) || value == 0.0) {
Expand All @@ -187,14 +186,23 @@ PHPAPI double _php_math_round(double value, int places, int mode) {
* e.g.
* 0.285 * 10000000000 => 2850000000.0
* floor(0.285 * 10000000000) => 2850000000
*
* Using `if` twice to prevent accidental optimization with llvm 15.0.0.
*/
bool val_is_positive_or_zero = value >= 0.0;
cpu_round_mode = fegetround();
if (value >= 0.0) {
if (val_is_positive_or_zero) {
fesetround(FE_UPWARD);
tmp_value = floor(places > 0 ? value * exponent : value / exponent);
} else {
fesetround(FE_DOWNWARD);
tmp_value = ceil(places > 0 ? value * exponent : value / exponent);
}

adjusted_value = places > 0 ? value * exponent : value / exponent;

if (val_is_positive_or_zero) {
tmp_value = floor(adjusted_value);
} else {
tmp_value = ceil(adjusted_value);
}
fesetround(cpu_round_mode);

Expand Down