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