v8  3.11.10(node0.8.26)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
diy-fp.h
Go to the documentation of this file.
1 // Copyright 2011 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 V8_DIY_FP_H_
29 #define V8_DIY_FP_H_
30 
31 namespace v8 {
32 namespace internal {
33 
34 // This "Do It Yourself Floating Point" class implements a floating-point number
35 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
36 // have the most significant bit of the significand set.
37 // Multiplication and Subtraction do not normalize their results.
38 // DiyFp are not designed to contain special doubles (NaN and Infinity).
39 class DiyFp {
40  public:
41  static const int kSignificandSize = 64;
42 
43  DiyFp() : f_(0), e_(0) {}
44  DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
45 
46  // this = this - other.
47  // The exponents of both numbers must be the same and the significand of this
48  // must be bigger than the significand of other.
49  // The result will not be normalized.
50  void Subtract(const DiyFp& other) {
51  ASSERT(e_ == other.e_);
52  ASSERT(f_ >= other.f_);
53  f_ -= other.f_;
54  }
55 
56  // Returns a - b.
57  // The exponents of both numbers must be the same and this must be bigger
58  // than other. The result will not be normalized.
59  static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
60  DiyFp result = a;
61  result.Subtract(b);
62  return result;
63  }
64 
65 
66  // this = this * other.
67  void Multiply(const DiyFp& other);
68 
69  // returns a * b;
70  static DiyFp Times(const DiyFp& a, const DiyFp& b) {
71  DiyFp result = a;
72  result.Multiply(b);
73  return result;
74  }
75 
76  void Normalize() {
77  ASSERT(f_ != 0);
78  uint64_t f = f_;
79  int e = e_;
80 
81  // This method is mainly called for normalizing boundaries. In general
82  // boundaries need to be shifted by 10 bits. We thus optimize for this case.
83  const uint64_t k10MSBits = static_cast<uint64_t>(0x3FF) << 54;
84  while ((f & k10MSBits) == 0) {
85  f <<= 10;
86  e -= 10;
87  }
88  while ((f & kUint64MSB) == 0) {
89  f <<= 1;
90  e--;
91  }
92  f_ = f;
93  e_ = e;
94  }
95 
96  static DiyFp Normalize(const DiyFp& a) {
97  DiyFp result = a;
98  result.Normalize();
99  return result;
100  }
101 
102  uint64_t f() const { return f_; }
103  int e() const { return e_; }
104 
105  void set_f(uint64_t new_value) { f_ = new_value; }
106  void set_e(int new_value) { e_ = new_value; }
107 
108  private:
109  static const uint64_t kUint64MSB = static_cast<uint64_t>(1) << 63;
110 
111  uint64_t f_;
112  int e_;
113 };
114 
115 } } // namespace v8::internal
116 
117 #endif // V8_DIY_FP_H_
static DiyFp Minus(const DiyFp &a, const DiyFp &b)
Definition: diy-fp.h:59
static const int kSignificandSize
Definition: diy-fp.h:41
void set_f(uint64_t new_value)
Definition: diy-fp.h:105
static DiyFp Normalize(const DiyFp &a)
Definition: diy-fp.h:96
uint64_t f() const
Definition: diy-fp.h:102
#define ASSERT(condition)
Definition: checks.h:270
void Subtract(const DiyFp &other)
Definition: diy-fp.h:50
void Multiply(const DiyFp &other)
Definition: diy-fp.cc:36
DiyFp(uint64_t f, int e)
Definition: diy-fp.h:44
int e() const
Definition: diy-fp.h:103
static DiyFp Times(const DiyFp &a, const DiyFp &b)
Definition: diy-fp.h:70
void set_e(int new_value)
Definition: diy-fp.h:106
void Normalize()
Definition: diy-fp.h:76