Ruby 4.1.0dev (2026-12-06 revision 6b501f5444cf675f4a654bda46c812e2c1df9fb8)
integer.c
1#include "prism/internal/integer.h"
2
5
6#include "prism/internal/allocator.h"
7#include "prism/internal/arena.h"
8#include "prism/internal/assert.h"
9#include "prism/internal/bignum.h"
10#include "prism/internal/bit.h"
11#include "prism/internal/buffer.h"
12#include "prism/internal/memchr.h"
13#include "prism/internal/oom.h"
14
15#include <stdbool.h>
16#include <stddef.h>
17#include <stdint.h>
18#include <stdlib.h>
19#include <string.h>
20
26#define PM_INTEGER_DIVIDE_THRESHOLD 224
27
35#define PM_INTEGER_STRING_THRESHOLD 48
36
43#define PM_INTEGER_STRING_LEAF 8
44
48static const int8_t pm_integer_parse_digit_values[256] = {
49/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
53 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 3x */
54 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 4x */
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 5x */
56 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 6x */
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 7x */
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 8x */
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 9x */
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Ax */
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Bx */
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Cx */
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Dx */
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Ex */
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Fx */
66};
67
71static uint8_t
72pm_integer_parse_digit(const uint8_t character) {
73 int8_t value = pm_integer_parse_digit_values[character];
74 PRISM_ASSERT(value != -1 && "invalid digit");
75
76 return (uint8_t) value;
77}
78
84static PRISM_INLINE bool
85pm_integer_separator(uint8_t character) {
86 return character == '_' || character == '.';
87}
88
94static void
95pm_integer_from_bignum(pm_arena_t *arena, pm_integer_t *integer, const pm_bignum_t *bignum, uint32_t capacity) {
96 if (bignum->length <= 2) {
97 uint64_t value = pm_bignum_uint64(bignum);
98 PM_ARENA_REALLOC(arena, bignum->words, 0, sizeof(uint32_t) * capacity, uint32_t);
99 *integer = pm_integer_uint64(value);
100 } else {
101 uint32_t *words = PM_ARENA_REALLOC(arena, bignum->words, sizeof(uint32_t) * bignum->length, sizeof(uint32_t) * capacity, uint32_t);
102 *integer = (pm_integer_t) { .length = bignum->length, .words = { .values = words } };
103 }
104}
105
112static void
113pm_integer_parse_powof2(pm_arena_t *arena, pm_integer_t *integer, uint32_t base, uint32_t digits, const uint8_t *start, const uint8_t *end) {
114 static const uint8_t digit_bits[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
115 uint32_t leading_bits = digit_bits[pm_integer_parse_digit(*start)];
116
117 uint32_t bit = (uint32_t) pm_ctzll(base);
118 PRISM_ASSERT(bit >= 1 && bit <= 4);
119
120 uint32_t length = (uint32_t) (((uint64_t) (digits - 1) * bit + leading_bits + 31) / 32);
121 PRISM_ASSERT(length > 2);
122
123 uint32_t *words = PM_ARENA_ZALLOC_ARRAY(arena, uint32_t, length);
124 uint64_t bit_position = 0;
125
126 for (const uint8_t *cursor = end; cursor > start;) {
127 if (pm_integer_separator(*--cursor)) continue;
128 uint32_t value = pm_integer_parse_digit(*cursor);
129
130 uint32_t index = (uint32_t) (bit_position / 32);
131 uint32_t shift = (uint32_t) (bit_position % 32);
132
133 words[index] |= value << shift;
134 if (32 - shift < bit && index + 1 < length) words[index + 1] |= value >> (32 - shift);
135
136 bit_position += bit;
137 }
138
139 *integer = (pm_integer_t) { .length = length, .words = { .values = words } };
140}
141
152static PRISM_INLINE uint32_t
153pm_integer_decimal_words(uint64_t digits) {
154 return (uint32_t) ((digits * 3402 / 1024) / 32 + 1);
155}
156
165static PRISM_INLINE uint32_t
166pm_integer_chunks_words(uint32_t nchunks) {
167 return pm_integer_decimal_words((uint64_t) nchunks * 9) + 1;
168}
169
175static void
176pm_integer_parse_decimal_divide_chunks(pm_bignum_t *result, uint32_t nchunks, const uint32_t *chunks, const pm_bignum_t *powers, pm_bignum_scratch_t *scratch) {
177 if (nchunks <= PM_INTEGER_DIVIDE_THRESHOLD) {
178 result->length = 0;
179 for (uint32_t index = nchunks; index > 0; index--) pm_bignum_mul_add(result, PM_BIGNUM_DECIMAL_BASE, chunks[index - 1]);
180 } else {
181 uint32_t level = pm_bsrll((uint64_t) nchunks - 1);
182 uint32_t split = (uint32_t) 1 << level;
183
184 uint32_t *saved = scratch->cursor;
185 pm_bignum_t high = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_chunks_words(nchunks - split)) };
186 pm_integer_parse_decimal_divide_chunks(&high, nchunks - split, chunks + split, powers, scratch);
187
188 PRISM_ASSERT(high.length + powers[level].length <= pm_integer_chunks_words(nchunks));
189 pm_bignum_multiply(result, &high, &powers[level], scratch);
190 scratch->cursor = saved;
191
192 pm_bignum_t low = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_chunks_words(split)) };
193 pm_integer_parse_decimal_divide_chunks(&low, split, chunks, powers, scratch);
194 pm_bignum_add(result, &low);
195 scratch->cursor = saved;
196 }
197}
198
209static void
210pm_integer_parse_decimal_divide(pm_arena_t *arena, pm_integer_t *integer, uint32_t digits, const uint8_t *start, const uint8_t *end) {
211 uint32_t nchunks = (uint32_t) (((uint64_t) digits + 8) / 9);
212 uint32_t capacity = pm_integer_chunks_words(nchunks);
213
214 pm_bignum_t result = {
215 .length = 0,
216 .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
217 };
218
219 /* The scratch space is about 12 times the number of chunks, which can
220 * exceed 32 bits, and on a 32-bit platform its size in bytes can exceed
221 * size_t. */
222 uint64_t scratch_size = (uint64_t) nchunks + 12 * (uint64_t) capacity + 1024;
223 if (scratch_size > SIZE_MAX / sizeof(uint32_t)) PRISM_OOM();
224
225 uint32_t *allocation = (uint32_t *) xmalloc(sizeof(uint32_t) * (size_t) scratch_size);
226 if (allocation == NULL) PRISM_OOM();
227 pm_bignum_scratch_t scratch = { .cursor = allocation, .end = allocation + (size_t) scratch_size };
228
229 /* Pack the digits into chunks of nine, least significant first. The most
230 * significant chunk takes the leftover digits so the rest are exactly
231 * nine. */
232 uint32_t *chunks = pm_bignum_scratch_take(&scratch, nchunks);
233 uint32_t chunk_size = digits % 9 == 0 ? 9 : digits % 9;
234 uint32_t chunk_digits = 0;
235 uint32_t chunk_index = nchunks;
236 uint32_t chunk = 0;
237
238 for (const uint8_t *cursor = start; cursor < end; cursor++) {
239 if (pm_integer_separator(*cursor)) continue;
240 chunk = chunk * 10 + pm_integer_parse_digit(*cursor);
241
242 if (++chunk_digits == chunk_size) {
243 chunks[--chunk_index] = chunk;
244 chunk_size = 9;
245 chunk_digits = 0;
246 chunk = 0;
247 }
248 }
249 PRISM_ASSERT(chunk_index == 0);
250
251 /* powers[level] is 10**(9 * 2**level), for every level a split uses. */
252 pm_bignum_t powers[64];
253 uint32_t level = 0;
254 powers[level].words = pm_bignum_scratch_take(&scratch, 1);
255 powers[level].words[0] = PM_BIGNUM_DECIMAL_BASE;
256 powers[level].length = 1;
257
258 for (uint32_t split = 2; split < nchunks; split *= 2) {
259 const pm_bignum_t *previous = &powers[level++];
260 powers[level].words = pm_bignum_scratch_take(&scratch, 2 * previous->length);
261 pm_bignum_multiply(&powers[level], previous, previous, &scratch);
262 }
263
264 pm_integer_parse_decimal_divide_chunks(&result, nchunks, chunks, powers, &scratch);
265 xfree_sized(allocation, sizeof(uint32_t) * (size_t) scratch_size);
266
267 PRISM_ASSERT(result.length > 2 && result.length <= capacity);
268 pm_integer_from_bignum(arena, integer, &result, capacity);
269}
270
278static void
279pm_integer_parse_decimal_schoolbook(pm_arena_t *arena, pm_integer_t *integer, uint32_t digits, const uint8_t *start, const uint8_t *end) {
280 static const uint32_t powof10[10] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
281
282 /* The words that turn out to be unneeded are given back to the arena
283 * below. */
284 uint32_t capacity = pm_integer_decimal_words(digits);
285 pm_bignum_t bignum = {
286 .length = 0,
287 .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
288 };
289
290 uint32_t chunk_size = digits % 9 == 0 ? 9 : digits % 9;
291 uint32_t chunk_digits = 0;
292 uint32_t chunk = 0;
293
294 for (const uint8_t *cursor = start; cursor < end; cursor++) {
295 if (pm_integer_separator(*cursor)) continue;
296 chunk = chunk * 10 + pm_integer_parse_digit(*cursor);
297
298 if (++chunk_digits == chunk_size) {
299 pm_bignum_mul_add(&bignum, powof10[chunk_size], chunk);
300
301 chunk_size = 9;
302 chunk_digits = 0;
303 chunk = 0;
304 }
305 }
306
307 PRISM_ASSERT(bignum.length > 2 && bignum.length <= capacity);
308 pm_integer_from_bignum(arena, integer, &bignum, capacity);
309}
310
314static void
315pm_integer_parse_digits(pm_arena_t *arena, pm_integer_t *integer, uint32_t base, const uint8_t *start, const uint8_t *end) {
316 /* Leading zeros do not change the value, so skip them here and the paths
317 * below always start at a nonzero digit. */
318 while (start < end && (*start == '0' || pm_integer_separator(*start))) start++;
319
320 /* Everything has been consumed if the integer is zero, or if it is invalid
321 * and has no digits. Either way the result is zero. */
322 if (start >= end) {
323 *integer = (pm_integer_t) { 0 };
324 return;
325 }
326
327 /* Accumulate into a uint64_t, which covers every integer whose words are
328 * stored inline. value * base + digit overflows exactly when value
329 * is past limit, or equal to it with digit past limit_digit. */
330 const uint64_t limit = UINT64_MAX / base;
331 const uint64_t limit_digit = UINT64_MAX % base;
332
333 const uint8_t *cursor = start;
334 uint64_t value = (uint64_t) pm_integer_parse_digit(*cursor++);
335
336 for (; cursor < end; cursor++) {
337 if (pm_integer_separator(*cursor)) continue;
338 uint64_t digit = (uint64_t) pm_integer_parse_digit(*cursor);
339
340 if (value > limit || (value == limit && digit > limit_digit)) {
341 uint32_t digits = 0;
342 for (const uint8_t *scan = start; scan < end; scan++) {
343 if (!pm_integer_separator(*scan)) digits++;
344 }
345
346 if (base != 10) {
347 pm_integer_parse_powof2(arena, integer, base, digits, start, end);
348 } else if (((uint64_t) digits + 8) / 9 > PM_INTEGER_DIVIDE_THRESHOLD) {
349 pm_integer_parse_decimal_divide(arena, integer, digits, start, end);
350 } else {
351 pm_integer_parse_decimal_schoolbook(arena, integer, digits, start, end);
352 }
353
354 return;
355 }
356
357 value = value * base + digit;
358 }
359
360 *integer = pm_integer_uint64(value);
361}
362
368void
369pm_integer_parse(pm_arena_t *arena, pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end) {
370 /* Ignore unary +. Unary - is parsed differently and will not end up here.
371 * Instead, it will modify the parsed integer later. */
372 if (*start == '+') start++;
373
374 /* Skip past any prefixes. */
375 switch (base) {
376 case PM_INTEGER_BASE_BINARY:
377 start += 2; /* 0b */
378 break;
379 case PM_INTEGER_BASE_OCTAL:
380 start++; /* 0 */
381 if (*start == '_' || *start == 'o' || *start == 'O') start++; /* o */
382 break;
383 case PM_INTEGER_BASE_DECIMAL:
384 if (*start == '0' && (end - start) > 1) start += 2; /* 0d */
385 break;
386 case PM_INTEGER_BASE_HEXADECIMAL:
387 start += 2; /* 0x */
388 break;
389 }
390
391 pm_integer_parse_digits(arena, integer, (uint32_t) base, start, end);
392}
393
403void
404pm_integer_parse_rational(pm_arena_t *arena, pm_integer_t *numerator, pm_integer_t *denominator, const uint8_t *start, const uint8_t *end) {
405 if (*start == '+') start++;
406
407 const uint8_t *point = pm_memchr(start, '.', (size_t) (end - start));
408 PRISM_ASSERT(point != NULL);
409
410 /* Each trailing zero after the decimal point divides both sides by 10, so
411 * drop them before parsing. */
412 while (end > point + 1 && (end[-1] == '0' || end[-1] == '_')) end--;
413
414 uint32_t exponent = 0;
415 for (const uint8_t *cursor = point + 1; cursor < end; cursor++) {
416 if (*cursor != '_') exponent++;
417 }
418
419 pm_integer_parse_digits(arena, numerator, 10, start, end);
420 uint32_t twos = 0;
421 uint32_t fives = 0;
422
423 if (numerator->length > 0 && exponent > 0) {
424 uint32_t length = numerator->length;
425 pm_bignum_t bignum = {
426 .length = length,
427 .words = length <= 2 ? numerator->words.value : numerator->words.values
428 };
429
430 uint64_t trailing = pm_bignum_trailing_zeros(&bignum);
431 twos = trailing > exponent ? exponent : (uint32_t) trailing;
432 pm_bignum_shift_right(&bignum, twos);
433
434 /* 5**13 is the largest power of five that fits in a word, so divide by
435 * it while it divides evenly before dividing by five alone. */
436 while (exponent - fives >= 13 && pm_bignum_div_exact(&bignum, PM_BIGNUM_POWER5_13)) fives += 13;
437 while (fives < exponent && pm_bignum_div_exact(&bignum, 5)) fives++;
438
439 if (length <= 2) {
440 *numerator = pm_integer_uint64(pm_bignum_uint64(&bignum));
441 } else {
442 pm_integer_from_bignum(arena, numerator, &bignum, length);
443 }
444 }
445
446 /* The denominator is what is left of 10**exponent. 2378/1024 is just above
447 * log2(5), so this capacity never undercounts. */
448 uint32_t remaining_fives = exponent - fives;
449 uint32_t remaining_twos = exponent - twos;
450 uint32_t capacity = (uint32_t) (((uint64_t) remaining_fives * 2378 / 1024 + remaining_twos + 32) / 32);
451
452 pm_bignum_t bignum = {
453 .length = 1,
454 .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
455 };
456 bignum.words[0] = 1;
457
458 pm_bignum_multiply_powof5(&bignum, remaining_fives);
459 pm_bignum_shift_left(&bignum, remaining_twos);
460
461 PRISM_ASSERT(bignum.length <= capacity);
462 pm_integer_from_bignum(arena, denominator, &bignum, capacity);
463}
464
469static pm_bignum_t
470pm_integer_bignum(const pm_integer_t *integer) {
471 uint32_t length;
472 const uint32_t *words = pm_integer_words(integer, &length);
473 return (pm_bignum_t) { .length = length, .words = (uint32_t *) words };
474}
475
481int
482pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right) {
483 if (left->negative != right->negative) return left->negative ? -1 : 1;
484 int negative = left->negative ? -1 : 1;
485
486 pm_bignum_t left_bignum = pm_integer_bignum(left);
487 pm_bignum_t right_bignum = pm_integer_bignum(right);
488 return pm_bignum_compare(&left_bignum, &right_bignum) * negative;
489}
490
504static PRISM_INLINE uint32_t
505pm_integer_string_words(uint32_t length) {
506 return (uint32_t) ((uint64_t) length * 1097 / 1024 + 2);
507}
508
519static void
520pm_integer_string_convert(pm_bignum_t *result, const uint32_t *words, uint32_t length, const pm_bignum_t *powers, pm_bignum_scratch_t *scratch) {
521 /* The low half of a split can have leading zero words. */
522 while (length > 0 && words[length - 1] == 0) length--;
523 uint32_t *saved = scratch->cursor;
524
525 if (length <= PM_INTEGER_STRING_LEAF) {
526 pm_bignum_t bignum = { .length = length, .words = pm_bignum_scratch_take(scratch, length) };
527 memcpy(bignum.words, words, sizeof(uint32_t) * length);
528
529 result->length = 0;
530 while (bignum.length > 0) result->words[result->length++] = pm_bignum_div(&bignum, PM_BIGNUM_DECIMAL_BASE);
531 } else {
532 uint32_t level = pm_bsrll((uint64_t) length - 1);
533 uint32_t split = (uint32_t) 1 << level;
534
535 pm_bignum_t high = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_string_words(length - split)) };
536 pm_integer_string_convert(&high, words + split, length - split, powers, scratch);
537
538 PRISM_ASSERT(high.length + powers[level].length <= pm_integer_string_words(length));
539 pm_bignum_decimal_multiply(result, &high, &powers[level], scratch);
540 scratch->cursor = saved;
541
542 pm_bignum_t low = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_string_words(split)) };
543 pm_integer_string_convert(&low, words, split, powers, scratch);
544 pm_bignum_decimal_add(result, &low);
545 }
546
547 scratch->cursor = saved;
548}
549
560static void
561pm_integer_string_divide(pm_buffer_t *buffer, const uint32_t *words, uint32_t length) {
562 uint32_t capacity = pm_integer_string_words(length);
563
564 /* The scratch space is about 13 times the number of words, which can
565 * exceed 32 bits, and on a 32-bit platform its size in bytes can exceed
566 * size_t. */
567 uint64_t scratch_size = 12 * (uint64_t) capacity + 1024;
568 if (scratch_size > SIZE_MAX / sizeof(uint32_t)) PRISM_OOM();
569
570 uint32_t *allocation = (uint32_t *) xmalloc(sizeof(uint32_t) * (size_t) scratch_size);
571 if (allocation == NULL) PRISM_OOM();
572 pm_bignum_scratch_t scratch = { .cursor = allocation, .end = allocation + (size_t) scratch_size };
573
574 pm_bignum_t result = { .length = 0, .words = pm_bignum_scratch_take(&scratch, capacity) };
575
576 /* powers[level] is 2**(32 * 2**level) in base 10**9, for every level a
577 * split uses. */
578 pm_bignum_t powers[64];
579 uint32_t level = 0;
580 powers[level].words = pm_bignum_scratch_take(&scratch, 2);
581 powers[level].words[0] = (uint32_t) (PM_BIGNUM_BINARY_BASE % PM_BIGNUM_DECIMAL_BASE);
582 powers[level].words[1] = (uint32_t) (PM_BIGNUM_BINARY_BASE / PM_BIGNUM_DECIMAL_BASE);
583 powers[level].length = 2;
584
585 for (uint32_t split = 2; split < length; split *= 2) {
586 const pm_bignum_t *previous = &powers[level++];
587 powers[level] = (pm_bignum_t) { .length = 0, .words = pm_bignum_scratch_take(&scratch, 2 * previous->length) };
588 pm_bignum_decimal_multiply(&powers[level], previous, previous, &scratch);
589 }
590
591 pm_integer_string_convert(&result, words, length, powers, &scratch);
592 PRISM_ASSERT(result.length > 0 && result.length < capacity);
593
594 /* Every word but the most significant is exactly nine digits, padded with
595 * zeros. */
596 pm_buffer_append_decimal(buffer, result.words[result.length - 1]);
597
598 for (uint32_t index = result.length - 1; index > 0; index--) {
599 uint32_t word = result.words[index - 1];
600 char digits[9];
601
602 for (size_t digit = 9; digit > 0; digit--) {
603 digits[digit - 1] = (char) ('0' + word % 10);
604 word /= 10;
605 }
606
607 pm_buffer_append_string(buffer, digits, 9);
608 }
609
610 xfree_sized(allocation, sizeof(uint32_t) * (size_t) scratch_size);
611}
612
616void
617pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer) {
618 if (integer->negative) {
619 pm_buffer_append_byte(buffer, '-');
620 }
621
622 /* An integer whose words are inline fits in a uint64_t, so it can be
623 * appended directly. */
624 if (integer->length <= 2) {
625 pm_buffer_append_decimal(buffer, pm_integer_uint64_value(integer));
626 return;
627 }
628
629 if (integer->length > PM_INTEGER_STRING_THRESHOLD) {
630 pm_integer_string_divide(buffer, integer->words.values, integer->length);
631 return;
632 }
633
634 /* Dividing by 10**9 yields the next nine digits from the remainder, least
635 * significant first, so the digits are written from the end of the buffer
636 * toward the front. The division is destructive, so it works on a copy. */
637 const size_t words_size = sizeof(uint32_t) * integer->length;
638
639 /* A number of n words has at most n * 32 * log10(2) + 1 decimal digits,
640 * which is at most n * 10 for the three or more words that reach here. */
641 const size_t capacity = (size_t) integer->length * 10;
642
643 /* The copy of the words and the digits share one allocation, with the
644 * words first so that they are aligned. */
645 void *allocation = xmalloc(words_size + capacity);
646 if (allocation == NULL) PRISM_OOM();
647
648 pm_bignum_t bignum = { .length = integer->length, .words = (uint32_t *) allocation };
649 memcpy(bignum.words, integer->words.values, words_size);
650
651 char *digits = (char *) allocation + words_size;
652 size_t offset = capacity;
653
654 while (bignum.length > 0) {
655 uint32_t remainder = pm_bignum_div(&bignum, PM_BIGNUM_DECIMAL_BASE);
656
657 /* Every chunk but the most significant is exactly nine digits, padded
658 * with zeros. The most significant chunk is nonzero and stops at its
659 * leading digit. */
660 for (size_t index = 0; index < 9 && (bignum.length > 0 || remainder > 0); index++) {
661 digits[--offset] = (char) ('0' + remainder % 10);
662 remainder /= 10;
663 }
664 }
665
666 pm_buffer_append_string(buffer, digits + offset, capacity - offset);
667 xfree_sized(allocation, words_size + capacity);
668}
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition inline.h:12
static PRISM_INLINE uint64_t pm_integer_uint64_value(const pm_integer_t *integer)
Return the magnitude of an integer whose length is at most 2, which always fits in a uint64_t.
Definition integer.h:75
static PRISM_INLINE const uint32_t * pm_integer_words(const pm_integer_t *integer, uint32_t *length)
Return the words of an integer, least significant first.
Definition integer.h:57
C99 shim for <stdbool.h>.
A structure represents an arbitrary-sized integer.
Definition integer.h:19
uint32_t length
The number of words in the magnitude, with no leading zero words.
Definition integer.h:24
uint32_t * values
The words of an integer whose length is greater than 2.
Definition integer.h:45
bool negative
Whether or not the integer is negative.
Definition integer.h:29
uint32_t value[2]
The words of an integer whose length is at most 2.
Definition integer.h:40
union pm_integer_t::@344106104334032307171003340315163164214337104357 words
The words of the magnitude.