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

Side by Side Diff: src/fixed-dtoa.cc

Issue 1956005: Dtoa for fixed notation. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « src/fixed-dtoa.h ('k') | test/cctest/SConscript » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 return result; 96 return result;
97 } 97 }
98 } 98 }
99 99
100 bool IsZero() const { 100 bool IsZero() const {
101 return high_bits_ == 0 && low_bits_ == 0; 101 return high_bits_ == 0 && low_bits_ == 0;
102 } 102 }
103 103
104 int BitAt(int position) { 104 int BitAt(int position) {
105 if (position >= 64) { 105 if (position >= 64) {
106 return (high_bits_ >> (position - 64)) & 1; 106 return static_cast<int>(high_bits_ >> (position - 64)) & 1;
107 } else { 107 } else {
108 return (low_bits_ >> position) & 1; 108 return static_cast<int>(low_bits_ >> position) & 1;
109 } 109 }
110 } 110 }
111 111
112 private: 112 private:
113 static const uint64_t kMask32 = 0xFFFFFFFF; 113 static const uint64_t kMask32 = 0xFFFFFFFF;
114 // Value == (high_bits_ << 64) + low_bits_ 114 // Value == (high_bits_ << 64) + low_bits_
115 uint64_t high_bits_; 115 uint64_t high_bits_;
116 uint64_t low_bits_; 116 uint64_t low_bits_;
117 }; 117 };
118 118
(...skipping 20 matching lines...) Expand all
139 buffer[(*length) + number_length] = '0' + digit; 139 buffer[(*length) + number_length] = '0' + digit;
140 number_length++; 140 number_length++;
141 } 141 }
142 // Exchange the digits. 142 // Exchange the digits.
143 int i = *length; 143 int i = *length;
144 int j = *length + number_length - 1; 144 int j = *length + number_length - 1;
145 while (i < j) { 145 while (i < j) {
146 char tmp = buffer[i]; 146 char tmp = buffer[i];
147 buffer[i] = buffer[j]; 147 buffer[i] = buffer[j];
148 buffer[j] = tmp; 148 buffer[j] = tmp;
149 i++; j--; 149 i++;
150 j--;
150 } 151 }
151 *length += number_length; 152 *length += number_length;
152 } 153 }
153 154
154 155
155 static void FillDigits64FixedLength(uint64_t number, int requested_length, 156 static void FillDigits64FixedLength(uint64_t number, int requested_length,
156 Vector<char> buffer, int* length) { 157 Vector<char> buffer, int* length) {
157 const uint32_t kTen7 = 10000000; 158 const uint32_t kTen7 = 10000000;
158 // For efficiency cut the number into 3 uint32_t parts, and print those. 159 // For efficiency cut the number into 3 uint32_t parts, and print those.
159 uint32_t part2 = number % kTen7; 160 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
160 number /= kTen7; 161 number /= kTen7;
161 uint32_t part1 = number % kTen7; 162 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
162 uint32_t part0 = number / kTen7; 163 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
163 164
164 FillDigits32FixedLength(part0, 3, buffer, length); 165 FillDigits32FixedLength(part0, 3, buffer, length);
165 FillDigits32FixedLength(part1, 7, buffer, length); 166 FillDigits32FixedLength(part1, 7, buffer, length);
166 FillDigits32FixedLength(part2, 7, buffer, length); 167 FillDigits32FixedLength(part2, 7, buffer, length);
167 } 168 }
168 169
169 170
170 static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) { 171 static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) {
171 const uint32_t kTen7 = 10000000; 172 const uint32_t kTen7 = 10000000;
172 // For efficiency cut the number into 3 uint32_t parts, and print those. 173 // For efficiency cut the number into 3 uint32_t parts, and print those.
173 uint32_t part2 = number % kTen7; 174 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
174 number /= kTen7; 175 number /= kTen7;
175 uint32_t part1 = number % kTen7; 176 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
176 uint32_t part0 = number / kTen7; 177 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
177 178
178 if (part0 != 0) { 179 if (part0 != 0) {
179 FillDigits32(part0, buffer, length); 180 FillDigits32(part0, buffer, length);
180 FillDigits32FixedLength(part1, 7, buffer, length); 181 FillDigits32FixedLength(part1, 7, buffer, length);
181 FillDigits32FixedLength(part2, 7, buffer, length); 182 FillDigits32FixedLength(part2, 7, buffer, length);
182 } else if (part1 != 0) { 183 } else if (part1 != 0) {
183 FillDigits32(part1, buffer, length); 184 FillDigits32(part1, buffer, length);
184 FillDigits32FixedLength(part2, 7, buffer, length); 185 FillDigits32FixedLength(part2, 7, buffer, length);
185 } else { 186 } else {
186 FillDigits32(part2, buffer, length); 187 FillDigits32(part2, buffer, length);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 int point = -exponent; 243 int point = -exponent;
243 for (int i = 0; i < fractional_count; ++i) { 244 for (int i = 0; i < fractional_count; ++i) {
244 if (fractionals == 0) break; 245 if (fractionals == 0) break;
245 // Instead of multiplying by 10 we multiply by 5 and adjust the point 246 // Instead of multiplying by 10 we multiply by 5 and adjust the point
246 // location. This way the fractionals variable will not overflow. 247 // location. This way the fractionals variable will not overflow.
247 // Invariant at the beginning of the loop: fractionals < 2^point. 248 // Invariant at the beginning of the loop: fractionals < 2^point.
248 // Initially we have: point <= 64 and fractionals < 2^56 249 // Initially we have: point <= 64 and fractionals < 2^56
249 // After each iteration the point is decremented by one. 250 // After each iteration the point is decremented by one.
250 // Note that 5^3 = 125 < 128 = 2^7. 251 // Note that 5^3 = 125 < 128 = 2^7.
251 // Therefore three iterations of this loop will not overflow fractionals 252 // Therefore three iterations of this loop will not overflow fractionals
252 // (even without the subtraction at the end of the loop body). At this tim e 253 // (even without the subtraction at the end of the loop body). At this
253 // point will satisfy point <= 61 and therefore fractionals < 2^point and 254 // time point will satisfy point <= 61 and therefore fractionals < 2^point
254 // any further multiplication of fractionals by 5 will not overflow. 255 // and any further multiplication of fractionals by 5 will not overflow.
255 fractionals *= 5; 256 fractionals *= 5;
256 point--; 257 point--;
257 int digit = static_cast<int>(fractionals >> point); 258 int digit = static_cast<int>(fractionals >> point);
258 buffer[*length] = '0' + digit; 259 buffer[*length] = '0' + digit;
259 (*length)++; 260 (*length)++;
260 fractionals -= static_cast<uint64_t>(digit) << point; 261 fractionals -= static_cast<uint64_t>(digit) << point;
261 } 262 }
262 // If the first bit after the point is set we have to round up. 263 // If the first bit after the point is set we have to round up.
263 if (((fractionals >> (point - 1)) & 1) == 1) { 264 if (((fractionals >> (point - 1)) & 1) == 1) {
264 RoundUp(buffer, length, decimal_point); 265 RoundUp(buffer, length, decimal_point);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 // We know that v = significand * 2^exponent. 332 // We know that v = significand * 2^exponent.
332 // And the exponent > 11. 333 // And the exponent > 11.
333 // We simplify the task by dividing v by 10^17. 334 // We simplify the task by dividing v by 10^17.
334 // The quotient delivers the first digits, and the remainder fits into a 64 335 // The quotient delivers the first digits, and the remainder fits into a 64
335 // bit number. 336 // bit number.
336 // Dividing by 10^17 is equivalent to dividing by 5^17*2^17. 337 // Dividing by 10^17 is equivalent to dividing by 5^17*2^17.
337 const uint64_t kFive17 = V8_2PART_UINT64_C(0xB1, A2BC2EC5); // 5^17 338 const uint64_t kFive17 = V8_2PART_UINT64_C(0xB1, A2BC2EC5); // 5^17
338 uint64_t divisor = kFive17; 339 uint64_t divisor = kFive17;
339 int divisor_power = 17; 340 int divisor_power = 17;
340 uint64_t dividend = significand; 341 uint64_t dividend = significand;
341 uint64_t quotient; 342 uint32_t quotient;
342 uint64_t remainder; 343 uint64_t remainder;
343 // Let v = f * 2^e with f == significand and e == exponent. 344 // Let v = f * 2^e with f == significand and e == exponent.
344 // Then need q (quotient) and r (remainder) as follows: 345 // Then need q (quotient) and r (remainder) as follows:
345 // v = q * 10^17 + r 346 // v = q * 10^17 + r
346 // f * 2^e = q * 10^17 + r 347 // f * 2^e = q * 10^17 + r
347 // f * 2^e = q * 5^17 * 2^17 + r 348 // f * 2^e = q * 5^17 * 2^17 + r
348 // If e > 17 then 349 // If e > 17 then
349 // f * 2^(e-17) = q * 5^17 + r/2^17 350 // f * 2^(e-17) = q * 5^17 + r/2^17
350 // else 351 // else
351 // f = q * 5^17 * 2^(17-e) + r/2^e 352 // f = q * 5^17 * 2^(17-e) + r/2^e
352 if (exponent > divisor_power) { 353 if (exponent > divisor_power) {
353 // We only allow exponents of up to 20 and therefore (17 - e) <= 3 354 // We only allow exponents of up to 20 and therefore (17 - e) <= 3
354 dividend <<= exponent - divisor_power; 355 dividend <<= exponent - divisor_power;
355 quotient = dividend / divisor; 356 quotient = static_cast<uint32_t>(dividend / divisor);
356 remainder = (dividend % divisor) << divisor_power; 357 remainder = (dividend % divisor) << divisor_power;
357 } else { 358 } else {
358 divisor <<= divisor_power - exponent; 359 divisor <<= divisor_power - exponent;
359 quotient = dividend / divisor; 360 quotient = static_cast<uint32_t>(dividend / divisor);
360 remainder = (dividend % divisor) << exponent; 361 remainder = (dividend % divisor) << exponent;
361 } 362 }
362 FillDigits32(quotient, buffer, length); 363 FillDigits32(quotient, buffer, length);
363 FillDigits64FixedLength(remainder, divisor_power, buffer, length); 364 FillDigits64FixedLength(remainder, divisor_power, buffer, length);
364 *decimal_point = *length; 365 *decimal_point = *length;
365 } else if (exponent >= 0) { 366 } else if (exponent >= 0) {
366 // 0 <= exponent <= 11 367 // 0 <= exponent <= 11
367 significand <<= exponent; 368 significand <<= exponent;
368 FillDigits64(significand, buffer, length); 369 FillDigits64(significand, buffer, length);
369 *decimal_point = *length; 370 *decimal_point = *length;
(...skipping 25 matching lines...) Expand all
395 buffer[*length] = '\0'; 396 buffer[*length] = '\0';
396 if ((*length) == 0) { 397 if ((*length) == 0) {
397 // The string is empty and the decimal_point thus has no importance. Mimick 398 // 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 // Gay's dtoa and and set it to -fractional_count.
399 *decimal_point = -fractional_count; 400 *decimal_point = -fractional_count;
400 } 401 }
401 return true; 402 return true;
402 } 403 }
403 404
404 } } // namespace v8::internal 405 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/fixed-dtoa.h ('k') | test/cctest/SConscript » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698