Coverage Report

Created: 2025-06-24 06:43

/src/icu/source/i18n/double-conversion-utils.h
Line
Count
Source (jump to first uncovered line)
1
// © 2018 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
//
4
// From the double-conversion library. Original license:
5
//
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
9
// met:
10
//
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.
20
//
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.
32
33
// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34
#include "unicode/utypes.h"
35
#if !UCONFIG_NO_FORMATTING
36
37
#ifndef DOUBLE_CONVERSION_UTILS_H_
38
#define DOUBLE_CONVERSION_UTILS_H_
39
40
#include <cstdlib>
41
#include <cstring>
42
43
// ICU PATCH: Use U_ASSERT instead of <assert.h>
44
#include "uassert.h"
45
#ifndef DOUBLE_CONVERSION_ASSERT
46
#define DOUBLE_CONVERSION_ASSERT(condition)         \
47
0
    U_ASSERT(condition);
48
#endif
49
#ifndef DOUBLE_CONVERSION_UNIMPLEMENTED
50
0
#define DOUBLE_CONVERSION_UNIMPLEMENTED() (abort())
51
#endif
52
#ifndef DOUBLE_CONVERSION_NO_RETURN
53
#ifdef _MSC_VER
54
#define DOUBLE_CONVERSION_NO_RETURN __declspec(noreturn)
55
#else
56
#define DOUBLE_CONVERSION_NO_RETURN __attribute__((noreturn))
57
#endif
58
#endif
59
#ifndef DOUBLE_CONVERSION_UNREACHABLE
60
#ifdef _MSC_VER
61
void DOUBLE_CONVERSION_NO_RETURN abort_noreturn();
62
inline void abort_noreturn() { abort(); }
63
#define DOUBLE_CONVERSION_UNREACHABLE()   (abort_noreturn())
64
#else
65
0
#define DOUBLE_CONVERSION_UNREACHABLE()   (abort())
66
#endif
67
#endif
68
69
// Not all compilers support __has_attribute and combining a check for both
70
// ifdef and __has_attribute on the same preprocessor line isn't portable.
71
#ifdef __has_attribute
72
#   define DOUBLE_CONVERSION_HAS_ATTRIBUTE(x) __has_attribute(x)
73
#else
74
#   define DOUBLE_CONVERSION_HAS_ATTRIBUTE(x) 0
75
#endif
76
77
#ifndef DOUBLE_CONVERSION_UNUSED
78
#if DOUBLE_CONVERSION_HAS_ATTRIBUTE(unused)
79
#define DOUBLE_CONVERSION_UNUSED __attribute__((unused))
80
#else
81
#define DOUBLE_CONVERSION_UNUSED
82
#endif
83
#endif
84
85
#if DOUBLE_CONVERSION_HAS_ATTRIBUTE(uninitialized)
86
0
#define DOUBLE_CONVERSION_STACK_UNINITIALIZED __attribute__((uninitialized))
87
#else
88
#define DOUBLE_CONVERSION_STACK_UNINITIALIZED
89
#endif
90
91
// Double operations detection based on target architecture.
92
// Linux uses a 80bit wide floating point stack on x86. This induces double
93
// rounding, which in turn leads to wrong results.
94
// An easy way to test if the floating-point operations are correct is to
95
// evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then
96
// the result is equal to 89255e-22.
97
// The best way to test this, is to create a division-function and to compare
98
// the output of the division with the expected result. (Inlining must be
99
// disabled.)
100
// On Linux,x86 89255e-22 != Div_double(89255.0/1e22)
101
//
102
// For example:
103
/*
104
// -- in div.c
105
double Div_double(double x, double y) { return x / y; }
106
107
// -- in main.c
108
double Div_double(double x, double y);  // Forward declaration.
109
110
int main(int argc, char** argv) {
111
  return Div_double(89255.0, 1e22) == 89255e-22;
112
}
113
*/
114
// Run as follows ./main || echo "correct"
115
//
116
// If it prints "correct" then the architecture should be here, in the "correct" section.
117
#if defined(_M_X64) || defined(__x86_64__) || \
118
    defined(__ARMEL__) || defined(__avr32__) || defined(_M_ARM) || defined(_M_ARM64) || \
119
    defined(__hppa__) || defined(__ia64__) || \
120
    defined(__mips__) || \
121
    defined(__nios2__) || defined(__ghs) || \
122
    defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) || \
123
    defined(_POWER) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
124
    defined(__sparc__) || defined(__sparc) || defined(__s390__) || \
125
    defined(__SH4__) || defined(__alpha__) || \
126
    defined(_MIPS_ARCH_MIPS32R2) || defined(__ARMEB__) ||\
127
    defined(__AARCH64EL__) || defined(__aarch64__) || defined(__AARCH64EB__) || \
128
    defined(__riscv) || defined(__e2k__) || \
129
    defined(__or1k__) || defined(__arc__) || \
130
    defined(__microblaze__) || defined(__XTENSA__) || \
131
    defined(__EMSCRIPTEN__) || defined(__wasm32__)
132
#define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
133
#elif defined(__mc68000__) || \
134
    defined(__pnacl__) || defined(__native_client__)
135
#undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
136
#elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
137
#if defined(_WIN32)
138
// Windows uses a 64bit wide floating point stack.
139
#define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1
140
#else
141
#undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS
142
#endif  // _WIN32
143
#else
144
#error Target architecture was not detected as supported by Double-Conversion.
145
#endif
146
147
#if defined(_WIN32) && !defined(__MINGW32__)
148
149
typedef signed char int8_t;
150
typedef unsigned char uint8_t;
151
typedef short int16_t;  // NOLINT
152
typedef unsigned short uint16_t;  // NOLINT
153
typedef int int32_t;
154
typedef unsigned int uint32_t;
155
typedef __int64 int64_t;
156
typedef unsigned __int64 uint64_t;
157
// intptr_t and friends are defined in crtdefs.h through stdio.h.
158
159
#else
160
161
#include <stdint.h>
162
163
#endif
164
165
typedef uint16_t uc16;
166
167
// The following macro works on both 32 and 64-bit platforms.
168
// Usage: instead of writing 0x1234567890123456
169
//      write DOUBLE_CONVERSION_UINT64_2PART_C(0x12345678,90123456);
170
0
#define DOUBLE_CONVERSION_UINT64_2PART_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
171
172
173
// The expression DOUBLE_CONVERSION_ARRAY_SIZE(a) is a compile-time constant of type
174
// size_t which represents the number of elements of the given
175
// array. You should only use DOUBLE_CONVERSION_ARRAY_SIZE on statically allocated
176
// arrays.
177
#ifndef DOUBLE_CONVERSION_ARRAY_SIZE
178
#define DOUBLE_CONVERSION_ARRAY_SIZE(a)                                   \
179
  ((sizeof(a) / sizeof(*(a))) /                         \
180
  static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
181
#endif
182
183
// A macro to disallow the evil copy constructor and operator= functions
184
// This should be used in the private: declarations for a class
185
#ifndef DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN
186
#define DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)      \
187
  TypeName(const TypeName&);                    \
188
  void operator=(const TypeName&)
189
#endif
190
191
// A macro to disallow all the implicit constructors, namely the
192
// default constructor, copy constructor and operator= functions.
193
//
194
// This should be used in the private: declarations for a class
195
// that wants to prevent anyone from instantiating it. This is
196
// especially useful for classes containing only static methods.
197
#ifndef DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS
198
#define DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
199
  TypeName();                                    \
200
  DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(TypeName)
201
#endif
202
203
// ICU PATCH: Wrap in ICU namespace
204
U_NAMESPACE_BEGIN
205
206
namespace double_conversion {
207
208
0
inline int StrLength(const char* string) {
209
0
  size_t length = strlen(string);
210
0
  DOUBLE_CONVERSION_ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
211
0
  return static_cast<int>(length);
212
0
}
213
214
// This is a simplified version of V8's Vector class.
215
template <typename T>
216
class Vector {
217
 public:
218
0
  Vector() : start_(NULL), length_(0) {}
219
0
  Vector(T* data, int len) : start_(data), length_(len) {
220
0
    DOUBLE_CONVERSION_ASSERT(len == 0 || (len > 0 && data != NULL));
221
0
  }
Unexecuted instantiation: icu_70::double_conversion::Vector<char>::Vector(char*, int)
Unexecuted instantiation: icu_70::double_conversion::Vector<char const>::Vector(char const*, int)
222
223
  // Returns a vector using the same backing storage as this one,
224
  // spanning from and including 'from', to but not including 'to'.
225
0
  Vector<T> SubVector(int from, int to) {
226
0
    DOUBLE_CONVERSION_ASSERT(to <= length_);
227
0
    DOUBLE_CONVERSION_ASSERT(from < to);
228
0
    DOUBLE_CONVERSION_ASSERT(0 <= from);
229
0
    return Vector<T>(start() + from, to - from);
230
0
  }
231
232
  // Returns the length of the vector.
233
0
  int length() const { return length_; }
Unexecuted instantiation: icu_70::double_conversion::Vector<char>::length() const
Unexecuted instantiation: icu_70::double_conversion::Vector<char const>::length() const
234
235
  // Returns whether or not the vector is empty.
236
0
  bool is_empty() const { return length_ == 0; }
237
238
  // Returns the pointer to the start of the data in the vector.
239
0
  T* start() const { return start_; }
Unexecuted instantiation: icu_70::double_conversion::Vector<char>::start() const
Unexecuted instantiation: icu_70::double_conversion::Vector<char const>::start() const
240
241
  // Access individual vector elements - checks bounds in debug mode.
242
0
  T& operator[](int index) const {
243
0
    DOUBLE_CONVERSION_ASSERT(0 <= index && index < length_);
244
0
    return start_[index];
245
0
  }
Unexecuted instantiation: icu_70::double_conversion::Vector<char>::operator[](int) const
Unexecuted instantiation: icu_70::double_conversion::Vector<char const>::operator[](int) const
246
247
  T& first() { return start_[0]; }
248
249
0
  T& last() { return start_[length_ - 1]; }
250
251
0
  void pop_back() {
252
0
    DOUBLE_CONVERSION_ASSERT(!is_empty());
253
0
    --length_;
254
0
  }
255
256
 private:
257
  T* start_;
258
  int length_;
259
};
260
261
262
// Helper class for building result strings in a character buffer. The
263
// purpose of the class is to use safe operations that checks the
264
// buffer bounds on all operations in debug mode.
265
class StringBuilder {
266
 public:
267
  StringBuilder(char* buffer, int buffer_size)
268
0
      : buffer_(buffer, buffer_size), position_(0) { }
269
270
0
  ~StringBuilder() { if (!is_finalized()) Finalize(); }
271
272
0
  int size() const { return buffer_.length(); }
273
274
  // Get the current position in the builder.
275
0
  int position() const {
276
0
    DOUBLE_CONVERSION_ASSERT(!is_finalized());
277
0
    return position_;
278
0
  }
279
280
  // Reset the position.
281
0
  void Reset() { position_ = 0; }
282
283
  // Add a single character to the builder. It is not allowed to add
284
  // 0-characters; use the Finalize() method to terminate the string
285
  // instead.
286
0
  void AddCharacter(char c) {
287
0
    DOUBLE_CONVERSION_ASSERT(c != '\0');
288
0
    DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
289
0
    buffer_[position_++] = c;
290
0
  }
291
292
  // Add an entire string to the builder. Uses strlen() internally to
293
  // compute the length of the input string.
294
0
  void AddString(const char* s) {
295
0
    AddSubstring(s, StrLength(s));
296
0
  }
297
298
  // Add the first 'n' characters of the given string 's' to the
299
  // builder. The input string must have enough characters.
300
0
  void AddSubstring(const char* s, int n) {
301
0
    DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ + n < buffer_.length());
302
0
    DOUBLE_CONVERSION_ASSERT(static_cast<size_t>(n) <= strlen(s));
303
0
    memmove(&buffer_[position_], s, n);
304
0
    position_ += n;
305
0
  }
306
307
308
  // Add character padding to the builder. If count is non-positive,
309
  // nothing is added to the builder.
310
0
  void AddPadding(char c, int count) {
311
0
    for (int i = 0; i < count; i++) {
312
0
      AddCharacter(c);
313
0
    }
314
0
  }
315
316
  // Finalize the string by 0-terminating it and returning the buffer.
317
0
  char* Finalize() {
318
0
    DOUBLE_CONVERSION_ASSERT(!is_finalized() && position_ < buffer_.length());
319
0
    buffer_[position_] = '\0';
320
0
    // Make sure nobody managed to add a 0-character to the
321
0
    // buffer while building the string.
322
0
    DOUBLE_CONVERSION_ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
323
0
    position_ = -1;
324
0
    DOUBLE_CONVERSION_ASSERT(is_finalized());
325
0
    return buffer_.start();
326
0
  }
327
328
 private:
329
  Vector<char> buffer_;
330
  int position_;
331
332
0
  bool is_finalized() const { return position_ < 0; }
333
334
  DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
335
};
336
337
// The type-based aliasing rule allows the compiler to assume that pointers of
338
// different types (for some definition of different) never alias each other.
339
// Thus the following code does not work:
340
//
341
// float f = foo();
342
// int fbits = *(int*)(&f);
343
//
344
// The compiler 'knows' that the int pointer can't refer to f since the types
345
// don't match, so the compiler may cache f in a register, leaving random data
346
// in fbits.  Using C++ style casts makes no difference, however a pointer to
347
// char data is assumed to alias any other pointer.  This is the 'memcpy
348
// exception'.
349
//
350
// Bit_cast uses the memcpy exception to move the bits from a variable of one
351
// type of a variable of another type.  Of course the end result is likely to
352
// be implementation dependent.  Most compilers (gcc-4.2 and MSVC 2005)
353
// will completely optimize BitCast away.
354
//
355
// There is an additional use for BitCast.
356
// Recent gccs will warn when they see casts that may result in breakage due to
357
// the type-based aliasing rule.  If you have checked that there is no breakage
358
// you can use BitCast to cast one pointer type to another.  This confuses gcc
359
// enough that it can no longer see that you have cast one pointer type to
360
// another thus avoiding the warning.
361
template <class Dest, class Source>
362
0
Dest BitCast(const Source& source) {
363
  // Compile time assertion: sizeof(Dest) == sizeof(Source)
364
  // A compile error here means your Dest and Source have different sizes.
365
0
#if __cplusplus >= 201103L
366
0
  static_assert(sizeof(Dest) == sizeof(Source),
367
0
                "source and destination size mismatch");
368
#else
369
  DOUBLE_CONVERSION_UNUSED
370
  typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
371
#endif
372
373
0
  Dest dest;
374
0
  memmove(&dest, &source, sizeof(dest));
375
0
  return dest;
376
0
}
Unexecuted instantiation: unsigned long icu_70::double_conversion::BitCast<unsigned long, double>(double const&)
Unexecuted instantiation: double icu_70::double_conversion::BitCast<double, unsigned long>(unsigned long const&)
Unexecuted instantiation: unsigned int icu_70::double_conversion::BitCast<unsigned int, float>(float const&)
Unexecuted instantiation: float icu_70::double_conversion::BitCast<float, unsigned int>(unsigned int const&)
377
378
template <class Dest, class Source>
379
Dest BitCast(Source* source) {
380
  return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
381
}
382
383
}  // namespace double_conversion
384
385
// ICU PATCH: Close ICU namespace
386
U_NAMESPACE_END
387
388
#endif  // DOUBLE_CONVERSION_UTILS_H_
389
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING