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. |
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); } |
54 | |
55 | // Helper functions for doubles. |
56 | class 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); |
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; |
66 | |
67 | Double() : d64_(0) {} |
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)) {} |
72 | |
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()); |
79 | } |
80 | |
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(); |
85 | int e = Exponent(); |
86 | |
87 | // The current double could be a denormal. |
88 | while ((f & kHiddenBit) == 0) { |
89 | f <<= 1; |
90 | e--; |
91 | } |
92 | // Do the final shifts in one go. |
93 | f <<= DiyFp::kSignificandSize - kSignificandSize; |
94 | e -= DiyFp::kSignificandSize - kSignificandSize; |
95 | return DiyFp(f, e); |
96 | } |
97 | |
98 | // Returns the double's bit as uint64. |
99 | uint64_t AsUint64() const { |
100 | return d64_; |
101 | } |
102 | |
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) { |
107 | // -0.0 |
108 | return 0.0; |
109 | } |
110 | if (Sign() < 0) { |
111 | return Double(d64_ - 1).value(); |
112 | } else { |
113 | return Double(d64_ + 1).value(); |
114 | } |
115 | } |
116 | |
117 | double PreviousDouble() const { |
118 | if (d64_ == (kInfinity | kSignMask)) return -Infinity(); |
119 | if (Sign() < 0) { |
120 | return Double(d64_ + 1).value(); |
121 | } else { |
122 | if (Significand() == 0) return -0.0; |
123 | return Double(d64_ - 1).value(); |
124 | } |
125 | } |
126 | |
127 | int Exponent() const { |
128 | if (IsDenormal()) return kDenormalExponent; |
129 | |
130 | uint64_t d64 = AsUint64(); |
131 | int biased_e = |
132 | static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize); |
133 | return biased_e - kExponentBias; |
134 | } |
135 | |
136 | uint64_t Significand() const { |
137 | uint64_t d64 = AsUint64(); |
138 | uint64_t significand = d64 & kSignificandMask; |
139 | if (!IsDenormal()) { |
140 | return significand + kHiddenBit; |
141 | } else { |
142 | return significand; |
143 | } |
144 | } |
145 | |
146 | // Returns true if the double is a denormal. |
147 | bool IsDenormal() const { |
148 | uint64_t d64 = AsUint64(); |
149 | return (d64 & kExponentMask) == 0; |
150 | } |
151 | |
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; |
157 | } |
158 | |
159 | bool IsNan() const { |
160 | uint64_t d64 = AsUint64(); |
161 | return ((d64 & kExponentMask) == kExponentMask) && |
162 | ((d64 & kSignificandMask) != 0); |
163 | } |
164 | |
165 | bool IsInfinite() const { |
166 | uint64_t d64 = AsUint64(); |
167 | return ((d64 & kExponentMask) == kExponentMask) && |
168 | ((d64 & kSignificandMask) == 0); |
169 | } |
170 | |
171 | int Sign() const { |
172 | uint64_t d64 = AsUint64(); |
173 | return (d64 & kSignMask) == 0? 1: -1; |
174 | } |
175 | |
176 | // Precondition: the value encoded by this Double must be greater or equal |
177 | // than +0.0. |
178 | DiyFp UpperBoundary() const { |
179 | DOUBLE_CONVERSION_ASSERT(Sign() > 0); |
180 | return DiyFp(Significand() * 2 + 1, Exponent() - 1); |
181 | } |
182 | |
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)); |
191 | DiyFp m_minus; |
192 | if (LowerBoundaryIsCloser()) { |
193 | m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); |
194 | } else { |
195 | m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); |
196 | } |
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; |
201 | } |
202 | |
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); |
214 | } |
215 | |
216 | double value() const { return uint64_to_double(d64_); } |
217 | |
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; |
227 | } |
228 | if (order <= kDenormalExponent) return 0; |
229 | return order - kDenormalExponent; |
230 | } |
231 | |
232 | static double Infinity() { |
233 | return Double(kInfinity).value(); |
234 | } |
235 | |
236 | static double NaN() { |
237 | return Double(kNaN).value(); |
238 | } |
239 | |
240 | private: |
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); |
244 | |
245 | const uint64_t d64_; |
246 | |
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 | DOUBLE_CONVERSION_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 | |
286 | Single() : d32_(0) {} |
287 | explicit Single(float f) : d32_(float_to_uint32(f)) {} |
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). |
292 | DiyFp AsDiyFp() const { |
293 | DOUBLE_CONVERSION_ASSERT(Sign() > 0); |
294 | DOUBLE_CONVERSION_ASSERT(!IsSpecial()); |
295 | return DiyFp(Significand(), Exponent()); |
296 | } |
297 | |
298 | // Returns the single's bit as uint64. |
299 | uint32_t AsUint32() const { |
300 | return d32_; |
301 | } |
302 | |
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 | |
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. |
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. |
330 | bool IsSpecial() const { |
331 | uint32_t d32 = AsUint32(); |
332 | return (d32 & kExponentMask) == kExponentMask; |
333 | } |
334 | |
335 | bool IsNan() const { |
336 | uint32_t d32 = AsUint32(); |
337 | return ((d32 & kExponentMask) == kExponentMask) && |
338 | ((d32 & kSignificandMask) != 0); |
339 | } |
340 | |
341 | bool IsInfinite() const { |
342 | uint32_t d32 = AsUint32(); |
343 | return ((d32 & kExponentMask) == kExponentMask) && |
344 | ((d32 & kSignificandMask) == 0); |
345 | } |
346 | |
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. |
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)); |
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. |
374 | DiyFp UpperBoundary() const { |
375 | DOUBLE_CONVERSION_ASSERT(Sign() > 0); |
376 | return DiyFp(Significand() * 2 + 1, Exponent() - 1); |
377 | } |
378 | |
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 | |
392 | float value() const { return uint32_to_float(d32_); } |
393 | |
394 | static float Infinity() { |
395 | return Single(kInfinity).value(); |
396 | } |
397 | |
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 | DOUBLE_CONVERSION_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 | |