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

Side by Side Diff: third_party/WebKit/Source/wtf/dtoa/utils.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 26 matching lines...) Expand all
37 // Double operations detection based on target architecture. 37 // Double operations detection based on target architecture.
38 // Linux uses a 80bit wide floating point stack on x86. This induces double 38 // Linux uses a 80bit wide floating point stack on x86. This induces double
39 // rounding, which in turn leads to wrong results. 39 // rounding, which in turn leads to wrong results.
40 // An easy way to test if the floating-point operations are correct is to 40 // An easy way to test if the floating-point operations are correct is to
41 // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then 41 // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then
42 // the result is equal to 89255e-22. 42 // the result is equal to 89255e-22.
43 // The best way to test this, is to create a division-function and to compare 43 // The best way to test this, is to create a division-function and to compare
44 // the output of the division with the expected result. (Inlining must be 44 // the output of the division with the expected result. (Inlining must be
45 // disabled.) 45 // disabled.)
46 // On Linux,x86 89255e-22 != Div_double(89255.0/1e22) 46 // On Linux,x86 89255e-22 != Div_double(89255.0/1e22)
47 #if defined(_M_X64) || defined(__x86_64__) || \ 47 #if defined(_M_X64) || defined(__x86_64__) || \
48 defined(__ARMEL__) || defined(__aarch64__) || \ 48 defined(__ARMEL__) || defined(__aarch64__) || \
49 defined(__MIPSEL__) 49 defined(__MIPSEL__)
50 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1 50 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
51 #elif defined(_M_IX86) || defined(__i386__) 51 #elif defined(_M_IX86) || defined(__i386__)
52 #if defined(_WIN32) 52 #if defined(_WIN32)
53 // Windows uses a 64bit wide floating point stack. 53 // Windows uses a 64bit wide floating point stack.
54 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1 54 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
55 #else 55 #else
56 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 56 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
57 #endif // _WIN32 57 #endif // _WIN32
58 #else 58 #else
59 #error Target architecture was not detected as supported by Double-Conversion. 59 #error Target architecture was not detected as supported by Double-Conversion.
60 #endif 60 #endif
61 61
62
63 #if defined(_WIN32) && !defined(__MINGW32__) 62 #if defined(_WIN32) && !defined(__MINGW32__)
64 63
65 typedef signed char int8_t; 64 typedef signed char int8_t;
66 typedef unsigned char uint8_t; 65 typedef unsigned char uint8_t;
67 typedef short int16_t; // NOLINT 66 typedef short int16_t; // NOLINT
68 typedef unsigned short uint16_t; // NOLINT 67 typedef unsigned short uint16_t; // NOLINT
69 typedef int int32_t; 68 typedef int int32_t;
70 typedef unsigned int uint32_t; 69 typedef unsigned int uint32_t;
71 typedef __int64 int64_t; 70 typedef __int64 int64_t;
72 typedef unsigned __int64 uint64_t; 71 typedef unsigned __int64 uint64_t;
73 // intptr_t and friends are defined in crtdefs.h through stdio.h. 72 // intptr_t and friends are defined in crtdefs.h through stdio.h.
74 73
75 #else 74 #else
76 75
77 #include <stdint.h> 76 #include <stdint.h>
78 77
79 #endif 78 #endif
80 79
81 // The following macro works on both 32 and 64-bit platforms. 80 // The following macro works on both 32 and 64-bit platforms.
82 // Usage: instead of writing 0x1234567890123456 81 // Usage: instead of writing 0x1234567890123456
83 // write UINT64_2PART_C(0x12345678,90123456); 82 // write UINT64_2PART_C(0x12345678,90123456);
84 #define UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u)) 83 #define UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
85 84
86
87 // The expression ARRAY_SIZE(a) is a compile-time constant of type 85 // The expression ARRAY_SIZE(a) is a compile-time constant of type
88 // size_t which represents the number of elements of the given 86 // size_t which represents the number of elements of the given
89 // array. You should only use ARRAY_SIZE on statically allocated 87 // array. You should only use ARRAY_SIZE on statically allocated
90 // arrays. 88 // arrays.
91 #define ARRAY_SIZE(a) \ 89 #define ARRAY_SIZE(a) \
92 ((sizeof(a) / sizeof(*(a))) / \ 90 ((sizeof(a) / sizeof(*(a))) / \
93 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 91 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
94 92
95 // A macro to disallow the evil copy constructor and operator= functions 93 // A macro to disallow the evil copy constructor and operator= functions
96 // This should be used in the private: declarations for a class 94 // This should be used in the private: declarations for a class
97 #ifndef DISALLOW_COPY_AND_ASSIGN 95 #ifndef DISALLOW_COPY_AND_ASSIGN
98 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 96 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
99 TypeName(const TypeName&); \ 97 TypeName(const TypeName&); \
100 void operator=(const TypeName&) 98 void operator=(const TypeName&)
101 #endif // DISALLOW_COPY_AND_ASSIGN 99 #endif // DISALLOW_COPY_AND_ASSIGN
102 100
103 // A macro to disallow all the implicit constructors, namely the 101 // A macro to disallow all the implicit constructors, namely the
104 // default constructor, copy constructor and operator= functions. 102 // default constructor, copy constructor and operator= functions.
105 // 103 //
106 // This should be used in the private: declarations for a class 104 // This should be used in the private: declarations for a class
107 // that wants to prevent anyone from instantiating it. This is 105 // that wants to prevent anyone from instantiating it. This is
108 // especially useful for classes containing only static methods. 106 // especially useful for classes containing only static methods.
109 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ 107 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
110 TypeName() = delete; \ 108 TypeName() = delete; \
111 DISALLOW_COPY_AND_ASSIGN(TypeName) 109 DISALLOW_COPY_AND_ASSIGN(TypeName)
112 110
113 namespace WTF { 111 namespace WTF {
114 112
115 namespace double_conversion { 113 namespace double_conversion {
116 114
117 static const int kCharSize = sizeof(char); 115 static const int kCharSize = sizeof(char);
118 116
119 // Returns the maximum of the two parameters. 117 // Returns the maximum of the two parameters.
120 template <typename T> 118 template <typename T>
121 static T Max(T a, T b) { 119 static T Max(T a, T b) {
122 return a < b ? b : a; 120 return a < b ? b : a;
121 }
122
123 // Returns the minimum of the two parameters.
124 template <typename T>
125 static T Min(T a, T b) {
126 return a < b ? a : b;
127 }
128
129 inline int StrLength(const char* string) {
130 size_t length = strlen(string);
131 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
132 return static_cast<int>(length);
133 }
134
135 // This is a simplified version of V8's Vector class.
136 template <typename T>
137 class Vector {
138 public:
139 Vector()
140 : start_(NULL), length_(0) {}
141 Vector(T* data, int length)
142 : start_(data), length_(length) {
143 ASSERT(length == 0 || (length > 0 && data != NULL));
144 }
145
146 // Returns a vector using the same backing storage as this one,
147 // spanning from and including 'from', to but not including 'to'.
148 Vector<T> SubVector(int from, int to) {
149 ASSERT(to <= length_);
150 ASSERT(from < to);
151 ASSERT(0 <= from);
152 return Vector<T>(start() + from, to - from);
153 }
154
155 // Returns the length of the vector.
156 int length() const { return length_; }
157
158 // Returns whether or not the vector is empty.
159 bool is_empty() const { return length_ == 0; }
160
161 // Returns the pointer to the start of the data in the vector.
162 T* start() const { return start_; }
163
164 // Access individual vector elements - checks bounds in debug mode.
165 T& operator[](int index) const {
166 ASSERT(0 <= index && index < length_);
167 return start_[index];
168 }
169
170 T& first() { return start_[0]; }
171
172 T& last() { return start_[length_ - 1]; }
173
174 private:
175 T* start_;
176 int length_;
177 };
178
179 // Helper class for building result strings in a character buffer. The
180 // purpose of the class is to use safe operations that checks the
181 // buffer bounds on all operations in debug mode.
182 class StringBuilder {
183 public:
184 StringBuilder(char* buffer, int size)
185 : buffer_(buffer, size), position_(0) {}
186
187 ~StringBuilder() {
188 if (!is_finalized())
189 Finalize();
190 }
191
192 int size() const { return buffer_.length(); }
193
194 // Get the current position in the builder.
195 int position() const {
196 ASSERT(!is_finalized());
197 return position_;
198 }
199
200 // Set the current position in the builder.
201 void SetPosition(int position) {
202 ASSERT(!is_finalized());
203 ASSERT_WITH_SECURITY_IMPLICATION(position < size());
204 position_ = position;
205 }
206
207 // Reset the position.
208 void Reset() { position_ = 0; }
209
210 // Add a single character to the builder. It is not allowed to add
211 // 0-characters; use the Finalize() method to terminate the string
212 // instead.
213 void AddCharacter(char c) {
214 ASSERT(c != '\0');
215 ASSERT(!is_finalized() && position_ < buffer_.length());
216 buffer_[position_++] = c;
217 }
218
219 // Add an entire string to the builder. Uses strlen() internally to
220 // compute the length of the input string.
221 void AddString(const char* s) {
222 AddSubstring(s, StrLength(s));
223 }
224
225 // Add the first 'n' characters of the given string 's' to the
226 // builder. The input string must have enough characters.
227 void AddSubstring(const char* s, int n) {
228 ASSERT(!is_finalized() && position_ + n < buffer_.length());
229 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<size_t>(n) <= strlen(s));
230 memcpy(&buffer_[position_], s, n * kCharSize);
231 position_ += n;
232 }
233
234 // Add character padding to the builder. If count is non-positive,
235 // nothing is added to the builder.
236 void AddPadding(char c, int count) {
237 for (int i = 0; i < count; i++) {
238 AddCharacter(c);
123 } 239 }
124 240 }
125 241
126 // Returns the minimum of the two parameters. 242 // Finalize the string by 0-terminating it and returning the buffer.
127 template <typename T> 243 char* Finalize() {
128 static T Min(T a, T b) { 244 ASSERT(!is_finalized() && position_ < buffer_.length());
129 return a < b ? a : b; 245 buffer_[position_] = '\0';
130 } 246 // Make sure nobody managed to add a 0-character to the
131 247 // buffer while building the string.
132 248 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
133 inline int StrLength(const char* string) { 249 position_ = -1;
134 size_t length = strlen(string); 250 ASSERT(is_finalized());
135 ASSERT(length == static_cast<size_t>(static_cast<int>(length))); 251 return buffer_.start();
136 return static_cast<int>(length); 252 }
137 } 253
138 254 private:
139 // This is a simplified version of V8's Vector class. 255 Vector<char> buffer_;
140 template <typename T> 256 int position_;
141 class Vector { 257
142 public: 258 bool is_finalized() const { return position_ < 0; }
143 Vector() : start_(NULL), length_(0) {} 259
144 Vector(T* data, int length) : start_(data), length_(length) { 260 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
145 ASSERT(length == 0 || (length > 0 && data != NULL)); 261 };
146 } 262
147 263 // The type-based aliasing rule allows the compiler to assume that pointers of
148 // Returns a vector using the same backing storage as this one, 264 // different types (for some definition of different) never alias each other.
149 // spanning from and including 'from', to but not including 'to'. 265 // Thus the following code does not work:
150 Vector<T> SubVector(int from, int to) { 266 //
151 ASSERT(to <= length_); 267 // float f = foo();
152 ASSERT(from < to); 268 // int fbits = *(int*)(&f);
153 ASSERT(0 <= from); 269 //
154 return Vector<T>(start() + from, to - from); 270 // The compiler 'knows' that the int pointer can't refer to f since the types
155 } 271 // don't match, so the compiler may cache f in a register, leaving random data
156 272 // in fbits. Using C++ style casts makes no difference, however a pointer to
157 // Returns the length of the vector. 273 // char data is assumed to alias any other pointer. This is the 'memcpy
158 int length() const { return length_; } 274 // exception'.
159 275 //
160 // Returns whether or not the vector is empty. 276 // Bit_cast uses the memcpy exception to move the bits from a variable of one
161 bool is_empty() const { return length_ == 0; } 277 // type of a variable of another type. Of course the end result is likely to
162 278 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
163 // Returns the pointer to the start of the data in the vector. 279 // will completely optimize BitCast away.
164 T* start() const { return start_; } 280 //
165 281 // There is an additional use for BitCast.
166 // Access individual vector elements - checks bounds in debug mode. 282 // Recent gccs will warn when they see casts that may result in breakage due to
167 T& operator[](int index) const { 283 // the type-based aliasing rule. If you have checked that there is no breakage
168 ASSERT(0 <= index && index < length_); 284 // you can use BitCast to cast one pointer type to another. This confuses gcc
169 return start_[index]; 285 // enough that it can no longer see that you have cast one pointer type to
170 } 286 // another thus avoiding the warning.
171 287 template <class Dest, class Source>
172 T& first() { return start_[0]; } 288 inline Dest BitCast(const Source& source) {
173 289 // Compile time assertion: sizeof(Dest) == sizeof(Source)
174 T& last() { return start_[length_ - 1]; } 290 // A compile error here means your Dest and Source have different sizes.
175 291 static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal");
176 private: 292
177 T* start_; 293 Dest dest;
178 int length_; 294 memcpy(&dest, &source, sizeof(dest));
179 }; 295 return dest;
180 296 }
181 297
182 // Helper class for building result strings in a character buffer. The 298 template <class Dest, class Source>
183 // purpose of the class is to use safe operations that checks the 299 inline Dest BitCast(Source* source) {
184 // buffer bounds on all operations in debug mode. 300 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
185 class StringBuilder { 301 }
186 public:
187 StringBuilder(char* buffer, int size)
188 : buffer_(buffer, size), position_(0) { }
189
190 ~StringBuilder() { if (!is_finalized()) Finalize(); }
191
192 int size() const { return buffer_.length(); }
193
194 // Get the current position in the builder.
195 int position() const {
196 ASSERT(!is_finalized());
197 return position_;
198 }
199
200 // Set the current position in the builder.
201 void SetPosition(int position)
202 {
203 ASSERT(!is_finalized());
204 ASSERT_WITH_SECURITY_IMPLICATION(position < size());
205 position_ = position;
206 }
207
208 // Reset the position.
209 void Reset() { position_ = 0; }
210
211 // Add a single character to the builder. It is not allowed to add
212 // 0-characters; use the Finalize() method to terminate the string
213 // instead.
214 void AddCharacter(char c) {
215 ASSERT(c != '\0');
216 ASSERT(!is_finalized() && position_ < buffer_.length());
217 buffer_[position_++] = c;
218 }
219
220 // Add an entire string to the builder. Uses strlen() internally to
221 // compute the length of the input string.
222 void AddString(const char* s) {
223 AddSubstring(s, StrLength(s));
224 }
225
226 // Add the first 'n' characters of the given string 's' to the
227 // builder. The input string must have enough characters.
228 void AddSubstring(const char* s, int n) {
229 ASSERT(!is_finalized() && position_ + n < buffer_.length());
230 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<size_t>(n) <= strlen(s) );
231 memcpy(&buffer_[position_], s, n * kCharSize);
232 position_ += n;
233 }
234
235
236 // Add character padding to the builder. If count is non-positive,
237 // nothing is added to the builder.
238 void AddPadding(char c, int count) {
239 for (int i = 0; i < count; i++) {
240 AddCharacter(c);
241 }
242 }
243
244 // Finalize the string by 0-terminating it and returning the buffer.
245 char* Finalize() {
246 ASSERT(!is_finalized() && position_ < buffer_.length());
247 buffer_[position_] = '\0';
248 // Make sure nobody managed to add a 0-character to the
249 // buffer while building the string.
250 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
251 position_ = -1;
252 ASSERT(is_finalized());
253 return buffer_.start();
254 }
255
256 private:
257 Vector<char> buffer_;
258 int position_;
259
260 bool is_finalized() const { return position_ < 0; }
261
262 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
263 };
264
265 // The type-based aliasing rule allows the compiler to assume that pointers of
266 // different types (for some definition of different) never alias each other .
267 // Thus the following code does not work:
268 //
269 // float f = foo();
270 // int fbits = *(int*)(&f);
271 //
272 // The compiler 'knows' that the int pointer can't refer to f since the type s
273 // don't match, so the compiler may cache f in a register, leaving random da ta
274 // in fbits. Using C++ style casts makes no difference, however a pointer t o
275 // char data is assumed to alias any other pointer. This is the 'memcpy
276 // exception'.
277 //
278 // Bit_cast uses the memcpy exception to move the bits from a variable of on e
279 // type of a variable of another type. Of course the end result is likely t o
280 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
281 // will completely optimize BitCast away.
282 //
283 // There is an additional use for BitCast.
284 // Recent gccs will warn when they see casts that may result in breakage due to
285 // the type-based aliasing rule. If you have checked that there is no break age
286 // you can use BitCast to cast one pointer type to another. This confuses g cc
287 // enough that it can no longer see that you have cast one pointer type to
288 // another thus avoiding the warning.
289 template <class Dest, class Source>
290 inline Dest BitCast(const Source& source) {
291 // Compile time assertion: sizeof(Dest) == sizeof(Source)
292 // A compile error here means your Dest and Source have different sizes.
293 static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal");
294
295 Dest dest;
296 memcpy(&dest, &source, sizeof(dest));
297 return dest;
298 }
299
300 template <class Dest, class Source>
301 inline Dest BitCast(Source* source) {
302 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
303 }
304 302
305 } // namespace double_conversion 303 } // namespace double_conversion
306 304
307 } // namespace WTF 305 } // namespace WTF
308 306
309 #endif // DOUBLE_CONVERSION_UTILS_H_ 307 #endif // DOUBLE_CONVERSION_UTILS_H_
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa/strtod.h ('k') | third_party/WebKit/Source/wtf/dtoa_test.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698