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