xref: /aosp_15_r20/external/icu/libicu/cts_headers/double-conversion-utils.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2018 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker //
4*0e209d39SAndroid Build Coastguard Worker // From the double-conversion library. Original license:
5*0e209d39SAndroid Build Coastguard Worker //
6*0e209d39SAndroid Build Coastguard Worker // Copyright 2010 the V8 project authors. All rights reserved.
7*0e209d39SAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without
8*0e209d39SAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions are
9*0e209d39SAndroid Build Coastguard Worker // met:
10*0e209d39SAndroid Build Coastguard Worker //
11*0e209d39SAndroid Build Coastguard Worker //     * Redistributions of source code must retain the above copyright
12*0e209d39SAndroid Build Coastguard Worker //       notice, this list of conditions and the following disclaimer.
13*0e209d39SAndroid Build Coastguard Worker //     * Redistributions in binary form must reproduce the above
14*0e209d39SAndroid Build Coastguard Worker //       copyright notice, this list of conditions and the following
15*0e209d39SAndroid Build Coastguard Worker //       disclaimer in the documentation and/or other materials provided
16*0e209d39SAndroid Build Coastguard Worker //       with the distribution.
17*0e209d39SAndroid Build Coastguard Worker //     * Neither the name of Google Inc. nor the names of its
18*0e209d39SAndroid Build Coastguard Worker //       contributors may be used to endorse or promote products derived
19*0e209d39SAndroid Build Coastguard Worker //       from this software without specific prior written permission.
20*0e209d39SAndroid Build Coastguard Worker //
21*0e209d39SAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*0e209d39SAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*0e209d39SAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24*0e209d39SAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25*0e209d39SAndroid Build Coastguard Worker // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26*0e209d39SAndroid Build Coastguard Worker // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27*0e209d39SAndroid Build Coastguard Worker // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*0e209d39SAndroid Build Coastguard Worker // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29*0e209d39SAndroid Build Coastguard Worker // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30*0e209d39SAndroid Build Coastguard Worker // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*0e209d39SAndroid Build Coastguard Worker // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*0e209d39SAndroid Build Coastguard Worker 
33*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
35*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_FORMATTING
36*0e209d39SAndroid Build Coastguard Worker 
37*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_UTILS_H_
38*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_UTILS_H_
39*0e209d39SAndroid Build Coastguard Worker 
40*0e209d39SAndroid Build Coastguard Worker // Use DOUBLE_CONVERSION_NON_PREFIXED_MACROS to get unprefixed macros as was
41*0e209d39SAndroid Build Coastguard Worker // the case in double-conversion releases prior to 3.1.6
42*0e209d39SAndroid Build Coastguard Worker 
43*0e209d39SAndroid Build Coastguard Worker #include <cstdlib>
44*0e209d39SAndroid Build Coastguard Worker #include <cstring>
45*0e209d39SAndroid Build Coastguard Worker 
46*0e209d39SAndroid Build Coastguard Worker // For pre-C++11 compatibility
47*0e209d39SAndroid Build Coastguard Worker #if __cplusplus >= 201103L
48*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_NULLPTR nullptr
49*0e209d39SAndroid Build Coastguard Worker #else
50*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_NULLPTR NULL
51*0e209d39SAndroid Build Coastguard Worker #endif
52*0e209d39SAndroid Build Coastguard Worker 
53*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: Use U_ASSERT instead of <assert.h>
54*0e209d39SAndroid Build Coastguard Worker #include "uassert.h"
55*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_ASSERT
56*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_ASSERT(condition)         \
57*0e209d39SAndroid Build Coastguard Worker     U_ASSERT(condition)
58*0e209d39SAndroid Build Coastguard Worker #endif
59*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(ASSERT)
60*0e209d39SAndroid Build Coastguard Worker #define ASSERT DOUBLE_CONVERSION_ASSERT
61*0e209d39SAndroid Build Coastguard Worker #endif
62*0e209d39SAndroid Build Coastguard Worker 
63*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_UNIMPLEMENTED
64*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_UNIMPLEMENTED() (abort())
65*0e209d39SAndroid Build Coastguard Worker #endif
66*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UNIMPLEMENTED)
67*0e209d39SAndroid Build Coastguard Worker #define UNIMPLEMENTED DOUBLE_CONVERSION_UNIMPLEMENTED
68*0e209d39SAndroid Build Coastguard Worker #endif
69*0e209d39SAndroid Build Coastguard Worker 
70*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_NO_RETURN
71*0e209d39SAndroid Build Coastguard Worker #ifdef _MSC_VER
72*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_NO_RETURN __declspec(noreturn)
73*0e209d39SAndroid Build Coastguard Worker #else
74*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_NO_RETURN __attribute__((noreturn))
75*0e209d39SAndroid Build Coastguard Worker #endif
76*0e209d39SAndroid Build Coastguard Worker #endif
77*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(NO_RETURN)
78*0e209d39SAndroid Build Coastguard Worker #define NO_RETURN DOUBLE_CONVERSION_NO_RETURN
79*0e209d39SAndroid Build Coastguard Worker #endif
80*0e209d39SAndroid Build Coastguard Worker 
81*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_UNREACHABLE
82*0e209d39SAndroid Build Coastguard Worker #ifdef _MSC_VER
83*0e209d39SAndroid Build Coastguard Worker void DOUBLE_CONVERSION_NO_RETURN abort_noreturn();
abort_noreturn()84*0e209d39SAndroid Build Coastguard Worker inline void abort_noreturn() { abort(); }
85*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_UNREACHABLE()   (abort_noreturn())
86*0e209d39SAndroid Build Coastguard Worker #else
87*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_UNREACHABLE()   (abort())
88*0e209d39SAndroid Build Coastguard Worker #endif
89*0e209d39SAndroid Build Coastguard Worker #endif
90*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UNREACHABLE)
91*0e209d39SAndroid Build Coastguard Worker #define UNREACHABLE DOUBLE_CONVERSION_UNREACHABLE
92*0e209d39SAndroid Build Coastguard Worker #endif
93*0e209d39SAndroid Build Coastguard Worker 
94*0e209d39SAndroid Build Coastguard Worker // Not all compilers support __has_attribute and combining a check for both
95*0e209d39SAndroid Build Coastguard Worker // ifdef and __has_attribute on the same preprocessor line isn't portable.
96*0e209d39SAndroid Build Coastguard Worker #ifdef __has_attribute
97*0e209d39SAndroid Build Coastguard Worker #   define DOUBLE_CONVERSION_HAS_ATTRIBUTE(x) __has_attribute(x)
98*0e209d39SAndroid Build Coastguard Worker #else
99*0e209d39SAndroid Build Coastguard Worker #   define DOUBLE_CONVERSION_HAS_ATTRIBUTE(x) 0
100*0e209d39SAndroid Build Coastguard Worker #endif
101*0e209d39SAndroid Build Coastguard Worker 
102*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_UNUSED
103*0e209d39SAndroid Build Coastguard Worker #if DOUBLE_CONVERSION_HAS_ATTRIBUTE(unused)
104*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_UNUSED __attribute__((unused))
105*0e209d39SAndroid Build Coastguard Worker #else
106*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_UNUSED
107*0e209d39SAndroid Build Coastguard Worker #endif
108*0e209d39SAndroid Build Coastguard Worker #endif
109*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UNUSED)
110*0e209d39SAndroid Build Coastguard Worker #define UNUSED DOUBLE_CONVERSION_UNUSED
111*0e209d39SAndroid Build Coastguard Worker #endif
112*0e209d39SAndroid Build Coastguard Worker 
113*0e209d39SAndroid Build Coastguard Worker #if DOUBLE_CONVERSION_HAS_ATTRIBUTE(uninitialized)
114*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_STACK_UNINITIALIZED __attribute__((uninitialized))
115*0e209d39SAndroid Build Coastguard Worker #else
116*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_STACK_UNINITIALIZED
117*0e209d39SAndroid Build Coastguard Worker #endif
118*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(STACK_UNINITIALIZED)
119*0e209d39SAndroid Build Coastguard Worker #define STACK_UNINITIALIZED DOUBLE_CONVERSION_STACK_UNINITIALIZED
120*0e209d39SAndroid Build Coastguard Worker #endif
121*0e209d39SAndroid Build Coastguard Worker 
122*0e209d39SAndroid Build Coastguard Worker // Double operations detection based on target architecture.
123*0e209d39SAndroid Build Coastguard Worker // Linux uses a 80bit wide floating point stack on x86. This induces double
124*0e209d39SAndroid Build Coastguard Worker // rounding, which in turn leads to wrong results.
125*0e209d39SAndroid Build Coastguard Worker // An easy way to test if the floating-point operations are correct is to
126*0e209d39SAndroid Build Coastguard Worker // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then
127*0e209d39SAndroid Build Coastguard Worker // the result is equal to 89255e-22.
128*0e209d39SAndroid Build Coastguard Worker // The best way to test this, is to create a division-function and to compare
129*0e209d39SAndroid Build Coastguard Worker // the output of the division with the expected result. (Inlining must be
130*0e209d39SAndroid Build Coastguard Worker // disabled.)
131*0e209d39SAndroid Build Coastguard Worker // On Linux,x86 89255e-22 != Div_double(89255.0/1e22)
132*0e209d39SAndroid Build Coastguard Worker //
133*0e209d39SAndroid Build Coastguard Worker // For example:
134*0e209d39SAndroid Build Coastguard Worker /*
135*0e209d39SAndroid Build Coastguard Worker // -- in div.c
136*0e209d39SAndroid Build Coastguard Worker double Div_double(double x, double y) { return x / y; }
137*0e209d39SAndroid Build Coastguard Worker 
138*0e209d39SAndroid Build Coastguard Worker // -- in main.c
139*0e209d39SAndroid Build Coastguard Worker double Div_double(double x, double y);  // Forward declaration.
140*0e209d39SAndroid Build Coastguard Worker 
141*0e209d39SAndroid Build Coastguard Worker int main(int argc, char** argv) {
142*0e209d39SAndroid Build Coastguard Worker   return Div_double(89255.0, 1e22) == 89255e-22;
143*0e209d39SAndroid Build Coastguard Worker }
144*0e209d39SAndroid Build Coastguard Worker */
145*0e209d39SAndroid Build Coastguard Worker // Run as follows ./main || echo "correct"
146*0e209d39SAndroid Build Coastguard Worker //
147*0e209d39SAndroid Build Coastguard Worker // If it prints "correct" then the architecture should be here, in the "correct" section.
148*0e209d39SAndroid Build Coastguard Worker #if defined(_M_X64) || defined(__x86_64__) || \
149*0e209d39SAndroid Build Coastguard Worker     defined(__ARMEL__) || defined(__avr32__) || defined(_M_ARM) || defined(_M_ARM64) || \
150*0e209d39SAndroid Build Coastguard Worker     defined(__hppa__) || defined(__ia64__) || \
151*0e209d39SAndroid Build Coastguard Worker     defined(__mips__) || \
152*0e209d39SAndroid Build Coastguard Worker     defined(__loongarch__) || \
153*0e209d39SAndroid Build Coastguard Worker     defined(__nios2__) || defined(__ghs) || \
154*0e209d39SAndroid Build Coastguard Worker     defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) || \
155*0e209d39SAndroid Build Coastguard Worker     defined(_POWER) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
156*0e209d39SAndroid Build Coastguard Worker     defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
157*0e209d39SAndroid Build Coastguard Worker     defined(__SH4__) || defined(__alpha__) || \
158*0e209d39SAndroid Build Coastguard Worker     defined(_MIPS_ARCH_MIPS32R2) || defined(__ARMEB__) ||\
159*0e209d39SAndroid Build Coastguard Worker     defined(__AARCH64EL__) || defined(__aarch64__) || defined(__AARCH64EB__) || \
160*0e209d39SAndroid Build Coastguard Worker     defined(__riscv) || defined(__e2k__) || \
161*0e209d39SAndroid Build Coastguard Worker     defined(__or1k__) || defined(__arc__) || defined(__ARC64__) || \
162*0e209d39SAndroid Build Coastguard Worker     defined(__microblaze__) || defined(__XTENSA__) || \
163*0e209d39SAndroid Build Coastguard Worker     defined(__EMSCRIPTEN__) || defined(__wasm32__)
164*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
165*0e209d39SAndroid Build Coastguard Worker #elif defined(__mc68000__) || \
166*0e209d39SAndroid Build Coastguard Worker     defined(__pnacl__) || defined(__native_client__)
167*0e209d39SAndroid Build Coastguard Worker #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
168*0e209d39SAndroid Build Coastguard Worker #elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
169*0e209d39SAndroid Build Coastguard Worker #if defined(_WIN32)
170*0e209d39SAndroid Build Coastguard Worker // Windows uses a 64bit wide floating point stack.
171*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
172*0e209d39SAndroid Build Coastguard Worker #else
173*0e209d39SAndroid Build Coastguard Worker #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
174*0e209d39SAndroid Build Coastguard Worker #endif  // _WIN32
175*0e209d39SAndroid Build Coastguard Worker #else
176*0e209d39SAndroid Build Coastguard Worker #error Target architecture was not detected as supported by Double-Conversion.
177*0e209d39SAndroid Build Coastguard Worker #endif
178*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(CORRECT_DOUBLE_OPERATIONS)
179*0e209d39SAndroid Build Coastguard Worker #define CORRECT_DOUBLE_OPERATIONS DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
180*0e209d39SAndroid Build Coastguard Worker #endif
181*0e209d39SAndroid Build Coastguard Worker 
182*0e209d39SAndroid Build Coastguard Worker #if defined(_WIN32) && !defined(__MINGW32__)
183*0e209d39SAndroid Build Coastguard Worker 
184*0e209d39SAndroid Build Coastguard Worker typedef signed char int8_t;
185*0e209d39SAndroid Build Coastguard Worker typedef unsigned char uint8_t;
186*0e209d39SAndroid Build Coastguard Worker typedef short int16_t;  // NOLINT
187*0e209d39SAndroid Build Coastguard Worker typedef unsigned short uint16_t;  // NOLINT
188*0e209d39SAndroid Build Coastguard Worker typedef int int32_t;
189*0e209d39SAndroid Build Coastguard Worker typedef unsigned int uint32_t;
190*0e209d39SAndroid Build Coastguard Worker typedef __int64 int64_t;
191*0e209d39SAndroid Build Coastguard Worker typedef unsigned __int64 uint64_t;
192*0e209d39SAndroid Build Coastguard Worker // intptr_t and friends are defined in crtdefs.h through stdio.h.
193*0e209d39SAndroid Build Coastguard Worker 
194*0e209d39SAndroid Build Coastguard Worker #else
195*0e209d39SAndroid Build Coastguard Worker 
196*0e209d39SAndroid Build Coastguard Worker #include <stdint.h>
197*0e209d39SAndroid Build Coastguard Worker 
198*0e209d39SAndroid Build Coastguard Worker #endif
199*0e209d39SAndroid Build Coastguard Worker 
200*0e209d39SAndroid Build Coastguard Worker typedef uint16_t uc16;
201*0e209d39SAndroid Build Coastguard Worker 
202*0e209d39SAndroid Build Coastguard Worker // The following macro works on both 32 and 64-bit platforms.
203*0e209d39SAndroid Build Coastguard Worker // Usage: instead of writing 0x1234567890123456
204*0e209d39SAndroid Build Coastguard Worker //      write DOUBLE_CONVERSION_UINT64_2PART_C(0x12345678,90123456);
205*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
206*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(UINT64_2PART_C)
207*0e209d39SAndroid Build Coastguard Worker #define UINT64_2PART_C DOUBLE_CONVERSION_UINT64_2PART_C
208*0e209d39SAndroid Build Coastguard Worker #endif
209*0e209d39SAndroid Build Coastguard Worker 
210*0e209d39SAndroid Build Coastguard Worker // The expression DOUBLE_CONVERSION_ARRAY_SIZE(a) is a compile-time constant of type
211*0e209d39SAndroid Build Coastguard Worker // size_t which represents the number of elements of the given
212*0e209d39SAndroid Build Coastguard Worker // array. You should only use DOUBLE_CONVERSION_ARRAY_SIZE on statically allocated
213*0e209d39SAndroid Build Coastguard Worker // arrays.
214*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_ARRAY_SIZE
215*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_ARRAY_SIZE(a)                                   \
216*0e209d39SAndroid Build Coastguard Worker   ((sizeof(a) / sizeof(*(a))) /                         \
217*0e209d39SAndroid Build Coastguard Worker   static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
218*0e209d39SAndroid Build Coastguard Worker #endif
219*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(ARRAY_SIZE)
220*0e209d39SAndroid Build Coastguard Worker #define ARRAY_SIZE DOUBLE_CONVERSION_ARRAY_SIZE
221*0e209d39SAndroid Build Coastguard Worker #endif
222*0e209d39SAndroid Build Coastguard Worker 
223*0e209d39SAndroid Build Coastguard Worker // A macro to disallow the evil copy constructor and operator= functions
224*0e209d39SAndroid Build Coastguard Worker // This should be used in the private: declarations for a class
225*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
226*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)      \
227*0e209d39SAndroid Build Coastguard Worker   TypeName(const TypeName&);                    \
228*0e209d39SAndroid Build Coastguard Worker   void operator=(const TypeName&)
229*0e209d39SAndroid Build Coastguard Worker #endif
230*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(DC_DISALLOW_COPY_AND_ASSIGN)
231*0e209d39SAndroid Build Coastguard Worker #define DC_DISALLOW_COPY_AND_ASSIGN DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
232*0e209d39SAndroid Build Coastguard Worker #endif
233*0e209d39SAndroid Build Coastguard Worker 
234*0e209d39SAndroid Build Coastguard Worker // A macro to disallow all the implicit constructors, namely the
235*0e209d39SAndroid Build Coastguard Worker // default constructor, copy constructor and operator= functions.
236*0e209d39SAndroid Build Coastguard Worker //
237*0e209d39SAndroid Build Coastguard Worker // This should be used in the private: declarations for a class
238*0e209d39SAndroid Build Coastguard Worker // that wants to prevent anyone from instantiating it. This is
239*0e209d39SAndroid Build Coastguard Worker // especially useful for classes containing only static methods.
240*0e209d39SAndroid Build Coastguard Worker #ifndef DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
241*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
242*0e209d39SAndroid Build Coastguard Worker   TypeName();                                    \
243*0e209d39SAndroid Build Coastguard Worker   DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)
244*0e209d39SAndroid Build Coastguard Worker #endif
245*0e209d39SAndroid Build Coastguard Worker #if defined(DOUBLE_CONVERSION_NON_PREFIXED_MACROS) && !defined(DC_DISALLOW_IMPLICIT_CONSTRUCTORS)
246*0e209d39SAndroid Build Coastguard Worker #define DC_DISALLOW_IMPLICIT_CONSTRUCTORS DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
247*0e209d39SAndroid Build Coastguard Worker #endif
248*0e209d39SAndroid Build Coastguard Worker 
249*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: Wrap in ICU namespace
250*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
251*0e209d39SAndroid Build Coastguard Worker 
252*0e209d39SAndroid Build Coastguard Worker namespace double_conversion {
253*0e209d39SAndroid Build Coastguard Worker 
StrLength(const char * string)254*0e209d39SAndroid Build Coastguard Worker inline int StrLength(const char* string) {
255*0e209d39SAndroid Build Coastguard Worker   size_t length = strlen(string);
256*0e209d39SAndroid Build Coastguard Worker   DOUBLE_CONVERSION_ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
257*0e209d39SAndroid Build Coastguard Worker   return static_cast<int>(length);
258*0e209d39SAndroid Build Coastguard Worker }
259*0e209d39SAndroid Build Coastguard Worker 
260*0e209d39SAndroid Build Coastguard Worker // This is a simplified version of V8's Vector class.
261*0e209d39SAndroid Build Coastguard Worker template <typename T>
262*0e209d39SAndroid Build Coastguard Worker class Vector {
263*0e209d39SAndroid Build Coastguard Worker  public:
Vector()264*0e209d39SAndroid Build Coastguard Worker   Vector() : start_(DOUBLE_CONVERSION_NULLPTR), length_(0) {}
Vector(T * data,int len)265*0e209d39SAndroid Build Coastguard Worker   Vector(T* data, int len) : start_(data), length_(len) {
266*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(len == 0 || (len > 0 && data != DOUBLE_CONVERSION_NULLPTR));
267*0e209d39SAndroid Build Coastguard Worker   }
268*0e209d39SAndroid Build Coastguard Worker 
269*0e209d39SAndroid Build Coastguard Worker   // Returns a vector using the same backing storage as this one,
270*0e209d39SAndroid Build Coastguard Worker   // spanning from and including 'from', to but not including 'to'.
SubVector(int from,int to)271*0e209d39SAndroid Build Coastguard Worker   Vector<T> SubVector(int from, int to) {
272*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(to <= length_);
273*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(from < to);
274*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(0 <= from);
275*0e209d39SAndroid Build Coastguard Worker     return Vector<T>(start() + from, to - from);
276*0e209d39SAndroid Build Coastguard Worker   }
277*0e209d39SAndroid Build Coastguard Worker 
278*0e209d39SAndroid Build Coastguard Worker   // Returns the length of the vector.
length()279*0e209d39SAndroid Build Coastguard Worker   int length() const { return length_; }
280*0e209d39SAndroid Build Coastguard Worker 
281*0e209d39SAndroid Build Coastguard Worker   // Returns whether or not the vector is empty.
is_empty()282*0e209d39SAndroid Build Coastguard Worker   bool is_empty() const { return length_ == 0; }
283*0e209d39SAndroid Build Coastguard Worker 
284*0e209d39SAndroid Build Coastguard Worker   // Returns the pointer to the start of the data in the vector.
start()285*0e209d39SAndroid Build Coastguard Worker   T* start() const { return start_; }
286*0e209d39SAndroid Build Coastguard Worker 
287*0e209d39SAndroid Build Coastguard Worker   // Access individual vector elements - checks bounds in debug mode.
288*0e209d39SAndroid Build Coastguard Worker   T& operator[](int index) const {
289*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(0 <= index && index < length_);
290*0e209d39SAndroid Build Coastguard Worker     return start_[index];
291*0e209d39SAndroid Build Coastguard Worker   }
292*0e209d39SAndroid Build Coastguard Worker 
first()293*0e209d39SAndroid Build Coastguard Worker   T& first() { return start_[0]; }
294*0e209d39SAndroid Build Coastguard Worker 
last()295*0e209d39SAndroid Build Coastguard Worker   T& last() { return start_[length_ - 1]; }
296*0e209d39SAndroid Build Coastguard Worker 
pop_back()297*0e209d39SAndroid Build Coastguard Worker   void pop_back() {
298*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(!is_empty());
299*0e209d39SAndroid Build Coastguard Worker     --length_;
300*0e209d39SAndroid Build Coastguard Worker   }
301*0e209d39SAndroid Build Coastguard Worker 
302*0e209d39SAndroid Build Coastguard Worker  private:
303*0e209d39SAndroid Build Coastguard Worker   T* start_;
304*0e209d39SAndroid Build Coastguard Worker   int length_;
305*0e209d39SAndroid Build Coastguard Worker };
306*0e209d39SAndroid Build Coastguard Worker 
307*0e209d39SAndroid Build Coastguard Worker 
308*0e209d39SAndroid Build Coastguard Worker // Helper class for building result strings in a character buffer. The
309*0e209d39SAndroid Build Coastguard Worker // purpose of the class is to use safe operations that checks the
310*0e209d39SAndroid Build Coastguard Worker // buffer bounds on all operations in debug mode.
311*0e209d39SAndroid Build Coastguard Worker class StringBuilder {
312*0e209d39SAndroid Build Coastguard Worker  public:
StringBuilder(char * buffer,int buffer_size)313*0e209d39SAndroid Build Coastguard Worker   StringBuilder(char* buffer, int buffer_size)
314*0e209d39SAndroid Build Coastguard Worker       : buffer_(buffer, buffer_size), position_(0) { }
315*0e209d39SAndroid Build Coastguard Worker 
~StringBuilder()316*0e209d39SAndroid Build Coastguard Worker   ~StringBuilder() { if (!is_finalized()) Finalize(); }
317*0e209d39SAndroid Build Coastguard Worker 
size()318*0e209d39SAndroid Build Coastguard Worker   int size() const { return buffer_.length(); }
319*0e209d39SAndroid Build Coastguard Worker 
320*0e209d39SAndroid Build Coastguard Worker   // Get the current position in the builder.
position()321*0e209d39SAndroid Build Coastguard Worker   int position() const {
322*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(!is_finalized());
323*0e209d39SAndroid Build Coastguard Worker     return position_;
324*0e209d39SAndroid Build Coastguard Worker   }
325*0e209d39SAndroid Build Coastguard Worker 
326*0e209d39SAndroid Build Coastguard Worker   // Reset the position.
Reset()327*0e209d39SAndroid Build Coastguard Worker   void Reset() { position_ = 0; }
328*0e209d39SAndroid Build Coastguard Worker 
329*0e209d39SAndroid Build Coastguard Worker   // Add a single character to the builder. It is not allowed to add
330*0e209d39SAndroid Build Coastguard Worker   // 0-characters; use the Finalize() method to terminate the string
331*0e209d39SAndroid Build Coastguard Worker   // instead.
AddCharacter(char c)332*0e209d39SAndroid Build Coastguard Worker   void AddCharacter(char c) {
333*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(c != '\0');
334*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
335*0e209d39SAndroid Build Coastguard Worker     buffer_[position_++] = c;
336*0e209d39SAndroid Build Coastguard Worker   }
337*0e209d39SAndroid Build Coastguard Worker 
338*0e209d39SAndroid Build Coastguard Worker   // Add an entire string to the builder. Uses strlen() internally to
339*0e209d39SAndroid Build Coastguard Worker   // compute the length of the input string.
AddString(const char * s)340*0e209d39SAndroid Build Coastguard Worker   void AddString(const char* s) {
341*0e209d39SAndroid Build Coastguard Worker     AddSubstring(s, StrLength(s));
342*0e209d39SAndroid Build Coastguard Worker   }
343*0e209d39SAndroid Build Coastguard Worker 
344*0e209d39SAndroid Build Coastguard Worker   // Add the first 'n' characters of the given string 's' to the
345*0e209d39SAndroid Build Coastguard Worker   // builder. The input string must have enough characters.
AddSubstring(const char * s,int n)346*0e209d39SAndroid Build Coastguard Worker   void AddSubstring(const char* s, int n) {
347*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ + n < buffer_.length());
348*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(static_cast<size_t>(n) <= strlen(s));
349*0e209d39SAndroid Build Coastguard Worker     memmove(&buffer_[position_], s, static_cast<size_t>(n));
350*0e209d39SAndroid Build Coastguard Worker     position_ += n;
351*0e209d39SAndroid Build Coastguard Worker   }
352*0e209d39SAndroid Build Coastguard Worker 
353*0e209d39SAndroid Build Coastguard Worker 
354*0e209d39SAndroid Build Coastguard Worker   // Add character padding to the builder. If count is non-positive,
355*0e209d39SAndroid Build Coastguard Worker   // nothing is added to the builder.
AddPadding(char c,int count)356*0e209d39SAndroid Build Coastguard Worker   void AddPadding(char c, int count) {
357*0e209d39SAndroid Build Coastguard Worker     for (int i = 0; i < count; i++) {
358*0e209d39SAndroid Build Coastguard Worker       AddCharacter(c);
359*0e209d39SAndroid Build Coastguard Worker     }
360*0e209d39SAndroid Build Coastguard Worker   }
361*0e209d39SAndroid Build Coastguard Worker 
362*0e209d39SAndroid Build Coastguard Worker   // Finalize the string by 0-terminating it and returning the buffer.
Finalize()363*0e209d39SAndroid Build Coastguard Worker   char* Finalize() {
364*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
365*0e209d39SAndroid Build Coastguard Worker     buffer_[position_] = '\0';
366*0e209d39SAndroid Build Coastguard Worker     // Make sure nobody managed to add a 0-character to the
367*0e209d39SAndroid Build Coastguard Worker     // buffer while building the string.
368*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
369*0e209d39SAndroid Build Coastguard Worker     position_ = -1;
370*0e209d39SAndroid Build Coastguard Worker     DOUBLE_CONVERSION_ASSERT(is_finalized());
371*0e209d39SAndroid Build Coastguard Worker     return buffer_.start();
372*0e209d39SAndroid Build Coastguard Worker   }
373*0e209d39SAndroid Build Coastguard Worker 
374*0e209d39SAndroid Build Coastguard Worker  private:
375*0e209d39SAndroid Build Coastguard Worker   Vector<char> buffer_;
376*0e209d39SAndroid Build Coastguard Worker   int position_;
377*0e209d39SAndroid Build Coastguard Worker 
is_finalized()378*0e209d39SAndroid Build Coastguard Worker   bool is_finalized() const { return position_ < 0; }
379*0e209d39SAndroid Build Coastguard Worker 
380*0e209d39SAndroid Build Coastguard Worker   DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
381*0e209d39SAndroid Build Coastguard Worker };
382*0e209d39SAndroid Build Coastguard Worker 
383*0e209d39SAndroid Build Coastguard Worker // The type-based aliasing rule allows the compiler to assume that pointers of
384*0e209d39SAndroid Build Coastguard Worker // different types (for some definition of different) never alias each other.
385*0e209d39SAndroid Build Coastguard Worker // Thus the following code does not work:
386*0e209d39SAndroid Build Coastguard Worker //
387*0e209d39SAndroid Build Coastguard Worker // float f = foo();
388*0e209d39SAndroid Build Coastguard Worker // int fbits = *(int*)(&f);
389*0e209d39SAndroid Build Coastguard Worker //
390*0e209d39SAndroid Build Coastguard Worker // The compiler 'knows' that the int pointer can't refer to f since the types
391*0e209d39SAndroid Build Coastguard Worker // don't match, so the compiler may cache f in a register, leaving random data
392*0e209d39SAndroid Build Coastguard Worker // in fbits.  Using C++ style casts makes no difference, however a pointer to
393*0e209d39SAndroid Build Coastguard Worker // char data is assumed to alias any other pointer.  This is the 'memcpy
394*0e209d39SAndroid Build Coastguard Worker // exception'.
395*0e209d39SAndroid Build Coastguard Worker //
396*0e209d39SAndroid Build Coastguard Worker // Bit_cast uses the memcpy exception to move the bits from a variable of one
397*0e209d39SAndroid Build Coastguard Worker // type of a variable of another type.  Of course the end result is likely to
398*0e209d39SAndroid Build Coastguard Worker // be implementation dependent.  Most compilers (gcc-4.2 and MSVC 2005)
399*0e209d39SAndroid Build Coastguard Worker // will completely optimize BitCast away.
400*0e209d39SAndroid Build Coastguard Worker //
401*0e209d39SAndroid Build Coastguard Worker // There is an additional use for BitCast.
402*0e209d39SAndroid Build Coastguard Worker // Recent gccs will warn when they see casts that may result in breakage due to
403*0e209d39SAndroid Build Coastguard Worker // the type-based aliasing rule.  If you have checked that there is no breakage
404*0e209d39SAndroid Build Coastguard Worker // you can use BitCast to cast one pointer type to another.  This confuses gcc
405*0e209d39SAndroid Build Coastguard Worker // enough that it can no longer see that you have cast one pointer type to
406*0e209d39SAndroid Build Coastguard Worker // another thus avoiding the warning.
407*0e209d39SAndroid Build Coastguard Worker template <class Dest, class Source>
BitCast(const Source & source)408*0e209d39SAndroid Build Coastguard Worker Dest BitCast(const Source& source) {
409*0e209d39SAndroid Build Coastguard Worker   // Compile time assertion: sizeof(Dest) == sizeof(Source)
410*0e209d39SAndroid Build Coastguard Worker   // A compile error here means your Dest and Source have different sizes.
411*0e209d39SAndroid Build Coastguard Worker #if __cplusplus >= 201103L
412*0e209d39SAndroid Build Coastguard Worker   static_assert(sizeof(Dest) == sizeof(Source),
413*0e209d39SAndroid Build Coastguard Worker                 "source and destination size mismatch");
414*0e209d39SAndroid Build Coastguard Worker #else
415*0e209d39SAndroid Build Coastguard Worker   DOUBLE_CONVERSION_UNUSED
416*0e209d39SAndroid Build Coastguard Worker   typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
417*0e209d39SAndroid Build Coastguard Worker #endif
418*0e209d39SAndroid Build Coastguard Worker 
419*0e209d39SAndroid Build Coastguard Worker   Dest dest;
420*0e209d39SAndroid Build Coastguard Worker   memmove(&dest, &source, sizeof(dest));
421*0e209d39SAndroid Build Coastguard Worker   return dest;
422*0e209d39SAndroid Build Coastguard Worker }
423*0e209d39SAndroid Build Coastguard Worker 
424*0e209d39SAndroid Build Coastguard Worker template <class Dest, class Source>
BitCast(Source * source)425*0e209d39SAndroid Build Coastguard Worker Dest BitCast(Source* source) {
426*0e209d39SAndroid Build Coastguard Worker   return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
427*0e209d39SAndroid Build Coastguard Worker }
428*0e209d39SAndroid Build Coastguard Worker 
429*0e209d39SAndroid Build Coastguard Worker }  // namespace double_conversion
430*0e209d39SAndroid Build Coastguard Worker 
431*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: Close ICU namespace
432*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
433*0e209d39SAndroid Build Coastguard Worker 
434*0e209d39SAndroid Build Coastguard Worker #endif  // DOUBLE_CONVERSION_UTILS_H_
435*0e209d39SAndroid Build Coastguard Worker #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING
436