1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 // This "Do It Yourself Floating Point" class implements a floating-point number
12 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
13 // have the most significant bit of the significand set.
14 // Multiplication and Subtraction do not normalize their results.
15 // DiyFp are not designed to contain special doubles (NaN and Infinity).
18 static const int kSignificandSize = 64;
20 DiyFp() : f_(0), e_(0) {}
21 DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
23 // this = this - other.
24 // The exponents of both numbers must be the same and the significand of this
25 // must be bigger than the significand of other.
26 // The result will not be normalized.
27 void Subtract(const DiyFp& other) {
28 DCHECK(e_ == other.e_);
29 DCHECK(f_ >= other.f_);
34 // The exponents of both numbers must be the same and this must be bigger
35 // than other. The result will not be normalized.
36 static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
43 // this = this * other.
44 void Multiply(const DiyFp& other);
47 static DiyFp Times(const DiyFp& a, const DiyFp& b) {
58 // This method is mainly called for normalizing boundaries. In general
59 // boundaries need to be shifted by 10 bits. We thus optimize for this case.
60 const uint64_t k10MSBits = static_cast<uint64_t>(0x3FF) << 54;
61 while ((f & k10MSBits) == 0) {
65 while ((f & kUint64MSB) == 0) {
73 static DiyFp Normalize(const DiyFp& a) {
79 uint64_t f() const { return f_; }
80 int e() const { return e_; }
82 void set_f(uint64_t new_value) { f_ = new_value; }
83 void set_e(int new_value) { e_ = new_value; }
86 static const uint64_t kUint64MSB = static_cast<uint64_t>(1) << 63;
92 } } // namespace v8::internal
94 #endif // V8_DIY_FP_H_