Skip to content

Commit bbb21e4

Browse files
committed
Fixed to avoid incorrect optimization with llvm15.0.0
1 parent b558a18 commit bbb21e4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

ext/standard/math.c

Lines changed: 11 additions & 4 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,22 @@ 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
*/
191192
cpu_round_mode = fegetround();
192193
if (value >= 0.0) {
193194
fesetround(FE_UPWARD);
194-
tmp_value = floor(places > 0 ? value * exponent : value / exponent);
195195
} else {
196196
fesetround(FE_DOWNWARD);
197-
tmp_value = ceil(places > 0 ? value * exponent : value / exponent);
197+
}
198+
199+
adjusted_value = places > 0 ? value * exponent : value / exponent;
200+
201+
if (value >= 0.0) {
202+
tmp_value = floor(adjusted_value);
203+
} else {
204+
tmp_value = ceil(adjusted_value);
198205
}
199206
fesetround(cpu_round_mode);
200207

0 commit comments

Comments
 (0)