1#include "prism/internal/integer.h"
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"
26#define PM_INTEGER_DIVIDE_THRESHOLD 224
35#define PM_INTEGER_STRING_THRESHOLD 48
43#define PM_INTEGER_STRING_LEAF 8
48static const int8_t pm_integer_parse_digit_values[256] = {
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
54 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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");
76 return (uint8_t) value;
85pm_integer_separator(uint8_t character) {
86 return character ==
'_' || character ==
'.';
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);
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 } };
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)];
117 uint32_t bit = (uint32_t) pm_ctzll(base);
118 PRISM_ASSERT(bit >= 1 && bit <= 4);
120 uint32_t length = (uint32_t) (((uint64_t) (digits - 1) * bit + leading_bits + 31) / 32);
121 PRISM_ASSERT(length > 2);
123 uint32_t *words = PM_ARENA_ZALLOC_ARRAY(arena, uint32_t, length);
124 uint64_t bit_position = 0;
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);
130 uint32_t index = (uint32_t) (bit_position / 32);
131 uint32_t shift = (uint32_t) (bit_position % 32);
133 words[index] |= value << shift;
134 if (32 - shift < bit && index + 1 < length) words[index + 1] |= value >> (32 - shift);
139 *integer = (
pm_integer_t) { .length = length, .words = { .values = words } };
153pm_integer_decimal_words(uint64_t digits) {
154 return (uint32_t) ((digits * 3402 / 1024) / 32 + 1);
166pm_integer_chunks_words(uint32_t nchunks) {
167 return pm_integer_decimal_words((uint64_t) nchunks * 9) + 1;
177 if (nchunks <= PM_INTEGER_DIVIDE_THRESHOLD) {
179 for (uint32_t index = nchunks; index > 0; index--) pm_bignum_mul_add(result, PM_BIGNUM_DECIMAL_BASE, chunks[index - 1]);
181 uint32_t level = pm_bsrll((uint64_t) nchunks - 1);
182 uint32_t split = (uint32_t) 1 << level;
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);
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;
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;
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);
216 .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
222 uint64_t scratch_size = (uint64_t) nchunks + 12 * (uint64_t) capacity + 1024;
223 if (scratch_size > SIZE_MAX /
sizeof(uint32_t)) PRISM_OOM();
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 };
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;
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);
242 if (++chunk_digits == chunk_size) {
243 chunks[--chunk_index] = chunk;
249 PRISM_ASSERT(chunk_index == 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;
258 for (uint32_t split = 2; split < nchunks; split *= 2) {
260 powers[level].words = pm_bignum_scratch_take(&scratch, 2 * previous->length);
261 pm_bignum_multiply(&powers[level], previous, previous, &scratch);
264 pm_integer_parse_decimal_divide_chunks(&result, nchunks, chunks, powers, &scratch);
265 xfree_sized(allocation,
sizeof(uint32_t) * (
size_t) scratch_size);
267 PRISM_ASSERT(result.length > 2 && result.length <= capacity);
268 pm_integer_from_bignum(arena, integer, &result, capacity);
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 };
284 uint32_t capacity = pm_integer_decimal_words(digits);
287 .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
290 uint32_t chunk_size = digits % 9 == 0 ? 9 : digits % 9;
291 uint32_t chunk_digits = 0;
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);
298 if (++chunk_digits == chunk_size) {
299 pm_bignum_mul_add(&bignum, powof10[chunk_size], chunk);
307 PRISM_ASSERT(bignum.length > 2 && bignum.length <= capacity);
308 pm_integer_from_bignum(arena, integer, &bignum, capacity);
315pm_integer_parse_digits(
pm_arena_t *arena,
pm_integer_t *integer, uint32_t base,
const uint8_t *start,
const uint8_t *end) {
318 while (start < end && (*start ==
'0' || pm_integer_separator(*start))) start++;
330 const uint64_t limit = UINT64_MAX / base;
331 const uint64_t limit_digit = UINT64_MAX % base;
333 const uint8_t *cursor = start;
334 uint64_t value = (uint64_t) pm_integer_parse_digit(*cursor++);
336 for (; cursor < end; cursor++) {
337 if (pm_integer_separator(*cursor))
continue;
338 uint64_t digit = (uint64_t) pm_integer_parse_digit(*cursor);
340 if (value > limit || (value == limit && digit > limit_digit)) {
342 for (
const uint8_t *scan = start; scan < end; scan++) {
343 if (!pm_integer_separator(*scan)) digits++;
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);
351 pm_integer_parse_decimal_schoolbook(arena, integer, digits, start, end);
357 value = value * base + digit;
360 *integer = pm_integer_uint64(value);
369pm_integer_parse(
pm_arena_t *arena,
pm_integer_t *integer, pm_integer_base_t base,
const uint8_t *start,
const uint8_t *end) {
372 if (*start ==
'+') start++;
376 case PM_INTEGER_BASE_BINARY:
379 case PM_INTEGER_BASE_OCTAL:
381 if (*start ==
'_' || *start ==
'o' || *start ==
'O') start++;
383 case PM_INTEGER_BASE_DECIMAL:
384 if (*start ==
'0' && (end - start) > 1) start += 2;
386 case PM_INTEGER_BASE_HEXADECIMAL:
391 pm_integer_parse_digits(arena, integer, (uint32_t) base, start, end);
405 if (*start ==
'+') start++;
407 const uint8_t *point = pm_memchr(start,
'.', (
size_t) (end - start));
408 PRISM_ASSERT(point != NULL);
412 while (end > point + 1 && (end[-1] ==
'0' || end[-1] ==
'_')) end--;
414 uint32_t exponent = 0;
415 for (
const uint8_t *cursor = point + 1; cursor < end; cursor++) {
416 if (*cursor !=
'_') exponent++;
419 pm_integer_parse_digits(arena, numerator, 10, start, end);
423 if (numerator->
length > 0 && exponent > 0) {
424 uint32_t length = numerator->
length;
430 uint64_t trailing = pm_bignum_trailing_zeros(&bignum);
431 twos = trailing > exponent ? exponent : (uint32_t) trailing;
432 pm_bignum_shift_right(&bignum, twos);
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++;
440 *numerator = pm_integer_uint64(pm_bignum_uint64(&bignum));
442 pm_integer_from_bignum(arena, numerator, &bignum, length);
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);
454 .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
458 pm_bignum_multiply_powof5(&bignum, remaining_fives);
459 pm_bignum_shift_left(&bignum, remaining_twos);
461 PRISM_ASSERT(bignum.length <= capacity);
462 pm_integer_from_bignum(arena, denominator, &bignum, capacity);
473 return (
pm_bignum_t) { .length = length, .words = (uint32_t *) words };
484 int negative = left->
negative ? -1 : 1;
487 pm_bignum_t right_bignum = pm_integer_bignum(right);
488 return pm_bignum_compare(&left_bignum, &right_bignum) * negative;
505pm_integer_string_words(uint32_t length) {
506 return (uint32_t) ((uint64_t) length * 1097 / 1024 + 2);
522 while (length > 0 && words[length - 1] == 0) length--;
523 uint32_t *saved = scratch->cursor;
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);
530 while (bignum.length > 0) result->words[result->length++] = pm_bignum_div(&bignum, PM_BIGNUM_DECIMAL_BASE);
532 uint32_t level = pm_bsrll((uint64_t) length - 1);
533 uint32_t split = (uint32_t) 1 << level;
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);
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;
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);
547 scratch->cursor = saved;
561pm_integer_string_divide(
pm_buffer_t *buffer,
const uint32_t *words, uint32_t length) {
562 uint32_t capacity = pm_integer_string_words(length);
567 uint64_t scratch_size = 12 * (uint64_t) capacity + 1024;
568 if (scratch_size > SIZE_MAX /
sizeof(uint32_t)) PRISM_OOM();
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 };
574 pm_bignum_t result = { .length = 0, .words = pm_bignum_scratch_take(&scratch, capacity) };
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;
585 for (uint32_t split = 2; split < length; split *= 2) {
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);
591 pm_integer_string_convert(&result, words, length, powers, &scratch);
592 PRISM_ASSERT(result.length > 0 && result.length < capacity);
596 pm_buffer_append_decimal(buffer, result.words[result.length - 1]);
598 for (uint32_t index = result.length - 1; index > 0; index--) {
599 uint32_t word = result.words[index - 1];
602 for (
size_t digit = 9; digit > 0; digit--) {
603 digits[digit - 1] = (char) (
'0' + word % 10);
607 pm_buffer_append_string(buffer, digits, 9);
610 xfree_sized(allocation,
sizeof(uint32_t) * (
size_t) scratch_size);
619 pm_buffer_append_byte(buffer,
'-');
624 if (integer->
length <= 2) {
629 if (integer->
length > PM_INTEGER_STRING_THRESHOLD) {
637 const size_t words_size =
sizeof(uint32_t) * integer->
length;
641 const size_t capacity = (size_t) integer->
length * 10;
645 void *allocation =
xmalloc(words_size + capacity);
646 if (allocation == NULL) PRISM_OOM();
648 pm_bignum_t bignum = { .length = integer->
length, .words = (uint32_t *) allocation };
649 memcpy(bignum.words, integer->
words.
values, words_size);
651 char *digits = (
char *) allocation + words_size;
652 size_t offset = capacity;
654 while (bignum.length > 0) {
655 uint32_t remainder = pm_bignum_div(&bignum, PM_BIGNUM_DECIMAL_BASE);
660 for (
size_t index = 0; index < 9 && (bignum.length > 0 || remainder > 0); index++) {
661 digits[--offset] = (char) (
'0' + remainder % 10);
666 pm_buffer_append_string(buffer, digits + offset, capacity - offset);
667 xfree_sized(allocation, words_size + capacity);
#define xmalloc
Old name of ruby_xmalloc.
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
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.
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.
C99 shim for <stdbool.h>.
A structure represents an arbitrary-sized integer.
uint32_t length
The number of words in the magnitude, with no leading zero words.
uint32_t * values
The words of an integer whose length is greater than 2.
bool negative
Whether or not the integer is negative.
uint32_t value[2]
The words of an integer whose length is at most 2.
union pm_integer_t::@344106104334032307171003340315163164214337104357 words
The words of the magnitude.