1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 // From the double-conversion library. Original license:
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
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.
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.
33 // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34 #include "unicode/utypes.h"
35 #if !UCONFIG_NO_FORMATTING
37 #ifndef DOUBLE_CONVERSION_DOUBLE_H_
38 #define DOUBLE_CONVERSION_DOUBLE_H_
40 // ICU PATCH: Customize header file paths for ICU.
42 #include "double-conversion-diy-fp.h"
44 // ICU PATCH: Wrap in ICU namespace
47 namespace double_conversion
{
49 // We assume that doubles and uint64_t have the same endianness.
50 static uint64_t double_to_uint64(double d
) { return BitCast
<uint64_t>(d
); }
51 static double uint64_to_double(uint64_t d64
) { return BitCast
<double>(d64
); }
52 static uint32_t float_to_uint32(float f
) { return BitCast
<uint32_t>(f
); }
53 static float uint32_to_float(uint32_t d32
) { return BitCast
<float>(d32
); }
55 // Helper functions for doubles.
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;
66 explicit Double(double d
) : d64_(double_to_uint64(d
)) {}
67 explicit Double(uint64_t d64
) : d64_(d64
) {}
68 explicit Double(DiyFp diy_fp
)
69 : d64_(DiyFpToUint64(diy_fp
)) {}
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 DiyFp
AsDiyFp() const {
76 return DiyFp(Significand(), Exponent());
79 // The value encoded by this Double must be strictly greater than 0.
80 DiyFp
AsNormalizedDiyFp() const {
81 ASSERT(value() > 0.0);
82 uint64_t f
= Significand();
85 // The current double could be a denormal.
86 while ((f
& kHiddenBit
) == 0) {
90 // Do the final shifts in one go.
91 f
<<= DiyFp::kSignificandSize
- kSignificandSize
;
92 e
-= DiyFp::kSignificandSize
- kSignificandSize
;
96 // Returns the double's bit as uint64.
97 uint64_t AsUint64() const {
101 // Returns the next greater double. Returns +infinity on input +infinity.
102 double NextDouble() const {
103 if (d64_
== kInfinity
) return Double(kInfinity
).value();
104 if (Sign() < 0 && Significand() == 0) {
109 return Double(d64_
- 1).value();
111 return Double(d64_
+ 1).value();
115 double PreviousDouble() const {
116 if (d64_
== (kInfinity
| kSignMask
)) return -Infinity();
118 return Double(d64_
+ 1).value();
120 if (Significand() == 0) return -0.0;
121 return Double(d64_
- 1).value();
125 int Exponent() const {
126 if (IsDenormal()) return kDenormalExponent
;
128 uint64_t d64
= AsUint64();
130 static_cast<int>((d64
& kExponentMask
) >> kPhysicalSignificandSize
);
131 return biased_e
- kExponentBias
;
134 uint64_t Significand() const {
135 uint64_t d64
= AsUint64();
136 uint64_t significand
= d64
& kSignificandMask
;
138 return significand
+ kHiddenBit
;
144 // Returns true if the double is a denormal.
145 bool IsDenormal() const {
146 uint64_t d64
= AsUint64();
147 return (d64
& kExponentMask
) == 0;
150 // We consider denormals not to be special.
151 // Hence only Infinity and NaN are special.
152 bool IsSpecial() const {
153 uint64_t d64
= AsUint64();
154 return (d64
& kExponentMask
) == kExponentMask
;
158 uint64_t d64
= AsUint64();
159 return ((d64
& kExponentMask
) == kExponentMask
) &&
160 ((d64
& kSignificandMask
) != 0);
163 bool IsInfinite() const {
164 uint64_t d64
= AsUint64();
165 return ((d64
& kExponentMask
) == kExponentMask
) &&
166 ((d64
& kSignificandMask
) == 0);
170 uint64_t d64
= AsUint64();
171 return (d64
& kSignMask
) == 0? 1: -1;
174 // Precondition: the value encoded by this Double must be greater or equal
176 DiyFp
UpperBoundary() const {
178 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
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 void NormalizedBoundaries(DiyFp
* out_m_minus
, DiyFp
* out_m_plus
) const {
186 ASSERT(value() > 0.0);
187 DiyFp v
= this->AsDiyFp();
188 DiyFp m_plus
= DiyFp::Normalize(DiyFp((v
.f() << 1) + 1, v
.e() - 1));
190 if (LowerBoundaryIsCloser()) {
191 m_minus
= DiyFp((v
.f() << 2) - 1, v
.e() - 2);
193 m_minus
= DiyFp((v
.f() << 1) - 1, v
.e() - 1);
195 m_minus
.set_f(m_minus
.f() << (m_minus
.e() - m_plus
.e()));
196 m_minus
.set_e(m_plus
.e());
197 *out_m_plus
= m_plus
;
198 *out_m_minus
= m_minus
;
201 bool LowerBoundaryIsCloser() const {
202 // The boundary is closer if the significand is of the form f == 2^p-1 then
203 // the lower boundary is closer.
204 // Think of v = 1000e10 and v- = 9999e9.
205 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
206 // at a distance of 1e8.
207 // The only exception is for the smallest normal: the largest denormal is
208 // at the same distance as its successor.
209 // Note: denormals have the same exponent as the smallest normals.
210 bool physical_significand_is_zero
= ((AsUint64() & kSignificandMask
) == 0);
211 return physical_significand_is_zero
&& (Exponent() != kDenormalExponent
);
214 double value() const { return uint64_to_double(d64_
); }
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 static int SignificandSizeForOrderOfMagnitude(int order
) {
223 if (order
>= (kDenormalExponent
+ kSignificandSize
)) {
224 return kSignificandSize
;
226 if (order
<= kDenormalExponent
) return 0;
227 return order
- kDenormalExponent
;
230 static double Infinity() {
231 return Double(kInfinity
).value();
234 static double NaN() {
235 return Double(kNaN
).value();
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);
247 static uint64_t DiyFpToUint64(DiyFp diy_fp
) {
248 uint64_t significand
= diy_fp
.f();
249 int exponent
= diy_fp
.e();
250 while (significand
> kHiddenBit
+ kSignificandMask
) {
254 if (exponent
>= kMaxExponent
) {
257 if (exponent
< kDenormalExponent
) {
260 while (exponent
> kDenormalExponent
&& (significand
& kHiddenBit
) == 0) {
264 uint64_t biased_exponent
;
265 if (exponent
== kDenormalExponent
&& (significand
& kHiddenBit
) == 0) {
268 biased_exponent
= static_cast<uint64_t>(exponent
+ kExponentBias
);
270 return (significand
& kSignificandMask
) |
271 (biased_exponent
<< kPhysicalSignificandSize
);
274 DISALLOW_COPY_AND_ASSIGN(Double
);
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;
286 Single() : d32_(0) {}
287 explicit Single(float f
) : d32_(float_to_uint32(f
)) {}
288 explicit Single(uint32_t d32
) : d32_(d32
) {}
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 DiyFp
AsDiyFp() const {
294 ASSERT(!IsSpecial());
295 return DiyFp(Significand(), Exponent());
298 // Returns the single's bit as uint64.
299 uint32_t AsUint32() const {
303 int Exponent() const {
304 if (IsDenormal()) return kDenormalExponent
;
306 uint32_t d32
= AsUint32();
308 static_cast<int>((d32
& kExponentMask
) >> kPhysicalSignificandSize
);
309 return biased_e
- kExponentBias
;
312 uint32_t Significand() const {
313 uint32_t d32
= AsUint32();
314 uint32_t significand
= d32
& kSignificandMask
;
316 return significand
+ kHiddenBit
;
322 // Returns true if the single is a denormal.
323 bool IsDenormal() const {
324 uint32_t d32
= AsUint32();
325 return (d32
& kExponentMask
) == 0;
328 // We consider denormals not to be special.
329 // Hence only Infinity and NaN are special.
330 bool IsSpecial() const {
331 uint32_t d32
= AsUint32();
332 return (d32
& kExponentMask
) == kExponentMask
;
336 uint32_t d32
= AsUint32();
337 return ((d32
& kExponentMask
) == kExponentMask
) &&
338 ((d32
& kSignificandMask
) != 0);
341 bool IsInfinite() const {
342 uint32_t d32
= AsUint32();
343 return ((d32
& kExponentMask
) == kExponentMask
) &&
344 ((d32
& kSignificandMask
) == 0);
348 uint32_t d32
= AsUint32();
349 return (d32
& kSignMask
) == 0? 1: -1;
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 void NormalizedBoundaries(DiyFp
* out_m_minus
, DiyFp
* out_m_plus
) const {
357 ASSERT(value() > 0.0);
358 DiyFp v
= this->AsDiyFp();
359 DiyFp m_plus
= DiyFp::Normalize(DiyFp((v
.f() << 1) + 1, v
.e() - 1));
361 if (LowerBoundaryIsCloser()) {
362 m_minus
= DiyFp((v
.f() << 2) - 1, v
.e() - 2);
364 m_minus
= DiyFp((v
.f() << 1) - 1, v
.e() - 1);
366 m_minus
.set_f(m_minus
.f() << (m_minus
.e() - m_plus
.e()));
367 m_minus
.set_e(m_plus
.e());
368 *out_m_plus
= m_plus
;
369 *out_m_minus
= m_minus
;
372 // Precondition: the value encoded by this Single must be greater or equal
374 DiyFp
UpperBoundary() const {
376 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
379 bool LowerBoundaryIsCloser() const {
380 // The boundary is closer if the significand is of the form f == 2^p-1 then
381 // the lower boundary is closer.
382 // Think of v = 1000e10 and v- = 9999e9.
383 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
384 // at a distance of 1e8.
385 // The only exception is for the smallest normal: the largest denormal is
386 // at the same distance as its successor.
387 // Note: denormals have the same exponent as the smallest normals.
388 bool physical_significand_is_zero
= ((AsUint32() & kSignificandMask
) == 0);
389 return physical_significand_is_zero
&& (Exponent() != kDenormalExponent
);
392 float value() const { return uint32_to_float(d32_
); }
394 static float Infinity() {
395 return Single(kInfinity
).value();
399 return Single(kNaN
).value();
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;
411 DISALLOW_COPY_AND_ASSIGN(Single
);
414 } // namespace double_conversion
416 // ICU PATCH: Close ICU namespace
419 #endif // DOUBLE_CONVERSION_DOUBLE_H_
420 #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING