blob: 31c35867de5610fbf1292738af3e9fd32b05bcab [file] [log] [blame]
Victor Chang73229502020-09-17 13:39:19 +01001// © 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
45U_NAMESPACE_BEGIN
46
47namespace double_conversion {
48
49// We assume that doubles and uint64_t have the same endianness.
50static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
51static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
52static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
53static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
54
55// Helper functions for doubles.
56class 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);
Victor Changd8aa9d52021-01-05 23:49:57 +000062 static const uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000);
Victor Chang73229502020-09-17 13:39:19 +010063 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 Double() : d64_(0) {}
69 explicit Double(double d) : d64_(double_to_uint64(d)) {}
70 explicit Double(uint64_t d64) : d64_(d64) {}
71 explicit Double(DiyFp diy_fp)
72 : 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 DiyFp AsDiyFp() const {
77 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
78 DOUBLE_CONVERSION_ASSERT(!IsSpecial());
79 return DiyFp(Significand(), Exponent());
80 }
81
82 // The value encoded by this Double must be strictly greater than 0.
83 DiyFp AsNormalizedDiyFp() const {
84 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
85 uint64_t f = Significand();
86 int e = Exponent();
87
88 // The current double could be a denormal.
89 while ((f & kHiddenBit) == 0) {
90 f <<= 1;
91 e--;
92 }
93 // Do the final shifts in one go.
94 f <<= DiyFp::kSignificandSize - kSignificandSize;
95 e -= DiyFp::kSignificandSize - kSignificandSize;
96 return DiyFp(f, e);
97 }
98
99 // Returns the double's bit as uint64.
100 uint64_t AsUint64() const {
101 return d64_;
102 }
103
104 // Returns the next greater double. Returns +infinity on input +infinity.
105 double NextDouble() const {
106 if (d64_ == kInfinity) return Double(kInfinity).value();
107 if (Sign() < 0 && Significand() == 0) {
108 // -0.0
109 return 0.0;
110 }
111 if (Sign() < 0) {
112 return Double(d64_ - 1).value();
113 } else {
114 return Double(d64_ + 1).value();
115 }
116 }
117
118 double PreviousDouble() const {
119 if (d64_ == (kInfinity | kSignMask)) return -Infinity();
120 if (Sign() < 0) {
121 return Double(d64_ + 1).value();
122 } else {
123 if (Significand() == 0) return -0.0;
124 return Double(d64_ - 1).value();
125 }
126 }
127
128 int Exponent() const {
129 if (IsDenormal()) return kDenormalExponent;
130
131 uint64_t d64 = AsUint64();
132 int biased_e =
133 static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
134 return biased_e - kExponentBias;
135 }
136
137 uint64_t Significand() const {
138 uint64_t d64 = AsUint64();
139 uint64_t significand = d64 & kSignificandMask;
140 if (!IsDenormal()) {
141 return significand + kHiddenBit;
142 } else {
143 return significand;
144 }
145 }
146
147 // Returns true if the double is a denormal.
148 bool IsDenormal() const {
149 uint64_t d64 = AsUint64();
150 return (d64 & kExponentMask) == 0;
151 }
152
153 // We consider denormals not to be special.
154 // Hence only Infinity and NaN are special.
155 bool IsSpecial() const {
156 uint64_t d64 = AsUint64();
157 return (d64 & kExponentMask) == kExponentMask;
158 }
159
160 bool IsNan() const {
161 uint64_t d64 = AsUint64();
162 return ((d64 & kExponentMask) == kExponentMask) &&
163 ((d64 & kSignificandMask) != 0);
164 }
165
Victor Changd8aa9d52021-01-05 23:49:57 +0000166 bool IsQuietNan() const {
167 return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
168 }
169
170 bool IsSignalingNan() const {
171 return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
172 }
173
174
Victor Chang73229502020-09-17 13:39:19 +0100175 bool IsInfinite() const {
176 uint64_t d64 = AsUint64();
177 return ((d64 & kExponentMask) == kExponentMask) &&
178 ((d64 & kSignificandMask) == 0);
179 }
180
181 int Sign() const {
182 uint64_t d64 = AsUint64();
183 return (d64 & kSignMask) == 0? 1: -1;
184 }
185
186 // Precondition: the value encoded by this Double must be greater or equal
187 // than +0.0.
188 DiyFp UpperBoundary() const {
189 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
190 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
191 }
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 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
198 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
199 DiyFp v = this->AsDiyFp();
200 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
201 DiyFp m_minus;
202 if (LowerBoundaryIsCloser()) {
203 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
204 } else {
205 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
206 }
207 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
208 m_minus.set_e(m_plus.e());
209 *out_m_plus = m_plus;
210 *out_m_minus = m_minus;
211 }
212
213 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 bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
223 return physical_significand_is_zero && (Exponent() != kDenormalExponent);
224 }
225
226 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 static int SignificandSizeForOrderOfMagnitude(int order) {
235 if (order >= (kDenormalExponent + kSignificandSize)) {
236 return kSignificandSize;
237 }
238 if (order <= kDenormalExponent) return 0;
239 return order - kDenormalExponent;
240 }
241
242 static double Infinity() {
243 return Double(kInfinity).value();
244 }
245
246 static double NaN() {
247 return Double(kNaN).value();
248 }
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 static uint64_t DiyFpToUint64(DiyFp diy_fp) {
258 uint64_t significand = diy_fp.f();
259 int exponent = diy_fp.e();
260 while (significand > kHiddenBit + kSignificandMask) {
261 significand >>= 1;
262 exponent++;
263 }
264 if (exponent >= kMaxExponent) {
265 return kInfinity;
266 }
267 if (exponent < kDenormalExponent) {
268 return 0;
269 }
270 while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
271 significand <<= 1;
272 exponent--;
273 }
274 uint64_t biased_exponent;
275 if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
276 biased_exponent = 0;
277 } else {
278 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
279 }
280 return (significand & kSignificandMask) |
281 (biased_exponent << kPhysicalSignificandSize);
282 }
283
284 DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
285};
286
287class 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;
Victor Changd8aa9d52021-01-05 23:49:57 +0000293 static const uint32_t kQuietNanBit = 0x00400000;
Victor Chang73229502020-09-17 13:39:19 +0100294 static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit.
295 static const int kSignificandSize = 24;
296
297 Single() : d32_(0) {}
298 explicit Single(float f) : d32_(float_to_uint32(f)) {}
299 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 DiyFp AsDiyFp() const {
304 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
305 DOUBLE_CONVERSION_ASSERT(!IsSpecial());
306 return DiyFp(Significand(), Exponent());
307 }
308
309 // Returns the single's bit as uint64.
310 uint32_t AsUint32() const {
311 return d32_;
312 }
313
314 int Exponent() const {
315 if (IsDenormal()) return kDenormalExponent;
316
317 uint32_t d32 = AsUint32();
318 int biased_e =
319 static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
320 return biased_e - kExponentBias;
321 }
322
323 uint32_t Significand() const {
324 uint32_t d32 = AsUint32();
325 uint32_t significand = d32 & kSignificandMask;
326 if (!IsDenormal()) {
327 return significand + kHiddenBit;
328 } else {
329 return significand;
330 }
331 }
332
333 // Returns true if the single is a denormal.
334 bool IsDenormal() const {
335 uint32_t d32 = AsUint32();
336 return (d32 & kExponentMask) == 0;
337 }
338
339 // We consider denormals not to be special.
340 // Hence only Infinity and NaN are special.
341 bool IsSpecial() const {
342 uint32_t d32 = AsUint32();
343 return (d32 & kExponentMask) == kExponentMask;
344 }
345
346 bool IsNan() const {
347 uint32_t d32 = AsUint32();
348 return ((d32 & kExponentMask) == kExponentMask) &&
349 ((d32 & kSignificandMask) != 0);
350 }
351
Victor Changd8aa9d52021-01-05 23:49:57 +0000352 bool IsQuietNan() const {
353 return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
354 }
355
356 bool IsSignalingNan() const {
357 return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
358 }
359
360
Victor Chang73229502020-09-17 13:39:19 +0100361 bool IsInfinite() const {
362 uint32_t d32 = AsUint32();
363 return ((d32 & kExponentMask) == kExponentMask) &&
364 ((d32 & kSignificandMask) == 0);
365 }
366
367 int Sign() const {
368 uint32_t d32 = AsUint32();
369 return (d32 & kSignMask) == 0? 1: -1;
370 }
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 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
377 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
378 DiyFp v = this->AsDiyFp();
379 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
380 DiyFp m_minus;
381 if (LowerBoundaryIsCloser()) {
382 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
383 } else {
384 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
385 }
386 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
387 m_minus.set_e(m_plus.e());
388 *out_m_plus = m_plus;
389 *out_m_minus = m_minus;
390 }
391
392 // Precondition: the value encoded by this Single must be greater or equal
393 // than +0.0.
394 DiyFp UpperBoundary() const {
395 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
396 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
397 }
398
399 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 bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
409 return physical_significand_is_zero && (Exponent() != kDenormalExponent);
410 }
411
412 float value() const { return uint32_to_float(d32_); }
413
414 static float Infinity() {
415 return Single(kInfinity).value();
416 }
417
418 static float NaN() {
419 return Single(kNaN).value();
420 }
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
437U_NAMESPACE_END
438
439#endif // DOUBLE_CONVERSION_DOUBLE_H_
440#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING