Skip to content

ext/bcmath: Performance improvement bcsqrt() #18771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Optimized and changed the initialization process to run a little later.
  • Loading branch information
SakiTakamachi committed Jun 11, 2025
commit 0b34dfdaa5eb7e97ecc793c4fb154a5874ac4322
13 changes: 6 additions & 7 deletions ext/bcmath/libbcmath/src/sqrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,9 @@ bool bc_sqrt(bc_num *num, size_t scale)

/* Initialize the variables. */
size_t cscale;
bc_num guess, guess1, point5, diff;
bc_num guess;
size_t rscale = MAX(scale, (*num)->n_scale);

bc_init_num(&guess1);
bc_init_num(&diff);
point5 = bc_new_num (1, 1);
point5->n_value[1] = 5;


/* Calculate the initial guess. */
if (num_cmp_one == BCMATH_RIGHT_GREATER) {
/* The number is between 0 and 1. Guess should start at 1. */
Expand All @@ -87,6 +81,11 @@ bool bc_sqrt(bc_num *num, size_t scale)
cscale = 3;
}

bc_num guess1 = NULL;
bc_num point5 = bc_new_num (1, 1);
point5->n_value[1] = 5;
bc_num diff = NULL;

/* Find the square root using Newton's algorithm. */
bool done = false;
while (!done) {
Expand Down