@@ -149,15 +149,21 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, size_t n2len, bc
149149 size_t n2_arr_size = (n2len + BC_VECTOR_SIZE - 1 ) / BC_VECTOR_SIZE ;
150150 size_t prod_arr_size = (prodlen + BC_VECTOR_SIZE - 1 ) / BC_VECTOR_SIZE ;
151151
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 ;
158154
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 ;
161167 BC_VECTOR * prod_vector = n2_vector + n2_arr_size ;
162168
163169 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
188194
189195 bc_mul_finish_from_vector (prod_vector , prod_arr_size , prodlen , prod );
190196
191- efree (buf );
197+ if (allocation_arr_size > BC_STACK_VECTOR_SIZE ) {
198+ efree (n1_vector );
199+ }
192200}
193201
194202/** This is bc_standard_mul implementation for square */
0 commit comments