Skip to content

ext/bcmath: optimized divmod() and mod() take 2 #18058

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 13 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
28 changes: 26 additions & 2 deletions ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ static inline void bc_divide_by_one(
}

static inline void bc_divide_by_pow_10(
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot)
const char *numeratorptr, size_t numerator_readable_size, size_t numerator_leading_zeros,
bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot,
bc_num *rem, size_t rem_size, bool use_rem,
size_t numerator_rem_len_diff)
{
if (use_quot) {
char *qptr = (*quot)->n_value;
Expand All @@ -363,6 +366,23 @@ static inline void bc_divide_by_pow_10(
(*quot)->n_scale -= qend - qptr;
}
}
if (use_rem) {
size_t rem_leading_zeros = numerator_leading_zeros + quot_size - numerator_rem_len_diff;
if (rem_size <= rem_leading_zeros) {
bc_free_num(rem);
*rem = bc_copy_num(BCG(_zero_));
return;
}
/* The values after this have already been copied, so just need to set them to 0. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied by whom?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About 10 lines above the line where this function is called, there’s the following line:

memcpy((*rem)->n_value + rem_write_size, numerator->n_value + rem_write_size + numerator_rem_len_diff, copy_size);

The intention here was that everything except the leading zeros has already been copied.

for (size_t i = 0; i < rem_leading_zeros; i++) {
(*rem)->n_value[i] = 0;
}
_bc_rm_leading_zeros(*rem);
if (bc_is_zero(*rem)) {
(*rem)->n_sign = PLUS;
(*rem)->n_scale = 0;
}
}
}

bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, size_t scale, bool use_quot, bool use_rem)
Expand Down Expand Up @@ -492,7 +512,11 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s

/* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */
if (divisor_size == 1 && *divisorptr == 1) {
bc_divide_by_pow_10(numeratorptr, numerator_readable_size, quot, quot_size, quot_scale);
bc_divide_by_pow_10(
numeratorptr, numerator_readable_size, numerator_leading_zeros,
quot, quot_size, quot_scale, use_quot,
rem, rem_size, use_rem, numerator_rem_len_diff
);
return true;
}

Expand Down