OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_DIY_FP_H_ | 5 #ifndef V8_DIY_FP_H_ |
6 #define V8_DIY_FP_H_ | 6 #define V8_DIY_FP_H_ |
7 | 7 |
8 namespace v8 { | 8 namespace v8 { |
9 namespace internal { | 9 namespace internal { |
10 | 10 |
11 // This "Do It Yourself Floating Point" class implements a floating-point number | 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 | 12 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will |
13 // have the most significant bit of the significand set. | 13 // have the most significant bit of the significand set. |
14 // Multiplication and Subtraction do not normalize their results. | 14 // Multiplication and Subtraction do not normalize their results. |
15 // DiyFp are not designed to contain special doubles (NaN and Infinity). | 15 // DiyFp are not designed to contain special doubles (NaN and Infinity). |
16 class DiyFp { | 16 class DiyFp { |
17 public: | 17 public: |
18 static const int kSignificandSize = 64; | 18 static const int kSignificandSize = 64; |
19 | 19 |
20 DiyFp() : f_(0), e_(0) {} | 20 DiyFp() : f_(0), e_(0) {} |
21 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} | 21 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} |
22 | 22 |
23 // this = this - other. | 23 // this = this - other. |
24 // The exponents of both numbers must be the same and the significand of this | 24 // The exponents of both numbers must be the same and the significand of this |
25 // must be bigger than the significand of other. | 25 // must be bigger than the significand of other. |
26 // The result will not be normalized. | 26 // The result will not be normalized. |
27 void Subtract(const DiyFp& other) { | 27 void Subtract(const DiyFp& other) { |
28 ASSERT(e_ == other.e_); | 28 DCHECK(e_ == other.e_); |
29 ASSERT(f_ >= other.f_); | 29 DCHECK(f_ >= other.f_); |
30 f_ -= other.f_; | 30 f_ -= other.f_; |
31 } | 31 } |
32 | 32 |
33 // Returns a - b. | 33 // Returns a - b. |
34 // The exponents of both numbers must be the same and this must be bigger | 34 // The exponents of both numbers must be the same and this must be bigger |
35 // than other. The result will not be normalized. | 35 // than other. The result will not be normalized. |
36 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { | 36 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { |
37 DiyFp result = a; | 37 DiyFp result = a; |
38 result.Subtract(b); | 38 result.Subtract(b); |
39 return result; | 39 return result; |
40 } | 40 } |
41 | 41 |
42 | 42 |
43 // this = this * other. | 43 // this = this * other. |
44 void Multiply(const DiyFp& other); | 44 void Multiply(const DiyFp& other); |
45 | 45 |
46 // returns a * b; | 46 // returns a * b; |
47 static DiyFp Times(const DiyFp& a, const DiyFp& b) { | 47 static DiyFp Times(const DiyFp& a, const DiyFp& b) { |
48 DiyFp result = a; | 48 DiyFp result = a; |
49 result.Multiply(b); | 49 result.Multiply(b); |
50 return result; | 50 return result; |
51 } | 51 } |
52 | 52 |
53 void Normalize() { | 53 void Normalize() { |
54 ASSERT(f_ != 0); | 54 DCHECK(f_ != 0); |
55 uint64_t f = f_; | 55 uint64_t f = f_; |
56 int e = e_; | 56 int e = e_; |
57 | 57 |
58 // This method is mainly called for normalizing boundaries. In general | 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. | 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; | 60 const uint64_t k10MSBits = static_cast<uint64_t>(0x3FF) << 54; |
61 while ((f & k10MSBits) == 0) { | 61 while ((f & k10MSBits) == 0) { |
62 f <<= 10; | 62 f <<= 10; |
63 e -= 10; | 63 e -= 10; |
64 } | 64 } |
(...skipping 20 matching lines...) Expand all Loading... |
85 private: | 85 private: |
86 static const uint64_t kUint64MSB = static_cast<uint64_t>(1) << 63; | 86 static const uint64_t kUint64MSB = static_cast<uint64_t>(1) << 63; |
87 | 87 |
88 uint64_t f_; | 88 uint64_t f_; |
89 int e_; | 89 int e_; |
90 }; | 90 }; |
91 | 91 |
92 } } // namespace v8::internal | 92 } } // namespace v8::internal |
93 | 93 |
94 #endif // V8_DIY_FP_H_ | 94 #endif // V8_DIY_FP_H_ |
OLD | NEW |