You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 line
11 KiB

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