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

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

Issue 20652002: Fix trailing whitespace in scripts and misc. files (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Don't change literal diff. Created 7 years, 5 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 | « Source/wtf/dtoa/fast-dtoa.cc ('k') | Source/wtf/dtoa/strtod.cc » ('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 18 matching lines...) Expand all
29 29
30 #include <math.h> 30 #include <math.h>
31 31
32 #include "double.h" 32 #include "double.h"
33 #include "fixed-dtoa.h" 33 #include "fixed-dtoa.h"
34 #include "wtf/UnusedParam.h" 34 #include "wtf/UnusedParam.h"
35 35
36 namespace WTF { 36 namespace WTF {
37 37
38 namespace double_conversion { 38 namespace double_conversion {
39 39
40 // Represents a 128bit type. This class should be replaced by a native type on 40 // Represents a 128bit type. This class should be replaced by a native type on
41 // platforms that support 128bit integers. 41 // platforms that support 128bit integers.
42 class UInt128 { 42 class UInt128 {
43 public: 43 public:
44 UInt128() : high_bits_(0), low_bits_(0) { } 44 UInt128() : high_bits_(0), low_bits_(0) { }
45 UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { } 45 UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { }
46 46
47 void Multiply(uint32_t multiplicand) { 47 void Multiply(uint32_t multiplicand) {
48 uint64_t accumulator; 48 uint64_t accumulator;
49 49
50 accumulator = (low_bits_ & kMask32) * multiplicand; 50 accumulator = (low_bits_ & kMask32) * multiplicand;
51 uint32_t part = static_cast<uint32_t>(accumulator & kMask32); 51 uint32_t part = static_cast<uint32_t>(accumulator & kMask32);
52 accumulator >>= 32; 52 accumulator >>= 32;
53 accumulator = accumulator + (low_bits_ >> 32) * multiplicand; 53 accumulator = accumulator + (low_bits_ >> 32) * multiplicand;
54 low_bits_ = (accumulator << 32) + part; 54 low_bits_ = (accumulator << 32) + part;
55 accumulator >>= 32; 55 accumulator >>= 32;
56 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand; 56 accumulator = accumulator + (high_bits_ & kMask32) * multiplicand;
57 part = static_cast<uint32_t>(accumulator & kMask32); 57 part = static_cast<uint32_t>(accumulator & kMask32);
58 accumulator >>= 32; 58 accumulator >>= 32;
59 accumulator = accumulator + (high_bits_ >> 32) * multiplicand; 59 accumulator = accumulator + (high_bits_ >> 32) * multiplicand;
60 high_bits_ = (accumulator << 32) + part; 60 high_bits_ = (accumulator << 32) + part;
61 ASSERT((accumulator >> 32) == 0); 61 ASSERT((accumulator >> 32) == 0);
62 } 62 }
63 63
64 void Shift(int shift_amount) { 64 void Shift(int shift_amount) {
65 ASSERT(-64 <= shift_amount && shift_amount <= 64); 65 ASSERT(-64 <= shift_amount && shift_amount <= 64);
66 if (shift_amount == 0) { 66 if (shift_amount == 0) {
67 return; 67 return;
68 } else if (shift_amount == -64) { 68 } else if (shift_amount == -64) {
69 high_bits_ = low_bits_; 69 high_bits_ = low_bits_;
70 low_bits_ = 0; 70 low_bits_ = 0;
71 } else if (shift_amount == 64) { 71 } else if (shift_amount == 64) {
72 low_bits_ = high_bits_; 72 low_bits_ = high_bits_;
73 high_bits_ = 0; 73 high_bits_ = 0;
74 } else if (shift_amount <= 0) { 74 } else if (shift_amount <= 0) {
75 high_bits_ <<= -shift_amount; 75 high_bits_ <<= -shift_amount;
76 high_bits_ += low_bits_ >> (64 + shift_amount); 76 high_bits_ += low_bits_ >> (64 + shift_amount);
77 low_bits_ <<= -shift_amount; 77 low_bits_ <<= -shift_amount;
78 } else { 78 } else {
79 low_bits_ >>= shift_amount; 79 low_bits_ >>= shift_amount;
80 low_bits_ += high_bits_ << (64 - shift_amount); 80 low_bits_ += high_bits_ << (64 - shift_amount);
81 high_bits_ >>= shift_amount; 81 high_bits_ >>= shift_amount;
82 } 82 }
83 } 83 }
84 84
85 // Modifies *this to *this MOD (2^power). 85 // Modifies *this to *this MOD (2^power).
86 // Returns *this DIV (2^power). 86 // Returns *this DIV (2^power).
87 int DivModPowerOf2(int power) { 87 int DivModPowerOf2(int power) {
88 if (power >= 64) { 88 if (power >= 64) {
89 int result = static_cast<int>(high_bits_ >> (power - 64)); 89 int result = static_cast<int>(high_bits_ >> (power - 64));
90 high_bits_ -= static_cast<uint64_t>(result) << (power - 64); 90 high_bits_ -= static_cast<uint64_t>(result) << (power - 64);
91 return result; 91 return result;
92 } else { 92 } else {
93 uint64_t part_low = low_bits_ >> power; 93 uint64_t part_low = low_bits_ >> power;
94 uint64_t part_high = high_bits_ << (64 - power); 94 uint64_t part_high = high_bits_ << (64 - power);
95 int result = static_cast<int>(part_low + part_high); 95 int result = static_cast<int>(part_low + part_high);
96 high_bits_ = 0; 96 high_bits_ = 0;
97 low_bits_ -= part_low << power; 97 low_bits_ -= part_low << power;
98 return result; 98 return result;
99 } 99 }
100 } 100 }
101 101
102 bool IsZero() const { 102 bool IsZero() const {
103 return high_bits_ == 0 && low_bits_ == 0; 103 return high_bits_ == 0 && low_bits_ == 0;
104 } 104 }
105 105
106 int BitAt(int position) { 106 int BitAt(int position) {
107 if (position >= 64) { 107 if (position >= 64) {
108 return static_cast<int>(high_bits_ >> (position - 64)) & 1; 108 return static_cast<int>(high_bits_ >> (position - 64)) & 1;
109 } else { 109 } else {
110 return static_cast<int>(low_bits_ >> position) & 1; 110 return static_cast<int>(low_bits_ >> position) & 1;
111 } 111 }
112 } 112 }
113 113
114 private: 114 private:
115 static const uint64_t kMask32 = 0xFFFFFFFF; 115 static const uint64_t kMask32 = 0xFFFFFFFF;
116 // Value == (high_bits_ << 64) + low_bits_ 116 // Value == (high_bits_ << 64) + low_bits_
117 uint64_t high_bits_; 117 uint64_t high_bits_;
118 uint64_t low_bits_; 118 uint64_t low_bits_;
119 }; 119 };
120 120
121 121
122 static const int kDoubleSignificandSize = 53; // Includes the hidden bit. 122 static const int kDoubleSignificandSize = 53; // Includes the hidden bit.
123 123
124 124
125 static void FillDigits32FixedLength(uint32_t number, int requested_length, 125 static void FillDigits32FixedLength(uint32_t number, int requested_length,
126 Vector<char> buffer, int* length) { 126 Vector<char> buffer, int* length) {
127 for (int i = requested_length - 1; i >= 0; --i) { 127 for (int i = requested_length - 1; i >= 0; --i) {
128 buffer[(*length) + i] = '0' + number % 10; 128 buffer[(*length) + i] = '0' + number % 10;
129 number /= 10; 129 number /= 10;
130 } 130 }
131 *length += requested_length; 131 *length += requested_length;
132 } 132 }
133 133
134 134
135 static void FillDigits32(uint32_t number, Vector<char> buffer, int* length) { 135 static void FillDigits32(uint32_t number, Vector<char> buffer, int* length) {
136 int number_length = 0; 136 int number_length = 0;
137 // We fill the digits in reverse order and exchange them afterwards. 137 // We fill the digits in reverse order and exchange them afterwards.
138 while (number != 0) { 138 while (number != 0) {
139 int digit = number % 10; 139 int digit = number % 10;
140 number /= 10; 140 number /= 10;
141 buffer[(*length) + number_length] = '0' + digit; 141 buffer[(*length) + number_length] = '0' + digit;
142 number_length++; 142 number_length++;
143 } 143 }
144 // Exchange the digits. 144 // Exchange the digits.
145 int i = *length; 145 int i = *length;
146 int j = *length + number_length - 1; 146 int j = *length + number_length - 1;
147 while (i < j) { 147 while (i < j) {
148 char tmp = buffer[i]; 148 char tmp = buffer[i];
149 buffer[i] = buffer[j]; 149 buffer[i] = buffer[j];
150 buffer[j] = tmp; 150 buffer[j] = tmp;
151 i++; 151 i++;
152 j--; 152 j--;
153 } 153 }
154 *length += number_length; 154 *length += number_length;
155 } 155 }
156 156
157 157
158 static void FillDigits64FixedLength(uint64_t number, int requested_length, 158 static void FillDigits64FixedLength(uint64_t number, int requested_length,
159 Vector<char> buffer, int* length) { 159 Vector<char> buffer, int* length) {
160 UNUSED_PARAM(requested_length); 160 UNUSED_PARAM(requested_length);
161 const uint32_t kTen7 = 10000000; 161 const uint32_t kTen7 = 10000000;
162 // For efficiency cut the number into 3 uint32_t parts, and print those. 162 // For efficiency cut the number into 3 uint32_t parts, and print those.
163 uint32_t part2 = static_cast<uint32_t>(number % kTen7); 163 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
164 number /= kTen7; 164 number /= kTen7;
165 uint32_t part1 = static_cast<uint32_t>(number % kTen7); 165 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
166 uint32_t part0 = static_cast<uint32_t>(number / kTen7); 166 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
167 167
168 FillDigits32FixedLength(part0, 3, buffer, length); 168 FillDigits32FixedLength(part0, 3, buffer, length);
169 FillDigits32FixedLength(part1, 7, buffer, length); 169 FillDigits32FixedLength(part1, 7, buffer, length);
170 FillDigits32FixedLength(part2, 7, buffer, length); 170 FillDigits32FixedLength(part2, 7, buffer, length);
171 } 171 }
172 172
173 173
174 static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) { 174 static void FillDigits64(uint64_t number, Vector<char> buffer, int* length) {
175 const uint32_t kTen7 = 10000000; 175 const uint32_t kTen7 = 10000000;
176 // For efficiency cut the number into 3 uint32_t parts, and print those. 176 // For efficiency cut the number into 3 uint32_t parts, and print those.
177 uint32_t part2 = static_cast<uint32_t>(number % kTen7); 177 uint32_t part2 = static_cast<uint32_t>(number % kTen7);
178 number /= kTen7; 178 number /= kTen7;
179 uint32_t part1 = static_cast<uint32_t>(number % kTen7); 179 uint32_t part1 = static_cast<uint32_t>(number % kTen7);
180 uint32_t part0 = static_cast<uint32_t>(number / kTen7); 180 uint32_t part0 = static_cast<uint32_t>(number / kTen7);
181 181
182 if (part0 != 0) { 182 if (part0 != 0) {
183 FillDigits32(part0, buffer, length); 183 FillDigits32(part0, buffer, length);
184 FillDigits32FixedLength(part1, 7, buffer, length); 184 FillDigits32FixedLength(part1, 7, buffer, length);
185 FillDigits32FixedLength(part2, 7, buffer, length); 185 FillDigits32FixedLength(part2, 7, buffer, length);
186 } else if (part1 != 0) { 186 } else if (part1 != 0) {
187 FillDigits32(part1, buffer, length); 187 FillDigits32(part1, buffer, length);
188 FillDigits32FixedLength(part2, 7, buffer, length); 188 FillDigits32FixedLength(part2, 7, buffer, length);
189 } else { 189 } else {
190 FillDigits32(part2, buffer, length); 190 FillDigits32(part2, buffer, length);
191 } 191 }
192 } 192 }
193 193
194 194
195 static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) { 195 static void RoundUp(Vector<char> buffer, int* length, int* decimal_point) {
196 // An empty buffer represents 0. 196 // An empty buffer represents 0.
197 if (*length == 0) { 197 if (*length == 0) {
198 buffer[0] = '1'; 198 buffer[0] = '1';
199 *decimal_point = 1; 199 *decimal_point = 1;
200 *length = 1; 200 *length = 1;
201 return; 201 return;
202 } 202 }
203 // Round the last digit until we either have a digit that was not '9' or until 203 // Round the last digit until we either have a digit that was not '9' or until
204 // we reached the first digit. 204 // we reached the first digit.
205 buffer[(*length) - 1]++; 205 buffer[(*length) - 1]++;
206 for (int i = (*length) - 1; i > 0; --i) { 206 for (int i = (*length) - 1; i > 0; --i) {
207 if (buffer[i] != '0' + 10) { 207 if (buffer[i] != '0' + 10) {
208 return; 208 return;
209 } 209 }
210 buffer[i] = '0'; 210 buffer[i] = '0';
211 buffer[i - 1]++; 211 buffer[i - 1]++;
212 } 212 }
213 // If the first digit is now '0' + 10, we would need to set it to '0' an d add 213 // If the first digit is now '0' + 10, we would need to set it to '0' an d add
214 // a '1' in front. However we reach the first digit only if all followin g 214 // a '1' in front. However we reach the first digit only if all followin g
215 // digits had been '9' before rounding up. Now all trailing digits are ' 0' and 215 // digits had been '9' before rounding up. Now all trailing digits are ' 0' and
216 // we simply switch the first digit to '1' and update the decimal-point 216 // we simply switch the first digit to '1' and update the decimal-point
217 // (indicating that the point is now one digit to the right). 217 // (indicating that the point is now one digit to the right).
218 if (buffer[0] == '0' + 10) { 218 if (buffer[0] == '0' + 10) {
219 buffer[0] = '1'; 219 buffer[0] = '1';
220 (*decimal_point)++; 220 (*decimal_point)++;
221 } 221 }
222 } 222 }
223 223
224 224
225 // The given fractionals number represents a fixed-point number with binary 225 // The given fractionals number represents a fixed-point number with binary
226 // point at bit (-exponent). 226 // point at bit (-exponent).
227 // Preconditions: 227 // Preconditions:
228 // -128 <= exponent <= 0. 228 // -128 <= exponent <= 0.
229 // 0 <= fractionals * 2^exponent < 1 229 // 0 <= fractionals * 2^exponent < 1
230 // The buffer holds the result. 230 // The buffer holds the result.
231 // The function will round its result. During the rounding-process digits no t 231 // The function will round its result. During the rounding-process digits no t
232 // generated by this function might be updated, and the decimal-point variab le 232 // generated by this function might be updated, and the decimal-point variab le
233 // might be updated. If this function generates the digits 99 and the buffer 233 // might be updated. If this function generates the digits 99 and the buffer
234 // already contained "199" (thus yielding a buffer of "19999") then a 234 // already contained "199" (thus yielding a buffer of "19999") then a
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 point--; 281 point--;
282 int digit = fractionals128.DivModPowerOf2(point); 282 int digit = fractionals128.DivModPowerOf2(point);
283 buffer[*length] = '0' + digit; 283 buffer[*length] = '0' + digit;
284 (*length)++; 284 (*length)++;
285 } 285 }
286 if (fractionals128.BitAt(point - 1) == 1) { 286 if (fractionals128.BitAt(point - 1) == 1) {
287 RoundUp(buffer, length, decimal_point); 287 RoundUp(buffer, length, decimal_point);
288 } 288 }
289 } 289 }
290 } 290 }
291 291
292 292
293 // Removes leading and trailing zeros. 293 // Removes leading and trailing zeros.
294 // If leading zeros are removed then the decimal point position is adjusted. 294 // If leading zeros are removed then the decimal point position is adjusted.
295 static void TrimZeros(Vector<char> buffer, int* length, int* decimal_point) { 295 static void TrimZeros(Vector<char> buffer, int* length, int* decimal_point) {
296 while (*length > 0 && buffer[(*length) - 1] == '0') { 296 while (*length > 0 && buffer[(*length) - 1] == '0') {
297 (*length)--; 297 (*length)--;
298 } 298 }
299 int first_non_zero = 0; 299 int first_non_zero = 0;
300 while (first_non_zero < *length && buffer[first_non_zero] == '0') { 300 while (first_non_zero < *length && buffer[first_non_zero] == '0') {
301 first_non_zero++; 301 first_non_zero++;
302 } 302 }
303 if (first_non_zero != 0) { 303 if (first_non_zero != 0) {
304 for (int i = first_non_zero; i < *length; ++i) { 304 for (int i = first_non_zero; i < *length; ++i) {
305 buffer[i - first_non_zero] = buffer[i]; 305 buffer[i - first_non_zero] = buffer[i];
306 } 306 }
307 *length -= first_non_zero; 307 *length -= first_non_zero;
308 *decimal_point -= first_non_zero; 308 *decimal_point -= first_non_zero;
309 } 309 }
310 } 310 }
311 311
312 312
313 bool FastFixedDtoa(double v, 313 bool FastFixedDtoa(double v,
314 int fractional_count, 314 int fractional_count,
315 Vector<char> buffer, 315 Vector<char> buffer,
316 int* length, 316 int* length,
317 int* decimal_point) { 317 int* decimal_point) {
318 const uint32_t kMaxUInt32 = 0xFFFFFFFF; 318 const uint32_t kMaxUInt32 = 0xFFFFFFFF;
319 uint64_t significand = Double(v).Significand(); 319 uint64_t significand = Double(v).Significand();
320 int exponent = Double(v).Exponent(); 320 int exponent = Double(v).Exponent();
321 // v = significand * 2^exponent (with significand a 53bit integer). 321 // v = significand * 2^exponent (with significand a 53bit integer).
322 // If the exponent is larger than 20 (i.e. we may have a 73bit number) t hen we 322 // If the exponent is larger than 20 (i.e. we may have a 73bit number) t hen we
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 } 397 }
398 TrimZeros(buffer, length, decimal_point); 398 TrimZeros(buffer, length, decimal_point);
399 buffer[*length] = '\0'; 399 buffer[*length] = '\0';
400 if ((*length) == 0) { 400 if ((*length) == 0) {
401 // The string is empty and the decimal_point thus has no importance. Mimick 401 // The string is empty and the decimal_point thus has no importance. Mimick
402 // Gay's dtoa and and set it to -fractional_count. 402 // Gay's dtoa and and set it to -fractional_count.
403 *decimal_point = -fractional_count; 403 *decimal_point = -fractional_count;
404 } 404 }
405 return true; 405 return true;
406 } 406 }
407 407
408 } // namespace double_conversion 408 } // namespace double_conversion
409 409
410 } // namespace WTF 410 } // namespace WTF
OLDNEW
« no previous file with comments | « Source/wtf/dtoa/fast-dtoa.cc ('k') | Source/wtf/dtoa/strtod.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698