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