Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:28

0001 // Copyright 2010 the V8 project authors. All rights reserved.
0002 // Redistribution and use in source and binary forms, with or without
0003 // modification, are permitted provided that the following conditions are
0004 // met:
0005 //
0006 //     * Redistributions of source code must retain the above copyright
0007 //       notice, this list of conditions and the following disclaimer.
0008 //     * Redistributions in binary form must reproduce the above
0009 //       copyright notice, this list of conditions and the following
0010 //       disclaimer in the documentation and/or other materials provided
0011 //       with the distribution.
0012 //     * Neither the name of Google Inc. nor the names of its
0013 //       contributors may be used to endorse or promote products derived
0014 //       from this software without specific prior written permission.
0015 //
0016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027 
0028 #ifndef DOUBLE_CONVERSION_UTILS_H_
0029 #define DOUBLE_CONVERSION_UTILS_H_
0030 
0031 // Use DOUBLE_CONVERSION_NON_PREFIXED_MACROS to get unprefixed macros as was
0032 // the case in double-conversion releases prior to 3.1.6
0033 
0034 #include <cstdlib>
0035 #include <cstring>
0036 
0037 // For pre-C++11 compatibility
0038 #if __cplusplus >= 201103L
0039 #define DOUBLE_CONVERSION_NULLPTR nullptr
0040 #else
0041 #define DOUBLE_CONVERSION_NULLPTR NULL
0042 #endif
0043 
0044 #include <cassert>
0045 #ifndef DOUBLE_CONVERSION_ASSERT
0046 #define DOUBLE_CONVERSION_ASSERT(condition)         \
0047     assert(condition)
0048 #endif
0049 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(ASSERT)
0050 #define ASSERT DOUBLE_CONVERSION_ASSERT
0051 #endif
0052 
0053 #ifndef DOUBLE_CONVERSION_UNIMPLEMENTED
0054 #define DOUBLE_CONVERSION_UNIMPLEMENTED() (abort())
0055 #endif
0056 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UNIMPLEMENTED)
0057 #define UNIMPLEMENTED DOUBLE_CONVERSION_UNIMPLEMENTED
0058 #endif
0059 
0060 #ifndef DOUBLE_CONVERSION_NO_RETURN
0061 #ifdef _MSC_VER
0062 #define DOUBLE_CONVERSION_NO_RETURN __declspec(noreturn)
0063 #else
0064 #define DOUBLE_CONVERSION_NO_RETURN __attribute__((noreturn))
0065 #endif
0066 #endif
0067 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(NO_RETURN)
0068 #define NO_RETURN DOUBLE_CONVERSION_NO_RETURN
0069 #endif
0070 
0071 #ifndef DOUBLE_CONVERSION_UNREACHABLE
0072 #ifdef _MSC_VER
0073 void DOUBLE_CONVERSION_NO_RETURN abort_noreturn();
0074 inline void abort_noreturn() { abort(); }
0075 #define DOUBLE_CONVERSION_UNREACHABLE()   (abort_noreturn())
0076 #else
0077 #define DOUBLE_CONVERSION_UNREACHABLE()   (abort())
0078 #endif
0079 #endif
0080 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UNREACHABLE)
0081 #define UNREACHABLE DOUBLE_CONVERSION_UNREACHABLE
0082 #endif
0083 
0084 // Not all compilers support __has_attribute and combining a check for both
0085 // ifdef and __has_attribute on the same preprocessor line isn't portable.
0086 #ifdef __has_attribute
0087 #   define DOUBLE_CONVERSION_HAS_ATTRIBUTE(x) __has_attribute(x)
0088 #else
0089 #   define DOUBLE_CONVERSION_HAS_ATTRIBUTE(x) 0
0090 #endif
0091 
0092 #ifndef DOUBLE_CONVERSION_UNUSED
0093 #if DOUBLE_CONVERSION_HAS_ATTRIBUTE(unused)
0094 #define DOUBLE_CONVERSION_UNUSED __attribute__((unused))
0095 #else
0096 #define DOUBLE_CONVERSION_UNUSED
0097 #endif
0098 #endif
0099 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UNUSED)
0100 #define UNUSED DOUBLE_CONVERSION_UNUSED
0101 #endif
0102 
0103 #if DOUBLE_CONVERSION_HAS_ATTRIBUTE(uninitialized)
0104 #define DOUBLE_CONVERSION_STACK_UNINITIALIZED __attribute__((uninitialized))
0105 #else
0106 #define DOUBLE_CONVERSION_STACK_UNINITIALIZED
0107 #endif
0108 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(STACK_UNINITIALIZED)
0109 #define STACK_UNINITIALIZED DOUBLE_CONVERSION_STACK_UNINITIALIZED
0110 #endif
0111 
0112 // Double operations detection based on target architecture.
0113 // Linux uses a 80bit wide floating point stack on x86. This induces double
0114 // rounding, which in turn leads to wrong results.
0115 // An easy way to test if the floating-point operations are correct is to
0116 // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then
0117 // the result is equal to 89255e-22.
0118 // The best way to test this, is to create a division-function and to compare
0119 // the output of the division with the expected result. (Inlining must be
0120 // disabled.)
0121 // On Linux,x86 89255e-22 != Div_double(89255.0/1e22)
0122 //
0123 // For example:
0124 /*
0125 // -- in div.c
0126 double Div_double(double x, double y) { return x / y; }
0127 
0128 // -- in main.c
0129 double Div_double(double x, double y);  // Forward declaration.
0130 
0131 int main(int argc, char** argv) {
0132   return Div_double(89255.0, 1e22) == 89255e-22;
0133 }
0134 */
0135 // Run as follows ./main || echo "correct"
0136 //
0137 // If it prints "correct" then the architecture should be here, in the "correct" section.
0138 #if defined(_M_X64) || defined(__x86_64__) || \
0139     defined(__ARMEL__) || defined(__avr32__) || defined(_M_ARM) || defined(_M_ARM64) || \
0140     defined(__hppa__) || defined(__ia64__) || \
0141     defined(__mips__) || \
0142     defined(__loongarch__) || \
0143     defined(__nios2__) || defined(__ghs) || \
0144     defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) || \
0145     defined(_POWER) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
0146     defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
0147     defined(__SH4__) || defined(__alpha__) || \
0148     defined(_MIPS_ARCH_MIPS32R2) || defined(__ARMEB__) ||\
0149     defined(__AARCH64EL__) || defined(__aarch64__) || defined(__AARCH64EB__) || \
0150     defined(__riscv) || defined(__e2k__) || \
0151     defined(__or1k__) || defined(__arc__) || defined(__ARC64__) || \
0152     defined(__microblaze__) || defined(__XTENSA__) || \
0153     defined(__EMSCRIPTEN__) || defined(__wasm32__)
0154 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
0155 #elif defined(__mc68000__) || \
0156     defined(__pnacl__) || defined(__native_client__)
0157 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
0158 #elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
0159 #if defined(_WIN32)
0160 // Windows uses a 64bit wide floating point stack.
0161 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
0162 #else
0163 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
0164 #endif  // _WIN32
0165 #else
0166 #error Target architecture was not detected as supported by Double-Conversion.
0167 #endif
0168 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(CORRECT_DOUBLE_OPERATIONS)
0169 #define CORRECT_DOUBLE_OPERATIONS DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
0170 #endif
0171 
0172 #if defined(_WIN32) && !defined(__MINGW32__)
0173 
0174 typedef signed char int8_t;
0175 typedef unsigned char uint8_t;
0176 typedef short int16_t;  // NOLINT
0177 typedef unsigned short uint16_t;  // NOLINT
0178 typedef int int32_t;
0179 typedef unsigned int uint32_t;
0180 typedef __int64 int64_t;
0181 typedef unsigned __int64 uint64_t;
0182 // intptr_t and friends are defined in crtdefs.h through stdio.h.
0183 
0184 #else
0185 
0186 #include <stdint.h>
0187 
0188 #endif
0189 
0190 typedef uint16_t uc16;
0191 
0192 // The following macro works on both 32 and 64-bit platforms.
0193 // Usage: instead of writing 0x1234567890123456
0194 //      write DOUBLE_CONVERSION_UINT64_2PART_C(0x12345678,90123456);
0195 #define DOUBLE_CONVERSION_UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
0196 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UINT64_2PART_C)
0197 #define UINT64_2PART_C DOUBLE_CONVERSION_UINT64_2PART_C
0198 #endif
0199 
0200 // The expression DOUBLE_CONVERSION_ARRAY_SIZE(a) is a compile-time constant of type
0201 // size_t which represents the number of elements of the given
0202 // array. You should only use DOUBLE_CONVERSION_ARRAY_SIZE on statically allocated
0203 // arrays.
0204 #ifndef DOUBLE_CONVERSION_ARRAY_SIZE
0205 #define DOUBLE_CONVERSION_ARRAY_SIZE(a)                                   \
0206   ((sizeof(a) / sizeof(*(a))) /                         \
0207   static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
0208 #endif
0209 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(ARRAY_SIZE)
0210 #define ARRAY_SIZE DOUBLE_CONVERSION_ARRAY_SIZE
0211 #endif
0212 
0213 // A macro to disallow the evil copy constructor and operator= functions
0214 // This should be used in the private: declarations for a class
0215 #ifndef DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
0216 #define DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)      \
0217   TypeName(const TypeName&);                    \
0218   void operator=(const TypeName&)
0219 #endif
0220 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(DC_DISALLOW_COPY_AND_ASSIGN)
0221 #define DC_DISALLOW_COPY_AND_ASSIGN DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
0222 #endif
0223 
0224 // A macro to disallow all the implicit constructors, namely the
0225 // default constructor, copy constructor and operator= functions.
0226 //
0227 // This should be used in the private: declarations for a class
0228 // that wants to prevent anyone from instantiating it. This is
0229 // especially useful for classes containing only static methods.
0230 #ifndef DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
0231 #define DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
0232   TypeName();                                    \
0233   DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)
0234 #endif
0235 #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(DC_DISALLOW_IMPLICIT_CONSTRUCTORS)
0236 #define DC_DISALLOW_IMPLICIT_CONSTRUCTORS DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
0237 #endif
0238 
0239 namespace double_conversion {
0240 
0241 inline int StrLength(const char* string) {
0242   size_t length = strlen(string);
0243   DOUBLE_CONVERSION_ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
0244   return static_cast<int>(length);
0245 }
0246 
0247 // This is a simplified version of V8's Vector class.
0248 template <typename T>
0249 class Vector {
0250  public:
0251   Vector() : start_(DOUBLE_CONVERSION_NULLPTR), length_(0) {}
0252   Vector(T* data, int len) : start_(data), length_(len) {
0253     DOUBLE_CONVERSION_ASSERT(len == 0 || (len > 0 && data != DOUBLE_CONVERSION_NULLPTR));
0254   }
0255 
0256   // Returns a vector using the same backing storage as this one,
0257   // spanning from and including 'from', to but not including 'to'.
0258   Vector<T> SubVector(int from, int to) {
0259     DOUBLE_CONVERSION_ASSERT(to <= length_);
0260     DOUBLE_CONVERSION_ASSERT(from < to);
0261     DOUBLE_CONVERSION_ASSERT(0 <= from);
0262     return Vector<T>(start() + from, to - from);
0263   }
0264 
0265   // Returns the length of the vector.
0266   int length() const { return length_; }
0267 
0268   // Returns whether or not the vector is empty.
0269   bool is_empty() const { return length_ == 0; }
0270 
0271   // Returns the pointer to the start of the data in the vector.
0272   T* start() const { return start_; }
0273 
0274   // Access individual vector elements - checks bounds in debug mode.
0275   T& operator[](int index) const {
0276     DOUBLE_CONVERSION_ASSERT(0 <= index && index < length_);
0277     return start_[index];
0278   }
0279 
0280   T& first() { return start_[0]; }
0281 
0282   T& last() { return start_[length_ - 1]; }
0283 
0284   void pop_back() {
0285     DOUBLE_CONVERSION_ASSERT(!is_empty());
0286     --length_;
0287   }
0288 
0289  private:
0290   T* start_;
0291   int length_;
0292 };
0293 
0294 
0295 // Helper class for building result strings in a character buffer. The
0296 // purpose of the class is to use safe operations that checks the
0297 // buffer bounds on all operations in debug mode.
0298 class StringBuilder {
0299  public:
0300   StringBuilder(char* buffer, int buffer_size)
0301       : buffer_(buffer, buffer_size), position_(0) { }
0302 
0303   ~StringBuilder() { if (!is_finalized()) Finalize(); }
0304 
0305   int size() const { return buffer_.length(); }
0306 
0307   // Get the current position in the builder.
0308   int position() const {
0309     DOUBLE_CONVERSION_ASSERT(!is_finalized());
0310     return position_;
0311   }
0312 
0313   // Reset the position.
0314   void Reset() { position_ = 0; }
0315 
0316   // Add a single character to the builder. It is not allowed to add
0317   // 0-characters; use the Finalize() method to terminate the string
0318   // instead.
0319   void AddCharacter(char c) {
0320     DOUBLE_CONVERSION_ASSERT(c != '\0');
0321     DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
0322     buffer_[position_++] = c;
0323   }
0324 
0325   // Add an entire string to the builder. Uses strlen() internally to
0326   // compute the length of the input string.
0327   void AddString(const char* s) {
0328     AddSubstring(s, StrLength(s));
0329   }
0330 
0331   // Add the first 'n' characters of the given string 's' to the
0332   // builder. The input string must have enough characters.
0333   void AddSubstring(const char* s, int n) {
0334     DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ + n < buffer_.length());
0335     DOUBLE_CONVERSION_ASSERT(static_cast<size_t>(n) <= strlen(s));
0336     memmove(&buffer_[position_], s, static_cast<size_t>(n));
0337     position_ += n;
0338   }
0339 
0340 
0341   // Add character padding to the builder. If count is non-positive,
0342   // nothing is added to the builder.
0343   void AddPadding(char c, int count) {
0344     for (int i = 0; i < count; i++) {
0345       AddCharacter(c);
0346     }
0347   }
0348 
0349   // Finalize the string by 0-terminating it and returning the buffer.
0350   char* Finalize() {
0351     DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
0352     buffer_[position_] = '\0';
0353     // Make sure nobody managed to add a 0-character to the
0354     // buffer while building the string.
0355     DOUBLE_CONVERSION_ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
0356     position_ = -1;
0357     DOUBLE_CONVERSION_ASSERT(is_finalized());
0358     return buffer_.start();
0359   }
0360 
0361  private:
0362   Vector<char> buffer_;
0363   int position_;
0364 
0365   bool is_finalized() const { return position_ < 0; }
0366 
0367   DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
0368 };
0369 
0370 // The type-based aliasing rule allows the compiler to assume that pointers of
0371 // different types (for some definition of different) never alias each other.
0372 // Thus the following code does not work:
0373 //
0374 // float f = foo();
0375 // int fbits = *(int*)(&f);
0376 //
0377 // The compiler 'knows' that the int pointer can't refer to f since the types
0378 // don't match, so the compiler may cache f in a register, leaving random data
0379 // in fbits.  Using C++ style casts makes no difference, however a pointer to
0380 // char data is assumed to alias any other pointer.  This is the 'memcpy
0381 // exception'.
0382 //
0383 // Bit_cast uses the memcpy exception to move the bits from a variable of one
0384 // type of a variable of another type.  Of course the end result is likely to
0385 // be implementation dependent.  Most compilers (gcc-4.2 and MSVC 2005)
0386 // will completely optimize BitCast away.
0387 //
0388 // There is an additional use for BitCast.
0389 // Recent gccs will warn when they see casts that may result in breakage due to
0390 // the type-based aliasing rule.  If you have checked that there is no breakage
0391 // you can use BitCast to cast one pointer type to another.  This confuses gcc
0392 // enough that it can no longer see that you have cast one pointer type to
0393 // another thus avoiding the warning.
0394 template <class Dest, class Source>
0395 Dest BitCast(const Source& source) {
0396   // Compile time assertion: sizeof(Dest) == sizeof(Source)
0397   // A compile error here means your Dest and Source have different sizes.
0398 #if __cplusplus >= 201103L
0399   static_assert(sizeof(Dest) == sizeof(Source),
0400                 "source and destination size mismatch");
0401 #else
0402   DOUBLE_CONVERSION_UNUSED
0403   typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
0404 #endif
0405 
0406   Dest dest;
0407   memmove(&dest, &source, sizeof(dest));
0408   return dest;
0409 }
0410 
0411 template <class Dest, class Source>
0412 Dest BitCast(Source* source) {
0413   return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
0414 }
0415 
0416 }  // namespace double_conversion
0417 
0418 #endif  // DOUBLE_CONVERSION_UTILS_H_