/src/moddable/xs/sources/xsMath.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2016-2025 Moddable Tech, Inc. |
3 | | * |
4 | | * This file is part of the Moddable SDK Runtime. |
5 | | * |
6 | | * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This file incorporates work covered by the following copyright and |
20 | | * permission notice: |
21 | | * |
22 | | * Copyright (C) 2010-2016 Marvell International Ltd. |
23 | | * Copyright (C) 2002-2010 Kinoma, Inc. |
24 | | * |
25 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
26 | | * you may not use this file except in compliance with the License. |
27 | | * You may obtain a copy of the License at |
28 | | * |
29 | | * http://www.apache.org/licenses/LICENSE-2.0 |
30 | | * |
31 | | * Unless required by applicable law or agreed to in writing, software |
32 | | * distributed under the License is distributed on an "AS IS" BASIS, |
33 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 | | * See the License for the specific language governing permissions and |
35 | | * limitations under the License. |
36 | | */ |
37 | | |
38 | | #include "xsAll.h" |
39 | | |
40 | | void fxBuildMath(txMachine* the) |
41 | 6.57k | { |
42 | 6.57k | txSlot* slot; |
43 | 6.57k | mxPush(mxObjectPrototype); |
44 | 6.57k | slot = fxLastProperty(the, fxNewObjectInstance(the)); |
45 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_abs), 1, mxID(_abs), XS_DONT_ENUM_FLAG); |
46 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acos), 1, mxID(_acos), XS_DONT_ENUM_FLAG); |
47 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acosh), 1, mxID(_acosh), XS_DONT_ENUM_FLAG); |
48 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asin), 1, mxID(_asin), XS_DONT_ENUM_FLAG); |
49 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asinh), 1, mxID(_asinh), XS_DONT_ENUM_FLAG); |
50 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan), 1, mxID(_atan), XS_DONT_ENUM_FLAG); |
51 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atanh), 1, mxID(_atanh), XS_DONT_ENUM_FLAG); |
52 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan2), 2, mxID(_atan2), XS_DONT_ENUM_FLAG); |
53 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cbrt), 1, mxID(_cbrt), XS_DONT_ENUM_FLAG); |
54 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_ceil), 1, mxID(_ceil), XS_DONT_ENUM_FLAG); |
55 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_clz32), 1, mxID(_clz32), XS_DONT_ENUM_FLAG); |
56 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cos), 1, mxID(_cos), XS_DONT_ENUM_FLAG); |
57 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cosh), 1, mxID(_cosh), XS_DONT_ENUM_FLAG); |
58 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_exp), 1, mxID(_exp), XS_DONT_ENUM_FLAG); |
59 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_expm1), 1, mxID(_expm1), XS_DONT_ENUM_FLAG); |
60 | 6.57k | #if mxFloat16 |
61 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_f16round), 1, mxID(_f16round), XS_DONT_ENUM_FLAG); |
62 | 6.57k | #endif |
63 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_floor), 1, mxID(_floor), XS_DONT_ENUM_FLAG); |
64 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_fround), 1, mxID(_fround), XS_DONT_ENUM_FLAG); |
65 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_hypot), 2, mxID(_hypot_), XS_DONT_ENUM_FLAG); |
66 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idiv), 2, mxID(_idiv), XS_DONT_ENUM_FLAG); |
67 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idivmod), 2, mxID(_idivmod), XS_DONT_ENUM_FLAG); |
68 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imod), 2, mxID(_imod), XS_DONT_ENUM_FLAG); |
69 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imul), 2, mxID(_imul), XS_DONT_ENUM_FLAG); |
70 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imuldiv), 2, mxID(_imuldiv), XS_DONT_ENUM_FLAG); |
71 | 6.57k | #if mxECMAScript2023 |
72 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irandom), 0, mxID(_irandom), XS_DONT_ENUM_FLAG); |
73 | 6.57k | #endif |
74 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irem), 2, mxID(_irem), XS_DONT_ENUM_FLAG); |
75 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log), 1, mxID(_log), XS_DONT_ENUM_FLAG); |
76 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log1p), 1, mxID(_log1p), XS_DONT_ENUM_FLAG); |
77 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log10), 1, mxID(_log10), XS_DONT_ENUM_FLAG); |
78 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log2), 1, mxID(_log2), XS_DONT_ENUM_FLAG); |
79 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_max), 2, mxID(_max), XS_DONT_ENUM_FLAG); |
80 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_min), 2, mxID(_min), XS_DONT_ENUM_FLAG); |
81 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_mod), 2, mxID(_mod), XS_DONT_ENUM_FLAG); |
82 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_pow), 2, mxID(_pow), XS_DONT_ENUM_FLAG); |
83 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_random), 0, mxID(_random), XS_DONT_ENUM_FLAG); |
84 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_round), 1, mxID(_round), XS_DONT_ENUM_FLAG); |
85 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sign), 1, mxID(_sign), XS_DONT_ENUM_FLAG); |
86 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sin), 1, mxID(_sin), XS_DONT_ENUM_FLAG); |
87 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sinh), 1, mxID(_sinh), XS_DONT_ENUM_FLAG); |
88 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sqrt), 1, mxID(_sqrt), XS_DONT_ENUM_FLAG); |
89 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tan), 1, mxID(_tan), XS_DONT_ENUM_FLAG); |
90 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tanh), 1, mxID(_tanh), XS_DONT_ENUM_FLAG); |
91 | 6.57k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_trunc), 1, mxID(_trunc), XS_DONT_ENUM_FLAG); |
92 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_E, mxID(_E), XS_GET_ONLY); |
93 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_LN10, mxID(_LN10), XS_GET_ONLY); |
94 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_LN2, mxID(_LN2), XS_GET_ONLY); |
95 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_LOG10E, mxID(_LOG10E), XS_GET_ONLY); |
96 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_LOG2E, mxID(_LOG2E), XS_GET_ONLY); |
97 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_PI, mxID(_PI), XS_GET_ONLY); |
98 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_SQRT1_2, mxID(_SQRT1_2), XS_GET_ONLY); |
99 | 6.57k | slot = fxNextNumberProperty(the, slot, C_M_SQRT2, mxID(_SQRT2), XS_GET_ONLY); |
100 | 6.57k | slot = fxNextStringXProperty(the, slot, "Math", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
101 | 6.57k | mxPull(mxMathObject); |
102 | | //@@ c_srand((unsigned)c_time(0)); |
103 | 6.57k | } |
104 | | |
105 | | #define mxNanResultIfNoArg \ |
106 | 0 | if (mxArgc < 1) { \ |
107 | 0 | mxResult->kind = XS_NUMBER_KIND; \ |
108 | 0 | mxResult->value.number = C_NAN; \ |
109 | 0 | return; \ |
110 | 0 | } |
111 | | |
112 | | #define mxNanResultIfNoArg2 \ |
113 | 0 | if (mxArgc < 2) { \ |
114 | 0 | mxResult->kind = XS_NUMBER_KIND; \ |
115 | 0 | mxResult->value.number = C_NAN; \ |
116 | 0 | return; \ |
117 | 0 | } |
118 | | |
119 | | void fx_Math_abs(txMachine* the) |
120 | 0 | { |
121 | 0 | mxNanResultIfNoArg; |
122 | 0 | fxToNumber(the, mxArgv(0)); |
123 | 0 | mxResult->kind = XS_NUMBER_KIND; |
124 | 0 | mxResult->value.number = c_fabs(mxArgv(0)->value.number); |
125 | 0 | } |
126 | | |
127 | | void fx_Math_acos(txMachine* the) |
128 | 0 | { |
129 | 0 | mxNanResultIfNoArg; |
130 | 0 | fxToNumber(the, mxArgv(0)); |
131 | 0 | mxResult->kind = XS_NUMBER_KIND; |
132 | 0 | mxResult->value.number = c_acos(mxArgv(0)->value.number); |
133 | 0 | } |
134 | | |
135 | | void fx_Math_acosh(txMachine* the) |
136 | 0 | { |
137 | 0 | mxNanResultIfNoArg; |
138 | 0 | fxToNumber(the, mxArgv(0)); |
139 | 0 | mxResult->kind = XS_NUMBER_KIND; |
140 | 0 | mxResult->value.number = c_acosh(mxArgv(0)->value.number); |
141 | 0 | } |
142 | | |
143 | | void fx_Math_asin(txMachine* the) |
144 | 0 | { |
145 | 0 | mxNanResultIfNoArg; |
146 | 0 | fxToNumber(the, mxArgv(0)); |
147 | 0 | mxResult->kind = XS_NUMBER_KIND; |
148 | 0 | mxResult->value.number = c_asin(mxArgv(0)->value.number); |
149 | 0 | } |
150 | | |
151 | | void fx_Math_asinh(txMachine* the) |
152 | 0 | { |
153 | 0 | mxNanResultIfNoArg; |
154 | 0 | fxToNumber(the, mxArgv(0)); |
155 | 0 | mxResult->kind = XS_NUMBER_KIND; |
156 | 0 | mxResult->value.number = c_asinh(mxArgv(0)->value.number); |
157 | 0 | } |
158 | | |
159 | | void fx_Math_atan(txMachine* the) |
160 | 0 | { |
161 | 0 | mxNanResultIfNoArg; |
162 | 0 | fxToNumber(the, mxArgv(0)); |
163 | 0 | mxResult->kind = XS_NUMBER_KIND; |
164 | 0 | mxResult->value.number = c_atan(mxArgv(0)->value.number); |
165 | 0 | } |
166 | | |
167 | | void fx_Math_atanh(txMachine* the) |
168 | 0 | { |
169 | 0 | mxNanResultIfNoArg; |
170 | 0 | fxToNumber(the, mxArgv(0)); |
171 | 0 | mxResult->kind = XS_NUMBER_KIND; |
172 | 0 | mxResult->value.number = c_atanh(mxArgv(0)->value.number); |
173 | 0 | } |
174 | | |
175 | | void fx_Math_atan2(txMachine* the) |
176 | 0 | { |
177 | 0 | mxNanResultIfNoArg2; |
178 | 0 | fxToNumber(the, mxArgv(0)); |
179 | 0 | fxToNumber(the, mxArgv(1)); |
180 | 0 | mxResult->kind = XS_NUMBER_KIND; |
181 | 0 | mxResult->value.number = c_atan2(mxArgv(0)->value.number, mxArgv(1)->value.number); |
182 | 0 | } |
183 | | |
184 | | void fx_Math_cbrt(txMachine* the) |
185 | 0 | { |
186 | 0 | mxNanResultIfNoArg; |
187 | 0 | fxToNumber(the, mxArgv(0)); |
188 | 0 | mxResult->kind = XS_NUMBER_KIND; |
189 | 0 | mxResult->value.number = c_cbrt(mxArgv(0)->value.number); |
190 | 0 | } |
191 | | |
192 | | void fx_Math_ceil(txMachine* the) |
193 | 0 | { |
194 | 0 | mxNanResultIfNoArg; |
195 | 0 | if (XS_INTEGER_KIND == mxArgv(0)->kind) { |
196 | 0 | mxResult->kind = XS_INTEGER_KIND; |
197 | 0 | mxResult->value.integer = mxArgv(0)->value.integer; |
198 | 0 | return; |
199 | 0 | } |
200 | 0 | fxToNumber(the, mxArgv(0)); |
201 | 0 | mxResult->kind = XS_NUMBER_KIND; |
202 | 0 | mxResult->value.number = c_ceil(mxArgv(0)->value.number); |
203 | 0 | fx_Math_toInteger(the); |
204 | 0 | } |
205 | | |
206 | | void fx_Math_clz32(txMachine* the) |
207 | 0 | { |
208 | 0 | txUnsigned x = (mxArgc > 0) ? fxToUnsigned(the, mxArgv(0)) : 0; |
209 | 0 | txInteger r; |
210 | 0 | if (x) |
211 | | #if mxWindows |
212 | | { |
213 | | _BitScanReverse(&r, x); |
214 | | r = 31 - r; |
215 | | } |
216 | | #else |
217 | 0 | r = __builtin_clz(x); |
218 | 0 | #endif |
219 | 0 | else |
220 | 0 | r = 32; |
221 | 0 | mxResult->kind = XS_INTEGER_KIND; |
222 | 0 | mxResult->value.integer = r; |
223 | 0 | } |
224 | | |
225 | | void fx_Math_cos(txMachine* the) |
226 | 0 | { |
227 | 0 | mxNanResultIfNoArg; |
228 | 0 | fxToNumber(the, mxArgv(0)); |
229 | 0 | mxResult->kind = XS_NUMBER_KIND; |
230 | 0 | mxResult->value.number = c_cos(mxArgv(0)->value.number); |
231 | 0 | } |
232 | | |
233 | | void fx_Math_cosh(txMachine* the) |
234 | 0 | { |
235 | 0 | mxNanResultIfNoArg; |
236 | 0 | fxToNumber(the, mxArgv(0)); |
237 | 0 | mxResult->kind = XS_NUMBER_KIND; |
238 | 0 | mxResult->value.number = c_cosh(mxArgv(0)->value.number); |
239 | 0 | } |
240 | | |
241 | | void fx_Math_exp(txMachine* the) |
242 | 0 | { |
243 | 0 | mxNanResultIfNoArg; |
244 | 0 | fxToNumber(the, mxArgv(0)); |
245 | 0 | mxResult->kind = XS_NUMBER_KIND; |
246 | 0 | mxResult->value.number = c_exp(mxArgv(0)->value.number); |
247 | 0 | } |
248 | | |
249 | | void fx_Math_expm1(txMachine* the) |
250 | 0 | { |
251 | 0 | mxNanResultIfNoArg; |
252 | 0 | fxToNumber(the, mxArgv(0)); |
253 | 0 | mxResult->kind = XS_NUMBER_KIND; |
254 | 0 | mxResult->value.number = c_expm1(mxArgv(0)->value.number); |
255 | 0 | } |
256 | | |
257 | | void fx_Math_floor(txMachine* the) |
258 | 0 | { |
259 | 0 | mxNanResultIfNoArg; |
260 | 0 | if (XS_INTEGER_KIND == mxArgv(0)->kind) { |
261 | 0 | mxResult->kind = XS_INTEGER_KIND; |
262 | 0 | mxResult->value.integer = mxArgv(0)->value.integer; |
263 | 0 | return; |
264 | 0 | } |
265 | 0 | fxToNumber(the, mxArgv(0)); |
266 | 0 | mxResult->kind = XS_NUMBER_KIND; |
267 | 0 | mxResult->value.number = c_floor(mxArgv(0)->value.number); |
268 | 0 | fx_Math_toInteger(the); |
269 | 0 | } |
270 | | |
271 | | void fx_Math_fround(txMachine* the) |
272 | 0 | { |
273 | 0 | float arg; |
274 | 0 | mxNanResultIfNoArg; |
275 | 0 | if (XS_INTEGER_KIND == mxArgv(0)->kind) { |
276 | 0 | mxResult->kind = XS_INTEGER_KIND; |
277 | 0 | mxResult->value.integer = mxArgv(0)->value.integer; |
278 | 0 | return; |
279 | 0 | } |
280 | 0 | arg = (float)fxToNumber(the, mxArgv(0)); |
281 | 0 | mxResult->kind = XS_NUMBER_KIND; |
282 | 0 | mxResult->value.number = arg; |
283 | 0 | } |
284 | | |
285 | | void fx_Math_hypot(txMachine* the) |
286 | 0 | { |
287 | 0 | if (mxArgc == 2) { |
288 | 0 | fxToNumber(the, mxArgv(0)); |
289 | 0 | fxToNumber(the, mxArgv(1)); |
290 | 0 | mxResult->kind = XS_NUMBER_KIND; |
291 | 0 | mxResult->value.number = c_hypot(mxArgv(0)->value.number, mxArgv(1)->value.number); |
292 | 0 | } |
293 | 0 | else { |
294 | 0 | txInteger c = mxArgc, i; |
295 | 0 | txNumber result = 0; |
296 | 0 | for (i = 0; i < c; i++) { |
297 | 0 | txNumber argument = fxToNumber(the, mxArgv(i)); |
298 | 0 | result += argument * argument; |
299 | 0 | } |
300 | 0 | mxResult->kind = XS_NUMBER_KIND; |
301 | 0 | mxResult->value.number = c_sqrt(result); |
302 | 0 | } |
303 | 0 | } |
304 | | |
305 | | void fx_Math_idiv(txMachine* the) |
306 | 0 | { |
307 | 0 | txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0; |
308 | 0 | txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0; |
309 | 0 | if (y == 0) { |
310 | 0 | mxResult->kind = XS_NUMBER_KIND; |
311 | 0 | mxResult->value.number = C_NAN; |
312 | 0 | } |
313 | 0 | else { |
314 | 0 | mxResult->kind = XS_INTEGER_KIND; |
315 | 0 | #if mxIntegerDivideOverflowException |
316 | 0 | if ((x == (txInteger)0x80000000) && (y == -1)) |
317 | 0 | mxResult->value.integer = x; |
318 | 0 | else |
319 | 0 | #endif |
320 | 0 | mxResult->value.integer = x / y; |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | | void fx_Math_idivmod(txMachine* the) |
325 | 0 | { |
326 | 0 | mxTypeError("not available"); |
327 | 0 | } |
328 | | |
329 | | void fx_Math_imod(txMachine* the) |
330 | 0 | { |
331 | 0 | txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0; |
332 | 0 | txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0; |
333 | 0 | if (y == 0) { |
334 | 0 | mxResult->kind = XS_NUMBER_KIND; |
335 | 0 | mxResult->value.number = C_NAN; |
336 | 0 | } |
337 | 0 | else { |
338 | 0 | mxResult->kind = XS_INTEGER_KIND; |
339 | 0 | #if mxIntegerDivideOverflowException |
340 | 0 | if ((x == (txInteger)0x80000000) && (y == -1)) |
341 | 0 | mxResult->value.integer = 0; |
342 | 0 | else |
343 | 0 | #endif |
344 | 0 | mxResult->value.integer = (x % y + y) % y; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | | void fx_Math_imul(txMachine* the) |
349 | 0 | { |
350 | 0 | txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0; |
351 | 0 | txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0; |
352 | 0 | mxResult->kind = XS_INTEGER_KIND; |
353 | 0 | mxResult->value.integer = x * y; |
354 | 0 | } |
355 | | |
356 | | void fx_Math_imuldiv(txMachine* the) |
357 | 0 | { |
358 | 0 | txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0; |
359 | 0 | txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0; |
360 | 0 | txS8 z = (mxArgc > 2) ? fxToInteger(the, mxArgv(2)) : 0; |
361 | 0 | if (z == 0) { |
362 | 0 | mxResult->kind = XS_NUMBER_KIND; |
363 | 0 | mxResult->value.number = C_NAN; |
364 | 0 | } |
365 | 0 | else { |
366 | 0 | txS8 r = (x * y) / z; |
367 | 0 | mxResult->kind = XS_INTEGER_KIND; |
368 | 0 | mxResult->value.integer = (txInteger)(r & 0x00000000FFFFFFFF); |
369 | 0 | } |
370 | 0 | } |
371 | | |
372 | | void fx_Math_irandom(txMachine* the) |
373 | 0 | { |
374 | 0 | double min = 0; |
375 | 0 | double max = 2147483647; |
376 | 0 | uint32_t result; |
377 | 0 | if (mxArgc > 1) { |
378 | 0 | min = (double)fxToInteger(the, mxArgv(0)); |
379 | 0 | max = (double)fxToInteger(the, mxArgv(1)); |
380 | 0 | } |
381 | 0 | else if (mxArgc > 0) { |
382 | 0 | max = (double)fxToInteger(the, mxArgv(0)); |
383 | 0 | } |
384 | 0 | result = c_rand(); |
385 | 0 | while (result == C_RAND_MAX) |
386 | 0 | result = c_rand(); |
387 | 0 | if (max < min) |
388 | 0 | mxResult->value.integer = (txInteger)c_ceil(min + (((double)result / (double)C_RAND_MAX) * (max - min))); |
389 | 0 | else |
390 | 0 | mxResult->value.integer = (txInteger)c_floor(min + (((double)result / (double)C_RAND_MAX) * (max - min))); |
391 | 0 | mxResult->kind = XS_INTEGER_KIND; |
392 | 0 | } |
393 | | |
394 | | void fx_Math_irandom_secure(txMachine* the) |
395 | 0 | { |
396 | 0 | mxTypeError("secure mode"); |
397 | 0 | } |
398 | | |
399 | | void fx_Math_irem(txMachine* the) |
400 | 0 | { |
401 | 0 | txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0; |
402 | 0 | txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0; |
403 | 0 | if (y == 0) { |
404 | 0 | mxResult->kind = XS_NUMBER_KIND; |
405 | 0 | mxResult->value.number = C_NAN; |
406 | 0 | } |
407 | 0 | else { |
408 | 0 | mxResult->kind = XS_INTEGER_KIND; |
409 | 0 | #if mxIntegerDivideOverflowException |
410 | 0 | if ((x == (txInteger)0x80000000) && (y == -1)) |
411 | 0 | mxResult->value.integer = 0; |
412 | 0 | else |
413 | 0 | #endif |
414 | 0 | mxResult->value.integer = x % y; |
415 | 0 | } |
416 | 0 | } |
417 | | |
418 | | void fx_Math_log(txMachine* the) |
419 | 0 | { |
420 | 0 | mxNanResultIfNoArg; |
421 | 0 | fxToNumber(the, mxArgv(0)); |
422 | 0 | mxResult->kind = XS_NUMBER_KIND; |
423 | 0 | mxResult->value.number = c_log(mxArgv(0)->value.number); |
424 | 0 | } |
425 | | |
426 | | void fx_Math_log1p(txMachine* the) |
427 | 0 | { |
428 | 0 | mxNanResultIfNoArg; |
429 | 0 | fxToNumber(the, mxArgv(0)); |
430 | 0 | mxResult->kind = XS_NUMBER_KIND; |
431 | 0 | mxResult->value.number = c_log1p(mxArgv(0)->value.number); |
432 | 0 | } |
433 | | |
434 | | void fx_Math_log10(txMachine* the) |
435 | 0 | { |
436 | 0 | mxNanResultIfNoArg; |
437 | 0 | fxToNumber(the, mxArgv(0)); |
438 | 0 | mxResult->kind = XS_NUMBER_KIND; |
439 | 0 | mxResult->value.number = c_log10(mxArgv(0)->value.number); |
440 | 0 | } |
441 | | |
442 | | void fx_Math_log2(txMachine* the) |
443 | 0 | { |
444 | 0 | mxNanResultIfNoArg; |
445 | 0 | fxToNumber(the, mxArgv(0)); |
446 | 0 | mxResult->kind = XS_NUMBER_KIND; |
447 | | #if mxAndroid |
448 | | mxResult->value.number = c_log(mxArgv(0)->value.number) / c_log(2); |
449 | | #else |
450 | 0 | mxResult->value.number = c_log2(mxArgv(0)->value.number); |
451 | 0 | #endif |
452 | 0 | } |
453 | | |
454 | | void fx_Math_max(txMachine* the) |
455 | 0 | { |
456 | 0 | txInteger c = mxArgc, i = 0; |
457 | 0 | mxResult->kind = XS_NUMBER_KIND; |
458 | 0 | mxResult->value.number = -((txNumber)C_INFINITY); |
459 | 0 | if (0 == c) |
460 | 0 | return; |
461 | | |
462 | 0 | if (XS_INTEGER_KIND == mxArgv(0)->kind) { |
463 | 0 | mxResult->kind = XS_INTEGER_KIND; |
464 | 0 | mxResult->value.integer = mxArgv(0)->value.integer; |
465 | 0 | i = 1; |
466 | 0 | } |
467 | |
|
468 | 0 | for (; i < c; i++) { |
469 | 0 | txSlot *slot = mxArgv(i); |
470 | 0 | if (XS_INTEGER_KIND == mxResult->kind) { |
471 | 0 | if (XS_INTEGER_KIND == slot->kind) { |
472 | 0 | if (mxResult->value.integer < slot->value.integer) { |
473 | 0 | mxResult->value.integer = slot->value.integer; |
474 | 0 | } |
475 | 0 | continue; |
476 | 0 | } |
477 | 0 | mxResult->kind = XS_NUMBER_KIND; |
478 | 0 | mxResult->value.number = mxResult->value.integer; |
479 | 0 | } |
480 | | |
481 | 0 | txNumber n = fxToNumber(the, slot); |
482 | 0 | if (c_isnan(n)) { |
483 | 0 | for (; i < c; i++) |
484 | 0 | fxToNumber(the, mxArgv(i)); |
485 | 0 | mxResult->value.number = C_NAN; |
486 | 0 | return; |
487 | 0 | } |
488 | 0 | if (mxResult->value.number < n) |
489 | 0 | mxResult->value.number = n; |
490 | 0 | else if ((mxResult->value.number == 0) && (n == 0)) { |
491 | 0 | if (c_signbit(mxResult->value.number) != c_signbit(n)) |
492 | 0 | mxResult->value.number = 0; |
493 | 0 | } |
494 | 0 | } |
495 | 0 | } |
496 | | |
497 | | void fx_Math_min(txMachine* the) |
498 | 0 | { |
499 | 0 | txInteger c = mxArgc, i = 0; |
500 | 0 | mxResult->kind = XS_NUMBER_KIND; |
501 | 0 | mxResult->value.number = (txNumber)C_INFINITY; |
502 | 0 | if (0 == c) |
503 | 0 | return; |
504 | | |
505 | 0 | if (XS_INTEGER_KIND == mxArgv(0)->kind) { |
506 | 0 | mxResult->kind = XS_INTEGER_KIND; |
507 | 0 | mxResult->value.integer = mxArgv(0)->value.integer; |
508 | 0 | i = 1; |
509 | 0 | } |
510 | |
|
511 | 0 | for (; i < c; i++) { |
512 | 0 | txSlot *slot = mxArgv(i); |
513 | 0 | if (XS_INTEGER_KIND == mxResult->kind) { |
514 | 0 | if (XS_INTEGER_KIND == slot->kind) { |
515 | 0 | if (mxResult->value.integer > slot->value.integer) |
516 | 0 | mxResult->value.integer = slot->value.integer; |
517 | 0 | continue; |
518 | 0 | } |
519 | 0 | mxResult->kind = XS_NUMBER_KIND; |
520 | 0 | mxResult->value.number = mxResult->value.integer; |
521 | 0 | } |
522 | | |
523 | 0 | txNumber n = fxToNumber(the, slot); |
524 | 0 | if (c_isnan(n)) { |
525 | 0 | for (; i < c; i++) |
526 | 0 | fxToNumber(the, mxArgv(i)); |
527 | 0 | mxResult->value.number = C_NAN; |
528 | 0 | return; |
529 | 0 | } |
530 | 0 | if (mxResult->value.number > n) |
531 | 0 | mxResult->value.number = n; |
532 | 0 | else if ((mxResult->value.number == 0) && (n == 0)) { |
533 | 0 | if (c_signbit(mxResult->value.number) != c_signbit(n)) |
534 | 0 | mxResult->value.number = -0.0; |
535 | 0 | } |
536 | 0 | } |
537 | 0 | } |
538 | | |
539 | | void fx_Math_mod(txMachine* the) |
540 | 0 | { |
541 | 0 | txNumber x = (mxArgc > 0) ? fxToNumber(the, mxArgv(0)) : 0; |
542 | 0 | txNumber y = (mxArgc > 1) ? fxToNumber(the, mxArgv(1)) : 0; |
543 | 0 | mxResult->kind = XS_NUMBER_KIND; |
544 | 0 | mxResult->value.number = c_fmod((c_fmod(x, y) + y), y); |
545 | 0 | } |
546 | | |
547 | | txNumber fx_pow(txNumber x, txNumber y) |
548 | 0 | { |
549 | 0 | if (!c_isfinite(y) && (c_fabs(x) == 1.0)) |
550 | 0 | return C_NAN; |
551 | 0 | return c_pow(x, y); |
552 | 0 | } |
553 | | |
554 | | void fx_Math_pow(txMachine* the) |
555 | 0 | { |
556 | 0 | txNumber x, y; |
557 | 0 | mxNanResultIfNoArg2; |
558 | 0 | x = fxToNumber(the, mxArgv(0)); |
559 | 0 | y = fxToNumber(the, mxArgv(1)); |
560 | 0 | mxResult->kind = XS_NUMBER_KIND; |
561 | 0 | mxResult->value.number = fx_pow(x, y); |
562 | 0 | } |
563 | | |
564 | | void fx_Math_random(txMachine* the) |
565 | 0 | { |
566 | 0 | uint32_t result; |
567 | 0 | do { |
568 | 0 | result = c_rand(); |
569 | 0 | } while (result == C_RAND_MAX); |
570 | 0 | mxResult->kind = XS_NUMBER_KIND; |
571 | 0 | mxResult->value.number = (double)result / (double)C_RAND_MAX; |
572 | 0 | } |
573 | | |
574 | | void fx_Math_random_secure(txMachine* the) |
575 | 0 | { |
576 | 0 | mxTypeError("secure mode"); |
577 | 0 | } |
578 | | |
579 | | void fx_Math_round(txMachine* the) |
580 | 0 | { |
581 | 0 | txNumber arg; |
582 | 0 | mxNanResultIfNoArg; |
583 | 0 | if (XS_INTEGER_KIND == mxArgv(0)->kind) { |
584 | 0 | mxResult->kind = XS_INTEGER_KIND; |
585 | 0 | mxResult->value.integer = mxArgv(0)->value.integer; |
586 | 0 | return; |
587 | 0 | } |
588 | 0 | arg = fxToNumber(the, mxArgv(0)); |
589 | 0 | if (c_isnormal(arg) && (-4503599627370495 < arg) && (arg < 4503599627370495)) { // 2 ** 52 - 1 |
590 | 0 | if ((arg < -0.5) || (0.5 <= arg)) |
591 | 0 | arg = c_floor(arg + 0.5); |
592 | 0 | else if (arg < 0) |
593 | 0 | arg = -0.0; |
594 | 0 | else if (arg > 0) |
595 | 0 | arg = 0.0; |
596 | 0 | } |
597 | 0 | mxResult->kind = XS_NUMBER_KIND; |
598 | 0 | mxResult->value.number = arg; |
599 | 0 | fx_Math_toInteger(the); |
600 | 0 | } |
601 | | |
602 | | void fx_Math_sqrt(txMachine* the) |
603 | 0 | { |
604 | 0 | mxNanResultIfNoArg; |
605 | 0 | fxToNumber(the, mxArgv(0)); |
606 | 0 | mxResult->kind = XS_NUMBER_KIND; |
607 | 0 | mxResult->value.number = c_sqrt(mxArgv(0)->value.number); |
608 | 0 | } |
609 | | |
610 | | void fx_Math_sign(txMachine* the) |
611 | 0 | { |
612 | 0 | txNumber arg; |
613 | 0 | mxNanResultIfNoArg; |
614 | 0 | arg = fxToNumber(the, mxArgv(0)); |
615 | 0 | mxResult->kind = XS_NUMBER_KIND; |
616 | 0 | if (c_isnan(arg)) |
617 | 0 | mxResult->value.number = C_NAN; |
618 | 0 | else if (arg < 0) |
619 | 0 | mxResult->value.number = -1; |
620 | 0 | else if (arg > 0) |
621 | 0 | mxResult->value.number = 1; |
622 | 0 | else |
623 | 0 | mxResult->value.number = arg; |
624 | 0 | fx_Math_toInteger(the); |
625 | 0 | } |
626 | | |
627 | | void fx_Math_sin(txMachine* the) |
628 | 0 | { |
629 | 0 | mxNanResultIfNoArg; |
630 | 0 | fxToNumber(the, mxArgv(0)); |
631 | 0 | mxResult->kind = XS_NUMBER_KIND; |
632 | 0 | mxResult->value.number = c_sin(mxArgv(0)->value.number); |
633 | 0 | } |
634 | | |
635 | | void fx_Math_sinh(txMachine* the) |
636 | 0 | { |
637 | 0 | mxNanResultIfNoArg; |
638 | 0 | fxToNumber(the, mxArgv(0)); |
639 | 0 | mxResult->kind = XS_NUMBER_KIND; |
640 | 0 | mxResult->value.number = c_sinh(mxArgv(0)->value.number); |
641 | 0 | } |
642 | | |
643 | | void fx_Math_tan(txMachine* the) |
644 | 0 | { |
645 | 0 | mxNanResultIfNoArg; |
646 | 0 | fxToNumber(the, mxArgv(0)); |
647 | 0 | mxResult->kind = XS_NUMBER_KIND; |
648 | 0 | mxResult->value.number = c_tan(mxArgv(0)->value.number); |
649 | 0 | } |
650 | | |
651 | | void fx_Math_tanh(txMachine* the) |
652 | 0 | { |
653 | 0 | mxNanResultIfNoArg; |
654 | 0 | fxToNumber(the, mxArgv(0)); |
655 | 0 | mxResult->kind = XS_NUMBER_KIND; |
656 | 0 | mxResult->value.number = c_tanh(mxArgv(0)->value.number); |
657 | 0 | } |
658 | | |
659 | | void fx_Math_trunc(txMachine* the) |
660 | 0 | { |
661 | 0 | mxNanResultIfNoArg; |
662 | 0 | fxToNumber(the, mxArgv(0)); |
663 | 0 | mxResult->kind = XS_NUMBER_KIND; |
664 | 0 | mxResult->value.number = c_trunc(mxArgv(0)->value.number); |
665 | 0 | fx_Math_toInteger(the); |
666 | 0 | } |
667 | | |
668 | | void fx_Math_toInteger(txMachine* the) |
669 | 0 | { |
670 | 0 | txNumber number = mxResult->value.number; |
671 | 0 | txInteger integer = (txInteger)number; |
672 | 0 | txNumber check = integer; |
673 | 0 | if ((number == check) && (number || !c_signbit(number))) { |
674 | 0 | mxResult->value.integer = integer; |
675 | 0 | mxResult->kind = XS_INTEGER_KIND; |
676 | 0 | } |
677 | 0 | } |