tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

double-conversion-diy-fp.h (5541B)


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