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
25 changes: 23 additions & 2 deletions ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ static void bc_do_div(
const char *numerator, size_t numerator_size, size_t numerator_readable_size,
const char *divisor, size_t divisor_size,
bc_num *quot, size_t quot_size,
bool use_quot
bc_num *rem, size_t rem_over_size, size_t rem_write_size,
bool use_quot, bool use_rem
) {
size_t numerator_arr_size = BC_ARR_SIZE_FROM_LEN(numerator_size);
size_t divisor_arr_size = BC_ARR_SIZE_FROM_LEN(divisor_size);
Expand Down Expand Up @@ -298,6 +299,24 @@ static void bc_do_div(
char *qend = qptr + (*quot)->n_len + (*quot)->n_scale - 1;
bc_convert_vector_to_char(quot_vectors, qptr, qend, quot_real_arr_size);
}
if (use_rem) {
char *rptr = (*rem)->n_value;
char *rend = rptr + rem_write_size - 1;

size_t rem_arr_size = (rem_write_size + rem_over_size + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE;
Copy link
Member

Choose a reason for hiding this comment

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

Didn't we have a macro for the rounding-up divide ?

if (UNEXPECTED(rem_arr_size > numerator_arr_size)) {
/* If numerator_arr_size is exceeded because the integer part is zero */
rem_arr_size = numerator_arr_size;
*rptr = 0;
}
BC_VECTOR *rem_vectors = numerator_vectors;

if (rem_over_size > 0) {
bc_convert_vector_to_char_with_skip(rem_vectors, rptr, rend, rem_arr_size, rem_over_size);
Copy link
Member

Choose a reason for hiding this comment

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

I still don't understand the skip

} else {
bc_convert_vector_to_char(rem_vectors, rptr, rend, rem_arr_size);
}
}

if (allocation_arr_size > BC_STACK_VECTOR_SIZE) {
efree(numerator_vectors);
Expand Down Expand Up @@ -524,7 +543,9 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
bc_do_div(
numeratorptr, numerator_size, numerator_readable_size,
divisorptr, divisor_size,
quot, quot_size
quot, quot_size,
rem, rem_over_size, rem_write_size,
use_quot, use_rem
);

if (use_quot) {
Expand Down