![]() |
|
|||
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_DIY_FP_H_ 0029 #define DOUBLE_CONVERSION_DIY_FP_H_ 0030 0031 #include "utils.h" 0032 0033 namespace arrow_vendored { 0034 namespace double_conversion { 0035 0036 // This "Do It Yourself Floating Point" class implements a floating-point number 0037 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will 0038 // have the most significant bit of the significand set. 0039 // Multiplication and Subtraction do not normalize their results. 0040 // DiyFp store only non-negative numbers and are not designed to contain special 0041 // doubles (NaN and Infinity). 0042 class DiyFp { 0043 public: 0044 static const int kSignificandSize = 64; 0045 0046 DiyFp() : f_(0), e_(0) {} 0047 DiyFp(const uint64_t significand, const int32_t exponent) : f_(significand), e_(exponent) {} 0048 0049 // this -= other. 0050 // The exponents of both numbers must be the same and the significand of this 0051 // must be greater or equal than the significand of other. 0052 // The result will not be normalized. 0053 void Subtract(const DiyFp& other) { 0054 DOUBLE_CONVERSION_ASSERT(e_ == other.e_); 0055 DOUBLE_CONVERSION_ASSERT(f_ >= other.f_); 0056 f_ -= other.f_; 0057 } 0058 0059 // Returns a - b. 0060 // The exponents of both numbers must be the same and a must be greater 0061 // or equal than b. The result will not be normalized. 0062 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { 0063 DiyFp result = a; 0064 result.Subtract(b); 0065 return result; 0066 } 0067 0068 // this *= other. 0069 void Multiply(const DiyFp& other) { 0070 // Simply "emulates" a 128 bit multiplication. 0071 // However: the resulting number only contains 64 bits. The least 0072 // significant 64 bits are only used for rounding the most significant 64 0073 // bits. 0074 const uint64_t kM32 = 0xFFFFFFFFU; 0075 const uint64_t a = f_ >> 32; 0076 const uint64_t b = f_ & kM32; 0077 const uint64_t c = other.f_ >> 32; 0078 const uint64_t d = other.f_ & kM32; 0079 const uint64_t ac = a * c; 0080 const uint64_t bc = b * c; 0081 const uint64_t ad = a * d; 0082 const uint64_t bd = b * d; 0083 // By adding 1U << 31 to tmp we round the final result. 0084 // Halfway cases will be rounded up. 0085 const uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32) + (1U << 31); 0086 e_ += other.e_ + 64; 0087 f_ = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32); 0088 } 0089 0090 // returns a * b; 0091 static DiyFp Times(const DiyFp& a, const DiyFp& b) { 0092 DiyFp result = a; 0093 result.Multiply(b); 0094 return result; 0095 } 0096 0097 void Normalize() { 0098 DOUBLE_CONVERSION_ASSERT(f_ != 0); 0099 uint64_t significand = f_; 0100 int32_t exponent = e_; 0101 0102 // This method is mainly called for normalizing boundaries. In general, 0103 // boundaries need to be shifted by 10 bits, and we optimize for this case. 0104 const uint64_t k10MSBits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000); 0105 while ((significand & k10MSBits) == 0) { 0106 significand <<= 10; 0107 exponent -= 10; 0108 } 0109 while ((significand & kUint64MSB) == 0) { 0110 significand <<= 1; 0111 exponent--; 0112 } 0113 f_ = significand; 0114 e_ = exponent; 0115 } 0116 0117 static DiyFp Normalize(const DiyFp& a) { 0118 DiyFp result = a; 0119 result.Normalize(); 0120 return result; 0121 } 0122 0123 uint64_t f() const { return f_; } 0124 int32_t e() const { return e_; } 0125 0126 void set_f(uint64_t new_value) { f_ = new_value; } 0127 void set_e(int32_t new_value) { e_ = new_value; } 0128 0129 private: 0130 static const uint64_t kUint64MSB = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000); 0131 0132 uint64_t f_; 0133 int32_t e_; 0134 }; 0135 0136 } // namespace double_conversion 0137 } // namespace arrow_vendored 0138 0139 #endif // DOUBLE_CONVERSION_DIY_FP_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |