Coverage Report

Created: 2025-12-14 06:18

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.0k
{
42
24.0k
  txSlot* slot;
43
24.0k
  mxPush(mxObjectPrototype);
44
24.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
45
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_abs), 1, mxID(_abs), XS_DONT_ENUM_FLAG);
46
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acos), 1, mxID(_acos), XS_DONT_ENUM_FLAG);
47
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_acosh), 1, mxID(_acosh), XS_DONT_ENUM_FLAG);
48
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asin), 1, mxID(_asin), XS_DONT_ENUM_FLAG);
49
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_asinh), 1, mxID(_asinh), XS_DONT_ENUM_FLAG);
50
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan), 1, mxID(_atan), XS_DONT_ENUM_FLAG);
51
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atanh), 1, mxID(_atanh), XS_DONT_ENUM_FLAG);
52
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_atan2), 2, mxID(_atan2), XS_DONT_ENUM_FLAG);
53
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cbrt), 1, mxID(_cbrt), XS_DONT_ENUM_FLAG);
54
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_ceil), 1, mxID(_ceil), XS_DONT_ENUM_FLAG);
55
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_clz32), 1, mxID(_clz32), XS_DONT_ENUM_FLAG);
56
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cos), 1, mxID(_cos), XS_DONT_ENUM_FLAG);
57
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_cosh), 1, mxID(_cosh), XS_DONT_ENUM_FLAG);
58
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_exp), 1, mxID(_exp), XS_DONT_ENUM_FLAG);
59
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_expm1), 1, mxID(_expm1), XS_DONT_ENUM_FLAG);
60
24.0k
#if mxFloat16
61
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_f16round), 1, mxID(_f16round), XS_DONT_ENUM_FLAG);
62
24.0k
#endif
63
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_floor), 1, mxID(_floor), XS_DONT_ENUM_FLAG);
64
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_fround), 1, mxID(_fround), XS_DONT_ENUM_FLAG);
65
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_hypot), 2, mxID(_hypot_), XS_DONT_ENUM_FLAG);
66
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idiv), 2, mxID(_idiv), XS_DONT_ENUM_FLAG);
67
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_idivmod), 2, mxID(_idivmod), XS_DONT_ENUM_FLAG);
68
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imod), 2, mxID(_imod), XS_DONT_ENUM_FLAG);
69
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imul), 2, mxID(_imul), XS_DONT_ENUM_FLAG);
70
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_imuldiv), 2, mxID(_imuldiv), XS_DONT_ENUM_FLAG);
71
24.0k
#if mxECMAScript2023
72
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irandom), 0, mxID(_irandom), XS_DONT_ENUM_FLAG);
73
24.0k
#endif
74
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_irem), 2, mxID(_irem), XS_DONT_ENUM_FLAG);
75
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log), 1, mxID(_log), XS_DONT_ENUM_FLAG);
76
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log1p), 1, mxID(_log1p), XS_DONT_ENUM_FLAG);
77
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log10), 1, mxID(_log10), XS_DONT_ENUM_FLAG);
78
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_log2), 1, mxID(_log2), XS_DONT_ENUM_FLAG);
79
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_max), 2, mxID(_max), XS_DONT_ENUM_FLAG);
80
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_min), 2, mxID(_min), XS_DONT_ENUM_FLAG);
81
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_mod), 2, mxID(_mod), XS_DONT_ENUM_FLAG);
82
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_pow), 2, mxID(_pow), XS_DONT_ENUM_FLAG);
83
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_random), 0, mxID(_random), XS_DONT_ENUM_FLAG);
84
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_round), 1, mxID(_round), XS_DONT_ENUM_FLAG);
85
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sign), 1, mxID(_sign), XS_DONT_ENUM_FLAG);
86
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sin), 1, mxID(_sin), XS_DONT_ENUM_FLAG);
87
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sinh), 1, mxID(_sinh), XS_DONT_ENUM_FLAG);
88
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_sqrt), 1, mxID(_sqrt), XS_DONT_ENUM_FLAG);
89
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tan), 1, mxID(_tan), XS_DONT_ENUM_FLAG);
90
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_tanh), 1, mxID(_tanh), XS_DONT_ENUM_FLAG);
91
24.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Math_trunc), 1, mxID(_trunc), XS_DONT_ENUM_FLAG);
92
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_E, mxID(_E), XS_GET_ONLY);
93
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_LN10, mxID(_LN10), XS_GET_ONLY);
94
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_LN2, mxID(_LN2), XS_GET_ONLY);
95
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_LOG10E, mxID(_LOG10E), XS_GET_ONLY);
96
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_LOG2E, mxID(_LOG2E), XS_GET_ONLY);
97
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_PI, mxID(_PI), XS_GET_ONLY);
98
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT1_2, mxID(_SQRT1_2), XS_GET_ONLY);
99
24.0k
  slot = fxNextNumberProperty(the, slot, C_M_SQRT2, mxID(_SQRT2), XS_GET_ONLY);
100
24.0k
  slot = fxNextStringXProperty(the, slot, "Math", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
101
24.0k
  mxPull(mxMathObject);
102
//@@  c_srand((unsigned)c_time(0));
103
24.0k
}
104
105
#define mxNanResultIfNoArg \
106
898k
  if (mxArgc < 1) {  \
107
12.9k
    mxResult->kind = XS_NUMBER_KIND; \
108
12.9k
    mxResult->value.number = C_NAN; \
109
12.9k
    return; \
110
12.9k
  }
111
112
#define mxNanResultIfNoArg2 \
113
708k
  if (mxArgc < 2) {  \
114
58
    mxResult->kind = XS_NUMBER_KIND; \
115
58
    mxResult->value.number = C_NAN; \
116
58
    return; \
117
58
  }
118
119
void fx_Math_abs(txMachine* the)
120
19
{
121
19
  mxNanResultIfNoArg;
122
19
  fxToNumber(the, mxArgv(0));
123
19
  mxResult->kind = XS_NUMBER_KIND;
124
19
  mxResult->value.number = c_fabs(mxArgv(0)->value.number);
125
19
}
126
127
void fx_Math_acos(txMachine* the)
128
4.67k
{
129
4.67k
  mxNanResultIfNoArg;
130
4.40k
  fxToNumber(the, mxArgv(0));
131
4.40k
  mxResult->kind = XS_NUMBER_KIND;
132
4.40k
  mxResult->value.number = c_acos(mxArgv(0)->value.number);
133
4.40k
}
134
135
void fx_Math_acosh(txMachine* the)
136
3.75k
{
137
3.75k
  mxNanResultIfNoArg;
138
3.21k
  fxToNumber(the, mxArgv(0));
139
3.21k
  mxResult->kind = XS_NUMBER_KIND;
140
3.21k
  mxResult->value.number = c_acosh(mxArgv(0)->value.number);
141
3.21k
}
142
143
void fx_Math_asin(txMachine* the)
144
7.35k
{
145
7.35k
  mxNanResultIfNoArg;
146
6.47k
  fxToNumber(the, mxArgv(0));
147
6.47k
  mxResult->kind = XS_NUMBER_KIND;
148
6.47k
  mxResult->value.number = c_asin(mxArgv(0)->value.number);
149
6.47k
}
150
151
void fx_Math_asinh(txMachine* the)
152
55.8k
{
153
55.8k
  mxNanResultIfNoArg;
154
55.8k
  fxToNumber(the, mxArgv(0));
155
55.8k
  mxResult->kind = XS_NUMBER_KIND;
156
55.8k
  mxResult->value.number = c_asinh(mxArgv(0)->value.number);
157
55.8k
}
158
159
void fx_Math_atan(txMachine* the)
160
5.30k
{
161
5.30k
  mxNanResultIfNoArg;
162
2.50k
  fxToNumber(the, mxArgv(0));
163
2.50k
  mxResult->kind = XS_NUMBER_KIND;
164
2.50k
  mxResult->value.number = c_atan(mxArgv(0)->value.number);
165
2.50k
}
166
167
void fx_Math_atanh(txMachine* the)
168
108k
{
169
108k
  mxNanResultIfNoArg;
170
108k
  fxToNumber(the, mxArgv(0));
171
108k
  mxResult->kind = XS_NUMBER_KIND;
172
108k
  mxResult->value.number = c_atanh(mxArgv(0)->value.number);
173
108k
}
174
175
void fx_Math_atan2(txMachine* the)
176
539k
{
177
539k
  mxNanResultIfNoArg2;
178
539k
  fxToNumber(the, mxArgv(0));
179
539k
  fxToNumber(the, mxArgv(1));
180
539k
  mxResult->kind = XS_NUMBER_KIND;
181
539k
  mxResult->value.number = c_atan2(mxArgv(0)->value.number, mxArgv(1)->value.number);
182
539k
}
183
184
void fx_Math_cbrt(txMachine* the)
185
7
{
186
7
  mxNanResultIfNoArg;
187
7
  fxToNumber(the, mxArgv(0));
188
7
  mxResult->kind = XS_NUMBER_KIND;
189
7
  mxResult->value.number = c_cbrt(mxArgv(0)->value.number);
190
7
}
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
52
{
208
52
  txUnsigned x = (mxArgc > 0) ? fxToUnsigned(the, mxArgv(0)) : 0;
209
52
  txInteger r;
210
52
  if (x)
211
#if mxWindows
212
  {
213
    _BitScanReverse(&r, x);
214
    r = 31 - r;
215
  }
216
#else
217
41
    r = __builtin_clz(x);
218
11
#endif  
219
11
  else
220
11
    r = 32;
221
52
  mxResult->kind = XS_INTEGER_KIND;
222
52
  mxResult->value.integer = r;
223
52
}
224
225
void fx_Math_cos(txMachine* the)
226
166k
{
227
166k
  mxNanResultIfNoArg;
228
166k
  fxToNumber(the, mxArgv(0));
229
166k
  mxResult->kind = XS_NUMBER_KIND;
230
166k
  mxResult->value.number = c_cos(mxArgv(0)->value.number);
231
166k
}
232
233
void fx_Math_cosh(txMachine* the)
234
78.5k
{
235
78.5k
  mxNanResultIfNoArg;
236
77.8k
  fxToNumber(the, mxArgv(0));
237
77.8k
  mxResult->kind = XS_NUMBER_KIND;
238
77.8k
  mxResult->value.number = c_cosh(mxArgv(0)->value.number);
239
77.8k
}
240
241
void fx_Math_exp(txMachine* the)
242
117k
{
243
117k
  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
24
{
251
24
  mxNanResultIfNoArg;
252
24
  fxToNumber(the, mxArgv(0));
253
24
  mxResult->kind = XS_NUMBER_KIND;
254
24
  mxResult->value.number = c_expm1(mxArgv(0)->value.number);
255
24
}
256
257
void fx_Math_floor(txMachine* the)
258
148k
{
259
148k
  mxNanResultIfNoArg;
260
148k
  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
82.1k
  fxToNumber(the, mxArgv(0));
266
82.1k
  mxResult->kind = XS_NUMBER_KIND;
267
82.1k
  mxResult->value.number = c_floor(mxArgv(0)->value.number);
268
82.1k
  fx_Math_toInteger(the);
269
82.1k
}
270
271
void fx_Math_fround(txMachine* the)
272
55
{
273
55
  float arg;
274
55
  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
36
{
287
36
  if (mxArgc == 2) {
288
19
    fxToNumber(the, mxArgv(0));
289
19
    fxToNumber(the, mxArgv(1));
290
19
    mxResult->kind = XS_NUMBER_KIND;
291
19
    mxResult->value.number = c_hypot(mxArgv(0)->value.number, mxArgv(1)->value.number);
292
19
  }
293
17
  else {
294
17
    txInteger c = mxArgc, i;
295
17
    txNumber result = 0;
296
39
    for (i = 0; i < c; i++) {
297
22
      txNumber argument = fxToNumber(the, mxArgv(i));
298
22
      result += argument * argument;
299
22
    }
300
17
    mxResult->kind = XS_NUMBER_KIND;
301
17
    mxResult->value.number = c_sqrt(result);
302
17
  }
303
36
}
304
305
void fx_Math_idiv(txMachine* the)
306
52.0k
{
307
52.0k
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
308
52.0k
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
309
52.0k
  if (y == 0) {
310
34.4k
    mxResult->kind = XS_NUMBER_KIND;
311
34.4k
    mxResult->value.number = C_NAN;
312
34.4k
  }
313
17.5k
  else {
314
17.5k
    mxResult->kind = XS_INTEGER_KIND;
315
17.5k
#if mxIntegerDivideOverflowException
316
17.5k
    if ((x == (txInteger)0x80000000) && (y == -1))
317
2
      mxResult->value.integer = x;
318
17.5k
    else
319
17.5k
#endif
320
17.5k
      mxResult->value.integer = x / y;
321
17.5k
  }
322
52.0k
}
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
51.9k
{
350
51.9k
  txInteger x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
351
51.9k
  txInteger y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
352
51.9k
  mxResult->kind = XS_INTEGER_KIND;
353
51.9k
  mxResult->value.integer = x * y;
354
51.9k
}
355
356
void fx_Math_imuldiv(txMachine* the)
357
103k
{
358
103k
  txS8 x = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
359
103k
  txS8 y = (mxArgc > 1) ? fxToInteger(the, mxArgv(1)) : 0;
360
103k
  txS8 z = (mxArgc > 2) ? fxToInteger(the, mxArgv(2)) : 0;
361
103k
  if (z == 0) {
362
60.6k
    mxResult->kind = XS_NUMBER_KIND;
363
60.6k
    mxResult->value.number = C_NAN;
364
60.6k
  }
365
43.3k
  else {
366
43.3k
    txS8 r = (x * y) / z;
367
43.3k
    mxResult->kind = XS_INTEGER_KIND;
368
43.3k
    mxResult->value.integer = (txInteger)(r & 0x00000000FFFFFFFF);
369
43.3k
  }
370
103k
}
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
23
{
420
23
  mxNanResultIfNoArg;
421
22
  fxToNumber(the, mxArgv(0));
422
22
  mxResult->kind = XS_NUMBER_KIND;
423
22
  mxResult->value.number = c_log(mxArgv(0)->value.number);
424
22
}
425
426
void fx_Math_log1p(txMachine* the)
427
994
{
428
994
  mxNanResultIfNoArg;
429
993
  fxToNumber(the, mxArgv(0));
430
993
  mxResult->kind = XS_NUMBER_KIND;
431
993
  mxResult->value.number = c_log1p(mxArgv(0)->value.number);
432
993
}
433
434
void fx_Math_log10(txMachine* the)
435
52
{
436
52
  mxNanResultIfNoArg;
437
51
  fxToNumber(the, mxArgv(0));
438
51
  mxResult->kind = XS_NUMBER_KIND;
439
51
  mxResult->value.number = c_log10(mxArgv(0)->value.number);
440
51
}
441
442
void fx_Math_log2(txMachine* the)
443
22
{
444
22
  mxNanResultIfNoArg;
445
22
  fxToNumber(the, mxArgv(0));
446
22
  mxResult->kind = XS_NUMBER_KIND;
447
#if mxAndroid
448
  mxResult->value.number = c_log(mxArgv(0)->value.number) / c_log(2);
449
#else
450
22
  mxResult->value.number = c_log2(mxArgv(0)->value.number);
451
22
#endif
452
22
}
453
454
void fx_Math_max(txMachine* the)
455
2.01k
{
456
2.01k
  txInteger c = mxArgc, i = 0;
457
2.01k
  mxResult->kind = XS_NUMBER_KIND;
458
2.01k
  mxResult->value.number = -((txNumber)C_INFINITY);
459
2.01k
  if (0 == c)
460
46
    return;
461
462
1.96k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
463
1.27k
    mxResult->kind = XS_INTEGER_KIND;
464
1.27k
    mxResult->value.integer = mxArgv(0)->value.integer;
465
1.27k
    i = 1;
466
1.27k
  }
467
468
4.48k
  for (; i < c; i++) {
469
3.01k
    txSlot *slot = mxArgv(i);
470
3.01k
    if (XS_INTEGER_KIND == mxResult->kind) {
471
1.54k
      if (XS_INTEGER_KIND == slot->kind) {
472
1.27k
        if (mxResult->value.integer < slot->value.integer) {
473
811
          mxResult->value.integer = slot->value.integer;
474
811
        }
475
1.27k
        continue;
476
1.27k
      }
477
269
      mxResult->kind = XS_NUMBER_KIND;
478
269
      mxResult->value.number = mxResult->value.integer;
479
269
    }
480
481
1.73k
    txNumber n = fxToNumber(the, slot);
482
1.73k
    if (c_isnan(n)) {
483
1.63k
      for (; i < c; i++)
484
1.14k
        fxToNumber(the, mxArgv(i));
485
495
      mxResult->value.number = C_NAN;
486
495
      return;
487
495
    }
488
1.24k
    if (mxResult->value.number < n)
489
482
      mxResult->value.number = n;
490
760
    else if ((mxResult->value.number == 0) && (n == 0)) {
491
211
      if (c_signbit(mxResult->value.number) != c_signbit(n))
492
33
        mxResult->value.number = 0;
493
211
    }
494
1.24k
  }
495
1.96k
}
496
497
void fx_Math_min(txMachine* the)
498
1.70k
{
499
1.70k
  txInteger c = mxArgc, i = 0;
500
1.70k
  mxResult->kind = XS_NUMBER_KIND;
501
1.70k
  mxResult->value.number = (txNumber)C_INFINITY;
502
1.70k
  if (0 == c)
503
955
    return;
504
505
752
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
506
384
    mxResult->kind = XS_INTEGER_KIND;
507
384
    mxResult->value.integer = mxArgv(0)->value.integer;
508
384
    i = 1;
509
384
  }
510
511
2.00k
  for (; i < c; i++) {
512
1.84k
    txSlot *slot = mxArgv(i);
513
1.84k
    if (XS_INTEGER_KIND == mxResult->kind) {
514
684
      if (XS_INTEGER_KIND == slot->kind) {
515
334
        if (mxResult->value.integer > slot->value.integer)
516
56
          mxResult->value.integer = slot->value.integer;
517
334
        continue;
518
334
      }
519
350
      mxResult->kind = XS_NUMBER_KIND;
520
350
      mxResult->value.number = mxResult->value.integer;
521
350
    }
522
  
523
1.51k
    txNumber n = fxToNumber(the, slot);
524
1.51k
    if (c_isnan(n)) {
525
1.45k
      for (; i < c; i++)
526
864
        fxToNumber(the, mxArgv(i));
527
592
      mxResult->value.number = C_NAN;
528
592
      return;
529
592
    }
530
922
    if (mxResult->value.number > n)
531
537
      mxResult->value.number = n;
532
385
    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
922
  }
537
752
}
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.43M
{
549
1.43M
  if (!c_isfinite(y) && (c_fabs(x) == 1.0))
550
973
    return C_NAN;
551
1.43M
  return c_pow(x, y);
552
1.43M
}
553
554
void fx_Math_pow(txMachine* the)
555
168k
{
556
168k
  txNumber x, y;
557
168k
  mxNanResultIfNoArg2;
558
168k
  x = fxToNumber(the, mxArgv(0));
559
168k
  y = fxToNumber(the, mxArgv(1));
560
168k
  mxResult->kind = XS_NUMBER_KIND;
561
168k
  mxResult->value.number = fx_pow(x, y);
562
168k
}
563
564
void fx_Math_random(txMachine* the)
565
103
{
566
103
  uint32_t result;
567
103
  do {
568
103
    result = c_rand();
569
103
  } while (result == C_RAND_MAX);
570
103
  mxResult->kind = XS_NUMBER_KIND;
571
103
  mxResult->value.number = (double)result / (double)C_RAND_MAX;
572
103
}
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
15.3k
{
581
15.3k
    txNumber arg;
582
15.3k
  mxNanResultIfNoArg;
583
15.3k
  if (XS_INTEGER_KIND == mxArgv(0)->kind) {
584
1.02k
    mxResult->kind = XS_INTEGER_KIND;
585
1.02k
    mxResult->value.integer = mxArgv(0)->value.integer;
586
1.02k
    return;
587
1.02k
  }
588
14.3k
  arg = fxToNumber(the, mxArgv(0));
589
14.3k
  if (c_isnormal(arg) && (-4503599627370495 < arg) && (arg < 4503599627370495)) { // 2 ** 52 - 1
590
13.1k
    if ((arg < -0.5) || (0.5 <= arg))
591
10.9k
      arg = c_floor(arg + 0.5);
592
2.21k
    else if (arg < 0)
593
1.08k
      arg = -0.0;
594
1.12k
    else if (arg > 0)
595
1.12k
      arg = 0.0;
596
13.1k
    }
597
14.3k
  mxResult->kind = XS_NUMBER_KIND;
598
14.3k
  mxResult->value.number = arg;
599
14.3k
  fx_Math_toInteger(the);
600
14.3k
}
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
15.9k
{
612
15.9k
  txNumber arg;
613
15.9k
  mxNanResultIfNoArg;
614
15.5k
  arg = fxToNumber(the, mxArgv(0));
615
15.5k
  mxResult->kind = XS_NUMBER_KIND;
616
15.5k
  if (c_isnan(arg))
617
11.8k
    mxResult->value.number = C_NAN;
618
3.71k
  else if (arg < 0)
619
316
    mxResult->value.number = -1;
620
3.39k
  else if (arg > 0)
621
1.50k
    mxResult->value.number = 1;
622
1.89k
  else
623
1.89k
    mxResult->value.number = arg;
624
15.5k
  fx_Math_toInteger(the);
625
15.5k
}
626
627
void fx_Math_sin(txMachine* the)
628
7.95k
{
629
7.95k
  mxNanResultIfNoArg;
630
6.85k
  fxToNumber(the, mxArgv(0));
631
6.85k
  mxResult->kind = XS_NUMBER_KIND;
632
6.85k
  mxResult->value.number = c_sin(mxArgv(0)->value.number);
633
6.85k
}
634
635
void fx_Math_sinh(txMachine* the)
636
49.2k
{
637
49.2k
  mxNanResultIfNoArg;
638
44.6k
  fxToNumber(the, mxArgv(0));
639
44.6k
  mxResult->kind = XS_NUMBER_KIND;
640
44.6k
  mxResult->value.number = c_sinh(mxArgv(0)->value.number);
641
44.6k
}
642
643
void fx_Math_tan(txMachine* the)
644
39.5k
{
645
39.5k
  mxNanResultIfNoArg;
646
39.4k
  fxToNumber(the, mxArgv(0));
647
39.4k
  mxResult->kind = XS_NUMBER_KIND;
648
39.4k
  mxResult->value.number = c_tan(mxArgv(0)->value.number);
649
39.4k
}
650
651
void fx_Math_tanh(txMachine* the)
652
4.14k
{
653
4.14k
  mxNanResultIfNoArg;
654
3.34k
  fxToNumber(the, mxArgv(0));
655
3.34k
  mxResult->kind = XS_NUMBER_KIND;
656
3.34k
  mxResult->value.number = c_tanh(mxArgv(0)->value.number);
657
3.34k
}
658
659
void fx_Math_trunc(txMachine* the)
660
91
{
661
91
  mxNanResultIfNoArg;
662
88
  fxToNumber(the, mxArgv(0));
663
88
  mxResult->kind = XS_NUMBER_KIND;
664
88
  mxResult->value.number = c_trunc(mxArgv(0)->value.number);
665
88
  fx_Math_toInteger(the);
666
88
}
667
668
void fx_Math_toInteger(txMachine* the)
669
421k
{
670
421k
  txNumber number = mxResult->value.number;
671
421k
  txInteger integer = (txInteger)number;
672
421k
  txNumber check = integer;
673
421k
  if ((number == check) && (number || !c_signbit(number))) {
674
266k
    mxResult->value.integer = integer;
675
266k
    mxResult->kind = XS_INTEGER_KIND;
676
266k
  }
677
421k
}