backburn 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/Makefile +50 -0
- data/README.md +520 -0
- data/docs/cli-and-outputs.md +226 -0
- data/docs/technical-spec.md +656 -0
- data/exe/backburn +8 -0
- data/ext/backburn/extconf.rb +31 -0
- data/ext/backburn/prism/COMMIT +1 -0
- data/ext/backburn/prism/LICENSE.md +7 -0
- data/ext/backburn/prism/Makefile +213 -0
- data/ext/backburn/prism/include/prism/arena.h +45 -0
- data/ext/backburn/prism/include/prism/arena.hpp +42 -0
- data/ext/backburn/prism/include/prism/ast.h +10395 -0
- data/ext/backburn/prism/include/prism/ast.hpp +641 -0
- data/ext/backburn/prism/include/prism/buffer.h +60 -0
- data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
- data/ext/backburn/prism/include/prism/comments.h +72 -0
- data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
- data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
- data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
- data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
- data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
- data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
- data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
- data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
- data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
- data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
- data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
- data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
- data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
- data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
- data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
- data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
- data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
- data/ext/backburn/prism/include/prism/errors_format.h +63 -0
- data/ext/backburn/prism/include/prism/excludes.h +29 -0
- data/ext/backburn/prism/include/prism/integer.h +79 -0
- data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
- data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
- data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
- data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
- data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
- data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
- data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
- data/ext/backburn/prism/include/prism/internal/char.h +145 -0
- data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
- data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
- data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
- data/ext/backburn/prism/include/prism/internal/double.h +19 -0
- data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
- data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
- data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
- data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
- data/ext/backburn/prism/include/prism/internal/list.h +62 -0
- data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
- data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
- data/ext/backburn/prism/include/prism/internal/node.h +197 -0
- data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
- data/ext/backburn/prism/include/prism/internal/options.h +248 -0
- data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
- data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
- data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
- data/ext/backburn/prism/include/prism/internal/source.h +72 -0
- data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
- data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
- data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
- data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
- data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
- data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
- data/ext/backburn/prism/include/prism/json.h +40 -0
- data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
- data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
- data/ext/backburn/prism/include/prism/node.h +139 -0
- data/ext/backburn/prism/include/prism/node.hpp +272 -0
- data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
- data/ext/backburn/prism/include/prism/options.h +393 -0
- data/ext/backburn/prism/include/prism/options.hpp +43 -0
- data/ext/backburn/prism/include/prism/parser.h +433 -0
- data/ext/backburn/prism/include/prism/parser.hpp +274 -0
- data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
- data/ext/backburn/prism/include/prism/serialize.h +123 -0
- data/ext/backburn/prism/include/prism/source.h +163 -0
- data/ext/backburn/prism/include/prism/stream.h +36 -0
- data/ext/backburn/prism/include/prism/string_query.h +97 -0
- data/ext/backburn/prism/include/prism/stringy.h +61 -0
- data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
- data/ext/backburn/prism/include/prism/version.h +46 -0
- data/ext/backburn/prism/include/prism.h +123 -0
- data/ext/backburn/prism/include/prism.hpp +24 -0
- data/ext/backburn/prism/src/arena.c +200 -0
- data/ext/backburn/prism/src/bignum.c +399 -0
- data/ext/backburn/prism/src/buffer.c +405 -0
- data/ext/backburn/prism/src/char.c +289 -0
- data/ext/backburn/prism/src/comments.c +455 -0
- data/ext/backburn/prism/src/constant_pool.c +410 -0
- data/ext/backburn/prism/src/diagnostic.c +918 -0
- data/ext/backburn/prism/src/double.c +394 -0
- data/ext/backburn/prism/src/encoding.c +1701 -0
- data/ext/backburn/prism/src/errors_format.c +650 -0
- data/ext/backburn/prism/src/integer.c +675 -0
- data/ext/backburn/prism/src/json.c +5745 -0
- data/ext/backburn/prism/src/line_offset_list.c +99 -0
- data/ext/backburn/prism/src/list.c +24 -0
- data/ext/backburn/prism/src/memchr.c +37 -0
- data/ext/backburn/prism/src/node.c +4464 -0
- data/ext/backburn/prism/src/options.c +475 -0
- data/ext/backburn/prism/src/parser.c +371 -0
- data/ext/backburn/prism/src/prettyprint.c +9038 -0
- data/ext/backburn/prism/src/prism.c +23615 -0
- data/ext/backburn/prism/src/regexp.c +3833 -0
- data/ext/backburn/prism/src/serialize.c +2656 -0
- data/ext/backburn/prism/src/source.c +525 -0
- data/ext/backburn/prism/src/static_literals.c +562 -0
- data/ext/backburn/prism/src/string_query.c +195 -0
- data/ext/backburn/prism/src/stringy.c +77 -0
- data/ext/backburn/prism/src/strncasecmp.c +37 -0
- data/ext/backburn/prism/src/strpbrk.c +465 -0
- data/ext/backburn/prism/src/tokens.c +724 -0
- data/ext/backburn/prism/src/xxh3.c +292 -0
- data/lib/backburn/version.rb +7 -0
- data/presets/devise.roots +11 -0
- data/presets/fast-mcp.roots +5 -0
- data/presets/graphql.roots +6 -0
- data/presets/phlex.roots +7 -0
- data/presets/pundit.roots +3 -0
- data/presets/rails-framework.roots +10 -0
- data/presets/rails.roots +83 -0
- data/presets/sidekiq.roots +4 -0
- data/presets/thor.roots +3 -0
- data/rails.roots +99 -0
- data/scripts/embed-presets.sh +13 -0
- data/scripts/version-h.sh +7 -0
- data/src/analysis/analysis.c +95 -0
- data/src/analysis/analysis.h +24 -0
- data/src/analysis/batches.c +127 -0
- data/src/analysis/batches.h +17 -0
- data/src/analysis/graph.c +238 -0
- data/src/analysis/graph.h +34 -0
- data/src/analysis/mark.c +300 -0
- data/src/analysis/mark.h +21 -0
- data/src/analysis/roots.c +229 -0
- data/src/analysis/roots.h +29 -0
- data/src/cli/cli.c +237 -0
- data/src/cli/cli.h +9 -0
- data/src/cli/config.c +112 -0
- data/src/cli/config.h +14 -0
- data/src/cli/options.c +17 -0
- data/src/cli/options.h +35 -0
- data/src/commands/commands.h +11 -0
- data/src/commands/explain.c +255 -0
- data/src/commands/gemfile.c +126 -0
- data/src/commands/gemfile.h +27 -0
- data/src/commands/gems.c +408 -0
- data/src/commands/init.c +92 -0
- data/src/commands/roots.c +82 -0
- data/src/commands/scan.c +91 -0
- data/src/core/intern.c +94 -0
- data/src/core/intern.h +40 -0
- data/src/core/json.c +17 -0
- data/src/core/json.h +9 -0
- data/src/core/model.c +25 -0
- data/src/core/model.h +85 -0
- data/src/core/util.c +116 -0
- data/src/core/util.h +48 -0
- data/src/index/erb.c +40 -0
- data/src/index/erb.h +9 -0
- data/src/index/haml.c +334 -0
- data/src/index/haml.h +10 -0
- data/src/index/indexer.c +702 -0
- data/src/index/indexer.h +19 -0
- data/src/index/walk.c +144 -0
- data/src/index/walk.h +14 -0
- data/src/main.c +81 -0
- data/src/output/baseline.c +101 -0
- data/src/output/baseline.h +15 -0
- data/src/output/report.c +440 -0
- data/src/output/report.h +28 -0
- metadata +231 -0
|
@@ -0,0 +1,399 @@
|
|
|
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
|
+
pm_bignum_words_add(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) {
|
|
71
|
+
return pm_bignum_base_words_add(PM_BIGNUM_BINARY_BASE, left, left_length, right, right_length);
|
|
72
|
+
}
|
|
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
|
+
pm_bignum_decimal_words_add(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) {
|
|
80
|
+
return pm_bignum_base_words_add(PM_BIGNUM_DECIMAL_BASE, left, left_length, right, right_length);
|
|
81
|
+
}
|
|
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
|
+
pm_bignum_words_subtract(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) {
|
|
89
|
+
pm_bignum_base_words_subtract(PM_BIGNUM_BINARY_BASE, left, left_length, right, right_length);
|
|
90
|
+
}
|
|
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
|
+
pm_bignum_decimal_words_subtract(uint32_t *left, uint32_t left_length, const uint32_t *right, uint32_t right_length) {
|
|
98
|
+
pm_bignum_base_words_subtract(PM_BIGNUM_DECIMAL_BASE, left, left_length, right, right_length);
|
|
99
|
+
}
|
|
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
|
+
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
|
+
memset(result, 0, sizeof(uint32_t) * (left_length + right_length));
|
|
110
|
+
|
|
111
|
+
for (uint32_t right_index = 0; right_index < right_length; right_index++) {
|
|
112
|
+
uint64_t carry = 0;
|
|
113
|
+
|
|
114
|
+
for (uint32_t left_index = 0; left_index < left_length; left_index++) {
|
|
115
|
+
carry += (uint64_t) left[left_index] * right[right_index] + result[left_index + right_index];
|
|
116
|
+
result[left_index + right_index] = (uint32_t) carry;
|
|
117
|
+
carry >>= 32;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
result[right_index + left_length] = (uint32_t) carry;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
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
|
+
#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
|
+
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
|
+
if (left_length == 0 || right_length == 0) {
|
|
144
|
+
memset(result, 0, sizeof(uint32_t) * (left_length + right_length));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
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
|
+
uint64_t carry = 0;
|
|
152
|
+
|
|
153
|
+
for (uint32_t index = 0; index < left_length + right_length - 1; index++) {
|
|
154
|
+
uint32_t left_start = index < right_length ? 0 : index - right_length + 1;
|
|
155
|
+
uint32_t left_end = index < left_length ? index + 1 : left_length;
|
|
156
|
+
|
|
157
|
+
uint64_t low = 0;
|
|
158
|
+
uint64_t high = 0;
|
|
159
|
+
uint32_t products = 0;
|
|
160
|
+
|
|
161
|
+
for (uint32_t left_index = left_start; left_index < left_end; left_index++) {
|
|
162
|
+
low += (uint64_t) left[left_index] * right[index - left_index];
|
|
163
|
+
|
|
164
|
+
if (++products == PM_BIGNUM_DECIMAL_PRODUCTS) {
|
|
165
|
+
high += low / PM_BIGNUM_DECIMAL_BASE;
|
|
166
|
+
low %= PM_BIGNUM_DECIMAL_BASE;
|
|
167
|
+
products = 0;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
low += carry % PM_BIGNUM_DECIMAL_BASE;
|
|
172
|
+
high += carry / PM_BIGNUM_DECIMAL_BASE;
|
|
173
|
+
|
|
174
|
+
result[index] = (uint32_t) (low % PM_BIGNUM_DECIMAL_BASE);
|
|
175
|
+
carry = high + low / PM_BIGNUM_DECIMAL_BASE;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* The product is below 10**(9 * (left_length + right_length)), so what is
|
|
179
|
+
* left over is the top word. */
|
|
180
|
+
PRISM_ASSERT(carry < PM_BIGNUM_DECIMAL_BASE);
|
|
181
|
+
result[left_length + right_length - 1] = (uint32_t) carry;
|
|
182
|
+
}
|
|
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
|
+
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
|
+
pm_bignum_radix_karatsuba(PM_BIGNUM_BINARY_BASE, result, left, left_length, right, right_length, scratch);
|
|
323
|
+
}
|
|
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
|
+
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
|
+
pm_bignum_radix_karatsuba(PM_BIGNUM_DECIMAL_BASE, result, left, left_length, right, right_length, scratch);
|
|
332
|
+
}
|
|
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
|
+
pm_bignum_add(pm_bignum_t *left, const pm_bignum_t *right) {
|
|
369
|
+
pm_bignum_radix_add_bignum(PM_BIGNUM_BINARY_BASE, left, right);
|
|
370
|
+
}
|
|
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
|
+
pm_bignum_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch) {
|
|
379
|
+
pm_bignum_radix_multiply_bignum(PM_BIGNUM_BINARY_BASE, result, left, right, scratch);
|
|
380
|
+
}
|
|
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
|
+
pm_bignum_decimal_add(pm_bignum_t *left, const pm_bignum_t *right) {
|
|
388
|
+
pm_bignum_radix_add_bignum(PM_BIGNUM_DECIMAL_BASE, left, right);
|
|
389
|
+
}
|
|
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
|
+
pm_bignum_decimal_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch) {
|
|
398
|
+
pm_bignum_radix_multiply_bignum(PM_BIGNUM_DECIMAL_BASE, result, left, right, scratch);
|
|
399
|
+
}
|