@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.370

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 (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1387 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ #include "runtime_internal.h"
29
+
30
+ #include <float.h>
31
+ #include <math.h>
32
+ #include <stdlib.h>
33
+ #include <string.h>
34
+
35
+ /*
36
+ * Exact BigInt primitives. Limbs use 30 value bits so every product,
37
+ * accumulated carry, and shift intermediate fits in uint64_t on each target.
38
+ */
39
+
40
+ #define BIGINT_BITS 30u
41
+ #define BIGINT_BASE (UINT32_C(1) << BIGINT_BITS)
42
+ #define BIGINT_MASK (BIGINT_BASE - UINT32_C(1))
43
+ #define BIGINT_MAX_BITS ((size_t)UINT32_C(65536))
44
+ #define BIGINT_MAX_LIMBS \
45
+ ((BIGINT_MAX_BITS + (size_t)BIGINT_BITS - 1u) / (size_t)BIGINT_BITS)
46
+
47
+ typedef struct {
48
+ uint32_t *limbs;
49
+ size_t length;
50
+ } Magnitude;
51
+
52
+ static size_t magnitude_bits(const uint32_t *limbs, size_t length);
53
+
54
+ static size_t normalized_length(const uint32_t *limbs, size_t length) {
55
+ while (length > 1u && limbs[length - 1u] == 0u) length -= 1u;
56
+ return length;
57
+ }
58
+
59
+ static bool checked_limb_bytes(size_t length, size_t *bytes) {
60
+ if (length == 0u ||
61
+ length > (SIZE_MAX - sizeof(OseoBigInt)) / sizeof(uint32_t)) {
62
+ return false;
63
+ }
64
+ *bytes = sizeof(OseoBigInt) + length * sizeof(uint32_t);
65
+ return true;
66
+ }
67
+
68
+ static uint32_t *allocate_limbs(
69
+ OseoContext *context,
70
+ size_t length,
71
+ bool clear
72
+ ) {
73
+ if (length == 0u || length > SIZE_MAX / sizeof(uint32_t)) return NULL;
74
+ size_t bytes = length * sizeof(uint32_t);
75
+ uint32_t *limbs = oseo_internal_allocate_heap_bytes(context, bytes);
76
+ if (limbs != NULL && clear) memset(limbs, 0, bytes);
77
+ return limbs;
78
+ }
79
+
80
+ static OseoResult allocation_failure(OseoContext *context) {
81
+ return failure(context, "OSEO2001", "BigInt allocation failed.");
82
+ }
83
+
84
+ static OseoResult bigint_limit_error(OseoContext *context) {
85
+ return oseo_internal_throw_error(
86
+ context,
87
+ OSEO_ERROR_RANGE,
88
+ "BigInt exceeds the 65,536-bit implementation limit."
89
+ );
90
+ }
91
+
92
+ static OseoResult publish_bigint(
93
+ OseoContext *context,
94
+ bool negative,
95
+ const uint32_t *limbs,
96
+ size_t length
97
+ ) {
98
+ length = normalized_length(limbs, length);
99
+ if (length == 1u && limbs[0] == 0u) negative = false;
100
+ if (magnitude_bits(limbs, length) > BIGINT_MAX_BITS) {
101
+ return bigint_limit_error(context);
102
+ }
103
+ size_t bytes;
104
+ if (!checked_limb_bytes(length, &bytes)) {
105
+ return failure(context, "OSEO2001", "BigInt is too large.");
106
+ }
107
+ OseoBigInt *integer = oseo_internal_allocate_heap_bytes(context, bytes);
108
+ if (integer == NULL) return allocation_failure(context);
109
+ integer->negative = negative;
110
+ integer->length = length;
111
+ memcpy(integer->limbs, limbs, length * sizeof(uint32_t));
112
+ return oseo_internal_publish_heap(
113
+ context,
114
+ &integer->header,
115
+ OSEO_HEAP_BIGINT
116
+ );
117
+ }
118
+
119
+ static OseoResult small_bigint(
120
+ OseoContext *context,
121
+ bool negative,
122
+ uint32_t value
123
+ ) {
124
+ uint32_t limb = value;
125
+ return publish_bigint(context, negative, &limb, 1u);
126
+ }
127
+
128
+ static int compare_magnitude(
129
+ const uint32_t *left,
130
+ size_t left_length,
131
+ const uint32_t *right,
132
+ size_t right_length
133
+ ) {
134
+ left_length = normalized_length(left, left_length);
135
+ right_length = normalized_length(right, right_length);
136
+ if (left_length < right_length) return -1;
137
+ if (left_length > right_length) return 1;
138
+ for (size_t index = left_length; index > 0u; index -= 1u) {
139
+ uint32_t left_limb = left[index - 1u];
140
+ uint32_t right_limb = right[index - 1u];
141
+ if (left_limb < right_limb) return -1;
142
+ if (left_limb > right_limb) return 1;
143
+ }
144
+ return 0;
145
+ }
146
+
147
+ static Magnitude add_magnitude(
148
+ OseoContext *context,
149
+ const OseoBigInt *left,
150
+ const OseoBigInt *right
151
+ ) {
152
+ size_t length = left->length > right->length
153
+ ? left->length
154
+ : right->length;
155
+ if (length == SIZE_MAX) return (Magnitude){NULL, 0u};
156
+ uint32_t *result = allocate_limbs(context, length + 1u, true);
157
+ if (result == NULL) return (Magnitude){NULL, 0u};
158
+ uint64_t carry = 0u;
159
+ for (size_t index = 0u; index < length; index += 1u) {
160
+ uint64_t sum = carry;
161
+ if (index < left->length) sum += left->limbs[index];
162
+ if (index < right->length) sum += right->limbs[index];
163
+ result[index] = (uint32_t)(sum & BIGINT_MASK);
164
+ carry = sum >> BIGINT_BITS;
165
+ }
166
+ result[length] = (uint32_t)carry;
167
+ return (Magnitude){result, normalized_length(result, length + 1u)};
168
+ }
169
+
170
+ /* Requires |left| >= |right|. */
171
+ static Magnitude subtract_magnitude(
172
+ OseoContext *context,
173
+ const uint32_t *left,
174
+ size_t left_length,
175
+ const uint32_t *right,
176
+ size_t right_length
177
+ ) {
178
+ uint32_t *result = allocate_limbs(context, left_length, true);
179
+ if (result == NULL) return (Magnitude){NULL, 0u};
180
+ uint64_t borrow = 0u;
181
+ for (size_t index = 0u; index < left_length; index += 1u) {
182
+ uint64_t left_limb = left[index];
183
+ uint64_t right_limb = index < right_length ? right[index] : 0u;
184
+ uint64_t needed = right_limb + borrow;
185
+ if (left_limb < needed) {
186
+ result[index] =
187
+ (uint32_t)(left_limb + BIGINT_BASE - needed);
188
+ borrow = 1u;
189
+ } else {
190
+ result[index] = (uint32_t)(left_limb - needed);
191
+ borrow = 0u;
192
+ }
193
+ }
194
+ return (Magnitude){
195
+ result,
196
+ normalized_length(result, left_length),
197
+ };
198
+ }
199
+
200
+ static Magnitude multiply_magnitude(
201
+ OseoContext *context,
202
+ const OseoBigInt *left,
203
+ const OseoBigInt *right
204
+ ) {
205
+ if (left->length > SIZE_MAX - right->length) {
206
+ return (Magnitude){NULL, 0u};
207
+ }
208
+ size_t length = left->length + right->length;
209
+ uint32_t *result = allocate_limbs(context, length, true);
210
+ if (result == NULL) return (Magnitude){NULL, 0u};
211
+ for (size_t left_index = 0u;
212
+ left_index < left->length;
213
+ left_index += 1u) {
214
+ uint64_t carry = 0u;
215
+ for (size_t right_index = 0u;
216
+ right_index < right->length;
217
+ right_index += 1u) {
218
+ size_t index = left_index + right_index;
219
+ uint64_t product =
220
+ (uint64_t)left->limbs[left_index] *
221
+ (uint64_t)right->limbs[right_index] +
222
+ result[index] + carry;
223
+ result[index] = (uint32_t)(product & BIGINT_MASK);
224
+ carry = product >> BIGINT_BITS;
225
+ }
226
+ result[left_index + right->length] = (uint32_t)carry;
227
+ }
228
+ return (Magnitude){result, normalized_length(result, length)};
229
+ }
230
+
231
+ static size_t magnitude_bits(const uint32_t *limbs, size_t length) {
232
+ length = normalized_length(limbs, length);
233
+ uint32_t top = limbs[length - 1u];
234
+ size_t top_bits = 0u;
235
+ while (top != 0u) {
236
+ top >>= 1u;
237
+ top_bits += 1u;
238
+ }
239
+ return (length - 1u) * BIGINT_BITS + top_bits;
240
+ }
241
+
242
+ static bool magnitude_bit(
243
+ const uint32_t *limbs,
244
+ size_t length,
245
+ size_t bit
246
+ ) {
247
+ size_t index = bit / BIGINT_BITS;
248
+ if (index >= length) return false;
249
+ return ((limbs[index] >> (bit % BIGINT_BITS)) & 1u) != 0u;
250
+ }
251
+
252
+ static void shift_one_add(uint32_t *limbs, size_t length, bool bit) {
253
+ uint32_t carry = bit ? 1u : 0u;
254
+ for (size_t index = 0u; index < length; index += 1u) {
255
+ uint32_t next = limbs[index] >> (BIGINT_BITS - 1u);
256
+ limbs[index] = ((limbs[index] << 1u) & BIGINT_MASK) | carry;
257
+ carry = next;
258
+ }
259
+ }
260
+
261
+ static void subtract_in_place(
262
+ uint32_t *left,
263
+ size_t left_length,
264
+ const uint32_t *right,
265
+ size_t right_length
266
+ ) {
267
+ uint64_t borrow = 0u;
268
+ for (size_t index = 0u; index < left_length; index += 1u) {
269
+ uint64_t right_limb = index < right_length ? right[index] : 0u;
270
+ uint64_t needed = right_limb + borrow;
271
+ if ((uint64_t)left[index] < needed) {
272
+ left[index] =
273
+ (uint32_t)((uint64_t)left[index] + BIGINT_BASE - needed);
274
+ borrow = 1u;
275
+ } else {
276
+ left[index] = (uint32_t)((uint64_t)left[index] - needed);
277
+ borrow = 0u;
278
+ }
279
+ }
280
+ }
281
+
282
+ static bool divide_magnitude(
283
+ OseoContext *context,
284
+ const OseoBigInt *dividend,
285
+ const OseoBigInt *divisor,
286
+ Magnitude *quotient,
287
+ Magnitude *remainder
288
+ ) {
289
+ size_t quotient_length = dividend->length;
290
+ if (divisor->length > SIZE_MAX - 1u) return false;
291
+ size_t remainder_length = divisor->length + 1u;
292
+ uint32_t *quotient_limbs =
293
+ allocate_limbs(context, quotient_length, true);
294
+ if (quotient_limbs == NULL) return false;
295
+ uint32_t *remainder_limbs =
296
+ allocate_limbs(context, remainder_length, true);
297
+ if (remainder_limbs == NULL) {
298
+ free(quotient_limbs);
299
+ return false;
300
+ }
301
+ size_t bits = magnitude_bits(dividend->limbs, dividend->length);
302
+ for (size_t cursor = bits; cursor > 0u; cursor -= 1u) {
303
+ size_t bit = cursor - 1u;
304
+ shift_one_add(
305
+ remainder_limbs,
306
+ remainder_length,
307
+ magnitude_bit(dividend->limbs, dividend->length, bit)
308
+ );
309
+ if (compare_magnitude(
310
+ remainder_limbs,
311
+ remainder_length,
312
+ divisor->limbs,
313
+ divisor->length
314
+ ) >= 0) {
315
+ subtract_in_place(
316
+ remainder_limbs,
317
+ remainder_length,
318
+ divisor->limbs,
319
+ divisor->length
320
+ );
321
+ quotient_limbs[bit / BIGINT_BITS] |=
322
+ UINT32_C(1) << (bit % BIGINT_BITS);
323
+ }
324
+ }
325
+ *quotient = (Magnitude){
326
+ quotient_limbs,
327
+ normalized_length(quotient_limbs, quotient_length),
328
+ };
329
+ *remainder = (Magnitude){
330
+ remainder_limbs,
331
+ normalized_length(remainder_limbs, remainder_length),
332
+ };
333
+ return true;
334
+ }
335
+
336
+ static bool bigint_to_size(const OseoBigInt *integer, size_t *value) {
337
+ size_t result = 0u;
338
+ for (size_t index = integer->length; index > 0u; index -= 1u) {
339
+ if (result > (SIZE_MAX - integer->limbs[index - 1u]) / BIGINT_BASE) {
340
+ return false;
341
+ }
342
+ result = result * BIGINT_BASE + integer->limbs[index - 1u];
343
+ }
344
+ *value = result;
345
+ return true;
346
+ }
347
+
348
+ static Magnitude left_shift_magnitude(
349
+ OseoContext *context,
350
+ const OseoBigInt *value,
351
+ size_t shift
352
+ ) {
353
+ size_t limb_shift = shift / BIGINT_BITS;
354
+ uint32_t bit_shift = (uint32_t)(shift % BIGINT_BITS);
355
+ if (value->length > SIZE_MAX - limb_shift - 1u) {
356
+ return (Magnitude){NULL, 0u};
357
+ }
358
+ size_t length = value->length + limb_shift + 1u;
359
+ uint32_t *result = allocate_limbs(context, length, true);
360
+ if (result == NULL) return (Magnitude){NULL, 0u};
361
+ uint32_t carry = 0u;
362
+ for (size_t index = 0u; index < value->length; index += 1u) {
363
+ uint64_t shifted =
364
+ ((uint64_t)value->limbs[index] << bit_shift) | carry;
365
+ result[index + limb_shift] = (uint32_t)(shifted & BIGINT_MASK);
366
+ carry = (uint32_t)(shifted >> BIGINT_BITS);
367
+ }
368
+ result[value->length + limb_shift] = carry;
369
+ return (Magnitude){result, normalized_length(result, length)};
370
+ }
371
+
372
+ static Magnitude right_shift_magnitude(
373
+ OseoContext *context,
374
+ const OseoBigInt *value,
375
+ size_t shift,
376
+ bool *discarded
377
+ ) {
378
+ size_t limb_shift = shift / BIGINT_BITS;
379
+ uint32_t bit_shift = (uint32_t)(shift % BIGINT_BITS);
380
+ *discarded = false;
381
+ for (size_t index = 0u;
382
+ index < value->length && index < limb_shift;
383
+ index += 1u) {
384
+ if (value->limbs[index] != 0u) *discarded = true;
385
+ }
386
+ if (limb_shift >= value->length) {
387
+ if (!oseo_internal_bigint_is_zero(
388
+ tagged(OSEO_TAG_HEAP, (uint64_t)(uintptr_t)value)
389
+ )) {
390
+ *discarded = true;
391
+ }
392
+ uint32_t *zero = allocate_limbs(context, 1u, true);
393
+ return (Magnitude){zero, zero == NULL ? 0u : 1u};
394
+ }
395
+ size_t length = value->length - limb_shift;
396
+ uint32_t *result = allocate_limbs(context, length, true);
397
+ if (result == NULL) return (Magnitude){NULL, 0u};
398
+ uint32_t carry = 0u;
399
+ for (size_t cursor = value->length; cursor > limb_shift; cursor -= 1u) {
400
+ uint32_t limb = value->limbs[cursor - 1u];
401
+ size_t output = cursor - 1u - limb_shift;
402
+ if (bit_shift == 0u) {
403
+ result[output] = limb;
404
+ } else {
405
+ result[output] =
406
+ (limb >> bit_shift) |
407
+ (carry << (BIGINT_BITS - bit_shift));
408
+ carry = limb & ((UINT32_C(1) << bit_shift) - 1u);
409
+ }
410
+ }
411
+ if (carry != 0u) *discarded = true;
412
+ return (Magnitude){result, normalized_length(result, length)};
413
+ }
414
+
415
+ static Magnitude increment_magnitude(
416
+ OseoContext *context,
417
+ Magnitude input
418
+ ) {
419
+ if (input.length == SIZE_MAX) {
420
+ free(input.limbs);
421
+ return (Magnitude){NULL, 0u};
422
+ }
423
+ uint32_t *result = allocate_limbs(context, input.length + 1u, true);
424
+ if (result == NULL) {
425
+ free(input.limbs);
426
+ return (Magnitude){NULL, 0u};
427
+ }
428
+ memcpy(result, input.limbs, input.length * sizeof(uint32_t));
429
+ free(input.limbs);
430
+ uint32_t carry = 1u;
431
+ for (size_t index = 0u; index < input.length && carry != 0u; index += 1u) {
432
+ uint32_t sum = result[index] + carry;
433
+ result[index] = sum & BIGINT_MASK;
434
+ carry = sum >> BIGINT_BITS;
435
+ }
436
+ result[input.length] = carry;
437
+ return (Magnitude){
438
+ result,
439
+ normalized_length(result, input.length + 1u),
440
+ };
441
+ }
442
+
443
+ static Magnitude bitwise_magnitude(
444
+ OseoContext *context,
445
+ const OseoBigInt *left,
446
+ const OseoBigInt *right,
447
+ OseoBigIntOperator operator,
448
+ bool *negative
449
+ ) {
450
+ size_t width = left->length > right->length
451
+ ? left->length
452
+ : right->length;
453
+ if (width == SIZE_MAX) return (Magnitude){NULL, 0u};
454
+ width += 1u;
455
+ uint32_t *left_bits = allocate_limbs(context, width, true);
456
+ if (left_bits == NULL) return (Magnitude){NULL, 0u};
457
+ uint32_t *right_bits = allocate_limbs(context, width, true);
458
+ if (right_bits == NULL) {
459
+ free(left_bits);
460
+ return (Magnitude){NULL, 0u};
461
+ }
462
+ memcpy(left_bits, left->limbs, left->length * sizeof(uint32_t));
463
+ memcpy(right_bits, right->limbs, right->length * sizeof(uint32_t));
464
+ uint32_t *operands[2] = {left_bits, right_bits};
465
+ const bool signs[2] = {left->negative, right->negative};
466
+ for (size_t operand = 0u; operand < 2u; operand += 1u) {
467
+ if (!signs[operand]) continue;
468
+ uint32_t carry = 1u;
469
+ for (size_t index = 0u; index < width; index += 1u) {
470
+ uint32_t value = (~operands[operand][index]) & BIGINT_MASK;
471
+ uint32_t sum = value + carry;
472
+ operands[operand][index] = sum & BIGINT_MASK;
473
+ carry = sum >> BIGINT_BITS;
474
+ }
475
+ }
476
+ uint32_t *result = left_bits;
477
+ for (size_t index = 0u; index < width; index += 1u) {
478
+ if (operator == OSEO_BIGINT_AND) {
479
+ result[index] &= right_bits[index];
480
+ } else if (operator == OSEO_BIGINT_OR) {
481
+ result[index] |= right_bits[index];
482
+ } else {
483
+ result[index] ^= right_bits[index];
484
+ }
485
+ }
486
+ free(right_bits);
487
+ *negative = (result[width - 1u] >> (BIGINT_BITS - 1u)) != 0u;
488
+ if (*negative) {
489
+ uint32_t carry = 1u;
490
+ for (size_t index = 0u; index < width; index += 1u) {
491
+ uint32_t value = (~result[index]) & BIGINT_MASK;
492
+ uint32_t sum = value + carry;
493
+ result[index] = sum & BIGINT_MASK;
494
+ carry = sum >> BIGINT_BITS;
495
+ }
496
+ }
497
+ return (Magnitude){result, normalized_length(result, width)};
498
+ }
499
+
500
+ static OseoResult arithmetic_result(
501
+ OseoContext *context,
502
+ bool negative,
503
+ Magnitude magnitude
504
+ ) {
505
+ if (magnitude.limbs == NULL) return allocation_failure(context);
506
+ OseoResult result = publish_bigint(
507
+ context,
508
+ negative,
509
+ magnitude.limbs,
510
+ magnitude.length
511
+ );
512
+ free(magnitude.limbs);
513
+ return result;
514
+ }
515
+
516
+ static OseoResult add_or_subtract(
517
+ OseoContext *context,
518
+ const OseoBigInt *left,
519
+ const OseoBigInt *right,
520
+ bool subtract
521
+ ) {
522
+ bool right_negative = right->negative != subtract;
523
+ if (left->negative == right_negative) {
524
+ Magnitude sum = add_magnitude(context, left, right);
525
+ return arithmetic_result(context, left->negative, sum);
526
+ }
527
+ int order = compare_magnitude(
528
+ left->limbs,
529
+ left->length,
530
+ right->limbs,
531
+ right->length
532
+ );
533
+ if (order == 0) return small_bigint(context, false, 0u);
534
+ const OseoBigInt *larger = order > 0 ? left : right;
535
+ const OseoBigInt *smaller = order > 0 ? right : left;
536
+ Magnitude difference = subtract_magnitude(
537
+ context,
538
+ larger->limbs,
539
+ larger->length,
540
+ smaller->limbs,
541
+ smaller->length
542
+ );
543
+ bool negative = order > 0 ? left->negative : right_negative;
544
+ return arithmetic_result(context, negative, difference);
545
+ }
546
+
547
+ static OseoResult shift_result(
548
+ OseoContext *context,
549
+ const OseoBigInt *left,
550
+ const OseoBigInt *right,
551
+ bool requested_left
552
+ ) {
553
+ if (left->length == 1u && left->limbs[0] == 0u) {
554
+ return small_bigint(context, false, 0u);
555
+ }
556
+ bool left_direction = requested_left != right->negative;
557
+ size_t shift;
558
+ if (!bigint_to_size(right, &shift)) {
559
+ if (left_direction) {
560
+ return bigint_limit_error(context);
561
+ }
562
+ return small_bigint(context, left->negative, left->negative ? 1u : 0u);
563
+ }
564
+ if (left_direction) {
565
+ size_t bits = magnitude_bits(left->limbs, left->length);
566
+ if (shift > BIGINT_MAX_BITS || bits > BIGINT_MAX_BITS - shift) {
567
+ return bigint_limit_error(context);
568
+ }
569
+ return arithmetic_result(
570
+ context,
571
+ left->negative,
572
+ left_shift_magnitude(context, left, shift)
573
+ );
574
+ }
575
+ bool discarded = false;
576
+ Magnitude result = right_shift_magnitude(
577
+ context,
578
+ left,
579
+ shift,
580
+ &discarded
581
+ );
582
+ if (left->negative && discarded && result.limbs != NULL) {
583
+ result = increment_magnitude(context, result);
584
+ }
585
+ return arithmetic_result(context, left->negative, result);
586
+ }
587
+
588
+ static OseoResult exponentiate(
589
+ OseoContext *context,
590
+ OseoValue left_value,
591
+ OseoValue right_value
592
+ ) {
593
+ OseoBigInt *exponent = bigint_object(right_value);
594
+ if (exponent->negative) {
595
+ return oseo_internal_throw_error(
596
+ context,
597
+ OSEO_ERROR_RANGE,
598
+ "A BigInt exponent cannot be negative."
599
+ );
600
+ }
601
+ size_t exponent_size;
602
+ OseoBigInt *base = bigint_object(left_value);
603
+ if (!bigint_to_size(exponent, &exponent_size)) {
604
+ if (oseo_internal_bigint_is_zero(left_value)) {
605
+ return small_bigint(context, false, 0u);
606
+ }
607
+ if (base->length == 1u && base->limbs[0] == 1u) {
608
+ bool negative = base->negative &&
609
+ (exponent->limbs[0] & 1u) != 0u;
610
+ return small_bigint(context, negative, 1u);
611
+ }
612
+ return bigint_limit_error(context);
613
+ }
614
+ size_t base_bits = magnitude_bits(base->limbs, base->length);
615
+ if (base_bits > 1u &&
616
+ exponent_size > (BIGINT_MAX_BITS - 1u) / (base_bits - 1u)) {
617
+ return bigint_limit_error(context);
618
+ }
619
+ OseoValue slots[4] = {
620
+ left_value,
621
+ right_value,
622
+ oseo_undefined(),
623
+ oseo_undefined(),
624
+ };
625
+ OseoRootFrame frame = {NULL, slots, 4u};
626
+ oseo_roots_push(context, &frame);
627
+ OseoResult one = small_bigint(context, false, 1u);
628
+ if (one.status != OSEO_STATUS_NORMAL) {
629
+ oseo_roots_pop(context, &frame);
630
+ return one;
631
+ }
632
+ slots[2] = one.value;
633
+ slots[3] = slots[0];
634
+ size_t remaining = exponent_size;
635
+ while (remaining > 0u) {
636
+ if ((remaining & 1u) != 0u) {
637
+ Magnitude product = multiply_magnitude(
638
+ context,
639
+ bigint_object(slots[2]),
640
+ bigint_object(slots[3])
641
+ );
642
+ OseoResult next = arithmetic_result(
643
+ context,
644
+ bigint_object(slots[2])->negative !=
645
+ bigint_object(slots[3])->negative,
646
+ product
647
+ );
648
+ if (next.status != OSEO_STATUS_NORMAL) {
649
+ oseo_roots_pop(context, &frame);
650
+ return next;
651
+ }
652
+ slots[2] = next.value;
653
+ }
654
+ remaining >>= 1u;
655
+ if (remaining == 0u) break;
656
+ Magnitude square = multiply_magnitude(
657
+ context,
658
+ bigint_object(slots[3]),
659
+ bigint_object(slots[3])
660
+ );
661
+ OseoResult next = arithmetic_result(context, false, square);
662
+ if (next.status != OSEO_STATUS_NORMAL) {
663
+ oseo_roots_pop(context, &frame);
664
+ return next;
665
+ }
666
+ slots[3] = next.value;
667
+ }
668
+ OseoResult result = normal(slots[2]);
669
+ oseo_roots_pop(context, &frame);
670
+ return result;
671
+ }
672
+
673
+ OseoResult oseo_bigint_literal(
674
+ OseoContext *context,
675
+ const char *digits,
676
+ uint32_t radix
677
+ ) {
678
+ if (digits == NULL ||
679
+ (radix != 2u && radix != 8u && radix != 10u && radix != 16u)) {
680
+ return failure(context, "OSEO2001", "Invalid BigInt constant.");
681
+ }
682
+ size_t length = strlen(digits);
683
+ if (length == 0u || length > (SIZE_MAX - 1u) / 4u) {
684
+ return failure(context, "OSEO2001", "Invalid BigInt constant.");
685
+ }
686
+ size_t capacity = length * 4u / BIGINT_BITS + 2u;
687
+ if (capacity > BIGINT_MAX_LIMBS + 1u) {
688
+ capacity = BIGINT_MAX_LIMBS + 1u;
689
+ }
690
+ uint32_t *limbs = allocate_limbs(context, capacity, true);
691
+ if (limbs == NULL) return allocation_failure(context);
692
+ size_t limb_count = 1u;
693
+ for (size_t index = 0u; index < length; index += 1u) {
694
+ char character = digits[index];
695
+ uint32_t digit;
696
+ if (character >= '0' && character <= '9') {
697
+ digit = (uint32_t)(character - '0');
698
+ } else if (character >= 'a' && character <= 'f') {
699
+ digit = (uint32_t)(character - 'a' + 10);
700
+ } else {
701
+ free(limbs);
702
+ return failure(context, "OSEO2001", "Invalid BigInt constant.");
703
+ }
704
+ if (digit >= radix) {
705
+ free(limbs);
706
+ return failure(context, "OSEO2001", "Invalid BigInt constant.");
707
+ }
708
+ uint64_t carry = digit;
709
+ for (size_t limb = 0u; limb < limb_count; limb += 1u) {
710
+ uint64_t value = (uint64_t)limbs[limb] * radix + carry;
711
+ limbs[limb] = (uint32_t)(value & BIGINT_MASK);
712
+ carry = value >> BIGINT_BITS;
713
+ }
714
+ if (carry != 0u) {
715
+ if (limb_count >= capacity) {
716
+ free(limbs);
717
+ return bigint_limit_error(context);
718
+ }
719
+ limbs[limb_count] = (uint32_t)carry;
720
+ limb_count += 1u;
721
+ }
722
+ }
723
+ OseoResult result = publish_bigint(
724
+ context,
725
+ false,
726
+ limbs,
727
+ limb_count
728
+ );
729
+ free(limbs);
730
+ return result;
731
+ }
732
+
733
+ bool oseo_internal_bigint_is_zero(OseoValue value) {
734
+ OseoBigInt *integer = bigint_object(value);
735
+ return integer->length == 1u && integer->limbs[0] == 0u;
736
+ }
737
+
738
+ static double magnitude_to_number(
739
+ const uint32_t *limbs,
740
+ size_t length,
741
+ bool negative
742
+ ) {
743
+ size_t bit_length = magnitude_bits(limbs, length);
744
+ if (bit_length == 0u) return 0.0;
745
+
746
+ size_t kept_bits = bit_length < DBL_MANT_DIG
747
+ ? bit_length
748
+ : DBL_MANT_DIG;
749
+ uint64_t significant = 0u;
750
+ for (size_t offset = 0u; offset < kept_bits; offset += 1u) {
751
+ size_t bit = bit_length - offset - 1u;
752
+ size_t limb = bit / BIGINT_BITS;
753
+ size_t shift = bit % BIGINT_BITS;
754
+ significant = (significant << 1u) |
755
+ ((limbs[limb] >> shift) & 1u);
756
+ }
757
+
758
+ size_t discarded = bit_length - kept_bits;
759
+ if (discarded > 0u) {
760
+ size_t round_index = discarded - 1u;
761
+ size_t round_limb = round_index / BIGINT_BITS;
762
+ size_t round_shift = round_index % BIGINT_BITS;
763
+ bool round_bit =
764
+ ((limbs[round_limb] >> round_shift) & 1u) != 0u;
765
+ bool sticky = false;
766
+ for (size_t bit = 0u; bit < round_index; bit += 1u) {
767
+ size_t limb = bit / BIGINT_BITS;
768
+ size_t shift = bit % BIGINT_BITS;
769
+ if (((limbs[limb] >> shift) & 1u) != 0u) {
770
+ sticky = true;
771
+ break;
772
+ }
773
+ }
774
+ if (round_bit && (sticky || (significant & 1u) != 0u)) {
775
+ significant += 1u;
776
+ if (significant == (UINT64_C(1) << DBL_MANT_DIG)) {
777
+ significant >>= 1u;
778
+ discarded += 1u;
779
+ }
780
+ }
781
+ }
782
+ double number = discarded > (size_t)(DBL_MAX_EXP - DBL_MANT_DIG)
783
+ ? INFINITY
784
+ : ldexp((double)significant, (int)discarded);
785
+ return negative ? -number : number;
786
+ }
787
+
788
+ double oseo_internal_bigint_to_number(OseoValue value) {
789
+ const OseoBigInt *integer = bigint_object(value);
790
+ return magnitude_to_number(
791
+ integer->limbs,
792
+ integer->length,
793
+ integer->negative
794
+ );
795
+ }
796
+
797
+ double oseo_internal_integer_digits_to_number(
798
+ const uint16_t *units,
799
+ size_t length,
800
+ uint32_t radix
801
+ ) {
802
+ enum {
803
+ NUMBER_LIMBS =
804
+ (DBL_MAX_EXP + (int)BIGINT_BITS) / (int)BIGINT_BITS,
805
+ };
806
+ uint32_t limbs[NUMBER_LIMBS] = {0u};
807
+ size_t limb_count = 1u;
808
+ for (size_t index = 0u; index < length; index += 1u) {
809
+ uint16_t unit = units[index];
810
+ uint32_t digit;
811
+ if (unit >= UINT16_C('0') && unit <= UINT16_C('9')) {
812
+ digit = (uint32_t)(unit - UINT16_C('0'));
813
+ } else if (unit >= UINT16_C('a') && unit <= UINT16_C('z')) {
814
+ digit = (uint32_t)(unit - UINT16_C('a')) + 10u;
815
+ } else if (unit >= UINT16_C('A') && unit <= UINT16_C('Z')) {
816
+ digit = (uint32_t)(unit - UINT16_C('A')) + 10u;
817
+ } else {
818
+ return NAN;
819
+ }
820
+ if (digit >= radix) return NAN;
821
+ uint64_t carry = digit;
822
+ for (size_t limb = 0u; limb < limb_count; limb += 1u) {
823
+ uint64_t next = (uint64_t)limbs[limb] * radix + carry;
824
+ limbs[limb] = (uint32_t)(next & BIGINT_MASK);
825
+ carry = next >> BIGINT_BITS;
826
+ }
827
+ if (carry != 0u) {
828
+ if (limb_count >= NUMBER_LIMBS) return INFINITY;
829
+ limbs[limb_count] = (uint32_t)carry;
830
+ limb_count += 1u;
831
+ }
832
+ if (magnitude_bits(limbs, limb_count) > DBL_MAX_EXP) {
833
+ return INFINITY;
834
+ }
835
+ }
836
+ return magnitude_to_number(limbs, limb_count, false);
837
+ }
838
+
839
+ bool oseo_internal_bigint_equal(OseoValue left, OseoValue right) {
840
+ OseoBigInt *left_integer = bigint_object(left);
841
+ OseoBigInt *right_integer = bigint_object(right);
842
+ return left_integer->negative == right_integer->negative &&
843
+ left_integer->length == right_integer->length &&
844
+ memcmp(
845
+ left_integer->limbs,
846
+ right_integer->limbs,
847
+ left_integer->length * sizeof(uint32_t)
848
+ ) == 0;
849
+ }
850
+
851
+ int oseo_internal_bigint_compare(OseoValue left, OseoValue right) {
852
+ OseoBigInt *left_integer = bigint_object(left);
853
+ OseoBigInt *right_integer = bigint_object(right);
854
+ if (left_integer->negative != right_integer->negative) {
855
+ return left_integer->negative ? -1 : 1;
856
+ }
857
+ int order = compare_magnitude(
858
+ left_integer->limbs,
859
+ left_integer->length,
860
+ right_integer->limbs,
861
+ right_integer->length
862
+ );
863
+ return left_integer->negative ? -order : order;
864
+ }
865
+
866
+ static int compare_magnitude_double(const OseoBigInt *integer, double number) {
867
+ if (number < 1.0) {
868
+ return oseo_internal_bigint_is_zero(
869
+ tagged(OSEO_TAG_HEAP, (uint64_t)(uintptr_t)integer)
870
+ ) ? 0 : 1;
871
+ }
872
+ int exponent;
873
+ double fraction = frexp(number, &exponent);
874
+ uint64_t significand = (uint64_t)ldexp(fraction, DBL_MANT_DIG);
875
+ size_t integer_bits = magnitude_bits(integer->limbs, integer->length);
876
+ size_t number_bits = (size_t)exponent;
877
+ if (integer_bits < number_bits) return -1;
878
+ if (integer_bits > number_bits) return 1;
879
+ int shift = exponent - DBL_MANT_DIG;
880
+ for (size_t cursor = integer_bits; cursor > 0u; cursor -= 1u) {
881
+ size_t bit = cursor - 1u;
882
+ bool integer_bit = magnitude_bit(
883
+ integer->limbs,
884
+ integer->length,
885
+ bit
886
+ );
887
+ bool number_bit = false;
888
+ if (shift >= 0) {
889
+ if (bit >= (size_t)shift &&
890
+ bit - (size_t)shift < DBL_MANT_DIG) {
891
+ number_bit =
892
+ ((significand >> (bit - (size_t)shift)) & 1u) != 0u;
893
+ }
894
+ } else {
895
+ size_t discarded = (size_t)(-shift);
896
+ if (bit + discarded < DBL_MANT_DIG) {
897
+ number_bit =
898
+ ((significand >> (bit + discarded)) & 1u) != 0u;
899
+ }
900
+ }
901
+ if (integer_bit != number_bit) return integer_bit ? 1 : -1;
902
+ }
903
+ return 0;
904
+ }
905
+
906
+ int oseo_internal_bigint_compare_number(OseoValue value, double number) {
907
+ OseoBigInt *integer = bigint_object(value);
908
+ if (isinf(number)) return number > 0.0 ? -1 : 1;
909
+ bool number_negative = signbit(number) && number != 0.0;
910
+ if (integer->negative != number_negative) {
911
+ return integer->negative ? -1 : 1;
912
+ }
913
+ double absolute = fabs(number);
914
+ double integral = trunc(absolute);
915
+ int order = compare_magnitude_double(integer, integral);
916
+ if (integer->negative) order = -order;
917
+ if (order == 0 && absolute != integral) {
918
+ return integer->negative ? 1 : -1;
919
+ }
920
+ return order;
921
+ }
922
+
923
+ OseoResult oseo_internal_bigint_negate(
924
+ OseoContext *context,
925
+ OseoValue value
926
+ ) {
927
+ OseoBigInt *integer = bigint_object(value);
928
+ bool negative = !integer->negative;
929
+ if (oseo_internal_bigint_is_zero(value)) negative = false;
930
+ return publish_bigint(
931
+ context,
932
+ negative,
933
+ integer->limbs,
934
+ integer->length
935
+ );
936
+ }
937
+
938
+ OseoResult oseo_internal_bigint_not(
939
+ OseoContext *context,
940
+ OseoValue value
941
+ ) {
942
+ OseoValue slots[2] = {value, oseo_undefined()};
943
+ OseoRootFrame frame = {NULL, slots, 2u};
944
+ oseo_roots_push(context, &frame);
945
+ OseoResult one = small_bigint(context, false, 1u);
946
+ if (one.status == OSEO_STATUS_NORMAL) {
947
+ slots[1] = one.value;
948
+ OseoResult negative = oseo_internal_bigint_negate(context, slots[0]);
949
+ if (negative.status == OSEO_STATUS_NORMAL) {
950
+ slots[0] = negative.value;
951
+ one = add_or_subtract(
952
+ context,
953
+ bigint_object(slots[0]),
954
+ bigint_object(slots[1]),
955
+ true
956
+ );
957
+ } else {
958
+ one = negative;
959
+ }
960
+ }
961
+ oseo_roots_pop(context, &frame);
962
+ return one;
963
+ }
964
+
965
+ OseoResult oseo_internal_bigint_binary(
966
+ OseoContext *context,
967
+ OseoValue left_value,
968
+ OseoValue right_value,
969
+ OseoBigIntOperator operator
970
+ ) {
971
+ OseoBigInt *left = bigint_object(left_value);
972
+ OseoBigInt *right = bigint_object(right_value);
973
+ if (operator == OSEO_BIGINT_ADD || operator == OSEO_BIGINT_SUBTRACT) {
974
+ return add_or_subtract(
975
+ context,
976
+ left,
977
+ right,
978
+ operator == OSEO_BIGINT_SUBTRACT
979
+ );
980
+ }
981
+ if (operator == OSEO_BIGINT_MULTIPLY) {
982
+ Magnitude product = multiply_magnitude(context, left, right);
983
+ return arithmetic_result(
984
+ context,
985
+ left->negative != right->negative,
986
+ product
987
+ );
988
+ }
989
+ if (operator == OSEO_BIGINT_DIVIDE ||
990
+ operator == OSEO_BIGINT_REMAINDER) {
991
+ if (oseo_internal_bigint_is_zero(right_value)) {
992
+ return oseo_internal_throw_error(
993
+ context,
994
+ OSEO_ERROR_RANGE,
995
+ "BigInt division by zero."
996
+ );
997
+ }
998
+ Magnitude quotient;
999
+ Magnitude remainder;
1000
+ if (!divide_magnitude(
1001
+ context,
1002
+ left,
1003
+ right,
1004
+ &quotient,
1005
+ &remainder
1006
+ )) {
1007
+ return allocation_failure(context);
1008
+ }
1009
+ bool return_quotient = operator == OSEO_BIGINT_DIVIDE;
1010
+ Magnitude selected = return_quotient ? quotient : remainder;
1011
+ free(return_quotient ? remainder.limbs : quotient.limbs);
1012
+ bool negative = return_quotient
1013
+ ? left->negative != right->negative
1014
+ : left->negative;
1015
+ return arithmetic_result(context, negative, selected);
1016
+ }
1017
+ if (operator == OSEO_BIGINT_EXPONENTIATE) {
1018
+ return exponentiate(context, left_value, right_value);
1019
+ }
1020
+ if (operator == OSEO_BIGINT_SHIFT_LEFT ||
1021
+ operator == OSEO_BIGINT_SHIFT_RIGHT) {
1022
+ return shift_result(
1023
+ context,
1024
+ left,
1025
+ right,
1026
+ operator == OSEO_BIGINT_SHIFT_LEFT
1027
+ );
1028
+ }
1029
+ bool negative = false;
1030
+ Magnitude result = bitwise_magnitude(
1031
+ context,
1032
+ left,
1033
+ right,
1034
+ operator,
1035
+ &negative
1036
+ );
1037
+ return arithmetic_result(context, negative, result);
1038
+ }
1039
+
1040
+ static bool bigint_whitespace(uint16_t unit) {
1041
+ return unit == UINT16_C(0x0009) || unit == UINT16_C(0x000a) ||
1042
+ unit == UINT16_C(0x000b) || unit == UINT16_C(0x000c) ||
1043
+ unit == UINT16_C(0x000d) || unit == UINT16_C(0x0020) ||
1044
+ unit == UINT16_C(0x00a0) || unit == UINT16_C(0x1680) ||
1045
+ (unit >= UINT16_C(0x2000) && unit <= UINT16_C(0x200a)) ||
1046
+ unit == UINT16_C(0x2028) || unit == UINT16_C(0x2029) ||
1047
+ unit == UINT16_C(0x202f) || unit == UINT16_C(0x205f) ||
1048
+ unit == UINT16_C(0x3000) || unit == UINT16_C(0xfeff);
1049
+ }
1050
+
1051
+ OseoResult oseo_internal_string_to_bigint(
1052
+ OseoContext *context,
1053
+ const OseoString *string,
1054
+ bool *valid
1055
+ ) {
1056
+ size_t start = 0u;
1057
+ size_t end = string->length;
1058
+ while (start < end && bigint_whitespace(string->units[start])) start += 1u;
1059
+ while (end > start && bigint_whitespace(string->units[end - 1u])) end -= 1u;
1060
+ if (start == end) {
1061
+ *valid = true;
1062
+ return small_bigint(context, false, 0u);
1063
+ }
1064
+ bool has_sign = false;
1065
+ bool negative = false;
1066
+ if (string->units[start] == '+' || string->units[start] == '-') {
1067
+ has_sign = true;
1068
+ negative = string->units[start] == '-';
1069
+ start += 1u;
1070
+ if (start == end) {
1071
+ *valid = false;
1072
+ return normal(oseo_undefined());
1073
+ }
1074
+ }
1075
+ uint32_t radix = 10u;
1076
+ if (end - start >= 2u && string->units[start] == '0') {
1077
+ uint16_t prefix = string->units[start + 1u];
1078
+ if (prefix == 'b' || prefix == 'B') radix = 2u;
1079
+ else if (prefix == 'o' || prefix == 'O') radix = 8u;
1080
+ else if (prefix == 'x' || prefix == 'X') radix = 16u;
1081
+ if (radix != 10u) {
1082
+ if (has_sign) {
1083
+ *valid = false;
1084
+ return normal(oseo_undefined());
1085
+ }
1086
+ start += 2u;
1087
+ }
1088
+ }
1089
+ if (start == end || end - start > (SIZE_MAX - 1u) / 4u) {
1090
+ *valid = false;
1091
+ return normal(oseo_undefined());
1092
+ }
1093
+ size_t count = end - start;
1094
+ char *digits = oseo_internal_allocate_heap_bytes(context, count + 1u);
1095
+ if (digits == NULL) return allocation_failure(context);
1096
+ for (size_t index = 0u; index < count; index += 1u) {
1097
+ uint16_t unit = string->units[start + index];
1098
+ bool decimal = unit >= '0' && unit <= '9';
1099
+ bool hexadecimal =
1100
+ (unit >= 'a' && unit <= 'f') ||
1101
+ (unit >= 'A' && unit <= 'F');
1102
+ uint32_t digit = decimal
1103
+ ? (uint32_t)(unit - '0')
1104
+ : hexadecimal
1105
+ ? (uint32_t)((unit | UINT16_C(0x20)) - 'a' + 10)
1106
+ : UINT32_MAX;
1107
+ if (unit > UINT16_C(0x7f) || digit >= radix) {
1108
+ free(digits);
1109
+ *valid = false;
1110
+ return normal(oseo_undefined());
1111
+ }
1112
+ digits[index] = (char)(hexadecimal
1113
+ ? unit | UINT16_C(0x20)
1114
+ : unit);
1115
+ }
1116
+ digits[count] = '\0';
1117
+ OseoResult result = oseo_bigint_literal(context, digits, radix);
1118
+ free(digits);
1119
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1120
+ if (negative && !oseo_internal_bigint_is_zero(result.value)) {
1121
+ OseoValue slot = result.value;
1122
+ OseoRootFrame frame = {NULL, &slot, 1u};
1123
+ oseo_roots_push(context, &frame);
1124
+ result = oseo_internal_bigint_negate(context, slot);
1125
+ oseo_roots_pop(context, &frame);
1126
+ }
1127
+ *valid = result.status == OSEO_STATUS_NORMAL;
1128
+ return result;
1129
+ }
1130
+
1131
+ /*
1132
+ * Lowercase digit characters for every radix ECMA-262 admits, so a
1133
+ * value of 10 through 35 spells a through z.
1134
+ */
1135
+ static const char bigint_digit_characters[] =
1136
+ "0123456789abcdefghijklmnopqrstuvwxyz";
1137
+
1138
+ static OseoResult bigint_string_rooted(
1139
+ OseoContext *context,
1140
+ OseoValue value,
1141
+ uint32_t radix
1142
+ ) {
1143
+ OseoBigInt *integer = bigint_object(value);
1144
+ bool negative = integer->negative;
1145
+ size_t working_length = integer->length;
1146
+ uint32_t *working = allocate_limbs(context, working_length, false);
1147
+ if (working == NULL) return allocation_failure(context);
1148
+ memcpy(working, integer->limbs, working_length * sizeof(uint32_t));
1149
+ /*
1150
+ * Every radix of at least two consumes one whole bit per digit, so
1151
+ * the magnitude's bit length bounds the digit count, and the two
1152
+ * extra bytes cover the zero digit and the sign.
1153
+ */
1154
+ size_t capacity = magnitude_bits(working, working_length) + 2u;
1155
+ char *reverse = oseo_internal_allocate_heap_bytes(context, capacity);
1156
+ if (reverse == NULL) {
1157
+ free(working);
1158
+ return allocation_failure(context);
1159
+ }
1160
+ size_t digits = 0u;
1161
+ do {
1162
+ uint64_t remainder = 0u;
1163
+ for (size_t cursor = working_length; cursor > 0u; cursor -= 1u) {
1164
+ uint64_t current =
1165
+ (remainder << BIGINT_BITS) | working[cursor - 1u];
1166
+ working[cursor - 1u] = (uint32_t)(current / radix);
1167
+ remainder = current % radix;
1168
+ }
1169
+ reverse[digits] = bigint_digit_characters[remainder];
1170
+ digits += 1u;
1171
+ working_length = normalized_length(working, working_length);
1172
+ } while (!(working_length == 1u && working[0] == 0u));
1173
+ free(working);
1174
+ if (negative) {
1175
+ reverse[digits] = '-';
1176
+ digits += 1u;
1177
+ }
1178
+ if (digits > SIZE_MAX / sizeof(uint16_t)) {
1179
+ free(reverse);
1180
+ return failure(context, "OSEO2001", "BigInt text is too large.");
1181
+ }
1182
+ uint16_t *units = oseo_internal_allocate_heap_bytes(
1183
+ context,
1184
+ digits * sizeof(uint16_t)
1185
+ );
1186
+ if (units == NULL) {
1187
+ free(reverse);
1188
+ return allocation_failure(context);
1189
+ }
1190
+ for (size_t index = 0u; index < digits; index += 1u) {
1191
+ units[index] =
1192
+ (uint16_t)(unsigned char)reverse[digits - index - 1u];
1193
+ }
1194
+ free(reverse);
1195
+ OseoResult result = oseo_internal_allocate_string(
1196
+ context,
1197
+ units,
1198
+ digits
1199
+ );
1200
+ free(units);
1201
+ return result;
1202
+ }
1203
+
1204
+ OseoResult oseo_internal_bigint_string(
1205
+ OseoContext *context,
1206
+ OseoValue value
1207
+ ) {
1208
+ return oseo_internal_bigint_radix_string(context, value, 10u);
1209
+ }
1210
+
1211
+ OseoResult oseo_internal_bigint_radix_string(
1212
+ OseoContext *context,
1213
+ OseoValue value,
1214
+ uint32_t radix
1215
+ ) {
1216
+ if (radix < 2u || radix > 36u) {
1217
+ return failure(context, "OSEO2001", "Invalid BigInt text radix.");
1218
+ }
1219
+ OseoValue slot = value;
1220
+ OseoRootFrame frame = {NULL, &slot, 1u};
1221
+ oseo_roots_push(context, &frame);
1222
+ OseoResult result = bigint_string_rooted(context, slot, radix);
1223
+ oseo_roots_pop(context, &frame);
1224
+ return result;
1225
+ }
1226
+
1227
+ OseoResult oseo_internal_bigint_from_integral_number(
1228
+ OseoContext *context,
1229
+ double number
1230
+ ) {
1231
+ if (number == 0.0) return small_bigint(context, false, 0u);
1232
+ bool negative = number < 0.0;
1233
+ double magnitude = negative ? -number : number;
1234
+ int exponent = 0;
1235
+ double fraction = frexp(magnitude, &exponent);
1236
+ /*
1237
+ * magnitude is fraction * 2**exponent with fraction in [0.5, 1), so
1238
+ * the scaled significand is an exact integer and magnitude is
1239
+ * significand * 2**(exponent - DBL_MANT_DIG). An integral magnitude
1240
+ * below 2**DBL_MANT_DIG leaves that shift negative with the dropped
1241
+ * low bits already zero, so the right shift stays exact.
1242
+ */
1243
+ uint64_t significand = (uint64_t)ldexp(fraction, DBL_MANT_DIG);
1244
+ int shift = exponent - DBL_MANT_DIG;
1245
+ if (shift < 0) {
1246
+ significand >>= (unsigned)(-shift);
1247
+ shift = 0;
1248
+ }
1249
+ size_t offset = (size_t)shift;
1250
+ size_t length = (size_t)exponent / BIGINT_BITS + 1u;
1251
+ uint32_t *limbs = allocate_limbs(context, length, true);
1252
+ if (limbs == NULL) return allocation_failure(context);
1253
+ for (unsigned index = 0u; index < 64u; index += 1u) {
1254
+ if (((significand >> index) & UINT64_C(1)) == 0u) continue;
1255
+ size_t bit = offset + index;
1256
+ limbs[bit / BIGINT_BITS] |=
1257
+ UINT32_C(1) << (bit % (size_t)BIGINT_BITS);
1258
+ }
1259
+ OseoResult result = publish_bigint(context, negative, limbs, length);
1260
+ free(limbs);
1261
+ return result;
1262
+ }
1263
+
1264
+ /*
1265
+ * Replace the width-bounded magnitude in `limbs` with 2**width minus
1266
+ * itself, which the two's complement produces in place. The caller
1267
+ * guarantees a nonzero input, so the increment never carries past the
1268
+ * width.
1269
+ */
1270
+ static void width_complement(
1271
+ uint32_t *limbs,
1272
+ size_t length,
1273
+ size_t top_limb,
1274
+ uint32_t top_mask
1275
+ ) {
1276
+ for (size_t index = 0u; index < length; index += 1u) {
1277
+ limbs[index] = (~limbs[index]) & BIGINT_MASK;
1278
+ }
1279
+ limbs[top_limb] &= top_mask;
1280
+ uint32_t carry = 1u;
1281
+ for (size_t index = 0u; index < length && carry != 0u; index += 1u) {
1282
+ uint32_t sum = limbs[index] + carry;
1283
+ limbs[index] = sum & BIGINT_MASK;
1284
+ carry = sum >> BIGINT_BITS;
1285
+ }
1286
+ }
1287
+
1288
+ static OseoResult bigint_as_width_rooted(
1289
+ OseoContext *context,
1290
+ OseoValue value,
1291
+ double bits,
1292
+ bool signed_result
1293
+ ) {
1294
+ const OseoBigInt *integer = bigint_object(value);
1295
+ bool negative = integer->negative;
1296
+ size_t source_bits = magnitude_bits(integer->limbs, integer->length);
1297
+ if (bits == 0.0) return small_bigint(context, false, 0u);
1298
+ if (bits > (double)source_bits) {
1299
+ /*
1300
+ * Every bit of the operand sits strictly below the sign bit of
1301
+ * the requested width, so the signed result is the operand and
1302
+ * the unsigned result is the operand whenever it is not
1303
+ * negative. Neither needs 2**bits to exist.
1304
+ */
1305
+ if (signed_result || !negative) return normal(value);
1306
+ if (bits > (double)BIGINT_MAX_BITS) return bigint_limit_error(context);
1307
+ }
1308
+ size_t width = (size_t)bits;
1309
+ size_t top_limb = width / (size_t)BIGINT_BITS;
1310
+ size_t top_bit = width % (size_t)BIGINT_BITS;
1311
+ uint32_t top_mask = top_bit == 0u
1312
+ ? 0u
1313
+ : (uint32_t)((UINT32_C(1) << top_bit) - 1u);
1314
+ size_t length = top_limb + 1u;
1315
+ uint32_t *limbs = allocate_limbs(context, length, true);
1316
+ if (limbs == NULL) return allocation_failure(context);
1317
+ for (size_t index = 0u; index < length; index += 1u) {
1318
+ limbs[index] = index < integer->length ? integer->limbs[index] : 0u;
1319
+ }
1320
+ limbs[top_limb] &= top_mask;
1321
+ bool low_zero = normalized_length(limbs, length) == 1u && limbs[0] == 0u;
1322
+ bool result_negative = false;
1323
+ /*
1324
+ * Both the unsigned wrap of a negative operand and the signed
1325
+ * reading of a set sign bit are 2**width minus the current value,
1326
+ * which the width-bounded two's complement produces without ever
1327
+ * materializing 2**width itself.
1328
+ */
1329
+ bool complement = negative && !low_zero;
1330
+ if (complement) width_complement(limbs, length, top_limb, top_mask);
1331
+ if (signed_result && magnitude_bit(limbs, length, width - 1u)) {
1332
+ width_complement(limbs, length, top_limb, top_mask);
1333
+ result_negative = true;
1334
+ }
1335
+ OseoResult result =
1336
+ publish_bigint(context, result_negative, limbs, length);
1337
+ free(limbs);
1338
+ return result;
1339
+ }
1340
+
1341
+ uint64_t oseo_internal_bigint_to_raw_uint64(OseoValue value) {
1342
+ const OseoBigInt *integer = bigint_object(value);
1343
+ /*
1344
+ * The limbs are little-endian and each holds BIGINT_BITS bits, so
1345
+ * three of them span the low 64 bits and every higher limb only
1346
+ * contributes multiples of 2**64. Unsigned overflow is the modulo
1347
+ * this conversion wants, so the discarded bits need no separate
1348
+ * reduction.
1349
+ */
1350
+ size_t limit = integer->length < 3u ? integer->length : 3u;
1351
+ uint64_t magnitude = 0u;
1352
+ for (size_t index = limit; index > 0u; index -= 1u) {
1353
+ magnitude = (magnitude << BIGINT_BITS) |
1354
+ (uint64_t)integer->limbs[index - 1u];
1355
+ }
1356
+ /* Two's complement over the same 64-bit width, which is the
1357
+ * specified ToBigUint64 of a negative operand. */
1358
+ return integer->negative ? UINT64_C(0) - magnitude : magnitude;
1359
+ }
1360
+
1361
+ OseoResult oseo_internal_bigint_from_uint64(
1362
+ OseoContext *context,
1363
+ uint64_t magnitude,
1364
+ bool negative
1365
+ ) {
1366
+ uint32_t limbs[3];
1367
+ for (size_t index = 0u; index < 3u; index += 1u) {
1368
+ limbs[index] = (uint32_t)(magnitude & BIGINT_MASK);
1369
+ magnitude >>= BIGINT_BITS;
1370
+ }
1371
+ return publish_bigint(context, negative, limbs, 3u);
1372
+ }
1373
+
1374
+ OseoResult oseo_internal_bigint_as_width(
1375
+ OseoContext *context,
1376
+ OseoValue value,
1377
+ double bits,
1378
+ bool signed_result
1379
+ ) {
1380
+ OseoValue slot = value;
1381
+ OseoRootFrame frame = {NULL, &slot, 1u};
1382
+ oseo_roots_push(context, &frame);
1383
+ OseoResult result =
1384
+ bigint_as_width_rooted(context, slot, bits, signed_result);
1385
+ oseo_roots_pop(context, &frame);
1386
+ return result;
1387
+ }