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_DOUBLE_H_ | |
29 #define V8_DOUBLE_H_ | |
30 | |
31 #include "diy_fp.h" | |
Lasse Reichstein
2010/02/22 11:31:36
For consistency with the remaining code, please do
Florian Loitsch
2010/02/22 15:52:53
Done.
floitsch
2012/04/10 15:54:34
Done.
| |
32 | |
33 namespace v8 { | |
34 namespace internal { | |
35 | |
36 typedef union { | |
Lasse Reichstein
2010/02/22 11:31:36
Using a union to convert is bound to give problems
Florian Loitsch
2010/02/22 15:52:53
Done.
floitsch
2012/04/10 15:54:34
Done.
| |
37 double d; | |
38 uint64_t n; | |
39 } converter_t; | |
40 | |
41 // TODO(floitsch): the following conversion functions only work when uint64 and | |
42 // doubles share the same endianess. | |
43 static uint64_t double_to_uint64(double d) { | |
44 converter_t tmp; | |
45 tmp.d = d; | |
46 return tmp.n; | |
47 } | |
48 | |
49 static double uint64_to_double(uint64_t d64) { | |
50 converter_t tmp; | |
51 tmp.n = d64; | |
52 return tmp.d; | |
53 } | |
54 | |
55 // Helper functions for doubles. | |
56 class Double { | |
57 public: | |
58 static const int kSignificandSize = 52; // excluding the hidden bit | |
Lasse Reichstein
2010/02/22 11:31:36
Format comments as full sentences (capital start l
Florian Loitsch
2010/02/22 15:52:53
Done.
floitsch
2012/04/10 15:54:34
Done.
| |
59 | |
60 Double() : d64_(0.0) {} | |
61 explicit Double(double d) : d64_(double_to_uint64(d)) {} | |
62 explicit Double(uint64_t d64) : d64_(d64) {} | |
63 | |
64 DiyFp AsDiyFp() const { | |
Lasse Reichstein
2010/02/22 11:31:36
Do you have any suggested pronunciation of DiyFp?
Florian Loitsch
2010/02/22 15:52:53
Do It Yourself Floating Point. Name is copied from
floitsch
2012/04/10 15:54:34
Do It Yourself Floating Point. This name is copied
| |
65 ASSERT(!IsSpecial()); | |
66 return DiyFp(Significand(), Exponent()); | |
67 } | |
68 | |
69 DiyFp AsNormalizedDiyFp() const { | |
70 uint64_t f = Significand(); | |
71 int e = Exponent(); | |
72 | |
73 // the current double could be a denormal. | |
74 while ((f & kHiddenBit) == 0) { | |
75 f <<= 1; | |
76 e--; | |
77 } | |
78 /* do the final shifts in one go. Don't forget the hidden bit (the '-1') */ | |
79 f <<= DiyFp::kSignificandSize - kSignificandSize - 1; | |
80 e -= DiyFp::kSignificandSize - kSignificandSize - 1; | |
81 return DiyFp(f, e); | |
82 } | |
83 | |
84 int Exponent() const { | |
85 if (IsDenormal()) return kDenormalExponent; | |
86 | |
87 uint64_t d64 = AsUint64(); | |
88 int biased_e = (d64 & kExponentMask) >> kSignificandSize; | |
89 return biased_e - kExponentBias; | |
90 } | |
91 | |
92 uint64_t Significand() const { | |
93 uint64_t d64 = AsUint64(); | |
94 uint64_t significand = d64 & kSignificandMask; | |
95 if (!IsDenormal()) { | |
96 return significand + kHiddenBit; | |
97 } else { | |
98 return significand; | |
99 } | |
100 } | |
101 | |
102 // Returns true if the double is a denormal. | |
103 bool IsDenormal() const { | |
104 uint64_t d64 = AsUint64(); | |
105 return (d64 & kExponentMask) == 0; | |
106 } | |
107 | |
108 // We consider denormals not to be special. | |
109 // Hence only Infinity and NaN are special. | |
110 bool IsSpecial() const { | |
111 uint64_t d64 = AsUint64(); | |
112 return (d64 & kExponentMask) == kExponentMask; | |
113 } | |
114 | |
115 // Returns the two boundaries of this. | |
116 // The bigger boundary (m_plus) is normalized. The lower boundary has the same | |
117 // exponent as m_plus. | |
118 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { | |
119 DiyFp v = this->AsDiyFp(); | |
120 bool significand_is_zero = (v.f() == kHiddenBit); | |
121 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); | |
122 DiyFp m_minus; | |
123 if (significand_is_zero && v.e() != kDenormalExponent) { | |
124 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9. | |
125 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but | |
126 // at a distance of 1e8. | |
127 // The only exception is for the smallest normal: the largest denormal is | |
128 // at the same distance as its successor. | |
129 // Note: denormals have the same exponent as the smallest normals. | |
130 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); | |
131 } else { | |
132 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); | |
133 } | |
134 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); | |
135 m_minus.set_e(m_plus.e()); | |
136 *out_m_plus = m_plus; | |
137 *out_m_minus = m_minus; | |
138 } | |
139 | |
140 double value() const { return uint64_to_double(d64_); } | |
141 | |
142 private: | |
143 static const int kExponentBias = 0x3FF + kSignificandSize; | |
144 static const int kDenormalExponent = -kExponentBias + 1; | |
145 static const uint64_t kExponentMask = V8_2PART_UINT64_C(0x7FF00000,00000000 ); | |
146 static const uint64_t kSignificandMask = V8_2PART_UINT64_C(0x000FFFFF,FFFFFFFF ); | |
147 static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000,00000000 ); | |
148 | |
149 uint64_t d64_; | |
150 | |
151 // Returns the double's bit as uint64. | |
152 uint64_t AsUint64() const { | |
153 return d64_; | |
154 } | |
155 }; | |
156 | |
157 } } // namespace v8::internal | |
158 | |
159 #endif // V8_DOUBLE_H_ | |
OLD | NEW |