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
29 changes: 19 additions & 10 deletions ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,10 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
return false;
}

bc_free_num(quot);
size_t quot_scale = scale;
if (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.

Instead of using a separate argument for use_quot, use_rem. Why not check if quot/rem is a NULL pointer?

bc_free_num(quot);
}

/* If numerator is zero, the quotient is always zero. */
if (bc_is_zero(numerator)) {
Expand Down Expand Up @@ -399,12 +401,14 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
numerator_size -= divisor_trailing_zeros;

size_t quot_size = numerator_size - divisor_size + 1; /* numerator_size >= divisor_size */
if (quot_size > quot_scale) {
*quot = bc_new_num_nonzeroed(quot_size - quot_scale, quot_scale);
} else {
*quot = bc_new_num_nonzeroed(1, quot_scale); /* 1 is for 0 */
if (use_quot) {
if (quot_size > quot_scale) {
*quot = bc_new_num_nonzeroed(quot_size - quot_scale, quot_scale);
} else {
*quot = bc_new_num_nonzeroed(1, quot_scale); /* 1 is for 0 */
}
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
}
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;

/* Size that can be read from numeratorptr */
size_t numerator_readable_size = numerator->n_len + numerator->n_scale - numerator_leading_zeros;
Expand All @@ -422,13 +426,18 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
quot, quot_size
);

_bc_rm_leading_zeros(*quot);
if (bc_is_zero(*quot)) {
(*quot)->n_sign = PLUS;
if (use_quot) {
_bc_rm_leading_zeros(*quot);
if (bc_is_zero(*quot)) {
(*quot)->n_sign = PLUS;
(*quot)->n_scale = 0;
Copy link
Member

Choose a reason for hiding this comment

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

I guess my previous question is answered because this line returned, but you should instead amend the previous commit so that it wasn't deleted in the first place. That keeps the history cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

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

@nielsdos

Sorry, I think a line was accidentally removed when I rebased and merged in master.

I had only reviewed the final version of the code, so I didn’t notice that one of the intermediate commits included an unintended deletion.

}
}
return true;

quot_zero:
*quot = bc_copy_num(BCG(_zero_));
if (use_quot) {
*quot = bc_copy_num(BCG(_zero_));
}
return true;
}