Avoid extra inits while flooring or ceiling

This commit is contained in:
Niels Dossche 2024-05-01 21:23:39 +02:00
parent 34b2116eb5
commit 039344cf70
3 changed files with 14 additions and 19 deletions

View File

@ -611,20 +611,18 @@ PHP_FUNCTION(bccomp)
static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
{
zend_string *numstr;
bc_num num = NULL, result;
bc_num num = NULL, result = NULL;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(numstr)
ZEND_PARSE_PARAMETERS_END();
bc_init_num(&result);
if (php_str2num(&num, ZSTR_VAL(numstr)) == FAILURE) {
zend_argument_value_error(1, "is not well-formed");
goto cleanup;
}
bc_floor_or_ceil(num, is_floor, &result);
result = bc_floor_or_ceil(num, is_floor);
RETVAL_NEW_STR(bc_num2str_ex(result, 0));
cleanup: {

View File

@ -145,7 +145,7 @@ bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result);
bc_num bc_floor_or_ceil(bc_num num, bool is_floor);
void bc_round(bc_num num, zend_long places, zend_long mode, bc_num *result);

View File

@ -18,22 +18,19 @@
#include "private.h"
#include <stddef.h>
void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result)
bc_num bc_floor_or_ceil(bc_num num, bool is_floor)
{
/* clear result */
bc_free_num(result);
/* Initialize result */
*result = bc_new_num(num->n_len, 0);
(*result)->n_sign = num->n_sign;
bc_num result = bc_new_num(num->n_len, 0);
result->n_sign = num->n_sign;
/* copy integer part */
memcpy((*result)->n_value, num->n_value, num->n_len);
memcpy(result->n_value, num->n_value, num->n_len);
/* If the number is positive and we are flooring, then nothing else needs to be done.
* Similarly, if the number is negative and we are ceiling, then nothing else needs to be done. */
if (num->n_scale == 0 || (*result)->n_sign == (is_floor ? PLUS : MINUS)) {
return;
if (num->n_scale == 0 || result->n_sign == (is_floor ? PLUS : MINUS)) {
return result;
}
/* check fractional part. */
@ -46,12 +43,12 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result)
/* If all digits past the decimal point are 0 */
if (count == 0) {
return;
return result;
}
/* Increment the absolute value of the result by 1 and add sign information */
bc_num tmp = _bc_do_add(*result, BCG(_one_), 0);
tmp->n_sign = (*result)->n_sign;
bc_free_num(result);
*result = tmp;
bc_num tmp = _bc_do_add(result, BCG(_one_), 0);
tmp->n_sign = result->n_sign;
bc_free_num(&result);
return tmp;
}