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