Skip to content

Commit 32225c7

Browse files
authored
changing bcmath signature: change scale type to Optional<int64_t> (#1444)
* add fixes, del extra files * rename param, var * change signature in .txt in kphp-light * minifix
1 parent 90b8119 commit 32225c7

4 files changed

Lines changed: 50 additions & 67 deletions

File tree

builtin-functions/kphp-full/_functions.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,14 +784,14 @@ define('PHP_ROUND_HALF_EVEN', 123423145);
784784
define('PHP_ROUND_HALF_ODD', 123423146);
785785

786786
function bcscale ($scale ::: int) ::: void;
787-
function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
788-
function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
789-
function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
790-
function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
791-
function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
792-
function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
793-
function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: int;
794-
function bcsqrt($num ::: string, $scale ::: int = PHP_INT_MIN): string;
787+
function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
788+
function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
789+
function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
790+
function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
791+
function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
792+
function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
793+
function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: int;
794+
function bcsqrt($num ::: string, $scale ::: ?int = null): string;
795795

796796
function mysqli_errno(\mysqli $dn) ::: int;
797797
function mysqli_error(\mysqli $dn) ::: string;

builtin-functions/kphp-light/stdlib/math-functions.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,23 @@ function min (...$a) ::: ^1[*];
140140
*/
141141
function max (...$a) ::: ^1[*];
142142

143-
function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
143+
function bcscale ($scale ::: int) ::: void;
144144

145-
function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: int;
145+
function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
146146

147-
function bcdiv ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
147+
function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
148148

149-
function bcmod ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
149+
function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
150150

151-
function bcpow ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
151+
function bcadd ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
152152

153-
function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
153+
function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
154154

155-
function bcscale ($scale ::: int) ::: void;
155+
function bcmul ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: string;
156156

157-
function bcsqrt($num ::: string, $scale ::: int = PHP_INT_MIN): string;
157+
function bccomp ($lhs ::: string, $rhs ::: string, $scale ::: ?int = null) ::: int;
158158

159-
function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;
159+
function bcsqrt($num ::: string, $scale ::: ?int = null): string;
160160

161161
function is_nan ($v ::: float) ::: bool;
162162

runtime-common/stdlib/math/bcmath-functions.cpp

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,10 @@ void f$bcscale(int64_t scale) noexcept {
490490
}
491491
}
492492

493-
string f$bcdiv(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept {
493+
string f$bcdiv(const string& lhs_str, const string& rhs_str, Optional<int64_t> opt_scale) noexcept {
494494
auto& math_lib_context{MathLibContext::get()};
495495
const auto& math_lib_constants{MathLibConstants::get()};
496-
if (scale == std::numeric_limits<int64_t>::min()) {
497-
scale = math_lib_context.bc_scale;
498-
}
496+
int64_t scale{!opt_scale.has_value() ? math_lib_context.bc_scale : opt_scale.val()};
499497
if (scale < 0) {
500498
php_warning("Wrong parameter scale = %" PRIi64 " in function bcdiv", scale);
501499
scale = 0;
@@ -533,12 +531,10 @@ static string scale_num(const string& num, int64_t scale) noexcept {
533531
return num;
534532
}
535533

536-
string f$bcmod(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept {
534+
string f$bcmod(const string& lhs_str, const string& rhs_str, Optional<int64_t> opt_scale) noexcept {
537535
auto& math_lib_context{MathLibContext::get()};
538536
const auto& math_lib_constants{MathLibConstants::get()};
539-
if (scale == std::numeric_limits<int64_t>::min()) {
540-
scale = math_lib_context.bc_scale;
541-
}
537+
int64_t scale{!opt_scale.has_value() ? math_lib_context.bc_scale : opt_scale.val()};
542538
if (scale < 0) {
543539
php_warning("Wrong parameter scale = %" PRIi64 " in function bcmod", scale);
544540
scale = 0;
@@ -600,12 +596,10 @@ static std::pair<std::int64_t, bool> bc_num2int(const bcmath_impl_::BcNum& num)
600596
return {ingeger, true};
601597
}
602598

603-
string f$bcpow(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept {
599+
string f$bcpow(const string& lhs_str, const string& rhs_str, Optional<int64_t> opt_scale) noexcept {
604600
auto& math_lib_context{MathLibContext::get()};
605601
const auto& math_lib_constants{MathLibConstants::get()};
606-
if (scale == std::numeric_limits<int64_t>::min()) {
607-
scale = math_lib_context.bc_scale;
608-
}
602+
int64_t scale{!opt_scale.has_value() ? math_lib_context.bc_scale : opt_scale.val()};
609603
if (scale < 0) {
610604
php_warning("Wrong parameter scale = %" PRIi64 " in function bcpow", scale);
611605
scale = 0;
@@ -681,18 +675,17 @@ string f$bcpow(const string& lhs_str, const string& rhs_str, int64_t scale) noex
681675
return f$bcadd(result, math_lib_constants.ZERO, scale);
682676
}
683677

684-
string f$bcadd(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept {
678+
string f$bcadd(const string& lhs_str, const string& rhs_str, Optional<int64_t> opt_scale) noexcept {
685679
const auto& math_lib_constants{MathLibConstants::get()};
686680
if (lhs_str.empty()) {
687-
return f$bcadd(math_lib_constants.ZERO, rhs_str, scale);
681+
return f$bcadd(math_lib_constants.ZERO, rhs_str, opt_scale);
688682
}
689683
if (rhs_str.empty()) {
690-
return f$bcadd(lhs_str, math_lib_constants.ZERO, scale);
684+
return f$bcadd(lhs_str, math_lib_constants.ZERO, opt_scale);
691685
}
692686

693-
if (scale == std::numeric_limits<int64_t>::min()) {
694-
scale = MathLibContext::get().bc_scale;
695-
}
687+
int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()};
688+
696689
if (scale < 0) {
697690
php_warning("Wrong parameter scale = %" PRIi64 " in function bcadd", scale);
698691
scale = 0;
@@ -713,18 +706,15 @@ string f$bcadd(const string& lhs_str, const string& rhs_str, int64_t scale) noex
713706
return bc_add(lhs, rhs, static_cast<int>(scale));
714707
}
715708

716-
string f$bcsub(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept {
709+
string f$bcsub(const string& lhs_str, const string& rhs_str, Optional<int64_t> opt_scale) noexcept {
717710
const auto& math_lib_constants{MathLibConstants::get()};
718711
if (lhs_str.empty()) {
719-
return f$bcsub(math_lib_constants.ZERO, rhs_str, scale);
712+
return f$bcsub(math_lib_constants.ZERO, rhs_str, opt_scale);
720713
}
721714
if (rhs_str.empty()) {
722-
return f$bcsub(lhs_str, math_lib_constants.ZERO, scale);
723-
}
724-
725-
if (scale == std::numeric_limits<int64_t>::min()) {
726-
scale = MathLibContext::get().bc_scale;
715+
return f$bcsub(lhs_str, math_lib_constants.ZERO, opt_scale);
727716
}
717+
int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()};
728718
if (scale < 0) {
729719
php_warning("Wrong parameter scale = %" PRIi64 " in function bcsub", scale);
730720
scale = 0;
@@ -747,18 +737,15 @@ string f$bcsub(const string& lhs_str, const string& rhs_str, int64_t scale) noex
747737
return bc_add(lhs, rhs, static_cast<int>(scale));
748738
}
749739

750-
string f$bcmul(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept {
740+
string f$bcmul(const string& lhs_str, const string& rhs_str, Optional<int64_t> opt_scale) noexcept {
751741
const auto& math_lib_constants{MathLibConstants::get()};
752742
if (lhs_str.empty()) {
753-
return f$bcmul(math_lib_constants.ZERO, rhs_str, scale);
743+
return f$bcmul(math_lib_constants.ZERO, rhs_str, opt_scale);
754744
}
755745
if (rhs_str.empty()) {
756-
return f$bcmul(lhs_str, math_lib_constants.ZERO, scale);
757-
}
758-
759-
if (scale == std::numeric_limits<int64_t>::min()) {
760-
scale = MathLibContext::get().bc_scale;
746+
return f$bcmul(lhs_str, math_lib_constants.ZERO, opt_scale);
761747
}
748+
int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()};
762749
if (scale < 0) {
763750
php_warning("Wrong parameter scale = %" PRIi64 " in function bcmul", scale);
764751
scale = 0;
@@ -779,18 +766,16 @@ string f$bcmul(const string& lhs_str, const string& rhs_str, int64_t scale) noex
779766
return bc_mul_positive(lhs, rhs, static_cast<int>(scale), lhs.n_sign * rhs.n_sign);
780767
}
781768

782-
int64_t f$bccomp(const string& lhs_str, const string& rhs_str, int64_t scale) noexcept {
769+
int64_t f$bccomp(const string& lhs_str, const string& rhs_str, Optional<int64_t> opt_scale) noexcept {
783770
const auto& math_lib_constants{MathLibConstants::get()};
784771
if (lhs_str.empty()) {
785-
return f$bccomp(math_lib_constants.ZERO, rhs_str, scale);
772+
return f$bccomp(math_lib_constants.ZERO, rhs_str, opt_scale);
786773
}
787774
if (rhs_str.empty()) {
788-
return f$bccomp(lhs_str, math_lib_constants.ZERO, scale);
775+
return f$bccomp(lhs_str, math_lib_constants.ZERO, opt_scale);
789776
}
790777

791-
if (scale == std::numeric_limits<int64_t>::min()) {
792-
scale = MathLibContext::get().bc_scale;
793-
}
778+
int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()};
794779
if (scale < 0) {
795780
php_warning("Wrong parameter scale = %" PRIi64 " in function bccomp", scale);
796781
scale = 0;
@@ -907,10 +892,8 @@ static std::pair<string, bool> bc_sqrt(const bcmath_impl_::BcNum& num, int scale
907892
return {bc_div_positive(guess, math_lib_constants.BC_NUM_ONE, rscale, 1), true};
908893
}
909894

910-
string f$bcsqrt(const string& num_str, int64_t scale) noexcept {
911-
if (scale == std::numeric_limits<int64_t>::min()) {
912-
scale = MathLibContext::get().bc_scale;
913-
}
895+
string f$bcsqrt(const string& num_str, Optional<int64_t> opt_scale) noexcept {
896+
int64_t scale{!opt_scale.has_value() ? MathLibContext::get().bc_scale : opt_scale.val()};
914897
if (scale < 0) {
915898
php_warning("Wrong parameter scale = %" PRIi64 " in function bcsqrt", scale);
916899
scale = 0;

runtime-common/stdlib/math/bcmath-functions.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ std::pair<BcNum, bool> bc_parse_number(const string& num) noexcept;
1515

1616
void f$bcscale(int64_t scale) noexcept;
1717

18-
string f$bcdiv(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
18+
string f$bcdiv(const string& lhs_str, const string& rhs_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;
1919

20-
string f$bcmod(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
20+
string f$bcmod(const string& lhs_str, const string& rhs_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;
2121

22-
string f$bcpow(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
22+
string f$bcpow(const string& lhs_str, const string& rhs_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;
2323

24-
string f$bcadd(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
24+
string f$bcadd(const string& lhs_str, const string& rhs_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;
2525

26-
string f$bcsub(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
26+
string f$bcsub(const string& lhs_str, const string& rhs_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;
2727

28-
string f$bcmul(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
28+
string f$bcmul(const string& lhs_str, const string& rhs_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;
2929

30-
int64_t f$bccomp(const string& lhs_str, const string& rhs_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
30+
int64_t f$bccomp(const string& lhs_str, const string& rhs_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;
3131

32-
string f$bcsqrt(const string& num_str, int64_t scale = std::numeric_limits<int64_t>::min()) noexcept;
32+
string f$bcsqrt(const string& num_str, Optional<int64_t> scale = Optional<int64_t>{}) noexcept;

0 commit comments

Comments
 (0)