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
= 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 int kPhysicalSignificandSize
= 52; // Excludes the hidden bit.
63 static const int kSignificandSize
= 53;
64 static const int kExponentBias
= 0x3FF + kPhysicalSignificandSize
;
65 static const int kMaxExponent
= 0x7FF - kExponentBias
;
68 explicit Double(double d
) : d64_(double_to_uint64(d
)) {}
69 explicit Double(uint64_t d64
) : d64_(d64
) {}
70 explicit Double(DiyFp diy_fp
)
71 : d64_(DiyFpToUint64(diy_fp
)) {}
73 // The value encoded by this Double must be greater or equal to +0.0.
74 // It must not be special (infinity, or NaN).
75 DiyFp
AsDiyFp() const {
76 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
77 DOUBLE_CONVERSION_ASSERT(!IsSpecial());
78 return DiyFp(Significand(), Exponent());
81 // The value encoded by this Double must be strictly greater than 0.
82 DiyFp
AsNormalizedDiyFp() const {
83 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
84 uint64_t f
= Significand();
87 // The current double could be a denormal.
88 while ((f
& kHiddenBit
) == 0) {
92 // Do the final shifts in one go.
93 f
<<= DiyFp::kSignificandSize
- kSignificandSize
;
94 e
-= DiyFp::kSignificandSize
- kSignificandSize
;
98 // Returns the double's bit as uint64.
99 uint64_t AsUint64() const {
103 // Returns the next greater double. Returns +infinity on input +infinity.
104 double NextDouble() const {
105 if (d64_
== kInfinity
) return Double(kInfinity
).value();
106 if (Sign() < 0 && Significand() == 0) {
111 return Double(d64_
- 1).value();
113 return Double(d64_
+ 1).value();
117 double PreviousDouble() const {
118 if (d64_
== (kInfinity
| kSignMask
)) return -Infinity();
120 return Double(d64_
+ 1).value();
122 if (Significand() == 0) return -0.0;
123 return Double(d64_
- 1).value();
127 int Exponent() const {
128 if (IsDenormal()) return kDenormalExponent
;
130 uint64_t d64
= AsUint64();
132 static_cast<int>((d64
& kExponentMask
) >> kPhysicalSignificandSize
);
133 return biased_e
- kExponentBias
;
136 uint64_t Significand() const {
137 uint64_t d64
= AsUint64();
138 uint64_t significand
= d64
& kSignificandMask
;
140 return significand
+ kHiddenBit
;
146 // Returns true if the double is a denormal.
147 bool IsDenormal() const {
148 uint64_t d64
= AsUint64();
149 return (d64
& kExponentMask
) == 0;
152 // We consider denormals not to be special.
153 // Hence only Infinity and NaN are special.
154 bool IsSpecial() const {
155 uint64_t d64
= AsUint64();
156 return (d64
& kExponentMask
) == kExponentMask
;
160 uint64_t d64
= AsUint64();
161 return ((d64
& kExponentMask
) == kExponentMask
) &&
162 ((d64
& kSignificandMask
) != 0);
165 bool IsInfinite() const {
166 uint64_t d64
= AsUint64();
167 return ((d64
& kExponentMask
) == kExponentMask
) &&
168 ((d64
& kSignificandMask
) == 0);
172 uint64_t d64
= AsUint64();
173 return (d64
& kSignMask
) == 0? 1: -1;
176 // Precondition: the value encoded by this Double must be greater or equal
178 DiyFp
UpperBoundary() const {
179 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
180 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
183 // Computes the two boundaries of this.
184 // The bigger boundary (m_plus) is normalized. The lower boundary has the same
185 // exponent as m_plus.
186 // Precondition: the value encoded by this Double must be greater than 0.
187 void NormalizedBoundaries(DiyFp
* out_m_minus
, DiyFp
* out_m_plus
) const {
188 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
189 DiyFp v
= this->AsDiyFp();
190 DiyFp m_plus
= DiyFp::Normalize(DiyFp((v
.f() << 1) + 1, v
.e() - 1));
192 if (LowerBoundaryIsCloser()) {
193 m_minus
= DiyFp((v
.f() << 2) - 1, v
.e() - 2);
195 m_minus
= DiyFp((v
.f() << 1) - 1, v
.e() - 1);
197 m_minus
.set_f(m_minus
.f() << (m_minus
.e() - m_plus
.e()));
198 m_minus
.set_e(m_plus
.e());
199 *out_m_plus
= m_plus
;
200 *out_m_minus
= m_minus
;
203 bool LowerBoundaryIsCloser() const {
204 // The boundary is closer if the significand is of the form f == 2^p-1 then
205 // the lower boundary is closer.
206 // Think of v = 1000e10 and v- = 9999e9.
207 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
208 // at a distance of 1e8.
209 // The only exception is for the smallest normal: the largest denormal is
210 // at the same distance as its successor.
211 // Note: denormals have the same exponent as the smallest normals.
212 bool physical_significand_is_zero
= ((AsUint64() & kSignificandMask
) == 0);
213 return physical_significand_is_zero
&& (Exponent() != kDenormalExponent
);
216 double value() const { return uint64_to_double(d64_
); }
218 // Returns the significand size for a given order of magnitude.
219 // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
220 // This function returns the number of significant binary digits v will have
221 // once it's encoded into a double. In almost all cases this is equal to
222 // kSignificandSize. The only exceptions are denormals. They start with
223 // leading zeroes and their effective significand-size is hence smaller.
224 static int SignificandSizeForOrderOfMagnitude(int order
) {
225 if (order
>= (kDenormalExponent
+ kSignificandSize
)) {
226 return kSignificandSize
;
228 if (order
<= kDenormalExponent
) return 0;
229 return order
- kDenormalExponent
;
232 static double Infinity() {
233 return Double(kInfinity
).value();
236 static double NaN() {
237 return Double(kNaN
).value();
241 static const int kDenormalExponent
= -kExponentBias
+ 1;
242 static const uint64_t kInfinity
= DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
243 static const uint64_t kNaN
= DOUBLE_CONVERSION_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 DOUBLE_CONVERSION_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 {
293 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
294 DOUBLE_CONVERSION_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 DOUBLE_CONVERSION_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 {
375 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
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 DOUBLE_CONVERSION_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