@@ -149,15 +149,21 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
149
149
size_t n2_arr_size = (n2len + BC_VECTOR_SIZE - 1 ) / BC_VECTOR_SIZE ;
150
150
size_t prod_arr_size = (prodlen + BC_VECTOR_SIZE - 1 ) / BC_VECTOR_SIZE ;
151
151
152
- /*
153
- * let's say that N is the max of n1len and n2len (and a multiple of BC_VECTOR_SIZE for simplicity),
154
- * then this sum is <= N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE - 1
155
- * which is equal to N - 1 if BC_VECTOR_SIZE is 4, and N/2 - 1 if BC_VECTOR_SIZE is 8.
156
- */
157
- BC_VECTOR * buf = safe_emalloc (n1_arr_size + n2_arr_size + prod_arr_size , sizeof (BC_VECTOR ), 0 );
152
+ BC_VECTOR stack_vectors [BC_STACK_VECTOR_SIZE ];
153
+ size_t allocation_arr_size = n1_arr_size + n2_arr_size + prod_arr_size ;
158
154
159
- BC_VECTOR * n1_vector = buf ;
160
- BC_VECTOR * n2_vector = buf + n1_arr_size ;
155
+ BC_VECTOR * n1_vector ;
156
+ if (allocation_arr_size <= BC_STACK_VECTOR_SIZE ) {
157
+ n1_vector = stack_vectors ;
158
+ } else {
159
+ /*
160
+ * let's say that N is the max of n1len and n2len (and a multiple of BC_VECTOR_SIZE for simplicity),
161
+ * then this sum is <= N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE + N/BC_VECTOR_SIZE - 1
162
+ * which is equal to N - 1 if BC_VECTOR_SIZE is 4, and N/2 - 1 if BC_VECTOR_SIZE is 8.
163
+ */
164
+ n1_vector = safe_emalloc (allocation_arr_size , sizeof (BC_VECTOR ), 0 );
165
+ }
166
+ BC_VECTOR * n2_vector = n1_vector + n1_arr_size ;
161
167
BC_VECTOR * prod_vector = n2_vector + n2_arr_size ;
162
168
163
169
for (i = 0 ; i < prod_arr_size ; i ++ ) {
@@ -188,7 +194,9 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
188
194
189
195
bc_mul_finish_from_vector (prod_vector , prod_arr_size , prodlen , prod );
190
196
191
- efree (buf );
197
+ if (allocation_arr_size > BC_STACK_VECTOR_SIZE ) {
198
+ efree (n1_vector );
199
+ }
192
200
}
193
201
194
202
/** This is bc_standard_mul implementation for square */
0 commit comments