From 0cd952d851b10b0b26055d3071902e053f8a1237 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Wed, 1 May 2024 21:10:43 +0200 Subject: [PATCH] Avoid unnecessary destruction in bc_sub() --- ext/bcmath/bcmath.c | 6 ++---- ext/bcmath/libbcmath/src/bcmath.h | 8 +++++++- ext/bcmath/libbcmath/src/divmod.c | 2 +- ext/bcmath/libbcmath/src/output.c | 7 +++---- ext/bcmath/libbcmath/src/recmul.c | 8 +++----- ext/bcmath/libbcmath/src/sqrt.c | 2 +- ext/bcmath/libbcmath/src/sub.c | 6 ++---- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 874a430559d..d6b8ae4d59e 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -195,7 +195,7 @@ PHP_FUNCTION(bcsub) zend_string *left, *right; zend_long scale_param; bool scale_param_is_null = 1; - bc_num first = NULL, second = NULL, result; + bc_num first = NULL, second = NULL, result = NULL; int scale; ZEND_PARSE_PARAMETERS_START(2, 3) @@ -214,8 +214,6 @@ PHP_FUNCTION(bcsub) scale = (int) scale_param; } - bc_init_num(&result); - if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) { zend_argument_value_error(1, "is not well-formed"); goto cleanup; @@ -226,7 +224,7 @@ PHP_FUNCTION(bcsub) goto cleanup; } - bc_sub (first, second, &result, scale); + result = bc_sub (first, second, scale); RETVAL_NEW_STR(bc_num2str_ex(result, scale)); diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h index d046fdb7b9e..83e867546cc 100644 --- a/ext/bcmath/libbcmath/src/bcmath.h +++ b/ext/bcmath/libbcmath/src/bcmath.h @@ -123,7 +123,13 @@ bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min); *(result) = add_ex; \ } while (0) -void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min); +bc_num bc_sub(bc_num n1, bc_num n2, size_t scale_min); + +#define bc_sub_ex(n1, n2, result, scale_min) do { \ + bc_num sub_ex = bc_sub(n1, n2, scale_min); \ + bc_free_num (result); \ + *(result) = sub_ex; \ +} while (0) void bc_multiply(bc_num n1, bc_num n2, bc_num *prod, size_t scale); diff --git a/ext/bcmath/libbcmath/src/divmod.c b/ext/bcmath/libbcmath/src/divmod.c index d4f9d4d38cf..3dab0104c87 100644 --- a/ext/bcmath/libbcmath/src/divmod.c +++ b/ext/bcmath/libbcmath/src/divmod.c @@ -62,7 +62,7 @@ bool bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, size_t scale quotient = bc_copy_num(temp); } bc_multiply(temp, num2, &temp, rscale); - bc_sub(num1, temp, rem, rscale); + bc_sub_ex(num1, temp, rem, rscale); bc_free_num (&temp); if (quot) { diff --git a/ext/bcmath/libbcmath/src/output.c b/ext/bcmath/libbcmath/src/output.c index 47a82d20655..699e2ab3b6a 100644 --- a/ext/bcmath/libbcmath/src/output.c +++ b/ext/bcmath/libbcmath/src/output.c @@ -79,7 +79,7 @@ void bc_out_num(bc_num num, int o_base, void (*out_char)(char), bool leading_zer int index, fdigit; bool pre_space; stk_rec *digits, *temp; - bc_num int_part, frac_part, base, cur_dig, t_num, max_o_digit; + bc_num int_part, base, cur_dig, t_num, max_o_digit; /* The negative sign if needed. */ if (num->n_sign == MINUS) (*out_char)('-'); @@ -120,10 +120,9 @@ void bc_out_num(bc_num num, int o_base, void (*out_char)(char), bool leading_zer digits = NULL; bc_init_num(&int_part); bc_divide(num, BCG(_one_), &int_part, 0); - bc_init_num(&frac_part); bc_init_num(&cur_dig); bc_init_num(&base); - bc_sub(num, int_part, &frac_part, 0); + bc_num frac_part = bc_sub(num, int_part, 0); /* Make the INT_PART and FRAC_PART positive. */ int_part->n_sign = PLUS; frac_part->n_sign = PLUS; @@ -166,7 +165,7 @@ void bc_out_num(bc_num num, int o_base, void (*out_char)(char), bool leading_zer bc_multiply(frac_part, base, &frac_part, num->n_scale); fdigit = bc_num2long(frac_part); bc_int2num(&int_part, fdigit); - bc_sub(frac_part, int_part, &frac_part, 0); + bc_sub_ex(frac_part, int_part, &frac_part, 0); if (o_base <= 16) { (*out_char)(ref_str[fdigit]); } else { diff --git a/ext/bcmath/libbcmath/src/recmul.c b/ext/bcmath/libbcmath/src/recmul.c index fe6a34817d9..ea4c64300f0 100644 --- a/ext/bcmath/libbcmath/src/recmul.c +++ b/ext/bcmath/libbcmath/src/recmul.c @@ -163,7 +163,7 @@ static void _bc_shift_addsub(bc_num accum, bc_num val, int shift, bool sub) static void _bc_rec_mul(bc_num u, size_t ulen, bc_num v, size_t vlen, bc_num *prod) { bc_num u0, u1, v0, v1; - bc_num m1, m2, m3, d1, d2; + bc_num m1, m2, m3; size_t n; bool m1zero; @@ -203,10 +203,8 @@ static void _bc_rec_mul(bc_num u, size_t ulen, bc_num v, size_t vlen, bc_num *pr /* Calculate sub results ... */ - bc_init_num(&d1); - bc_init_num(&d2); - bc_sub(u1, u0, &d1, 0); - bc_sub(v0, v1, &d2, 0); + bc_num d1 = bc_sub(u1, u0, 0); + bc_num d2 = bc_sub(v0, v1, 0); /* Do recursive multiplies and shifted adds. */ diff --git a/ext/bcmath/libbcmath/src/sqrt.c b/ext/bcmath/libbcmath/src/sqrt.c index 8c88b0baeef..6be595deb4f 100644 --- a/ext/bcmath/libbcmath/src/sqrt.c +++ b/ext/bcmath/libbcmath/src/sqrt.c @@ -94,7 +94,7 @@ bool bc_sqrt(bc_num *num, size_t scale) bc_divide(*num, guess, &guess, cscale); bc_add_ex(guess, guess1, &guess, 0); bc_multiply(guess, point5, &guess, cscale); - bc_sub(guess, guess1, &diff, cscale + 1); + bc_sub_ex(guess, guess1, &diff, cscale + 1); if (bc_is_near_zero(diff, cscale)) { if (cscale < rscale + 1) { cscale = MIN (cscale * 3, rscale + 1); diff --git a/ext/bcmath/libbcmath/src/sub.c b/ext/bcmath/libbcmath/src/sub.c index d90f1a8858f..185f9651dea 100644 --- a/ext/bcmath/libbcmath/src/sub.c +++ b/ext/bcmath/libbcmath/src/sub.c @@ -39,7 +39,7 @@ N2 is subtracted from N1 and the result placed in RESULT. SCALE_MIN is the minimum scale for the result. */ -void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min) +bc_num bc_sub(bc_num n1, bc_num n2, size_t scale_min) { bc_num diff = NULL; @@ -70,7 +70,5 @@ void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min) } } - /* Clean up and return. */ - bc_free_num (result); - *result = diff; + return diff; }