Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: third_party/WebKit/Source/wtf/dtoa/double.h

Issue 2764243002: Move files in wtf/ to platform/wtf/ (Part 9). (Closed)
Patch Set: Rebase. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Use of this source code is governed by a BSD-style license that can be
3 // modification, are permitted provided that the following conditions are 3 // found in the LICENSE file.
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 4
28 #ifndef DOUBLE_CONVERSION_DOUBLE_H_ 5 #include "platform/wtf/dtoa/double.h"
29 #define DOUBLE_CONVERSION_DOUBLE_H_
30 6
31 #include "diy-fp.h" 7 // The contents of this header was moved to platform/wtf as part of
32 8 // WTF migration project. See the following post for details:
33 namespace WTF { 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY CAAJ
34
35 namespace double_conversion {
36
37 // We assume that doubles and uint64_t have the same endianness.
38 static uint64_t double_to_uint64(double d) {
39 return BitCast<uint64_t>(d);
40 }
41 static double uint64_to_double(uint64_t d64) {
42 return BitCast<double>(d64);
43 }
44
45 // Helper functions for doubles.
46 class Double {
47 public:
48 static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000);
49 static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000);
50 static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
51 static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000);
52 static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
53 static const int kSignificandSize = 53;
54
55 Double() : d64_(0) {}
56 explicit Double(double d) : d64_(double_to_uint64(d)) {}
57 explicit Double(uint64_t d64) : d64_(d64) {}
58 explicit Double(DiyFp diy_fp) : d64_(DiyFpToUint64(diy_fp)) {}
59
60 // The value encoded by this Double must be greater or equal to +0.0.
61 // It must not be special (infinity, or NaN).
62 DiyFp AsDiyFp() const {
63 DCHECK_GT(Sign(), 0);
64 DCHECK(!IsSpecial());
65 return DiyFp(Significand(), Exponent());
66 }
67
68 // The value encoded by this Double must be strictly greater than 0.
69 DiyFp AsNormalizedDiyFp() const {
70 DCHECK_GT(value(), 0.0);
71 uint64_t f = Significand();
72 int e = Exponent();
73
74 // The current double could be a denormal.
75 while ((f & kHiddenBit) == 0) {
76 f <<= 1;
77 e--;
78 }
79 // Do the final shifts in one go.
80 f <<= DiyFp::kSignificandSize - kSignificandSize;
81 e -= DiyFp::kSignificandSize - kSignificandSize;
82 return DiyFp(f, e);
83 }
84
85 // Returns the double's bit as uint64.
86 uint64_t AsUint64() const { return d64_; }
87
88 // Returns the next greater double. Returns +infinity on input +infinity.
89 double NextDouble() const {
90 if (d64_ == kInfinity)
91 return Double(kInfinity).value();
92 if (Sign() < 0 && Significand() == 0) {
93 // -0.0
94 return 0.0;
95 }
96 if (Sign() < 0) {
97 return Double(d64_ - 1).value();
98 } else {
99 return Double(d64_ + 1).value();
100 }
101 }
102
103 int Exponent() const {
104 if (IsDenormal())
105 return kDenormalExponent;
106
107 uint64_t d64 = AsUint64();
108 int biased_e =
109 static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
110 return biased_e - kExponentBias;
111 }
112
113 uint64_t Significand() const {
114 uint64_t d64 = AsUint64();
115 uint64_t significand = d64 & kSignificandMask;
116 if (!IsDenormal()) {
117 return significand + kHiddenBit;
118 } else {
119 return significand;
120 }
121 }
122
123 // Returns true if the double is a denormal.
124 bool IsDenormal() const {
125 uint64_t d64 = AsUint64();
126 return (d64 & kExponentMask) == 0;
127 }
128
129 // We consider denormals not to be special.
130 // Hence only Infinity and NaN are special.
131 bool IsSpecial() const {
132 uint64_t d64 = AsUint64();
133 return (d64 & kExponentMask) == kExponentMask;
134 }
135
136 bool IsNan() const {
137 uint64_t d64 = AsUint64();
138 return ((d64 & kExponentMask) == kExponentMask) &&
139 ((d64 & kSignificandMask) != 0);
140 }
141
142 bool IsInfinite() const {
143 uint64_t d64 = AsUint64();
144 return ((d64 & kExponentMask) == kExponentMask) &&
145 ((d64 & kSignificandMask) == 0);
146 }
147
148 int Sign() const {
149 uint64_t d64 = AsUint64();
150 return (d64 & kSignMask) == 0 ? 1 : -1;
151 }
152
153 // Precondition: the value encoded by this Double must be greater or equal
154 // than +0.0.
155 DiyFp UpperBoundary() const {
156 DCHECK_GT(Sign(), 0);
157 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
158 }
159
160 // Computes the two boundaries of this.
161 // The bigger boundary (m_plus) is normalized. The lower boundary has the same
162 // exponent as m_plus.
163 // Precondition: the value encoded by this Double must be greater than 0.
164 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
165 DCHECK_GT(value(), 0.0);
166 DiyFp v = this->AsDiyFp();
167 bool significand_is_zero = (v.f() == kHiddenBit);
168 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
169 DiyFp m_minus;
170 if (significand_is_zero && v.e() != kDenormalExponent) {
171 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
172 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
173 // at a distance of 1e8.
174 // The only exception is for the smallest normal: the largest denormal is
175 // at the same distance as its successor.
176 // Note: denormals have the same exponent as the smallest normals.
177 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
178 } else {
179 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
180 }
181 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
182 m_minus.set_e(m_plus.e());
183 *out_m_plus = m_plus;
184 *out_m_minus = m_minus;
185 }
186
187 double value() const { return uint64_to_double(d64_); }
188
189 // Returns the significand size for a given order of magnitude.
190 // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
191 // This function returns the number of significant binary digits v will have
192 // once it's encoded into a double. In almost all cases this is equal to
193 // kSignificandSize. The only exceptions are denormals. They start with
194 // leading zeroes and their effective significand-size is hence smaller.
195 static int SignificandSizeForOrderOfMagnitude(int order) {
196 if (order >= (kDenormalExponent + kSignificandSize)) {
197 return kSignificandSize;
198 }
199 if (order <= kDenormalExponent)
200 return 0;
201 return order - kDenormalExponent;
202 }
203
204 static double Infinity() { return Double(kInfinity).value(); }
205
206 static double NaN() { return Double(kNaN).value(); }
207
208 private:
209 static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
210 static const int kDenormalExponent = -kExponentBias + 1;
211 static const int kMaxExponent = 0x7FF - kExponentBias;
212 static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000);
213 static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000);
214
215 const uint64_t d64_;
216
217 static uint64_t DiyFpToUint64(DiyFp diy_fp) {
218 uint64_t significand = diy_fp.f();
219 int exponent = diy_fp.e();
220 while (significand > kHiddenBit + kSignificandMask) {
221 significand >>= 1;
222 exponent++;
223 }
224 if (exponent >= kMaxExponent) {
225 return kInfinity;
226 }
227 if (exponent < kDenormalExponent) {
228 return 0;
229 }
230 while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
231 significand <<= 1;
232 exponent--;
233 }
234 uint64_t biased_exponent;
235 if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
236 biased_exponent = 0;
237 } else {
238 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
239 }
240 return (significand & kSignificandMask) |
241 (biased_exponent << kPhysicalSignificandSize);
242 }
243 };
244
245 } // namespace double_conversion
246
247 } // namespace WTF
248
249 #endif // DOUBLE_CONVERSION_DOUBLE_H_
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa/diy-fp.cc ('k') | third_party/WebKit/Source/wtf/dtoa/double-conversion.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698