Ruby 4.1.0dev (2026-11-12 revision acc624e47b178e3514d848a949f8e25d4e49e175)
bignum.h
1/*
2 * Arithmetic on arbitrary-precision unsigned integers stored as arrays of
3 * 32-bit words.
4 */
5#ifndef PRISM_INTERNAL_BIGNUM_H
6#define PRISM_INTERNAL_BIGNUM_H
7
9
10#include "prism/internal/assert.h"
11#include "prism/internal/bit.h"
12
13#include <stdbool.h>
14#include <stddef.h>
15#include <stdint.h>
16#include <string.h>
17
18/*
19 * The base of the words of a number.
20 */
21#define PM_BIGNUM_BINARY_BASE ((uint64_t) 1 << 32)
22
23/*
24 * The base of the words of a decimal number, each of which is nine digits.
25 */
26#define PM_BIGNUM_DECIMAL_BASE 1000000000
27
28/*
29 * An arbitrary-precision unsigned integer.
30 */
31typedef struct {
32 /* The number of words in use. */
33 uint32_t length;
34
35 /* The words, least significant first. */
36 uint32_t *words;
38
39/*
40 * Multiply the number by multiplier and add addend, in place. A word times a
41 * word plus a word is at most 2**64 - 2**32, so each intermediate product fits
42 * in a uint64_t. A nonzero carry out of the top word becomes a new top word, so
43 * the storage must have room for it.
44 */
45static PRISM_INLINE void
46pm_bignum_mul_add(pm_bignum_t *bignum, uint32_t multiplier, uint32_t addend) {
47 PRISM_ASSERT(multiplier != 0);
48 uint64_t carry = addend;
49
50 for (uint32_t index = 0; index < bignum->length; index++) {
51 uint64_t product = (uint64_t) bignum->words[index] * multiplier + carry;
52 bignum->words[index] = (uint32_t) product;
53 carry = product >> 32;
54 }
55
56 if (carry != 0) bignum->words[bignum->length++] = (uint32_t) carry;
57}
58
59/*
60 * Divide the number by divisor, in place, working down from the top word.
61 * Returns the remainder. The remainder is always below the divisor, so shifting
62 * it above the next word fits in a uint64_t, and since the divisor is a single
63 * word the quotient is at most one word shorter. When inlined with a constant
64 * divisor, the compiler replaces the division with a multiplication.
65 */
66static PRISM_INLINE uint32_t
67pm_bignum_div(pm_bignum_t *bignum, uint32_t divisor) {
68 PRISM_ASSERT(divisor != 0);
69 uint64_t remainder = 0;
70
71 for (uint32_t index = bignum->length; index > 0; index--) {
72 uint64_t current = (remainder << 32) | bignum->words[index - 1];
73 bignum->words[index - 1] = (uint32_t) (current / divisor);
74 remainder = current % divisor;
75 }
76
77 if (bignum->length > 0 && bignum->words[bignum->length - 1] == 0) bignum->length--;
78 return (uint32_t) remainder;
79}
80
81/*
82 * Divide the number by divisor if it divides evenly, and return whether it did.
83 * Otherwise the number is left unchanged: multiplying the quotient back and
84 * adding the remainder restores it exactly.
85 */
86static PRISM_INLINE bool
87pm_bignum_div_exact(pm_bignum_t *bignum, uint32_t divisor) {
88 uint32_t remainder = pm_bignum_div(bignum, divisor);
89 if (remainder == 0) return true;
90
91 pm_bignum_mul_add(bignum, divisor, remainder);
92 return false;
93}
94
95/*
96 * Shift the number left by count bits, in place. The words grow by count / 32
97 * plus possibly one more, so the storage must have room for them.
98 */
99static PRISM_INLINE void
100pm_bignum_shift_left(pm_bignum_t *bignum, size_t count) {
101 if (bignum->length == 0) return;
102
103 uint32_t *words = bignum->words;
104 uint32_t length = bignum->length;
105 uint32_t word_shift = (uint32_t) (count / 32);
106 uint32_t bit_shift = (uint32_t) (count % 32);
107
108 /* Work down from the top word, so each word is read before the word that
109 * replaces it is written. */
110 uint32_t carry = bit_shift == 0 ? 0 : words[length - 1] >> (32 - bit_shift);
111 for (uint32_t index = length; index > 0; index--) {
112 uint32_t below = (bit_shift == 0 || index == 1) ? 0 : words[index - 2] >> (32 - bit_shift);
113 words[index - 1 + word_shift] = (words[index - 1] << bit_shift) | below;
114 }
115
116 memset(words, 0, sizeof(uint32_t) * word_shift);
117 bignum->length = length + word_shift;
118 if (carry != 0) words[bignum->length++] = carry;
119}
120
121/*
122 * Shift the number right by count bits, in place, dropping the bits that are
123 * shifted out.
124 */
125static PRISM_INLINE void
126pm_bignum_shift_right(pm_bignum_t *bignum, size_t count) {
127 uint32_t *words = bignum->words;
128 uint32_t word_shift = (uint32_t) (count / 32);
129 uint32_t bit_shift = (uint32_t) (count % 32);
130
131 if (word_shift >= bignum->length) {
132 bignum->length = 0;
133 return;
134 }
135
136 /* Work up from the bottom word, so each word is read before the word that
137 * replaces it is written. */
138 uint32_t length = bignum->length - word_shift;
139 for (uint32_t index = 0; index < length; index++) {
140 uint32_t above = (bit_shift == 0 || index + 1 == length) ? 0 : words[index + word_shift + 1] << (32 - bit_shift);
141 words[index] = (words[index + word_shift] >> bit_shift) | above;
142 }
143
144 bignum->length = length;
145 if (words[length - 1] == 0) bignum->length--;
146}
147
148/*
149 * Return the number of trailing zero bits in a nonzero number. Its top word is
150 * nonzero, so the scan for the first nonzero word stops within its length.
151 */
152static PRISM_INLINE uint64_t
153pm_bignum_trailing_zeros(const pm_bignum_t *bignum) {
154 PRISM_ASSERT(bignum->length > 0 && bignum->words[bignum->length - 1] != 0);
155
156 uint32_t index = 0;
157 while (bignum->words[index] == 0) index++;
158 return (uint64_t) index * 32 + pm_ctzll(bignum->words[index]);
159}
160
161/*
162 * Compare two numbers. Returns -1 if left is less than right, 0 if they are
163 * equal, and 1 if left is greater than right.
164 */
165static PRISM_INLINE int
166pm_bignum_compare(const pm_bignum_t *left, const pm_bignum_t *right) {
167 if (left->length != right->length) return left->length < right->length ? -1 : 1;
168
169 for (uint32_t index = left->length; index > 0; index--) {
170 uint32_t left_word = left->words[index - 1];
171 uint32_t right_word = right->words[index - 1];
172 if (left_word != right_word) return left_word < right_word ? -1 : 1;
173 }
174
175 return 0;
176}
177
178/*
179 * Return the value of a number of at most two words. Words past the length may
180 * hold stale values after an in-place operation, so only the words in use are
181 * read.
182 */
183static PRISM_INLINE uint64_t
184pm_bignum_uint64(const pm_bignum_t *bignum) {
185 PRISM_ASSERT(bignum->length <= 2);
186 return (bignum->length > 0 ? (uint64_t) bignum->words[0] : 0) | (bignum->length > 1 ? (uint64_t) bignum->words[1] << 32 : 0);
187}
188
189/*
190 * Temporary words for the operations that need them, taken and given back in
191 * stack order: an operation saves the cursor, takes what it needs, and
192 * restores the cursor before it returns.
193 */
194typedef struct {
195 /* The next free word. */
196 uint32_t *cursor;
197
198 /* One past the last word. */
199 uint32_t *end;
201
202/*
203 * Take count words from the scratch space.
204 */
205static PRISM_INLINE uint32_t *
206pm_bignum_scratch_take(pm_bignum_scratch_t *scratch, uint32_t count) {
207 PRISM_ASSERT((size_t) (scratch->end - scratch->cursor) >= count);
208 uint32_t *words = scratch->cursor;
209 scratch->cursor += count;
210 return words;
211}
212
213/*
214 * Add right into left, in place. The storage must have room for the sum, which
215 * is at most one word longer than the longer of the two.
216 */
217void pm_bignum_add(pm_bignum_t *left, const pm_bignum_t *right);
218
219/*
220 * Multiply left by right into result, whose storage has room for
221 * left->length + right->length words and overlaps neither. Long numbers use
222 * Karatsuba's method, which takes its temporaries from scratch.
223 */
224void pm_bignum_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch);
225
226/*
227 * Add right into left, in place, in base 10**9. The storage must have room for
228 * the sum, which is at most one word longer than the longer of the two.
229 */
230void pm_bignum_decimal_add(pm_bignum_t *left, const pm_bignum_t *right);
231
232/*
233 * Multiply left by right into result, in base 10**9, whose storage has room
234 * for left->length + right->length words and overlaps neither. Long numbers
235 * use Karatsuba's method, which takes its temporaries from scratch.
236 */
237void pm_bignum_decimal_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch);
238
239#endif
#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
C99 shim for <stdbool.h>