Coverage Report

Created: 2025-08-28 06:23

/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
23.5k
{
42
23.5k
  txSlot* slot;
43
23.5k
  mxPush(mxObjectPrototype);
44
23.5k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
45
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_abs), 1, mxID(_abs), XS_DONT_ENUM_FLAG);
46
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acos), 1, mxID(_acos), XS_DONT_ENUM_FLAG);
47
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acosh), 1, mxID(_acosh), XS_DONT_ENUM_FLAG);
48
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asin), 1, mxID(_asin), XS_DONT_ENUM_FLAG);
49
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asinh), 1, mxID(_asinh), XS_DONT_ENUM_FLAG);
50
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan), 1, mxID(_atan), XS_DONT_ENUM_FLAG);
51
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atanh), 1, mxID(_atanh), XS_DONT_ENUM_FLAG);
52
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan2), 2, mxID(_atan2), XS_DONT_ENUM_FLAG);
53
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cbrt), 1, mxID(_cbrt), XS_DONT_ENUM_FLAG);
54
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_ceil), 1, mxID(_ceil), XS_DONT_ENUM_FLAG);
55
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_clz32), 1, mxID(_clz32), XS_DONT_ENUM_FLAG);
56
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cos), 1, mxID(_cos), XS_DONT_ENUM_FLAG);
57
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cosh), 1, mxID(_cosh), XS_DONT_ENUM_FLAG);
58
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_exp), 1, mxID(_exp), XS_DONT_ENUM_FLAG);
59
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_expm1), 1, mxID(_expm1), XS_DONT_ENUM_FLAG);
60
23.5k
#if mxFloat16
61
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_f16round), 1, mxID(_f16round), XS_DONT_ENUM_FLAG);
62
23.5k
#endif
63
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_floor), 1, mxID(_floor), XS_DONT_ENUM_FLAG);
64
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_fround), 1, mxID(_fround), XS_DONT_ENUM_FLAG);
65
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_hypot), 2, mxID(_hypot_), XS_DONT_ENUM_FLAG);
66
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idiv), 2, mxID(_idiv), XS_DONT_ENUM_FLAG);
67
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idivmod), 2, mxID(_idivmod), XS_DONT_ENUM_FLAG);
68
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imod), 2, mxID(_imod), XS_DONT_ENUM_FLAG);
69
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imul), 2, mxID(_imul), XS_DONT_ENUM_FLAG);
70
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imuldiv), 2, mxID(_imuldiv), XS_DONT_ENUM_FLAG);
71
23.5k
#if mxECMAScript2023
72
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irandom), 0, mxID(_irandom), XS_DONT_ENUM_FLAG);
73
23.5k
#endif
74
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irem), 2, mxID(_irem), XS_DONT_ENUM_FLAG);
75
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log), 1, mxID(_log), XS_DONT_ENUM_FLAG);
76
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log1p), 1, mxID(_log1p), XS_DONT_ENUM_FLAG);
77
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log10), 1, mxID(_log10), XS_DONT_ENUM_FLAG);
78
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log2), 1, mxID(_log2), XS_DONT_ENUM_FLAG);
79
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_max), 2, mxID(_max), XS_DONT_ENUM_FLAG);
80
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_min), 2, mxID(_min), XS_DONT_ENUM_FLAG);
81
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_mod), 2, mxID(_mod), XS_DONT_ENUM_FLAG);
82
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_pow), 2, mxID(_pow), XS_DONT_ENUM_FLAG);
83
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_random), 0, mxID(_random), XS_DONT_ENUM_FLAG);
84
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_round), 1, mxID(_round), XS_DONT_ENUM_FLAG);
85
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sign), 1, mxID(_sign), XS_DONT_ENUM_FLAG);
86
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sin), 1, mxID(_sin), XS_DONT_ENUM_FLAG);
87
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sinh), 1, mxID(_sinh), XS_DONT_ENUM_FLAG);
88
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sqrt), 1, mxID(_sqrt), XS_DONT_ENUM_FLAG);
89
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tan), 1, mxID(_tan), XS_DONT_ENUM_FLAG);
90
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tanh), 1, mxID(_tanh), XS_DONT_ENUM_FLAG);
91
23.5k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_trunc), 1, mxID(_trunc), XS_DONT_ENUM_FLAG);
92
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_E, mxID(_E), XS_GET_ONLY);
93
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_LN10, mxID(_LN10), XS_GET_ONLY);
94
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_LN2, mxID(_LN2), XS_GET_ONLY);
95
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_LOG10E, mxID(_LOG10E), XS_GET_ONLY);
96
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_LOG2E, mxID(_LOG2E), XS_GET_ONLY);
97
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_PI, mxID(_PI), XS_GET_ONLY);
98
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT1_2, mxID(_SQRT1_2), XS_GET_ONLY);
99
23.5k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT2, mxID(_SQRT2), XS_GET_ONLY);
100
23.5k
  slot = fxNextStringXProperty(the, slot, "Math", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
101
23.5k
  mxPull(mxMathObject);
102
//@@  c_srand((unsigned)c_time(0));
103
23.5k
}
104
105
#define mxNanResultIfNoArg \
106
1.03M
  if (mxArgc < 1) {  \
107
5.34k
    mxResult->kind = XS_NUMBER_KIND; \
108
5.34k
    mxResult->value.number = C_NAN; \
109
5.34k
    return; \
110
5.34k
  }
111
112
#define mxNanResultIfNoArg2 \
113
586k
  if (mxArgc < 2) {  \
114
15
    mxResult->kind = XS_NUMBER_KIND; \
115
15
    mxResult->value.number = C_NAN; \
116
15
    return; \
117
15
  }
118
119
void fx_Math_abs(txMachine* the)
120
17
{
121
17
  mxNanResultIfNoArg;
122
17
  fxToNumber(the, mxArgv(0));
123
17
  mxResult->kind = XS_NUMBER_KIND;
124
17
  mxResult->value.number = c_fabs(mxArgv(0)->value.number);
125
17
}
126
127
void fx_Math_acos(txMachine* the)
128
7.50k
{
129
7.50k
  mxNanResultIfNoArg;
130
6.69k
  fxToNumber(the, mxArgv(0));
131
6.69k
  mxResult->kind = XS_NUMBER_KIND;
132
6.69k
  mxResult->value.number = c_acos(mxArgv(0)->value.number);
133
6.69k
}
134
135
void fx_Math_acosh(txMachine* the)
136
249k
{
137
249k
  mxNanResultIfNoArg;
138
248k
  fxToNumber(the, mxArgv(0));
139
248k
  mxResult->kind = XS_NUMBER_KIND;
140
248k
  mxResult->value.number = c_acosh(mxArgv(0)->value.number);
141
248k
}
142
143
void fx_Math_asin(txMachine* the)
144
3.68k
{
145
3.68k
  mxNanResultIfNoArg;
146
3.68k
  fxToNumber(the, mxArgv(0));
147
3.68k
  mxResult->kind = XS_NUMBER_KIND;
148
3.68k
  mxResult->value.number = c_asin(mxArgv(0)->value.number);
149
3.68k
}
150
151
void fx_Math_asinh(txMachine* the)
152
56.7k
{
153
56.7k
  mxNanResultIfNoArg;
154
56.7k
  fxToNumber(the, mxArgv(0));
155
56.7k
  mxResult->kind = XS_NUMBER_KIND;
156
56.7k
  mxResult->value.number = c_asinh(mxArgv(0)->value.number);
157
56.7k
}
158
159
void fx_Math_atan(txMachine* the)
160
4.72k
{
161
4.72k
  mxNanResultIfNoArg;
162
3.93k
  fxToNumber(the, mxArgv(0));
163
3.93k
  mxResult->kind = XS_NUMBER_KIND;
164
3.93k
  mxResult->value.number = c_atan(mxArgv(0)->value.number);
165
3.93k
}
166
167
void fx_Math_atanh(txMachine* the)
168
58
{
169
58
  mxNanResultIfNoArg;
170
58
  fxToNumber(the, mxArgv(0));
171
58
  mxResult->kind = XS_NUMBER_KIND;
172
58
  mxResult->value.number = c_atanh(mxArgv(0)->value.number);
173
58
}
174
175
void fx_Math_atan2(txMachine* the)
176
478k
{
177
478k
  mxNanResultIfNoArg2;
178
478k
  fxToNumber(the, mxArgv(0));
179
478k
  fxToNumber(the, mxArgv(1));
180
478k
  mxResult->kind = XS_NUMBER_KIND;
181
478k
  mxResult->value.number = c_atan2(mxArgv(0)->value.number, mxArgv(1)->value.number);
182
478k
}
183
184
void fx_Math_cbrt(txMachine* the)
185
15
{
186
15
  mxNanResultIfNoArg;
187
15
  fxToNumber(the, mxArgv(0));
188
15
  mxResult->kind = XS_NUMBER_KIND;
189
15
  mxResult->value.number = c_cbrt(mxArgv(0)->value.number);
190
15
}
191
192
void fx_Math_ceil(txMachine* the)
193
63.7k
{
194
63.7k
  mxNanResultIfNoArg;
195
63.7k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
196
4
    mxResult->kind = XS_INTEGER_KIND;
197
4
    mxResult->value.integer = mxArgv(0)->value.integer;
198
4
    return;
199
4
  }
200
63.7k
  fxToNumber(the, mxArgv(0));
201
63.7k
  mxResult->kind = XS_NUMBER_KIND;
202
63.7k
  mxResult->value.number = c_ceil(mxArgv(0)->value.number);
203
63.7k
  fx_Math_toInteger(the);
204
63.7k
}
205
206
void fx_Math_clz32(txMachine* the)
207
47
{
208
47
  txUnsigned x = (mxArgc > 0) ? fxToUnsigned(the, mxArgv(0)) : 0;
209
47
  txInteger r;
210
47
  if (x)
211
#if mxWindows
212
  {
213
    _BitScanReverse(&r, x);
214
    r = 31 - r;
215
  }
216
#else
217
36
    r = __builtin_clz(x);
218
11
#endif  
219
11
  else
220
11
    r = 32;
221
47
  mxResult->kind = XS_INTEGER_KIND;
222
47
  mxResult->value.integer = r;
223
47
}
224
225
void fx_Math_cos(txMachine* the)
226
170k
{
227
170k
  mxNanResultIfNoArg;
228
170k
  fxToNumber(the, mxArgv(0));
229
170k
  mxResult->kind = XS_NUMBER_KIND;
230
170k
  mxResult->value.number = c_cos(mxArgv(0)->value.number);
231
170k
}
232
233
void fx_Math_cosh(txMachine* the)
234
2.66k
{
235
2.66k
  mxNanResultIfNoArg;
236
2.23k
  fxToNumber(the, mxArgv(0));
237
2.23k
  mxResult->kind = XS_NUMBER_KIND;
238
2.23k
  mxResult->value.number = c_cosh(mxArgv(0)->value.number);
239
2.23k
}
240
241
void fx_Math_exp(txMachine* the)
242
116k
{
243
116k
  mxNanResultIfNoArg;
244
116k
  fxToNumber(the, mxArgv(0));
245
116k
  mxResult->kind = XS_NUMBER_KIND;
246
116k
  mxResult->value.number = c_exp(mxArgv(0)->value.number);
247
116k
}
248
249
void fx_Math_expm1(txMachine* the)
250
21
{
251
21
  mxNanResultIfNoArg;
252
21
  fxToNumber(the, mxArgv(0));
253
21
  mxResult->kind = XS_NUMBER_KIND;
254
21
  mxResult->value.number = c_expm1(mxArgv(0)->value.number);
255
21
}
256
257
void fx_Math_floor(txMachine* the)
258
192k
{
259
192k
  mxNanResultIfNoArg;
260
192k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
261
66.8k
    mxResult->kind = XS_INTEGER_KIND;
262
66.8k
    mxResult->value.integer = mxArgv(0)->value.integer;
263
66.8k
    return;
264
66.8k
  }
265
125k
  fxToNumber(the, mxArgv(0));
266
125k
  mxResult->kind = XS_NUMBER_KIND;
267
125k
  mxResult->value.number = c_floor(mxArgv(0)->value.number);
268
125k
  fx_Math_toInteger(the);
269
125k
}
270
271
void fx_Math_fround(txMachine* the)
272
53
{
273
53
  float arg;
274
53
  mxNanResultIfNoArg;
275
52
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
276
6
    mxResult->kind = XS_INTEGER_KIND;
277
6
    mxResult->value.integer = mxArgv(0)->value.integer;
278
6
    return;
279
6
  }
280
46
  arg = (float)fxToNumber(the, mxArgv(0));
281
46
  mxResult->kind = XS_NUMBER_KIND;
282
46
  mxResult->value.number = arg;
283
46
}
284
285
void fx_Math_hypot(txMachine* the)
286
39
{
287
39
  if (mxArgc == 2) {
288
20
    fxToNumber(the, mxArgv(0));
289
20
    fxToNumber(the, mxArgv(1));
290
20
    mxResult->kind = XS_NUMBER_KIND;
291
20
    mxResult->value.number = c_hypot(mxArgv(0)->value.number, mxArgv(1)->value.number);
292
20
  }
293
19
  else {
294
19
    txInteger c = mxArgc, i;
295
19
    txNumber result = 0;
296
47
    for (i = 0; i < c; i++) {
297
28
      txNumber argument = fxToNumber(the, mxArgv(i));
298
28
      result += argument * argument;
299
28
    }
300
19
    mxResult->kind = XS_NUMBER_KIND;
301
19
    mxResult->value.number = c_sqrt(result);
302
19
  }
303
39
}
304
305
void fx_Math_idiv(txMachine* the)
306
26.3k
{
307
26.3k
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
308
26.3k
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
309
26.3k
  if (y == 0) {
310
25.7k
    mxResult->kind = XS_NUMBER_KIND;
311
25.7k
    mxResult->value.number = C_NAN;
312
25.7k
  }
313
529
  else {
314
529
    mxResult->kind = XS_INTEGER_KIND;
315
529
#if mxIntegerDivideOverflowException
316
529
    if ((x == (txInteger)0x80000000) && (y == -1))
317
4
      mxResult->value.integer = x;
318
525
    else
319
525
#endif
320
525
      mxResult->value.integer = x / y;
321
529
  }
322
26.3k
}
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
26.2k
{
350
26.2k
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
351
26.2k
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
352
26.2k
  mxResult->kind = XS_INTEGER_KIND;
353
26.2k
  mxResult->value.integer = x * y;
354
26.2k
}
355
356
void fx_Math_imuldiv(txMachine* the)
357
52.4k
{
358
52.4k
  txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
359
52.4k
  txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
360
52.4k
  txS8 z = (mxArgc > 2) ? fxToInteger(the, mxArgv(2)) : 0;
361
52.4k
  if (z == 0) {
362
26.3k
    mxResult->kind = XS_NUMBER_KIND;
363
26.3k
    mxResult->value.number = C_NAN;
364
26.3k
  }
365
26.1k
  else {
366
26.1k
    txS8 r = (x * y) / z;
367
26.1k
    mxResult->kind = XS_INTEGER_KIND;
368
26.1k
    mxResult->value.integer = (txInteger)(r & 0x00000000FFFFFFFF);
369
26.1k
  }
370
52.4k
}
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
21
{
420
21
  mxNanResultIfNoArg;
421
20
  fxToNumber(the, mxArgv(0));
422
20
  mxResult->kind = XS_NUMBER_KIND;
423
20
  mxResult->value.number = c_log(mxArgv(0)->value.number);
424
20
}
425
426
void fx_Math_log1p(txMachine* the)
427
297
{
428
297
  mxNanResultIfNoArg;
429
296
  fxToNumber(the, mxArgv(0));
430
296
  mxResult->kind = XS_NUMBER_KIND;
431
296
  mxResult->value.number = c_log1p(mxArgv(0)->value.number);
432
296
}
433
434
void fx_Math_log10(txMachine* the)
435
42
{
436
42
  mxNanResultIfNoArg;
437
41
  fxToNumber(the, mxArgv(0));
438
41
  mxResult->kind = XS_NUMBER_KIND;
439
41
  mxResult->value.number = c_log10(mxArgv(0)->value.number);
440
41
}
441
442
void fx_Math_log2(txMachine* the)
443
25
{
444
25
  mxNanResultIfNoArg;
445
25
  fxToNumber(the, mxArgv(0));
446
25
  mxResult->kind = XS_NUMBER_KIND;
447
#if mxAndroid
448
  mxResult->value.number = c_log(mxArgv(0)->value.number) / c_log(2);
449
#else
450
25
  mxResult->value.number = c_log2(mxArgv(0)->value.number);
451
25
#endif
452
25
}
453
454
void fx_Math_max(txMachine* the)
455
2.47k
{
456
2.47k
  txInteger c = mxArgc, i = 0;
457
2.47k
  mxResult->kind = XS_NUMBER_KIND;
458
2.47k
  mxResult->value.number = -((txNumber)C_INFINITY);
459
2.47k
  if (0 == c)
460
1
    return;
461
462
2.47k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
463
1.73k
    mxResult->kind = XS_INTEGER_KIND;
464
1.73k
    mxResult->value.integer = mxArgv(0)->value.integer;
465
1.73k
    i = 1;
466
1.73k
  }
467
468
6.16k
  for (; i < c; i++) {
469
4.29k
    txSlot *slot = mxArgv(i);
470
4.29k
    if (XS_INTEGER_KIND == mxResult->kind) {
471
2.36k
      if (XS_INTEGER_KIND == slot->kind) {
472
1.79k
        if (mxResult->value.integer < slot->value.integer) {
473
1.18k
          mxResult->value.integer = slot->value.integer;
474
1.18k
        }
475
1.79k
        continue;
476
1.79k
      }
477
561
      mxResult->kind = XS_NUMBER_KIND;
478
561
      mxResult->value.number = mxResult->value.integer;
479
561
    }
480
481
2.50k
    txNumber n = fxToNumber(the, slot);
482
2.50k
    if (c_isnan(n)) {
483
2.00k
      for (; i < c; i++)
484
1.39k
        fxToNumber(the, mxArgv(i));
485
607
      mxResult->value.number = C_NAN;
486
607
      return;
487
607
    }
488
1.89k
    if (mxResult->value.number < n)
489
412
      mxResult->value.number = n;
490
1.48k
    else if ((mxResult->value.number == 0) && (n == 0)) {
491
225
      if (c_signbit(mxResult->value.number) != c_signbit(n))
492
45
        mxResult->value.number = 0;
493
225
    }
494
1.89k
  }
495
2.47k
}
496
497
void fx_Math_min(txMachine* the)
498
2.35k
{
499
2.35k
  txInteger c = mxArgc, i = 0;
500
2.35k
  mxResult->kind = XS_NUMBER_KIND;
501
2.35k
  mxResult->value.number = (txNumber)C_INFINITY;
502
2.35k
  if (0 == c)
503
1.49k
    return;
504
505
862
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
506
457
    mxResult->kind = XS_INTEGER_KIND;
507
457
    mxResult->value.integer = mxArgv(0)->value.integer;
508
457
    i = 1;
509
457
  }
510
511
1.86k
  for (; i < c; i++) {
512
1.58k
    txSlot *slot = mxArgv(i);
513
1.58k
    if (XS_INTEGER_KIND == mxResult->kind) {
514
572
      if (XS_INTEGER_KIND == slot->kind) {
515
284
        if (mxResult->value.integer > slot->value.integer)
516
21
          mxResult->value.integer = slot->value.integer;
517
284
        continue;
518
284
      }
519
288
      mxResult->kind = XS_NUMBER_KIND;
520
288
      mxResult->value.number = mxResult->value.integer;
521
288
    }
522
  
523
1.29k
    txNumber n = fxToNumber(the, slot);
524
1.29k
    if (c_isnan(n)) {
525
1.48k
      for (; i < c; i++)
526
908
        fxToNumber(the, mxArgv(i));
527
574
      mxResult->value.number = C_NAN;
528
574
      return;
529
574
    }
530
723
    if (mxResult->value.number > n)
531
412
      mxResult->value.number = n;
532
311
    else if ((mxResult->value.number == 0) && (n == 0)) {
533
102
      if (c_signbit(mxResult->value.number) != c_signbit(n))
534
83
        mxResult->value.number = -0.0;
535
102
    }
536
723
  }
537
862
}
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
1.81M
{
549
1.81M
  if (!c_isfinite(y) && (c_fabs(x) == 1.0))
550
578
    return C_NAN;
551
1.81M
  return c_pow(x, y);
552
1.81M
}
553
554
void fx_Math_pow(txMachine* the)
555
108k
{
556
108k
  txNumber x, y;
557
108k
  mxNanResultIfNoArg2;
558
108k
  x = fxToNumber(the, mxArgv(0));
559
108k
  y = fxToNumber(the, mxArgv(1));
560
108k
  mxResult->kind = XS_NUMBER_KIND;
561
108k
  mxResult->value.number = fx_pow(x, y);
562
108k
}
563
564
void fx_Math_random(txMachine* the)
565
68.6k
{
566
68.6k
  uint32_t result;
567
68.6k
  do {
568
68.6k
    result = c_rand();
569
68.6k
  } while (result == C_RAND_MAX);
570
68.6k
  mxResult->kind = XS_NUMBER_KIND;
571
68.6k
  mxResult->value.number = (double)result / (double)C_RAND_MAX;
572
68.6k
}
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
61.9k
{
581
61.9k
    txNumber arg;
582
61.9k
  mxNanResultIfNoArg;
583
61.9k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
584
3
    mxResult->kind = XS_INTEGER_KIND;
585
3
    mxResult->value.integer = mxArgv(0)->value.integer;
586
3
    return;
587
3
  }
588
61.9k
  arg = fxToNumber(the, mxArgv(0));
589
61.9k
  if (c_isnormal(arg) && (-4503599627370495 < arg) && (arg < 4503599627370495)) { // 2 ** 52 - 1
590
13.0k
    if ((arg < -0.5) || (0.5 <= arg))
591
12.8k
      arg = c_floor(arg + 0.5);
592
203
    else if (arg < 0)
593
98
      arg = -0.0;
594
105
    else if (arg > 0)
595
105
      arg = 0.0;
596
13.0k
    }
597
61.9k
  mxResult->kind = XS_NUMBER_KIND;
598
61.9k
  mxResult->value.number = arg;
599
61.9k
  fx_Math_toInteger(the);
600
61.9k
}
601
602
void fx_Math_sqrt(txMachine* the)
603
1.20k
{
604
1.20k
  mxNanResultIfNoArg;
605
1.20k
  fxToNumber(the, mxArgv(0));
606
1.20k
  mxResult->kind = XS_NUMBER_KIND;
607
1.20k
  mxResult->value.number = c_sqrt(mxArgv(0)->value.number);
608
1.20k
}
609
610
void fx_Math_sign(txMachine* the)
611
8.37k
{
612
8.37k
  txNumber arg;
613
8.37k
  mxNanResultIfNoArg;
614
8.27k
  arg = fxToNumber(the, mxArgv(0));
615
8.27k
  mxResult->kind = XS_NUMBER_KIND;
616
8.27k
  if (c_isnan(arg))
617
5.47k
    mxResult->value.number = C_NAN;
618
2.80k
  else if (arg < 0)
619
828
    mxResult->value.number = -1;
620
1.97k
  else if (arg > 0)
621
657
    mxResult->value.number = 1;
622
1.32k
  else
623
1.32k
    mxResult->value.number = arg;
624
8.27k
  fx_Math_toInteger(the);
625
8.27k
}
626
627
void fx_Math_sin(txMachine* the)
628
6.80k
{
629
6.80k
  mxNanResultIfNoArg;
630
6.01k
  fxToNumber(the, mxArgv(0));
631
6.01k
  mxResult->kind = XS_NUMBER_KIND;
632
6.01k
  mxResult->value.number = c_sin(mxArgv(0)->value.number);
633
6.01k
}
634
635
void fx_Math_sinh(txMachine* the)
636
81.3k
{
637
81.3k
  mxNanResultIfNoArg;
638
81.3k
  fxToNumber(the, mxArgv(0));
639
81.3k
  mxResult->kind = XS_NUMBER_KIND;
640
81.3k
  mxResult->value.number = c_sinh(mxArgv(0)->value.number);
641
81.3k
}
642
643
void fx_Math_tan(txMachine* the)
644
6.13k
{
645
6.13k
  mxNanResultIfNoArg;
646
5.96k
  fxToNumber(the, mxArgv(0));
647
5.96k
  mxResult->kind = XS_NUMBER_KIND;
648
5.96k
  mxResult->value.number = c_tan(mxArgv(0)->value.number);
649
5.96k
}
650
651
void fx_Math_tanh(txMachine* the)
652
3.78k
{
653
3.78k
  mxNanResultIfNoArg;
654
3.39k
  fxToNumber(the, mxArgv(0));
655
3.39k
  mxResult->kind = XS_NUMBER_KIND;
656
3.39k
  mxResult->value.number = c_tanh(mxArgv(0)->value.number);
657
3.39k
}
658
659
void fx_Math_trunc(txMachine* the)
660
96
{
661
96
  mxNanResultIfNoArg;
662
94
  fxToNumber(the, mxArgv(0));
663
94
  mxResult->kind = XS_NUMBER_KIND;
664
94
  mxResult->value.number = c_trunc(mxArgv(0)->value.number);
665
94
  fx_Math_toInteger(the);
666
94
}
667
668
void fx_Math_toInteger(txMachine* the)
669
511k
{
670
511k
  txNumber number = mxResult->value.number;
671
511k
  txInteger integer = (txInteger)number;
672
511k
  txNumber check = integer;
673
511k
  if ((number == check) && (number || !c_signbit(number))) {
674
261k
    mxResult->value.integer = integer;
675
261k
    mxResult->kind = XS_INTEGER_KIND;
676
261k
  }
677
511k
}