1ddda9e81d3175130f2029c0e1205d265a00c32edjkummerow@chromium.org// Copyright 2011 the V8 project authors. All rights reserved.
23484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org// Use of this source code is governed by a BSD-style license that can be
33484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org// found in the LICENSE file.
4cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
5cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org#ifndef V8_DIY_FP_H_
6cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org#define V8_DIY_FP_H_
7cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
8cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.orgnamespace v8 {
9cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.orgnamespace internal {
10cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
11cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org// This "Do It Yourself Floating Point" class implements a floating-point number
12cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org// with a uint64 significand and an int exponent. Normalized DiyFp numbers will
13cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org// have the most significant bit of the significand set.
14cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org// Multiplication and Subtraction do not normalize their results.
15cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org// DiyFp are not designed to contain special doubles (NaN and Infinity).
16cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.orgclass DiyFp {
17cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org public:
18cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  static const int kSignificandSize = 64;
19cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
20cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  DiyFp() : f_(0), e_(0) {}
21cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
22cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
23cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // this = this - other.
24cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // The exponents of both numbers must be the same and the significand of this
25cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // must be bigger than the significand of other.
26cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // The result will not be normalized.
27cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  void Subtract(const DiyFp& other) {
28e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(e_ == other.e_);
29e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(f_ >= other.f_);
30cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    f_ -= other.f_;
31cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  }
32cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
33cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // Returns a - b.
34cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // The exponents of both numbers must be the same and this must be bigger
35cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // than other. The result will not be normalized.
36cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
37cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    DiyFp result = a;
38cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    result.Subtract(b);
39cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    return result;
40cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  }
41cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
42cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
43cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // this = this * other.
44cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  void Multiply(const DiyFp& other);
45cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
46cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  // returns a * b;
47cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  static DiyFp Times(const DiyFp& a, const DiyFp& b) {
48cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    DiyFp result = a;
49cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    result.Multiply(b);
50cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    return result;
51cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  }
52cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
53cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  void Normalize() {
54e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(f_ != 0);
55cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    uint64_t f = f_;
56cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    int e = e_;
57cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
58cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    // This method is mainly called for normalizing boundaries. In general
59cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    // boundaries need to be shifted by 10 bits. We thus optimize for this case.
60ddda9e81d3175130f2029c0e1205d265a00c32edjkummerow@chromium.org    const uint64_t k10MSBits = static_cast<uint64_t>(0x3FF) << 54;
61cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    while ((f & k10MSBits) == 0) {
62cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      f <<= 10;
63cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      e -= 10;
64cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    }
65cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    while ((f & kUint64MSB) == 0) {
66cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      f <<= 1;
67cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      e--;
68cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    }
69cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    f_ = f;
70cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    e_ = e;
71cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  }
72cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
73cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  static DiyFp Normalize(const DiyFp& a) {
74cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    DiyFp result = a;
75cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    result.Normalize();
76cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    return result;
77cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  }
78cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
79cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  uint64_t f() const { return f_; }
80cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  int e() const { return e_; }
81cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
82cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  void set_f(uint64_t new_value) { f_ = new_value; }
83cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  void set_e(int new_value) { e_ = new_value; }
84cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
85cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org private:
86ddda9e81d3175130f2029c0e1205d265a00c32edjkummerow@chromium.org  static const uint64_t kUint64MSB = static_cast<uint64_t>(1) << 63;
87cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
88cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  uint64_t f_;
89cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org  int e_;
90cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org};
91cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
92cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org} }  // namespace v8::internal
93cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
94cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org#endif  // V8_DIY_FP_H_
95