Skip to content

ext/bcmath: Simplify bc_divide() code #17987

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

Merged
merged 10 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.5.0alpha1

- BCMath:
. Simplify `bc_divide()` code. (SakiTakamachi)

- CLI:
. Add --ini=diff to print INI settings changed from the builtin default.
(timwolla)
Expand Down
34 changes: 31 additions & 3 deletions ext/bcmath/libbcmath/src/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ static inline BC_VECTOR bc_partial_convert_to_vector(const char *n, size_t len)
}

BC_VECTOR num = 0;
BC_VECTOR base = 1;
BC_VECTOR digit_base_value = 1;

for (size_t i = 0; i < len; i++) {
num += *n * base;
base *= BASE;
num += *n * digit_base_value;
digit_base_value *= BASE;
n--;
}

Expand All @@ -57,4 +57,32 @@ static inline void bc_convert_to_vector(BC_VECTOR *n_vector, const char *nend, s
}
}

static inline void bc_convert_to_vector_with_zero_pad(BC_VECTOR *n_vector, const char *nend, size_t nlen, size_t zeros)
{
while (zeros >= BC_VECTOR_SIZE) {
*n_vector = 0;
n_vector++;
zeros -= BC_VECTOR_SIZE;
}

if (zeros > 0) {
*n_vector = 0;
BC_VECTOR digit_base_value = BC_POW_10_LUT[zeros];
size_t len_to_write = MIN(BC_VECTOR_SIZE - zeros, nlen);
for (size_t i = 0; i < len_to_write; i++) {
*n_vector += *nend * digit_base_value;
digit_base_value *= BASE;
nend--;
}
n_vector++;
nlen -= len_to_write;
}

if (nlen == 0) {
return;
}

bc_convert_to_vector(n_vector, nend, nlen);
}

#endif
Loading