Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:14

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 arrow_vendored {
0034 namespace double_conversion {
0035 
0036 class Bignum {
0037  public:
0038   // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately.
0039   // This bignum can encode much bigger numbers, since it contains an
0040   // exponent.
0041   static const int kMaxSignificantBits = 3584;
0042 
0043   Bignum() : used_bigits_(0), exponent_(0) {}
0044 
0045   void AssignUInt16(const uint16_t value);
0046   void AssignUInt64(uint64_t value);
0047   void AssignBignum(const Bignum& other);
0048 
0049   void AssignDecimalString(const Vector<const char> value);
0050   void AssignHexString(const Vector<const char> value);
0051 
0052   void AssignPowerUInt16(uint16_t base, const int exponent);
0053 
0054   void AddUInt64(const uint64_t operand);
0055   void AddBignum(const Bignum& other);
0056   // Precondition: this >= other.
0057   void SubtractBignum(const Bignum& other);
0058 
0059   void Square();
0060   void ShiftLeft(const int shift_amount);
0061   void MultiplyByUInt32(const uint32_t factor);
0062   void MultiplyByUInt64(const uint64_t factor);
0063   void MultiplyByPowerOfTen(const int exponent);
0064   void Times10() { return MultiplyByUInt32(10); }
0065   // Pseudocode:
0066   //  int result = this / other;
0067   //  this = this % other;
0068   // In the worst case this function is in O(this/other).
0069   uint16_t DivideModuloIntBignum(const Bignum& other);
0070 
0071   bool ToHexString(char* buffer, const int buffer_size) const;
0072 
0073   // Returns
0074   //  -1 if a < b,
0075   //   0 if a == b, and
0076   //  +1 if a > b.
0077   static int Compare(const Bignum& a, const Bignum& b);
0078   static bool Equal(const Bignum& a, const Bignum& b) {
0079     return Compare(a, b) == 0;
0080   }
0081   static bool LessEqual(const Bignum& a, const Bignum& b) {
0082     return Compare(a, b) <= 0;
0083   }
0084   static bool Less(const Bignum& a, const Bignum& b) {
0085     return Compare(a, b) < 0;
0086   }
0087   // Returns Compare(a + b, c);
0088   static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c);
0089   // Returns a + b == c
0090   static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
0091     return PlusCompare(a, b, c) == 0;
0092   }
0093   // Returns a + b <= c
0094   static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) {
0095     return PlusCompare(a, b, c) <= 0;
0096   }
0097   // Returns a + b < c
0098   static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) {
0099     return PlusCompare(a, b, c) < 0;
0100   }
0101  private:
0102   typedef uint32_t Chunk;
0103   typedef uint64_t DoubleChunk;
0104 
0105   static const int kChunkSize = sizeof(Chunk) * 8;
0106   static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8;
0107   // With bigit size of 28 we loose some bits, but a double still fits easily
0108   // into two chunks, and more importantly we can use the Comba multiplication.
0109   static const int kBigitSize = 28;
0110   static const Chunk kBigitMask = (1 << kBigitSize) - 1;
0111   // Every instance allocates kBigitLength chunks on the stack. Bignums cannot
0112   // grow. There are no checks if the stack-allocated space is sufficient.
0113   static const int kBigitCapacity = kMaxSignificantBits / kBigitSize;
0114 
0115   static void EnsureCapacity(const int size) {
0116     if (size > kBigitCapacity) {
0117       DOUBLE_CONVERSION_UNREACHABLE();
0118     }
0119   }
0120   void Align(const Bignum& other);
0121   void Clamp();
0122   bool IsClamped() const {
0123     return used_bigits_ == 0 || RawBigit(used_bigits_ - 1) != 0;
0124   }
0125   void Zero() {
0126     used_bigits_ = 0;
0127     exponent_ = 0;
0128   }
0129   // Requires this to have enough capacity (no tests done).
0130   // Updates used_bigits_ if necessary.
0131   // shift_amount must be < kBigitSize.
0132   void BigitsShiftLeft(const int shift_amount);
0133   // BigitLength includes the "hidden" bigits encoded in the exponent.
0134   int BigitLength() const { return used_bigits_ + exponent_; }
0135   Chunk& RawBigit(const int index);
0136   const Chunk& RawBigit(const int index) const;
0137   Chunk BigitOrZero(const int index) const;
0138   void SubtractTimes(const Bignum& other, const int factor);
0139 
0140   // The Bignum's value is value(bigits_buffer_) * 2^(exponent_ * kBigitSize),
0141   // where the value of the buffer consists of the lower kBigitSize bits of
0142   // the first used_bigits_ Chunks in bigits_buffer_, first chunk has lowest
0143   // significant bits.
0144   int16_t used_bigits_;
0145   int16_t exponent_;
0146   Chunk bigits_buffer_[kBigitCapacity];
0147 
0148   DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Bignum);
0149 };
0150 
0151 }  // namespace double_conversion
0152 }  // namespace arrow_vendored
0153 
0154 #endif  // DOUBLE_CONVERSION_BIGNUM_H_