Coverage Report

Created: 2025-06-24 06:43

/src/icu/source/i18n/double-conversion-ieee.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 2012 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_DOUBLE_H_
38
#define DOUBLE_CONVERSION_DOUBLE_H_
39
40
// ICU PATCH: Customize header file paths for ICU.
41
42
#include "double-conversion-diy-fp.h"
43
44
// ICU PATCH: Wrap in ICU namespace
45
U_NAMESPACE_BEGIN
46
47
namespace double_conversion {
48
49
// We assume that doubles and uint64_t have the same endianness.
50
0
static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
Unexecuted instantiation: double-conversion-double-to-string.cpp:icu_70::double_conversion::double_to_uint64(double)
Unexecuted instantiation: double-conversion-fast-dtoa.cpp:icu_70::double_conversion::double_to_uint64(double)
Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_70::double_conversion::double_to_uint64(double)
Unexecuted instantiation: double-conversion-strtod.cpp:icu_70::double_conversion::double_to_uint64(double)
Unexecuted instantiation: double-conversion-bignum-dtoa.cpp:icu_70::double_conversion::double_to_uint64(double)
51
0
static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
Unexecuted instantiation: double-conversion-double-to-string.cpp:icu_70::double_conversion::uint64_to_double(unsigned long)
Unexecuted instantiation: double-conversion-fast-dtoa.cpp:icu_70::double_conversion::uint64_to_double(unsigned long)
Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_70::double_conversion::uint64_to_double(unsigned long)
Unexecuted instantiation: double-conversion-strtod.cpp:icu_70::double_conversion::uint64_to_double(unsigned long)
Unexecuted instantiation: double-conversion-bignum-dtoa.cpp:icu_70::double_conversion::uint64_to_double(unsigned long)
52
0
static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
Unexecuted instantiation: double-conversion-double-to-string.cpp:icu_70::double_conversion::float_to_uint32(float)
Unexecuted instantiation: double-conversion-fast-dtoa.cpp:icu_70::double_conversion::float_to_uint32(float)
Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_70::double_conversion::float_to_uint32(float)
Unexecuted instantiation: double-conversion-strtod.cpp:icu_70::double_conversion::float_to_uint32(float)
Unexecuted instantiation: double-conversion-bignum-dtoa.cpp:icu_70::double_conversion::float_to_uint32(float)
53
0
static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
Unexecuted instantiation: double-conversion-double-to-string.cpp:icu_70::double_conversion::uint32_to_float(unsigned int)
Unexecuted instantiation: double-conversion-fast-dtoa.cpp:icu_70::double_conversion::uint32_to_float(unsigned int)
Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_70::double_conversion::uint32_to_float(unsigned int)
Unexecuted instantiation: double-conversion-strtod.cpp:icu_70::double_conversion::uint32_to_float(unsigned int)
Unexecuted instantiation: double-conversion-bignum-dtoa.cpp:icu_70::double_conversion::uint32_to_float(unsigned int)
54
55
// Helper functions for doubles.
56
class Double {
57
 public:
58
  static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
59
  static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
60
  static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
61
  static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
62
  static const uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000);
63
  static const int kPhysicalSignificandSize = 52;  // Excludes the hidden bit.
64
  static const int kSignificandSize = 53;
65
  static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
66
  static const int kMaxExponent = 0x7FF - kExponentBias;
67
68
0
  Double() : d64_(0) {}
69
0
  explicit Double(double d) : d64_(double_to_uint64(d)) {}
70
0
  explicit Double(uint64_t d64) : d64_(d64) {}
71
  explicit Double(DiyFp diy_fp)
72
0
    : d64_(DiyFpToUint64(diy_fp)) {}
73
74
  // The value encoded by this Double must be greater or equal to +0.0.
75
  // It must not be special (infinity, or NaN).
76
0
  DiyFp AsDiyFp() const {
77
0
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
78
0
    DOUBLE_CONVERSION_ASSERT(!IsSpecial());
79
0
    return DiyFp(Significand(), Exponent());
80
0
  }
81
82
  // The value encoded by this Double must be strictly greater than 0.
83
0
  DiyFp AsNormalizedDiyFp() const {
84
0
    DOUBLE_CONVERSION_ASSERT(value() > 0.0);
85
0
    uint64_t f = Significand();
86
0
    int e = Exponent();
87
88
    // The current double could be a denormal.
89
0
    while ((f & kHiddenBit) == 0) {
90
0
      f <<= 1;
91
0
      e--;
92
0
    }
93
    // Do the final shifts in one go.
94
0
    f <<= DiyFp::kSignificandSize - kSignificandSize;
95
0
    e -= DiyFp::kSignificandSize - kSignificandSize;
96
0
    return DiyFp(f, e);
97
0
  }
98
99
  // Returns the double's bit as uint64.
100
0
  uint64_t AsUint64() const {
101
0
    return d64_;
102
0
  }
103
104
  // Returns the next greater double. Returns +infinity on input +infinity.
105
0
  double NextDouble() const {
106
0
    if (d64_ == kInfinity) return Double(kInfinity).value();
107
0
    if (Sign() < 0 && Significand() == 0) {
108
      // -0.0
109
0
      return 0.0;
110
0
    }
111
0
    if (Sign() < 0) {
112
0
      return Double(d64_ - 1).value();
113
0
    } else {
114
0
      return Double(d64_ + 1).value();
115
0
    }
116
0
  }
117
118
0
  double PreviousDouble() const {
119
0
    if (d64_ == (kInfinity | kSignMask)) return -Infinity();
120
0
    if (Sign() < 0) {
121
0
      return Double(d64_ + 1).value();
122
0
    } else {
123
0
      if (Significand() == 0) return -0.0;
124
0
      return Double(d64_ - 1).value();
125
0
    }
126
0
  }
127
128
0
  int Exponent() const {
129
0
    if (IsDenormal()) return kDenormalExponent;
130
131
0
    uint64_t d64 = AsUint64();
132
0
    int biased_e =
133
0
        static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
134
0
    return biased_e - kExponentBias;
135
0
  }
136
137
0
  uint64_t Significand() const {
138
0
    uint64_t d64 = AsUint64();
139
0
    uint64_t significand = d64 & kSignificandMask;
140
0
    if (!IsDenormal()) {
141
0
      return significand + kHiddenBit;
142
0
    } else {
143
0
      return significand;
144
0
    }
145
0
  }
146
147
  // Returns true if the double is a denormal.
148
0
  bool IsDenormal() const {
149
0
    uint64_t d64 = AsUint64();
150
0
    return (d64 & kExponentMask) == 0;
151
0
  }
152
153
  // We consider denormals not to be special.
154
  // Hence only Infinity and NaN are special.
155
0
  bool IsSpecial() const {
156
0
    uint64_t d64 = AsUint64();
157
0
    return (d64 & kExponentMask) == kExponentMask;
158
0
  }
159
160
0
  bool IsNan() const {
161
0
    uint64_t d64 = AsUint64();
162
0
    return ((d64 & kExponentMask) == kExponentMask) &&
163
0
        ((d64 & kSignificandMask) != 0);
164
0
  }
165
166
0
  bool IsQuietNan() const {
167
0
    return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
168
0
  }
169
170
0
  bool IsSignalingNan() const {
171
0
    return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
172
0
  }
173
174
175
0
  bool IsInfinite() const {
176
0
    uint64_t d64 = AsUint64();
177
0
    return ((d64 & kExponentMask) == kExponentMask) &&
178
0
        ((d64 & kSignificandMask) == 0);
179
0
  }
180
181
0
  int Sign() const {
182
0
    uint64_t d64 = AsUint64();
183
0
    return (d64 & kSignMask) == 0? 1: -1;
184
0
  }
185
186
  // Precondition: the value encoded by this Double must be greater or equal
187
  // than +0.0.
188
0
  DiyFp UpperBoundary() const {
189
0
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
190
0
    return DiyFp(Significand() * 2 + 1, Exponent() - 1);
191
0
  }
192
193
  // Computes the two boundaries of this.
194
  // The bigger boundary (m_plus) is normalized. The lower boundary has the same
195
  // exponent as m_plus.
196
  // Precondition: the value encoded by this Double must be greater than 0.
197
0
  void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
198
0
    DOUBLE_CONVERSION_ASSERT(value() > 0.0);
199
0
    DiyFp v = this->AsDiyFp();
200
0
    DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
201
0
    DiyFp m_minus;
202
0
    if (LowerBoundaryIsCloser()) {
203
0
      m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
204
0
    } else {
205
0
      m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
206
0
    }
207
0
    m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
208
0
    m_minus.set_e(m_plus.e());
209
0
    *out_m_plus = m_plus;
210
0
    *out_m_minus = m_minus;
211
0
  }
212
213
0
  bool LowerBoundaryIsCloser() const {
214
    // The boundary is closer if the significand is of the form f == 2^p-1 then
215
    // the lower boundary is closer.
216
    // Think of v = 1000e10 and v- = 9999e9.
217
    // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
218
    // at a distance of 1e8.
219
    // The only exception is for the smallest normal: the largest denormal is
220
    // at the same distance as its successor.
221
    // Note: denormals have the same exponent as the smallest normals.
222
0
    bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
223
0
    return physical_significand_is_zero && (Exponent() != kDenormalExponent);
224
0
  }
225
226
0
  double value() const { return uint64_to_double(d64_); }
227
228
  // Returns the significand size for a given order of magnitude.
229
  // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
230
  // This function returns the number of significant binary digits v will have
231
  // once it's encoded into a double. In almost all cases this is equal to
232
  // kSignificandSize. The only exceptions are denormals. They start with
233
  // leading zeroes and their effective significand-size is hence smaller.
234
0
  static int SignificandSizeForOrderOfMagnitude(int order) {
235
0
    if (order >= (kDenormalExponent + kSignificandSize)) {
236
0
      return kSignificandSize;
237
0
    }
238
0
    if (order <= kDenormalExponent) return 0;
239
0
    return order - kDenormalExponent;
240
0
  }
241
242
0
  static double Infinity() {
243
0
    return Double(kInfinity).value();
244
0
  }
245
246
0
  static double NaN() {
247
0
    return Double(kNaN).value();
248
0
  }
249
250
 private:
251
  static const int kDenormalExponent = -kExponentBias + 1;
252
  static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
253
  static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
254
255
  const uint64_t d64_;
256
257
0
  static uint64_t DiyFpToUint64(DiyFp diy_fp) {
258
0
    uint64_t significand = diy_fp.f();
259
0
    int exponent = diy_fp.e();
260
0
    while (significand > kHiddenBit + kSignificandMask) {
261
0
      significand >>= 1;
262
0
      exponent++;
263
0
    }
264
0
    if (exponent >= kMaxExponent) {
265
0
      return kInfinity;
266
0
    }
267
0
    if (exponent < kDenormalExponent) {
268
0
      return 0;
269
0
    }
270
0
    while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
271
0
      significand <<= 1;
272
0
      exponent--;
273
0
    }
274
0
    uint64_t biased_exponent;
275
0
    if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
276
0
      biased_exponent = 0;
277
0
    } else {
278
0
      biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
279
0
    }
280
0
    return (significand & kSignificandMask) |
281
0
        (biased_exponent << kPhysicalSignificandSize);
282
0
  }
283
284
  DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
285
};
286
287
class Single {
288
 public:
289
  static const uint32_t kSignMask = 0x80000000;
290
  static const uint32_t kExponentMask = 0x7F800000;
291
  static const uint32_t kSignificandMask = 0x007FFFFF;
292
  static const uint32_t kHiddenBit = 0x00800000;
293
  static const uint32_t kQuietNanBit = 0x00400000;
294
  static const int kPhysicalSignificandSize = 23;  // Excludes the hidden bit.
295
  static const int kSignificandSize = 24;
296
297
0
  Single() : d32_(0) {}
298
0
  explicit Single(float f) : d32_(float_to_uint32(f)) {}
299
0
  explicit Single(uint32_t d32) : d32_(d32) {}
300
301
  // The value encoded by this Single must be greater or equal to +0.0.
302
  // It must not be special (infinity, or NaN).
303
0
  DiyFp AsDiyFp() const {
304
0
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
305
0
    DOUBLE_CONVERSION_ASSERT(!IsSpecial());
306
0
    return DiyFp(Significand(), Exponent());
307
0
  }
308
309
  // Returns the single's bit as uint64.
310
0
  uint32_t AsUint32() const {
311
0
    return d32_;
312
0
  }
313
314
0
  int Exponent() const {
315
0
    if (IsDenormal()) return kDenormalExponent;
316
317
0
    uint32_t d32 = AsUint32();
318
0
    int biased_e =
319
0
        static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
320
0
    return biased_e - kExponentBias;
321
0
  }
322
323
0
  uint32_t Significand() const {
324
0
    uint32_t d32 = AsUint32();
325
0
    uint32_t significand = d32 & kSignificandMask;
326
0
    if (!IsDenormal()) {
327
0
      return significand + kHiddenBit;
328
0
    } else {
329
0
      return significand;
330
0
    }
331
0
  }
332
333
  // Returns true if the single is a denormal.
334
0
  bool IsDenormal() const {
335
0
    uint32_t d32 = AsUint32();
336
0
    return (d32 & kExponentMask) == 0;
337
0
  }
338
339
  // We consider denormals not to be special.
340
  // Hence only Infinity and NaN are special.
341
0
  bool IsSpecial() const {
342
0
    uint32_t d32 = AsUint32();
343
0
    return (d32 & kExponentMask) == kExponentMask;
344
0
  }
345
346
0
  bool IsNan() const {
347
0
    uint32_t d32 = AsUint32();
348
0
    return ((d32 & kExponentMask) == kExponentMask) &&
349
0
        ((d32 & kSignificandMask) != 0);
350
0
  }
351
352
0
  bool IsQuietNan() const {
353
0
    return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
354
0
  }
355
356
0
  bool IsSignalingNan() const {
357
0
    return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
358
0
  }
359
360
361
0
  bool IsInfinite() const {
362
0
    uint32_t d32 = AsUint32();
363
0
    return ((d32 & kExponentMask) == kExponentMask) &&
364
0
        ((d32 & kSignificandMask) == 0);
365
0
  }
366
367
0
  int Sign() const {
368
0
    uint32_t d32 = AsUint32();
369
0
    return (d32 & kSignMask) == 0? 1: -1;
370
0
  }
371
372
  // Computes the two boundaries of this.
373
  // The bigger boundary (m_plus) is normalized. The lower boundary has the same
374
  // exponent as m_plus.
375
  // Precondition: the value encoded by this Single must be greater than 0.
376
0
  void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
377
0
    DOUBLE_CONVERSION_ASSERT(value() > 0.0);
378
0
    DiyFp v = this->AsDiyFp();
379
0
    DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
380
0
    DiyFp m_minus;
381
0
    if (LowerBoundaryIsCloser()) {
382
0
      m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
383
0
    } else {
384
0
      m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
385
0
    }
386
0
    m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
387
0
    m_minus.set_e(m_plus.e());
388
0
    *out_m_plus = m_plus;
389
0
    *out_m_minus = m_minus;
390
0
  }
391
392
  // Precondition: the value encoded by this Single must be greater or equal
393
  // than +0.0.
394
0
  DiyFp UpperBoundary() const {
395
0
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
396
0
    return DiyFp(Significand() * 2 + 1, Exponent() - 1);
397
0
  }
398
399
0
  bool LowerBoundaryIsCloser() const {
400
    // The boundary is closer if the significand is of the form f == 2^p-1 then
401
    // the lower boundary is closer.
402
    // Think of v = 1000e10 and v- = 9999e9.
403
    // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
404
    // at a distance of 1e8.
405
    // The only exception is for the smallest normal: the largest denormal is
406
    // at the same distance as its successor.
407
    // Note: denormals have the same exponent as the smallest normals.
408
0
    bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
409
0
    return physical_significand_is_zero && (Exponent() != kDenormalExponent);
410
0
  }
411
412
0
  float value() const { return uint32_to_float(d32_); }
413
414
0
  static float Infinity() {
415
0
    return Single(kInfinity).value();
416
0
  }
417
418
0
  static float NaN() {
419
0
    return Single(kNaN).value();
420
0
  }
421
422
 private:
423
  static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
424
  static const int kDenormalExponent = -kExponentBias + 1;
425
  static const int kMaxExponent = 0xFF - kExponentBias;
426
  static const uint32_t kInfinity = 0x7F800000;
427
  static const uint32_t kNaN = 0x7FC00000;
428
429
  const uint32_t d32_;
430
431
  DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
432
};
433
434
}  // namespace double_conversion
435
436
// ICU PATCH: Close ICU namespace
437
U_NAMESPACE_END
438
439
#endif  // DOUBLE_CONVERSION_DOUBLE_H_
440
#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING