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
26 changes: 26 additions & 0 deletions ext/bcmath/libbcmath/src/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,30 @@ static inline void bc_convert_vector_to_char(const BC_VECTOR *vector, char *nptr
}
}

static inline void bc_convert_vector_to_char_with_skip(const BC_VECTOR *vector, char *nptr, char *nend, size_t arr_size, size_t skip)
Copy link
Member

Choose a reason for hiding this comment

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

This function either needs a better name or a doc block explaining what it does on a high level. What is skip and why is it necessary? I don't know at this point.

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

For example, if BC_VECTOR = [7890, 3456, 12], and want to construct a char array like 1234567, need to skip three digits 890 in 7890.

This kind of skipping is necessary when storing the rem (from division) into a char array, especially when the number of digits in BC_VECTOR doesn’t match the required scale for the rem.

↑ Would this explanation be clear enough to convey the intent?

{
/* bulk skip */
size_t array_skip = skip / BC_VECTOR_SIZE;
arr_size -= array_skip;
vector += array_skip;

/* skip */
skip %= BC_VECTOR_SIZE;
if (skip > 0) {
BC_VECTOR current_vector = *vector;
current_vector /= BC_POW_10_LUT[skip];
size_t write_size = MIN(nend - nptr + 1, BC_VECTOR_SIZE - skip);
for (size_t i = 0; i < write_size; i++) {
*nend-- = current_vector % BASE;
current_vector /= BASE;
}
vector++;
arr_size--;
}

if (arr_size > 0) {
bc_convert_vector_to_char(vector, nptr, nend, arr_size);
}
}

#endif