mfbt/double-conversion/diy-fp.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/double-conversion/diy-fp.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +// Copyright 2010 the V8 project authors. All rights reserved.
     1.5 +// Redistribution and use in source and binary forms, with or without
     1.6 +// modification, are permitted provided that the following conditions are
     1.7 +// met:
     1.8 +//
     1.9 +//     * Redistributions of source code must retain the above copyright
    1.10 +//       notice, this list of conditions and the following disclaimer.
    1.11 +//     * Redistributions in binary form must reproduce the above
    1.12 +//       copyright notice, this list of conditions and the following
    1.13 +//       disclaimer in the documentation and/or other materials provided
    1.14 +//       with the distribution.
    1.15 +//     * Neither the name of Google Inc. nor the names of its
    1.16 +//       contributors may be used to endorse or promote products derived
    1.17 +//       from this software without specific prior written permission.
    1.18 +//
    1.19 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.20 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.21 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.22 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.23 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.24 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.25 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.26 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.27 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.28 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.29 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +
    1.31 +#ifndef DOUBLE_CONVERSION_DIY_FP_H_
    1.32 +#define DOUBLE_CONVERSION_DIY_FP_H_
    1.33 +
    1.34 +#include "utils.h"
    1.35 +
    1.36 +namespace double_conversion {
    1.37 +
    1.38 +// This "Do It Yourself Floating Point" class implements a floating-point number
    1.39 +// with a uint64 significand and an int exponent. Normalized DiyFp numbers will
    1.40 +// have the most significant bit of the significand set.
    1.41 +// Multiplication and Subtraction do not normalize their results.
    1.42 +// DiyFp are not designed to contain special doubles (NaN and Infinity).
    1.43 +class DiyFp {
    1.44 + public:
    1.45 +  static const int kSignificandSize = 64;
    1.46 +
    1.47 +  DiyFp() : f_(0), e_(0) {}
    1.48 +  DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
    1.49 +
    1.50 +  // this = this - other.
    1.51 +  // The exponents of both numbers must be the same and the significand of this
    1.52 +  // must be bigger than the significand of other.
    1.53 +  // The result will not be normalized.
    1.54 +  void Subtract(const DiyFp& other) {
    1.55 +    ASSERT(e_ == other.e_);
    1.56 +    ASSERT(f_ >= other.f_);
    1.57 +    f_ -= other.f_;
    1.58 +  }
    1.59 +
    1.60 +  // Returns a - b.
    1.61 +  // The exponents of both numbers must be the same and this must be bigger
    1.62 +  // than other. The result will not be normalized.
    1.63 +  static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
    1.64 +    DiyFp result = a;
    1.65 +    result.Subtract(b);
    1.66 +    return result;
    1.67 +  }
    1.68 +
    1.69 +
    1.70 +  // this = this * other.
    1.71 +  void Multiply(const DiyFp& other);
    1.72 +
    1.73 +  // returns a * b;
    1.74 +  static DiyFp Times(const DiyFp& a, const DiyFp& b) {
    1.75 +    DiyFp result = a;
    1.76 +    result.Multiply(b);
    1.77 +    return result;
    1.78 +  }
    1.79 +
    1.80 +  void Normalize() {
    1.81 +    ASSERT(f_ != 0);
    1.82 +    uint64_t f = f_;
    1.83 +    int e = e_;
    1.84 +
    1.85 +    // This method is mainly called for normalizing boundaries. In general
    1.86 +    // boundaries need to be shifted by 10 bits. We thus optimize for this case.
    1.87 +    const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
    1.88 +    while ((f & k10MSBits) == 0) {
    1.89 +      f <<= 10;
    1.90 +      e -= 10;
    1.91 +    }
    1.92 +    while ((f & kUint64MSB) == 0) {
    1.93 +      f <<= 1;
    1.94 +      e--;
    1.95 +    }
    1.96 +    f_ = f;
    1.97 +    e_ = e;
    1.98 +  }
    1.99 +
   1.100 +  static DiyFp Normalize(const DiyFp& a) {
   1.101 +    DiyFp result = a;
   1.102 +    result.Normalize();
   1.103 +    return result;
   1.104 +  }
   1.105 +
   1.106 +  uint64_t f() const { return f_; }
   1.107 +  int e() const { return e_; }
   1.108 +
   1.109 +  void set_f(uint64_t new_value) { f_ = new_value; }
   1.110 +  void set_e(int new_value) { e_ = new_value; }
   1.111 +
   1.112 + private:
   1.113 +  static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000);
   1.114 +
   1.115 +  uint64_t f_;
   1.116 +  int e_;
   1.117 +};
   1.118 +
   1.119 +}  // namespace double_conversion
   1.120 +
   1.121 +#endif  // DOUBLE_CONVERSION_DIY_FP_H_

mercurial