OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2010 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 // Implements a 64bit significand/int exponent Floating Point number. | |
35 // Only the minus and multiply operations are implemented. Both operations have | |
36 // severe limitations. But we don't need more. | |
37 // DiyFp are not designed to contain special doubles (NaN and Infinity). | |
38 class DiyFp { | |
39 public: | |
40 static const int kSignificandSize = 64; | |
41 | |
42 DiyFp() : f_(0), e_(0) {} | |
43 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} | |
44 | |
45 // this = this - other. | |
46 // The exponents of both numbers must be the same and this must be bigger | |
47 // than other. The result will not be normalized. | |
48 void Subtract(const DiyFp& other) { | |
49 ASSERT(e_ == other.e_); | |
50 ASSERT(f_ >= other.f_); | |
51 f_ -= other.f_; | |
52 } | |
53 | |
54 // returns a - b. | |
55 // The exponents of both numbers must be the same and this must be bigger | |
56 // than other. The result will not be normalized. | |
57 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { | |
58 DiyFp result = a; | |
59 result.Subtract(b); | |
60 return result; | |
61 } | |
62 | |
63 | |
64 // this = this * other. | |
65 void Multiply(const DiyFp& other) { | |
66 // Simply "emulates" a 128 bit multiplication. | |
67 // However: the resulting number only contains 64 bits. The last 64 bits | |
68 // are only used for rounding the first 64 bits. | |
Lasse Reichstein
2010/02/22 11:31:36
Which bits are first and last? Do you mean low and
Florian Loitsch
2010/02/22 15:52:53
Done.
floitsch
2012/04/10 15:54:34
Done.
| |
69 const uint64_t kM32 = 0xFFFFFFFF; | |
Lasse Reichstein
2010/02/22 11:31:36
Put a "u" on the end of the literal to make it uns
Florian Loitsch
2010/02/22 15:52:53
Done.
floitsch
2012/04/10 15:54:34
Done.
| |
70 uint64_t a = f_ >> 32; | |
71 uint64_t b = f_ & kM32; | |
72 uint64_t c = other.f_ >> 32; | |
73 uint64_t d = other.f_ & kM32; | |
74 uint64_t ac = a * c; | |
75 uint64_t bc = b * c; | |
76 uint64_t ad = a * d; | |
77 uint64_t bd = b * d; | |
78 uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32); | |
79 tmp += 1U << 31; // round | |
80 uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32); | |
81 e_ += other.e_ + 64; | |
82 f_ = result_f; | |
83 } | |
84 | |
85 // returns a * b; | |
86 static DiyFp Times(const DiyFp& a, const DiyFp& b) { | |
87 DiyFp result = a; | |
88 result.Multiply(b); | |
89 return result; | |
90 } | |
91 | |
92 void Normalize() { | |
93 ASSERT(f_ != 0); | |
94 uint64_t f = f_; | |
95 int e = e_; | |
96 | |
97 // This method is mainly called for normalizing boundaries. In general | |
98 // boundaries need to be shifted by 10 bits. We thus optimize for this case. | |
99 const uint64_t k10MSBits = V8_2PART_UINT64_C(0xFFC00000,00000000); | |
100 while ((f & k10MSBits) == 0) { | |
101 f <<= 10; | |
102 e -= 10; | |
103 } | |
104 while ((f & kUint64MSB) == 0) { | |
105 f <<= 1; | |
106 e--; | |
107 } | |
108 f_ = f; | |
109 e_ = e; | |
110 } | |
111 | |
112 static DiyFp Normalize(const DiyFp& a) { | |
113 DiyFp result = a; | |
114 result.Normalize(); | |
115 return result; | |
116 } | |
117 | |
118 uint64_t f() const { return f_; } | |
119 int e() const { return e_; } | |
120 | |
121 void set_f(uint64_t new_value) { f_ = new_value; } | |
122 void set_e(int new_value) { e_ = new_value; } | |
123 | |
124 private: | |
125 static const uint64_t kUint64MSB = V8_2PART_UINT64_C(0x80000000,00000000); | |
126 | |
127 uint64_t f_; | |
128 int e_; | |
129 }; | |
130 | |
131 } } // namespace v8::internal | |
132 | |
133 #endif // V8_DIY_FP_H_ | |
OLD | NEW |