Line | Count | Source |
1 | | #include "prism/internal/bignum.h" |
2 | | |
3 | | #include "prism/compiler/force_inline.h" |
4 | | #include "prism/internal/assert.h" |
5 | | |
6 | | #include <stddef.h> |
7 | | #include <stdint.h> |
8 | | #include <string.h> |
9 | | |
10 | | /** |
11 | | * Multiplications where the shorter number has fewer words than this use the |
12 | | * schoolbook method, which is faster than Karatsuba's at small sizes. |
13 | | */ |
14 | | #define PM_BIGNUM_KARATSUBA_THRESHOLD 24 |
15 | | |
16 | | /** |
17 | | * Add right into left, in place, in the given base, where left is at least as |
18 | | * long. Returns the carry out of left's length words. Two words plus a carry |
19 | | * are below twice the base, so each sum fits in a uint64_t and carries at most |
20 | | * one into the next word. |
21 | | */ |
22 | | static PRISM_FORCE_INLINE uint32_t |
23 | | pm_bignum_base_words_add(uint64_t base, uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
24 | | uint32_t carry = 0; |
25 | | uint32_t index = 0; |
26 | | |
27 | | for (; index < right_length; index++) { |
28 | | uint64_t sum = (uint64_t) left[index] + right[index] + carry; |
29 | | carry = sum >= base; |
30 | | left[index] = (uint32_t) (carry ? sum - base : sum); |
31 | | } |
32 | | |
33 | | for (; carry != 0 && index < left_length; index++) { |
34 | | uint64_t sum = (uint64_t) left[index] + 1; |
35 | | carry = sum == base; |
36 | | left[index] = (uint32_t) (carry ? 0 : sum); |
37 | | } |
38 | | |
39 | | return carry; |
40 | | } |
41 | | |
42 | | /** |
43 | | * Subtract right from left, in place, in the given base. Left must be at least |
44 | | * right. A word plus the base fits in a uint64_t, so borrowing does too. |
45 | | */ |
46 | | static PRISM_FORCE_INLINE void |
47 | | pm_bignum_base_words_subtract(uint64_t base, uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
48 | | (void) left_length; |
49 | | uint32_t borrow = 0; |
50 | | uint32_t index = 0; |
51 | | |
52 | | for (; index < right_length; index++) { |
53 | | uint64_t subtrahend = (uint64_t) right[index] + borrow; |
54 | | borrow = left[index] < subtrahend; |
55 | | left[index] = (uint32_t) (borrow ? left[index] + base - subtrahend : left[index] - subtrahend); |
56 | | } |
57 | | |
58 | | for (; borrow != 0; index++) { |
59 | | PRISM_ASSERT(index < left_length); |
60 | | borrow = left[index] == 0; |
61 | | left[index] = (uint32_t) (borrow ? base - 1 : left[index] - 1); |
62 | | } |
63 | | } |
64 | | |
65 | | /** |
66 | | * Add right into left, in place, in base 2**32, where left is at least as |
67 | | * long. Returns the carry out of left's length words. |
68 | | */ |
69 | | static uint32_t |
70 | 473 | pm_bignum_words_add(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
71 | 473 | return pm_bignum_base_words_add(PM_BIGNUM_BINARY_BASE, left, left_length, right, right_length); |
72 | 473 | } |
73 | | |
74 | | /** |
75 | | * Add right into left, in place, in base 10**9, where left is at least as |
76 | | * long. Returns the carry out of left's length words. |
77 | | */ |
78 | | static uint32_t |
79 | 0 | pm_bignum_decimal_words_add(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
80 | 0 | return pm_bignum_base_words_add(PM_BIGNUM_DECIMAL_BASE, left, left_length, right, right_length); |
81 | 0 | } |
82 | | |
83 | | /** |
84 | | * Subtract right from left, in place, in base 2**32. Left must be at least |
85 | | * right. |
86 | | */ |
87 | | static void |
88 | 0 | pm_bignum_words_subtract(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
89 | 0 | pm_bignum_base_words_subtract(PM_BIGNUM_BINARY_BASE, left, left_length, right, right_length); |
90 | 0 | } |
91 | | |
92 | | /** |
93 | | * Subtract right from left, in place, in base 10**9. Left must be at least |
94 | | * right. |
95 | | */ |
96 | | static void |
97 | 0 | pm_bignum_decimal_words_subtract(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
98 | 0 | pm_bignum_base_words_subtract(PM_BIGNUM_DECIMAL_BASE, left, left_length, right, right_length); |
99 | 0 | } |
100 | | |
101 | | /** |
102 | | * Multiply left by right into result, which has room for left_length + |
103 | | * right_length words and overlaps neither, using the schoolbook method. A word |
104 | | * times a word plus two words is at most 2**64 - 1, so each step fits in a |
105 | | * uint64_t. |
106 | | */ |
107 | | static void |
108 | 1.70k | pm_bignum_words_multiply_schoolbook(uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
109 | 1.70k | memset(result, 0, sizeof(uint32_t) * (left_length + right_length)); |
110 | | |
111 | 6.84k | for (uint32_t right_index = 0; right_index < right_length; right_index++5.14k ) { Branch (111:36): [True: 5.14k, False: 1.70k]
|
112 | 5.14k | uint64_t carry = 0; |
113 | | |
114 | 34.9k | for (uint32_t left_index = 0; left_index < left_length; left_index++29.7k ) { Branch (114:39): [True: 29.7k, False: 5.14k]
|
115 | 29.7k | carry += (uint64_t) left[left_index] * right[right_index] + result[left_index + right_index]; |
116 | 29.7k | result[left_index + right_index] = (uint32_t) carry; |
117 | 29.7k | carry >>= 32; |
118 | 29.7k | } |
119 | | |
120 | 5.14k | result[right_index + left_length] = (uint32_t) carry; |
121 | 5.14k | } |
122 | 1.70k | } |
123 | | |
124 | | /** |
125 | | * The number of products of two words in base 10**9 that can be added to a |
126 | | * word before the sum could overflow a uint64_t: 18 * (10**9 - 1)**2 plus |
127 | | * 10**9 - 1 is just below 1.8 * 10**19, while 2**64 is just above |
128 | | * 1.84 * 10**19. |
129 | | */ |
130 | 0 | #define PM_BIGNUM_DECIMAL_PRODUCTS 18 |
131 | | |
132 | | /** |
133 | | * Multiply left by right into result, in base 10**9, which has room for |
134 | | * left_length + right_length words and overlaps neither, using the schoolbook |
135 | | * method. Dividing by 10**9 is what costs the most here, so each word of the |
136 | | * result is computed in one go as the sum of the products of the pairs of |
137 | | * words that land on it, which only divides once every |
138 | | * PM_BIGNUM_DECIMAL_PRODUCTS products to keep the sum in a uint64_t, and once |
139 | | * more to carry into the next word. |
140 | | */ |
141 | | static void |
142 | 0 | pm_bignum_decimal_words_multiply_schoolbook(uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
143 | 0 | if (left_length == 0 || right_length == 0) { Branch (143:9): [True: 0, False: 0]
Branch (143:29): [True: 0, False: 0]
|
144 | 0 | memset(result, 0, sizeof(uint32_t) * (left_length + right_length)); |
145 | 0 | return; |
146 | 0 | } |
147 | | |
148 | | /* The sum of every column is below its number of products times 10**18, |
149 | | * and there are fewer than 2**29 products in a column, so the carry into |
150 | | * the next column is below 2**29 * 10**9 and fits in a uint64_t. */ |
151 | 0 | uint64_t carry = 0; |
152 | |
|
153 | 0 | for (uint32_t index = 0; index < left_length + right_length - 1; index++) { Branch (153:30): [True: 0, False: 0]
|
154 | 0 | uint32_t left_start = index < right_length ? 0 : index - right_length + 1; Branch (154:31): [True: 0, False: 0]
|
155 | 0 | uint32_t left_end = index < left_length ? index + 1 : left_length; Branch (155:29): [True: 0, False: 0]
|
156 | |
|
157 | 0 | uint64_t low = 0; |
158 | 0 | uint64_t high = 0; |
159 | 0 | uint32_t products = 0; |
160 | |
|
161 | 0 | for (uint32_t left_index = left_start; left_index < left_end; left_index++) { Branch (161:48): [True: 0, False: 0]
|
162 | 0 | low += (uint64_t) left[left_index] * right[index - left_index]; |
163 | |
|
164 | 0 | if (++products == PM_BIGNUM_DECIMAL_PRODUCTS) { Branch (164:17): [True: 0, False: 0]
|
165 | 0 | high += low / PM_BIGNUM_DECIMAL_BASE; |
166 | 0 | low %= PM_BIGNUM_DECIMAL_BASE; |
167 | 0 | products = 0; |
168 | 0 | } |
169 | 0 | } |
170 | |
|
171 | 0 | low += carry % PM_BIGNUM_DECIMAL_BASE; |
172 | 0 | high += carry / PM_BIGNUM_DECIMAL_BASE; |
173 | |
|
174 | 0 | result[index] = (uint32_t) (low % PM_BIGNUM_DECIMAL_BASE); |
175 | 0 | carry = high + low / PM_BIGNUM_DECIMAL_BASE; |
176 | 0 | } |
177 | | |
178 | | /* The product is below 10**(9 * (left_length + right_length)), so what is |
179 | | * left over is the top word. */ |
180 | 0 | PRISM_ASSERT(carry < PM_BIGNUM_DECIMAL_BASE); Branch (180:5): [True: 0, False: 0]
Branch (180:5): [True: 0, False: 0]
|
181 | 0 | result[left_length + right_length - 1] = (uint32_t) carry; |
182 | 0 | } |
183 | | |
184 | | static void pm_bignum_words_multiply(uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length, pm_bignum_scratch_t *scratch); |
185 | | static void pm_bignum_decimal_words_multiply(uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length, pm_bignum_scratch_t *scratch); |
186 | | |
187 | | /** |
188 | | * The helpers below take the base of the words, 2**32 or 10**9, as a |
189 | | * parameter, but are always inlined into callers that pass a constant, so the |
190 | | * choice is made at compile time and every call they make is direct. |
191 | | */ |
192 | | |
193 | | /** |
194 | | * Add right into left in the given base. Returns the carry out of left's |
195 | | * length words. |
196 | | */ |
197 | | static PRISM_FORCE_INLINE uint32_t |
198 | | pm_bignum_radix_add(uint64_t base, uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
199 | | return base == PM_BIGNUM_DECIMAL_BASE ? pm_bignum_decimal_words_add(left, left_length, right, right_length) : pm_bignum_words_add(left, left_length, right, right_length); |
200 | | } |
201 | | |
202 | | /** |
203 | | * Subtract right from left in the given base. |
204 | | */ |
205 | | static PRISM_FORCE_INLINE void |
206 | | pm_bignum_radix_subtract(uint64_t base, uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
207 | | if (base == PM_BIGNUM_DECIMAL_BASE) { |
208 | | pm_bignum_decimal_words_subtract(left, left_length, right, right_length); |
209 | | } else { |
210 | | pm_bignum_words_subtract(left, left_length, right, right_length); |
211 | | } |
212 | | } |
213 | | |
214 | | /** |
215 | | * Multiply left by right with the schoolbook method in the given base. |
216 | | */ |
217 | | static PRISM_FORCE_INLINE void |
218 | | pm_bignum_radix_multiply_schoolbook(uint64_t base, uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) { |
219 | | if (base == PM_BIGNUM_DECIMAL_BASE) { |
220 | | pm_bignum_decimal_words_multiply_schoolbook(result, left, left_length, right, right_length); |
221 | | } else { |
222 | | pm_bignum_words_multiply_schoolbook(result, left, left_length, right, right_length); |
223 | | } |
224 | | } |
225 | | |
226 | | /** |
227 | | * Multiply left by right in the given base. |
228 | | */ |
229 | | static PRISM_FORCE_INLINE void |
230 | | pm_bignum_radix_multiply(uint64_t base, uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length, pm_bignum_scratch_t *scratch) { |
231 | | if (base == PM_BIGNUM_DECIMAL_BASE) { |
232 | | pm_bignum_decimal_words_multiply(result, left, left_length, right, right_length, scratch); |
233 | | } else { |
234 | | pm_bignum_words_multiply(result, left, left_length, right, right_length, scratch); |
235 | | } |
236 | | } |
237 | | |
238 | | /** |
239 | | * Multiply left by right into result, which has room for left_length + |
240 | | * right_length words and overlaps neither, in the given base. Above |
241 | | * PM_BIGNUM_KARATSUBA_THRESHOLD this uses Karatsuba's method: splitting each |
242 | | * number into low and high halves at the same word, low * low and high * high |
243 | | * go straight into the bottom and top of the result, and the middle term is |
244 | | * (low + high) * (low + high) minus those two, which takes three |
245 | | * multiplications of half the size instead of four. None of this depends on |
246 | | * the base, only the additions, subtractions, and schoolbook multiplications |
247 | | * that it is built from. |
248 | | */ |
249 | | static PRISM_FORCE_INLINE void |
250 | | pm_bignum_radix_karatsuba(uint64_t base, uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length, pm_bignum_scratch_t *scratch) { |
251 | | if (left_length < right_length) { |
252 | | const uint32_t *words = left; |
253 | | left = right; |
254 | | right = words; |
255 | | |
256 | | uint32_t length = left_length; |
257 | | left_length = right_length; |
258 | | right_length = length; |
259 | | } |
260 | | |
261 | | if (right_length < PM_BIGNUM_KARATSUBA_THRESHOLD) { |
262 | | pm_bignum_radix_multiply_schoolbook(base, result, left, left_length, right, right_length); |
263 | | return; |
264 | | } |
265 | | |
266 | | uint32_t *saved = scratch->cursor; |
267 | | |
268 | | /* When left is at least twice as long, splitting both at the same word |
269 | | * would leave the halves badly mismatched, so instead multiply right by |
270 | | * each right-sized piece of left and add the products into the result. */ |
271 | | if (left_length >= 2 * right_length) { |
272 | | memset(result, 0, sizeof(uint32_t) * (left_length + right_length)); |
273 | | uint32_t *product = pm_bignum_scratch_take(scratch, 2 * right_length); |
274 | | |
275 | | for (uint32_t offset = 0; offset < left_length; offset += right_length) { |
276 | | uint32_t piece = left_length - offset < right_length ? left_length - offset : right_length; |
277 | | pm_bignum_radix_multiply(base, product, left + offset, piece, right, right_length, scratch); |
278 | | pm_bignum_radix_add(base, result + offset, left_length + right_length - offset, product, piece + right_length); |
279 | | } |
280 | | |
281 | | scratch->cursor = saved; |
282 | | return; |
283 | | } |
284 | | |
285 | | uint32_t half = right_length / 2; |
286 | | uint32_t left_high_length = left_length - half; |
287 | | uint32_t right_high_length = right_length - half; |
288 | | |
289 | | pm_bignum_radix_multiply(base, result, left, half, right, half, scratch); |
290 | | pm_bignum_radix_multiply(base, result + 2 * half, left + half, left_high_length, right + half, right_high_length, scratch); |
291 | | |
292 | | /* Each high half is at least as long as the low half, so adding the low |
293 | | * half into a copy of the high half needs at most one more word. */ |
294 | | uint32_t *left_sum = pm_bignum_scratch_take(scratch, left_high_length + 1); |
295 | | memcpy(left_sum, left + half, sizeof(uint32_t) * left_high_length); |
296 | | left_sum[left_high_length] = 0; |
297 | | pm_bignum_radix_add(base, left_sum, left_high_length + 1, left, half); |
298 | | |
299 | | uint32_t *right_sum = pm_bignum_scratch_take(scratch, right_high_length + 1); |
300 | | memcpy(right_sum, right + half, sizeof(uint32_t) * right_high_length); |
301 | | right_sum[right_high_length] = 0; |
302 | | pm_bignum_radix_add(base, right_sum, right_high_length + 1, right, half); |
303 | | |
304 | | uint32_t middle_length = left_high_length + right_high_length + 2; |
305 | | uint32_t *middle = pm_bignum_scratch_take(scratch, middle_length); |
306 | | pm_bignum_radix_multiply(base, middle, left_sum, left_high_length + 1, right_sum, right_high_length + 1, scratch); |
307 | | pm_bignum_radix_subtract(base, middle, middle_length, result, 2 * half); |
308 | | pm_bignum_radix_subtract(base, middle, middle_length, result + 2 * half, left_high_length + right_high_length); |
309 | | |
310 | | while (middle_length > 0 && middle[middle_length - 1] == 0) middle_length--; |
311 | | pm_bignum_radix_add(base, result + half, left_length + right_length - half, middle, middle_length); |
312 | | |
313 | | scratch->cursor = saved; |
314 | | } |
315 | | |
316 | | /** |
317 | | * Multiply left by right into result in base 2**32, which has room for |
318 | | * left_length + right_length words and overlaps neither. |
319 | | */ |
320 | | static void |
321 | 1.70k | pm_bignum_words_multiply(uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length, pm_bignum_scratch_t *scratch) { |
322 | 1.70k | pm_bignum_radix_karatsuba(PM_BIGNUM_BINARY_BASE, result, left, left_length, right, right_length, scratch); |
323 | 1.70k | } |
324 | | |
325 | | /** |
326 | | * Multiply left by right into result in base 10**9, which has room for |
327 | | * left_length + right_length words and overlaps neither. |
328 | | */ |
329 | | static void |
330 | 0 | pm_bignum_decimal_words_multiply(uint32_t *result, const uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length, pm_bignum_scratch_t *scratch) { |
331 | 0 | pm_bignum_radix_karatsuba(PM_BIGNUM_DECIMAL_BASE, result, left, left_length, right, right_length, scratch); |
332 | 0 | } |
333 | | |
334 | | /** |
335 | | * Add right into left, in place, in the given base. Left is zero-extended to |
336 | | * the length of right if it is shorter, and a carry out of the top becomes a |
337 | | * new top word. |
338 | | */ |
339 | | static PRISM_FORCE_INLINE void |
340 | | pm_bignum_radix_add_bignum(uint64_t base, pm_bignum_t *left, const pm_bignum_t *right) { |
341 | | if (left->length < right->length) { |
342 | | memset(left->words + left->length, 0, sizeof(uint32_t) * (right->length - left->length)); |
343 | | left->length = right->length; |
344 | | } |
345 | | |
346 | | uint32_t carry = pm_bignum_radix_add(base, left->words, left->length, right->words, right->length); |
347 | | if (carry != 0) left->words[left->length++] = carry; |
348 | | } |
349 | | |
350 | | /** |
351 | | * Multiply left by right into result, in the given base, and trim the zero |
352 | | * words off the top of the product. |
353 | | */ |
354 | | static PRISM_FORCE_INLINE void |
355 | | pm_bignum_radix_multiply_bignum(uint64_t base, pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch) { |
356 | | uint32_t length = left->length + right->length; |
357 | | pm_bignum_radix_multiply(base, result->words, left->words, left->length, right->words, right->length, scratch); |
358 | | |
359 | | while (length > 0 && result->words[length - 1] == 0) length--; |
360 | | result->length = length; |
361 | | } |
362 | | |
363 | | /** |
364 | | * Add right into left, in place. The storage must have room for the sum, which |
365 | | * is at most one word longer than the longer of the two. |
366 | | */ |
367 | | void |
368 | 473 | pm_bignum_add(pm_bignum_t *left, const pm_bignum_t *right) { |
369 | 473 | pm_bignum_radix_add_bignum(PM_BIGNUM_BINARY_BASE, left, right); |
370 | 473 | } |
371 | | |
372 | | /** |
373 | | * Multiply left by right into result, whose storage has room for |
374 | | * left->length + right->length words and overlaps neither. Long numbers use |
375 | | * Karatsuba's method, which takes its temporaries from scratch. |
376 | | */ |
377 | | void |
378 | 1.70k | pm_bignum_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch) { |
379 | 1.70k | pm_bignum_radix_multiply_bignum(PM_BIGNUM_BINARY_BASE, result, left, right, scratch); |
380 | 1.70k | } |
381 | | |
382 | | /** |
383 | | * Add right into left, in place, in base 10**9. The storage must have room for |
384 | | * the sum, which is at most one word longer than the longer of the two. |
385 | | */ |
386 | | void |
387 | 0 | pm_bignum_decimal_add(pm_bignum_t *left, const pm_bignum_t *right) { |
388 | 0 | pm_bignum_radix_add_bignum(PM_BIGNUM_DECIMAL_BASE, left, right); |
389 | 0 | } |
390 | | |
391 | | /** |
392 | | * Multiply left by right into result, in base 10**9, whose storage has room |
393 | | * for left->length + right->length words and overlaps neither. Long numbers |
394 | | * use Karatsuba's method, which takes its temporaries from scratch. |
395 | | */ |
396 | | void |
397 | 0 | pm_bignum_decimal_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch) { |
398 | 0 | pm_bignum_radix_multiply_bignum(PM_BIGNUM_DECIMAL_BASE, result, left, right, scratch); |
399 | 0 | } |