Coverage Report

Created: 2025-11-09 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsMath.c
Line
Count
Source
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
24.9k
{
42
24.9k
  txSlot* slot;
43
24.9k
  mxPush(mxObjectPrototype);
44
24.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
45
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_abs), 1, mxID(_abs), XS_DONT_ENUM_FLAG);
46
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acos), 1, mxID(_acos), XS_DONT_ENUM_FLAG);
47
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acosh), 1, mxID(_acosh), XS_DONT_ENUM_FLAG);
48
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asin), 1, mxID(_asin), XS_DONT_ENUM_FLAG);
49
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asinh), 1, mxID(_asinh), XS_DONT_ENUM_FLAG);
50
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan), 1, mxID(_atan), XS_DONT_ENUM_FLAG);
51
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atanh), 1, mxID(_atanh), XS_DONT_ENUM_FLAG);
52
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan2), 2, mxID(_atan2), XS_DONT_ENUM_FLAG);
53
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cbrt), 1, mxID(_cbrt), XS_DONT_ENUM_FLAG);
54
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_ceil), 1, mxID(_ceil), XS_DONT_ENUM_FLAG);
55
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_clz32), 1, mxID(_clz32), XS_DONT_ENUM_FLAG);
56
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cos), 1, mxID(_cos), XS_DONT_ENUM_FLAG);
57
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cosh), 1, mxID(_cosh), XS_DONT_ENUM_FLAG);
58
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_exp), 1, mxID(_exp), XS_DONT_ENUM_FLAG);
59
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_expm1), 1, mxID(_expm1), XS_DONT_ENUM_FLAG);
60
24.9k
#if mxFloat16
61
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_f16round), 1, mxID(_f16round), XS_DONT_ENUM_FLAG);
62
24.9k
#endif
63
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_floor), 1, mxID(_floor), XS_DONT_ENUM_FLAG);
64
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_fround), 1, mxID(_fround), XS_DONT_ENUM_FLAG);
65
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_hypot), 2, mxID(_hypot_), XS_DONT_ENUM_FLAG);
66
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idiv), 2, mxID(_idiv), XS_DONT_ENUM_FLAG);
67
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idivmod), 2, mxID(_idivmod), XS_DONT_ENUM_FLAG);
68
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imod), 2, mxID(_imod), XS_DONT_ENUM_FLAG);
69
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imul), 2, mxID(_imul), XS_DONT_ENUM_FLAG);
70
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imuldiv), 2, mxID(_imuldiv), XS_DONT_ENUM_FLAG);
71
24.9k
#if mxECMAScript2023
72
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irandom), 0, mxID(_irandom), XS_DONT_ENUM_FLAG);
73
24.9k
#endif
74
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irem), 2, mxID(_irem), XS_DONT_ENUM_FLAG);
75
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log), 1, mxID(_log), XS_DONT_ENUM_FLAG);
76
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log1p), 1, mxID(_log1p), XS_DONT_ENUM_FLAG);
77
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log10), 1, mxID(_log10), XS_DONT_ENUM_FLAG);
78
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log2), 1, mxID(_log2), XS_DONT_ENUM_FLAG);
79
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_max), 2, mxID(_max), XS_DONT_ENUM_FLAG);
80
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_min), 2, mxID(_min), XS_DONT_ENUM_FLAG);
81
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_mod), 2, mxID(_mod), XS_DONT_ENUM_FLAG);
82
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_pow), 2, mxID(_pow), XS_DONT_ENUM_FLAG);
83
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_random), 0, mxID(_random), XS_DONT_ENUM_FLAG);
84
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_round), 1, mxID(_round), XS_DONT_ENUM_FLAG);
85
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sign), 1, mxID(_sign), XS_DONT_ENUM_FLAG);
86
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sin), 1, mxID(_sin), XS_DONT_ENUM_FLAG);
87
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sinh), 1, mxID(_sinh), XS_DONT_ENUM_FLAG);
88
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sqrt), 1, mxID(_sqrt), XS_DONT_ENUM_FLAG);
89
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tan), 1, mxID(_tan), XS_DONT_ENUM_FLAG);
90
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tanh), 1, mxID(_tanh), XS_DONT_ENUM_FLAG);
91
24.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_trunc), 1, mxID(_trunc), XS_DONT_ENUM_FLAG);
92
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_E, mxID(_E), XS_GET_ONLY);
93
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_LN10, mxID(_LN10), XS_GET_ONLY);
94
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_LN2, mxID(_LN2), XS_GET_ONLY);
95
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_LOG10E, mxID(_LOG10E), XS_GET_ONLY);
96
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_LOG2E, mxID(_LOG2E), XS_GET_ONLY);
97
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_PI, mxID(_PI), XS_GET_ONLY);
98
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT1_2, mxID(_SQRT1_2), XS_GET_ONLY);
99
24.9k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT2, mxID(_SQRT2), XS_GET_ONLY);
100
24.9k
  slot = fxNextStringXProperty(the, slot, "Math", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
101
24.9k
  mxPull(mxMathObject);
102
//@@  c_srand((unsigned)c_time(0));
103
24.9k
}
104
105
#define mxNanResultIfNoArg \
106
1.14M
  if (mxArgc < 1) {  \
107
7.65k
    mxResult->kind = XS_NUMBER_KIND; \
108
7.65k
    mxResult->value.number = C_NAN; \
109
7.65k
    return; \
110
7.65k
  }
111
112
#define mxNanResultIfNoArg2 \
113
719k
  if (mxArgc < 2) {  \
114
60
    mxResult->kind = XS_NUMBER_KIND; \
115
60
    mxResult->value.number = C_NAN; \
116
60
    return; \
117
60
  }
118
119
void fx_Math_abs(txMachine* the)
120
12
{
121
12
  mxNanResultIfNoArg;
122
12
  fxToNumber(the, mxArgv(0));
123
12
  mxResult->kind = XS_NUMBER_KIND;
124
12
  mxResult->value.number = c_fabs(mxArgv(0)->value.number);
125
12
}
126
127
void fx_Math_acos(txMachine* the)
128
6.19k
{
129
6.19k
  mxNanResultIfNoArg;
130
5.42k
  fxToNumber(the, mxArgv(0));
131
5.42k
  mxResult->kind = XS_NUMBER_KIND;
132
5.42k
  mxResult->value.number = c_acos(mxArgv(0)->value.number);
133
5.42k
}
134
135
void fx_Math_acosh(txMachine* the)
136
82.8k
{
137
82.8k
  mxNanResultIfNoArg;
138
81.7k
  fxToNumber(the, mxArgv(0));
139
81.7k
  mxResult->kind = XS_NUMBER_KIND;
140
81.7k
  mxResult->value.number = c_acosh(mxArgv(0)->value.number);
141
81.7k
}
142
143
void fx_Math_asin(txMachine* the)
144
5.57k
{
145
5.57k
  mxNanResultIfNoArg;
146
4.45k
  fxToNumber(the, mxArgv(0));
147
4.45k
  mxResult->kind = XS_NUMBER_KIND;
148
4.45k
  mxResult->value.number = c_asin(mxArgv(0)->value.number);
149
4.45k
}
150
151
void fx_Math_asinh(txMachine* the)
152
55.1k
{
153
55.1k
  mxNanResultIfNoArg;
154
55.1k
  fxToNumber(the, mxArgv(0));
155
55.1k
  mxResult->kind = XS_NUMBER_KIND;
156
55.1k
  mxResult->value.number = c_asinh(mxArgv(0)->value.number);
157
55.1k
}
158
159
void fx_Math_atan(txMachine* the)
160
3.50k
{
161
3.50k
  mxNanResultIfNoArg;
162
2.46k
  fxToNumber(the, mxArgv(0));
163
2.46k
  mxResult->kind = XS_NUMBER_KIND;
164
2.46k
  mxResult->value.number = c_atan(mxArgv(0)->value.number);
165
2.46k
}
166
167
void fx_Math_atanh(txMachine* the)
168
181k
{
169
181k
  mxNanResultIfNoArg;
170
181k
  fxToNumber(the, mxArgv(0));
171
181k
  mxResult->kind = XS_NUMBER_KIND;
172
181k
  mxResult->value.number = c_atanh(mxArgv(0)->value.number);
173
181k
}
174
175
void fx_Math_atan2(txMachine* the)
176
611k
{
177
611k
  mxNanResultIfNoArg2;
178
611k
  fxToNumber(the, mxArgv(0));
179
611k
  fxToNumber(the, mxArgv(1));
180
611k
  mxResult->kind = XS_NUMBER_KIND;
181
611k
  mxResult->value.number = c_atan2(mxArgv(0)->value.number, mxArgv(1)->value.number);
182
611k
}
183
184
void fx_Math_cbrt(txMachine* the)
185
10
{
186
10
  mxNanResultIfNoArg;
187
10
  fxToNumber(the, mxArgv(0));
188
10
  mxResult->kind = XS_NUMBER_KIND;
189
10
  mxResult->value.number = c_cbrt(mxArgv(0)->value.number);
190
10
}
191
192
void fx_Math_ceil(txMachine* the)
193
67.0k
{
194
67.0k
  mxNanResultIfNoArg;
195
67.0k
  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
67.0k
  fxToNumber(the, mxArgv(0));
201
67.0k
  mxResult->kind = XS_NUMBER_KIND;
202
67.0k
  mxResult->value.number = c_ceil(mxArgv(0)->value.number);
203
67.0k
  fx_Math_toInteger(the);
204
67.0k
}
205
206
void fx_Math_clz32(txMachine* the)
207
55
{
208
55
  txUnsigned x = (mxArgc > 0) ? fxToUnsigned(the, mxArgv(0)) : 0;
209
55
  txInteger r;
210
55
  if (x)
211
#if mxWindows
212
  {
213
    _BitScanReverse(&r, x);
214
    r = 31 - r;
215
  }
216
#else
217
43
    r = __builtin_clz(x);
218
12
#endif  
219
12
  else
220
12
    r = 32;
221
55
  mxResult->kind = XS_INTEGER_KIND;
222
55
  mxResult->value.integer = r;
223
55
}
224
225
void fx_Math_cos(txMachine* the)
226
224k
{
227
224k
  mxNanResultIfNoArg;
228
224k
  fxToNumber(the, mxArgv(0));
229
224k
  mxResult->kind = XS_NUMBER_KIND;
230
224k
  mxResult->value.number = c_cos(mxArgv(0)->value.number);
231
224k
}
232
233
void fx_Math_cosh(txMachine* the)
234
5.34k
{
235
5.34k
  mxNanResultIfNoArg;
236
4.62k
  fxToNumber(the, mxArgv(0));
237
4.62k
  mxResult->kind = XS_NUMBER_KIND;
238
4.62k
  mxResult->value.number = c_cosh(mxArgv(0)->value.number);
239
4.62k
}
240
241
void fx_Math_exp(txMachine* the)
242
62.6k
{
243
62.6k
  mxNanResultIfNoArg;
244
62.0k
  fxToNumber(the, mxArgv(0));
245
62.0k
  mxResult->kind = XS_NUMBER_KIND;
246
62.0k
  mxResult->value.number = c_exp(mxArgv(0)->value.number);
247
62.0k
}
248
249
void fx_Math_expm1(txMachine* the)
250
31
{
251
31
  mxNanResultIfNoArg;
252
31
  fxToNumber(the, mxArgv(0));
253
31
  mxResult->kind = XS_NUMBER_KIND;
254
31
  mxResult->value.number = c_expm1(mxArgv(0)->value.number);
255
31
}
256
257
void fx_Math_floor(txMachine* the)
258
237k
{
259
237k
  mxNanResultIfNoArg;
260
237k
  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
170k
  fxToNumber(the, mxArgv(0));
266
170k
  mxResult->kind = XS_NUMBER_KIND;
267
170k
  mxResult->value.number = c_floor(mxArgv(0)->value.number);
268
170k
  fx_Math_toInteger(the);
269
170k
}
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
19
{
287
19
  if (mxArgc == 2) {
288
12
    fxToNumber(the, mxArgv(0));
289
12
    fxToNumber(the, mxArgv(1));
290
12
    mxResult->kind = XS_NUMBER_KIND;
291
12
    mxResult->value.number = c_hypot(mxArgv(0)->value.number, mxArgv(1)->value.number);
292
12
  }
293
7
  else {
294
7
    txInteger c = mxArgc, i;
295
7
    txNumber result = 0;
296
15
    for (i = 0; i < c; i++) {
297
8
      txNumber argument = fxToNumber(the, mxArgv(i));
298
8
      result += argument * argument;
299
8
    }
300
7
    mxResult->kind = XS_NUMBER_KIND;
301
7
    mxResult->value.number = c_sqrt(result);
302
7
  }
303
19
}
304
305
void fx_Math_idiv(txMachine* the)
306
26.2k
{
307
26.2k
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
308
26.2k
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
309
26.2k
  if (y == 0) {
310
25.7k
    mxResult->kind = XS_NUMBER_KIND;
311
25.7k
    mxResult->value.number = C_NAN;
312
25.7k
  }
313
500
  else {
314
500
    mxResult->kind = XS_INTEGER_KIND;
315
500
#if mxIntegerDivideOverflowException
316
500
    if ((x == (txInteger)0x80000000) && (y == -1))
317
2
      mxResult->value.integer = x;
318
498
    else
319
498
#endif
320
498
      mxResult->value.integer = x / y;
321
500
  }
322
26.2k
}
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
20
{
420
20
  mxNanResultIfNoArg;
421
19
  fxToNumber(the, mxArgv(0));
422
19
  mxResult->kind = XS_NUMBER_KIND;
423
19
  mxResult->value.number = c_log(mxArgv(0)->value.number);
424
19
}
425
426
void fx_Math_log1p(txMachine* the)
427
999
{
428
999
  mxNanResultIfNoArg;
429
996
  fxToNumber(the, mxArgv(0));
430
996
  mxResult->kind = XS_NUMBER_KIND;
431
996
  mxResult->value.number = c_log1p(mxArgv(0)->value.number);
432
996
}
433
434
void fx_Math_log10(txMachine* the)
435
73
{
436
73
  mxNanResultIfNoArg;
437
72
  fxToNumber(the, mxArgv(0));
438
72
  mxResult->kind = XS_NUMBER_KIND;
439
72
  mxResult->value.number = c_log10(mxArgv(0)->value.number);
440
72
}
441
442
void fx_Math_log2(txMachine* the)
443
23
{
444
23
  mxNanResultIfNoArg;
445
23
  fxToNumber(the, mxArgv(0));
446
23
  mxResult->kind = XS_NUMBER_KIND;
447
#if mxAndroid
448
  mxResult->value.number = c_log(mxArgv(0)->value.number) / c_log(2);
449
#else
450
23
  mxResult->value.number = c_log2(mxArgv(0)->value.number);
451
23
#endif
452
23
}
453
454
void fx_Math_max(txMachine* the)
455
2.32k
{
456
2.32k
  txInteger c = mxArgc, i = 0;
457
2.32k
  mxResult->kind = XS_NUMBER_KIND;
458
2.32k
  mxResult->value.number = -((txNumber)C_INFINITY);
459
2.32k
  if (0 == c)
460
14
    return;
461
462
2.30k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
463
1.64k
    mxResult->kind = XS_INTEGER_KIND;
464
1.64k
    mxResult->value.integer = mxArgv(0)->value.integer;
465
1.64k
    i = 1;
466
1.64k
  }
467
468
5.15k
  for (; i < c; i++) {
469
3.33k
    txSlot *slot = mxArgv(i);
470
3.33k
    if (XS_INTEGER_KIND == mxResult->kind) {
471
1.95k
      if (XS_INTEGER_KIND == slot->kind) {
472
1.64k
        if (mxResult->value.integer < slot->value.integer) {
473
1.10k
          mxResult->value.integer = slot->value.integer;
474
1.10k
        }
475
1.64k
        continue;
476
1.64k
      }
477
308
      mxResult->kind = XS_NUMBER_KIND;
478
308
      mxResult->value.number = mxResult->value.integer;
479
308
    }
480
481
1.68k
    txNumber n = fxToNumber(the, slot);
482
1.68k
    if (c_isnan(n)) {
483
1.61k
      for (; i < c; i++)
484
1.12k
        fxToNumber(the, mxArgv(i));
485
489
      mxResult->value.number = C_NAN;
486
489
      return;
487
489
    }
488
1.19k
    if (mxResult->value.number < n)
489
392
      mxResult->value.number = n;
490
807
    else if ((mxResult->value.number == 0) && (n == 0)) {
491
245
      if (c_signbit(mxResult->value.number) != c_signbit(n))
492
66
        mxResult->value.number = 0;
493
245
    }
494
1.19k
  }
495
2.30k
}
496
497
void fx_Math_min(txMachine* the)
498
1.68k
{
499
1.68k
  txInteger c = mxArgc, i = 0;
500
1.68k
  mxResult->kind = XS_NUMBER_KIND;
501
1.68k
  mxResult->value.number = (txNumber)C_INFINITY;
502
1.68k
  if (0 == c)
503
955
    return;
504
505
730
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
506
379
    mxResult->kind = XS_INTEGER_KIND;
507
379
    mxResult->value.integer = mxArgv(0)->value.integer;
508
379
    i = 1;
509
379
  }
510
511
1.99k
  for (; i < c; i++) {
512
1.82k
    txSlot *slot = mxArgv(i);
513
1.82k
    if (XS_INTEGER_KIND == mxResult->kind) {
514
666
      if (XS_INTEGER_KIND == slot->kind) {
515
326
        if (mxResult->value.integer > slot->value.integer)
516
54
          mxResult->value.integer = slot->value.integer;
517
326
        continue;
518
326
      }
519
340
      mxResult->kind = XS_NUMBER_KIND;
520
340
      mxResult->value.number = mxResult->value.integer;
521
340
    }
522
  
523
1.49k
    txNumber n = fxToNumber(the, slot);
524
1.49k
    if (c_isnan(n)) {
525
1.34k
      for (; i < c; i++)
526
784
        fxToNumber(the, mxArgv(i));
527
565
      mxResult->value.number = C_NAN;
528
565
      return;
529
565
    }
530
934
    if (mxResult->value.number > n)
531
541
      mxResult->value.number = n;
532
393
    else if ((mxResult->value.number == 0) && (n == 0)) {
533
142
      if (c_signbit(mxResult->value.number) != c_signbit(n))
534
62
        mxResult->value.number = -0.0;
535
142
    }
536
934
  }
537
730
}
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.92M
{
549
1.92M
  if (!c_isfinite(y) && (c_fabs(x) == 1.0))
550
551
    return C_NAN;
551
1.92M
  return c_pow(x, y);
552
1.92M
}
553
554
void fx_Math_pow(txMachine* the)
555
107k
{
556
107k
  txNumber x, y;
557
107k
  mxNanResultIfNoArg2;
558
107k
  x = fxToNumber(the, mxArgv(0));
559
107k
  y = fxToNumber(the, mxArgv(1));
560
107k
  mxResult->kind = XS_NUMBER_KIND;
561
107k
  mxResult->value.number = fx_pow(x, y);
562
107k
}
563
564
void fx_Math_random(txMachine* the)
565
14.7k
{
566
14.7k
  uint32_t result;
567
14.7k
  do {
568
14.7k
    result = c_rand();
569
14.7k
  } while (result == C_RAND_MAX);
570
14.7k
  mxResult->kind = XS_NUMBER_KIND;
571
14.7k
  mxResult->value.number = (double)result / (double)C_RAND_MAX;
572
14.7k
}
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
103k
{
581
103k
    txNumber arg;
582
103k
  mxNanResultIfNoArg;
583
103k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
584
19
    mxResult->kind = XS_INTEGER_KIND;
585
19
    mxResult->value.integer = mxArgv(0)->value.integer;
586
19
    return;
587
19
  }
588
103k
  arg = fxToNumber(the, mxArgv(0));
589
103k
  if (c_isnormal(arg) && (-4503599627370495 < arg) && (arg < 4503599627370495)) { // 2 ** 52 - 1
590
48.0k
    if ((arg < -0.5) || (0.5 <= arg))
591
4.93k
      arg = c_floor(arg + 0.5);
592
43.0k
    else if (arg < 0)
593
1.05k
      arg = -0.0;
594
42.0k
    else if (arg > 0)
595
42.0k
      arg = 0.0;
596
48.0k
    }
597
103k
  mxResult->kind = XS_NUMBER_KIND;
598
103k
  mxResult->value.number = arg;
599
103k
  fx_Math_toInteger(the);
600
103k
}
601
602
void fx_Math_sqrt(txMachine* the)
603
1.00k
{
604
1.00k
  mxNanResultIfNoArg;
605
1.00k
  fxToNumber(the, mxArgv(0));
606
1.00k
  mxResult->kind = XS_NUMBER_KIND;
607
1.00k
  mxResult->value.number = c_sqrt(mxArgv(0)->value.number);
608
1.00k
}
609
610
void fx_Math_sign(txMachine* the)
611
10.9k
{
612
10.9k
  txNumber arg;
613
10.9k
  mxNanResultIfNoArg;
614
10.4k
  arg = fxToNumber(the, mxArgv(0));
615
10.4k
  mxResult->kind = XS_NUMBER_KIND;
616
10.4k
  if (c_isnan(arg))
617
5.48k
    mxResult->value.number = C_NAN;
618
4.96k
  else if (arg < 0)
619
751
    mxResult->value.number = -1;
620
4.21k
  else if (arg > 0)
621
1.50k
    mxResult->value.number = 1;
622
2.71k
  else
623
2.71k
    mxResult->value.number = arg;
624
10.4k
  fx_Math_toInteger(the);
625
10.4k
}
626
627
void fx_Math_sin(txMachine* the)
628
7.86k
{
629
7.86k
  mxNanResultIfNoArg;
630
6.75k
  fxToNumber(the, mxArgv(0));
631
6.75k
  mxResult->kind = XS_NUMBER_KIND;
632
6.75k
  mxResult->value.number = c_sin(mxArgv(0)->value.number);
633
6.75k
}
634
635
void fx_Math_sinh(txMachine* the)
636
76.0k
{
637
76.0k
  mxNanResultIfNoArg;
638
76.0k
  fxToNumber(the, mxArgv(0));
639
76.0k
  mxResult->kind = XS_NUMBER_KIND;
640
76.0k
  mxResult->value.number = c_sinh(mxArgv(0)->value.number);
641
76.0k
}
642
643
void fx_Math_tan(txMachine* the)
644
6.31k
{
645
6.31k
  mxNanResultIfNoArg;
646
6.24k
  fxToNumber(the, mxArgv(0));
647
6.24k
  mxResult->kind = XS_NUMBER_KIND;
648
6.24k
  mxResult->value.number = c_tan(mxArgv(0)->value.number);
649
6.24k
}
650
651
void fx_Math_tanh(txMachine* the)
652
3.33k
{
653
3.33k
  mxNanResultIfNoArg;
654
2.66k
  fxToNumber(the, mxArgv(0));
655
2.66k
  mxResult->kind = XS_NUMBER_KIND;
656
2.66k
  mxResult->value.number = c_tanh(mxArgv(0)->value.number);
657
2.66k
}
658
659
void fx_Math_trunc(txMachine* the)
660
98
{
661
98
  mxNanResultIfNoArg;
662
96
  fxToNumber(the, mxArgv(0));
663
96
  mxResult->kind = XS_NUMBER_KIND;
664
96
  mxResult->value.number = c_trunc(mxArgv(0)->value.number);
665
96
  fx_Math_toInteger(the);
666
96
}
667
668
void fx_Math_toInteger(txMachine* the)
669
593k
{
670
593k
  txNumber number = mxResult->value.number;
671
593k
  txInteger integer = (txInteger)number;
672
593k
  txNumber check = integer;
673
593k
  if ((number == check) && (number || !c_signbit(number))) {
674
445k
    mxResult->value.integer = integer;
675
445k
    mxResult->kind = XS_INTEGER_KIND;
676
445k
  }
677
593k
}