Skip to content

Commit 039344c

Browse files
committed
Avoid extra inits while flooring or ceiling
1 parent 34b2116 commit 039344c

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

ext/bcmath/bcmath.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,20 +611,18 @@ PHP_FUNCTION(bccomp)
611611
static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
612612
{
613613
zend_string *numstr;
614-
bc_num num = NULL, result;
614+
bc_num num = NULL, result = NULL;
615615

616616
ZEND_PARSE_PARAMETERS_START(1, 1)
617617
Z_PARAM_STR(numstr)
618618
ZEND_PARSE_PARAMETERS_END();
619619

620-
bc_init_num(&result);
621-
622620
if (php_str2num(&num, ZSTR_VAL(numstr)) == FAILURE) {
623621
zend_argument_value_error(1, "is not well-formed");
624622
goto cleanup;
625623
}
626624

627-
bc_floor_or_ceil(num, is_floor, &result);
625+
result = bc_floor_or_ceil(num, is_floor);
628626
RETVAL_NEW_STR(bc_num2str_ex(result, 0));
629627

630628
cleanup: {

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
145145

146146
bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
147147

148-
void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result);
148+
bc_num bc_floor_or_ceil(bc_num num, bool is_floor);
149149

150150
void bc_round(bc_num num, zend_long places, zend_long mode, bc_num *result);
151151

ext/bcmath/libbcmath/src/floor_or_ceil.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@
1818
#include "private.h"
1919
#include <stddef.h>
2020

21-
void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result)
21+
bc_num bc_floor_or_ceil(bc_num num, bool is_floor)
2222
{
23-
/* clear result */
24-
bc_free_num(result);
25-
2623
/* Initialize result */
27-
*result = bc_new_num(num->n_len, 0);
28-
(*result)->n_sign = num->n_sign;
24+
bc_num result = bc_new_num(num->n_len, 0);
25+
result->n_sign = num->n_sign;
2926

3027
/* copy integer part */
31-
memcpy((*result)->n_value, num->n_value, num->n_len);
28+
memcpy(result->n_value, num->n_value, num->n_len);
3229

3330
/* If the number is positive and we are flooring, then nothing else needs to be done.
3431
* Similarly, if the number is negative and we are ceiling, then nothing else needs to be done. */
35-
if (num->n_scale == 0 || (*result)->n_sign == (is_floor ? PLUS : MINUS)) {
36-
return;
32+
if (num->n_scale == 0 || result->n_sign == (is_floor ? PLUS : MINUS)) {
33+
return result;
3734
}
3835

3936
/* check fractional part. */
@@ -46,12 +43,12 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result)
4643

4744
/* If all digits past the decimal point are 0 */
4845
if (count == 0) {
49-
return;
46+
return result;
5047
}
5148

5249
/* Increment the absolute value of the result by 1 and add sign information */
53-
bc_num tmp = _bc_do_add(*result, BCG(_one_), 0);
54-
tmp->n_sign = (*result)->n_sign;
55-
bc_free_num(result);
56-
*result = tmp;
50+
bc_num tmp = _bc_do_add(result, BCG(_one_), 0);
51+
tmp->n_sign = result->n_sign;
52+
bc_free_num(&result);
53+
return tmp;
5754
}

0 commit comments

Comments
 (0)