Coverage Report

Created: 2026-12-01 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsBigInt.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  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
#ifndef mxCompile
41
static txSlot* fxBigIntCheck(txMachine* the, txSlot* it);
42
static txBigInt* fxIntegerToBigInt(txMachine* the, txSlot* slot);
43
static txBigInt* fxNumberToBigInt(txMachine* the, txSlot* slot);
44
static txBigInt* fxStringToBigInt(txMachine* the, txSlot* slot, txFlag whole);
45
#endif
46
47
#ifndef howmany
48
397k
#define howmany(x, y) (((x) + (y) - 1) / (y))
49
#endif
50
#ifndef MAX
51
2.69M
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
52
#endif
53
#ifndef MIN
54
5.82M
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
55
#endif
56
57
const txU4 gxDataOne[1] = { 1 };
58
const txU4 gxDataZero[1] = { 0 };
59
const txBigInt gxBigIntNaN = { .sign=0, .size=0, .data=(txU4*)gxDataZero };
60
const txBigInt gxBigIntOne = { .sign=0, .size=1, .data=(txU4*)gxDataOne };
61
const txBigInt gxBigIntZero = { .sign=0, .size=1, .data=(txU4*)gxDataZero };
62
63
static txBigInt *fxBigInt_fit(txMachine* the, txBigInt *r);
64
65
#ifdef mxMetering
66
static void fxBigInt_meter(txMachine* the, int n);
67
10.5M
#define mxBigInt_meter(N) if (the) fxBigInt_meter(the, N)
68
#else
69
#define mxBigInt_meter(N)
70
#endif
71
72
// BYTE CODE
73
74
#ifndef mxCompile
75
76
void fxBuildBigInt(txMachine* the)
77
33.9k
{
78
33.9k
  txSlot* slot;
79
33.9k
  mxPush(mxObjectPrototype);
80
33.9k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
81
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_toString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG);
82
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
83
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG);
84
33.9k
  slot = fxNextStringXProperty(the, slot, "BigInt", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
85
33.9k
  mxBigIntPrototype = *the->stack;
86
33.9k
  slot = fxBuildHostConstructor(the, mxCallback(fx_BigInt), 1, mxID(_BigInt));
87
33.9k
  mxBigIntConstructor = *the->stack;
88
33.9k
  slot = fxLastProperty(the, slot);
89
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_asIntN), 2, mxID(_asIntN), XS_DONT_ENUM_FLAG);
90
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_asUintN), 2, mxID(_asUintN), XS_DONT_ENUM_FLAG);
91
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_bitLength), 1, mxID(_bitLength), XS_DONT_ENUM_FLAG);
92
33.9k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_fromArrayBuffer), 1, mxID(_fromArrayBuffer), XS_DONT_ENUM_FLAG);
93
33.9k
  mxPop();
94
33.9k
}
95
96
void fx_BigInt(txMachine* the)
97
4.93k
{
98
4.93k
  if (mxHasTarget)
99
1
    mxTypeError("new: BigInt");
100
4.93k
  if (mxArgc > 0)
101
4.93k
    *mxResult = *mxArgv(0);
102
4.93k
  fxToPrimitive(the, mxResult, XS_NUMBER_HINT);
103
4.93k
  if (mxResult->kind == XS_NUMBER_KIND) {
104
1.81k
    int fpclass = c_fpclassify(mxResult->value.number);
105
1.81k
    txNumber check = c_trunc(mxResult->value.number);
106
1.81k
    if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE) && (mxResult->value.number == check))
107
1.79k
      fxNumberToBigInt(the, mxResult);
108
25
    else
109
25
      mxRangeError("cannot coerce number to bigint");
110
1.81k
  }
111
3.11k
  else if (mxResult->kind == XS_INTEGER_KIND) {
112
1.19k
    fxIntegerToBigInt(the, mxResult);
113
1.19k
  }
114
1.91k
  else
115
1.91k
    fxToBigInt(the, mxResult, 1);
116
4.93k
}
117
118
txNumber fx_BigInt_asAux(txMachine* the)
119
15.6k
{
120
15.6k
  txNumber value, index;
121
15.6k
  if (mxArgc < 1)
122
7
    index = 0;
123
15.6k
  else {
124
15.6k
    if (mxArgv(0)->kind == XS_UNDEFINED_KIND)
125
1.04k
      index = 0;
126
14.5k
    else {
127
14.5k
      value = fxToNumber(the, mxArgv(0));
128
14.5k
      if (c_isnan(value))
129
586
        index = 0;
130
13.9k
      else {
131
13.9k
        value = c_trunc(value);
132
13.9k
        if (value < 0)
133
17
          mxRangeError("index < 0");
134
13.9k
        index = value;
135
13.9k
        if (index <= 0)
136
2.41k
          index = 0;
137
11.5k
        else if (index > C_MAX_SAFE_INTEGER)
138
8
          index = C_MAX_SAFE_INTEGER;
139
13.9k
        if (value != index)
140
8
          mxRangeError("invalid index");
141
13.9k
      }
142
14.5k
    }
143
15.6k
  }
144
15.6k
  if (mxArgc < 2)
145
14
    mxTypeError("no bigint");
146
15.5k
  return index;
147
15.6k
}
148
149
void fx_BigInt_asIntN(txMachine* the)
150
10.5k
{
151
10.5k
  txInteger index = (txInteger)fx_BigInt_asAux(the);
152
10.5k
  txBigInt* arg = fxToBigInt(the, mxArgv(1), 1);
153
10.5k
  txBigInt* result;
154
10.5k
  if (fxBigInt_iszero(arg)) {
155
1.08k
    result = fxBigInt_dup(the, arg);
156
1.08k
  }
157
9.43k
  else {
158
9.43k
    txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
159
9.43k
    txBigInt* mask = fxBigInt_usub(the, C_NULL, bits, (txBigInt *)&gxBigIntOne);
160
9.43k
    result = fxBigInt_uand(the, C_NULL, arg, mask);
161
9.43k
    if ((arg->sign) && !fxBigInt_iszero(result))
162
2.15k
      result = fxBigInt_usub(the, C_NULL, bits, result);
163
9.43k
    if (index && fxBigInt_comp(result, fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index - 1)) >= 0)
164
2.11k
      result = fxBigInt_sub(the, C_NULL, result, bits);
165
9.43k
    result = fxBigInt_fit(the, result);
166
9.43k
  }
167
10.5k
  mxResult->value.bigint = *result;
168
10.5k
  mxResult->kind = XS_BIGINT_KIND;
169
//  txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
170
//  txBigInt* result = fxBigInt_mod(the, C_NULL, fxToBigInt(the, mxArgv(1), 1), bits);
171
//  if (index && fxBigInt_comp(result, fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index - 1)) >= 0)
172
//    result = fxBigInt_sub(the, C_NULL, result, bits);
173
//  mxResult->value.bigint = *result;
174
//  mxResult->kind = XS_BIGINT_KIND;
175
10.5k
}
176
177
void fx_BigInt_asUintN(txMachine* the)
178
5.12k
{
179
5.12k
  txInteger index = (txInteger)fx_BigInt_asAux(the);
180
5.12k
  txBigInt* arg = fxToBigInt(the, mxArgv(1), 1);
181
5.12k
  txBigInt* result;
182
5.12k
  if (fxBigInt_iszero(arg)) {
183
2.05k
    result = fxBigInt_dup(the, arg);
184
2.05k
  }
185
3.06k
  else {
186
3.06k
    txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
187
3.06k
    txBigInt* mask = fxBigInt_sub(the, C_NULL, bits, (txBigInt *)&gxBigIntOne);
188
3.06k
    result = fxBigInt_uand(the, C_NULL, arg, mask);
189
3.06k
    if ((arg->sign) && !fxBigInt_iszero(result))
190
564
      result = fxBigInt_usub(the, C_NULL, bits, result);
191
3.06k
    result = fxBigInt_fit(the, result);
192
3.06k
  }
193
5.12k
  mxResult->value.bigint = *result;
194
5.12k
  mxResult->kind = XS_BIGINT_KIND;
195
//  fxToBigInt(the, mxArgv(1), 1);
196
//  mxResult->value.bigint = *fxBigInt_mod(the, C_NULL, &mxArgv(1)->value.bigint, fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index));
197
//  mxResult->kind = XS_BIGINT_KIND;
198
5.12k
}
199
200
void fx_BigInt_bitLength(txMachine *the)
201
0
{
202
0
  txBigInt* arg = fxToBigInt(the, mxArgv(0), 1);
203
0
  mxResult->value.integer = fxBigInt_bitsize(arg);;
204
0
  mxResult->kind = XS_INTEGER_KIND;
205
0
}
206
207
void fx_BigInt_fromArrayBuffer(txMachine* the)
208
4
{
209
4
  txSlot* slot;
210
4
  txSlot* arrayBuffer = C_NULL;
211
4
  txSlot* bufferInfo;
212
4
  txBoolean sign = 0;
213
4
  int endian = EndianBig;
214
4
  txInteger length;
215
4
  txBigInt* bigint;
216
4
  txU1 *src, *dst;
217
4
  if (mxArgc < 1)
218
0
    mxTypeError("no argument");
219
4
  slot = mxArgv(0);
220
4
  if (slot->kind == XS_REFERENCE_KIND) {
221
3
    slot = slot->value.reference->next;
222
3
    if (slot && (slot->kind == XS_ARRAY_BUFFER_KIND))
223
2
      arrayBuffer = slot;
224
3
  }
225
4
  if (!arrayBuffer)
226
2
    mxTypeError("argument: not an ArrayBuffer instance");
227
2
  bufferInfo = arrayBuffer->next;
228
2
  length = bufferInfo->value.bufferInfo.length;
229
2
  if ((mxArgc > 1) && fxToBoolean(the, mxArgv(1)))
230
1
    sign = 1;
231
2
  if ((mxArgc > 2) && fxToBoolean(the, mxArgv(2)))
232
0
    endian = EndianLittle;
233
2
  if (sign)
234
1
    length--;
235
2
  if (length <= 0) {
236
2
    mxSyntaxError("argument: invalid ArrayBuffer instance");
237
//    mxResult->value.bigint = gxBigIntNaN;
238
//    mxResult->kind = XS_BIGINT_X_KIND;
239
0
    return;
240
2
  }
241
0
  bigint = fxBigInt_alloc(the, howmany(length, sizeof(txU4)));
242
0
  bigint->data[bigint->size - 1] = 0;
243
0
  src = (txU1*)(arrayBuffer->value.arrayBuffer.address);
244
0
  dst = (txU1*)(bigint->data);
245
0
  if (sign)
246
0
    bigint->sign = *src++;
247
#if mxBigEndian
248
  if (endian != EndianLittle) {
249
#else
250
0
  if (endian != EndianBig) {
251
0
#endif  
252
0
    c_memcpy(dst, src, length);
253
0
  }
254
0
  else {
255
0
    dst += length;
256
0
    while (length > 0) {
257
0
      dst--;
258
0
      *dst = *src;
259
0
      src++;
260
0
      length--;
261
0
    }
262
0
  }
263
0
  length = bigint->size - 1;
264
0
  while (length && (bigint->data[length] == 0))
265
0
    length--;
266
0
  bigint->size = length + 1;
267
0
  mxPullSlot(mxResult);
268
0
}
269
270
void fx_BigInt_prototype_toString(txMachine* the)
271
251k
{
272
251k
  txSlot* slot;
273
251k
  txU4 radix;
274
251k
  slot = fxBigIntCheck(the, mxThis);
275
251k
  if (!slot) mxTypeError("this: not a bigint");
276
251k
  if (mxArgc == 0)
277
84.5k
    radix = 10;
278
166k
  else if (mxIsUndefined(mxArgv(0)))
279
4
    radix = 10;
280
166k
  else {
281
166k
    radix = fxToInteger(the, mxArgv(0));
282
166k
    if ((radix < 2) || (36 < radix))
283
80
      mxRangeError("invalid radix");
284
166k
  }
285
251k
  mxPushSlot(slot);
286
251k
  fxBigintToString(the, the->stack, radix);
287
251k
  mxPullSlot(mxResult);
288
251k
}
289
290
void fx_BigInt_prototype_valueOf(txMachine* the)
291
598
{
292
598
  txSlot* slot = fxBigIntCheck(the, mxThis);
293
598
  if (!slot) mxTypeError("this: not a bigint");
294
20
  mxResult->kind = slot->kind;
295
20
  mxResult->value = slot->value;
296
20
}
297
298
void fxBigInt(txMachine* the, txSlot* slot, txU1 sign, txU2 size, txU4* data)
299
0
{
300
0
  txBigInt* bigint = fxBigInt_alloc(the, size);
301
0
  c_memcpy(bigint->data, data, size * sizeof(txU4));
302
0
  bigint->sign = sign;
303
0
  mxPullSlot(slot);
304
0
}
305
306
void fxBigIntX(txMachine* the, txSlot* slot, txU1 sign, txU2 size, txU4* data)
307
0
{
308
0
  slot->value.bigint.data = data;
309
0
  slot->value.bigint.size = size;
310
0
  slot->value.bigint.sign = sign;
311
0
  slot->kind = XS_BIGINT_X_KIND;
312
0
}
313
314
txSlot* fxBigIntCheck(txMachine* the, txSlot* it)
315
251k
{
316
251k
  txSlot* result = C_NULL;
317
251k
  if ((it->kind == XS_BIGINT_KIND) || (it->kind == XS_BIGINT_X_KIND))
318
251k
    result = it;
319
619
  else if (it->kind == XS_REFERENCE_KIND) {
320
595
    txSlot* instance = it->value.reference;
321
595
    it = instance->next;
322
595
    if ((it) && (it->flag & XS_INTERNAL_FLAG) && ((it->kind == XS_BIGINT_KIND) || (it->kind == XS_BIGINT_X_KIND)))
323
19
      result = it;
324
595
  }
325
251k
  return result;
326
251k
}
327
328
void fxBigIntCoerce(txMachine* the, txSlot* slot)
329
215
{
330
215
  fxToBigInt(the, slot, 1);
331
215
}
332
333
txBoolean fxBigIntCompare(txMachine* the, txBoolean less, txBoolean equal, txBoolean more, txSlot* left, txSlot* right)
334
7.74M
{
335
7.74M
  int result;
336
7.74M
  txNumber delta = 0;
337
7.74M
  if (mxBigIntIsNaN(&left->value.bigint))
338
0
    return less & more & !equal;
339
7.74M
  if (right->kind == XS_STRING_KIND)
340
1.09M
    fxStringToBigInt(the, right, 1);
341
7.74M
  if ((right->kind != XS_BIGINT_KIND) && (right->kind != XS_BIGINT_X_KIND)) {
342
6.24M
    fxToNumber(the, right);
343
6.24M
    result = c_fpclassify(right->value.number);
344
6.24M
    if (result == C_FP_NAN)
345
16.0k
      return less & more & !equal;
346
6.23M
    if (result == C_FP_INFINITE)
347
200k
      return (right->value.number > 0) ? less : more;
348
6.03M
    delta = right->value.number - c_trunc(right->value.number);
349
6.03M
    fxNumberToBigInt(the, right);
350
6.03M
  }
351
7.53M
  if (mxBigIntIsNaN(&right->value.bigint))
352
763k
    return less & more & !equal;
353
6.76M
  result = fxBigInt_comp(&left->value.bigint, &right->value.bigint);
354
6.76M
  if (result < 0)
355
3.01M
    return less;
356
3.75M
  if (result > 0)
357
3.74M
    return more;
358
4.05k
  if (delta > 0)
359
1.68k
    return less;
360
2.36k
  if (delta < 0)
361
1.73k
    return more;
362
629
  return equal;
363
2.36k
}
364
365
void fxBigIntDecode(txMachine* the, txSize size)
366
16.1M
{
367
16.1M
  txBigInt* bigint;
368
16.1M
  mxPushUndefined();
369
16.1M
  bigint = &the->stack->value.bigint;
370
16.1M
  bigint->data = fxNewChunk(the, size);
371
16.1M
  bigint->size = size >> 2;
372
16.1M
  bigint->sign = 0;
373
#if mxBigEndian
374
  {
375
    txS1* src = (txS1*)the->code;
376
    txS1* dst = (txS1*)bigint->data;
377
    while (size) {
378
      dst[3] = *src++;
379
      dst[2] = *src++;
380
      dst[1] = *src++;
381
      dst[0] = *src++;
382
      dst += 4;
383
      size--;
384
    }
385
  }
386
#else
387
16.1M
  c_memcpy(bigint->data, the->code, size);
388
16.1M
#endif  
389
16.1M
  the->stack->kind = XS_BIGINT_KIND;
390
16.1M
}
391
392
#endif  
393
394
void fxBigIntEncode(txByte* code, txBigInt* bigint, txSize size)
395
34.0k
{
396
#if mxBigEndian
397
  {
398
    txS1* src = (txS1*)bigint->data;
399
    txS1* dst = (txS1*)code;
400
    while (size) {
401
      dst[3] = *src++;
402
      dst[2] = *src++;
403
      dst[1] = *src++;
404
      dst[0] = *src++;
405
      dst += 4;
406
      size--;
407
    }
408
  }
409
#else
410
34.0k
  c_memcpy(code, bigint->data, size);
411
34.0k
#endif
412
34.0k
}
413
414
#ifndef mxCompile
415
txSlot* fxBigIntToInstance(txMachine* the, txSlot* slot)
416
1.20M
{
417
1.20M
  txSlot* instance;
418
1.20M
  txSlot* internal;
419
1.20M
  mxPush(mxBigIntPrototype);
420
1.20M
  instance = fxNewObjectInstance(the);
421
1.20M
  internal = instance->next = fxNewSlot(the);
422
1.20M
  internal->flag = XS_INTERNAL_FLAG;
423
1.20M
  internal->kind = slot->kind;
424
1.20M
  internal->value = slot->value;
425
1.20M
  if (the->frame->flag & XS_STRICT_FLAG)
426
1.41k
    instance->flag |= XS_DONT_PATCH_FLAG;
427
1.20M
  mxPullSlot(slot);
428
1.20M
  return instance;
429
1.20M
}
430
#endif
431
432
txSize fxBigIntMeasure(txBigInt* bigint)
433
102k
{
434
102k
  return bigint->size * sizeof(txU4);
435
102k
}
436
437
txSize fxBigIntMaximum(txSize length)
438
150k
{
439
150k
  return sizeof(txU4) * (1 + (((txSize)c_ceil((txNumber)length * c_log(10) / c_log(2))) / 32));
440
150k
}
441
442
txSize fxBigIntMaximumB(txSize length)
443
1.45k
{
444
1.45k
  return sizeof(txU4) * (1 + howmany(1, mxBigIntWordSize) + (length / 32));
445
1.45k
}
446
447
txSize fxBigIntMaximumO(txSize length)
448
103
{
449
103
  return sizeof(txU4) * (1 + howmany(3, mxBigIntWordSize) + ((length * 3) / 32));
450
103
}
451
452
txSize fxBigIntMaximumX(txSize length)
453
333k
{
454
333k
  return sizeof(txU4) * (1 + howmany(4, mxBigIntWordSize) + ((length * 4) / 32));
455
333k
}
456
457
void fxBigIntParse(txBigInt* bigint, txString p, txSize length, txInteger sign)
458
150k
{
459
150k
  txU4 data[1] = { 0 };
460
150k
  txBigInt digit = { .sign=0, .size=1, .data=data };
461
150k
  bigint->sign = 0;
462
150k
  bigint->size = 1;
463
150k
  bigint->data[0] = 0;
464
286k
  while (length) {
465
135k
    char c = *p++;
466
135k
    data[0] = c - '0';
467
135k
    fxBigInt_uadd(NULL, bigint, fxBigInt_umul1(NULL, bigint, bigint, 10), &digit);
468
135k
    length--;
469
135k
  }
470
150k
  if ((bigint->size > 1) || (bigint->data[0] != 0))
471
76.0k
    bigint->sign = sign;
472
150k
}
473
474
void fxBigIntParseB(txBigInt* bigint, txString p, txSize length)
475
1.45k
{
476
1.45k
  txU4 data[1] = { 0 };
477
1.45k
  txBigInt digit = { .sign=0, .size=1, .data=data };
478
1.45k
  bigint->sign = 0;
479
1.45k
  bigint->size = 1;
480
1.45k
  bigint->data[0] = 0;
481
4.06k
  while (length) {
482
2.61k
    char c = *p++;
483
2.61k
    data[0] = c - '0';
484
2.61k
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 1), &digit);
485
2.61k
    length--;
486
2.61k
  }
487
1.45k
}
488
489
void fxBigIntParseO(txBigInt* bigint, txString p, txSize length)
490
103
{
491
103
  txU4 data[1] = { 0 };
492
103
  txBigInt digit = { .sign=0, .size=1, .data=data };
493
103
  bigint->sign = 0;
494
103
  bigint->size = 1;
495
103
  bigint->data[0] = 0;
496
879
  while (length) {
497
776
    char c = *p++;
498
776
    data[0] = c - '0';
499
776
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 3), &digit);
500
776
    length--;
501
776
  }
502
103
}
503
504
void fxBigIntParseX(txBigInt* bigint, txString p, txSize length)
505
333k
{
506
333k
  txU4 data[1] = { 0 };
507
333k
  txBigInt digit = { .sign=0, .size=1, .data=data };
508
333k
  bigint->sign = 0;
509
333k
  bigint->size = 1;
510
333k
  bigint->data[0] = 0;
511
3.06M
  while (length) {
512
2.73M
    char c = *p++;
513
2.73M
    if (('0' <= c) && (c <= '9'))
514
337k
      data[0] = c - '0';
515
2.39M
    else if (('a' <= c) && (c <= 'f'))
516
731k
      data[0] = 10 + c - 'a';
517
1.66M
    else
518
1.66M
      data[0] = 10 + c - 'A';
519
2.73M
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 4), &digit);
520
2.73M
    length--;
521
2.73M
  }
522
333k
}
523
524
#ifndef mxCompile
525
526
void fxBigintToArrayBuffer(txMachine* the, txSlot* slot, txU4 total, txBoolean sign, int endian)
527
61.8k
{
528
61.8k
  txBigInt* bigint = fxToBigInt(the, slot, 1);
529
61.8k
  txU4 length = howmany(fxBigInt_bitsize(bigint), 8);
530
61.8k
  txU4 offset = 0;
531
61.8k
  txU1 *src, *dst;
532
61.8k
  if (length == 0)
533
61.8k
    length = 1;
534
61.8k
  if (length < total) {
535
44.3k
    if (endian == EndianBig)
536
44.3k
      offset = total - length;
537
44.3k
  }
538
17.5k
  else {
539
17.5k
    total = length;
540
17.5k
  }
541
61.8k
  if (sign) {
542
9.33k
    if (total >= 0x7FFFFFFF)
543
0
      mxRangeError("byteLength too big");
544
9.33k
    offset++;
545
9.33k
    total++;
546
9.33k
  }
547
61.8k
  fxConstructArrayBufferResult(the, mxThis, total);
548
61.8k
  src = (txU1*)(bigint->data);
549
61.8k
  dst = (txU1*)(mxResult->value.reference->next->value.arrayBuffer.address);
550
61.8k
  if (sign)
551
9.33k
    *dst = bigint->sign;
552
61.8k
  dst += offset;
553
#if mxBigEndian
554
  if (endian != EndianLittle) {
555
#else
556
61.8k
  if (endian != EndianBig) {
557
1
#endif  
558
1
    c_memcpy(dst, src, length);
559
1
  }
560
61.8k
  else {
561
61.8k
    src += length;
562
123k
    while (length > 0) {
563
61.8k
      src--;
564
61.8k
      *dst = *src;
565
61.8k
      dst++;
566
61.8k
      length--;
567
61.8k
    }
568
61.8k
  }
569
61.8k
}
570
571
txNumber fxBigIntToNumber(txMachine* the, txSlot* slot)
572
527
{
573
527
  txBigInt* bigint = &slot->value.bigint;
574
527
  txNumber base = 4294967296;
575
527
  txNumber number = 0;
576
527
  int i;
577
1.18k
  for (i = bigint->size; --i >= 0;)
578
658
    number = (number * base) + bigint->data[i];
579
527
  if (bigint->sign)
580
8
    number = -number;
581
527
  slot->value.number = number;
582
527
  slot->kind = XS_NUMBER_KIND;
583
527
  return number;
584
527
}
585
586
typedef struct {
587
  uint32_t k;
588
  uint32_t base_to_k;
589
} BaseChunk32;
590
591
static const BaseChunk32 base_chunks32[] ICACHE_FLASH_ATTR = {
592
  {31, 0x80000000},
593
  {19, 1162261467},
594
  {15, 1073741824},
595
  {13, 1220703125},
596
  {12, 2176782336},
597
  {11, 1977326743},
598
  {10, 1073741824},
599
  {10, 3486784401},
600
  { 9, 1000000000},   // base 10
601
  { 8, 214358881},
602
  { 8, 429981696},
603
  { 7, 62748517},
604
  { 7, 105413504},
605
  { 7, 170859375},
606
  { 7, 268435456},
607
  { 6, 24137569},
608
  { 6, 34012224},
609
  { 6, 47045881},
610
  { 6, 64000000},
611
  { 6, 85766121},
612
  { 6, 113379904},
613
  { 6, 148035889},
614
  { 6, 191102976},
615
  { 5, 9765625},
616
  { 5, 11881376},
617
  { 5, 14348907},
618
  { 5, 17210368},
619
  { 5, 20511149},
620
  { 5, 24300000},
621
  { 5, 28629151},
622
  { 5, 33554432},
623
  { 5, 39135393},
624
  { 5, 45435424},
625
  { 5, 52521875},
626
  { 5, 60466176}
627
};
628
629
void fxBigintToString(txMachine* the, txSlot* slot, txU4 radix)
630
265k
{
631
265k
  static const char gxDigits[] ICACHE_FLASH_ATTR = "0123456789abcdefghijklmnopqrstuvwxyz";
632
265k
  txSize length, offset;
633
265k
  txSlot* result;
634
265k
  txSlot* stack;
635
265k
  if (0 == radix) radix = 10;
636
265k
  const BaseChunk32 *bc = base_chunks32 + (radix - 2);
637
638
265k
  if (mxBigIntIsNaN(&slot->value.bigint)) {
639
0
    fxStringX(the, slot, "NaN");
640
0
    return;
641
0
  }
642
265k
  mxBigInt_meter(slot->value.bigint.size);
643
644
265k
  mxPushUndefined();
645
265k
  result = the->stack;
646
  
647
265k
  mxPushSlot(slot);
648
265k
  fxBigInt_dup(the, &the->stack->value.bigint);
649
265k
  stack = the->stack;
650
651
265k
  length = 1 + bc->k + (txSize)c_ceil((txNumber)(stack->value.bigint.size + 1) * 32 * c_log(2) / c_log(radix));
652
265k
  if (stack->value.bigint.sign)
653
86.3k
    length++;
654
265k
  offset = length;
655
265k
  result->value.string = fxNewChunk(the, length);
656
265k
  result->kind = XS_STRING_KIND;
657
658
265k
  result->value.string[--offset] = 0;
659
265k
  int32_t nonZeroWords = stack->value.bigint.size;
660
727k
  do {
661
727k
    uint64_t carry = 0;
662
133M
    for (uint32_t i = nonZeroWords - 1, count = nonZeroWords, base_to_k = bc->base_to_k; count > 0; --count) {
663
132M
      carry = (carry << 32) | stack->value.bigint.data[i];
664
132M
      stack->value.bigint.data[i--] = (uint32_t)(carry / base_to_k);
665
132M
      carry %= base_to_k;
666
132M
    }
667
727k
    uint32_t remainder = (uint32_t)carry, k = bc->k;
668
10.1M
    do {
669
10.1M
      result->value.string[--offset] = c_read8(gxDigits + (remainder % radix));
670
10.1M
      remainder /= radix;
671
10.1M
    } while (--k);
672
673
1.42M
    while (nonZeroWords && (0 == stack->value.bigint.data[nonZeroWords - 1]))
674
696k
      nonZeroWords -= 1;
675
727k
  } while (nonZeroWords);
676
677
5.66M
  while (('0' == result->value.string[offset]) && result->value.string[offset + 1])
678
5.39M
    offset++;
679
680
265k
  if (stack->value.bigint.sign)
681
86.3k
    result->value.string[--offset] = '-';
682
265k
  c_memmove(result->value.string, result->value.string + offset, length - offset);
683
684
265k
  mxPop();
685
265k
  mxPop();
686
265k
  mxPullSlot(slot);
687
265k
}
688
689
txS8 fxToBigInt64(txMachine* the, txSlot* slot)
690
149
{
691
149
  return (txS8)fxToBigUint64(the, slot);
692
149
}
693
694
txU8 fxToBigUint64(txMachine* the, txSlot* slot)
695
458
{
696
458
  txBigInt* bigint = fxToBigInt(the, slot, 1);
697
458
  txU8 result = bigint->data[0];
698
458
  if (bigint->size > 1)
699
256
    result |= (txU8)(bigint->data[1]) << 32;
700
458
  if (bigint->sign && result) {
701
32
    result--;
702
32
    result = 0xFFFFFFFFFFFFFFFFll - result;
703
32
  }
704
458
  return result;
705
458
}
706
707
txBigInt* fxIntegerToBigInt(txMachine* the, txSlot* slot)
708
1.19k
{
709
1.19k
  txInteger integer = slot->value.integer;
710
1.19k
  txBigInt* bigint = &slot->value.bigint;
711
1.19k
  txU1 sign = 0;
712
1.19k
  txU4 uint;
713
1.19k
  if (integer < 0) {
714
28
    uint = (-2147483648 == integer) ? 2147483648 : -integer;
715
28
    sign = 1;
716
28
  }
717
1.17k
  else
718
1.17k
    uint = integer;
719
1.19k
  bigint->data = fxNewChunk(the, sizeof(txU4));
720
1.19k
  bigint->data[0] = uint;
721
1.19k
  bigint->sign = sign;
722
1.19k
  bigint->size = 1;
723
1.19k
  slot->kind = XS_BIGINT_KIND;
724
1.19k
  return bigint;
725
1.19k
}
726
727
txBigInt* fxNumberToBigInt(txMachine* the, txSlot* slot)
728
6.03M
{
729
6.03M
  txBigInt* bigint = &slot->value.bigint;
730
6.03M
  txNumber number = slot->value.number;
731
6.03M
  txNumber limit = 4294967296;
732
6.03M
  txU1 sign = 0;
733
6.03M
  txU2 size = 1;
734
6.03M
  if (number < 0) {
735
2.09M
    number = -number;
736
2.09M
    sign = 1;
737
2.09M
  }
738
9.49M
  while (number >= limit) {
739
3.46M
    size++;
740
3.46M
    number /= limit;
741
3.46M
  }
742
6.03M
  bigint->data = fxNewChunk(the, fxMultiplyChunkSizes(the, size, sizeof(txU4)));
743
6.03M
  bigint->size = size;
744
15.5M
  while (size > 0) {
745
9.49M
    txU4 part = (txU4)number;
746
9.49M
    number -= part;
747
9.49M
    size--;
748
9.49M
    bigint->data[size] = part;
749
9.49M
    number *= limit;
750
9.49M
  }
751
6.03M
  bigint->sign = sign;
752
6.03M
  slot->kind = XS_BIGINT_KIND;
753
6.03M
  return bigint;
754
6.03M
}
755
756
txBigInt* fxStringToBigInt(txMachine* the, txSlot* slot, txFlag whole)
757
1.16M
{
758
1.16M
  txString s = slot->value.string;
759
1.16M
  txString p = fxSkipSpaces(s);
760
1.16M
  txSize offset, length;
761
1.16M
  txInteger sign = 0;
762
1.16M
  txBigInt bigint = {.sign=0, .size=0, .data=C_NULL };
763
1.16M
  char c = *p;
764
1.16M
  if (c == '0') {
765
1.08M
    char d = *(p + 1);
766
1.08M
    if (whole && ((d == 'B') || (d == 'b') || (d == 'O') || (d == 'o') || (d == 'X') || (d == 'x'))) {
767
815k
      p += 2;
768
815k
      offset = mxPtrDiff(p - s);
769
815k
      if ((d == 'B') || (d == 'b')) {
770
175k
        while (((c = *p)) && ('0' <= c) && (c <= '1'))
771
1.73k
          p++;
772
173k
        length = mxPtrDiff(p - s - offset);
773
173k
        p = fxSkipSpaces(p);
774
173k
        if ((length > 0) && (*p == 0)) {
775
1.37k
          bigint.data = fxNewChunk(the, fxBigIntMaximumB(length));
776
1.37k
          fxBigIntParseB(&bigint, slot->value.string + offset, length);
777
1.37k
        }
778
173k
      }
779
641k
      else if ((d == 'O') || (d == 'o')) {
780
2.89k
        while (((c = *p)) && ('0' <= c) && (c <= '7'))
781
2.30k
          p++;
782
588
        length = mxPtrDiff(p - s - offset);
783
588
        p = fxSkipSpaces(p);
784
588
        if ((length > 0) && (*p == 0)) {
785
15
          bigint.data = fxNewChunk(the, fxBigIntMaximumO(length));
786
15
          fxBigIntParseO(&bigint, slot->value.string + offset, length);
787
15
        }
788
588
      }
789
641k
      else if ((d == 'X') || (d == 'x')) {
790
4.41M
        while (((c = *p)) && ((('0' <= c) && (c <= '9')) || (('a' <= c) && (c <= 'f')) || (('A' <= c) && (c <= 'F'))))
791
3.77M
          p++;
792
641k
        length = mxPtrDiff(p - s - offset);
793
641k
        p = fxSkipSpaces(p);
794
641k
        if ((length > 0) && (*p == 0)) {
795
333k
          bigint.data = fxNewChunk(the, fxBigIntMaximumX(length));
796
333k
          fxBigIntParseX(&bigint, slot->value.string + offset, length);
797
333k
        }
798
641k
      }
799
815k
      goto bail;
800
815k
    }
801
1.08M
  }
802
345k
  if (c == '-') {
803
230
    sign = 1;
804
230
    p++;
805
230
  }
806
345k
  else if (c == '+') {
807
179
    p++;
808
179
  }
809
345k
  offset = mxPtrDiff(p - s);
810
640k
  while (((c = *p)) && ('0' <= c) && (c <= '9'))
811
295k
    p++;
812
345k
  length = mxPtrDiff(p - s - offset);
813
345k
  p = fxSkipSpaces(p);
814
345k
  if (*p == 0) {
815
62.0k
    bigint.data = fxNewChunk(the, fxBigIntMaximum(length));
816
62.0k
    fxBigIntParse(&bigint, slot->value.string + offset, length, sign);
817
62.0k
  }
818
1.16M
bail:
819
1.16M
  if (bigint.data) {
820
396k
    slot->value.bigint = bigint;
821
396k
    slot->kind = XS_BIGINT_KIND;
822
396k
  }
823
763k
  else {
824
763k
    slot->value.bigint = gxBigIntNaN;
825
763k
    slot->kind = XS_BIGINT_X_KIND;
826
763k
  }
827
1.16M
  return &slot->value.bigint;
828
345k
}
829
830
txBigInt* fxToBigInt(txMachine* the, txSlot* slot, txFlag strict)
831
80.0k
{
832
80.1k
again:
833
80.1k
  switch (slot->kind) {
834
3.99k
  case XS_BOOLEAN_KIND:
835
3.99k
    slot->value.bigint = *fxBigInt_dup(the, (txBigInt *)((slot->value.boolean) ? &gxBigIntOne : &gxBigIntZero));
836
3.99k
    slot->kind = XS_BIGINT_KIND;
837
3.99k
    break;
838
15
  case XS_INTEGER_KIND:
839
15
    if (strict)
840
15
      mxTypeError("cannot coerce number to bigint");
841
0
    fxIntegerToBigInt(the, slot); 
842
0
    break;
843
12
  case XS_NUMBER_KIND:
844
12
    if (strict)
845
12
      mxTypeError("cannot coerce number to bigint");
846
0
    fxNumberToBigInt(the, slot);  
847
0
    break;
848
63.8k
  case XS_STRING_KIND:
849
63.8k
  case XS_STRING_X_KIND:
850
63.8k
    fxStringToBigInt(the, slot, 1);
851
63.8k
    if (mxBigIntIsNaN(&slot->value.bigint))
852
487
      mxSyntaxError("cannot coerce string to bigint");
853
63.3k
    break;
854
63.3k
  case XS_BIGINT_KIND:
855
12.1k
  case XS_BIGINT_X_KIND:
856
12.1k
    break;
857
13
  case XS_SYMBOL_KIND:
858
13
    mxTypeError("cannot coerce symbol to bigint");
859
0
    break;
860
117
  case XS_REFERENCE_KIND:
861
117
    fxToPrimitive(the, slot, XS_NUMBER_HINT);
862
117
    goto again;
863
48
  default:
864
48
    mxTypeError("cannot coerce to bigint");
865
0
    break;
866
80.1k
  }
867
79.4k
  return &slot->value.bigint;
868
80.1k
}
869
870
void fxFromBigInt64(txMachine* the, txSlot* slot, txS8 it)
871
1.08k
{
872
1.08k
  txU1 sign = 0;
873
1.08k
  txU8 value = 0;
874
1.08k
  if (it < 0) {
875
882
    if (it & 0x7FFFFFFFFFFFFFFFll)
876
882
      value = -it;
877
0
    else
878
0
      value = (txU8)it;
879
882
    sign = 1;
880
882
  }
881
205
  else
882
205
    value = it;
883
1.08k
  if (value > 0x00000000FFFFFFFFll) {
884
970
    slot->value.bigint.data = fxNewChunk(the, 2 * sizeof(txU4));
885
970
    slot->value.bigint.data[0] = (txU4)value;
886
970
    slot->value.bigint.data[1] = (txU4)(value >> 32);
887
970
    slot->value.bigint.size = 2;
888
970
  }
889
117
  else {
890
117
    slot->value.bigint.data = fxNewChunk(the, sizeof(txU4));
891
117
    slot->value.bigint.data[0] = (txU4)value;
892
117
    slot->value.bigint.size = 1;
893
117
  }
894
1.08k
    slot->value.bigint.sign = sign;
895
1.08k
  slot->kind = XS_BIGINT_KIND;
896
1.08k
}
897
898
void fxFromBigUint64(txMachine* the, txSlot* slot, txU8 value)
899
113
{
900
113
  if (value > 0x00000000FFFFFFFFll) {
901
81
    slot->value.bigint.data = fxNewChunk(the, 2 * sizeof(txU4));
902
81
    slot->value.bigint.data[0] = (txU4)(value);
903
81
    slot->value.bigint.data[1] = (txU4)(value >> 32);
904
81
    slot->value.bigint.size = 2;
905
81
  }
906
32
  else {
907
32
    slot->value.bigint.data = fxNewChunk(the, sizeof(txU4));
908
32
    slot->value.bigint.data[0] = (txU4)value;
909
32
    slot->value.bigint.size = 1;
910
32
  }
911
113
    slot->value.bigint.sign = 0;
912
113
  slot->kind = XS_BIGINT_KIND;
913
113
}
914
915
#endif
916
917
txBigInt *fxBigInt_alloc(txMachine* the, txU4 size)
918
21.7M
{
919
21.7M
#ifndef mxCompile
920
21.7M
  txBigInt* bigint;
921
21.7M
  if (size > 0xFFFF) {
922
1
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
923
1
  }
924
21.7M
  mxPushUndefined();
925
21.7M
  bigint = &the->stack->value.bigint;
926
21.7M
  bigint->data = fxNewChunk(the, fxMultiplyChunkSizes(the, size, sizeof(txU4)));
927
21.7M
  bigint->size = size;
928
21.7M
  bigint->sign = 0;
929
21.7M
  the->stack->kind = XS_BIGINT_KIND;
930
21.7M
  return bigint;
931
#else
932
  return NULL;
933
#endif
934
21.7M
}
935
936
void fxBigInt_free(txMachine* the, txBigInt *bigint)
937
5.59M
{
938
5.59M
#ifndef mxCompile
939
5.59M
  if (bigint == &the->stack->value.bigint)
940
5.59M
    the->stack++;
941
//  else
942
//    fprintf(stderr, "oops\n");
943
5.59M
#endif
944
5.59M
}
945
946
int fxBigInt_comp(txBigInt *a, txBigInt *b)
947
6.77M
{
948
6.77M
  if (a->sign != b->sign)
949
2.38M
    return(a->sign ? -1: 1);
950
4.39M
  else if (a->sign)
951
1.07M
    return(fxBigInt_ucomp(b, a));
952
3.32M
  else
953
3.32M
    return(fxBigInt_ucomp(a, b));
954
6.77M
}
955
956
int fxBigInt_ucomp(txBigInt *a, txBigInt *b)
957
9.54M
{
958
9.54M
  int i;
959
960
9.54M
  if (a->size != b->size)
961
5.80M
    return(a->size > b->size ? 1: -1);
962
4.72M
  for (i = a->size; --i >= 0;) {
963
4.40M
    if (a->data[i] != b->data[i])
964
3.41M
      return(a->data[i] > b->data[i] ? 1: -1);
965
4.40M
  }
966
319k
  return(0);
967
3.73M
}
968
969
int fxBigInt_iszero(txBigInt *a)
970
13.9M
{
971
13.9M
  return(a->size == 1 && a->data[0] == 0);
972
13.9M
}
973
974
void
975
fxBigInt_fill0(txBigInt *r)
976
1.01M
{
977
1.01M
  c_memset(r->data, 0, r->size * sizeof(txU4));
978
1.01M
}
979
980
void fxBigInt_copy(txBigInt *a, txBigInt *b)
981
12.3M
{
982
12.3M
  c_memmove(a->data, b->data, b->size * sizeof(txU4));
983
12.3M
  a->size = b->size;
984
12.3M
  a->sign = b->sign;
985
12.3M
}
986
987
txBigInt *fxBigInt_dup(txMachine* the, txBigInt *a)
988
272k
{
989
272k
  txBigInt *r = fxBigInt_alloc(the, a->size);
990
272k
  fxBigInt_copy(r, a);
991
272k
  return(r);
992
272k
}
993
994
int
995
fxBigInt_ffs(txBigInt *a)
996
120k
{
997
120k
  int i;
998
120k
  txU4 w = a->data[a->size - 1];
999
1000
3.81M
  for (i = 0; i < (int)mxBigIntWordSize && !(w & ((txU4)1 << (mxBigIntWordSize - 1 - i))); i++)
1001
3.69M
    ;
1002
120k
  return(i);
1003
120k
}
1004
1005
txBigInt *fxBigInt_fit(txMachine* the, txBigInt *r)
1006
1.35M
{
1007
1.35M
  int i = r->size;
1008
1.42M
  while (i > 0) {
1009
1.41M
    i--;
1010
1.41M
    if (r->data[i])
1011
1.34M
      break;
1012
1.41M
  }
1013
1.35M
  r->size = (txU2)(i + 1);
1014
1.35M
  mxBigInt_meter(r->size);
1015
1.35M
  return r;
1016
1.35M
}
1017
1018
1019
#if mxWindows
1020
int
1021
fxBigInt_bitsize(txBigInt *e)
1022
{
1023
  return(e->size * mxBigIntWordSize - fxBigInt_ffs(e));
1024
}
1025
#else
1026
int
1027
fxBigInt_bitsize(txBigInt *a)
1028
258k
{
1029
258k
  int i = a->size;
1030
258k
  txU4 w;
1031
322k
  while (i > 0) {
1032
258k
    i--;
1033
258k
    w = a->data[i];
1034
258k
    if (w) {
1035
193k
      int c = __builtin_clz(w);
1036
193k
      return (i * mxBigIntWordSize) + mxBigIntWordSize - c;
1037
193k
    }
1038
258k
  }
1039
64.4k
  return 0;
1040
258k
}
1041
#endif
1042
1043
int fxBigInt_isset(txBigInt *e, txU4 i)
1044
602k
{
1045
602k
  txU4 w = e->data[i / mxBigIntWordSize];
1046
602k
  return((w & ((txU4)1 << (i % mxBigIntWordSize))) != 0);
1047
602k
}
1048
1049
/*
1050
 * bitwise operations
1051
 */
1052
1053
txBigInt *fxBigInt_not(txMachine* the, txBigInt *r, txBigInt *a)
1054
380k
{
1055
380k
  if (a->sign)
1056
1.94k
    r = fxBigInt_usub(the, r, a, (txBigInt *)&gxBigIntOne);
1057
378k
  else {
1058
378k
    r = fxBigInt_uadd(the, r, a, (txBigInt *)&gxBigIntOne);
1059
378k
    r->sign = 1;
1060
378k
  }
1061
380k
  return(r);
1062
380k
}
1063
1064
txBigInt *fxBigInt_and(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1065
4.06k
{
1066
4.06k
  txBigInt *aa, *bb;
1067
4.06k
  int i;
1068
4.06k
  if (a->sign) {
1069
2.80k
    if (b->sign)
1070
2.38k
      goto AND_MINUS_MINUS;
1071
419
    aa = a;
1072
419
    a = b;
1073
419
    b = aa; 
1074
419
    goto AND_PLUS_MINUS;
1075
2.80k
  }
1076
1.26k
  if (b->sign)
1077
631
    goto AND_PLUS_MINUS;
1078
636
  return fxBigInt_fit(the, fxBigInt_uand(the, r, a, b));
1079
1.05k
AND_PLUS_MINUS:
1080
// GMP: OP1 & -OP2 == OP1 & ~(OP2 - 1)
1081
1.05k
  if (r == NULL)
1082
1.05k
    r = fxBigInt_alloc(the, a->size);
1083
1.05k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1084
1.05k
    if (a->size > bb->size) {
1085
1.07k
    for (i = 0; i < bb->size; i++)
1086
681
      r->data[i] = a->data[i] & ~bb->data[i];
1087
12.4k
    for (; i < a->size; i++)
1088
12.0k
      r->data[i] = a->data[i];
1089
394
    }
1090
656
    else {
1091
1.52k
    for (i = 0; i < a->size; i++)
1092
864
      r->data[i] = a->data[i] & ~bb->data[i];
1093
656
    }
1094
1.05k
    fxBigInt_free(the, bb);
1095
1.05k
  return fxBigInt_fit(the, r);
1096
2.38k
AND_MINUS_MINUS:
1097
// GMP: -((-OP1) & (-OP2)) = -(~(OP1 - 1) & ~(OP2 - 1)) == ~(~(OP1 - 1) & ~(OP2 - 1)) + 1 == ((OP1 - 1) | (OP2 - 1)) + 1
1098
2.38k
  if (r == NULL)
1099
2.38k
    r = fxBigInt_alloc(the, MAX(a->size, b->size) + 1);
1100
2.38k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1101
2.38k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1102
2.38k
  r = fxBigInt_uor(the, r, aa, bb);
1103
2.38k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1104
2.38k
  r->sign = 1;
1105
2.38k
  fxBigInt_free(the, bb);
1106
2.38k
  fxBigInt_free(the, aa);
1107
2.38k
  return fxBigInt_fit(the, r);
1108
1.26k
}
1109
1110
txBigInt *fxBigInt_uand(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1111
185k
{
1112
185k
  int i;
1113
185k
  if (a->size > b->size) {
1114
101k
    txBigInt *t = b;
1115
101k
    b = a;
1116
101k
    a = t;
1117
101k
  }
1118
185k
  if (r == NULL)
1119
12.9k
    r = fxBigInt_alloc(the, a->size);
1120
172k
  else
1121
172k
    r->size = a->size;
1122
942k
  for (i = 0; i < a->size; i++)
1123
756k
    r->data[i] = a->data[i] & b->data[i];
1124
185k
  return(r);
1125
185k
}
1126
1127
txBigInt *fxBigInt_or(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1128
783k
{
1129
783k
  txBigInt *aa, *bb;
1130
783k
  int i;
1131
783k
  mxBigInt_meter(MAX(a->size, b->size));
1132
783k
  if (a->sign) {
1133
680k
    if (b->sign)
1134
172k
      goto OR_MINUS_MINUS;
1135
507k
    aa = a;
1136
507k
    a = b;
1137
507k
    b = aa; 
1138
507k
    goto OR_PLUS_MINUS;
1139
680k
  }
1140
102k
  if (b->sign)
1141
100k
    goto OR_PLUS_MINUS;
1142
2.20k
  return fxBigInt_fit(the, fxBigInt_uor(the, r, a, b));
1143
608k
OR_PLUS_MINUS:
1144
// GMP: -(OP1 | (-OP2)) = -(OP1 | ~(OP2 - 1)) == ~(OP1 | ~(OP2 - 1)) + 1 == (~OP1 & (OP2 - 1)) + 1
1145
608k
  if (r == NULL)
1146
608k
    r = fxBigInt_alloc(the, b->size + 1);
1147
608k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1148
608k
    if (a->size < bb->size) {
1149
516k
    for (i = 0; i < a->size; i++)
1150
290k
      r->data[i] = ~a->data[i] & bb->data[i];
1151
2.36M
    for (; i < bb->size; i++)
1152
2.13M
      r->data[i] = bb->data[i];
1153
226k
  }
1154
381k
  else {
1155
1.15M
    for (i = 0; i < bb->size; i++)
1156
772k
      r->data[i] = ~a->data[i] & bb->data[i];
1157
381k
  }
1158
608k
  r->size = bb->size;
1159
608k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1160
608k
  r->sign = 1;
1161
608k
  fxBigInt_free(the, bb);
1162
608k
  return fxBigInt_fit(the, r);
1163
172k
OR_MINUS_MINUS:
1164
// GMP: -((-OP1) | (-OP2)) = -(~(OP1 - 1) | ~(OP2 - 1)) == ~(~(OP1 - 1) | ~(OP2 - 1)) + 1 = = ((OP1 - 1) & (OP2 - 1)) + 1
1165
172k
  if (r == NULL)
1166
172k
    r = fxBigInt_alloc(the, MIN(a->size, b->size) + 1);
1167
172k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1168
172k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1169
172k
  r = fxBigInt_uand(the, r, aa, bb);
1170
172k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1171
172k
  r->sign = 1;
1172
172k
  fxBigInt_free(the, bb);
1173
172k
  fxBigInt_free(the, aa);
1174
172k
  return fxBigInt_fit(the, r);
1175
102k
}
1176
1177
txBigInt *fxBigInt_uor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1178
4.59k
{
1179
4.59k
  int i;
1180
4.59k
  if (a->size < b->size) {
1181
1.11k
    txBigInt *t = b;
1182
1.11k
    b = a;
1183
1.11k
    a = t;
1184
1.11k
  }
1185
4.59k
  if (r == NULL)
1186
2.20k
    r = fxBigInt_alloc(the, a->size);
1187
2.38k
  else
1188
2.38k
    r->size = a->size;
1189
12.9k
  for (i = 0; i < b->size; i++)
1190
8.34k
    r->data[i] = a->data[i] | b->data[i];
1191
374k
  for (; i < a->size; i++)
1192
369k
    r->data[i] = a->data[i];
1193
4.59k
  return(r);
1194
4.59k
}
1195
1196
txBigInt *fxBigInt_xor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1197
469k
{
1198
469k
  txBigInt *aa, *bb;
1199
469k
  if (a->sign) {
1200
30.7k
    if (b->sign)
1201
30.5k
      goto XOR_MINUS_MINUS;
1202
197
    aa = a;
1203
197
    a = b;
1204
197
    b = aa; 
1205
197
    goto XOR_PLUS_MINUS;
1206
30.7k
  }
1207
438k
  if (b->sign)
1208
422k
    goto XOR_PLUS_MINUS;
1209
16.1k
  return fxBigInt_fit(the, fxBigInt_uxor(the, r, a, b));
1210
422k
XOR_PLUS_MINUS:
1211
// GMP: -(OP1 ^ (-OP2)) == -(OP1 ^ ~(OP2 - 1)) == ~(OP1 ^ ~(OP2 - 1)) + 1 == (OP1 ^ (OP2 - 1)) + 1
1212
422k
  if (r == NULL)
1213
422k
    r = fxBigInt_alloc(the, MAX(a->size, b->size) + 1);
1214
422k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1215
422k
  r = fxBigInt_uxor(the, r, a, bb);
1216
422k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1217
422k
  r->sign = 1;
1218
422k
  fxBigInt_free(the, bb);
1219
422k
  return fxBigInt_fit(the, r);
1220
30.5k
XOR_MINUS_MINUS:
1221
// GMP: (-OP1) ^ (-OP2) == ~(OP1 - 1) ^ ~(OP2 - 1) == (OP1 - 1) ^ (OP2 - 1)
1222
30.5k
  if (r == NULL)
1223
30.5k
    r = fxBigInt_alloc(the, MAX(a->size, b->size));
1224
30.5k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1225
30.5k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1226
30.5k
  r = fxBigInt_uxor(the, r, aa, bb);
1227
30.5k
  fxBigInt_free(the, bb);
1228
30.5k
  fxBigInt_free(the, aa);
1229
30.5k
  return fxBigInt_fit(the, r);
1230
438k
}
1231
1232
txBigInt *fxBigInt_uxor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1233
469k
{
1234
469k
  int i;
1235
469k
  if (a->size < b->size) {
1236
136k
    txBigInt *t = b;
1237
136k
    b = a;
1238
136k
    a = t;
1239
136k
  }
1240
469k
  if (r == NULL)
1241
16.1k
    r = fxBigInt_alloc(the, a->size);
1242
453k
  else
1243
453k
    r->size = a->size;
1244
1.07M
  for (i = 0; i < b->size; i++)
1245
603k
    r->data[i] = a->data[i] ^ b->data[i];
1246
11.2M
  for (; i < a->size; i++)
1247
10.7M
    r->data[i] = a->data[i];
1248
469k
  return(r);
1249
469k
}
1250
1251
txBigInt *fxBigInt_lsl(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1252
869
{
1253
869
  if (b->sign) {
1254
429
    if (a->sign) {
1255
49
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1256
49
            if (b->data[0])
1257
43
                r = fxBigInt_uadd(the, C_NULL, r, (txBigInt *)&gxBigIntOne);
1258
49
      r->sign = 1;
1259
49
    }
1260
380
    else
1261
380
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1262
429
  }
1263
440
  else {
1264
440
    r = fxBigInt_ulsl1(the, r, a, b->data[0]);
1265
440
    r->sign = a->sign;
1266
440
  }
1267
869
  return fxBigInt_fit(the, r);
1268
869
}
1269
1270
txBigInt *fxBigInt_ulsl1(txMachine* the, txBigInt *r, txBigInt *a, txU4 sw)
1271
3.12M
{
1272
3.12M
  txU4 wsz, bsz;
1273
3.12M
  int n;
1274
1275
3.12M
  wsz = sw / mxBigIntWordSize;
1276
3.12M
  bsz = sw % mxBigIntWordSize;
1277
3.12M
  n = a->size + wsz + ((bsz == 0) ? 0 : 1);
1278
3.12M
  if (r == NULL) 
1279
388k
    r = fxBigInt_alloc(the, n);
1280
3.12M
  if (bsz == 0) {
1281
128k
    c_memmove(&r->data[wsz], a->data, a->size * sizeof(txU4));
1282
128k
    c_memset(r->data, 0, wsz * sizeof(txU4));
1283
128k
    r->size = a->size + wsz;
1284
128k
  }
1285
2.99M
  else {
1286
2.99M
    r->data[a->size + wsz] = a->data[a->size - 1] >> (mxBigIntWordSize - bsz);
1287
255M
    for (n = a->size; --n >= 1;)
1288
252M
      r->data[n + wsz] = (a->data[n] << bsz) | (a->data[n - 1] >> (mxBigIntWordSize - bsz));
1289
2.99M
    r->data[wsz] = a->data[0] << bsz;
1290
    /* clear the remaining part */
1291
7.17M
    for (n = wsz; --n >= 0;)
1292
4.17M
      r->data[n] = 0;
1293
    /* adjust r */
1294
2.99M
    r->size = a->size + wsz + (r->data[a->size + wsz] == 0 ? 0: 1);
1295
2.99M
  }
1296
3.12M
  return(r);
1297
3.12M
}
1298
1299
txBigInt *fxBigInt_lsr(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1300
84.3k
{
1301
84.3k
  if (b->sign) {
1302
4.63k
    r = fxBigInt_ulsl1(the, r, a, b->data[0]);
1303
4.63k
    r->sign = a->sign;
1304
4.63k
  }
1305
79.6k
  else {
1306
79.6k
    if (a->sign) {
1307
76.2k
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1308
76.2k
            if (b->data[0])
1309
75.5k
                r = fxBigInt_uadd(the, C_NULL, r, (txBigInt *)&gxBigIntOne);
1310
76.2k
      r->sign = 1;
1311
76.2k
    }
1312
3.48k
    else
1313
3.48k
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1314
79.6k
  }
1315
84.3k
  return fxBigInt_fit(the, r);
1316
84.3k
}
1317
1318
txBigInt *fxBigInt_ulsr1(txMachine* the, txBigInt *r, txBigInt *a, txU4 sw)
1319
600k
{
1320
600k
  int wsz, bsz;
1321
600k
  int i, n;
1322
1323
600k
  wsz = sw / mxBigIntWordSize;
1324
600k
  bsz = sw % mxBigIntWordSize;
1325
600k
  n = a->size - wsz;
1326
600k
  if (n <= 0) {
1327
39.3k
    if (r == NULL) r = fxBigInt_alloc(the, 1);
1328
39.3k
    r->size = 1;
1329
39.3k
    r->data[0] = 0;
1330
39.3k
    return(r);
1331
39.3k
  }
1332
  /* 'r' can be the same as 'a' */
1333
560k
  if (r == NULL)
1334
40.7k
    r = fxBigInt_alloc(the, n);
1335
560k
  if (bsz == 0) {
1336
306k
    c_memmove(r->data, &a->data[wsz], n * sizeof(txU4));
1337
306k
    r->size = n;
1338
306k
  }
1339
254k
  else {
1340
314k
    for (i = 0; i < n - 1; i++)
1341
60.8k
      r->data[i] = (a->data[i + wsz] >> bsz) | (a->data[i + wsz + 1] << (mxBigIntWordSize - bsz));
1342
254k
    r->data[i] = a->data[i + wsz] >> bsz;
1343
254k
    r->size = (n > 1 && r->data[n - 1] == 0) ? n - 1: n;
1344
254k
  }
1345
560k
  return(r);
1346
600k
}
1347
1348
txBigInt *fxBigInt_nop(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1349
20.1k
{
1350
20.1k
#ifndef mxCompile
1351
20.1k
  mxTypeError("no such operation");
1352
0
#endif
1353
0
  return C_NULL;
1354
20.1k
}
1355
1356
/*
1357
 * arithmetic
1358
 */
1359
1360
txBigInt *fxBigInt_add(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1361
1.54M
{
1362
1.54M
  if ((aa->sign ^ bb->sign) == 0) {
1363
764k
    rr = fxBigInt_uadd(the, rr, aa, bb);
1364
764k
    if (aa->sign)
1365
187
      rr->sign = 1;
1366
764k
  }
1367
775k
  else {
1368
775k
    if (!aa->sign)
1369
3.46k
      rr = fxBigInt_usub(the, rr, aa, bb);
1370
772k
    else
1371
772k
      rr = fxBigInt_usub(the, rr, bb, aa);
1372
775k
  }
1373
1.54M
  mxBigInt_meter(rr->size);
1374
1.54M
  return(rr);
1375
1.54M
}
1376
1377
txBigInt *fxBigInt_dec(txMachine* the, txBigInt *r, txBigInt *a)
1378
27
{
1379
27
  return fxBigInt_sub(the, r, a, (txBigInt *)&gxBigIntOne);
1380
27
}
1381
1382
txBigInt *fxBigInt_inc(txMachine* the, txBigInt *r, txBigInt *a)
1383
1.52M
{
1384
1.52M
  return fxBigInt_add(the, r, a, (txBigInt *)&gxBigIntOne);
1385
1.52M
}
1386
1387
txBigInt *fxBigInt_neg(txMachine* the, txBigInt *r, txBigInt *a)
1388
8.97M
{
1389
8.97M
  if (r == NULL)
1390
8.97M
    r = fxBigInt_alloc(the, a->size);
1391
8.97M
  fxBigInt_copy(r, a);
1392
8.97M
  if (!fxBigInt_iszero(a))
1393
8.52M
    r->sign = !a->sign;
1394
8.97M
  return(r);
1395
8.97M
}
1396
1397
txBigInt *fxBigInt_sub(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1398
10.2k
{
1399
10.2k
  if ((aa->sign ^ bb->sign) == 0) {
1400
8.77k
    if (!aa->sign)
1401
8.10k
      rr = fxBigInt_usub(the, rr, aa, bb);
1402
673
    else
1403
673
      rr = fxBigInt_usub(the, rr, bb, aa);
1404
8.77k
  }
1405
1.45k
  else {
1406
1.45k
    txU1 sign = aa->sign; /* could be replaced if rr=aa */
1407
1.45k
    rr = fxBigInt_uadd(the, rr, aa, bb);
1408
1.45k
    rr->sign = sign;
1409
1.45k
  }
1410
10.2k
  mxBigInt_meter(rr->size);
1411
10.2k
  return(rr);
1412
10.2k
}
1413
1414
#if __has_builtin(__builtin_add_overflow)
1415
static int fxBigInt_uadd_prim(txU4 *rp, txU4 *ap, txU4 *bp, int an, int bn)
1416
5.30M
{
1417
5.30M
  txU4 c = 0;
1418
5.30M
  int i;
1419
1420
10.6M
  for (i = 0; i < an; i++) {
1421
#ifdef __ets__
1422
  unsigned int r;
1423
  if (__builtin_uadd_overflow(ap[i], bp[i], &r)) {
1424
    rp[i] = r + c;
1425
    c = 1;
1426
  }
1427
  else {
1428
    unsigned int t;
1429
    c = __builtin_uadd_overflow(r, c, &t);
1430
    rp[i] = t;
1431
  }
1432
#else
1433
5.30M
    unsigned int r = rp[i];
1434
5.30M
    c = __builtin_uadd_overflow(ap[i], bp[i], &r) | (txU4)__builtin_uadd_overflow(r, c, &r);
1435
5.30M
    rp[i] = r;
1436
5.30M
#endif
1437
5.30M
  }
1438
5.31M
  for (; c && (i < bn); i++) { {
1439
14.7k
    unsigned int t;
1440
14.7k
    c = __builtin_uadd_overflow(1, bp[i], &t);
1441
14.7k
    rp[i] = t;
1442
14.7k
  }
1443
14.7k
  }
1444
270M
  for (; i < bn; i++) {
1445
265M
    rp[i] = bp[i];
1446
265M
  }
1447
5.30M
  return(c);
1448
5.30M
}
1449
#else
1450
static int fxBigInt_uadd_prim(txU4 *rp, txU4 *ap, txU4 *bp, int an, int bn)
1451
{
1452
  txU4 a, b, t, r, c = 0;
1453
  int i;
1454
1455
  for (i = 0; i < an; i++) {
1456
    a = ap[i];
1457
    b = bp[i];
1458
    t = a + b;
1459
    r = t + c;
1460
    rp[i] = r;
1461
    c = t < a || r < t;
1462
  }
1463
  for (; i < bn; i++) {
1464
    r = bp[i] + c;
1465
    rp[i] = r;
1466
    c = r < c;
1467
  }
1468
  return(c);
1469
}
1470
#endif
1471
1472
txBigInt *fxBigInt_uadd(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1473
5.30M
{
1474
5.30M
  txBigInt *x, *y;
1475
5.30M
  int c;
1476
1477
5.30M
  if (aa->size < bb->size) {
1478
1.32k
    x = aa;
1479
1.32k
    y = bb;
1480
1.32k
  }
1481
5.30M
  else {
1482
5.30M
    x = bb;
1483
5.30M
    y = aa;
1484
5.30M
  }
1485
5.30M
  if (rr == NULL)
1486
1.22M
    rr = fxBigInt_alloc(the, y->size + 1);
1487
5.30M
  c = fxBigInt_uadd_prim(rr->data, x->data, y->data, x->size, y->size);
1488
  /* CAUTION: rr may equals aa or bb. do not touch until here */
1489
5.30M
  rr->size = y->size;
1490
5.30M
  rr->sign = 0;
1491
5.30M
  if (c != 0)
1492
1.10k
    rr->data[rr->size++] = 1;
1493
5.30M
  return(rr);
1494
5.30M
}
1495
1496
txBigInt *fxBigInt_usub(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1497
2.54M
{
1498
2.54M
  int i, n;
1499
2.54M
  txU4 a, b, r, t;
1500
2.54M
  txU4 *ap, *bp, *rp;
1501
2.54M
  txU4 c = 0;
1502
1503
2.54M
  if (rr == NULL)
1504
2.24M
    rr = fxBigInt_alloc(the, MAX(aa->size, bb->size));
1505
2.54M
  rr->sign = (aa->size < bb->size ||
1506
2.47M
        (aa->size == bb->size && fxBigInt_ucomp(aa, bb) < 0));
1507
2.54M
  if (rr->sign) {
1508
761k
    txBigInt *tt = aa;
1509
761k
    aa = bb;
1510
761k
    bb = tt;
1511
761k
  }
1512
2.54M
  ap = aa->data;
1513
2.54M
  bp = bb->data;
1514
2.54M
  rp = rr->data;
1515
2.54M
  n = MIN(aa->size, bb->size);
1516
300M
  for (i = 0; i < n; i++) {
1517
297M
    a = ap[i];
1518
297M
    b = bp[i];
1519
297M
    t = a - b;
1520
297M
    r = t - c;
1521
297M
    rp[i] = r;
1522
297M
    c = a < b || r > t;
1523
297M
  }
1524
2.54M
  if (aa->size >= bb->size) {
1525
2.54M
    n = aa->size;
1526
15.6M
    for (; i < n; i++) {
1527
13.1M
      t = ap[i];
1528
13.1M
      r = t - c;
1529
13.1M
      rp[i] = r;
1530
13.1M
      c = r > t;
1531
13.1M
    }
1532
2.54M
  }
1533
0
  else {
1534
0
    n = bb->size;
1535
0
    for (; i < n; i++) {
1536
0
      t = 0 - bp[i];
1537
0
      r = t - c;
1538
0
      rp[i] = r;
1539
0
      c = r > t;
1540
0
    }
1541
0
  }
1542
  /* remove leading 0s */
1543
2.91M
  while (--i > 0 && rp[i] == 0)
1544
366k
    ;
1545
2.54M
  rr->size = i + 1;
1546
2.54M
  return(rr);
1547
2.54M
}
1548
1549
txBigInt *fxBigInt_mul(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1550
163k
{
1551
163k
  rr = fxBigInt_umul(the, rr, aa, bb);
1552
163k
  if ((aa->sign != bb->sign) && !fxBigInt_iszero(rr))
1553
29.7k
    rr->sign = 1;
1554
163k
  mxBigInt_meter(rr->size);
1555
163k
  return(rr);
1556
163k
}
1557
1558
txBigInt *fxBigInt_umul(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1559
388k
{
1560
388k
  txU4 b;
1561
388k
  txU4 *ap, *bp, *rp;
1562
388k
  txU4 c = 0;
1563
388k
  int i, j, n, m;
1564
1565
388k
  if (rr == NULL)
1566
163k
    rr = fxBigInt_alloc(the, aa->size + bb->size);
1567
388k
  fxBigInt_fill0(rr);
1568
388k
  ap = aa->data;
1569
388k
  bp = bb->data;
1570
388k
  rp = rr->data;
1571
388k
  n = bb->size;
1572
1.22M
  for (i = 0, j = 0; i < n; i++) {
1573
836k
    b = bp[i];
1574
836k
    c = 0;
1575
836k
    m = aa->size;
1576
52.5M
    for (j = 0; j < m; j++)
1577
51.7M
      mxBigIntMulAdd(ap[j], b, rp[i + j], c, rp[i + j], c);
1578
836k
    rp[i + j] = c;
1579
836k
  }
1580
  /* remove leading 0s */
1581
668k
  for (n = i + j; --n > 0 && rp[n] == 0;)
1582
280k
    ;
1583
388k
  rr->size = n + 1;
1584
388k
  rr->sign = 0;
1585
388k
  return(rr);
1586
388k
}
1587
1588
txBigInt *fxBigInt_umul1(txMachine* the, txBigInt *r, txBigInt *a, txU4 b)
1589
850k
{
1590
850k
  int i, n;
1591
850k
  txU4 c = 0;
1592
850k
  txU4 *ap, *rp;
1593
1594
850k
  if (r == NULL)
1595
98.2k
    r = fxBigInt_alloc(the, a->size + 1);
1596
850k
  ap = a->data;
1597
850k
  rp = r->data;
1598
850k
  n = a->size;
1599
298M
  for (i = 0; i < n; i++)
1600
297M
    mxBigIntMulAdd(ap[i], b, c, 0, rp[i], c);
1601
850k
  if (c != 0)
1602
615k
    rp[i] = c;
1603
235k
  else {
1604
    /* remove leading 0s */
1605
676k
    while (--i > 0 && rp[i] == 0)
1606
441k
      ;
1607
235k
  }
1608
850k
  r->size = i + 1;
1609
850k
  return(r);
1610
850k
}
1611
1612
txBigInt *fxBigInt_exp(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1613
229k
{
1614
229k
#ifndef mxCompile
1615
229k
  if (b->sign)
1616
74
    mxRangeError("negative exponent");
1617
229k
#endif
1618
229k
  if (fxBigInt_iszero(b)) {
1619
130k
    if (r == NULL)
1620
130k
      r = fxBigInt_alloc(the, 1);
1621
0
    else {
1622
0
      r->size = 1;
1623
0
      r->sign = 0;
1624
0
    }
1625
130k
    r->data[0] = 1;
1626
130k
  }
1627
98.2k
  else {
1628
98.2k
    int i = fxBigInt_bitsize(b) - 1;
1629
98.2k
    int odd = fxBigInt_isset(b, i);
1630
98.2k
    txU4 c = fxBigInt_bitsize(a);
1631
98.2k
    txBigInt *t = fxBigInt_umul1(the, NULL, b, c);
1632
98.2k
    t = fxBigInt_ulsr1(the, t, t, 5);
1633
98.2k
#ifndef mxCompile
1634
98.2k
    if ((t->size > 1) || (t->data[0] > 0xFFFF))
1635
54
      mxRangeError("too big exponent");
1636
98.1k
#endif
1637
98.1k
    c = 2 + t->data[0];
1638
98.1k
    fxBigInt_free(the, t);
1639
98.1k
        if (r == NULL)
1640
98.1k
      r = fxBigInt_alloc(the, c);
1641
98.1k
      t = fxBigInt_alloc(the, c);
1642
98.1k
        fxBigInt_copy(r, a);
1643
602k
    while (i > 0) {
1644
504k
            i--;
1645
504k
      t = fxBigInt_sqr(the, t, r);
1646
504k
      if ((odd = fxBigInt_isset(b, i))) {
1647
224k
        r->size = c;
1648
224k
        r = fxBigInt_umul(the, r, t, a);
1649
224k
      }
1650
279k
      else {
1651
279k
        txBigInt u = *r;
1652
279k
        *r = *t;
1653
279k
        *t = u;
1654
279k
      }
1655
504k
      t->size = c;
1656
504k
    }
1657
98.1k
    r->sign = a->sign & odd;
1658
98.1k
    fxBigInt_free(the, t);
1659
98.1k
  }
1660
228k
  mxBigInt_meter(r->size);
1661
228k
  return(r);
1662
229k
}
1663
1664
txBigInt *fxBigInt_sqr(txMachine* the, txBigInt *r, txBigInt *a)
1665
504k
{
1666
504k
  int i, j, t;
1667
504k
  txU4 *ap, *rp;
1668
504k
  txU4 ai, c, cc;
1669
504k
  txU4 overflow = 0;  /* overflow flag of 'u' */
1670
1671
504k
  if (r == NULL)
1672
0
    r = fxBigInt_alloc(the, a->size * 2);
1673
504k
  fxBigInt_fill0(r);
1674
504k
  t = a->size;
1675
504k
  ap = a->data;
1676
504k
  rp = r->data;
1677
1678
8.49M
  for (i = 0; i < t - 1; i++) {
1679
7.98M
    mxBigIntMulAdd(ap[i], ap[i], rp[i * 2], 0, rp[i * 2], c);
1680
7.98M
    cc = 0;
1681
7.98M
    ai = ap[i];
1682
1.00G
    for (j = i + 1; j < t; j++) {
1683
      /* ov:hi:lo = 2 * ai * aj + cc:c + rp[k]; the sum is below 2^65 so ov is 0 or 1 */
1684
995M
      int k = i + j;
1685
995M
      txU4 lo, hi, ov, carry;
1686
995M
      mxBigIntMulAdd(ai, ap[j], 0, 0, lo, hi);
1687
995M
      ov = hi >> (mxBigIntWordSize - 1);
1688
995M
      hi = (hi << 1) | (lo >> (mxBigIntWordSize - 1));
1689
995M
      lo <<= 1;
1690
995M
      lo += c; carry = lo < c; hi += carry; ov += hi < carry;
1691
995M
      hi += cc; ov += hi < cc;
1692
995M
      lo += rp[k]; carry = lo < rp[k]; hi += carry; ov += hi < carry;
1693
995M
      rp[k] = lo;
1694
995M
      c = hi;
1695
995M
      cc = ov;
1696
995M
    }
1697
7.98M
    c += overflow;
1698
7.98M
    rp[i + t] = c;    /* c = u */
1699
7.98M
    overflow = cc || c < overflow;
1700
7.98M
  }
1701
  /* the last loop */
1702
504k
  mxBigIntMulAdd(ap[i], ap[i], rp[i * 2], 0, rp[i * 2], c);
1703
504k
  rp[i + t] = c + overflow;
1704
1705
  /* remove leading 0s */
1706
797k
  for (i = 2*t; --i > 0 && rp[i] == 0;)
1707
293k
    ;
1708
504k
  r->size = i + 1;
1709
504k
  return(r);
1710
504k
}
1711
1712
txBigInt *fxBigInt_div(txMachine* the, txBigInt *q, txBigInt *a, txBigInt *b)
1713
7.85k
{
1714
7.85k
#ifndef mxCompile
1715
7.85k
  if (fxBigInt_iszero(b))
1716
2
    mxRangeError("zero divider");
1717
7.84k
#endif
1718
7.84k
  q = fxBigInt_udiv(the, q, a, b, C_NULL);
1719
7.84k
  if (!fxBigInt_iszero(q)) {
1720
5.06k
    if (a->sign)
1721
1.94k
      q->sign = !q->sign;
1722
5.06k
    if (b->sign)
1723
157
      q->sign = !q->sign;
1724
5.06k
  }
1725
7.84k
  mxBigInt_meter(q->size);
1726
7.84k
  return(q);
1727
7.85k
}
1728
1729
txBigInt *fxBigInt_mod(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1730
0
{
1731
0
  txBigInt *q;
1732
0
  mxBigInt_meter(((a->size - b->size) * (a->size + b->size)));
1733
0
  if (r == NULL)
1734
0
    r = fxBigInt_alloc(the, a->sign ? b->size : MIN(a->size, b->size));
1735
0
  q = fxBigInt_udiv(the, NULL, a, b, &r);
1736
0
  if (a->sign) {
1737
0
    if (!fxBigInt_iszero(r))
1738
0
      r = fxBigInt_sub(the, r, b, r);
1739
0
  }
1740
0
  fxBigInt_free(the, q);
1741
0
  mxBigInt_meter(r->size);
1742
0
  return(r);
1743
0
}
1744
1745
txBigInt *fxBigInt_rem(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1746
3.10M
{
1747
3.10M
  txBigInt *q;
1748
3.10M
#ifndef mxCompile
1749
3.10M
  if (fxBigInt_iszero(b))
1750
2
    mxRangeError("zero divider");
1751
3.10M
#endif
1752
3.10M
  mxBigInt_meter(((a->size - b->size) * (a->size + b->size)));
1753
3.10M
  if (r == NULL)
1754
3.10M
    r = fxBigInt_alloc(the, MIN(a->size, b->size));
1755
3.10M
  q = fxBigInt_udiv(the, NULL, a, b, &r);
1756
3.10M
  if (a->sign) {
1757
1.61M
    if (!fxBigInt_iszero(r))
1758
1.50M
            r->sign = !r->sign;
1759
1.61M
  }
1760
3.10M
  fxBigInt_free(the, q);
1761
3.10M
  mxBigInt_meter(r->size);
1762
3.10M
  return(r);
1763
3.10M
}
1764
1765
static void fxBigInt_makepoly(txBigInt *r, txBigInt *a, int t)
1766
426k
{
1767
426k
  int n;
1768
426k
  txU4 *rp, *ap;
1769
1770
  /* make up a polynomial a_t*b^n + a_{t-1}*b^{n-1} + ... */
1771
426k
  n = r->size;
1772
426k
  rp = &r->data[n];
1773
426k
  ap = a->data;
1774
1.58M
  while (--n >= 0) {
1775
1.15M
    *--rp = t < 0 ? 0: ap[t];
1776
1.15M
    --t;
1777
1.15M
  }
1778
426k
}
1779
1780
#if BN_NO_ULDIVMOD
1781
static txU8
1782
div64_32(txU8 a, txU4 b)
1783
{
1784
  txU4 high = (txU4)(a >> 32);
1785
  txU8 r = 0, bb = b, d = 1;
1786
1787
  if (high >= b) {
1788
    high /= b;
1789
    r = (txU8)high << 32;
1790
    a -= (txU8)(high * b) << 32;
1791
  }
1792
  while ((long long)bb > 0 && bb < a) {
1793
    bb += bb;
1794
    d += d;
1795
  }
1796
  do {
1797
    if (a >= bb) {
1798
      a -= bb;
1799
      r += d;
1800
    }
1801
    bb >>= 1;
1802
    d >>= 1;
1803
  } while (d != 0);
1804
  return r;
1805
}
1806
#endif
1807
1808
txBigInt *fxBigInt_udiv(txMachine* the, txBigInt *q, txBigInt *a, txBigInt *b, txBigInt **r)
1809
3.11M
{
1810
3.11M
  int sw;
1811
3.11M
  txBigInt *nb, *na, *tb, *a2, *b2, *tb2, *tb3;
1812
3.11M
  int i, n, t;
1813
3.11M
  txU4 *qp, *ap, *bp;
1814
3.11M
#define mk_dword(p, i)  (((txU8)(p)[i] << mxBigIntWordSize) | (p)[i - 1])
1815
1816
3.11M
  if (fxBigInt_ucomp(a, b) < 0) {
1817
2.99M
    if (q == NULL) {
1818
2.99M
      q = fxBigInt_alloc(the, 1);
1819
2.99M
    }
1820
0
    else {
1821
0
      q->sign = 0;
1822
0
      q->size = 1;
1823
0
    }
1824
2.99M
    q->data[0] = 0;
1825
2.99M
    if (r != NULL) {
1826
2.99M
      if (*r == NULL)
1827
0
        *r = fxBigInt_dup(the, a);
1828
2.99M
      else
1829
2.99M
        fxBigInt_copy(*r, a);
1830
2.99M
      (*r)->sign = 0;
1831
2.99M
    }
1832
2.99M
    return(q);
1833
2.99M
  }
1834
1835
  /* CAUTION: if q is present, it must take account of normalization */
1836
120k
  if (q == NULL)
1837
120k
    q = fxBigInt_alloc(the, a->size - b->size + 2);
1838
120k
  if (r != NULL && *r == NULL)
1839
0
    *r = fxBigInt_alloc(the, b->size);
1840
1841
  /* normalize */
1842
120k
  sw = fxBigInt_ffs(b);
1843
120k
  nb = fxBigInt_ulsl1(the, NULL, b, sw);
1844
120k
  na = fxBigInt_ulsl1(the, NULL, a, sw);
1845
120k
  t = nb->size - 1; /* the size must not change from 'b' */
1846
120k
  n = na->size - 1;
1847
1848
  /* adjust size of q */
1849
120k
  q->size = na->size - nb->size + 1;
1850
120k
  fxBigInt_fill0(q);  /* set 0 to quotient */
1851
1852
  /* process the most significant word */
1853
120k
  tb = fxBigInt_ulsl1(the, NULL, nb, (n - t) * mxBigIntWordSize);  /* y*b^n */
1854
120k
  if (fxBigInt_ucomp(na, tb) >= 0) {
1855
2.16k
    q->data[q->size - 1]++;
1856
2.16k
    fxBigInt_sub(C_NULL, na, na, tb);
1857
    /* since nomalization done, must be na < tb here */
1858
2.16k
  }
1859
1860
  /* prepare the constant for the adjustment: y_t*b + y_{t-1} */
1861
120k
  b2 = fxBigInt_alloc(the, 2);
1862
120k
  fxBigInt_makepoly(b2, nb, t);
1863
  /* and allocate for temporary buffer */
1864
120k
  a2 = fxBigInt_alloc(the, 3);
1865
120k
  tb2 = fxBigInt_alloc(the, 3);
1866
120k
  tb3 = fxBigInt_alloc(the, tb->size + 1);
1867
1868
120k
  qp = &q->data[q->size - 1];
1869
120k
  ap = na->data;
1870
120k
  bp = nb->data;
1871
426k
  for (i = n; i >= t + 1; --i) {
1872
305k
    txU4 tq;
1873
1874
    /* the first estimate */
1875
#if BN_NO_ULDIVMOD
1876
    tq = (ap[i] == bp[t]) ? ~(txU4)0: (txU4)div64_32(mk_dword(ap, i), bp[t]);
1877
#else
1878
305k
    tq = (ap[i] == bp[t]) ? ~(txU4)0: (txU4)(mk_dword(ap, i) / bp[t]);
1879
305k
#endif
1880
1881
    /* adjust */
1882
305k
    fxBigInt_makepoly(a2, na, i);
1883
310k
    while (fxBigInt_ucomp(fxBigInt_umul1(the, tb2, b2, tq), a2) > 0)
1884
4.78k
      --tq;
1885
1886
    /* next x */
1887
305k
    fxBigInt_ulsr1(the, tb, tb, mxBigIntWordSize);
1888
305k
    fxBigInt_usub(the, na, na, fxBigInt_umul1(the, tb3, tb, tq));
1889
305k
    if (na->sign) {
1890
1
      fxBigInt_add(the, na, na, tb);
1891
1
      --tq;
1892
1
    }
1893
305k
    *--qp = tq;
1894
305k
  }
1895
120k
  if (r != NULL)
1896
115k
    *r = fxBigInt_ulsr1(the, *r, na, sw);
1897
  /* remove leading 0s from q */
1898
239k
  for (i = q->size; --i > 0 && q->data[i] == 0;)
1899
118k
    ;
1900
120k
  q->size = i + 1;
1901
  
1902
120k
  fxBigInt_free(the, tb3);
1903
120k
  fxBigInt_free(the, tb2);
1904
120k
  fxBigInt_free(the, a2);
1905
120k
  fxBigInt_free(the, b2);
1906
120k
  fxBigInt_free(the, tb);
1907
120k
  fxBigInt_free(the, na);
1908
120k
  fxBigInt_free(the, nb);
1909
120k
  return(q);
1910
3.11M
}
1911
1912
#ifdef mxMetering
1913
void fxBigInt_meter(txMachine* the, int n)
1914
10.5M
{
1915
10.5M
  n--;
1916
10.5M
  the->meterIndex += n * XS_BIGINT_METERING;
1917
10.5M
}
1918
#endif
1919