Skip to content

Commit ee3fa02

Browse files
committed
The calculation process of BC_VECTOR was separated inline as bc_standard_vector_mul.
1 parent 63c3784 commit ee3fa02

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

Diff for: ext/bcmath/libbcmath/src/recmul.c

+34-28
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,38 @@ static inline void bc_fast_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len,
7272
}
7373
}
7474

75+
static inline void bc_standard_vector_mul(
76+
BC_VECTOR *n1_vector, size_t n1_arr_size, BC_VECTOR *n2_vector, size_t n2_arr_size, BC_VECTOR *prod_vector, size_t prod_arr_size)
77+
{
78+
for (size_t i = 0; i < prod_arr_size; i++) {
79+
prod_vector[i] = 0;
80+
}
81+
82+
/* Multiplication and addition */
83+
size_t count = 0;
84+
for (size_t i = 0; i < n1_arr_size; i++) {
85+
/*
86+
* This calculation adds the result multiple times to the array entries.
87+
* When multiplying large numbers of digits, there is a possibility of
88+
* overflow, so digit adjustment is performed beforehand.
89+
*/
90+
if (UNEXPECTED(count >= BC_VECTOR_NO_OVERFLOW_ADD_COUNT)) {
91+
bc_mul_carry_calc(prod_vector, prod_arr_size);
92+
count = 0;
93+
}
94+
count++;
95+
for (size_t j = 0; j < n2_arr_size; j++) {
96+
prod_vector[i + j] += n1_vector[i] * n2_vector[j];
97+
}
98+
}
99+
100+
/*
101+
* Move a value exceeding 4/8 digits by carrying to the next digit.
102+
* However, the last digit does nothing.
103+
*/
104+
bc_mul_carry_calc(prod_vector, prod_arr_size);
105+
}
106+
75107
/*
76108
* Converts the BCD of bc_num by 4 (32 bits) or 8 (64 bits) digits to an array of BC_VECTOR.
77109
* The array is generated starting with the smaller digits.
@@ -82,7 +114,6 @@ static inline void bc_fast_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len,
82114
*/
83115
static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc_num *prod)
84116
{
85-
size_t i;
86117
const char *n1end = n1->n_value + n1len - 1;
87118
const char *n2end = n2->n_value + n2len - 1;
88119
size_t prodlen = n1len + n2len;
@@ -108,37 +139,12 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
108139
BC_VECTOR *n2_vector = n1_vector + n1_arr_size;
109140
BC_VECTOR *prod_vector = n2_vector + n2_arr_size;
110141

111-
for (i = 0; i < prod_arr_size; i++) {
112-
prod_vector[i] = 0;
113-
}
114-
115142
/* Convert to BC_VECTOR[] */
116143
bc_convert_to_vector(n1_vector, n1end, n1len);
117144
bc_convert_to_vector(n2_vector, n2end, n2len);
118145

119-
/* Multiplication and addition */
120-
size_t count = 0;
121-
for (i = 0; i < n1_arr_size; i++) {
122-
/*
123-
* This calculation adds the result multiple times to the array entries.
124-
* When multiplying large numbers of digits, there is a possibility of
125-
* overflow, so digit adjustment is performed beforehand.
126-
*/
127-
if (UNEXPECTED(count >= BC_VECTOR_NO_OVERFLOW_ADD_COUNT)) {
128-
bc_mul_carry_calc(prod_vector, prod_arr_size);
129-
count = 0;
130-
}
131-
count++;
132-
for (size_t j = 0; j < n2_arr_size; j++) {
133-
prod_vector[i + j] += n1_vector[i] * n2_vector[j];
134-
}
135-
}
136-
137-
/*
138-
* Move a value exceeding 4/8 digits by carrying to the next digit.
139-
* However, the last digit does nothing.
140-
*/
141-
bc_mul_carry_calc(prod_vector, prod_arr_size);
146+
/* Do multiply */
147+
bc_standard_vector_mul(n1_vector, n1_arr_size, n2_vector, n2_arr_size, prod_vector, prod_arr_size);
142148

143149
/* Convert to bc_num */
144150
*prod = bc_new_num_nonzeroed(prodlen, 0);

0 commit comments

Comments
 (0)