Coverage Report

Created: 2025-10-10 06:11

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