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
56 changes: 31 additions & 25 deletions ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ static inline void bc_standard_div(
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
bc_num *quot, size_t quot_size,
bool use_quot
Copy link
Member

Choose a reason for hiding this comment

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

Same remark about the quot NULL pointer instead of the bool

) {
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 @@ -292,10 +293,11 @@ static void bc_do_div(
}

/* Convert to bc_num */
char *qptr = (*quot)->n_value;
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_quot) {
char *qptr = (*quot)->n_value;
char *qend = qptr + (*quot)->n_len + (*quot)->n_scale - 1;
bc_convert_vector_to_char(quot_vectors, qptr, qend, quot_real_arr_size);
}

if (allocation_arr_size > BC_STACK_VECTOR_SIZE) {
efree(numerator_vectors);
Expand All @@ -309,33 +311,37 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
memcpy((*num)->n_value, numerator->n_value, numerator->n_len + scale);
}

static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale)
static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale, bool use_quot)
{
bc_divide_copy_numerator(numerator, quot, quot_scale);
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
if (use_quot) {
bc_divide_copy_numerator(numerator, quot, quot_scale);
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
}
}

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)
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot)
{
char *qptr = (*quot)->n_value;
for (size_t i = quot_size; i <= quot_scale; i++) {
*qptr++ = 0;
}
if (use_quot) {
char *qptr = (*quot)->n_value;
for (size_t i = quot_size; i <= quot_scale; i++) {
*qptr++ = 0;
}

size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
memcpy(qptr, numeratorptr, numerator_use_size);
qptr += numerator_use_size;
size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size;
memcpy(qptr, numeratorptr, numerator_use_size);
qptr += numerator_use_size;

if (numerator_use_size < (*quot)->n_len) {
/* e.g. 12.3 / 0.01 <=> 1230 */
for (size_t i = numerator_use_size; i < (*quot)->n_len; i++) {
*qptr++ = 0;
if (numerator_use_size < (*quot)->n_len) {
/* e.g. 12.3 / 0.01 <=> 1230 */
for (size_t i = numerator_use_size; i < (*quot)->n_len; i++) {
*qptr++ = 0;
}
(*quot)->n_scale = 0;
} else {
char *qend = (*quot)->n_value + (*quot)->n_len + (*quot)->n_scale;
(*quot)->n_scale -= qend - qptr;
}
(*quot)->n_scale = 0;
} else {
char *qend = (*quot)->n_value + (*quot)->n_len + (*quot)->n_scale;
(*quot)->n_scale -= qend - qptr;
}
}

Expand All @@ -358,7 +364,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s

/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) {
bc_divide_by_one(numerator, divisor, quot, quot_scale);
bc_divide_by_one(numerator, divisor, quot, quot_scale, use_quot);
return true;
}

Expand Down