-
Notifications
You must be signed in to change notification settings - Fork 7.8k
ext/bcmath: Improving bcpow()
performance
#18099
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3ff726a
Removed `bc_fast_square` and `bc_standard_square`.
SakiTakamachi 788a884
Merged `bc_mul_finish_from_vector` into `bc_standard_mul`
SakiTakamachi 186214b
The calculation process of BC_VECTOR was separated inline as `bc_stan…
SakiTakamachi c1568f1
Removed the conversion process of bc_num and BC_VECTOR from `bc_squar…
SakiTakamachi a42d2e1
Changed bc_square_vector to normal multiplication and renamed it to b…
SakiTakamachi f303d7d
If base is 0, return early.
SakiTakamachi 757e3bc
optimized bc_raise
SakiTakamachi d203371
Use const appropriately
SakiTakamachi 2556951
Address comments
SakiTakamachi b96f2d9
Added overflow check
SakiTakamachi 54729fd
use SIZE_MAX
SakiTakamachi 91e3890
On failure, do nothing with the return value
SakiTakamachi 20c9309
Error handling is separated as a static function
SakiTakamachi 9e5c9a4
Fixed an error in enum type
SakiTakamachi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,31 +30,155 @@ | |
*************************************************************************/ | ||
|
||
#include "bcmath.h" | ||
#include "convert.h" | ||
#include "private.h" | ||
#include <assert.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
|
||
void bc_square_ex(bc_num n1, bc_num *result, size_t scale_min) { | ||
bc_num square_ex = bc_square(n1, scale_min); | ||
bc_free_num(result); | ||
*(result) = square_ex; | ||
static inline size_t bc_multiply_vector_ex( | ||
BC_VECTOR **n1_vector, size_t n1_arr_size, BC_VECTOR *n2_vector, size_t n2_arr_size, BC_VECTOR **result_vector) | ||
{ | ||
size_t result_arr_size = n1_arr_size + n2_arr_size; | ||
bc_multiply_vector(*n1_vector, n1_arr_size, n2_vector, n2_arr_size, *result_vector, result_arr_size); | ||
|
||
/* Eliminate extra zeros because they increase the number of calculations. */ | ||
while ((*result_vector)[result_arr_size - 1] == 0) { | ||
result_arr_size--; | ||
} | ||
|
||
/* Swap n1_vector and result_vector. */ | ||
BC_VECTOR *tmp = *n1_vector; | ||
*n1_vector = *result_vector; | ||
*result_vector = tmp; | ||
|
||
return result_arr_size; | ||
} | ||
|
||
static inline size_t bc_square_vector_ex(BC_VECTOR **base_vector, size_t base_arr_size, BC_VECTOR **result_vector) | ||
{ | ||
return bc_multiply_vector_ex(base_vector, base_arr_size, *base_vector, base_arr_size, result_vector); | ||
} | ||
|
||
/* Use "exponentiation by squaring". This is the fast path when the results are small. */ | ||
static inline bc_num bc_fast_raise( | ||
const char *base_end, long exponent, size_t base_len, size_t power_len, size_t power_scale, size_t power_full_len) | ||
{ | ||
BC_VECTOR base_vector = 0; | ||
|
||
/* Convert to BC_VECTOR[] */ | ||
bc_convert_to_vector(&base_vector, base_end, base_len); | ||
|
||
while ((exponent & 1) == 0) { | ||
base_vector *= base_vector; | ||
exponent >>= 1; | ||
} | ||
|
||
/* copy base to power */ | ||
BC_VECTOR power_vector = base_vector; | ||
exponent >>= 1; | ||
|
||
while (exponent > 0) { | ||
base_vector *= base_vector; | ||
if ((exponent & 1) == 1) { | ||
power_vector *= base_vector; | ||
} | ||
exponent >>= 1; | ||
} | ||
|
||
bc_num power = bc_new_num_nonzeroed(power_len, power_scale); | ||
char *pptr = power->n_value; | ||
char *pend = pptr + power_full_len - 1; | ||
|
||
while (pend >= pptr) { | ||
*pend-- = power_vector % BASE; | ||
power_vector /= BASE; | ||
} | ||
return power; | ||
} | ||
|
||
/* Use "exponentiation by squaring". This is the standard path. */ | ||
static bc_num bc_standard_raise( | ||
const char *base_ptr, const char *base_end, long exponent, size_t base_len, size_t power_scale) | ||
{ | ||
/* Remove the leading zeros as they will be filled in later. */ | ||
while (*base_ptr == 0) { | ||
base_ptr++; | ||
base_len--; | ||
} | ||
|
||
size_t base_arr_size = BC_ARR_SIZE_FROM_LEN(base_len); | ||
/* Since it is guaranteed that base_len * exponent does not overflow, there is no possibility of overflow here. */ | ||
size_t max_power_arr_size = base_arr_size * exponent; | ||
|
||
/* The allocated memory area is reused on a rotational basis, so the same size is required. */ | ||
BC_VECTOR *buf = safe_emalloc(max_power_arr_size, sizeof(BC_VECTOR) * 3, 0); | ||
BC_VECTOR *base_vector = buf; | ||
BC_VECTOR *power_vector = base_vector + max_power_arr_size; | ||
BC_VECTOR *tmp_result_vector = power_vector + max_power_arr_size; | ||
|
||
/* Convert to BC_VECTOR[] */ | ||
bc_convert_to_vector(base_vector, base_end, base_len); | ||
|
||
while ((exponent & 1) == 0) { | ||
base_arr_size = bc_square_vector_ex(&base_vector, base_arr_size, &tmp_result_vector); | ||
exponent >>= 1; | ||
} | ||
|
||
/* copy base to power */ | ||
size_t power_arr_size = base_arr_size; | ||
for (size_t i = 0; i < base_arr_size; i++) { | ||
power_vector[i] = base_vector[i]; | ||
} | ||
exponent >>= 1; | ||
|
||
while (exponent > 0) { | ||
base_arr_size = bc_square_vector_ex(&base_vector, base_arr_size, &tmp_result_vector); | ||
if ((exponent & 1) == 1) { | ||
power_arr_size = bc_multiply_vector_ex(&power_vector, power_arr_size, base_vector, base_arr_size, &tmp_result_vector); | ||
} | ||
exponent >>= 1; | ||
} | ||
|
||
/* Convert to bc_num */ | ||
size_t power_leading_zeros = 0; | ||
size_t power_len; | ||
size_t power_full_len = power_arr_size * BC_VECTOR_SIZE; | ||
if (power_full_len > power_scale) { | ||
power_len = power_full_len - power_scale; | ||
} else { | ||
power_len = 1; | ||
power_leading_zeros = power_scale - power_full_len + 1; | ||
power_full_len = power_scale + 1; | ||
} | ||
bc_num power = bc_new_num_nonzeroed(power_len, power_scale); | ||
|
||
char *pptr = power->n_value; | ||
char *pend = pptr + power_full_len - 1; | ||
|
||
/* Pad with leading zeros if necessary. */ | ||
memset(pptr, 0, power_leading_zeros); | ||
pptr += power_leading_zeros; | ||
|
||
bc_convert_vector_to_char(power_vector, pptr, pend, power_arr_size); | ||
|
||
efree(buf); | ||
|
||
return power; | ||
} | ||
|
||
/* Raise "base" to the "exponent" power. The result is placed in RESULT. | ||
Maximum exponent is LONG_MAX. If a "exponent" is not an integer, | ||
only the integer part is used. */ | ||
bool bc_raise(bc_num base, long exponent, bc_num *result, size_t scale) { | ||
bc_num temp, power; | ||
bc_raise_status bc_raise(bc_num base, long exponent, bc_num *result, size_t scale) { | ||
size_t rscale; | ||
size_t pwrscale; | ||
size_t calcscale; | ||
bool is_neg; | ||
|
||
/* Special case if exponent is a zero. */ | ||
if (exponent == 0) { | ||
bc_free_num (result); | ||
*result = bc_copy_num(BCG(_one_)); | ||
return true; | ||
return BC_RAISE_STATUS_OK; | ||
} | ||
|
||
/* Other initializations. */ | ||
|
@@ -67,44 +191,66 @@ bool bc_raise(bc_num base, long exponent, bc_num *result, size_t scale) { | |
rscale = MIN (base->n_scale * exponent, MAX(scale, base->n_scale)); | ||
} | ||
|
||
/* Set initial value of temp. */ | ||
power = bc_copy_num(base); | ||
pwrscale = base->n_scale; | ||
while ((exponent & 1) == 0) { | ||
pwrscale = 2 * pwrscale; | ||
bc_square_ex(power, &power, pwrscale); | ||
exponent = exponent >> 1; | ||
if (bc_is_zero(base)) { | ||
/* If the exponent is negative, it divides by 0 */ | ||
return is_neg ? BC_RAISE_STATUS_DIVIDE_BY_ZERO : BC_RAISE_STATUS_OK; | ||
} | ||
temp = bc_copy_num(power); | ||
calcscale = pwrscale; | ||
exponent = exponent >> 1; | ||
|
||
/* Do the calculation. */ | ||
while (exponent > 0) { | ||
pwrscale = 2 * pwrscale; | ||
bc_square_ex(power, &power, pwrscale); | ||
if ((exponent & 1) == 1) { | ||
calcscale = pwrscale + calcscale; | ||
bc_multiply_ex(temp, power, &temp, calcscale); | ||
} | ||
exponent = exponent >> 1; | ||
/* check overflow */ | ||
if (UNEXPECTED(base->n_len > SIZE_MAX / exponent)) { | ||
return BC_RAISE_STATUS_LEN_IS_OVERFLOW; | ||
} | ||
if (UNEXPECTED(base->n_scale > SIZE_MAX / exponent)) { | ||
return BC_RAISE_STATUS_SCALE_IS_OVERFLOW; | ||
} | ||
|
||
size_t base_len = base->n_len + base->n_scale; | ||
size_t power_len = base->n_len * exponent; | ||
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 wonder which of these can overflow 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. Added check in b96f2d9 |
||
size_t power_scale = base->n_scale * exponent; | ||
|
||
/* check overflow */ | ||
if (UNEXPECTED(power_len > SIZE_MAX - power_scale)) { | ||
return BC_RAISE_STATUS_FULLLEN_IS_OVERFLOW; | ||
} | ||
size_t power_full_len = power_len + power_scale; | ||
|
||
sign power_sign; | ||
if (base->n_sign == MINUS && (exponent & 1) == 1) { | ||
power_sign = MINUS; | ||
} else { | ||
power_sign = PLUS; | ||
} | ||
|
||
const char *base_end = base->n_value + base_len - 1; | ||
|
||
bc_num power; | ||
if (base_len <= BC_VECTOR_SIZE && power_full_len <= BC_VECTOR_SIZE * 2) { | ||
power = bc_fast_raise(base_end, exponent, base_len, power_len, power_scale, power_full_len); | ||
} else { | ||
power = bc_standard_raise(base->n_value, base_end, exponent, base_len, power_scale); | ||
} | ||
|
||
_bc_rm_leading_zeros(power); | ||
if (bc_is_zero(power)) { | ||
power->n_sign = PLUS; | ||
power->n_scale = 0; | ||
} else { | ||
power->n_sign = power_sign; | ||
} | ||
|
||
/* Assign the value. */ | ||
if (is_neg) { | ||
if (bc_divide(BCG(_one_), temp, result, rscale) == false) { | ||
bc_free_num (&temp); | ||
if (bc_divide(BCG(_one_), power, result, rscale) == false) { | ||
bc_free_num (&power); | ||
return false; | ||
return BC_RAISE_STATUS_DIVIDE_BY_ZERO; | ||
} | ||
bc_free_num (&temp); | ||
bc_free_num (&power); | ||
} else { | ||
bc_free_num (result); | ||
*result = temp; | ||
*result = power; | ||
(*result)->n_scale = MIN(scale, (*result)->n_scale); | ||
} | ||
bc_free_num (&power); | ||
return true; | ||
return BC_RAISE_STATUS_OK; | ||
} | ||
|
||
/* This is used internally by BCMath */ | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this overflow?
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.
Added check for
base->n_len * exponent
.So guaranteed not to overflow here. I added a comment.
b96f2d9