Skip to content

Commit d830c53

Browse files
committed
Optimized standard path calculation
1 parent 8a5da50 commit d830c53

File tree

3 files changed

+140
-42
lines changed

3 files changed

+140
-42
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,19 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
429429
*quot = bc_copy_num(BCG(_zero_));
430430
return true;
431431
}
432+
433+
void bc_divide_vector(
434+
BC_VECTOR *numerator_vectors, size_t numerator_arr_size,
435+
const BC_VECTOR *divisor_vectors, size_t divisor_arr_size, size_t divisor_size,
436+
BC_VECTOR *quot_vectors, size_t quot_arr_size
437+
) {
438+
ZEND_ASSERT(divisor_vectors[divisor_arr_size - 1] != 0);
439+
ZEND_ASSERT(quot_arr_size == numerator_arr_size - divisor_arr_size + 1);
440+
441+
/* Do the division */
442+
if (divisor_arr_size == 1) {
443+
bc_fast_div(numerator_vectors, numerator_arr_size, divisor_vectors[0], quot_vectors, quot_arr_size);
444+
} else {
445+
bc_standard_div(numerator_vectors, numerator_arr_size, divisor_vectors, divisor_arr_size, divisor_size, quot_vectors, quot_arr_size);
446+
}
447+
}

ext/bcmath/libbcmath/src/private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ bc_num _bc_do_sub (bc_num n1, bc_num n2);
8787
void bc_multiply_vector(
8888
const BC_VECTOR *n1_vector, size_t n1_arr_size, const BC_VECTOR *n2_vector, size_t n2_arr_size,
8989
BC_VECTOR *prod_vector, size_t prod_arr_size);
90+
void bc_divide_vector(
91+
BC_VECTOR *numerator_vectors, size_t numerator_arr_size,
92+
const BC_VECTOR *divisor_vectors, size_t divisor_arr_size, size_t divisor_size,
93+
BC_VECTOR *quot_vectors, size_t quot_arr_size);
9094
void _bc_rm_leading_zeros (bc_num num);
9195

9296
#endif

ext/bcmath/libbcmath/src/sqrt.c

Lines changed: 120 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*************************************************************************/
3131

3232
#include "bcmath.h"
33+
#include "convert.h"
3334
#include <stdbool.h>
3435
#include <stddef.h>
3536
#include "private.h"
@@ -100,57 +101,135 @@ static inline void bc_fast_sqrt(bc_num *num, size_t rscale, size_t num_calc_full
100101
*num = ret;
101102
}
102103

103-
static inline void bc_standard_sqrt(bc_num *num, size_t rscale, bcmath_compare_result num_cmp_one)
104+
static inline void bc_standard_sqrt(bc_num *num, size_t rscale, size_t num_calc_full_len, size_t leading_zeros)
104105
{
105-
bc_num guess;
106-
size_t cscale;
107-
/* Calculate the initial guess. */
108-
if (num_cmp_one == BCMATH_RIGHT_GREATER) {
109-
/* The number is between 0 and 1. Guess should start at 1. */
110-
guess = bc_copy_num(BCG(_one_));
111-
cscale = (*num)->n_scale;
106+
/* allocate memory */
107+
size_t n_arr_size = BC_ARR_SIZE_FROM_LEN(num_calc_full_len);
108+
109+
size_t guess_full_len = (num_calc_full_len + 1) / 2;
110+
/* Since add the old guess and the new guess together during the calculation,
111+
* there is a chance of overflow, so allocate an extra size. */
112+
size_t guess_arr_size = BC_ARR_SIZE_FROM_LEN(guess_full_len) + 1;
113+
114+
size_t allocate_size = n_arr_size * 2 + guess_arr_size * 3;
115+
BC_VECTOR *buf = safe_emalloc(allocate_size, sizeof(BC_VECTOR), 0);
116+
117+
BC_VECTOR *n_vector = buf;
118+
/* In division by successive approximation, the numerator is modified during the computation,
119+
* so it must be copied each time. */
120+
BC_VECTOR *n_vector_copy = n_vector + n_arr_size;
121+
BC_VECTOR *guess_vector = n_vector_copy + n_arr_size;
122+
BC_VECTOR *guess1_vector = guess_vector + guess_arr_size;
123+
BC_VECTOR *tmp_div_ret_vector = guess1_vector + guess_arr_size;
124+
125+
/* convert num to n_vector */
126+
size_t n_full_len = (*num)->n_len + (*num)->n_scale - leading_zeros;
127+
const char *nend = (*num)->n_value + (*num)->n_len + (*num)->n_scale - 1;
128+
size_t n_extend_zeros = num_calc_full_len - n_full_len;
129+
130+
bc_convert_to_vector_with_zero_pad(n_vector, nend, n_full_len, n_extend_zeros);
131+
132+
/* Prepare guess_vector (Temporary implementation) */
133+
for (size_t i = 0; i < guess_arr_size - 2; i++) {
134+
guess_vector[i] = BC_VECTOR_BOUNDARY_NUM - 1;
135+
}
136+
if (guess_full_len % BC_VECTOR_SIZE == 0) {
137+
guess_vector[guess_arr_size - 2] = BC_VECTOR_BOUNDARY_NUM - 1;
112138
} else {
113-
/* The number is greater than 1. Guess should start at 10^(exp/2). */
114-
bc_init_num(&guess);
115-
bc_int2num(&guess, 10);
116-
117-
bc_int2num(&guess1, (*num)->n_len);
118-
bc_multiply_ex(guess1, point5, &guess1, 0);
119-
guess1->n_scale = 0;
120-
bc_raise_bc_exponent(guess, guess1, &guess, 0);
121-
bc_free_num (&guess1);
122-
cscale = 3;
139+
guess_vector[guess_arr_size - 2] = 0;
140+
for (size_t i = 0; i < guess_full_len % BC_VECTOR_SIZE; i++) {
141+
guess_vector[guess_arr_size - 2] *= BASE;
142+
guess_vector[guess_arr_size - 2] += 9;
143+
}
123144
}
145+
guess_vector[guess_arr_size - 1] = 0;
146+
147+
size_t quot_size = n_arr_size - (guess_arr_size - 1) + 1;
124148

125-
bc_num guess1 = NULL;
126-
bc_num point5 = bc_new_num (1, 1);
127-
point5->n_value[1] = 5;
128-
bc_num diff = NULL;
149+
BC_VECTOR two[1] = { 2 };
129150

151+
/**
152+
* Newton's algorithm. Iterative expression is `x_{n+1} = (x_n + a / x_n) / 2`
153+
* If break down the calculation into detailed steps, it looks like this:
154+
* 1. quot = a / x_n
155+
* 2. add = x_n + quot1
156+
* 3. x_{n+1} = add / 2
157+
* 4. repeat until the difference between the `x_n` and `x_{n+1}` is less than or equal to 1.
158+
*/
130159
bool done = false;
131-
while (!done) {
132-
bc_free_num (&guess1);
133-
guess1 = bc_copy_num(guess);
134-
bc_divide(*num, guess, &guess, cscale);
135-
bc_add_ex(guess, guess1, &guess, 0);
136-
bc_multiply_ex(guess, point5, &guess, cscale);
137-
bc_sub_ex(guess, guess1, &diff, cscale + 1);
138-
if (bc_is_near_zero(diff, cscale)) {
139-
if (cscale < rscale + 1) {
140-
cscale = MIN (cscale * 3, rscale + 1);
160+
do {
161+
/* Since the value changes during division by successive approximation, use a copied version of it. */
162+
memcpy(n_vector_copy, n_vector, n_arr_size * sizeof(BC_VECTOR));
163+
164+
/* 1. quot = a / x_n */
165+
bc_divide_vector(
166+
n_vector_copy, n_arr_size,
167+
guess_vector, guess_arr_size - 1, guess_full_len,
168+
tmp_div_ret_vector, quot_size
169+
);
170+
171+
BC_VECTOR *tmp_vptr = guess1_vector;
172+
guess1_vector = guess_vector;
173+
guess_vector = tmp_vptr;
174+
175+
/* 2. add = x_n + quot1 */
176+
int carry = 0;
177+
for (size_t i = 0; i < guess_arr_size - 1; i++) {
178+
guess_vector[i] = guess1_vector[i] + tmp_div_ret_vector[i] + carry;
179+
if (guess_vector[i] >= BC_VECTOR_BOUNDARY_NUM) {
180+
guess_vector[i] -= BC_VECTOR_BOUNDARY_NUM;
181+
carry = 1;
141182
} else {
142-
done = true;
183+
carry = 0;
143184
}
144185
}
186+
guess_vector[guess_arr_size - 1] = tmp_div_ret_vector[guess_arr_size - 1] + carry;
187+
188+
/* 3. x_{n+1} = add / 2 */
189+
bc_divide_vector(
190+
guess_vector, guess_arr_size,
191+
two, 1, 1,
192+
tmp_div_ret_vector, guess_arr_size
193+
);
194+
195+
memcpy(guess_vector, tmp_div_ret_vector, guess_arr_size * sizeof(BC_VECTOR));
196+
197+
/* 4. repeat until the difference between the `x_n` and `x_{n+1}` is less than or equal to 1. */
198+
size_t diff = guess_vector[0] > guess1_vector[0] ? guess_vector[0] - guess1_vector[0] : guess1_vector[0] - guess_vector[0];
199+
if (diff <= 1) {
200+
bool is_same = true;
201+
for (size_t i = 1; i < guess_arr_size - 1; i++) {
202+
if (guess_vector[i] != guess1_vector[i]) {
203+
is_same = false;
204+
break;
205+
}
206+
}
207+
done = is_same;
208+
}
209+
} while (!done);
210+
211+
size_t guess_len;
212+
size_t guess_leading_zeros = 0;
213+
if (leading_zeros > 0) {
214+
guess_len = 1; /* for int zero */
215+
guess_leading_zeros = (leading_zeros + 1) / 2;
216+
} else {
217+
guess_len = ((*num)->n_len + 1) / 2;
145218
}
219+
bc_num ret = bc_new_num_nonzeroed(guess_len, rscale + 1);
220+
char *rptr = ret->n_value;
221+
char *rend = rptr + guess_len + rscale + 1 - 1;
222+
223+
for (size_t i = 0; i < guess_leading_zeros; i++) {
224+
*rptr++ = 0;
225+
}
226+
bc_convert_vector_to_char(guess_vector, rptr, rend, guess_arr_size - 1);
227+
ret->n_scale = rscale;
146228

147-
/* Assign the number and clean up. */
148-
bc_free_num (num);
149-
bc_divide(guess, BCG(_one_), num, rscale);
150-
bc_free_num (&guess);
151-
bc_free_num (&guess1);
152-
bc_free_num (&point5);
153-
bc_free_num (&diff);
229+
bc_free_num(num);
230+
*num = ret;
231+
232+
efree(buf);
154233
}
155234

156235
bool bc_sqrt(bc_num *num, size_t scale)
@@ -196,8 +275,7 @@ bool bc_sqrt(bc_num *num, size_t scale)
196275
if (num_calc_full_len < MAX_LENGTH_OF_LONG) {
197276
bc_fast_sqrt(num, rscale, num_calc_full_len, leading_zeros);
198277
} else {
199-
bc_standard_sqrt(num, rscale, num_cmp_one);
278+
bc_standard_sqrt(num, rscale, num_calc_full_len, leading_zeros);
200279
}
201-
202280
return true;
203281
}

0 commit comments

Comments
 (0)