1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 // From the double-conversion library. Original license:
6 // Copyright 2010 the V8 project authors. All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34 #include "unicode/utypes.h"
35 #if !UCONFIG_NO_FORMATTING
37 #ifndef DOUBLE_CONVERSION_UTILS_H_
38 #define DOUBLE_CONVERSION_UTILS_H_
43 // ICU PATCH: Use U_ASSERT instead of <assert.h>
45 #ifndef DOUBLE_CONVERSION_ASSERT
46 #define DOUBLE_CONVERSION_ASSERT(condition) \
49 #ifndef DOUBLE_CONVERSION_UNIMPLEMENTED
50 #define DOUBLE_CONVERSION_UNIMPLEMENTED() (abort())
52 #ifndef DOUBLE_CONVERSION_NO_RETURN
54 #define DOUBLE_CONVERSION_NO_RETURN __declspec(noreturn)
56 #define DOUBLE_CONVERSION_NO_RETURN __attribute__((noreturn))
59 #ifndef DOUBLE_CONVERSION_UNREACHABLE
61 void DOUBLE_CONVERSION_NO_RETURN
abort_noreturn();
62 inline void abort_noreturn() { abort(); }
63 #define DOUBLE_CONVERSION_UNREACHABLE() (abort_noreturn())
65 #define DOUBLE_CONVERSION_UNREACHABLE() (abort())
69 #ifndef DOUBLE_CONVERSION_UNUSED
71 #define DOUBLE_CONVERSION_UNUSED __attribute__((unused))
73 #define DOUBLE_CONVERSION_UNUSED
77 // Double operations detection based on target architecture.
78 // Linux uses a 80bit wide floating point stack on x86. This induces double
79 // rounding, which in turn leads to wrong results.
80 // An easy way to test if the floating-point operations are correct is to
81 // evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then
82 // the result is equal to 89255e-22.
83 // The best way to test this, is to create a division-function and to compare
84 // the output of the division with the expected result. (Inlining must be
86 // On Linux,x86 89255e-22 != Div_double(89255.0/1e22)
91 double Div_double(double x, double y) { return x / y; }
94 double Div_double(double x, double y); // Forward declaration.
96 int main(int argc, char** argv) {
97 return Div_double(89255.0, 1e22) == 89255e-22;
100 // Run as follows ./main || echo "correct"
102 // If it prints "correct" then the architecture should be here, in the "correct" section.
103 #if defined(_M_X64) || defined(__x86_64__) || \
104 defined(__ARMEL__) || defined(__avr32__) || defined(_M_ARM) || defined(_M_ARM64) || \
105 defined(__hppa__) || defined(__ia64__) || \
106 defined(__mips__) || \
107 defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) || \
108 defined(_POWER) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
109 defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
110 defined(__SH4__) || defined(__alpha__) || \
111 defined(_MIPS_ARCH_MIPS32R2) || defined(__ARMEB__) ||\
112 defined(__AARCH64EL__) || defined(__aarch64__) || defined(__AARCH64EB__) || \
113 defined(__riscv) || defined(__e2k__) || \
114 defined(__or1k__) || defined(__arc__) || \
115 defined(__EMSCRIPTEN__)
116 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
117 #elif defined(__mc68000__) || \
118 defined(__pnacl__) || defined(__native_client__)
119 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
120 #elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
122 // Windows uses a 64bit wide floating point stack.
123 #define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
125 #undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
128 #error Target architecture was not detected as supported by Double-Conversion.
131 #if defined(_WIN32) && !defined(__MINGW32__)
133 typedef signed char int8_t;
134 typedef unsigned char uint8_t;
135 typedef short int16_t; // NOLINT
136 typedef unsigned short uint16_t; // NOLINT
138 typedef unsigned int uint32_t;
139 typedef __int64
int64_t;
140 typedef unsigned __int64
uint64_t;
141 // intptr_t and friends are defined in crtdefs.h through stdio.h.
149 typedef uint16_t uc16
;
151 // The following macro works on both 32 and 64-bit platforms.
152 // Usage: instead of writing 0x1234567890123456
153 // write DOUBLE_CONVERSION_UINT64_2PART_C(0x12345678,90123456);
154 #define DOUBLE_CONVERSION_UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
157 // The expression DOUBLE_CONVERSION_ARRAY_SIZE(a) is a compile-time constant of type
158 // size_t which represents the number of elements of the given
159 // array. You should only use DOUBLE_CONVERSION_ARRAY_SIZE on statically allocated
161 #ifndef DOUBLE_CONVERSION_ARRAY_SIZE
162 #define DOUBLE_CONVERSION_ARRAY_SIZE(a) \
163 ((sizeof(a) / sizeof(*(a))) / \
164 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
167 // A macro to disallow the evil copy constructor and operator= functions
168 // This should be used in the private: declarations for a class
169 #ifndef DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
170 #define DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName) \
171 TypeName(const TypeName&); \
172 void operator=(const TypeName&)
175 // A macro to disallow all the implicit constructors, namely the
176 // default constructor, copy constructor and operator= functions.
178 // This should be used in the private: declarations for a class
179 // that wants to prevent anyone from instantiating it. This is
180 // especially useful for classes containing only static methods.
181 #ifndef DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
182 #define DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
184 DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)
187 // ICU PATCH: Wrap in ICU namespace
190 namespace double_conversion
{
192 inline int StrLength(const char* string
) {
193 size_t length
= strlen(string
);
194 DOUBLE_CONVERSION_ASSERT(length
== static_cast<size_t>(static_cast<int>(length
)));
195 return static_cast<int>(length
);
198 // This is a simplified version of V8's Vector class.
199 template <typename T
>
202 Vector() : start_(NULL
), length_(0) {}
203 Vector(T
* data
, int len
) : start_(data
), length_(len
) {
204 DOUBLE_CONVERSION_ASSERT(len
== 0 || (len
> 0 && data
!= NULL
));
207 // Returns a vector using the same backing storage as this one,
208 // spanning from and including 'from', to but not including 'to'.
209 Vector
<T
> SubVector(int from
, int to
) {
210 DOUBLE_CONVERSION_ASSERT(to
<= length_
);
211 DOUBLE_CONVERSION_ASSERT(from
< to
);
212 DOUBLE_CONVERSION_ASSERT(0 <= from
);
213 return Vector
<T
>(start() + from
, to
- from
);
216 // Returns the length of the vector.
217 int length() const { return length_
; }
219 // Returns whether or not the vector is empty.
220 bool is_empty() const { return length_
== 0; }
222 // Returns the pointer to the start of the data in the vector.
223 T
* start() const { return start_
; }
225 // Access individual vector elements - checks bounds in debug mode.
226 T
& operator[](int index
) const {
227 DOUBLE_CONVERSION_ASSERT(0 <= index
&& index
< length_
);
228 return start_
[index
];
231 T
& first() { return start_
[0]; }
233 T
& last() { return start_
[length_
- 1]; }
236 DOUBLE_CONVERSION_ASSERT(!is_empty());
246 // Helper class for building result strings in a character buffer. The
247 // purpose of the class is to use safe operations that checks the
248 // buffer bounds on all operations in debug mode.
249 class StringBuilder
{
251 StringBuilder(char* buffer
, int buffer_size
)
252 : buffer_(buffer
, buffer_size
), position_(0) { }
254 ~StringBuilder() { if (!is_finalized()) Finalize(); }
256 int size() const { return buffer_
.length(); }
258 // Get the current position in the builder.
259 int position() const {
260 DOUBLE_CONVERSION_ASSERT(!is_finalized());
264 // Reset the position.
265 void Reset() { position_
= 0; }
267 // Add a single character to the builder. It is not allowed to add
268 // 0-characters; use the Finalize() method to terminate the string
270 void AddCharacter(char c
) {
271 DOUBLE_CONVERSION_ASSERT(c
!= '\0');
272 DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_
< buffer_
.length());
273 buffer_
[position_
++] = c
;
276 // Add an entire string to the builder. Uses strlen() internally to
277 // compute the length of the input string.
278 void AddString(const char* s
) {
279 AddSubstring(s
, StrLength(s
));
282 // Add the first 'n' characters of the given string 's' to the
283 // builder. The input string must have enough characters.
284 void AddSubstring(const char* s
, int n
) {
285 DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_
+ n
< buffer_
.length());
286 DOUBLE_CONVERSION_ASSERT(static_cast<size_t>(n
) <= strlen(s
));
287 memmove(&buffer_
[position_
], s
, n
);
292 // Add character padding to the builder. If count is non-positive,
293 // nothing is added to the builder.
294 void AddPadding(char c
, int count
) {
295 for (int i
= 0; i
< count
; i
++) {
300 // Finalize the string by 0-terminating it and returning the buffer.
302 DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_
< buffer_
.length());
303 buffer_
[position_
] = '\0';
304 // Make sure nobody managed to add a 0-character to the
305 // buffer while building the string.
306 DOUBLE_CONVERSION_ASSERT(strlen(buffer_
.start()) == static_cast<size_t>(position_
));
308 DOUBLE_CONVERSION_ASSERT(is_finalized());
309 return buffer_
.start();
313 Vector
<char> buffer_
;
316 bool is_finalized() const { return position_
< 0; }
318 DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder
);
321 // The type-based aliasing rule allows the compiler to assume that pointers of
322 // different types (for some definition of different) never alias each other.
323 // Thus the following code does not work:
326 // int fbits = *(int*)(&f);
328 // The compiler 'knows' that the int pointer can't refer to f since the types
329 // don't match, so the compiler may cache f in a register, leaving random data
330 // in fbits. Using C++ style casts makes no difference, however a pointer to
331 // char data is assumed to alias any other pointer. This is the 'memcpy
334 // Bit_cast uses the memcpy exception to move the bits from a variable of one
335 // type of a variable of another type. Of course the end result is likely to
336 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
337 // will completely optimize BitCast away.
339 // There is an additional use for BitCast.
340 // Recent gccs will warn when they see casts that may result in breakage due to
341 // the type-based aliasing rule. If you have checked that there is no breakage
342 // you can use BitCast to cast one pointer type to another. This confuses gcc
343 // enough that it can no longer see that you have cast one pointer type to
344 // another thus avoiding the warning.
345 template <class Dest
, class Source
>
346 Dest
BitCast(const Source
& source
) {
347 // Compile time assertion: sizeof(Dest) == sizeof(Source)
348 // A compile error here means your Dest and Source have different sizes.
349 #if __cplusplus >= 201103L
350 static_assert(sizeof(Dest
) == sizeof(Source
),
351 "source and destination size mismatch");
353 DOUBLE_CONVERSION_UNUSED
354 typedef char VerifySizesAreEqual
[sizeof(Dest
) == sizeof(Source
) ? 1 : -1];
358 memmove(&dest
, &source
, sizeof(dest
));
362 template <class Dest
, class Source
>
363 Dest
BitCast(Source
* source
) {
364 return BitCast
<Dest
>(reinterpret_cast<uintptr_t>(source
));
367 } // namespace double_conversion
369 // ICU PATCH: Close ICU namespace
372 #endif // DOUBLE_CONVERSION_UTILS_H_
373 #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING