1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Multiplying two 19-decimal-place numbers.
//!
//! Both inputs are stored as integers counting 10⁻¹⁹ steps, so their plain
//! integer product counts 10⁻³⁸ steps: 38 decimal places. To get back to 19
//! places we divide that product by 10¹⁹ (10,000,000,000,000,000,000) and
//! round. Three problems:
//!
//! 1. The product of two 128-bit numbers needs up to 256 bits. We build it
//! from four 64 × 64-bit multiplications (`mul_128x128`).
//! 2. Dividing by 10¹⁹ with the CPU's divide instruction is slow, and a
//! 256-bit dividend is too wide for it anyway. Instead we divide in two
//! 64-bit "digits", like long division by hand, and each step replaces
//! the division by multiplications (`div2by1`).
//! 3. Rounding is decided from the remainder, by the crate's shared rounding
//! decision (`round::decide`).
//!
//! There is also a shortcut: when one input is a whole number (e.g. a
//! quantity of 250), no division is needed at all (`whole`,
//! `mul19_fast`).
//!
//! Checked against a big-integer oracle in `tests/mul_oracle.rs`, in every
//! rounding mode. The algorithm was imported from the design discussion (see
//! `docs/design-brief.md`). Since then: the rounding step supports every
//! mode; rounding up past 128 bits is reported as overflow (the original
//! wrapped to 0 in release builds); the whole-number shortcut checks both
//! operands and rejects whole parts of 2⁶⁴ or more; and the comments.
use crate;
/// 10¹⁹ = 10,000,000,000,000,000,000: the divisor, and the stored value of 1.
///
/// It fits in 64 bits with its top bit set (it's above 2⁶³ ≈ 9.2 × 10¹⁸).
/// The fast division below relies on that.
pub const D: u64 = 10_000_000_000_000_000_000;
/// A precomputed stand-in for 1/10¹⁹, so that we can divide by multiplying.
///
/// Mathematically it is ⌊(2¹²⁸ − 1) / 10¹⁹⌋ − 2⁶⁴. The ⌊(2¹²⁸ − 1) / 10¹⁹⌋ part
/// is "2¹²⁸ divided by 10¹⁹": multiplying by it and keeping the top half is
/// like dividing by 10¹⁹. That number needs 65 bits, and its top bit is
/// always 1 (because 10¹⁹ has its top bit set). So we subtract 2⁶⁴ to drop
/// that bit, fitting it in 64 bits, and add the dropped part back in
/// `div2by1` with a cheap addition.
///
/// This is the "reciprocal" from Möller & Granlund, *Improved division by
/// invariant integers* (2011), for further reading.
const V: u64 = as u64;
/// Divides the two-word number `u1 × 2⁶⁴ + u0` by 10¹⁹, returning the
/// quotient and remainder. Requires `u1 < 10¹⁹`, which guarantees the
/// quotient fits in one 64-bit word.
///
/// No divide instruction: two multiplications and a couple of corrections.
pub
/// Multiplies two 128-bit numbers into a 256-bit result, returned as four
/// 64-bit words, least significant first.
///
/// Schoolbook multiplication with 64-bit "digits": split each input into a
/// high and a low half, multiply the four pairs (each 64 × 64-bit product
/// fits in 128 bits), and add them up in the right positions with carries.
pub
/// `x × y / 10¹⁹` for magnitudes (no sign), rounded with `mode`, or `None`
/// if the result doesn't fit in 128 bits. `negative` is the sign the final
/// result will have, which the directed rounding modes need.
pub
/// Divides a 256-bit magnitude (four 64-bit words, least significant first)
/// by 10¹⁹, rounding with `mode`. `None` if the result doesn't fit in 128
/// bits. `negative` is the sign the final result will have.
///
/// Used to bring a product at 38 decimal places back to 19, for a single
/// multiplication and for a whole sum of products (`ProductSum`).
pub
/// `x × y / 10¹⁹` for magnitudes, rounded with `mode`, taking the shortcut
/// when either operand is a whole number. `None` if the result doesn't fit in 128 bits.
///
/// The entry point for `Dec19::mul` and `UDec19::mul`: they split their
/// operands into sign and magnitude, call this, and put the sign back.
pub
/// Gives a sign to a magnitude as an `i128`, or `None` if it doesn't fit.
/// `a × b` for two signed stored values, rounded with `mode`. `None` on
/// overflow.
/// `a × b`, rounded half-even. `None` on overflow.
/// `a × b`, rounded toward negative infinity. `None` on overflow.
// ---- Shortcut: one input is a whole number ---------------------------------
/// 5¹⁹ = 19,073,486,328,125. With 2¹⁹ it makes up 10¹⁹ = 2¹⁹ × 5¹⁹.
const P5: u128 = 19_073_486_328_125;
/// The number that "undoes" multiplying by `d`, when you only keep the low
/// 128 bits: `d × inverse` leaves 1 in the low 128 bits. Exists for any odd
/// `d`.
///
/// Found by Newton's method: each round of `x = x × (2 − d × x)` doubles the
/// number of correct low bits. Starting from `x = d` gives 3 correct bits (any
/// odd number times itself leaves 1 in the low 3 bits), so 7 rounds give
/// 3 × 2⁷ = 384 ≥ 128 correct bits.
const
/// Multiplying by this undoes a multiplication by 5¹⁹ (in the low 128 bits).
const INV5: u128 = inv_mod_2_128;
/// The largest number that can be multiplied by 5¹⁹ without overflowing 128
/// bits.
const LIM5: u128 = u128MAX / P5;
/// If the stored value `b` is a whole number below 2⁶⁴ (e.g. 250.0),
/// returns that number (250), without dividing. Otherwise `None`.
///
/// A stored value is a whole number exactly when it's divisible by 10¹⁹ =
/// 2¹⁹ × 5¹⁹, i.e. by both 2¹⁹ and 5¹⁹:
/// - divisible by 2¹⁹ means its low 19 bits are zero: one AND;
/// - for 5¹⁹, multiply by `INV5`, which undoes a multiplication by 5¹⁹. If
/// the number really is 5¹⁹ × k, the result is exactly k, which is at most
/// `LIM5`. If it isn't a multiple, the result is a number above `LIM5`:
/// multiplying by INV5 just reshuffles all 128-bit values one-to-one, and
/// the small results are all taken by the multiples.
///
/// A `Dec19`'s whole part is always below 2⁶⁴ (it's at most about
/// 1.7 × 10¹⁹), but a `UDec19`'s can reach about 3.4 × 10¹⁹. Those rare huge
/// whole numbers return `None` and take the general path.
pub
/// `x × n` for a magnitude and a whole number, or `None` if it doesn't fit
/// in 128 bits. Exact: no rounding needed.
pub
/// `a × b`, rounded with `mode`, taking the shortcut when `b` is a whole
/// number. `None` on overflow.
/// `a × b`, rounded half-even, taking the shortcut when `b` is a whole
/// number. `None` on overflow.