@agoric/xsnap 0.14.3-dev-b047af2.0 → 0.14.3-dev-2a16ddd.0

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