/src/mozilla-central/intl/icu/source/i18n/double-conversion-bignum.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2018 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | // |
4 | | // From the double-conversion library. Original license: |
5 | | // |
6 | | // Copyright 2010 the V8 project authors. All rights reserved. |
7 | | // Redistribution and use in source and binary forms, with or without |
8 | | // modification, are permitted provided that the following conditions are |
9 | | // met: |
10 | | // |
11 | | // * Redistributions of source code must retain the above copyright |
12 | | // notice, this list of conditions and the following disclaimer. |
13 | | // * Redistributions in binary form must reproduce the above |
14 | | // copyright notice, this list of conditions and the following |
15 | | // disclaimer in the documentation and/or other materials provided |
16 | | // with the distribution. |
17 | | // * Neither the name of Google Inc. nor the names of its |
18 | | // contributors may be used to endorse or promote products derived |
19 | | // from this software without specific prior written permission. |
20 | | // |
21 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | |
33 | | // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING |
34 | | #include "unicode/utypes.h" |
35 | | #if !UCONFIG_NO_FORMATTING |
36 | | |
37 | | // ICU PATCH: Customize header file paths for ICU. |
38 | | |
39 | | #include "double-conversion-bignum.h" |
40 | | #include "double-conversion-utils.h" |
41 | | |
42 | | // ICU PATCH: Wrap in ICU namespace |
43 | | U_NAMESPACE_BEGIN |
44 | | |
45 | | namespace double_conversion { |
46 | | |
47 | | Bignum::Bignum() |
48 | 0 | : bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) { |
49 | 0 | for (int i = 0; i < kBigitCapacity; ++i) { |
50 | 0 | bigits_[i] = 0; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | |
55 | | template<typename S> |
56 | | static int BitSize(S value) { |
57 | | (void) value; // Mark variable as used. |
58 | | return 8 * sizeof(value); |
59 | | } |
60 | | |
61 | | // Guaranteed to lie in one Bigit. |
62 | 0 | void Bignum::AssignUInt16(uint16_t value) { |
63 | 0 | ASSERT(kBigitSize >= BitSize(value)); |
64 | 0 | Zero(); |
65 | 0 | if (value == 0) return; |
66 | 0 | |
67 | 0 | EnsureCapacity(1); |
68 | 0 | bigits_[0] = value; |
69 | 0 | used_digits_ = 1; |
70 | 0 | } |
71 | | |
72 | | |
73 | 0 | void Bignum::AssignUInt64(uint64_t value) { |
74 | 0 | const int kUInt64Size = 64; |
75 | 0 |
|
76 | 0 | Zero(); |
77 | 0 | if (value == 0) return; |
78 | 0 | |
79 | 0 | int needed_bigits = kUInt64Size / kBigitSize + 1; |
80 | 0 | EnsureCapacity(needed_bigits); |
81 | 0 | for (int i = 0; i < needed_bigits; ++i) { |
82 | 0 | bigits_[i] = value & kBigitMask; |
83 | 0 | value = value >> kBigitSize; |
84 | 0 | } |
85 | 0 | used_digits_ = needed_bigits; |
86 | 0 | Clamp(); |
87 | 0 | } |
88 | | |
89 | | |
90 | 0 | void Bignum::AssignBignum(const Bignum& other) { |
91 | 0 | exponent_ = other.exponent_; |
92 | 0 | for (int i = 0; i < other.used_digits_; ++i) { |
93 | 0 | bigits_[i] = other.bigits_[i]; |
94 | 0 | } |
95 | 0 | // Clear the excess digits (if there were any). |
96 | 0 | for (int i = other.used_digits_; i < used_digits_; ++i) { |
97 | 0 | bigits_[i] = 0; |
98 | 0 | } |
99 | 0 | used_digits_ = other.used_digits_; |
100 | 0 | } |
101 | | |
102 | | |
103 | | static uint64_t ReadUInt64(Vector<const char> buffer, |
104 | | int from, |
105 | 0 | int digits_to_read) { |
106 | 0 | uint64_t result = 0; |
107 | 0 | for (int i = from; i < from + digits_to_read; ++i) { |
108 | 0 | int digit = buffer[i] - '0'; |
109 | 0 | ASSERT(0 <= digit && digit <= 9); |
110 | 0 | result = result * 10 + digit; |
111 | 0 | } |
112 | 0 | return result; |
113 | 0 | } |
114 | | |
115 | | |
116 | 0 | void Bignum::AssignDecimalString(Vector<const char> value) { |
117 | 0 | // 2^64 = 18446744073709551616 > 10^19 |
118 | 0 | const int kMaxUint64DecimalDigits = 19; |
119 | 0 | Zero(); |
120 | 0 | int length = value.length(); |
121 | 0 | unsigned int pos = 0; |
122 | 0 | // Let's just say that each digit needs 4 bits. |
123 | 0 | while (length >= kMaxUint64DecimalDigits) { |
124 | 0 | uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits); |
125 | 0 | pos += kMaxUint64DecimalDigits; |
126 | 0 | length -= kMaxUint64DecimalDigits; |
127 | 0 | MultiplyByPowerOfTen(kMaxUint64DecimalDigits); |
128 | 0 | AddUInt64(digits); |
129 | 0 | } |
130 | 0 | uint64_t digits = ReadUInt64(value, pos, length); |
131 | 0 | MultiplyByPowerOfTen(length); |
132 | 0 | AddUInt64(digits); |
133 | 0 | Clamp(); |
134 | 0 | } |
135 | | |
136 | | |
137 | 0 | static int HexCharValue(char c) { |
138 | 0 | if ('0' <= c && c <= '9') return c - '0'; |
139 | 0 | if ('a' <= c && c <= 'f') return 10 + c - 'a'; |
140 | 0 | ASSERT('A' <= c && c <= 'F'); |
141 | 0 | return 10 + c - 'A'; |
142 | 0 | } |
143 | | |
144 | | |
145 | 0 | void Bignum::AssignHexString(Vector<const char> value) { |
146 | 0 | Zero(); |
147 | 0 | int length = value.length(); |
148 | 0 |
|
149 | 0 | int needed_bigits = length * 4 / kBigitSize + 1; |
150 | 0 | EnsureCapacity(needed_bigits); |
151 | 0 | int string_index = length - 1; |
152 | 0 | for (int i = 0; i < needed_bigits - 1; ++i) { |
153 | 0 | // These bigits are guaranteed to be "full". |
154 | 0 | Chunk current_bigit = 0; |
155 | 0 | for (int j = 0; j < kBigitSize / 4; j++) { |
156 | 0 | current_bigit += HexCharValue(value[string_index--]) << (j * 4); |
157 | 0 | } |
158 | 0 | bigits_[i] = current_bigit; |
159 | 0 | } |
160 | 0 | used_digits_ = needed_bigits - 1; |
161 | 0 |
|
162 | 0 | Chunk most_significant_bigit = 0; // Could be = 0; |
163 | 0 | for (int j = 0; j <= string_index; ++j) { |
164 | 0 | most_significant_bigit <<= 4; |
165 | 0 | most_significant_bigit += HexCharValue(value[j]); |
166 | 0 | } |
167 | 0 | if (most_significant_bigit != 0) { |
168 | 0 | bigits_[used_digits_] = most_significant_bigit; |
169 | 0 | used_digits_++; |
170 | 0 | } |
171 | 0 | Clamp(); |
172 | 0 | } |
173 | | |
174 | | |
175 | 0 | void Bignum::AddUInt64(uint64_t operand) { |
176 | 0 | if (operand == 0) return; |
177 | 0 | Bignum other; |
178 | 0 | other.AssignUInt64(operand); |
179 | 0 | AddBignum(other); |
180 | 0 | } |
181 | | |
182 | | |
183 | 0 | void Bignum::AddBignum(const Bignum& other) { |
184 | 0 | ASSERT(IsClamped()); |
185 | 0 | ASSERT(other.IsClamped()); |
186 | 0 |
|
187 | 0 | // If this has a greater exponent than other append zero-bigits to this. |
188 | 0 | // After this call exponent_ <= other.exponent_. |
189 | 0 | Align(other); |
190 | 0 |
|
191 | 0 | // There are two possibilities: |
192 | 0 | // aaaaaaaaaaa 0000 (where the 0s represent a's exponent) |
193 | 0 | // bbbbb 00000000 |
194 | 0 | // ---------------- |
195 | 0 | // ccccccccccc 0000 |
196 | 0 | // or |
197 | 0 | // aaaaaaaaaa 0000 |
198 | 0 | // bbbbbbbbb 0000000 |
199 | 0 | // ----------------- |
200 | 0 | // cccccccccccc 0000 |
201 | 0 | // In both cases we might need a carry bigit. |
202 | 0 |
|
203 | 0 | EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_); |
204 | 0 | Chunk carry = 0; |
205 | 0 | int bigit_pos = other.exponent_ - exponent_; |
206 | 0 | ASSERT(bigit_pos >= 0); |
207 | 0 | for (int i = 0; i < other.used_digits_; ++i) { |
208 | 0 | Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry; |
209 | 0 | bigits_[bigit_pos] = sum & kBigitMask; |
210 | 0 | carry = sum >> kBigitSize; |
211 | 0 | bigit_pos++; |
212 | 0 | } |
213 | 0 |
|
214 | 0 | while (carry != 0) { |
215 | 0 | Chunk sum = bigits_[bigit_pos] + carry; |
216 | 0 | bigits_[bigit_pos] = sum & kBigitMask; |
217 | 0 | carry = sum >> kBigitSize; |
218 | 0 | bigit_pos++; |
219 | 0 | } |
220 | 0 | used_digits_ = Max(bigit_pos, used_digits_); |
221 | 0 | ASSERT(IsClamped()); |
222 | 0 | } |
223 | | |
224 | | |
225 | 0 | void Bignum::SubtractBignum(const Bignum& other) { |
226 | 0 | ASSERT(IsClamped()); |
227 | 0 | ASSERT(other.IsClamped()); |
228 | 0 | // We require this to be bigger than other. |
229 | 0 | ASSERT(LessEqual(other, *this)); |
230 | 0 |
|
231 | 0 | Align(other); |
232 | 0 |
|
233 | 0 | int offset = other.exponent_ - exponent_; |
234 | 0 | Chunk borrow = 0; |
235 | 0 | int i; |
236 | 0 | for (i = 0; i < other.used_digits_; ++i) { |
237 | 0 | ASSERT((borrow == 0) || (borrow == 1)); |
238 | 0 | Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow; |
239 | 0 | bigits_[i + offset] = difference & kBigitMask; |
240 | 0 | borrow = difference >> (kChunkSize - 1); |
241 | 0 | } |
242 | 0 | while (borrow != 0) { |
243 | 0 | Chunk difference = bigits_[i + offset] - borrow; |
244 | 0 | bigits_[i + offset] = difference & kBigitMask; |
245 | 0 | borrow = difference >> (kChunkSize - 1); |
246 | 0 | ++i; |
247 | 0 | } |
248 | 0 | Clamp(); |
249 | 0 | } |
250 | | |
251 | | |
252 | 0 | void Bignum::ShiftLeft(int shift_amount) { |
253 | 0 | if (used_digits_ == 0) return; |
254 | 0 | exponent_ += shift_amount / kBigitSize; |
255 | 0 | int local_shift = shift_amount % kBigitSize; |
256 | 0 | EnsureCapacity(used_digits_ + 1); |
257 | 0 | BigitsShiftLeft(local_shift); |
258 | 0 | } |
259 | | |
260 | | |
261 | 0 | void Bignum::MultiplyByUInt32(uint32_t factor) { |
262 | 0 | if (factor == 1) return; |
263 | 0 | if (factor == 0) { |
264 | 0 | Zero(); |
265 | 0 | return; |
266 | 0 | } |
267 | 0 | if (used_digits_ == 0) return; |
268 | 0 | |
269 | 0 | // The product of a bigit with the factor is of size kBigitSize + 32. |
270 | 0 | // Assert that this number + 1 (for the carry) fits into double chunk. |
271 | 0 | ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1); |
272 | 0 | DoubleChunk carry = 0; |
273 | 0 | for (int i = 0; i < used_digits_; ++i) { |
274 | 0 | DoubleChunk product = static_cast<DoubleChunk>(factor) * bigits_[i] + carry; |
275 | 0 | bigits_[i] = static_cast<Chunk>(product & kBigitMask); |
276 | 0 | carry = (product >> kBigitSize); |
277 | 0 | } |
278 | 0 | while (carry != 0) { |
279 | 0 | EnsureCapacity(used_digits_ + 1); |
280 | 0 | bigits_[used_digits_] = carry & kBigitMask; |
281 | 0 | used_digits_++; |
282 | 0 | carry >>= kBigitSize; |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | | |
287 | 0 | void Bignum::MultiplyByUInt64(uint64_t factor) { |
288 | 0 | if (factor == 1) return; |
289 | 0 | if (factor == 0) { |
290 | 0 | Zero(); |
291 | 0 | return; |
292 | 0 | } |
293 | 0 | ASSERT(kBigitSize < 32); |
294 | 0 | uint64_t carry = 0; |
295 | 0 | uint64_t low = factor & 0xFFFFFFFF; |
296 | 0 | uint64_t high = factor >> 32; |
297 | 0 | for (int i = 0; i < used_digits_; ++i) { |
298 | 0 | uint64_t product_low = low * bigits_[i]; |
299 | 0 | uint64_t product_high = high * bigits_[i]; |
300 | 0 | uint64_t tmp = (carry & kBigitMask) + product_low; |
301 | 0 | bigits_[i] = tmp & kBigitMask; |
302 | 0 | carry = (carry >> kBigitSize) + (tmp >> kBigitSize) + |
303 | 0 | (product_high << (32 - kBigitSize)); |
304 | 0 | } |
305 | 0 | while (carry != 0) { |
306 | 0 | EnsureCapacity(used_digits_ + 1); |
307 | 0 | bigits_[used_digits_] = carry & kBigitMask; |
308 | 0 | used_digits_++; |
309 | 0 | carry >>= kBigitSize; |
310 | 0 | } |
311 | 0 | } |
312 | | |
313 | | |
314 | 0 | void Bignum::MultiplyByPowerOfTen(int exponent) { |
315 | 0 | const uint64_t kFive27 = UINT64_2PART_C(0x6765c793, fa10079d); |
316 | 0 | const uint16_t kFive1 = 5; |
317 | 0 | const uint16_t kFive2 = kFive1 * 5; |
318 | 0 | const uint16_t kFive3 = kFive2 * 5; |
319 | 0 | const uint16_t kFive4 = kFive3 * 5; |
320 | 0 | const uint16_t kFive5 = kFive4 * 5; |
321 | 0 | const uint16_t kFive6 = kFive5 * 5; |
322 | 0 | const uint32_t kFive7 = kFive6 * 5; |
323 | 0 | const uint32_t kFive8 = kFive7 * 5; |
324 | 0 | const uint32_t kFive9 = kFive8 * 5; |
325 | 0 | const uint32_t kFive10 = kFive9 * 5; |
326 | 0 | const uint32_t kFive11 = kFive10 * 5; |
327 | 0 | const uint32_t kFive12 = kFive11 * 5; |
328 | 0 | const uint32_t kFive13 = kFive12 * 5; |
329 | 0 | const uint32_t kFive1_to_12[] = |
330 | 0 | { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6, |
331 | 0 | kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 }; |
332 | 0 |
|
333 | 0 | ASSERT(exponent >= 0); |
334 | 0 | if (exponent == 0) return; |
335 | 0 | if (used_digits_ == 0) return; |
336 | 0 | |
337 | 0 | // We shift by exponent at the end just before returning. |
338 | 0 | int remaining_exponent = exponent; |
339 | 0 | while (remaining_exponent >= 27) { |
340 | 0 | MultiplyByUInt64(kFive27); |
341 | 0 | remaining_exponent -= 27; |
342 | 0 | } |
343 | 0 | while (remaining_exponent >= 13) { |
344 | 0 | MultiplyByUInt32(kFive13); |
345 | 0 | remaining_exponent -= 13; |
346 | 0 | } |
347 | 0 | if (remaining_exponent > 0) { |
348 | 0 | MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]); |
349 | 0 | } |
350 | 0 | ShiftLeft(exponent); |
351 | 0 | } |
352 | | |
353 | | |
354 | 0 | void Bignum::Square() { |
355 | 0 | ASSERT(IsClamped()); |
356 | 0 | int product_length = 2 * used_digits_; |
357 | 0 | EnsureCapacity(product_length); |
358 | 0 |
|
359 | 0 | // Comba multiplication: compute each column separately. |
360 | 0 | // Example: r = a2a1a0 * b2b1b0. |
361 | 0 | // r = 1 * a0b0 + |
362 | 0 | // 10 * (a1b0 + a0b1) + |
363 | 0 | // 100 * (a2b0 + a1b1 + a0b2) + |
364 | 0 | // 1000 * (a2b1 + a1b2) + |
365 | 0 | // 10000 * a2b2 |
366 | 0 | // |
367 | 0 | // In the worst case we have to accumulate nb-digits products of digit*digit. |
368 | 0 | // |
369 | 0 | // Assert that the additional number of bits in a DoubleChunk are enough to |
370 | 0 | // sum up used_digits of Bigit*Bigit. |
371 | 0 | if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) { |
372 | 0 | UNIMPLEMENTED(); |
373 | 0 | } |
374 | 0 | DoubleChunk accumulator = 0; |
375 | 0 | // First shift the digits so we don't overwrite them. |
376 | 0 | int copy_offset = used_digits_; |
377 | 0 | for (int i = 0; i < used_digits_; ++i) { |
378 | 0 | bigits_[copy_offset + i] = bigits_[i]; |
379 | 0 | } |
380 | 0 | // We have two loops to avoid some 'if's in the loop. |
381 | 0 | for (int i = 0; i < used_digits_; ++i) { |
382 | 0 | // Process temporary digit i with power i. |
383 | 0 | // The sum of the two indices must be equal to i. |
384 | 0 | int bigit_index1 = i; |
385 | 0 | int bigit_index2 = 0; |
386 | 0 | // Sum all of the sub-products. |
387 | 0 | while (bigit_index1 >= 0) { |
388 | 0 | Chunk chunk1 = bigits_[copy_offset + bigit_index1]; |
389 | 0 | Chunk chunk2 = bigits_[copy_offset + bigit_index2]; |
390 | 0 | accumulator += static_cast<DoubleChunk>(chunk1) * chunk2; |
391 | 0 | bigit_index1--; |
392 | 0 | bigit_index2++; |
393 | 0 | } |
394 | 0 | bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask; |
395 | 0 | accumulator >>= kBigitSize; |
396 | 0 | } |
397 | 0 | for (int i = used_digits_; i < product_length; ++i) { |
398 | 0 | int bigit_index1 = used_digits_ - 1; |
399 | 0 | int bigit_index2 = i - bigit_index1; |
400 | 0 | // Invariant: sum of both indices is again equal to i. |
401 | 0 | // Inner loop runs 0 times on last iteration, emptying accumulator. |
402 | 0 | while (bigit_index2 < used_digits_) { |
403 | 0 | Chunk chunk1 = bigits_[copy_offset + bigit_index1]; |
404 | 0 | Chunk chunk2 = bigits_[copy_offset + bigit_index2]; |
405 | 0 | accumulator += static_cast<DoubleChunk>(chunk1) * chunk2; |
406 | 0 | bigit_index1--; |
407 | 0 | bigit_index2++; |
408 | 0 | } |
409 | 0 | // The overwritten bigits_[i] will never be read in further loop iterations, |
410 | 0 | // because bigit_index1 and bigit_index2 are always greater |
411 | 0 | // than i - used_digits_. |
412 | 0 | bigits_[i] = static_cast<Chunk>(accumulator) & kBigitMask; |
413 | 0 | accumulator >>= kBigitSize; |
414 | 0 | } |
415 | 0 | // Since the result was guaranteed to lie inside the number the |
416 | 0 | // accumulator must be 0 now. |
417 | 0 | ASSERT(accumulator == 0); |
418 | 0 |
|
419 | 0 | // Don't forget to update the used_digits and the exponent. |
420 | 0 | used_digits_ = product_length; |
421 | 0 | exponent_ *= 2; |
422 | 0 | Clamp(); |
423 | 0 | } |
424 | | |
425 | | |
426 | 0 | void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) { |
427 | 0 | ASSERT(base != 0); |
428 | 0 | ASSERT(power_exponent >= 0); |
429 | 0 | if (power_exponent == 0) { |
430 | 0 | AssignUInt16(1); |
431 | 0 | return; |
432 | 0 | } |
433 | 0 | Zero(); |
434 | 0 | int shifts = 0; |
435 | 0 | // We expect base to be in range 2-32, and most often to be 10. |
436 | 0 | // It does not make much sense to implement different algorithms for counting |
437 | 0 | // the bits. |
438 | 0 | while ((base & 1) == 0) { |
439 | 0 | base >>= 1; |
440 | 0 | shifts++; |
441 | 0 | } |
442 | 0 | int bit_size = 0; |
443 | 0 | int tmp_base = base; |
444 | 0 | while (tmp_base != 0) { |
445 | 0 | tmp_base >>= 1; |
446 | 0 | bit_size++; |
447 | 0 | } |
448 | 0 | int final_size = bit_size * power_exponent; |
449 | 0 | // 1 extra bigit for the shifting, and one for rounded final_size. |
450 | 0 | EnsureCapacity(final_size / kBigitSize + 2); |
451 | 0 |
|
452 | 0 | // Left to Right exponentiation. |
453 | 0 | int mask = 1; |
454 | 0 | while (power_exponent >= mask) mask <<= 1; |
455 | 0 |
|
456 | 0 | // The mask is now pointing to the bit above the most significant 1-bit of |
457 | 0 | // power_exponent. |
458 | 0 | // Get rid of first 1-bit; |
459 | 0 | mask >>= 2; |
460 | 0 | uint64_t this_value = base; |
461 | 0 |
|
462 | 0 | bool delayed_multipliciation = false; |
463 | 0 | const uint64_t max_32bits = 0xFFFFFFFF; |
464 | 0 | while (mask != 0 && this_value <= max_32bits) { |
465 | 0 | this_value = this_value * this_value; |
466 | 0 | // Verify that there is enough space in this_value to perform the |
467 | 0 | // multiplication. The first bit_size bits must be 0. |
468 | 0 | if ((power_exponent & mask) != 0) { |
469 | 0 | uint64_t base_bits_mask = |
470 | 0 | ~((static_cast<uint64_t>(1) << (64 - bit_size)) - 1); |
471 | 0 | bool high_bits_zero = (this_value & base_bits_mask) == 0; |
472 | 0 | if (high_bits_zero) { |
473 | 0 | this_value *= base; |
474 | 0 | } else { |
475 | 0 | delayed_multipliciation = true; |
476 | 0 | } |
477 | 0 | } |
478 | 0 | mask >>= 1; |
479 | 0 | } |
480 | 0 | AssignUInt64(this_value); |
481 | 0 | if (delayed_multipliciation) { |
482 | 0 | MultiplyByUInt32(base); |
483 | 0 | } |
484 | 0 |
|
485 | 0 | // Now do the same thing as a bignum. |
486 | 0 | while (mask != 0) { |
487 | 0 | Square(); |
488 | 0 | if ((power_exponent & mask) != 0) { |
489 | 0 | MultiplyByUInt32(base); |
490 | 0 | } |
491 | 0 | mask >>= 1; |
492 | 0 | } |
493 | 0 |
|
494 | 0 | // And finally add the saved shifts. |
495 | 0 | ShiftLeft(shifts * power_exponent); |
496 | 0 | } |
497 | | |
498 | | |
499 | | // Precondition: this/other < 16bit. |
500 | 0 | uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) { |
501 | 0 | ASSERT(IsClamped()); |
502 | 0 | ASSERT(other.IsClamped()); |
503 | 0 | ASSERT(other.used_digits_ > 0); |
504 | 0 |
|
505 | 0 | // Easy case: if we have less digits than the divisor than the result is 0. |
506 | 0 | // Note: this handles the case where this == 0, too. |
507 | 0 | if (BigitLength() < other.BigitLength()) { |
508 | 0 | return 0; |
509 | 0 | } |
510 | 0 | |
511 | 0 | Align(other); |
512 | 0 |
|
513 | 0 | uint16_t result = 0; |
514 | 0 |
|
515 | 0 | // Start by removing multiples of 'other' until both numbers have the same |
516 | 0 | // number of digits. |
517 | 0 | while (BigitLength() > other.BigitLength()) { |
518 | 0 | // This naive approach is extremely inefficient if `this` divided by other |
519 | 0 | // is big. This function is implemented for doubleToString where |
520 | 0 | // the result should be small (less than 10). |
521 | 0 | ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16)); |
522 | 0 | ASSERT(bigits_[used_digits_ - 1] < 0x10000); |
523 | 0 | // Remove the multiples of the first digit. |
524 | 0 | // Example this = 23 and other equals 9. -> Remove 2 multiples. |
525 | 0 | result += static_cast<uint16_t>(bigits_[used_digits_ - 1]); |
526 | 0 | SubtractTimes(other, bigits_[used_digits_ - 1]); |
527 | 0 | } |
528 | 0 |
|
529 | 0 | ASSERT(BigitLength() == other.BigitLength()); |
530 | 0 |
|
531 | 0 | // Both bignums are at the same length now. |
532 | 0 | // Since other has more than 0 digits we know that the access to |
533 | 0 | // bigits_[used_digits_ - 1] is safe. |
534 | 0 | Chunk this_bigit = bigits_[used_digits_ - 1]; |
535 | 0 | Chunk other_bigit = other.bigits_[other.used_digits_ - 1]; |
536 | 0 |
|
537 | 0 | if (other.used_digits_ == 1) { |
538 | 0 | // Shortcut for easy (and common) case. |
539 | 0 | int quotient = this_bigit / other_bigit; |
540 | 0 | bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient; |
541 | 0 | ASSERT(quotient < 0x10000); |
542 | 0 | result += static_cast<uint16_t>(quotient); |
543 | 0 | Clamp(); |
544 | 0 | return result; |
545 | 0 | } |
546 | 0 | |
547 | 0 | int division_estimate = this_bigit / (other_bigit + 1); |
548 | 0 | ASSERT(division_estimate < 0x10000); |
549 | 0 | result += static_cast<uint16_t>(division_estimate); |
550 | 0 | SubtractTimes(other, division_estimate); |
551 | 0 |
|
552 | 0 | if (other_bigit * (division_estimate + 1) > this_bigit) { |
553 | 0 | // No need to even try to subtract. Even if other's remaining digits were 0 |
554 | 0 | // another subtraction would be too much. |
555 | 0 | return result; |
556 | 0 | } |
557 | 0 | |
558 | 0 | while (LessEqual(other, *this)) { |
559 | 0 | SubtractBignum(other); |
560 | 0 | result++; |
561 | 0 | } |
562 | 0 | return result; |
563 | 0 | } |
564 | | |
565 | | |
566 | | template<typename S> |
567 | 0 | static int SizeInHexChars(S number) { |
568 | 0 | ASSERT(number > 0); |
569 | 0 | int result = 0; |
570 | 0 | while (number != 0) { |
571 | 0 | number >>= 4; |
572 | 0 | result++; |
573 | 0 | } |
574 | 0 | return result; |
575 | 0 | } |
576 | | |
577 | | |
578 | 0 | static char HexCharOfValue(int value) { |
579 | 0 | ASSERT(0 <= value && value <= 16); |
580 | 0 | if (value < 10) return static_cast<char>(value + '0'); |
581 | 0 | return static_cast<char>(value - 10 + 'A'); |
582 | 0 | } |
583 | | |
584 | | |
585 | 0 | bool Bignum::ToHexString(char* buffer, int buffer_size) const { |
586 | 0 | ASSERT(IsClamped()); |
587 | 0 | // Each bigit must be printable as separate hex-character. |
588 | 0 | ASSERT(kBigitSize % 4 == 0); |
589 | 0 | const int kHexCharsPerBigit = kBigitSize / 4; |
590 | 0 |
|
591 | 0 | if (used_digits_ == 0) { |
592 | 0 | if (buffer_size < 2) return false; |
593 | 0 | buffer[0] = '0'; |
594 | 0 | buffer[1] = '\0'; |
595 | 0 | return true; |
596 | 0 | } |
597 | 0 | // We add 1 for the terminating '\0' character. |
598 | 0 | int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit + |
599 | 0 | SizeInHexChars(bigits_[used_digits_ - 1]) + 1; |
600 | 0 | if (needed_chars > buffer_size) return false; |
601 | 0 | int string_index = needed_chars - 1; |
602 | 0 | buffer[string_index--] = '\0'; |
603 | 0 | for (int i = 0; i < exponent_; ++i) { |
604 | 0 | for (int j = 0; j < kHexCharsPerBigit; ++j) { |
605 | 0 | buffer[string_index--] = '0'; |
606 | 0 | } |
607 | 0 | } |
608 | 0 | for (int i = 0; i < used_digits_ - 1; ++i) { |
609 | 0 | Chunk current_bigit = bigits_[i]; |
610 | 0 | for (int j = 0; j < kHexCharsPerBigit; ++j) { |
611 | 0 | buffer[string_index--] = HexCharOfValue(current_bigit & 0xF); |
612 | 0 | current_bigit >>= 4; |
613 | 0 | } |
614 | 0 | } |
615 | 0 | // And finally the last bigit. |
616 | 0 | Chunk most_significant_bigit = bigits_[used_digits_ - 1]; |
617 | 0 | while (most_significant_bigit != 0) { |
618 | 0 | buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF); |
619 | 0 | most_significant_bigit >>= 4; |
620 | 0 | } |
621 | 0 | return true; |
622 | 0 | } |
623 | | |
624 | | |
625 | 0 | Bignum::Chunk Bignum::BigitAt(int index) const { |
626 | 0 | if (index >= BigitLength()) return 0; |
627 | 0 | if (index < exponent_) return 0; |
628 | 0 | return bigits_[index - exponent_]; |
629 | 0 | } |
630 | | |
631 | | |
632 | 0 | int Bignum::Compare(const Bignum& a, const Bignum& b) { |
633 | 0 | ASSERT(a.IsClamped()); |
634 | 0 | ASSERT(b.IsClamped()); |
635 | 0 | int bigit_length_a = a.BigitLength(); |
636 | 0 | int bigit_length_b = b.BigitLength(); |
637 | 0 | if (bigit_length_a < bigit_length_b) return -1; |
638 | 0 | if (bigit_length_a > bigit_length_b) return +1; |
639 | 0 | for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) { |
640 | 0 | Chunk bigit_a = a.BigitAt(i); |
641 | 0 | Chunk bigit_b = b.BigitAt(i); |
642 | 0 | if (bigit_a < bigit_b) return -1; |
643 | 0 | if (bigit_a > bigit_b) return +1; |
644 | 0 | // Otherwise they are equal up to this digit. Try the next digit. |
645 | 0 | } |
646 | 0 | return 0; |
647 | 0 | } |
648 | | |
649 | | |
650 | 0 | int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) { |
651 | 0 | ASSERT(a.IsClamped()); |
652 | 0 | ASSERT(b.IsClamped()); |
653 | 0 | ASSERT(c.IsClamped()); |
654 | 0 | if (a.BigitLength() < b.BigitLength()) { |
655 | 0 | return PlusCompare(b, a, c); |
656 | 0 | } |
657 | 0 | if (a.BigitLength() + 1 < c.BigitLength()) return -1; |
658 | 0 | if (a.BigitLength() > c.BigitLength()) return +1; |
659 | 0 | // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than |
660 | 0 | // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one |
661 | 0 | // of 'a'. |
662 | 0 | if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) { |
663 | 0 | return -1; |
664 | 0 | } |
665 | 0 | |
666 | 0 | Chunk borrow = 0; |
667 | 0 | // Starting at min_exponent all digits are == 0. So no need to compare them. |
668 | 0 | int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_); |
669 | 0 | for (int i = c.BigitLength() - 1; i >= min_exponent; --i) { |
670 | 0 | Chunk chunk_a = a.BigitAt(i); |
671 | 0 | Chunk chunk_b = b.BigitAt(i); |
672 | 0 | Chunk chunk_c = c.BigitAt(i); |
673 | 0 | Chunk sum = chunk_a + chunk_b; |
674 | 0 | if (sum > chunk_c + borrow) { |
675 | 0 | return +1; |
676 | 0 | } else { |
677 | 0 | borrow = chunk_c + borrow - sum; |
678 | 0 | if (borrow > 1) return -1; |
679 | 0 | borrow <<= kBigitSize; |
680 | 0 | } |
681 | 0 | } |
682 | 0 | if (borrow == 0) return 0; |
683 | 0 | return -1; |
684 | 0 | } |
685 | | |
686 | | |
687 | 0 | void Bignum::Clamp() { |
688 | 0 | while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) { |
689 | 0 | used_digits_--; |
690 | 0 | } |
691 | 0 | if (used_digits_ == 0) { |
692 | 0 | // Zero. |
693 | 0 | exponent_ = 0; |
694 | 0 | } |
695 | 0 | } |
696 | | |
697 | | |
698 | 0 | bool Bignum::IsClamped() const { |
699 | 0 | return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0; |
700 | 0 | } |
701 | | |
702 | | |
703 | 0 | void Bignum::Zero() { |
704 | 0 | for (int i = 0; i < used_digits_; ++i) { |
705 | 0 | bigits_[i] = 0; |
706 | 0 | } |
707 | 0 | used_digits_ = 0; |
708 | 0 | exponent_ = 0; |
709 | 0 | } |
710 | | |
711 | | |
712 | 0 | void Bignum::Align(const Bignum& other) { |
713 | 0 | if (exponent_ > other.exponent_) { |
714 | 0 | // If "X" represents a "hidden" digit (by the exponent) then we are in the |
715 | 0 | // following case (a == this, b == other): |
716 | 0 | // a: aaaaaaXXXX or a: aaaaaXXX |
717 | 0 | // b: bbbbbbX b: bbbbbbbbXX |
718 | 0 | // We replace some of the hidden digits (X) of a with 0 digits. |
719 | 0 | // a: aaaaaa000X or a: aaaaa0XX |
720 | 0 | int zero_digits = exponent_ - other.exponent_; |
721 | 0 | EnsureCapacity(used_digits_ + zero_digits); |
722 | 0 | for (int i = used_digits_ - 1; i >= 0; --i) { |
723 | 0 | bigits_[i + zero_digits] = bigits_[i]; |
724 | 0 | } |
725 | 0 | for (int i = 0; i < zero_digits; ++i) { |
726 | 0 | bigits_[i] = 0; |
727 | 0 | } |
728 | 0 | used_digits_ += zero_digits; |
729 | 0 | exponent_ -= zero_digits; |
730 | 0 | ASSERT(used_digits_ >= 0); |
731 | 0 | ASSERT(exponent_ >= 0); |
732 | 0 | } |
733 | 0 | } |
734 | | |
735 | | |
736 | 0 | void Bignum::BigitsShiftLeft(int shift_amount) { |
737 | 0 | ASSERT(shift_amount < kBigitSize); |
738 | 0 | ASSERT(shift_amount >= 0); |
739 | 0 | Chunk carry = 0; |
740 | 0 | for (int i = 0; i < used_digits_; ++i) { |
741 | 0 | Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount); |
742 | 0 | bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask; |
743 | 0 | carry = new_carry; |
744 | 0 | } |
745 | 0 | if (carry != 0) { |
746 | 0 | bigits_[used_digits_] = carry; |
747 | 0 | used_digits_++; |
748 | 0 | } |
749 | 0 | } |
750 | | |
751 | | |
752 | 0 | void Bignum::SubtractTimes(const Bignum& other, int factor) { |
753 | 0 | ASSERT(exponent_ <= other.exponent_); |
754 | 0 | if (factor < 3) { |
755 | 0 | for (int i = 0; i < factor; ++i) { |
756 | 0 | SubtractBignum(other); |
757 | 0 | } |
758 | 0 | return; |
759 | 0 | } |
760 | 0 | Chunk borrow = 0; |
761 | 0 | int exponent_diff = other.exponent_ - exponent_; |
762 | 0 | for (int i = 0; i < other.used_digits_; ++i) { |
763 | 0 | DoubleChunk product = static_cast<DoubleChunk>(factor) * other.bigits_[i]; |
764 | 0 | DoubleChunk remove = borrow + product; |
765 | 0 | Chunk difference = bigits_[i + exponent_diff] - (remove & kBigitMask); |
766 | 0 | bigits_[i + exponent_diff] = difference & kBigitMask; |
767 | 0 | borrow = static_cast<Chunk>((difference >> (kChunkSize - 1)) + |
768 | 0 | (remove >> kBigitSize)); |
769 | 0 | } |
770 | 0 | for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) { |
771 | 0 | if (borrow == 0) return; |
772 | 0 | Chunk difference = bigits_[i] - borrow; |
773 | 0 | bigits_[i] = difference & kBigitMask; |
774 | 0 | borrow = difference >> (kChunkSize - 1); |
775 | 0 | } |
776 | 0 | Clamp(); |
777 | 0 | } |
778 | | |
779 | | |
780 | | } // namespace double_conversion |
781 | | |
782 | | // ICU PATCH: Close ICU namespace |
783 | | U_NAMESPACE_END |
784 | | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING |