diff --git a/runtime-light/stdlib/math/math-functions.cpp b/runtime-common/stdlib/math/math-functions.cpp similarity index 89% rename from runtime-light/stdlib/math/math-functions.cpp rename to runtime-common/stdlib/math/math-functions.cpp index e1e7b5426..144a912f6 100644 --- a/runtime-light/stdlib/math/math-functions.cpp +++ b/runtime-common/stdlib/math/math-functions.cpp @@ -2,9 +2,9 @@ // Copyright (c) 2024 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime-light/stdlib/math/math-functions.h" +#include "runtime-common/stdlib/math/math-functions.h" -#include +#include mixed f$abs(const mixed &v) { mixed num = v.to_numeric(); diff --git a/runtime-light/stdlib/math/math-functions.h b/runtime-common/stdlib/math/math-functions.h similarity index 75% rename from runtime-light/stdlib/math/math-functions.h rename to runtime-common/stdlib/math/math-functions.h index 5465cb1b4..ed99ebe88 100644 --- a/runtime-light/stdlib/math/math-functions.h +++ b/runtime-common/stdlib/math/math-functions.h @@ -6,6 +6,42 @@ #include "runtime-common/core/runtime-core.h" +inline double f$ceil(double v); + +inline double f$cos(double v); + +inline double f$deg2rad(double v); + +inline double f$floor(double v); + +inline double f$log(double v); + +inline double f$log(double v, double base); + +template +inline T f$min(const array &a); + +template +inline T f$max(const array &a); + +template +inline T f$min(const T &arg1); + +template +inline T f$min(const T &arg1, const T &arg2, Args&&... args); + +template +inline T f$max(const T &arg1); + +template +inline T f$max(const T &arg1, const T &arg2, Args&&... args); + +inline double f$pi(); + +inline double f$round(double v, int64_t precision = 0); + +inline double f$sqrt(double v); + mixed f$abs(const mixed &v); int64_t f$abs(int64_t v); double f$abs(double v); @@ -101,7 +137,7 @@ inline double f$pi() { return M_PI; } -inline double f$round(double v, int64_t precision = 0) { +inline double f$round(double v, int64_t precision) { if (std::abs(precision) > 100) { php_warning("Wrong parameter precision (%" PRIi64 ") in function round", precision); return v; diff --git a/runtime-common/stdlib/stdlib.cmake b/runtime-common/stdlib/stdlib.cmake index aaa11259a..934ba1117 100644 --- a/runtime-common/stdlib/stdlib.cmake +++ b/runtime-common/stdlib/stdlib.cmake @@ -1,3 +1,4 @@ prepend(STDLIB_STRING stdlib/string/ string-functions.cpp) +prepend(STDLIB_MATH stdlib/math/ math-functions.cpp) -set(STDLIB_SRC "${STDLIB_STRING}") +set(STDLIB_SRC ${STDLIB_STRING} ${STDLIB_MATH}) diff --git a/runtime-light/stdlib/math/random-functions.h b/runtime-light/stdlib/math/random-functions.h deleted file mode 100644 index ba5dfa3ec..000000000 --- a/runtime-light/stdlib/math/random-functions.h +++ /dev/null @@ -1,7 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2024 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include -#include - diff --git a/runtime-light/stdlib/stdlib.cmake b/runtime-light/stdlib/stdlib.cmake index 40fb78f45..31d2714f4 100644 --- a/runtime-light/stdlib/stdlib.cmake +++ b/runtime-light/stdlib/stdlib.cmake @@ -11,7 +11,6 @@ prepend( hash/hash-functions.cpp job-worker/job-worker-api.cpp job-worker/job-worker-client-context.cpp - math/math-functions.cpp output/output-buffer.cpp output/print-functions.cpp regex/regex-context.cpp diff --git a/runtime/math_functions.cpp b/runtime/math_functions.cpp index 928b5ad87..6e358065f 100644 --- a/runtime/math_functions.cpp +++ b/runtime/math_functions.cpp @@ -358,16 +358,6 @@ string f$base_convert(const string &number, int64_t frombase, int64_t tobase) { return result; } -double f$round(double v, int64_t precision) { - if (std::abs(precision) > 100) { - php_warning("Wrong parameter precision (%" PRIi64 ") in function round", precision); - return v; - } - - double mul = pow(10.0, (double)precision); - return round(v * mul) / mul; -} - void init_math_functions() noexcept { MTRandGenerator::get().lazy_init(); } diff --git a/runtime/math_functions.h b/runtime/math_functions.h index 9102d45fb..4da9b71a0 100644 --- a/runtime/math_functions.h +++ b/runtime/math_functions.h @@ -5,6 +5,7 @@ #pragma once #include "runtime-common/core/runtime-core.h" +#include "runtime-common/stdlib/math/math-functions.h" int64_t f$bindec(const string &number) noexcept; @@ -38,43 +39,12 @@ Optional f$random_int(int64_t l, int64_t r) noexcept; Optional f$random_bytes(int64_t length) noexcept; - -template -inline T f$min(const array &a); - -template -inline T f$max(const array &a); - -template -inline T f$min(const T &arg1); - -template -inline T f$min(const T &arg1, const T &arg2, Args&&... args); - -template -inline T f$max(const T &arg1); - -template -inline T f$max(const T &arg1, const T &arg2, Args&&... args); - // TODO REMOVE? constexpr int64_t PHP_ROUND_HALF_UP = 123423141; constexpr int64_t PHP_ROUND_HALF_DOWN = 123423144; constexpr int64_t PHP_ROUND_HALF_EVEN = 123423145; constexpr int64_t PHP_ROUND_HALF_ODD = 123423146; -mixed f$abs(const mixed &v); - -int64_t f$abs(int64_t v); - -double f$abs(double v); - -int64_t f$abs(const Optional &v); - -int64_t f$abs(const Optional &v); - -double f$abs(const Optional &v); - inline double f$acos(double v); inline double f$atan(double v); @@ -83,20 +53,12 @@ inline double f$atan2(double y, double x); string f$base_convert(const string &number, int64_t frombase, int64_t tobase); -inline double f$ceil(double v); - -inline double f$cos(double v); - inline double f$cosh(double v); inline double f$acosh(double v); -inline double f$deg2rad(double v); - inline double f$exp(double v); -inline double f$floor(double v); - inline double f$fmod(double x, double y); inline bool f$is_finite(double v); @@ -105,20 +67,10 @@ inline bool f$is_infinite(double v); inline bool f$is_nan(double v); -inline double f$log(double v); - -inline double f$log(double v, double base); - -inline double f$pi(); - -double f$round(double v, int64_t precision = 0); - inline double f$sin(double v); inline double f$sinh(double v); -inline double f$sqrt(double v); - inline double f$tan(double v); inline double f$asin(double v); @@ -135,61 +87,6 @@ void init_math_functions() noexcept; * */ - -template -T f$min(const array &a) { - if (a.count() == 0) { - php_warning("Empty array specified to function min"); - return T(); - } - - typename array::const_iterator p = a.begin(); - T res = p.get_value(); - for (++p; p != a.end(); ++p) { - if (lt(p.get_value(), res)) { - res = p.get_value(); - } - } - return res; -} - -template -T f$max(const array &a) { - if (a.count() == 0) { - php_warning("Empty array specified to function max"); - return T(); - } - - typename array::const_iterator p = a.begin(); - T res = p.get_value(); - for (++p; p != a.end(); ++p) { - if (lt(res, p.get_value())) { - res = p.get_value(); - } - } - return res; -} - -template -T f$min(const T &arg1) { - return arg1; -} - -template -T f$min(const T &arg1, const T &arg2, Args&& ...args) { - return f$min(lt(arg1, arg2) ? arg1 : arg2, std::forward(args)...); -} - -template -T f$max(const T &arg1) { - return arg1; -} - -template -T f$max(const T &arg1, const T &arg2, Args&& ...args) { - return f$max(lt(arg2, arg1) ? arg1 : arg2, std::forward(args)...); -} - double f$acos(double v) { return acos(v); } @@ -202,14 +99,6 @@ double f$atan2(double y, double x) { return atan2(y, x); } -double f$ceil(double v) { - return ceil(v); -} - -double f$cos(double v) { - return cos(v); -} - double f$cosh(double v) { return cosh(v); } @@ -218,18 +107,10 @@ double f$acosh(double v) { return acosh(v); } -double f$deg2rad(double v) { - return v * M_PI / 180; -} - double f$exp(double v) { return exp(v); } -double f$floor(double v) { - return floor(v); -} - double f$fmod(double x, double y) { if (fabs(x) > 1e100 || fabs(y) < 1e-100) { return 0.0; @@ -250,24 +131,6 @@ bool f$is_nan(double v) { return (std::fpclassify(v) == FP_NAN); } -double f$log(double v) { - if (v <= 0.0) { - return 0.0; - } - return log(v); -} - -double f$log(double v, double base) { - if (v <= 0.0 || base <= 0.0 || fabs(base - 1.0) < 1e-9) { - return 0.0; - } - return log(v) / log(base); -} - -double f$pi() { - return M_PI; -} - double f$sin(double v) { return sin(v); } @@ -276,13 +139,6 @@ double f$sinh(double v) { return sinh(v); } -double f$sqrt(double v) { - if (v < 0) { - return 0.0; - } - return sqrt(v); -} - double f$tan(double v) { return tan(v); }