mfbt/double-conversion/bignum.cc

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial