• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
double_to_uint64(double d)50 static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
uint64_to_double(uint64_t d64)51 static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
float_to_uint32(float f)52 static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
uint32_to_float(uint32_t d32)53 static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
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 
Double()65   Double() : d64_(0) {}
Double(double d)66   explicit Double(double d) : d64_(double_to_uint64(d)) {}
Double(uint64_t d64)67   explicit Double(uint64_t d64) : d64_(d64) {}
Double(DiyFp diy_fp)68   explicit Double(DiyFp diy_fp)
69     : 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).
AsDiyFp()73   DiyFp AsDiyFp() const {
74     ASSERT(Sign() > 0);
75     ASSERT(!IsSpecial());
76     return DiyFp(Significand(), Exponent());
77   }
78 
79   // The value encoded by this Double must be strictly greater than 0.
AsNormalizedDiyFp()80   DiyFp AsNormalizedDiyFp() const {
81     ASSERT(value() > 0.0);
82     uint64_t f = Significand();
83     int e = Exponent();
84 
85     // The current double could be a denormal.
86     while ((f & kHiddenBit) == 0) {
87       f <<= 1;
88       e--;
89     }
90     // Do the final shifts in one go.
91     f <<= DiyFp::kSignificandSize - kSignificandSize;
92     e -= DiyFp::kSignificandSize - kSignificandSize;
93     return DiyFp(f, e);
94   }
95 
96   // Returns the double's bit as uint64.
AsUint64()97   uint64_t AsUint64() const {
98     return d64_;
99   }
100 
101   // Returns the next greater double. Returns +infinity on input +infinity.
NextDouble()102   double NextDouble() const {
103     if (d64_ == kInfinity) return Double(kInfinity).value();
104     if (Sign() < 0 && Significand() == 0) {
105       // -0.0
106       return 0.0;
107     }
108     if (Sign() < 0) {
109       return Double(d64_ - 1).value();
110     } else {
111       return Double(d64_ + 1).value();
112     }
113   }
114 
PreviousDouble()115   double PreviousDouble() const {
116     if (d64_ == (kInfinity | kSignMask)) return -Infinity();
117     if (Sign() < 0) {
118       return Double(d64_ + 1).value();
119     } else {
120       if (Significand() == 0) return -0.0;
121       return Double(d64_ - 1).value();
122     }
123   }
124 
Exponent()125   int Exponent() const {
126     if (IsDenormal()) return kDenormalExponent;
127 
128     uint64_t d64 = AsUint64();
129     int biased_e =
130         static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
131     return biased_e - kExponentBias;
132   }
133 
Significand()134   uint64_t Significand() const {
135     uint64_t d64 = AsUint64();
136     uint64_t significand = d64 & kSignificandMask;
137     if (!IsDenormal()) {
138       return significand + kHiddenBit;
139     } else {
140       return significand;
141     }
142   }
143 
144   // Returns true if the double is a denormal.
IsDenormal()145   bool IsDenormal() const {
146     uint64_t d64 = AsUint64();
147     return (d64 & kExponentMask) == 0;
148   }
149 
150   // We consider denormals not to be special.
151   // Hence only Infinity and NaN are special.
IsSpecial()152   bool IsSpecial() const {
153     uint64_t d64 = AsUint64();
154     return (d64 & kExponentMask) == kExponentMask;
155   }
156 
IsNan()157   bool IsNan() const {
158     uint64_t d64 = AsUint64();
159     return ((d64 & kExponentMask) == kExponentMask) &&
160         ((d64 & kSignificandMask) != 0);
161   }
162 
IsInfinite()163   bool IsInfinite() const {
164     uint64_t d64 = AsUint64();
165     return ((d64 & kExponentMask) == kExponentMask) &&
166         ((d64 & kSignificandMask) == 0);
167   }
168 
Sign()169   int Sign() const {
170     uint64_t d64 = AsUint64();
171     return (d64 & kSignMask) == 0? 1: -1;
172   }
173 
174   // Precondition: the value encoded by this Double must be greater or equal
175   // than +0.0.
UpperBoundary()176   DiyFp UpperBoundary() const {
177     ASSERT(Sign() > 0);
178     return DiyFp(Significand() * 2 + 1, Exponent() - 1);
179   }
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.
NormalizedBoundaries(DiyFp * out_m_minus,DiyFp * out_m_plus)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));
189     DiyFp m_minus;
190     if (LowerBoundaryIsCloser()) {
191       m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
192     } else {
193       m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
194     }
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;
199   }
200 
LowerBoundaryIsCloser()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);
212   }
213 
value()214   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.
SignificandSizeForOrderOfMagnitude(int order)222   static int SignificandSizeForOrderOfMagnitude(int order) {
223     if (order >= (kDenormalExponent + kSignificandSize)) {
224       return kSignificandSize;
225     }
226     if (order <= kDenormalExponent) return 0;
227     return order - kDenormalExponent;
228   }
229 
Infinity()230   static double Infinity() {
231     return Double(kInfinity).value();
232   }
233 
NaN()234   static double NaN() {
235     return Double(kNaN).value();
236   }
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 
DiyFpToUint64(DiyFp diy_fp)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) {
251       significand >>= 1;
252       exponent++;
253     }
254     if (exponent >= kMaxExponent) {
255       return kInfinity;
256     }
257     if (exponent < kDenormalExponent) {
258       return 0;
259     }
260     while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
261       significand <<= 1;
262       exponent--;
263     }
264     uint64_t biased_exponent;
265     if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
266       biased_exponent = 0;
267     } else {
268       biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
269     }
270     return (significand & kSignificandMask) |
271         (biased_exponent << kPhysicalSignificandSize);
272   }
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 
Single()286   Single() : d32_(0) {}
Single(float f)287   explicit Single(float f) : d32_(float_to_uint32(f)) {}
Single(uint32_t d32)288   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).
AsDiyFp()292   DiyFp AsDiyFp() const {
293     ASSERT(Sign() > 0);
294     ASSERT(!IsSpecial());
295     return DiyFp(Significand(), Exponent());
296   }
297 
298   // Returns the single's bit as uint64.
AsUint32()299   uint32_t AsUint32() const {
300     return d32_;
301   }
302 
Exponent()303   int Exponent() const {
304     if (IsDenormal()) return kDenormalExponent;
305 
306     uint32_t d32 = AsUint32();
307     int biased_e =
308         static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
309     return biased_e - kExponentBias;
310   }
311 
Significand()312   uint32_t Significand() const {
313     uint32_t d32 = AsUint32();
314     uint32_t significand = d32 & kSignificandMask;
315     if (!IsDenormal()) {
316       return significand + kHiddenBit;
317     } else {
318       return significand;
319     }
320   }
321 
322   // Returns true if the single is a denormal.
IsDenormal()323   bool IsDenormal() const {
324     uint32_t d32 = AsUint32();
325     return (d32 & kExponentMask) == 0;
326   }
327 
328   // We consider denormals not to be special.
329   // Hence only Infinity and NaN are special.
IsSpecial()330   bool IsSpecial() const {
331     uint32_t d32 = AsUint32();
332     return (d32 & kExponentMask) == kExponentMask;
333   }
334 
IsNan()335   bool IsNan() const {
336     uint32_t d32 = AsUint32();
337     return ((d32 & kExponentMask) == kExponentMask) &&
338         ((d32 & kSignificandMask) != 0);
339   }
340 
IsInfinite()341   bool IsInfinite() const {
342     uint32_t d32 = AsUint32();
343     return ((d32 & kExponentMask) == kExponentMask) &&
344         ((d32 & kSignificandMask) == 0);
345   }
346 
Sign()347   int Sign() const {
348     uint32_t d32 = AsUint32();
349     return (d32 & kSignMask) == 0? 1: -1;
350   }
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.
NormalizedBoundaries(DiyFp * out_m_minus,DiyFp * out_m_plus)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));
360     DiyFp m_minus;
361     if (LowerBoundaryIsCloser()) {
362       m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
363     } else {
364       m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
365     }
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;
370   }
371 
372   // Precondition: the value encoded by this Single must be greater or equal
373   // than +0.0.
UpperBoundary()374   DiyFp UpperBoundary() const {
375     ASSERT(Sign() > 0);
376     return DiyFp(Significand() * 2 + 1, Exponent() - 1);
377   }
378 
LowerBoundaryIsCloser()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);
390   }
391 
value()392   float value() const { return uint32_to_float(d32_); }
393 
Infinity()394   static float Infinity() {
395     return Single(kInfinity).value();
396   }
397 
NaN()398   static float NaN() {
399     return Single(kNaN).value();
400   }
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
421