Skip to content

Commit

Permalink
move some math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Sadokhov committed Nov 2, 2024
1 parent 2c136e1 commit 5dcba1c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
#include <cstdlib>

mixed f$abs(const mixed &v) {
mixed num = v.to_numeric();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<class T>
inline T f$min(const array<T> &a);

template<class T>
inline T f$max(const array<T> &a);

template<class T>
inline T f$min(const T &arg1);

template<class T, class... Args>
inline T f$min(const T &arg1, const T &arg2, Args&&... args);

template<class T>
inline T f$max(const T &arg1);

template<class T, class... Args>
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);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion runtime-common/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
@@ -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})
7 changes: 0 additions & 7 deletions runtime-light/stdlib/math/random-functions.h

This file was deleted.

1 change: 0 additions & 1 deletion runtime-light/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions runtime/math_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
146 changes: 1 addition & 145 deletions runtime/math_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,43 +39,12 @@ Optional<int64_t> f$random_int(int64_t l, int64_t r) noexcept;

Optional<string> f$random_bytes(int64_t length) noexcept;


template<class T>
inline T f$min(const array<T> &a);

template<class T>
inline T f$max(const array<T> &a);

template<class T>
inline T f$min(const T &arg1);

template<class T, class... Args>
inline T f$min(const T &arg1, const T &arg2, Args&&... args);

template<class T>
inline T f$max(const T &arg1);

template<class T, class... Args>
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<int64_t> &v);

int64_t f$abs(const Optional<bool> &v);

double f$abs(const Optional<double> &v);

inline double f$acos(double v);

inline double f$atan(double v);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -135,61 +87,6 @@ void init_math_functions() noexcept;
*
*/


template<class T>
T f$min(const array<T> &a) {
if (a.count() == 0) {
php_warning("Empty array specified to function min");
return T();
}

typename array<T>::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<class T>
T f$max(const array<T> &a) {
if (a.count() == 0) {
php_warning("Empty array specified to function max");
return T();
}

typename array<T>::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<class T>
T f$min(const T &arg1) {
return arg1;
}

template<class T, class ...Args>
T f$min(const T &arg1, const T &arg2, Args&& ...args) {
return f$min<T>(lt(arg1, arg2) ? arg1 : arg2, std::forward<Args>(args)...);
}

template<class T>
T f$max(const T &arg1) {
return arg1;
}

template<class T, class ...Args>
T f$max(const T &arg1, const T &arg2, Args&& ...args) {
return f$max<T>(lt(arg2, arg1) ? arg1 : arg2, std::forward<Args>(args)...);
}

double f$acos(double v) {
return acos(v);
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 5dcba1c

Please sign in to comment.