-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vadim Sadokhov
committed
Oct 29, 2024
1 parent
cdb8018
commit dd639bc
Showing
5 changed files
with
157 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// 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 <stdlib.h> | ||
|
||
mixed f$abs(const mixed &v) { | ||
mixed num = v.to_numeric(); | ||
if (num.is_int()) { | ||
return std::abs(num.to_int()); | ||
} | ||
return fabs(num.to_float()); | ||
} | ||
|
||
int64_t f$abs(int64_t v) { | ||
return std::abs(v); | ||
} | ||
|
||
double f$abs(double v) { | ||
return std::abs(v); | ||
} | ||
|
||
int64_t f$abs(const Optional<int64_t> &v) { | ||
return f$abs(val(v)); | ||
} | ||
|
||
int64_t f$abs(const Optional<bool> &v) { | ||
return f$abs(static_cast<int64_t>(val(v))); | ||
} | ||
|
||
double f$abs(const Optional<double> &v) { | ||
return f$abs(val(v)); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2024 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#pragma once | ||
|
||
#include "runtime-common/core/runtime-core.h" | ||
|
||
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$ceil(double v) { | ||
return ceil(v); | ||
} | ||
|
||
inline double f$cos(double v) { | ||
return cos(v); | ||
} | ||
|
||
inline double f$deg2rad(double v) { | ||
return v * M_PI / 180; | ||
} | ||
|
||
inline double f$floor(double v) { | ||
return floor(v); | ||
} | ||
|
||
inline double f$log(double v) { | ||
if (v <= 0.0) { | ||
return 0.0; | ||
} | ||
return log(v); | ||
} | ||
|
||
inline 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); | ||
} | ||
|
||
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)...); | ||
} | ||
|
||
inline double f$pi() { | ||
return M_PI; | ||
} | ||
|
||
inline double f$round(double v, int64_t precision = 0) { | ||
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; | ||
} | ||
|
||
inline double f$sqrt(double v) { | ||
if (v < 0) { | ||
return 0.0; | ||
} | ||
return sqrt(v); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters