Coverage Report

Created: 2025-05-24 06:08

/src/double-conversion/double-conversion/ieee.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2012 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
#ifndef DOUBLE_CONVERSION_DOUBLE_H_
29
#define DOUBLE_CONVERSION_DOUBLE_H_
30
31
#include "diy-fp.h"
32
33
namespace double_conversion {
34
35
// We assume that doubles and uint64_t have the same endianness.
36
1.48k
static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
Unexecuted instantiation: string-to-double.cc:double_conversion::double_to_uint64(double)
strtod.cc:double_conversion::double_to_uint64(double)
Line
Count
Source
36
1.48k
static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
37
3.41k
static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
string-to-double.cc:double_conversion::uint64_to_double(unsigned long)
Line
Count
Source
37
523
static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
strtod.cc:double_conversion::uint64_to_double(unsigned long)
Line
Count
Source
37
2.89k
static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
38
0
static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
Unexecuted instantiation: string-to-double.cc:double_conversion::float_to_uint32(float)
Unexecuted instantiation: strtod.cc:double_conversion::float_to_uint32(float)
39
0
static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
Unexecuted instantiation: string-to-double.cc:double_conversion::uint32_to_float(unsigned int)
Unexecuted instantiation: strtod.cc:double_conversion::uint32_to_float(unsigned int)
40
41
// Helper functions for doubles.
42
class Double {
43
 public:
44
  static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
45
  static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
46
  static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
47
  static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
48
  static const uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000);
49
  static const int kPhysicalSignificandSize = 52;  // Excludes the hidden bit.
50
  static const int kSignificandSize = 53;
51
  static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
52
  static const int kMaxExponent = 0x7FF - kExponentBias;
53
54
0
  Double() : d64_(0) {}
55
1.48k
  explicit Double(double d) : d64_(double_to_uint64(d)) {}
56
1.47k
  explicit Double(uint64_t d64) : d64_(d64) {}
57
  explicit Double(DiyFp diy_fp)
58
1.94k
    : d64_(DiyFpToUint64(diy_fp)) {}
59
60
  // The value encoded by this Double must be greater or equal to +0.0.
61
  // It must not be special (infinity, or NaN).
62
0
  DiyFp AsDiyFp() const {
63
0
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
64
0
    DOUBLE_CONVERSION_ASSERT(!IsSpecial());
65
0
    return DiyFp(Significand(), Exponent());
66
0
  }
67
68
  // The value encoded by this Double must be strictly greater than 0.
69
0
  DiyFp AsNormalizedDiyFp() const {
70
0
    DOUBLE_CONVERSION_ASSERT(value() > 0.0);
71
0
    uint64_t f = Significand();
72
0
    int e = Exponent();
73
0
74
0
    // The current double could be a denormal.
75
0
    while ((f & kHiddenBit) == 0) {
76
0
      f <<= 1;
77
0
      e--;
78
0
    }
79
0
    // Do the final shifts in one go.
80
0
    f <<= DiyFp::kSignificandSize - kSignificandSize;
81
0
    e -= DiyFp::kSignificandSize - kSignificandSize;
82
0
    return DiyFp(f, e);
83
0
  }
84
85
  // Returns the double's bit as uint64.
86
5.26k
  uint64_t AsUint64() const {
87
5.26k
    return d64_;
88
5.26k
  }
89
90
  // Returns the next greater double. Returns +infinity on input +infinity.
91
551
  double NextDouble() const {
92
551
    if (d64_ == kInfinity) return Double(kInfinity).value();
93
551
    if (Sign() < 0 && Significand() == 0) {
94
      // -0.0
95
0
      return 0.0;
96
0
    }
97
551
    if (Sign() < 0) {
98
0
      return Double(d64_ - 1).value();
99
551
    } else {
100
551
      return Double(d64_ + 1).value();
101
551
    }
102
551
  }
103
104
0
  double PreviousDouble() const {
105
0
    if (d64_ == (kInfinity | kSignMask)) return -Infinity();
106
0
    if (Sign() < 0) {
107
0
      return Double(d64_ + 1).value();
108
0
    } else {
109
0
      if (Significand() == 0) return -0.0;
110
0
      return Double(d64_ - 1).value();
111
0
    }
112
0
  }
113
114
783
  int Exponent() const {
115
783
    if (IsDenormal()) return kDenormalExponent;
116
117
733
    uint64_t d64 = AsUint64();
118
733
    int biased_e =
119
733
        static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
120
733
    return biased_e - kExponentBias;
121
783
  }
122
123
930
  uint64_t Significand() const {
124
930
    uint64_t d64 = AsUint64();
125
930
    uint64_t significand = d64 & kSignificandMask;
126
930
    if (!IsDenormal()) {
127
880
      return significand + kHiddenBit;
128
880
    } else {
129
50
      return significand;
130
50
    }
131
930
  }
132
133
  // Returns true if the double is a denormal.
134
1.71k
  bool IsDenormal() const {
135
1.71k
    uint64_t d64 = AsUint64();
136
1.71k
    return (d64 & kExponentMask) == 0;
137
1.71k
  }
138
139
  // We consider denormals not to be special.
140
  // Hence only Infinity and NaN are special.
141
0
  bool IsSpecial() const {
142
0
    uint64_t d64 = AsUint64();
143
0
    return (d64 & kExponentMask) == kExponentMask;
144
0
  }
145
146
0
  bool IsNan() const {
147
0
    uint64_t d64 = AsUint64();
148
0
    return ((d64 & kExponentMask) == kExponentMask) &&
149
0
        ((d64 & kSignificandMask) != 0);
150
0
  }
151
152
0
  bool IsQuietNan() const {
153
0
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
154
0
    return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
155
0
#else
156
0
    return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
157
0
#endif
158
0
  }
159
160
0
  bool IsSignalingNan() const {
161
0
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
162
0
    return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
163
0
#else
164
0
    return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
165
0
#endif
166
0
  }
167
168
169
0
  bool IsInfinite() const {
170
0
    uint64_t d64 = AsUint64();
171
0
    return ((d64 & kExponentMask) == kExponentMask) &&
172
0
        ((d64 & kSignificandMask) == 0);
173
0
  }
174
175
1.88k
  int Sign() const {
176
1.88k
    uint64_t d64 = AsUint64();
177
1.88k
    return (d64 & kSignMask) == 0? 1: -1;
178
1.88k
  }
179
180
  // Precondition: the value encoded by this Double must be greater or equal
181
  // than +0.0.
182
783
  DiyFp UpperBoundary() const {
183
783
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
184
783
    return DiyFp(Significand() * 2 + 1, Exponent() - 1);
185
783
  }
186
187
  // Computes the two boundaries of this.
188
  // The bigger boundary (m_plus) is normalized. The lower boundary has the same
189
  // exponent as m_plus.
190
  // Precondition: the value encoded by this Double must be greater than 0.
191
0
  void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
192
0
    DOUBLE_CONVERSION_ASSERT(value() > 0.0);
193
0
    DiyFp v = this->AsDiyFp();
194
0
    DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
195
0
    DiyFp m_minus;
196
0
    if (LowerBoundaryIsCloser()) {
197
0
      m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
198
0
    } else {
199
0
      m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
200
0
    }
201
0
    m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
202
0
    m_minus.set_e(m_plus.e());
203
0
    *out_m_plus = m_plus;
204
0
    *out_m_minus = m_minus;
205
0
  }
206
207
0
  bool LowerBoundaryIsCloser() const {
208
0
    // The boundary is closer if the significand is of the form f == 2^p-1 then
209
0
    // the lower boundary is closer.
210
0
    // Think of v = 1000e10 and v- = 9999e9.
211
0
    // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
212
0
    // at a distance of 1e8.
213
0
    // The only exception is for the smallest normal: the largest denormal is
214
0
    // at the same distance as its successor.
215
0
    // Note: denormals have the same exponent as the smallest normals.
216
0
    bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
217
0
    return physical_significand_is_zero && (Exponent() != kDenormalExponent);
218
0
  }
219
220
3.41k
  double value() const { return uint64_to_double(d64_); }
221
222
  // Returns the significand size for a given order of magnitude.
223
  // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
224
  // This function returns the number of significant binary digits v will have
225
  // once it's encoded into a double. In almost all cases this is equal to
226
  // kSignificandSize. The only exceptions are denormals. They start with
227
  // leading zeroes and their effective significand-size is hence smaller.
228
1.42k
  static int SignificandSizeForOrderOfMagnitude(int order) {
229
1.42k
    if (order >= (kDenormalExponent + kSignificandSize)) {
230
1.21k
      return kSignificandSize;
231
1.21k
    }
232
215
    if (order <= kDenormalExponent) return 0;
233
170
    return order - kDenormalExponent;
234
215
  }
235
236
918
  static double Infinity() {
237
918
    return Double(kInfinity).value();
238
918
  }
239
240
2
  static double NaN() {
241
2
    return Double(kNaN).value();
242
2
  }
243
244
 private:
245
  static const int kDenormalExponent = -kExponentBias + 1;
246
  static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
247
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
248
  static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF7FFFF, FFFFFFFF);
249
#else
250
  static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
251
#endif
252
253
254
  const uint64_t d64_;
255
256
1.94k
  static uint64_t DiyFpToUint64(DiyFp diy_fp) {
257
1.94k
    uint64_t significand = diy_fp.f();
258
1.94k
    int exponent = diy_fp.e();
259
1.96k
    while (significand > kHiddenBit + kSignificandMask) {
260
14
      significand >>= 1;
261
14
      exponent++;
262
14
    }
263
1.94k
    if (exponent >= kMaxExponent) {
264
45
      return kInfinity;
265
45
    }
266
1.90k
    if (exponent < kDenormalExponent) {
267
80
      return 0;
268
80
    }
269
7.77k
    while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
270
5.95k
      significand <<= 1;
271
5.95k
      exponent--;
272
5.95k
    }
273
1.82k
    uint64_t biased_exponent;
274
1.82k
    if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
275
190
      biased_exponent = 0;
276
1.63k
    } else {
277
1.63k
      biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
278
1.63k
    }
279
1.82k
    return (significand & kSignificandMask) |
280
1.82k
        (biased_exponent << kPhysicalSignificandSize);
281
1.90k
  }
282
283
  DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
284
};
285
286
class Single {
287
 public:
288
  static const uint32_t kSignMask = 0x80000000;
289
  static const uint32_t kExponentMask = 0x7F800000;
290
  static const uint32_t kSignificandMask = 0x007FFFFF;
291
  static const uint32_t kHiddenBit = 0x00800000;
292
  static const uint32_t kQuietNanBit = 0x00400000;
293
  static const int kPhysicalSignificandSize = 23;  // Excludes the hidden bit.
294
  static const int kSignificandSize = 24;
295
296
0
  Single() : d32_(0) {}
297
0
  explicit Single(float f) : d32_(float_to_uint32(f)) {}
298
0
  explicit Single(uint32_t d32) : d32_(d32) {}
299
300
  // The value encoded by this Single must be greater or equal to +0.0.
301
  // It must not be special (infinity, or NaN).
302
0
  DiyFp AsDiyFp() const {
303
0
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
304
0
    DOUBLE_CONVERSION_ASSERT(!IsSpecial());
305
0
    return DiyFp(Significand(), Exponent());
306
0
  }
307
308
  // Returns the single's bit as uint64.
309
0
  uint32_t AsUint32() const {
310
0
    return d32_;
311
0
  }
312
313
0
  int Exponent() const {
314
0
    if (IsDenormal()) return kDenormalExponent;
315
316
0
    uint32_t d32 = AsUint32();
317
0
    int biased_e =
318
0
        static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
319
0
    return biased_e - kExponentBias;
320
0
  }
321
322
0
  uint32_t Significand() const {
323
0
    uint32_t d32 = AsUint32();
324
0
    uint32_t significand = d32 & kSignificandMask;
325
0
    if (!IsDenormal()) {
326
0
      return significand + kHiddenBit;
327
0
    } else {
328
0
      return significand;
329
0
    }
330
0
  }
331
332
  // Returns true if the single is a denormal.
333
0
  bool IsDenormal() const {
334
0
    uint32_t d32 = AsUint32();
335
0
    return (d32 & kExponentMask) == 0;
336
0
  }
337
338
  // We consider denormals not to be special.
339
  // Hence only Infinity and NaN are special.
340
0
  bool IsSpecial() const {
341
0
    uint32_t d32 = AsUint32();
342
0
    return (d32 & kExponentMask) == kExponentMask;
343
0
  }
344
345
0
  bool IsNan() const {
346
0
    uint32_t d32 = AsUint32();
347
0
    return ((d32 & kExponentMask) == kExponentMask) &&
348
0
        ((d32 & kSignificandMask) != 0);
349
0
  }
350
351
0
  bool IsQuietNan() const {
352
0
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
353
0
    return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
354
0
#else
355
0
    return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
356
0
#endif
357
0
  }
358
359
0
  bool IsSignalingNan() const {
360
0
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
361
0
    return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
362
0
#else
363
0
    return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
364
0
#endif
365
0
  }
366
367
368
0
  bool IsInfinite() const {
369
0
    uint32_t d32 = AsUint32();
370
0
    return ((d32 & kExponentMask) == kExponentMask) &&
371
0
        ((d32 & kSignificandMask) == 0);
372
0
  }
373
374
0
  int Sign() const {
375
0
    uint32_t d32 = AsUint32();
376
0
    return (d32 & kSignMask) == 0? 1: -1;
377
0
  }
378
379
  // Computes the two boundaries of this.
380
  // The bigger boundary (m_plus) is normalized. The lower boundary has the same
381
  // exponent as m_plus.
382
  // Precondition: the value encoded by this Single must be greater than 0.
383
0
  void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
384
0
    DOUBLE_CONVERSION_ASSERT(value() > 0.0);
385
0
    DiyFp v = this->AsDiyFp();
386
0
    DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
387
0
    DiyFp m_minus;
388
0
    if (LowerBoundaryIsCloser()) {
389
0
      m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
390
0
    } else {
391
0
      m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
392
0
    }
393
0
    m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
394
0
    m_minus.set_e(m_plus.e());
395
0
    *out_m_plus = m_plus;
396
0
    *out_m_minus = m_minus;
397
0
  }
398
399
  // Precondition: the value encoded by this Single must be greater or equal
400
  // than +0.0.
401
0
  DiyFp UpperBoundary() const {
402
0
    DOUBLE_CONVERSION_ASSERT(Sign() > 0);
403
0
    return DiyFp(Significand() * 2 + 1, Exponent() - 1);
404
0
  }
405
406
0
  bool LowerBoundaryIsCloser() const {
407
0
    // The boundary is closer if the significand is of the form f == 2^p-1 then
408
0
    // the lower boundary is closer.
409
0
    // Think of v = 1000e10 and v- = 9999e9.
410
0
    // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
411
0
    // at a distance of 1e8.
412
0
    // The only exception is for the smallest normal: the largest denormal is
413
0
    // at the same distance as its successor.
414
0
    // Note: denormals have the same exponent as the smallest normals.
415
0
    bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
416
0
    return physical_significand_is_zero && (Exponent() != kDenormalExponent);
417
0
  }
418
419
0
  float value() const { return uint32_to_float(d32_); }
420
421
0
  static float Infinity() {
422
0
    return Single(kInfinity).value();
423
0
  }
424
425
0
  static float NaN() {
426
0
    return Single(kNaN).value();
427
0
  }
428
429
 private:
430
  static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
431
  static const int kDenormalExponent = -kExponentBias + 1;
432
  static const int kMaxExponent = 0xFF - kExponentBias;
433
  static const uint32_t kInfinity = 0x7F800000;
434
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
435
  static const uint32_t kNaN = 0x7FBFFFFF;
436
#else
437
  static const uint32_t kNaN = 0x7FC00000;
438
#endif
439
440
  const uint32_t d32_;
441
442
  DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
443
};
444
445
}  // namespace double_conversion
446
447
#endif  // DOUBLE_CONVERSION_DOUBLE_H_