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

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

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