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

Side by Side Diff: third_party/WebKit/Source/wtf/dtoa/fixed-dtoa.cc

Issue 2700123003: DO NOT COMMIT: Results of running old (current) clang-format on Blink (Closed)
Patch Set: Created 3 years, 10 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 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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "fixed-dtoa.h" 28 #include "fixed-dtoa.h"
29 29
30 #include <math.h>
30 #include "double.h" 31 #include "double.h"
31 #include <math.h>
32 32
33 namespace WTF { 33 namespace WTF {
34 34
35 namespace double_conversion { 35 namespace double_conversion {
36 36
37 // Represents a 128bit type. This class should be replaced by a native type on 37 // Represents a 128bit type. This class should be replaced by a native type on
38 // platforms that support 128bit integers. 38 // platforms that support 128bit integers.
39 class UInt128 { 39 class UInt128 {
40 public: 40 public:
41 UInt128() : high_bits_(0), low_bits_(0) { } 41 UInt128() : high_bits_(0), low_bits_(0) {}
42 UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { } 42 UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) {}
43 43
44 void Multiply(uint32_t multiplicand) { 44 void Multiply(uint32_t multiplicand) {
45 uint64_t accumulator; 45 uint64_t accumulator;
46 46
47 accumulator = (low_bits_ & kMask32) * multiplicand; 47 accumulator = (low_bits_ & kMask32) * multiplicand;
48 uint32_t part = static_cast<uint32_t>(accumulator & kMask32); 48 uint32_t part = static_cast<uint32_t>(accumulator & kMask32);
49 accumulator >>= 32; 49 accumulator >>= 32;
50 accumulator = accumulator + (low_bits_ >> 32) * multiplicand; 50 accumulator = accumulator + (low_bits_ >> 32) * multiplicand;
51 low_bits_ = (accumulator << 32) + part; 51 low_bits_ = (accumulator << 32) + part;
52 accumulator >>= 32; 52 accumulator >>= 32;
53 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand; 53 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand;
54 part = static_cast<uint32_t>(accumulator & kMask32); 54 part = static_cast<uint32_t>(accumulator & kMask32);
55 accumulator >>= 32; 55 accumulator >>= 32;
56 accumulator = accumulator + (high_bits_ >> 32) * multiplicand; 56 accumulator = accumulator + (high_bits_ >> 32) * multiplicand;
57 high_bits_ = (accumulator << 32) + part; 57 high_bits_ = (accumulator << 32) + part;
58 ASSERT((accumulator >> 32) == 0); 58 ASSERT((accumulator >> 32) == 0);
59 } 59 }
60 60
61 void Shift(int shift_amount) { 61 void Shift(int shift_amount) {
62 ASSERT(-64 <= shift_amount && shift_amount <= 64); 62 ASSERT(-64 <= shift_amount && shift_amount <= 64);
63 if (shift_amount == 0) { 63 if (shift_amount == 0) {
64 return; 64 return;
65 } else if (shift_amount == -64) { 65 } else if (shift_amount == -64) {
66 high_bits_ = low_bits_; 66 high_bits_ = low_bits_;
67 low_bits_ = 0; 67 low_bits_ = 0;
68 } else if (shift_amount == 64) { 68 } else if (shift_amount == 64) {
69 low_bits_ = high_bits_; 69 low_bits_ = high_bits_;
70 high_bits_ = 0; 70 high_bits_ = 0;
71 } else if (shift_amount <= 0) { 71 } else if (shift_amount <= 0) {
72 high_bits_ <<= -shift_amount; 72 high_bits_ <<= -shift_amount;
73 high_bits_ += low_bits_ >> (64 + shift_amount); 73 high_bits_ += low_bits_ >> (64 + shift_amount);
74 low_bits_ <<= -shift_amount; 74 low_bits_ <<= -shift_amount;
75 } else { 75 } else {
76 low_bits_ >>= shift_amount; 76 low_bits_ >>= shift_amount;
77 low_bits_ += high_bits_ << (64 - shift_amount); 77 low_bits_ += high_bits_ << (64 - shift_amount);
78 high_bits_ >>= shift_amount; 78 high_bits_ >>= shift_amount;
79 } 79 }
80 } 80 }
81 81
82 // Modifies *this to *this MOD (2^power). 82 // Modifies *this to *this MOD (2^power).
83 // Returns *this DIV (2^power). 83 // Returns *this DIV (2^power).
84 int DivModPowerOf2(int power) { 84 int DivModPowerOf2(int power) {
85 if (power >= 64) { 85 if (power >= 64) {
86 int result = static_cast<int>(high_bits_ >> (power - 64)); 86 int result = static_cast<int>(high_bits_ >> (power - 64));
87 high_bits_ -= static_cast<uint64_t>(result) << (power - 64); 87 high_bits_ -= static_cast<uint64_t>(result) << (power - 64);
88 return result; 88 return result;
89 } else { 89 } else {
90 uint64_t part_low = low_bits_ >> power; 90 uint64_t part_low = low_bits_ >> power;
91 uint64_t part_high = high_bits_ << (64 - power); 91 uint64_t part_high = high_bits_ << (64 - power);
92 int result = static_cast<int>(part_low + part_high); 92 int result = static_cast<int>(part_low + part_high);
93 high_bits_ = 0; 93 high_bits_ = 0;
94 low_bits_ -= part_low << power; 94 low_bits_ -= part_low << power;
95 return result; 95 return result;
96 } 96 }
97 } 97 }
98 98
99 bool IsZero() const { 99 bool IsZero() const { return high_bits_ == 0 && low_bits_ == 0; }
100 return high_bits_ == 0 && low_bits_ == 0; 100
101 } 101 int BitAt(int position) {
102 102 if (position >= 64) {
103 int BitAt(int position) { 103 return static_cast<int>(high_bits_ >> (position - 64)) & 1;
104 if (position >= 64) { 104 } else {
105 return static_cast<int>(high_bits_ >> (position - 64)) & 1; 105 return static_cast<int>(low_bits_ >> position) & 1;
106 } else { 106 }
107 return static_cast<int>(low_bits_ >> position) & 1; 107 }
108 } 108
109 } 109 private:
110 110 static const uint64_t kMask32 = 0xFFFFFFFF;
111 private: 111 // Value == (high_bits_ << 64) + low_bits_
112 static const uint64_t kMask32 = 0xFFFFFFFF; 112 uint64_t high_bits_;
113 // Value == (high_bits_ << 64) + low_bits_ 113 uint64_t low_bits_;
114 uint64_t high_bits_; 114 };
115 uint64_t low_bits_; 115
116 }; 116 static const int kDoubleSignificandSize = 53; // Includes the hidden bit.
117 117
118 118 static void FillDigits32FixedLength(uint32_t number,
119 static const int kDoubleSignificandSize = 53; // Includes the hidden bit. 119 int requested_length,
120 120 Vector<char> buffer,
121 121 int* length) {
122 static void FillDigits32FixedLength(uint32_t number, int requested_length, 122 for (int i = requested_length - 1; i >= 0; --i) {
123 Vector<char> buffer, int* length) { 123 buffer[(*length) + i] = '0' + number % 10;
124 for (int i = requested_length - 1; i >= 0; --i) { 124 number /= 10;
125 buffer[(*length) + i] = '0' + number % 10; 125 }
126 number /= 10; 126 *length += requested_length;
127 } 127 }
128 *length += requested_length; 128
129 } 129 static void FillDigits32(uint32_t number, Vector<char> buffer, int* length) {
130 130 int number_length = 0;
131 131 // We fill the digits in reverse order and exchange them afterwards.
132 static void FillDigits32(uint32_t number, Vector<char> buffer, int* length) { 132 while (number != 0) {
133 int number_length = 0; 133 char digit = number % 10;
134 // We fill the digits in reverse order and exchange them afterwards. 134 number /= 10;
135 while (number != 0) { 135 buffer[(*length) + number_length] = '0' + digit;
136 char digit = number % 10; 136 number_length++;
137 number /= 10; 137 }
138 buffer[(*length) + number_length] = '0' + digit; 138 // Exchange the digits.
139 number_length++; 139 int i = *length;
140 } 140 int j = *length + number_length - 1;
141 // Exchange the digits. 141 while (i < j) {
142 int i = *length; 142 char tmp = buffer[i];
143 int j = *length + number_length - 1; 143 buffer[i] = buffer[j];
144 while (i < j) { 144 buffer[j] = tmp;
145 char tmp = buffer[i]; 145 i++;
146 buffer[i] = buffer[j]; 146 j--;
147 buffer[j] = tmp; 147 }
148 i++; 148 *length += number_length;
149 j--; 149 }
150 } 150
151 *length += number_length; 151 static void FillDigits64FixedLength(uint64_t number,
152 } 152 int,
153 153 Vector<char> buffer,
154 154 int* length) {
155 static void FillDigits64FixedLength(uint64_t number, int, 155 const uint32_t kTen7 = 10000000;
156 Vector<char> buffer, int* length) { 156 // For efficiency cut the number into 3 uint32_t parts, and print those.
157 const uint32_t kTen7 = 10000000; 157 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
158 // For efficiency cut the number into 3 uint32_t parts, and print those. 158 number /= kTen7;
159 uint32_t part2 = static_cast<uint32_t>(number % kTen7); 159 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
160 number /= kTen7; 160 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
161 uint32_t part1 = static_cast<uint32_t>(number % kTen7); 161
162 uint32_t part0 = static_cast<uint32_t>(number / kTen7); 162 FillDigits32FixedLength(part0, 3, buffer, length);
163 163 FillDigits32FixedLength(part1, 7, buffer, length);
164 FillDigits32FixedLength(part0, 3, buffer, length); 164 FillDigits32FixedLength(part2, 7, buffer, length);
165 FillDigits32FixedLength(part1, 7, buffer, length); 165 }
166 FillDigits32FixedLength(part2, 7, buffer, length); 166
167 } 167 static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) {
168 168 const uint32_t kTen7 = 10000000;
169 169 // For efficiency cut the number into 3 uint32_t parts, and print those.
170 static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) { 170 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
171 const uint32_t kTen7 = 10000000; 171 number /= kTen7;
172 // For efficiency cut the number into 3 uint32_t parts, and print those. 172 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
173 uint32_t part2 = static_cast<uint32_t>(number % kTen7); 173 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
174 number /= kTen7; 174
175 uint32_t part1 = static_cast<uint32_t>(number % kTen7); 175 if (part0 != 0) {
176 uint32_t part0 = static_cast<uint32_t>(number / kTen7); 176 FillDigits32(part0, buffer, length);
177 177 FillDigits32FixedLength(part1, 7, buffer, length);
178 if (part0 != 0) { 178 FillDigits32FixedLength(part2, 7, buffer, length);
179 FillDigits32(part0, buffer, length); 179 } else if (part1 != 0) {
180 FillDigits32FixedLength(part1, 7, buffer, length); 180 FillDigits32(part1, buffer, length);
181 FillDigits32FixedLength(part2, 7, buffer, length); 181 FillDigits32FixedLength(part2, 7, buffer, length);
182 } else if (part1 != 0) { 182 } else {
183 FillDigits32(part1, buffer, length); 183 FillDigits32(part2, buffer, length);
184 FillDigits32FixedLength(part2, 7, buffer, length); 184 }
185 } else { 185 }
186 FillDigits32(part2, buffer, length); 186
187 } 187 static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) {
188 } 188 // An empty buffer represents 0.
189 189 if (*length == 0) {
190 190 buffer[0] = '1';
191 static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) { 191 *decimal_point = 1;
192 // An empty buffer represents 0. 192 *length = 1;
193 if (*length == 0) { 193 return;
194 buffer[0] = '1'; 194 }
195 *decimal_point = 1; 195 // Round the last digit until we either have a digit that was not '9' or until
196 *length = 1; 196 // we reached the first digit.
197 return; 197 buffer[(*length) - 1]++;
198 } 198 for (int i = (*length) - 1; i > 0; --i) {
199 // Round the last digit until we either have a digit that was not '9' or until 199 if (buffer[i] != '0' + 10) {
200 // we reached the first digit. 200 return;
201 buffer[(*length) - 1]++; 201 }
202 for (int i = (*length) - 1; i > 0; --i) { 202 buffer[i] = '0';
203 if (buffer[i] != '0' + 10) { 203 buffer[i - 1]++;
204 return; 204 }
205 } 205 // If the first digit is now '0' + 10, we would need to set it to '0' and add
206 buffer[i] = '0'; 206 // a '1' in front. However we reach the first digit only if all following
207 buffer[i - 1]++; 207 // digits had been '9' before rounding up. Now all trailing digits are '0' and
208 } 208 // we simply switch the first digit to '1' and update the decimal-point
209 // If the first digit is now '0' + 10, we would need to set it to '0' an d add 209 // (indicating that the point is now one digit to the right).
210 // a '1' in front. However we reach the first digit only if all followin g 210 if (buffer[0] == '0' + 10) {
211 // digits had been '9' before rounding up. Now all trailing digits are ' 0' and 211 buffer[0] = '1';
212 // we simply switch the first digit to '1' and update the decimal-point 212 (*decimal_point)++;
213 // (indicating that the point is now one digit to the right). 213 }
214 if (buffer[0] == '0' + 10) { 214 }
215 buffer[0] = '1'; 215
216 (*decimal_point)++; 216 // The given fractionals number represents a fixed-point number with binary
217 } 217 // point at bit (-exponent).
218 } 218 // Preconditions:
219 219 // -128 <= exponent <= 0.
220 220 // 0 <= fractionals * 2^exponent < 1
221 // The given fractionals number represents a fixed-point number with binary 221 // The buffer holds the result.
222 // point at bit (-exponent). 222 // The function will round its result. During the rounding-process digits not
223 // Preconditions: 223 // generated by this function might be updated, and the decimal-point variable
224 // -128 <= exponent <= 0. 224 // might be updated. If this function generates the digits 99 and the buffer
225 // 0 <= fractionals * 2^exponent < 1 225 // already contained "199" (thus yielding a buffer of "19999") then a
226 // The buffer holds the result. 226 // rounding-up will change the contents of the buffer to "20000".
227 // The function will round its result. During the rounding-process digits no t 227 static void FillFractionals(uint64_t fractionals,
228 // generated by this function might be updated, and the decimal-point variab le 228 int exponent,
229 // might be updated. If this function generates the digits 99 and the buffer 229 int fractional_count,
230 // already contained "199" (thus yielding a buffer of "19999") then a 230 Vector<char> buffer,
231 // rounding-up will change the contents of the buffer to "20000". 231 int* length,
232 static void FillFractionals(uint64_t fractionals, int exponent, 232 int* decimal_point) {
233 int fractional_count, Vector<char> buffer, 233 ASSERT(-128 <= exponent && exponent <= 0);
234 int* length, int* decimal_point) { 234 // 'fractionals' is a fixed-point number, with binary point at bit
235 ASSERT(-128 <= exponent && exponent <= 0); 235 // (-exponent). Inside the function the non-converted remainder of fractionals
236 // 'fractionals' is a fixed-point number, with binary point at bit 236 // is a fixed-point number, with binary point at bit 'point'.
237 // (-exponent). Inside the function the non-converted remainder of fract ionals 237 if (-exponent <= 64) {
238 // is a fixed-point number, with binary point at bit 'point'. 238 // One 64 bit number is sufficient.
239 if (-exponent <= 64) { 239 ASSERT(fractionals >> 56 == 0);
240 // One 64 bit number is sufficient. 240 int point = -exponent;
241 ASSERT(fractionals >> 56 == 0); 241 for (int i = 0; i < fractional_count; ++i) {
242 int point = -exponent; 242 if (fractionals == 0)
243 for (int i = 0; i < fractional_count; ++i) { 243 break;
244 if (fractionals == 0) break; 244 // Instead of multiplying by 10 we multiply by 5 and adjust the point
245 // Instead of multiplying by 10 we multiply by 5 and adjust the point 245 // location. This way the fractionals variable will not overflow.
246 // location. This way the fractionals variable will not overflow . 246 // Invariant at the beginning of the loop: fractionals < 2^point.
247 // Invariant at the beginning of the loop: fractionals < 2^point . 247 // Initially we have: point <= 64 and fractionals < 2^56
248 // Initially we have: point <= 64 and fractionals < 2^56 248 // After each iteration the point is decremented by one.
249 // After each iteration the point is decremented by one. 249 // Note that 5^3 = 125 < 128 = 2^7.
250 // Note that 5^3 = 125 < 128 = 2^7. 250 // Therefore three iterations of this loop will not overflow fractionals
251 // Therefore three iterations of this loop will not overflow fra ctionals 251 // (even without the subtraction at the end of the loop body). At this
252 // (even without the subtraction at the end of the loop body). A t this 252 // time point will satisfy point <= 61 and therefore fractionals < 2^point
253 // time point will satisfy point <= 61 and therefore fractionals < 2^point 253 // and any further multiplication of fractionals by 5 will not overflow.
254 // and any further multiplication of fractionals by 5 will not o verflow. 254 fractionals *= 5;
255 fractionals *= 5; 255 point--;
256 point--; 256 char digit = static_cast<char>(fractionals >> point);
257 char digit = static_cast<char>(fractionals >> point); 257 buffer[*length] = '0' + digit;
258 buffer[*length] = '0' + digit; 258 (*length)++;
259 (*length)++; 259 fractionals -= static_cast<uint64_t>(digit) << point;
260 fractionals -= static_cast<uint64_t>(digit) << point; 260 }
261 } 261 // If the first bit after the point is set we have to round up.
262 // If the first bit after the point is set we have to round up. 262 if (((fractionals >> (point - 1)) & 1) == 1) {
263 if (((fractionals >> (point - 1)) & 1) == 1) { 263 RoundUp(buffer, length, decimal_point);
264 RoundUp(buffer, length, decimal_point); 264 }
265 } 265 } else { // We need 128 bits.
266 } else { // We need 128 bits. 266 ASSERT(64 < -exponent && -exponent <= 128);
267 ASSERT(64 < -exponent && -exponent <= 128); 267 UInt128 fractionals128 = UInt128(fractionals, 0);
268 UInt128 fractionals128 = UInt128(fractionals, 0); 268 fractionals128.Shift(-exponent - 64);
269 fractionals128.Shift(-exponent - 64); 269 int point = 128;
270 int point = 128; 270 for (int i = 0; i < fractional_count; ++i) {
271 for (int i = 0; i < fractional_count; ++i) { 271 if (fractionals128.IsZero())
272 if (fractionals128.IsZero()) break; 272 break;
273 // As before: instead of multiplying by 10 we multiply by 5 and adjust the 273 // As before: instead of multiplying by 10 we multiply by 5 and adjust the
274 // point location. 274 // point location.
275 // This multiplication will not overflow for the same reasons as before. 275 // This multiplication will not overflow for the same reasons as before.
276 fractionals128.Multiply(5); 276 fractionals128.Multiply(5);
277 point--; 277 point--;
278 char digit = static_cast<char>(fractionals128.DivModPowerOf2(poi nt)); 278 char digit = static_cast<char>(fractionals128.DivModPowerOf2(point));
279 buffer[*length] = '0' + digit; 279 buffer[*length] = '0' + digit;
280 (*length)++; 280 (*length)++;
281 } 281 }
282 if (fractionals128.BitAt(point - 1) == 1) { 282 if (fractionals128.BitAt(point - 1) == 1) {
283 RoundUp(buffer, length, decimal_point); 283 RoundUp(buffer, length, decimal_point);
284 } 284 }
285 } 285 }
286 } 286 }
287 287
288 288 // Removes leading and trailing zeros.
289 // Removes leading and trailing zeros. 289 // If leading zeros are removed then the decimal point position is adjusted.
290 // If leading zeros are removed then the decimal point position is adjusted. 290 static void TrimZeros(Vector<char> buffer, int* length, int* decimal_point) {
291 static void TrimZeros(Vector<char> buffer, int* length, int* decimal_point) { 291 while (*length > 0 && buffer[(*length) - 1] == '0') {
292 while (*length > 0 && buffer[(*length) - 1] == '0') { 292 (*length)--;
293 (*length)--; 293 }
294 } 294 int first_non_zero = 0;
295 int first_non_zero = 0; 295 while (first_non_zero < *length && buffer[first_non_zero] == '0') {
296 while (first_non_zero < *length && buffer[first_non_zero] == '0') { 296 first_non_zero++;
297 first_non_zero++; 297 }
298 } 298 if (first_non_zero != 0) {
299 if (first_non_zero != 0) { 299 for (int i = first_non_zero; i < *length; ++i) {
300 for (int i = first_non_zero; i < *length; ++i) { 300 buffer[i - first_non_zero] = buffer[i];
301 buffer[i - first_non_zero] = buffer[i]; 301 }
302 } 302 *length -= first_non_zero;
303 *length -= first_non_zero; 303 *decimal_point -= first_non_zero;
304 *decimal_point -= first_non_zero; 304 }
305 } 305 }
306 } 306
307 307 bool FastFixedDtoa(double v,
308 308 int fractional_count,
309 bool FastFixedDtoa(double v, 309 Vector<char> buffer,
310 int fractional_count, 310 int* length,
311 Vector<char> buffer, 311 int* decimal_point) {
312 int* length, 312 const uint32_t kMaxUInt32 = 0xFFFFFFFF;
313 int* decimal_point) { 313 uint64_t significand = Double(v).Significand();
314 const uint32_t kMaxUInt32 = 0xFFFFFFFF; 314 int exponent = Double(v).Exponent();
315 uint64_t significand = Double(v).Significand(); 315 // v = significand * 2^exponent (with significand a 53bit integer).
316 int exponent = Double(v).Exponent(); 316 // If the exponent is larger than 20 (i.e. we may have a 73bit number) then we
317 // v = significand * 2^exponent (with significand a 53bit integer). 317 // don't know how to compute the representation. 2^73 ~= 9.5*10^21.
318 // If the exponent is larger than 20 (i.e. we may have a 73bit number) t hen we 318 // If necessary this limit could probably be increased, but we don't need
319 // don't know how to compute the representation. 2^73 ~= 9.5*10^21. 319 // more.
320 // If necessary this limit could probably be increased, but we don't nee d 320 if (exponent > 20)
321 // more. 321 return false;
322 if (exponent > 20) return false; 322 if (fractional_count > 20)
323 if (fractional_count > 20) return false; 323 return false;
324 *length = 0; 324 *length = 0;
325 // At most kDoubleSignificandSize bits of the significand are non-zero. 325 // At most kDoubleSignificandSize bits of the significand are non-zero.
326 // Given a 64 bit integer we have 11 0s followed by 53 potentially non-z ero 326 // Given a 64 bit integer we have 11 0s followed by 53 potentially non-zero
327 // bits: 0..11*..0xxx..53*..xx 327 // bits: 0..11*..0xxx..53*..xx
328 if (exponent + kDoubleSignificandSize > 64) { 328 if (exponent + kDoubleSignificandSize > 64) {
329 // The exponent must be > 11. 329 // The exponent must be > 11.
330 // 330 //
331 // We know that v = significand * 2^exponent. 331 // We know that v = significand * 2^exponent.
332 // And the exponent > 11. 332 // And the exponent > 11.
333 // We simplify the task by dividing v by 10^17. 333 // We simplify the task by dividing v by 10^17.
334 // The quotient delivers the first digits, and the remainder fits in to a 64 334 // The quotient delivers the first digits, and the remainder fits into a 64
335 // bit number. 335 // bit number.
336 // Dividing by 10^17 is equivalent to dividing by 5^17*2^17. 336 // Dividing by 10^17 is equivalent to dividing by 5^17*2^17.
337 const uint64_t kFive17 = UINT64_2PART_C(0xB1, A2BC2EC5); // 5^17 337 const uint64_t kFive17 = UINT64_2PART_C(0xB1, A2BC2EC5); // 5^17
338 uint64_t divisor = kFive17; 338 uint64_t divisor = kFive17;
339 int divisor_power = 17; 339 int divisor_power = 17;
340 uint64_t dividend = significand; 340 uint64_t dividend = significand;
341 uint32_t quotient; 341 uint32_t quotient;
342 uint64_t remainder; 342 uint64_t remainder;
343 // Let v = f * 2^e with f == significand and e == exponent. 343 // Let v = f * 2^e with f == significand and e == exponent.
344 // Then need q (quotient) and r (remainder) as follows: 344 // Then need q (quotient) and r (remainder) as follows:
345 // v = q * 10^17 + r 345 // v = q * 10^17 + r
346 // f * 2^e = q * 10^17 + r 346 // f * 2^e = q * 10^17 + r
347 // f * 2^e = q * 5^17 * 2^17 + r 347 // f * 2^e = q * 5^17 * 2^17 + r
348 // If e > 17 then 348 // If e > 17 then
349 // f * 2^(e-17) = q * 5^17 + r/2^17 349 // f * 2^(e-17) = q * 5^17 + r/2^17
350 // else 350 // else
351 // f = q * 5^17 * 2^(17-e) + r/2^e 351 // f = q * 5^17 * 2^(17-e) + r/2^e
352 if (exponent > divisor_power) { 352 if (exponent > divisor_power) {
353 // We only allow exponents of up to 20 and therefore (17 - e) <= 3 353 // We only allow exponents of up to 20 and therefore (17 - e) <= 3
354 dividend <<= exponent - divisor_power; 354 dividend <<= exponent - divisor_power;
355 quotient = static_cast<uint32_t>(dividend / divisor); 355 quotient = static_cast<uint32_t>(dividend / divisor);
356 remainder = (dividend % divisor) << divisor_power; 356 remainder = (dividend % divisor) << divisor_power;
357 } else { 357 } else {
358 divisor <<= divisor_power - exponent; 358 divisor <<= divisor_power - exponent;
359 quotient = static_cast<uint32_t>(dividend / divisor); 359 quotient = static_cast<uint32_t>(dividend / divisor);
360 remainder = (dividend % divisor) << exponent; 360 remainder = (dividend % divisor) << exponent;
361 } 361 }
362 FillDigits32(quotient, buffer, length); 362 FillDigits32(quotient, buffer, length);
363 FillDigits64FixedLength(remainder, divisor_power, buffer, length); 363 FillDigits64FixedLength(remainder, divisor_power, buffer, length);
364 *decimal_point = *length; 364 *decimal_point = *length;
365 } else if (exponent >= 0) { 365 } else if (exponent >= 0) {
366 // 0 <= exponent <= 11 366 // 0 <= exponent <= 11
367 significand <<= exponent; 367 significand <<= exponent;
368 FillDigits64(significand, buffer, length); 368 FillDigits64(significand, buffer, length);
369 *decimal_point = *length; 369 *decimal_point = *length;
370 } else if (exponent > -kDoubleSignificandSize) { 370 } else if (exponent > -kDoubleSignificandSize) {
371 // We have to cut the number. 371 // We have to cut the number.
372 uint64_t integrals = significand >> -exponent; 372 uint64_t integrals = significand >> -exponent;
373 uint64_t fractionals = significand - (integrals << -exponent); 373 uint64_t fractionals = significand - (integrals << -exponent);
374 if (integrals > kMaxUInt32) { 374 if (integrals > kMaxUInt32) {
375 FillDigits64(integrals, buffer, length); 375 FillDigits64(integrals, buffer, length);
376 } else { 376 } else {
377 FillDigits32(static_cast<uint32_t>(integrals), buffer, length); 377 FillDigits32(static_cast<uint32_t>(integrals), buffer, length);
378 } 378 }
379 *decimal_point = *length; 379 *decimal_point = *length;
380 FillFractionals(fractionals, exponent, fractional_count, 380 FillFractionals(fractionals, exponent, fractional_count, buffer, length,
381 buffer, length, decimal_point); 381 decimal_point);
382 } else if (exponent < -128) { 382 } else if (exponent < -128) {
383 // This configuration (with at most 20 digits) means that all digits must be 383 // This configuration (with at most 20 digits) means that all digits must be
384 // 0. 384 // 0.
385 ASSERT(fractional_count <= 20); 385 ASSERT(fractional_count <= 20);
386 buffer[0] = '\0'; 386 buffer[0] = '\0';
387 *length = 0; 387 *length = 0;
388 *decimal_point = -fractional_count; 388 *decimal_point = -fractional_count;
389 } else { 389 } else {
390 *decimal_point = 0; 390 *decimal_point = 0;
391 FillFractionals(significand, exponent, fractional_count, 391 FillFractionals(significand, exponent, fractional_count, buffer, length,
392 buffer, length, decimal_point); 392 decimal_point);
393 } 393 }
394 TrimZeros(buffer, length, decimal_point); 394 TrimZeros(buffer, length, decimal_point);
395 buffer[*length] = '\0'; 395 buffer[*length] = '\0';
396 if ((*length) == 0) { 396 if ((*length) == 0) {
397 // The string is empty and the decimal_point thus has no importance. Mimick 397 // The string is empty and the decimal_point thus has no importance. Mimick
398 // Gay's dtoa and and set it to -fractional_count. 398 // Gay's dtoa and and set it to -fractional_count.
399 *decimal_point = -fractional_count; 399 *decimal_point = -fractional_count;
400 } 400 }
401 return true; 401 return true;
402 } 402 }
403 403
404 } // namespace double_conversion 404 } // namespace double_conversion
405 405
406 } // namespace WTF 406 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa/fast-dtoa.cc ('k') | third_party/WebKit/Source/wtf/dtoa/strtod.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698