OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef DOUBLE_CONVERSION_DIY_FP_H_ | 28 #ifndef DOUBLE_CONVERSION_DIY_FP_H_ |
29 #define DOUBLE_CONVERSION_DIY_FP_H_ | 29 #define DOUBLE_CONVERSION_DIY_FP_H_ |
30 | 30 |
31 #include "utils.h" | 31 #include "utils.h" |
32 | 32 |
33 namespace WTF { | 33 namespace WTF { |
34 | 34 |
35 namespace double_conversion { | 35 namespace double_conversion { |
36 | 36 |
37 // This "Do It Yourself Floating Point" class implements a floating-point nu
mber | 37 // This "Do It Yourself Floating Point" class implements a floating-point nu
mber |
38 // with a uint64 significand and an int exponent. Normalized DiyFp numbers w
ill | 38 // with a uint64 significand and an int exponent. Normalized DiyFp numbers w
ill |
39 // have the most significant bit of the significand set. | 39 // have the most significant bit of the significand set. |
40 // Multiplication and Subtraction do not normalize their results. | 40 // Multiplication and Subtraction do not normalize their results. |
41 // DiyFp are not designed to contain special doubles (NaN and Infinity). | 41 // DiyFp are not designed to contain special doubles (NaN and Infinity). |
42 class DiyFp { | 42 class DiyFp { |
43 public: | 43 public: |
44 static const int kSignificandSize = 64; | 44 static const int kSignificandSize = 64; |
45 | 45 |
46 DiyFp() : f_(0), e_(0) {} | 46 DiyFp() : f_(0), e_(0) {} |
47 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} | 47 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} |
48 | 48 |
49 // this = this - other. | 49 // this = this - other. |
50 // The exponents of both numbers must be the same and the significand of
this | 50 // The exponents of both numbers must be the same and the significand of
this |
51 // must be bigger than the significand of other. | 51 // must be bigger than the significand of other. |
52 // The result will not be normalized. | 52 // The result will not be normalized. |
53 void Subtract(const DiyFp& other) { | 53 void Subtract(const DiyFp& other) { |
54 ASSERT(e_ == other.e_); | 54 ASSERT(e_ == other.e_); |
55 ASSERT(f_ >= other.f_); | 55 ASSERT(f_ >= other.f_); |
56 f_ -= other.f_; | 56 f_ -= other.f_; |
57 } | 57 } |
58 | 58 |
59 // Returns a - b. | 59 // Returns a - b. |
60 // The exponents of both numbers must be the same and this must be bigge
r | 60 // The exponents of both numbers must be the same and this must be bigge
r |
61 // than other. The result will not be normalized. | 61 // than other. The result will not be normalized. |
62 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { | 62 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { |
63 DiyFp result = a; | 63 DiyFp result = a; |
64 result.Subtract(b); | 64 result.Subtract(b); |
65 return result; | 65 return result; |
66 } | 66 } |
67 | 67 |
68 | 68 |
69 // this = this * other. | 69 // this = this * other. |
70 void Multiply(const DiyFp& other); | 70 void Multiply(const DiyFp& other); |
71 | 71 |
72 // returns a * b; | 72 // returns a * b; |
73 static DiyFp Times(const DiyFp& a, const DiyFp& b) { | 73 static DiyFp Times(const DiyFp& a, const DiyFp& b) { |
74 DiyFp result = a; | 74 DiyFp result = a; |
75 result.Multiply(b); | 75 result.Multiply(b); |
76 return result; | 76 return result; |
77 } | 77 } |
78 | 78 |
79 void Normalize() { | 79 void Normalize() { |
80 ASSERT(f_ != 0); | 80 ASSERT(f_ != 0); |
81 uint64_t f = f_; | 81 uint64_t f = f_; |
82 int e = e_; | 82 int e = e_; |
83 | 83 |
84 // This method is mainly called for normalizing boundaries. In gener
al | 84 // This method is mainly called for normalizing boundaries. In gener
al |
85 // boundaries need to be shifted by 10 bits. We thus optimize for th
is case. | 85 // boundaries need to be shifted by 10 bits. We thus optimize for th
is case. |
86 const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); | 86 const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); |
87 while ((f & k10MSBits) == 0) { | 87 while ((f & k10MSBits) == 0) { |
88 f <<= 10; | 88 f <<= 10; |
89 e -= 10; | 89 e -= 10; |
90 } | 90 } |
91 while ((f & kUint64MSB) == 0) { | 91 while ((f & kUint64MSB) == 0) { |
92 f <<= 1; | 92 f <<= 1; |
93 e--; | 93 e--; |
94 } | 94 } |
95 f_ = f; | 95 f_ = f; |
96 e_ = e; | 96 e_ = e; |
97 } | 97 } |
98 | 98 |
99 static DiyFp Normalize(const DiyFp& a) { | 99 static DiyFp Normalize(const DiyFp& a) { |
100 DiyFp result = a; | 100 DiyFp result = a; |
101 result.Normalize(); | 101 result.Normalize(); |
102 return result; | 102 return result; |
103 } | 103 } |
104 | 104 |
105 uint64_t f() const { return f_; } | 105 uint64_t f() const { return f_; } |
106 int e() const { return e_; } | 106 int e() const { return e_; } |
107 | 107 |
108 void set_f(uint64_t new_value) { f_ = new_value; } | 108 void set_f(uint64_t new_value) { f_ = new_value; } |
109 void set_e(int new_value) { e_ = new_value; } | 109 void set_e(int new_value) { e_ = new_value; } |
110 | 110 |
111 private: | 111 private: |
112 static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000); | 112 static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000); |
113 | 113 |
114 uint64_t f_; | 114 uint64_t f_; |
115 int e_; | 115 int e_; |
116 }; | 116 }; |
117 | 117 |
118 } // namespace double_conversion | 118 } // namespace double_conversion |
119 | 119 |
120 } // namespace WTF | 120 } // namespace WTF |
121 | 121 |
122 #endif // DOUBLE_CONVERSION_DIY_FP_H_ | 122 #endif // DOUBLE_CONVERSION_DIY_FP_H_ |
OLD | NEW |