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

Side by Side Diff: Source/WTF/wtf/dtoa/utils.h

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

Powered by Google App Engine
This is Rietveld 408576698