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