From 12557c13bac7bab765a1c6a48eeebe8e5bf86ddc Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Thu, 30 Oct 2025 18:17:17 +0300 Subject: [PATCH 1/4] add fixes, del extra files --- builtin-functions/kphp-full/_functions.txt | 16 +- .../stdlib/math/bcmath-functions.cpp | 147 ++++++++---------- runtime-common/stdlib/math/bcmath-functions.h | 16 +- 3 files changed, 81 insertions(+), 98 deletions(-) diff --git a/builtin-functions/kphp-full/_functions.txt b/builtin-functions/kphp-full/_functions.txt index 449ea8b7c5..694ec86f59 100644 --- a/builtin-functions/kphp-full/_functions.txt +++ b/builtin-functions/kphp-full/_functions.txt @@ -784,14 +784,14 @@ define('PHP_ROUND_HALF_EVEN', 123423145); define('PHP_ROUND_HALF_ODD', 123423146); function bcscale ($scale ::: int) ::: void; -function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; -function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; -function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; -function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; -function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; -function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; -function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: int; -function bcsqrt($num ::: string, $scale ::: int = PHP_INT_MIN): string; +function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; +function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; +function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; +function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; +function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; +function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; +function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: int; +function bcsqrt($num ::: string, $scale ::: ?int = null): string; function mysqli_errno(\mysqli $dn) ::: int; function mysqli_error(\mysqli $dn) ::: string; diff --git a/runtime-common/stdlib/math/bcmath-functions.cpp b/runtime-common/stdlib/math/bcmath-functions.cpp index 56d199a155..e98ec6d5b1 100644 --- a/runtime-common/stdlib/math/bcmath-functions.cpp +++ b/runtime-common/stdlib/math/bcmath-functions.cpp @@ -490,22 +490,20 @@ void f$bcscale(int64_t scale) noexcept { } } -string f$bcdiv(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept { +string f$bcdiv(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { auto& math_lib_context{MathLibContext::get()}; const auto& math_lib_constants{MathLibConstants::get()}; - if (scale == std::numeric_limits::min()) { - scale = math_lib_context.bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcdiv", scale); - scale = 0; + int64_t scale_val{!scale.has_value() ? math_lib_context.bc_scale : scale.val()}; + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcdiv", scale_val); + scale_val = 0; } if (lhs_str.empty()) { - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } if (rhs_str.empty()) { php_warning("Division by empty string in function bcdiv"); - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -520,7 +518,7 @@ string f$bcdiv(const string& lhs_str, const string& rhs_str, int64_t scale) noex return math_lib_constants.ZERO; } - return bc_div_positive(lhs, rhs, static_cast(scale), lhs.n_sign * rhs.n_sign); + return bc_div_positive(lhs, rhs, static_cast(scale_val), lhs.n_sign * rhs.n_sign); } static string scale_num(const string& num, int64_t scale) noexcept { @@ -533,19 +531,17 @@ static string scale_num(const string& num, int64_t scale) noexcept { return num; } -string f$bcmod(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept { +string f$bcmod(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { auto& math_lib_context{MathLibContext::get()}; const auto& math_lib_constants{MathLibConstants::get()}; - if (scale == std::numeric_limits::min()) { - scale = math_lib_context.bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcmod", scale); - scale = 0; + int64_t scale_val{!scale.has_value() ? math_lib_context.bc_scale : scale.val()}; + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcmod", scale_val); + scale_val = 0; } if (lhs_str.empty()) { - return bc_zero(scale); + return bc_zero(scale_val); } auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -565,7 +561,7 @@ string f$bcmod(const string& lhs_str, const string& rhs_str, int64_t scale) noex return {}; } - const int rscale = std::max(lhs.n_scale, rhs.n_scale + static_cast(scale)); + const int rscale = std::max(lhs.n_scale, rhs.n_scale + static_cast(scale_val)); // result should have the same sign as lhs const int result_sign = lhs.n_sign; @@ -581,7 +577,7 @@ string f$bcmod(const string& lhs_str, const string& rhs_str, int64_t scale) noex string sub = bc_sub(lhs, x, rscale); x = bcmath_impl_::bc_parse_number(sub).first; - return bc_div_positive(x, math_lib_constants.BC_NUM_ONE, scale, result_sign); + return bc_div_positive(x, math_lib_constants.BC_NUM_ONE, scale_val, result_sign); } static std::pair bc_num2int(const bcmath_impl_::BcNum& num) noexcept { @@ -600,27 +596,25 @@ static std::pair bc_num2int(const bcmath_impl_::BcNum& num) return {ingeger, true}; } -string f$bcpow(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept { +string f$bcpow(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { auto& math_lib_context{MathLibContext::get()}; const auto& math_lib_constants{MathLibConstants::get()}; - if (scale == std::numeric_limits::min()) { - scale = math_lib_context.bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcpow", scale); - scale = 0; + int64_t scale_val{!scale.has_value() ? math_lib_context.bc_scale : scale.val()}; + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcpow", scale_val); + scale_val = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str.empty() ? math_lib_constants.ZERO : lhs_str); if (!lhs_success) { php_warning("First parameter \"%s\" in function bcpow is not a number", lhs.str.c_str()); - return scale_num(math_lib_constants.ZERO, scale); + return scale_num(math_lib_constants.ZERO, scale_val); } const auto [rhs, rhs_success] = bcmath_impl_::bc_parse_number(rhs_str.empty() ? math_lib_constants.ZERO : rhs_str); if (!rhs_success) { php_warning("Second parameter \"%s\" in function bcpow is not a number", rhs.str.c_str()); - return scale_num(math_lib_constants.ONE, scale); + return scale_num(math_lib_constants.ONE, scale_val); } if (rhs.n_scale != 0) { php_warning("bcpow(): non-zero scale \"%s\" in exponent", rhs.str.c_str()); @@ -629,21 +623,21 @@ string f$bcpow(const string& lhs_str, const string& rhs_str, int64_t scale) noex auto [exponent, exp_success] = bc_num2int(rhs); if (!exp_success) { php_warning("Second parameter \"%s\" in function bcpow is larger than 1e18", rhs.str.c_str()); - return scale_num(math_lib_constants.ZERO, scale); + return scale_num(math_lib_constants.ZERO, scale_val); } if (exponent == 0) { - return scale_num(math_lib_constants.ONE, scale); + return scale_num(math_lib_constants.ONE, scale_val); } bool neg = false; - int rscale = scale; + int rscale = scale_val; if (exponent < 0) { neg = true; exponent = -exponent; } else { - rscale = std::min(static_cast(lhs.n_scale * exponent), std::max(static_cast(scale), lhs.n_scale)); + rscale = std::min(static_cast(lhs.n_scale * exponent), std::max(static_cast(scale_val), lhs.n_scale)); } // set initial value of temp @@ -681,7 +675,7 @@ string f$bcpow(const string& lhs_str, const string& rhs_str, int64_t scale) noex return f$bcadd(result, math_lib_constants.ZERO, scale); } -string f$bcadd(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept { +string f$bcadd(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { return f$bcadd(math_lib_constants.ZERO, rhs_str, scale); @@ -690,30 +684,29 @@ string f$bcadd(const string& lhs_str, const string& rhs_str, int64_t scale) noex return f$bcadd(lhs_str, math_lib_constants.ZERO, scale); } - if (scale == std::numeric_limits::min()) { - scale = MathLibContext::get().bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcadd", scale); - scale = 0; + int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; + + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcadd", scale_val); + scale_val = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); if (!lhs_success) { php_warning("First parameter \"%s\" in function bcadd is not a number", lhs.str.c_str()); - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } const auto [rhs, rhs_success] = bcmath_impl_::bc_parse_number(rhs_str); if (!rhs_success) { php_warning("Second parameter \"%s\" in function bcadd is not a number", rhs.str.c_str()); - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } - return bc_add(lhs, rhs, static_cast(scale)); + return bc_add(lhs, rhs, static_cast(scale_val)); } -string f$bcsub(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept { +string f$bcsub(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { return f$bcsub(math_lib_constants.ZERO, rhs_str, scale); @@ -721,33 +714,30 @@ string f$bcsub(const string& lhs_str, const string& rhs_str, int64_t scale) noex if (rhs_str.empty()) { return f$bcsub(lhs_str, math_lib_constants.ZERO, scale); } - - if (scale == std::numeric_limits::min()) { - scale = MathLibContext::get().bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcsub", scale); - scale = 0; + int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcsub", scale_val); + scale_val = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); if (!lhs_success) { php_warning("First parameter \"%s\" in function bcsub is not a number", lhs.str.c_str()); - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } auto [rhs, rhs_success] = bcmath_impl_::bc_parse_number(rhs_str); if (!rhs_success) { php_warning("Second parameter \"%s\" in function bcsub is not a number", rhs.str.c_str()); - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } rhs.n_sign *= -1; - return bc_add(lhs, rhs, static_cast(scale)); + return bc_add(lhs, rhs, static_cast(scale_val)); } -string f$bcmul(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept { +string f$bcmul(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { return f$bcmul(math_lib_constants.ZERO, rhs_str, scale); @@ -755,13 +745,10 @@ string f$bcmul(const string& lhs_str, const string& rhs_str, int64_t scale) noex if (rhs_str.empty()) { return f$bcmul(lhs_str, math_lib_constants.ZERO, scale); } - - if (scale == std::numeric_limits::min()) { - scale = MathLibContext::get().bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcmul", scale); - scale = 0; + int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcmul", scale_val); + scale_val = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -776,10 +763,10 @@ string f$bcmul(const string& lhs_str, const string& rhs_str, int64_t scale) noex return math_lib_constants.ZERO; } - return bc_mul_positive(lhs, rhs, static_cast(scale), lhs.n_sign * rhs.n_sign); + return bc_mul_positive(lhs, rhs, static_cast(scale_val), lhs.n_sign * rhs.n_sign); } -int64_t f$bccomp(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept { +int64_t f$bccomp(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { return f$bccomp(math_lib_constants.ZERO, rhs_str, scale); @@ -788,12 +775,10 @@ int64_t f$bccomp(const string& lhs_str, const string& rhs_str, int64_t scale) no return f$bccomp(lhs_str, math_lib_constants.ZERO, scale); } - if (scale == std::numeric_limits::min()) { - scale = MathLibContext::get().bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bccomp", scale); - scale = 0; + int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bccomp", scale_val); + scale_val = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -812,7 +797,7 @@ int64_t f$bccomp(const string& lhs_str, const string& rhs_str, int64_t scale) no return (lhs.n_sign - rhs.n_sign) / 2; } - return (1 - 2 * (lhs.n_sign < 0)) * bc_comp(lhs, rhs, static_cast(scale)); + return (1 - 2 * (lhs.n_sign < 0)) * bc_comp(lhs, rhs, static_cast(scale_val)); } // In some places we need to check if the number NUM is almost zero. @@ -907,30 +892,28 @@ static std::pair bc_sqrt(const bcmath_impl_::BcNum& num, int scale return {bc_div_positive(guess, math_lib_constants.BC_NUM_ONE, rscale, 1), true}; } -string f$bcsqrt(const string& num_str, int64_t scale) noexcept { - if (scale == std::numeric_limits::min()) { - scale = MathLibContext::get().bc_scale; - } - if (scale < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcsqrt", scale); - scale = 0; +string f$bcsqrt(const string& num_str, Optional scale) noexcept { + int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; + if (scale_val < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcsqrt", scale_val); + scale_val = 0; } if (num_str.empty()) { - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } const auto [num, parse_success] = bcmath_impl_::bc_parse_number(num_str); if (!parse_success) { php_warning("First parameter \"%s\" in function bcsqrt is not a number", num.str.c_str()); - return bc_zero(static_cast(scale)); + return bc_zero(static_cast(scale_val)); } - auto [sqrt, sqrt_success] = bc_sqrt(num, scale); + auto [sqrt, sqrt_success] = bc_sqrt(num, scale_val); if (!sqrt_success) { php_warning("bcsqrt(): Square root of negative number"); return {}; } const bcmath_impl_::BcNum& sqrt_bc_num = bcmath_impl_::bc_parse_number(sqrt).first; - return bc_div_positive(sqrt_bc_num, MathLibConstants::get().BC_NUM_ONE, scale, 1); + return bc_div_positive(sqrt_bc_num, MathLibConstants::get().BC_NUM_ONE, scale_val, 1); } diff --git a/runtime-common/stdlib/math/bcmath-functions.h b/runtime-common/stdlib/math/bcmath-functions.h index 6fd4adbebb..30893c379d 100644 --- a/runtime-common/stdlib/math/bcmath-functions.h +++ b/runtime-common/stdlib/math/bcmath-functions.h @@ -15,18 +15,18 @@ std::pair bc_parse_number(const string& num) noexcept; void f$bcscale(int64_t scale) noexcept; -string f$bcdiv(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits::min()) noexcept; +string f$bcdiv(const string& lhs_str, const string& rhs_str, Optional scale = Optional{}) noexcept; -string f$bcmod(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits::min()) noexcept; +string f$bcmod(const string& lhs_str, const string& rhs_str, Optional scale = Optional{}) noexcept; -string f$bcpow(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits::min()) noexcept; +string f$bcpow(const string& lhs_str, const string& rhs_str, Optional scale = Optional{}) noexcept; -string f$bcadd(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits::min()) noexcept; +string f$bcadd(const string& lhs_str, const string& rhs_str, Optional scale = Optional{}) noexcept; -string f$bcsub(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits::min()) noexcept; +string f$bcsub(const string& lhs_str, const string& rhs_str, Optional scale = Optional{}) noexcept; -string f$bcmul(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits::min()) noexcept; +string f$bcmul(const string& lhs_str, const string& rhs_str, Optional scale = Optional{}) noexcept; -int64_t f$bccomp(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits::min()) noexcept; +int64_t f$bccomp(const string& lhs_str, const string& rhs_str, Optional scale = Optional{}) noexcept; -string f$bcsqrt(const string& num_str, int64_t scale = std::numeric_limits::min()) noexcept; +string f$bcsqrt(const string& num_str, Optional scale = Optional{}) noexcept; From f30690e336139fb335639d55335a001b71843e43 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Thu, 30 Oct 2025 18:51:40 +0300 Subject: [PATCH 2/4] rename param, var --- .../stdlib/math/bcmath-functions.cpp | 146 +++++++++--------- 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/runtime-common/stdlib/math/bcmath-functions.cpp b/runtime-common/stdlib/math/bcmath-functions.cpp index e98ec6d5b1..81bbce6b16 100644 --- a/runtime-common/stdlib/math/bcmath-functions.cpp +++ b/runtime-common/stdlib/math/bcmath-functions.cpp @@ -490,20 +490,20 @@ void f$bcscale(int64_t scale) noexcept { } } -string f$bcdiv(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { +string f$bcdiv(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept { auto& math_lib_context{MathLibContext::get()}; const auto& math_lib_constants{MathLibConstants::get()}; - int64_t scale_val{!scale.has_value() ? math_lib_context.bc_scale : scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcdiv", scale_val); - scale_val = 0; + int64_t scale{!opt_scale.has_value() ? math_lib_context.bc_scale : opt_scale.val()}; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcdiv", scale); + scale = 0; } if (lhs_str.empty()) { - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } if (rhs_str.empty()) { php_warning("Division by empty string in function bcdiv"); - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -518,7 +518,7 @@ string f$bcdiv(const string& lhs_str, const string& rhs_str, Optional s return math_lib_constants.ZERO; } - return bc_div_positive(lhs, rhs, static_cast(scale_val), lhs.n_sign * rhs.n_sign); + return bc_div_positive(lhs, rhs, static_cast(scale), lhs.n_sign * rhs.n_sign); } static string scale_num(const string& num, int64_t scale) noexcept { @@ -531,17 +531,17 @@ static string scale_num(const string& num, int64_t scale) noexcept { return num; } -string f$bcmod(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { +string f$bcmod(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept { auto& math_lib_context{MathLibContext::get()}; const auto& math_lib_constants{MathLibConstants::get()}; - int64_t scale_val{!scale.has_value() ? math_lib_context.bc_scale : scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcmod", scale_val); - scale_val = 0; + int64_t scale{!opt_scale.has_value() ? math_lib_context.bc_scale : opt_scale.val()}; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcmod", scale); + scale = 0; } if (lhs_str.empty()) { - return bc_zero(scale_val); + return bc_zero(scale); } auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -561,7 +561,7 @@ string f$bcmod(const string& lhs_str, const string& rhs_str, Optional s return {}; } - const int rscale = std::max(lhs.n_scale, rhs.n_scale + static_cast(scale_val)); + const int rscale = std::max(lhs.n_scale, rhs.n_scale + static_cast(scale)); // result should have the same sign as lhs const int result_sign = lhs.n_sign; @@ -577,7 +577,7 @@ string f$bcmod(const string& lhs_str, const string& rhs_str, Optional s string sub = bc_sub(lhs, x, rscale); x = bcmath_impl_::bc_parse_number(sub).first; - return bc_div_positive(x, math_lib_constants.BC_NUM_ONE, scale_val, result_sign); + return bc_div_positive(x, math_lib_constants.BC_NUM_ONE, scale, result_sign); } static std::pair bc_num2int(const bcmath_impl_::BcNum& num) noexcept { @@ -596,25 +596,25 @@ static std::pair bc_num2int(const bcmath_impl_::BcNum& num) return {ingeger, true}; } -string f$bcpow(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { +string f$bcpow(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept { auto& math_lib_context{MathLibContext::get()}; const auto& math_lib_constants{MathLibConstants::get()}; - int64_t scale_val{!scale.has_value() ? math_lib_context.bc_scale : scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcpow", scale_val); - scale_val = 0; + int64_t scale{!opt_scale.has_value() ? math_lib_context.bc_scale : opt_scale.val()}; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcpow", scale); + scale = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str.empty() ? math_lib_constants.ZERO : lhs_str); if (!lhs_success) { php_warning("First parameter \"%s\" in function bcpow is not a number", lhs.str.c_str()); - return scale_num(math_lib_constants.ZERO, scale_val); + return scale_num(math_lib_constants.ZERO, scale); } const auto [rhs, rhs_success] = bcmath_impl_::bc_parse_number(rhs_str.empty() ? math_lib_constants.ZERO : rhs_str); if (!rhs_success) { php_warning("Second parameter \"%s\" in function bcpow is not a number", rhs.str.c_str()); - return scale_num(math_lib_constants.ONE, scale_val); + return scale_num(math_lib_constants.ONE, scale); } if (rhs.n_scale != 0) { php_warning("bcpow(): non-zero scale \"%s\" in exponent", rhs.str.c_str()); @@ -623,21 +623,21 @@ string f$bcpow(const string& lhs_str, const string& rhs_str, Optional s auto [exponent, exp_success] = bc_num2int(rhs); if (!exp_success) { php_warning("Second parameter \"%s\" in function bcpow is larger than 1e18", rhs.str.c_str()); - return scale_num(math_lib_constants.ZERO, scale_val); + return scale_num(math_lib_constants.ZERO, scale); } if (exponent == 0) { - return scale_num(math_lib_constants.ONE, scale_val); + return scale_num(math_lib_constants.ONE, scale); } bool neg = false; - int rscale = scale_val; + int rscale = scale; if (exponent < 0) { neg = true; exponent = -exponent; } else { - rscale = std::min(static_cast(lhs.n_scale * exponent), std::max(static_cast(scale_val), lhs.n_scale)); + rscale = std::min(static_cast(lhs.n_scale * exponent), std::max(static_cast(scale), lhs.n_scale)); } // set initial value of temp @@ -672,83 +672,83 @@ string f$bcpow(const string& lhs_str, const string& rhs_str, Optional s } } // just scale result - return f$bcadd(result, math_lib_constants.ZERO, scale); + return f$bcadd(result, math_lib_constants.ZERO, opt_scale); } -string f$bcadd(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { +string f$bcadd(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { - return f$bcadd(math_lib_constants.ZERO, rhs_str, scale); + return f$bcadd(math_lib_constants.ZERO, rhs_str, opt_scale); } if (rhs_str.empty()) { - return f$bcadd(lhs_str, math_lib_constants.ZERO, scale); + return f$bcadd(lhs_str, math_lib_constants.ZERO, opt_scale); } - int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; + int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcadd", scale_val); - scale_val = 0; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcadd", scale); + scale = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); if (!lhs_success) { php_warning("First parameter \"%s\" in function bcadd is not a number", lhs.str.c_str()); - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } const auto [rhs, rhs_success] = bcmath_impl_::bc_parse_number(rhs_str); if (!rhs_success) { php_warning("Second parameter \"%s\" in function bcadd is not a number", rhs.str.c_str()); - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } - return bc_add(lhs, rhs, static_cast(scale_val)); + return bc_add(lhs, rhs, static_cast(scale)); } -string f$bcsub(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { +string f$bcsub(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { - return f$bcsub(math_lib_constants.ZERO, rhs_str, scale); + return f$bcsub(math_lib_constants.ZERO, rhs_str, opt_scale); } if (rhs_str.empty()) { - return f$bcsub(lhs_str, math_lib_constants.ZERO, scale); + return f$bcsub(lhs_str, math_lib_constants.ZERO, opt_scale); } - int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcsub", scale_val); - scale_val = 0; + int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()}; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcsub", scale); + scale = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); if (!lhs_success) { php_warning("First parameter \"%s\" in function bcsub is not a number", lhs.str.c_str()); - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } auto [rhs, rhs_success] = bcmath_impl_::bc_parse_number(rhs_str); if (!rhs_success) { php_warning("Second parameter \"%s\" in function bcsub is not a number", rhs.str.c_str()); - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } rhs.n_sign *= -1; - return bc_add(lhs, rhs, static_cast(scale_val)); + return bc_add(lhs, rhs, static_cast(scale)); } -string f$bcmul(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { +string f$bcmul(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { - return f$bcmul(math_lib_constants.ZERO, rhs_str, scale); + return f$bcmul(math_lib_constants.ZERO, rhs_str, opt_scale); } if (rhs_str.empty()) { - return f$bcmul(lhs_str, math_lib_constants.ZERO, scale); + return f$bcmul(lhs_str, math_lib_constants.ZERO, opt_scale); } - int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcmul", scale_val); - scale_val = 0; + int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()}; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcmul", scale); + scale = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -763,22 +763,22 @@ string f$bcmul(const string& lhs_str, const string& rhs_str, Optional s return math_lib_constants.ZERO; } - return bc_mul_positive(lhs, rhs, static_cast(scale_val), lhs.n_sign * rhs.n_sign); + return bc_mul_positive(lhs, rhs, static_cast(scale), lhs.n_sign * rhs.n_sign); } -int64_t f$bccomp(const string& lhs_str, const string& rhs_str, Optional scale) noexcept { +int64_t f$bccomp(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept { const auto& math_lib_constants{MathLibConstants::get()}; if (lhs_str.empty()) { - return f$bccomp(math_lib_constants.ZERO, rhs_str, scale); + return f$bccomp(math_lib_constants.ZERO, rhs_str, opt_scale); } if (rhs_str.empty()) { - return f$bccomp(lhs_str, math_lib_constants.ZERO, scale); + return f$bccomp(lhs_str, math_lib_constants.ZERO, opt_scale); } - int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bccomp", scale_val); - scale_val = 0; + int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()}; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bccomp", scale); + scale = 0; } const auto [lhs, lhs_success] = bcmath_impl_::bc_parse_number(lhs_str); @@ -797,7 +797,7 @@ int64_t f$bccomp(const string& lhs_str, const string& rhs_str, Optional return (lhs.n_sign - rhs.n_sign) / 2; } - return (1 - 2 * (lhs.n_sign < 0)) * bc_comp(lhs, rhs, static_cast(scale_val)); + return (1 - 2 * (lhs.n_sign < 0)) * bc_comp(lhs, rhs, static_cast(scale)); } // In some places we need to check if the number NUM is almost zero. @@ -892,28 +892,28 @@ static std::pair bc_sqrt(const bcmath_impl_::BcNum& num, int scale return {bc_div_positive(guess, math_lib_constants.BC_NUM_ONE, rscale, 1), true}; } -string f$bcsqrt(const string& num_str, Optional scale) noexcept { - int64_t scale_val{!scale.has_value() ? MathLibContext::get().bc_scale : scale.val()}; - if (scale_val < 0) { - php_warning("Wrong parameter scale = %" PRIi64 " in function bcsqrt", scale_val); - scale_val = 0; +string f$bcsqrt(const string& num_str, Optional opt_scale) noexcept { + int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()}; + if (scale < 0) { + php_warning("Wrong parameter scale = %" PRIi64 " in function bcsqrt", scale); + scale = 0; } if (num_str.empty()) { - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } const auto [num, parse_success] = bcmath_impl_::bc_parse_number(num_str); if (!parse_success) { php_warning("First parameter \"%s\" in function bcsqrt is not a number", num.str.c_str()); - return bc_zero(static_cast(scale_val)); + return bc_zero(static_cast(scale)); } - auto [sqrt, sqrt_success] = bc_sqrt(num, scale_val); + auto [sqrt, sqrt_success] = bc_sqrt(num, scale); if (!sqrt_success) { php_warning("bcsqrt(): Square root of negative number"); return {}; } const bcmath_impl_::BcNum& sqrt_bc_num = bcmath_impl_::bc_parse_number(sqrt).first; - return bc_div_positive(sqrt_bc_num, MathLibConstants::get().BC_NUM_ONE, scale_val, 1); + return bc_div_positive(sqrt_bc_num, MathLibConstants::get().BC_NUM_ONE, scale, 1); } From 1753aae4218b8894c5225d5cba3ef619764899e6 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Thu, 6 Nov 2025 17:26:09 +0300 Subject: [PATCH 3/4] change signature in .txt in kphp-light --- .../kphp-light/stdlib/math-functions.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/builtin-functions/kphp-light/stdlib/math-functions.txt b/builtin-functions/kphp-light/stdlib/math-functions.txt index ac7e0198cd..ec13138059 100644 --- a/builtin-functions/kphp-light/stdlib/math-functions.txt +++ b/builtin-functions/kphp-light/stdlib/math-functions.txt @@ -140,23 +140,23 @@ function min (...$a) ::: ^1[*]; */ function max (...$a) ::: ^1[*]; -function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; +function bcscale ($scale ::: int) ::: void; -function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: int; +function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; -function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; +function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; -function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; +function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; -function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; +function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; -function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; +function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; -function bcscale ($scale ::: int) ::: void; +function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string; -function bcsqrt($num ::: string, $scale ::: int = PHP_INT_MIN): string; +function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: int; -function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string; +function bcsqrt($num ::: string, $scale ::: ?int = null): string; function is_nan ($v ::: float) ::: bool; From f2366e8cbe9159a7945ccf67c70d0808d02898b9 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Thu, 6 Nov 2025 17:29:09 +0300 Subject: [PATCH 4/4] minifix --- runtime-common/stdlib/math/bcmath-functions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime-common/stdlib/math/bcmath-functions.cpp b/runtime-common/stdlib/math/bcmath-functions.cpp index 81bbce6b16..ae91774d57 100644 --- a/runtime-common/stdlib/math/bcmath-functions.cpp +++ b/runtime-common/stdlib/math/bcmath-functions.cpp @@ -672,7 +672,7 @@ string f$bcpow(const string& lhs_str, const string& rhs_str, Optional o } } // just scale result - return f$bcadd(result, math_lib_constants.ZERO, opt_scale); + return f$bcadd(result, math_lib_constants.ZERO, scale); } string f$bcadd(const string& lhs_str, const string& rhs_str, Optional opt_scale) noexcept {