Skip to content

Commit 37488d6

Browse files
ext/bcmath: Minor fixes to bcmul() (#14564)
The original calculation method for prod_arr_size allowed for some error, which could have increased the number of simple loops without byte tricks at the end of the calculation when converting to bc_num. The new method calculates the size accurately, so the number of loops does not increase unnecessarily.
1 parent 460b851 commit 37488d6

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

ext/bcmath/libbcmath/src/recmul.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
/* Multiply utility routines */
5858

59-
static inline void bc_digits_adjustment(BC_VECTOR *prod_vector, size_t prod_arr_size)
59+
static inline void bc_mul_carry_calc(BC_VECTOR *prod_vector, size_t prod_arr_size)
6060
{
6161
for (size_t i = 0; i < prod_arr_size - 1; i++) {
6262
prod_vector[i + 1] += prod_vector[i] / BC_VECTOR_BOUNDARY_NUM;
@@ -237,7 +237,7 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
237237

238238
size_t n1_arr_size = (n1len + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
239239
size_t n2_arr_size = (n2len + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
240-
size_t prod_arr_size = n1_arr_size + n2_arr_size - 1;
240+
size_t prod_arr_size = (prodlen + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
241241

242242
/*
243243
* let's say that N is the max of n1len and n2len (and a multiple of BC_VECTOR_SIZE for simplicity),
@@ -254,7 +254,7 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
254254
prod_vector[i] = 0;
255255
}
256256

257-
/* Convert to uint[] */
257+
/* Convert to BC_VECTOR[] */
258258
bc_convert_to_vector(n1_vector, n1end, n1len);
259259
bc_convert_to_vector(n2_vector, n2end, n2len);
260260

@@ -267,7 +267,7 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
267267
* overflow, so digit adjustment is performed beforehand.
268268
*/
269269
if (UNEXPECTED(count >= BC_VECTOR_NO_OVERFLOW_ADD_COUNT)) {
270-
bc_digits_adjustment(prod_vector, prod_arr_size);
270+
bc_mul_carry_calc(prod_vector, prod_arr_size);
271271
count = 0;
272272
}
273273
count++;
@@ -280,7 +280,7 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
280280
* Move a value exceeding 4/8 digits by carrying to the next digit.
281281
* However, the last digit does nothing.
282282
*/
283-
bc_digits_adjustment(prod_vector, prod_arr_size);
283+
bc_mul_carry_calc(prod_vector, prod_arr_size);
284284

285285
/* Convert to bc_num */
286286
*prod = bc_new_num_nonzeroed(prodlen, 0);

0 commit comments

Comments
 (0)