-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
03955d6
aaf1f6a
b2c1b3f
c0000cd
0865562
5e4b175
13a98ae
b949c5e
484d9df
2d63079
c7916b5
a2ba77c
d3a5713
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
bc_free_num(quot); | ||
} | ||
|
||
/* If numerator is zero, the quotient is always zero. */ | ||
if (bc_is_zero(numerator)) { | ||
|
@@ -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; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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; | ||
} |
There was a problem hiding this comment.
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?