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

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

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

Powered by Google App Engine
This is Rietveld 408576698