Coverage Report

Created: 2018-09-25 14:53

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