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