Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:27

0001 // Copyright 2010 the V8 project authors. All rights reserved.
0002 // Redistribution and use in source and binary forms, with or without
0003 // modification, are permitted provided that the following conditions are
0004 // met:
0005 //
0006 //     * Redistributions of source code must retain the above copyright
0007 //       notice, this list of conditions and the following disclaimer.
0008 //     * Redistributions in binary form must reproduce the above
0009 //       copyright notice, this list of conditions and the following
0010 //       disclaimer in the documentation and/or other materials provided
0011 //       with the distribution.
0012 //     * Neither the name of Google Inc. nor the names of its
0013 //       contributors may be used to endorse or promote products derived
0014 //       from this software without specific prior written permission.
0015 //
0016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027 
0028 #ifndef DOUBLE_CONVERSION_BIGNUM_H_
0029 #define DOUBLE_CONVERSION_BIGNUM_H_
0030 
0031 #include "utils.h"
0032 
0033 namespace double_conversion {
0034 
0035 class Bignum {
0036  public:
0037   // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately.
0038   // This bignum can encode much bigger numbers, since it contains an
0039   // exponent.
0040   static const int kMaxSignificantBits = 3584;
0041 
0042   Bignum() : used_bigits_(0), exponent_(0) {}
0043 
0044   void AssignUInt16(const uint16_t value);
0045   void AssignUInt64(uint64_t value);
0046   void AssignBignum(const Bignum& other);
0047 
0048   void AssignDecimalString(const Vector<const char> value);
0049   void AssignHexString(const Vector<const char> value);
0050 
0051   void AssignPowerUInt16(uint16_t base, const int exponent);
0052 
0053   void AddUInt64(const uint64_t operand);
0054   void AddBignum(const Bignum& other);
0055   // Precondition: this >= other.
0056   void SubtractBignum(const Bignum& other);
0057 
0058   void Square();
0059   void ShiftLeft(const int shift_amount);
0060   void MultiplyByUInt32(const uint32_t factor);
0061   void MultiplyByUInt64(const uint64_t factor);
0062   void MultiplyByPowerOfTen(const int exponent);
0063   void Times10() { return MultiplyByUInt32(10); }
0064   // Pseudocode:
0065   //  int result = this / other;
0066   //  this = this % other;
0067   // In the worst case this function is in O(this/other).
0068   uint16_t DivideModuloIntBignum(const Bignum& other);
0069 
0070   bool ToHexString(char* buffer, const int buffer_size) const;
0071 
0072   // Returns
0073   //  -1 if a < b,
0074   //   0 if a == b, and
0075   //  +1 if a > b.
0076   static int Compare(const Bignum& a, const Bignum& b);
0077   static bool Equal(const Bignum& a, const Bignum& b) {
0078     return Compare(a, b) == 0;
0079   }
0080   static bool LessEqual(const Bignum& a, const Bignum& b) {
0081     return Compare(a, b) <= 0;
0082   }
0083   static bool Less(const Bignum& a, const Bignum& b) {
0084     return Compare(a, b) < 0;
0085   }
0086   // Returns Compare(a + b, c);
0087   static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c);
0088   // Returns a + b == c
0089   static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
0090     return PlusCompare(a, b, c) == 0;
0091   }
0092   // Returns a + b <= c
0093   static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
0094     return PlusCompare(a, b, c) <= 0;
0095   }
0096   // Returns a + b < c
0097   static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) {
0098     return PlusCompare(a, b, c) < 0;
0099   }
0100  private:
0101   typedef uint32_t Chunk;
0102   typedef uint64_t DoubleChunk;
0103 
0104   static const int kChunkSize = sizeof(Chunk) * 8;
0105   static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8;
0106   // With bigit size of 28 we loose some bits, but a double still fits easily
0107   // into two chunks, and more importantly we can use the Comba multiplication.
0108   static const int kBigitSize = 28;
0109   static const Chunk kBigitMask = (1 << kBigitSize) - 1;
0110   // Every instance allocates kBigitLength chunks on the stack. Bignums cannot
0111   // grow. There are no checks if the stack-allocated space is sufficient.
0112   static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
0113 
0114   static void EnsureCapacity(const int size) {
0115     if (size > kBigitCapacity) {
0116       DOUBLE_CONVERSION_UNREACHABLE();
0117     }
0118   }
0119   void Align(const Bignum& other);
0120   void Clamp();
0121   bool IsClamped() const {
0122     return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0;
0123   }
0124   void Zero() {
0125     used_bigits_ = 0;
0126     exponent_ = 0;
0127   }
0128   // Requires this to have enough capacity (no tests done).
0129   // Updates used_bigits_ if necessary.
0130   // shift_amount must be < kBigitSize.
0131   void BigitsShiftLeft(const int shift_amount);
0132   // BigitLength includes the "hidden" bigits encoded in the exponent.
0133   int BigitLength() const { return used_bigits_ + exponent_; }
0134   Chunk& RawBigit(const int index);
0135   const Chunk& RawBigit(const int index) const;
0136   Chunk BigitOrZero(const int index) const;
0137   void SubtractTimes(const Bignum& other, const int factor);
0138 
0139   // The Bignum's value is value(bigits_buffer_) * 2^(exponent_ * kBigitSize),
0140   // where the value of the buffer consists of the lower kBigitSize bits of
0141   // the first used_bigits_ Chunks in bigits_buffer_, first chunk has lowest
0142   // significant bits.
0143   int16_t used_bigits_;
0144   int16_t exponent_;
0145   Chunk bigits_buffer_[kBigitCapacity];
0146 
0147   DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum);
0148 };
0149 
0150 }  // namespace double_conversion
0151 
0152 #endif  // DOUBLE_CONVERSION_BIGNUM_H_