1*0e209d39SAndroid Build Coastguard Worker // © 2018 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker // 4*0e209d39SAndroid Build Coastguard Worker // From the double-conversion library. Original license: 5*0e209d39SAndroid Build Coastguard Worker // 6*0e209d39SAndroid Build Coastguard Worker // Copyright 2010 the V8 project authors. All rights reserved. 7*0e209d39SAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without 8*0e209d39SAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions are 9*0e209d39SAndroid Build Coastguard Worker // met: 10*0e209d39SAndroid Build Coastguard Worker // 11*0e209d39SAndroid Build Coastguard Worker // * Redistributions of source code must retain the above copyright 12*0e209d39SAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer. 13*0e209d39SAndroid Build Coastguard Worker // * Redistributions in binary form must reproduce the above 14*0e209d39SAndroid Build Coastguard Worker // copyright notice, this list of conditions and the following 15*0e209d39SAndroid Build Coastguard Worker // disclaimer in the documentation and/or other materials provided 16*0e209d39SAndroid Build Coastguard Worker // with the distribution. 17*0e209d39SAndroid Build Coastguard Worker // * Neither the name of Google Inc. nor the names of its 18*0e209d39SAndroid Build Coastguard Worker // contributors may be used to endorse or promote products derived 19*0e209d39SAndroid Build Coastguard Worker // from this software without specific prior written permission. 20*0e209d39SAndroid Build Coastguard Worker // 21*0e209d39SAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*0e209d39SAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*0e209d39SAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24*0e209d39SAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25*0e209d39SAndroid Build Coastguard Worker // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26*0e209d39SAndroid Build Coastguard Worker // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27*0e209d39SAndroid Build Coastguard Worker // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28*0e209d39SAndroid Build Coastguard Worker // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29*0e209d39SAndroid Build Coastguard Worker // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30*0e209d39SAndroid Build Coastguard Worker // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31*0e209d39SAndroid Build Coastguard Worker // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*0e209d39SAndroid Build Coastguard Worker 33*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING 34*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 35*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_FORMATTING 36*0e209d39SAndroid Build Coastguard Worker 37*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_DIY_FP_H_ 38*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_DIY_FP_H_ 39*0e209d39SAndroid Build Coastguard Worker 40*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: Customize header file paths for ICU. 41*0e209d39SAndroid Build Coastguard Worker 42*0e209d39SAndroid Build Coastguard Worker #include "double-conversion-utils.h" 43*0e209d39SAndroid Build Coastguard Worker 44*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: Wrap in ICU namespace 45*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 46*0e209d39SAndroid Build Coastguard Worker 47*0e209d39SAndroid Build Coastguard Worker namespace double_conversion { 48*0e209d39SAndroid Build Coastguard Worker 49*0e209d39SAndroid Build Coastguard Worker // This "Do It Yourself Floating Point" class implements a floating-point number 50*0e209d39SAndroid Build Coastguard Worker // with a uint64 significand and an int exponent. Normalized DiyFp numbers will 51*0e209d39SAndroid Build Coastguard Worker // have the most significant bit of the significand set. 52*0e209d39SAndroid Build Coastguard Worker // Multiplication and Subtraction do not normalize their results. 53*0e209d39SAndroid Build Coastguard Worker // DiyFp store only non-negative numbers and are not designed to contain special 54*0e209d39SAndroid Build Coastguard Worker // doubles (NaN and Infinity). 55*0e209d39SAndroid Build Coastguard Worker class DiyFp { 56*0e209d39SAndroid Build Coastguard Worker public: 57*0e209d39SAndroid Build Coastguard Worker static const int kSignificandSize = 64; 58*0e209d39SAndroid Build Coastguard Worker DiyFp()59*0e209d39SAndroid Build Coastguard Worker DiyFp() : f_(0), e_(0) {} DiyFp(const uint64_t significand,const int32_t exponent)60*0e209d39SAndroid Build Coastguard Worker DiyFp(const uint64_t significand, const int32_t exponent) : f_(significand), e_(exponent) {} 61*0e209d39SAndroid Build Coastguard Worker 62*0e209d39SAndroid Build Coastguard Worker // this -= other. 63*0e209d39SAndroid Build Coastguard Worker // The exponents of both numbers must be the same and the significand of this 64*0e209d39SAndroid Build Coastguard Worker // must be greater or equal than the significand of other. 65*0e209d39SAndroid Build Coastguard Worker // The result will not be normalized. Subtract(const DiyFp & other)66*0e209d39SAndroid Build Coastguard Worker void Subtract(const DiyFp& other) { 67*0e209d39SAndroid Build Coastguard Worker DOUBLE_CONVERSION_ASSERT(e_ == other.e_); 68*0e209d39SAndroid Build Coastguard Worker DOUBLE_CONVERSION_ASSERT(f_ >= other.f_); 69*0e209d39SAndroid Build Coastguard Worker f_ -= other.f_; 70*0e209d39SAndroid Build Coastguard Worker } 71*0e209d39SAndroid Build Coastguard Worker 72*0e209d39SAndroid Build Coastguard Worker // Returns a - b. 73*0e209d39SAndroid Build Coastguard Worker // The exponents of both numbers must be the same and a must be greater 74*0e209d39SAndroid Build Coastguard Worker // or equal than b. The result will not be normalized. Minus(const DiyFp & a,const DiyFp & b)75*0e209d39SAndroid Build Coastguard Worker static DiyFp Minus(const DiyFp& a, const DiyFp& b) { 76*0e209d39SAndroid Build Coastguard Worker DiyFp result = a; 77*0e209d39SAndroid Build Coastguard Worker result.Subtract(b); 78*0e209d39SAndroid Build Coastguard Worker return result; 79*0e209d39SAndroid Build Coastguard Worker } 80*0e209d39SAndroid Build Coastguard Worker 81*0e209d39SAndroid Build Coastguard Worker // this *= other. Multiply(const DiyFp & other)82*0e209d39SAndroid Build Coastguard Worker void Multiply(const DiyFp& other) { 83*0e209d39SAndroid Build Coastguard Worker // Simply "emulates" a 128 bit multiplication. 84*0e209d39SAndroid Build Coastguard Worker // However: the resulting number only contains 64 bits. The least 85*0e209d39SAndroid Build Coastguard Worker // significant 64 bits are only used for rounding the most significant 64 86*0e209d39SAndroid Build Coastguard Worker // bits. 87*0e209d39SAndroid Build Coastguard Worker const uint64_t kM32 = 0xFFFFFFFFU; 88*0e209d39SAndroid Build Coastguard Worker const uint64_t a = f_ >> 32; 89*0e209d39SAndroid Build Coastguard Worker const uint64_t b = f_ & kM32; 90*0e209d39SAndroid Build Coastguard Worker const uint64_t c = other.f_ >> 32; 91*0e209d39SAndroid Build Coastguard Worker const uint64_t d = other.f_ & kM32; 92*0e209d39SAndroid Build Coastguard Worker const uint64_t ac = a * c; 93*0e209d39SAndroid Build Coastguard Worker const uint64_t bc = b * c; 94*0e209d39SAndroid Build Coastguard Worker const uint64_t ad = a * d; 95*0e209d39SAndroid Build Coastguard Worker const uint64_t bd = b * d; 96*0e209d39SAndroid Build Coastguard Worker // By adding 1U << 31 to tmp we round the final result. 97*0e209d39SAndroid Build Coastguard Worker // Halfway cases will be rounded up. 98*0e209d39SAndroid Build Coastguard Worker const uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32) + (1U << 31); 99*0e209d39SAndroid Build Coastguard Worker e_ += other.e_ + 64; 100*0e209d39SAndroid Build Coastguard Worker f_ = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32); 101*0e209d39SAndroid Build Coastguard Worker } 102*0e209d39SAndroid Build Coastguard Worker 103*0e209d39SAndroid Build Coastguard Worker // returns a * b; Times(const DiyFp & a,const DiyFp & b)104*0e209d39SAndroid Build Coastguard Worker static DiyFp Times(const DiyFp& a, const DiyFp& b) { 105*0e209d39SAndroid Build Coastguard Worker DiyFp result = a; 106*0e209d39SAndroid Build Coastguard Worker result.Multiply(b); 107*0e209d39SAndroid Build Coastguard Worker return result; 108*0e209d39SAndroid Build Coastguard Worker } 109*0e209d39SAndroid Build Coastguard Worker Normalize()110*0e209d39SAndroid Build Coastguard Worker void Normalize() { 111*0e209d39SAndroid Build Coastguard Worker DOUBLE_CONVERSION_ASSERT(f_ != 0); 112*0e209d39SAndroid Build Coastguard Worker uint64_t significand = f_; 113*0e209d39SAndroid Build Coastguard Worker int32_t exponent = e_; 114*0e209d39SAndroid Build Coastguard Worker 115*0e209d39SAndroid Build Coastguard Worker // This method is mainly called for normalizing boundaries. In general, 116*0e209d39SAndroid Build Coastguard Worker // boundaries need to be shifted by 10 bits, and we optimize for this case. 117*0e209d39SAndroid Build Coastguard Worker const uint64_t k10MSBits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000); 118*0e209d39SAndroid Build Coastguard Worker while ((significand & k10MSBits) == 0) { 119*0e209d39SAndroid Build Coastguard Worker significand <<= 10; 120*0e209d39SAndroid Build Coastguard Worker exponent -= 10; 121*0e209d39SAndroid Build Coastguard Worker } 122*0e209d39SAndroid Build Coastguard Worker while ((significand & kUint64MSB) == 0) { 123*0e209d39SAndroid Build Coastguard Worker significand <<= 1; 124*0e209d39SAndroid Build Coastguard Worker exponent--; 125*0e209d39SAndroid Build Coastguard Worker } 126*0e209d39SAndroid Build Coastguard Worker f_ = significand; 127*0e209d39SAndroid Build Coastguard Worker e_ = exponent; 128*0e209d39SAndroid Build Coastguard Worker } 129*0e209d39SAndroid Build Coastguard Worker Normalize(const DiyFp & a)130*0e209d39SAndroid Build Coastguard Worker static DiyFp Normalize(const DiyFp& a) { 131*0e209d39SAndroid Build Coastguard Worker DiyFp result = a; 132*0e209d39SAndroid Build Coastguard Worker result.Normalize(); 133*0e209d39SAndroid Build Coastguard Worker return result; 134*0e209d39SAndroid Build Coastguard Worker } 135*0e209d39SAndroid Build Coastguard Worker f()136*0e209d39SAndroid Build Coastguard Worker uint64_t f() const { return f_; } e()137*0e209d39SAndroid Build Coastguard Worker int32_t e() const { return e_; } 138*0e209d39SAndroid Build Coastguard Worker set_f(uint64_t new_value)139*0e209d39SAndroid Build Coastguard Worker void set_f(uint64_t new_value) { f_ = new_value; } set_e(int32_t new_value)140*0e209d39SAndroid Build Coastguard Worker void set_e(int32_t new_value) { e_ = new_value; } 141*0e209d39SAndroid Build Coastguard Worker 142*0e209d39SAndroid Build Coastguard Worker private: 143*0e209d39SAndroid Build Coastguard Worker static const uint64_t kUint64MSB = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000); 144*0e209d39SAndroid Build Coastguard Worker 145*0e209d39SAndroid Build Coastguard Worker uint64_t f_; 146*0e209d39SAndroid Build Coastguard Worker int32_t e_; 147*0e209d39SAndroid Build Coastguard Worker }; 148*0e209d39SAndroid Build Coastguard Worker 149*0e209d39SAndroid Build Coastguard Worker } // namespace double_conversion 150*0e209d39SAndroid Build Coastguard Worker 151*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: Close ICU namespace 152*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 153*0e209d39SAndroid Build Coastguard Worker 154*0e209d39SAndroid Build Coastguard Worker #endif // DOUBLE_CONVERSION_DIY_FP_H_ 155*0e209d39SAndroid Build Coastguard Worker #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING 156