Skip to content

Commit 16d0394

Browse files
committed
Fixed to avoid incorrect optimization with llvm15.0.0
1 parent 700fbca commit 16d0394

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

ext/standard/math.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ static inline double php_round_helper(double integral, double value, double expo
163163
* mode. For the specifics of the algorithm, see http://wiki.php.net/rfc/rounding
164164
*/
165165
PHPAPI double _php_math_round(double value, int places, int mode) {
166-
double exponent;
167-
double tmp_value;
166+
double exponent, tmp_value, adjusted_value;
168167
int cpu_round_mode;
169168

170169
if (!zend_finite(value) || value == 0.0) {
@@ -187,14 +186,23 @@ PHPAPI double _php_math_round(double value, int places, int mode) {
187186
* e.g.
188187
* 0.285 * 10000000000 => 2850000000.0
189188
* floor(0.285 * 10000000000) => 2850000000
189+
*
190+
* Using `if` twice to prevent accidental optimization with llvm 15.0.0.
190191
*/
192+
bool val_is_positive_or_zero = value >= 0.0;
191193
cpu_round_mode = fegetround();
192-
if (value >= 0.0) {
194+
if (val_is_positive_or_zero) {
193195
fesetround(FE_UPWARD);
194-
tmp_value = floor(places > 0 ? value * exponent : value / exponent);
195196
} else {
196197
fesetround(FE_DOWNWARD);
197-
tmp_value = ceil(places > 0 ? value * exponent : value / exponent);
198+
}
199+
200+
adjusted_value = places > 0 ? value * exponent : value / exponent;
201+
202+
if (val_is_positive_or_zero) {
203+
tmp_value = floor(adjusted_value);
204+
} else {
205+
tmp_value = ceil(adjusted_value);
198206
}
199207
fesetround(cpu_round_mode);
200208

0 commit comments

Comments
 (0)