LCOV - code coverage report
Current view: top level - mfbt/double-conversion/source - diy-fp.h (source / functions) Hit Total Coverage
Test: output.info Lines: 35 37 94.6 %
Date: 2017-07-14 16:53:18 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright 2010 the V8 project authors. All rights reserved.
       2             : // Redistribution and use in source and binary forms, with or without
       3             : // modification, are permitted provided that the following conditions are
       4             : // met:
       5             : //
       6             : //     * Redistributions of source code must retain the above copyright
       7             : //       notice, this list of conditions and the following disclaimer.
       8             : //     * Redistributions in binary form must reproduce the above
       9             : //       copyright notice, this list of conditions and the following
      10             : //       disclaimer in the documentation and/or other materials provided
      11             : //       with the distribution.
      12             : //     * Neither the name of Google Inc. nor the names of its
      13             : //       contributors may be used to endorse or promote products derived
      14             : //       from this software without specific prior written permission.
      15             : //
      16             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      17             : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      18             : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      19             : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      20             : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      21             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      22             : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      23             : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      24             : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      25             : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      26             : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      27             : 
      28             : #ifndef DOUBLE_CONVERSION_DIY_FP_H_
      29             : #define DOUBLE_CONVERSION_DIY_FP_H_
      30             : 
      31             : #include "utils.h"
      32             : 
      33             : namespace double_conversion {
      34             : 
      35             : // This "Do It Yourself Floating Point" class implements a floating-point number
      36             : // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
      37             : // have the most significant bit of the significand set.
      38             : // Multiplication and Subtraction do not normalize their results.
      39             : // DiyFp are not designed to contain special doubles (NaN and Infinity).
      40             : class DiyFp {
      41             :  public:
      42             :   static const int kSignificandSize = 64;
      43             : 
      44          32 :   DiyFp() : f_(0), e_(0) {}
      45          64 :   DiyFp(uint64_t significand, int exponent) : f_(significand), e_(exponent) {}
      46             : 
      47             :   // this = this - other.
      48             :   // The exponents of both numbers must be the same and the significand of this
      49             :   // must be bigger than the significand of other.
      50             :   // The result will not be normalized.
      51          16 :   void Subtract(const DiyFp& other) {
      52          16 :     ASSERT(e_ == other.e_);
      53          16 :     ASSERT(f_ >= other.f_);
      54          16 :     f_ -= other.f_;
      55          16 :   }
      56             : 
      57             :   // Returns a - b.
      58             :   // The exponents of both numbers must be the same and this must be bigger
      59             :   // than other. The result will not be normalized.
      60          16 :   static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
      61          16 :     DiyFp result = a;
      62          16 :     result.Subtract(b);
      63          16 :     return result;
      64             :   }
      65             : 
      66             : 
      67             :   // this = this * other.
      68             :   void Multiply(const DiyFp& other);
      69             : 
      70             :   // returns a * b;
      71          24 :   static DiyFp Times(const DiyFp& a, const DiyFp& b) {
      72          24 :     DiyFp result = a;
      73          24 :     result.Multiply(b);
      74          24 :     return result;
      75             :   }
      76             : 
      77           8 :   void Normalize() {
      78           8 :     ASSERT(f_ != 0);
      79           8 :     uint64_t significand = f_;
      80           8 :     int exponent = e_;
      81             : 
      82             :     // This method is mainly called for normalizing boundaries. In general
      83             :     // boundaries need to be shifted by 10 bits. We thus optimize for this case.
      84           8 :     const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
      85          24 :     while ((significand & k10MSBits) == 0) {
      86           8 :       significand <<= 10;
      87           8 :       exponent -= 10;
      88             :     }
      89           8 :     while ((significand & kUint64MSB) == 0) {
      90           0 :       significand <<= 1;
      91           0 :       exponent--;
      92             :     }
      93           8 :     f_ = significand;
      94           8 :     e_ = exponent;
      95           8 :   }
      96             : 
      97           8 :   static DiyFp Normalize(const DiyFp& a) {
      98           8 :     DiyFp result = a;
      99           8 :     result.Normalize();
     100           8 :     return result;
     101             :   }
     102             : 
     103         341 :   uint64_t f() const { return f_; }
     104         337 :   int e() const { return e_; }
     105             : 
     106          69 :   void set_f(uint64_t new_value) { f_ = new_value; }
     107           8 :   void set_e(int new_value) { e_ = new_value; }
     108             : 
     109             :  private:
     110             :   static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000);
     111             : 
     112             :   uint64_t f_;
     113             :   int e_;
     114             : };
     115             : 
     116             : }  // namespace double_conversion
     117             : 
     118             : #endif  // DOUBLE_CONVERSION_DIY_FP_H_

Generated by: LCOV version 1.13