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