Skip to content

Commit b0ad768

Browse files
authored
move math functions to runtime-common (#1420)
* move math functions to runtime-common * enable tests * reformat math-functions.txt
1 parent 1dc4b82 commit b0ad768

12 files changed

Lines changed: 146 additions & 138 deletions

File tree

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

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ function hexdec ($number ::: string): int;
4141
*/
4242
function abs ($v ::: mixed) ::: ^1 | int;
4343

44+
/**
45+
* @kphp-extern-func-info
46+
* @kphp-pure-function
47+
*/
48+
function acos ($v ::: float) ::: float;
49+
50+
/**
51+
* @kphp-extern-func-info
52+
* @kphp-pure-function
53+
*/
54+
function atan ($v ::: float) ::: float;
55+
56+
/**
57+
* @kphp-extern-func-info
58+
* @kphp-pure-function
59+
*/
60+
function atan2 ($y ::: float, $x ::: float) ::: float;
61+
62+
/** @kphp-extern-func-info */
63+
function bindec ($number ::: string) ::: int;
64+
4465
/**
4566
* @kphp-pure-function
4667
*/
@@ -51,16 +72,31 @@ function ceil ($v ::: float) ::: float;
5172
*/
5273
function cos ($v ::: float) ::: float;
5374

75+
/** @kphp-extern-func-info */
76+
function decbin ($number ::: int) ::: string;
77+
5478
/**
5579
* @kphp-pure-function
5680
*/
5781
function deg2rad ($v ::: float) ::: float;
5882

83+
/**
84+
* @kphp-extern-func-info
85+
* @kphp-pure-function
86+
*/
87+
function exp ($v ::: float) ::: float;
88+
5989
/**
6090
* @kphp-pure-function
6191
*/
6292
function floor ($v ::: float) ::: float;
6393

94+
/** @kphp-extern-func-info */
95+
function is_finite ($v ::: float) ::: bool;
96+
97+
/** @kphp-extern-func-info */
98+
function is_infinite ($v ::: float) ::: bool;
99+
64100
/**
65101
* @kphp-pure-function
66102
*/
@@ -71,11 +107,23 @@ function log ($v ::: float, $base ::: float = 2.7182818284590452353602874713527)
71107
*/
72108
function round ($v ::: float, $precision ::: int = 0) ::: float;
73109

110+
/**
111+
* @kphp-extern-func-info
112+
* @kphp-pure-function
113+
*/
114+
function sin ($v ::: float) ::: float;
115+
74116
/**
75117
* @kphp-pure-function
76118
*/
77119
function sqrt ($v ::: float) ::: float;
78120

121+
/**
122+
* @kphp-extern-func-info
123+
* @kphp-pure-function
124+
*/
125+
function tan ($v ::: float) ::: float;
126+
79127
/**
80128
* @kphp-pure-function
81129
*/
@@ -116,41 +164,11 @@ function levenshtein ($str1 ::: string, $str2 ::: string) ::: int;
116164

117165
// === UNSUPPORTED ===
118166

119-
/**
120-
* @kphp-extern-func-info stub generation-required
121-
* @kphp-pure-function
122-
*/
123-
function acos ($v ::: float) ::: float;
124-
/**
125-
* @kphp-extern-func-info stub generation-required
126-
* @kphp-pure-function
127-
*/
128-
function atan ($v ::: float) ::: float;
129-
/**
130-
* @kphp-extern-func-info stub generation-required
131-
* @kphp-pure-function
132-
*/
133-
function atan2 ($y ::: float, $x ::: float) ::: float;
134-
/**
135-
* @kphp-extern-func-info stub generation-required
136-
* @kphp-pure-function
137-
*/
138-
function exp ($v ::: float) ::: float;
139167
/**
140168
* @kphp-extern-func-info stub generation-required
141169
* @kphp-pure-function
142170
*/
143171
function fmod ($x ::: float, $y ::: float) ::: float;
144-
/**
145-
* @kphp-extern-func-info stub generation-required
146-
* @kphp-pure-function
147-
*/
148-
function sin ($v ::: float) ::: float;
149-
/**
150-
* @kphp-extern-func-info stub generation-required
151-
* @kphp-pure-function
152-
*/
153-
function tan ($v ::: float) ::: float;
154172
/**
155173
* @kphp-extern-func-info stub generation-required
156174
* @kphp-pure-function
@@ -168,17 +186,7 @@ define('PHP_ROUND_HALF_DOWN', 123423144);
168186
define('PHP_ROUND_HALF_EVEN', 123423145);
169187
define('PHP_ROUND_HALF_ODD', 123423146);
170188

171-
/** @kphp-extern-func-info stub generation-required */
172-
function is_finite ($v ::: float) ::: bool;
173-
/** @kphp-extern-func-info stub generation-required */
174-
function is_infinite ($v ::: float) ::: bool;
175189
/** @kphp-extern-func-info stub generation-required */
176190
function random_int($l ::: int, $r ::: int) ::: int | false;
177191
/** @kphp-extern-func-info stub generation-required */
178192
function random_bytes($length ::: int) ::: string | false;
179-
180-
/** @kphp-extern-func-info stub generation-required */
181-
function bindec ($number ::: string) ::: int;
182-
183-
/** @kphp-extern-func-info stub generation-required */
184-
function decbin ($number ::: int) ::: string;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,44 @@
1313
#include "runtime-common/stdlib/string/string-context.h"
1414
#include "runtime-common/stdlib/string/string-functions.h"
1515

16+
int64_t f$bindec(const string& number) noexcept {
17+
uint64_t v = 0;
18+
bool bad_str_param = number.empty();
19+
bool overflow = false;
20+
for (string::size_type i = 0; i < number.size(); i++) {
21+
const char c = number[i];
22+
if (likely(vk::any_of_equal(c, '0', '1'))) {
23+
v = math_functions_impl_::mult_and_add<2>(v, static_cast<uint8_t>(c - '0'), overflow);
24+
} else {
25+
bad_str_param = true;
26+
}
27+
}
28+
29+
if (unlikely(bad_str_param)) {
30+
php_warning("Wrong parameter '%s' in function bindec", number.c_str());
31+
}
32+
if (unlikely(overflow)) {
33+
php_warning("Integer overflow on converting '%s' in function bindec, "
34+
"the result will be different from PHP",
35+
number.c_str());
36+
}
37+
return static_cast<int64_t>(v);
38+
}
39+
40+
string f$decbin(int64_t number) noexcept {
41+
auto v = static_cast<uint64_t>(number);
42+
43+
char s[66];
44+
int i = 65;
45+
46+
do {
47+
s[--i] = static_cast<char>((v & 1) + '0');
48+
v >>= 1;
49+
} while (v > 0);
50+
51+
return {s + i, static_cast<string::size_type>(65 - i)};
52+
}
53+
1654
string f$dechex(int64_t number) noexcept {
1755
auto v = static_cast<uint64_t>(number);
1856

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,30 @@ uint64_t mult_and_add(uint64_t x, uint8_t y, bool& overflow) noexcept {
2222

2323
} // namespace math_functions_impl_
2424

25+
inline double f$acos(double v) noexcept;
26+
27+
inline double f$atan(double v) noexcept;
28+
29+
inline double f$atan2(double y, double x) noexcept;
30+
31+
int64_t f$bindec(const string& number) noexcept;
32+
2533
inline double f$ceil(double v) noexcept;
2634

2735
inline double f$cos(double v) noexcept;
2836

37+
string f$decbin(int64_t number) noexcept;
38+
2939
inline double f$deg2rad(double v) noexcept;
3040

41+
inline double f$exp(double v) noexcept;
42+
3143
inline double f$floor(double v) noexcept;
3244

45+
inline bool f$is_finite(double v) noexcept;
46+
47+
inline bool f$is_infinite(double v) noexcept;
48+
3349
inline double f$log(double v) noexcept;
3450

3551
inline double f$log(double v, double base) noexcept;
@@ -56,8 +72,12 @@ inline double f$pi() noexcept;
5672

5773
inline double f$round(double v, int64_t precision = 0) noexcept;
5874

75+
inline double f$sin(double v) noexcept;
76+
5977
inline double f$sqrt(double v) noexcept;
6078

79+
inline double f$tan(double v) noexcept;
80+
6181
inline mixed f$abs(const mixed& v) noexcept {
6282
mixed num = v.to_numeric();
6383
if (num.is_int()) {
@@ -86,6 +106,18 @@ inline double f$abs(const Optional<double>& v) noexcept {
86106
return f$abs(val(v));
87107
}
88108

109+
inline double f$acos(double v) noexcept {
110+
return acos(v);
111+
}
112+
113+
inline double f$atan(double v) noexcept {
114+
return atan(v);
115+
}
116+
117+
inline double f$atan2(double y, double x) noexcept {
118+
return atan2(y, x);
119+
}
120+
89121
inline double f$ceil(double v) noexcept {
90122
return ceil(v);
91123
}
@@ -98,10 +130,23 @@ inline double f$deg2rad(double v) noexcept {
98130
return v * M_PI / 180;
99131
}
100132

133+
inline double f$exp(double v) noexcept {
134+
return exp(v);
135+
}
136+
101137
inline double f$floor(double v) noexcept {
102138
return floor(v);
103139
}
104140

141+
inline bool f$is_finite(double v) noexcept {
142+
int v_class = std::fpclassify(v);
143+
return (v_class != FP_NAN && v_class != FP_INFINITE);
144+
}
145+
146+
inline bool f$is_infinite(double v) noexcept {
147+
return (std::fpclassify(v) == FP_INFINITE);
148+
}
149+
105150
inline double f$log(double v) noexcept {
106151
if (v <= 0.0) {
107152
return 0.0;
@@ -188,13 +233,21 @@ inline double f$round(double v, int64_t precision) noexcept {
188233
return round(v * mul) / mul;
189234
}
190235

236+
inline double f$sin(double v) noexcept {
237+
return sin(v);
238+
}
239+
191240
inline double f$sqrt(double v) noexcept {
192241
if (v < 0) {
193242
return 0.0;
194243
}
195244
return sqrt(v);
196245
}
197246

247+
inline double f$tan(double v) noexcept {
248+
return tan(v);
249+
}
250+
198251
int64_t f$hexdec(const string& number) noexcept;
199252

200253
string f$dechex(int64_t number) noexcept;

runtime/math_functions.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,6 @@ int64_t secure_rand_buf(char* const buf, int64_t length) noexcept {
3434
}
3535
} // namespace
3636

37-
int64_t f$bindec(const string& number) noexcept {
38-
uint64_t v = 0;
39-
bool bad_str_param = number.empty();
40-
bool overflow = false;
41-
for (string::size_type i = 0; i < number.size(); i++) {
42-
const char c = number[i];
43-
if (likely(vk::any_of_equal(c, '0', '1'))) {
44-
v = math_functions_impl_::mult_and_add<2>(v, static_cast<uint8_t>(c - '0'), overflow);
45-
} else {
46-
bad_str_param = true;
47-
}
48-
}
49-
50-
if (unlikely(bad_str_param)) {
51-
php_warning("Wrong parameter '%s' in function bindec", number.c_str());
52-
}
53-
if (unlikely(overflow)) {
54-
php_warning("Integer overflow on converting '%s' in function bindec, "
55-
"the result will be different from PHP",
56-
number.c_str());
57-
}
58-
return static_cast<int64_t>(v);
59-
}
60-
61-
string f$decbin(int64_t number) noexcept {
62-
auto v = static_cast<uint64_t>(number);
63-
64-
char s[66];
65-
int i = 65;
66-
67-
do {
68-
s[--i] = static_cast<char>((v & 1) + '0');
69-
v >>= 1;
70-
} while (v > 0);
71-
72-
return {s + i, static_cast<string::size_type>(65 - i)};
73-
}
74-
7537
double f$lcg_value() {
7638
dl::enter_critical_section(); // OK
7739

0 commit comments

Comments
 (0)