Skip to content

Commit

Permalink
add math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Sadokhov committed Oct 29, 2024
1 parent cdb8018 commit dd639bc
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 69 deletions.
8 changes: 0 additions & 8 deletions builtin-functions/kphp-light/unsupported/math.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ function atan ($v ::: float) ::: float;
*/
function atan2 ($y ::: float, $x ::: float) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function ceil ($v ::: float) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function cos ($v ::: float) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function deg2rad ($v ::: float) ::: float;
Expand All @@ -58,7 +55,6 @@ function deg2rad ($v ::: float) ::: float;
*/
function exp ($v ::: float) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function floor ($v ::: float) ::: float;
Expand All @@ -68,12 +64,10 @@ function floor ($v ::: float) ::: float;
*/
function fmod ($x ::: float, $y ::: float) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function log ($v ::: float, $base ::: float = 2.7182818284590452353602874713527) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function round ($v ::: float, $precision ::: int = 0) ::: float;
Expand All @@ -83,7 +77,6 @@ function round ($v ::: float, $precision ::: int = 0) ::: float;
*/
function sin ($v ::: float) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function sqrt ($v ::: float) ::: float;
Expand All @@ -93,7 +86,6 @@ function sqrt ($v ::: float) ::: float;
*/
function tan ($v ::: float) ::: float;
/**
* @kphp-extern-func-info generate-stub
* @kphp-pure-function
*/
function pi() ::: float;
Expand Down
37 changes: 37 additions & 0 deletions runtime-light/stdlib/math/math-functions.cpp
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));
}


119 changes: 119 additions & 0 deletions runtime-light/stdlib/math/math-functions.h
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);
}
61 changes: 0 additions & 61 deletions runtime-light/stdlib/math/math.h

This file was deleted.

1 change: 1 addition & 0 deletions runtime-light/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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

0 comments on commit dd639bc

Please sign in to comment.