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
Prev Previous commit
Next Next commit
Changed bc_divide to bc_divide_ex, and changed three division fun…
…ctions to use it.
  • Loading branch information
SakiTakamachi committed May 23, 2025
commit b2c1b3fd07b3c28dbfa52b5e4d6ad95667041b39
1 change: 0 additions & 1 deletion ext/bcmath/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ if test "$PHP_BCMATH" != "no"; then
libbcmath/src/compare.c
libbcmath/src/convert.c
libbcmath/src/div.c
libbcmath/src/divmod.c
libbcmath/src/doaddsub.c
libbcmath/src/floor_or_ceil.c
libbcmath/src/long2num.c
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ARG_ENABLE("bcmath", "bc style precision math functions", "yes");
if (PHP_BCMATH == "yes") {
EXTENSION("bcmath", "bcmath.c", null, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
ADD_SOURCES("ext/bcmath/libbcmath/src", "add.c div.c init.c neg.c \
raisemod.c sub.c compare.c divmod.c int2num.c long2num.c \
raisemod.c sub.c compare.c int2num.c long2num.c \
num2long.c recmul.c sqrt.c zero.c doaddsub.c \
floor_or_ceil.c nearzero.c num2str.c raise.c rmzero.c str2num.c \
round.c convert.c", "bcmath");
Expand Down
11 changes: 6 additions & 5 deletions ext/bcmath/libbcmath/src/bcmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,7 @@ bc_num bc_multiply(bc_num n1, bc_num n2, size_t scale);
*(result) = mul_ex; \
} while (0)

bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, size_t scale);

bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);

bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, size_t scale, bool use_quot, bool use_rem);

bc_num bc_floor_or_ceil(bc_num num, bool is_floor);

Expand Down Expand Up @@ -188,4 +184,9 @@ bool bc_sqrt(bc_num *num, size_t scale);
#define bc_free_num(num) _bc_free_num_ex((num), 0)
#define bc_num2str(num) bc_num2str_ex((num), (num->n_scale))

/* div and mod */
#define bc_divide(n1, n2, quot, scale) bc_divide_ex((n1), (n2), (quot), NULL, (scale), true, false)
#define bc_modulo(n1, n2, rem, scale) bc_divide_ex((n1), (n2), NULL, (rem), (scale), false, true)
#define bc_divmod(n1, n2, quot, rem, scale) bc_divide_ex((n1), (n2), (quot), (rem), (scale), true, true)

#endif
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ static inline void bc_divide_by_pow_10(
}
}

bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, size_t scale, bool use_quot, bool use_rem)
{
/* divide by zero */
if (bc_is_zero(divisor)) {
Expand Down
90 changes: 0 additions & 90 deletions ext/bcmath/libbcmath/src/divmod.c

This file was deleted.