/src/icu/icu4c/source/i18n/double-conversion-ieee.h
Line | Count | Source |
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 | 1.19M | static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }double-conversion-double-to-string.cpp:icu_79::double_conversion::double_to_uint64(double) Line | Count | Source | 50 | 379k | static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); } |
double-conversion-fast-dtoa.cpp:icu_79::double_conversion::double_to_uint64(double) Line | Count | Source | 50 | 758k | static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); } |
Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_79::double_conversion::double_to_uint64(double) double-conversion-strtod.cpp:icu_79::double_conversion::double_to_uint64(double) Line | Count | Source | 50 | 18.8k | static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); } |
double-conversion-bignum-dtoa.cpp:icu_79::double_conversion::double_to_uint64(double) Line | Count | Source | 50 | 35.5k | static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); } |
|
51 | 370k | static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }Unexecuted instantiation: double-conversion-double-to-string.cpp:icu_79::double_conversion::uint64_to_double(unsigned long) Unexecuted instantiation: double-conversion-fast-dtoa.cpp:icu_79::double_conversion::uint64_to_double(unsigned long) Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_79::double_conversion::uint64_to_double(unsigned long) double-conversion-strtod.cpp:icu_79::double_conversion::uint64_to_double(unsigned long) Line | Count | Source | 51 | 370k | static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); } |
Unexecuted instantiation: double-conversion-bignum-dtoa.cpp:icu_79::double_conversion::uint64_to_double(unsigned long) |
52 | 0 | static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }Unexecuted instantiation: double-conversion-double-to-string.cpp:icu_79::double_conversion::float_to_uint32(float) Unexecuted instantiation: double-conversion-fast-dtoa.cpp:icu_79::double_conversion::float_to_uint32(float) Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_79::double_conversion::float_to_uint32(float) Unexecuted instantiation: double-conversion-strtod.cpp:icu_79::double_conversion::float_to_uint32(float) Unexecuted instantiation: double-conversion-bignum-dtoa.cpp:icu_79::double_conversion::float_to_uint32(float) |
53 | 0 | static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }Unexecuted instantiation: double-conversion-double-to-string.cpp:icu_79::double_conversion::uint32_to_float(unsigned int) Unexecuted instantiation: double-conversion-fast-dtoa.cpp:icu_79::double_conversion::uint32_to_float(unsigned int) Unexecuted instantiation: double-conversion-string-to-double.cpp:icu_79::double_conversion::uint32_to_float(unsigned int) Unexecuted instantiation: double-conversion-strtod.cpp:icu_79::double_conversion::uint32_to_float(unsigned int) Unexecuted instantiation: double-conversion-bignum-dtoa.cpp:icu_79::double_conversion::uint32_to_float(unsigned int) |
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 uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000); |
63 | | 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 | 0 | Double() : d64_(0) {} |
69 | 1.19M | explicit Double(double d) : d64_(double_to_uint64(d)) {} |
70 | 24.5k | explicit Double(uint64_t d64) : d64_(d64) {} |
71 | | explicit Double(DiyFp diy_fp) |
72 | 345k | : 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 | 379k | DiyFp AsDiyFp() const { |
77 | 379k | DOUBLE_CONVERSION_ASSERT(Sign() > 0); |
78 | 379k | DOUBLE_CONVERSION_ASSERT(!IsSpecial()); |
79 | 379k | return DiyFp(Significand(), Exponent()); |
80 | 379k | } |
81 | | |
82 | | // The value encoded by this Double must be strictly greater than 0. |
83 | 379k | DiyFp AsNormalizedDiyFp() const { |
84 | 379k | DOUBLE_CONVERSION_ASSERT(value() > 0.0); |
85 | 379k | uint64_t f = Significand(); |
86 | 379k | int e = Exponent(); |
87 | | |
88 | | // The current double could be a denormal. |
89 | 1.33M | while ((f & kHiddenBit) == 0) { |
90 | 955k | f <<= 1; |
91 | 955k | e--; |
92 | 955k | } |
93 | | // Do the final shifts in one go. |
94 | 379k | f <<= DiyFp::kSignificandSize - kSignificandSize; |
95 | 379k | e -= DiyFp::kSignificandSize - kSignificandSize; |
96 | 379k | return DiyFp(f, e); |
97 | 379k | } |
98 | | |
99 | | // Returns the double's bit as uint64. |
100 | 3.77M | uint64_t AsUint64() const { |
101 | 3.77M | return d64_; |
102 | 3.77M | } |
103 | | |
104 | | // Returns the next greater double. Returns +infinity on input +infinity. |
105 | 5.41k | double NextDouble() const { |
106 | 5.41k | if (d64_ == kInfinity) return Double(kInfinity).value(); |
107 | 5.41k | if (Sign() < 0 && Significand() == 0) { |
108 | | // -0.0 |
109 | 0 | return 0.0; |
110 | 0 | } |
111 | 5.41k | if (Sign() < 0) { |
112 | 0 | return Double(d64_ - 1).value(); |
113 | 5.41k | } else { |
114 | 5.41k | return Double(d64_ + 1).value(); |
115 | 5.41k | } |
116 | 5.41k | } |
117 | | |
118 | 0 | double PreviousDouble() const { |
119 | 0 | if (d64_ == (kInfinity | kSignMask)) return -Infinity(); |
120 | 0 | if (Sign() < 0) { |
121 | 0 | return Double(d64_ + 1).value(); |
122 | 0 | } else { |
123 | 0 | if (Significand() == 0) return -0.0; |
124 | 0 | return Double(d64_ - 1).value(); |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 785k | int Exponent() const { |
129 | 785k | if (IsDenormal()) return kDenormalExponent; |
130 | | |
131 | 644k | uint64_t d64 = AsUint64(); |
132 | 644k | int biased_e = |
133 | 644k | static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize); |
134 | 644k | return biased_e - kExponentBias; |
135 | 785k | } |
136 | | |
137 | 783k | uint64_t Significand() const { |
138 | 783k | uint64_t d64 = AsUint64(); |
139 | 783k | uint64_t significand = d64 & kSignificandMask; |
140 | 783k | if (!IsDenormal()) { |
141 | 643k | return significand + kHiddenBit; |
142 | 643k | } else { |
143 | 140k | return significand; |
144 | 140k | } |
145 | 783k | } |
146 | | |
147 | | // Returns true if the double is a denormal. |
148 | 1.56M | bool IsDenormal() const { |
149 | 1.56M | uint64_t d64 = AsUint64(); |
150 | 1.56M | return (d64 & kExponentMask) == 0; |
151 | 1.56M | } |
152 | | |
153 | | // We consider denormals not to be special. |
154 | | // Hence only Infinity and NaN are special. |
155 | 0 | bool IsSpecial() const { |
156 | 0 | uint64_t d64 = AsUint64(); |
157 | 0 | return (d64 & kExponentMask) == kExponentMask; |
158 | 0 | } |
159 | | |
160 | 0 | bool IsNan() const { |
161 | 0 | uint64_t d64 = AsUint64(); |
162 | 0 | return ((d64 & kExponentMask) == kExponentMask) && |
163 | 0 | ((d64 & kSignificandMask) != 0); |
164 | 0 | } |
165 | | |
166 | 0 | bool IsQuietNan() const { |
167 | 0 | #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) |
168 | 0 | return IsNan() && ((AsUint64() & kQuietNanBit) == 0); |
169 | 0 | #else |
170 | 0 | return IsNan() && ((AsUint64() & kQuietNanBit) != 0); |
171 | 0 | #endif |
172 | 0 | } |
173 | | |
174 | 0 | bool IsSignalingNan() const { |
175 | 0 | #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) |
176 | 0 | return IsNan() && ((AsUint64() & kQuietNanBit) != 0); |
177 | 0 | #else |
178 | 0 | return IsNan() && ((AsUint64() & kQuietNanBit) == 0); |
179 | 0 | #endif |
180 | 0 | } |
181 | | |
182 | | |
183 | 0 | bool IsInfinite() const { |
184 | 0 | uint64_t d64 = AsUint64(); |
185 | 0 | return ((d64 & kExponentMask) == kExponentMask) && |
186 | 0 | ((d64 & kSignificandMask) == 0); |
187 | 0 | } |
188 | | |
189 | 390k | int Sign() const { |
190 | 390k | uint64_t d64 = AsUint64(); |
191 | 390k | return (d64 & kSignMask) == 0? 1: -1; |
192 | 390k | } |
193 | | |
194 | | // Precondition: the value encoded by this Double must be greater or equal |
195 | | // than +0.0. |
196 | 10.4k | DiyFp UpperBoundary() const { |
197 | 10.4k | DOUBLE_CONVERSION_ASSERT(Sign() > 0); |
198 | 10.4k | return DiyFp(Significand() * 2 + 1, Exponent() - 1); |
199 | 10.4k | } |
200 | | |
201 | | // Computes the two boundaries of this. |
202 | | // The bigger boundary (m_plus) is normalized. The lower boundary has the same |
203 | | // exponent as m_plus. |
204 | | // Precondition: the value encoded by this Double must be greater than 0. |
205 | 379k | void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { |
206 | 379k | DOUBLE_CONVERSION_ASSERT(value() > 0.0); |
207 | 379k | DiyFp v = this->AsDiyFp(); |
208 | 379k | DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); |
209 | 379k | DiyFp m_minus; |
210 | 379k | if (LowerBoundaryIsCloser()) { |
211 | 3.69k | m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); |
212 | 375k | } else { |
213 | 375k | m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); |
214 | 375k | } |
215 | 379k | m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); |
216 | 379k | m_minus.set_e(m_plus.e()); |
217 | 379k | *out_m_plus = m_plus; |
218 | 379k | *out_m_minus = m_minus; |
219 | 379k | } |
220 | | |
221 | 391k | bool LowerBoundaryIsCloser() const { |
222 | | // The boundary is closer if the significand is of the form f == 2^p-1 then |
223 | | // the lower boundary is closer. |
224 | | // Think of v = 1000e10 and v- = 9999e9. |
225 | | // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but |
226 | | // at a distance of 1e8. |
227 | | // The only exception is for the smallest normal: the largest denormal is |
228 | | // at the same distance as its successor. |
229 | | // Note: denormals have the same exponent as the smallest normals. |
230 | 391k | bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0); |
231 | 391k | return physical_significand_is_zero && (Exponent() != kDenormalExponent); |
232 | 391k | } |
233 | | |
234 | 370k | double value() const { return uint64_to_double(d64_); } |
235 | | |
236 | | // Returns the significand size for a given order of magnitude. |
237 | | // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude. |
238 | | // This function returns the number of significant binary digits v will have |
239 | | // once it's encoded into a double. In almost all cases this is equal to |
240 | | // kSignificandSize. The only exceptions are denormals. They start with |
241 | | // leading zeroes and their effective significand-size is hence smaller. |
242 | 345k | static int SignificandSizeForOrderOfMagnitude(int order) { |
243 | 345k | if (order >= (kDenormalExponent + kSignificandSize)) { |
244 | 342k | return kSignificandSize; |
245 | 342k | } |
246 | 2.75k | if (order <= kDenormalExponent) return 0; |
247 | 1.32k | return order - kDenormalExponent; |
248 | 2.75k | } |
249 | | |
250 | 19.0k | static double Infinity() { |
251 | 19.0k | return Double(kInfinity).value(); |
252 | 19.0k | } |
253 | | |
254 | 0 | static double NaN() { |
255 | 0 | return Double(kNaN).value(); |
256 | 0 | } |
257 | | |
258 | | private: |
259 | | static const int kDenormalExponent = -kExponentBias + 1; |
260 | | static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000); |
261 | | #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) |
262 | | static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF7FFFF, FFFFFFFF); |
263 | | #else |
264 | | static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000); |
265 | | #endif |
266 | | |
267 | | |
268 | | const uint64_t d64_; |
269 | | |
270 | 345k | static uint64_t DiyFpToUint64(DiyFp diy_fp) { |
271 | 345k | uint64_t significand = diy_fp.f(); |
272 | 345k | int exponent = diy_fp.e(); |
273 | 347k | while (significand > kHiddenBit + kSignificandMask) { |
274 | 2.12k | significand >>= 1; |
275 | 2.12k | exponent++; |
276 | 2.12k | } |
277 | 345k | if (exponent >= kMaxExponent) { |
278 | 534 | return kInfinity; |
279 | 534 | } |
280 | 345k | if (exponent < kDenormalExponent) { |
281 | 854 | return 0; |
282 | 854 | } |
283 | 344k | while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) { |
284 | 0 | significand <<= 1; |
285 | 0 | exponent--; |
286 | 0 | } |
287 | 344k | uint64_t biased_exponent; |
288 | 344k | if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) { |
289 | 1.90k | biased_exponent = 0; |
290 | 342k | } else { |
291 | 342k | biased_exponent = static_cast<uint64_t>(exponent + kExponentBias); |
292 | 342k | } |
293 | 344k | return (significand & kSignificandMask) | |
294 | 344k | (biased_exponent << kPhysicalSignificandSize); |
295 | 345k | } |
296 | | |
297 | | DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double); |
298 | | }; |
299 | | |
300 | | class Single { |
301 | | public: |
302 | | static const uint32_t kSignMask = 0x80000000; |
303 | | static const uint32_t kExponentMask = 0x7F800000; |
304 | | static const uint32_t kSignificandMask = 0x007FFFFF; |
305 | | static const uint32_t kHiddenBit = 0x00800000; |
306 | | static const uint32_t kQuietNanBit = 0x00400000; |
307 | | static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit. |
308 | | static const int kSignificandSize = 24; |
309 | | |
310 | 0 | Single() : d32_(0) {} |
311 | 0 | explicit Single(float f) : d32_(float_to_uint32(f)) {} |
312 | 0 | explicit Single(uint32_t d32) : d32_(d32) {} |
313 | | |
314 | | // The value encoded by this Single must be greater or equal to +0.0. |
315 | | // It must not be special (infinity, or NaN). |
316 | 0 | DiyFp AsDiyFp() const { |
317 | 0 | DOUBLE_CONVERSION_ASSERT(Sign() > 0); |
318 | 0 | DOUBLE_CONVERSION_ASSERT(!IsSpecial()); |
319 | 0 | return DiyFp(Significand(), Exponent()); |
320 | 0 | } |
321 | | |
322 | | // Returns the single's bit as uint64. |
323 | 0 | uint32_t AsUint32() const { |
324 | 0 | return d32_; |
325 | 0 | } |
326 | | |
327 | 0 | int Exponent() const { |
328 | 0 | if (IsDenormal()) return kDenormalExponent; |
329 | | |
330 | 0 | uint32_t d32 = AsUint32(); |
331 | 0 | int biased_e = |
332 | 0 | static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize); |
333 | 0 | return biased_e - kExponentBias; |
334 | 0 | } |
335 | | |
336 | 0 | uint32_t Significand() const { |
337 | 0 | uint32_t d32 = AsUint32(); |
338 | 0 | uint32_t significand = d32 & kSignificandMask; |
339 | 0 | if (!IsDenormal()) { |
340 | 0 | return significand + kHiddenBit; |
341 | 0 | } else { |
342 | 0 | return significand; |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | | // Returns true if the single is a denormal. |
347 | 0 | bool IsDenormal() const { |
348 | 0 | uint32_t d32 = AsUint32(); |
349 | 0 | return (d32 & kExponentMask) == 0; |
350 | 0 | } |
351 | | |
352 | | // We consider denormals not to be special. |
353 | | // Hence only Infinity and NaN are special. |
354 | 0 | bool IsSpecial() const { |
355 | 0 | uint32_t d32 = AsUint32(); |
356 | 0 | return (d32 & kExponentMask) == kExponentMask; |
357 | 0 | } |
358 | | |
359 | 0 | bool IsNan() const { |
360 | 0 | uint32_t d32 = AsUint32(); |
361 | 0 | return ((d32 & kExponentMask) == kExponentMask) && |
362 | 0 | ((d32 & kSignificandMask) != 0); |
363 | 0 | } |
364 | | |
365 | 0 | bool IsQuietNan() const { |
366 | 0 | #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) |
367 | 0 | return IsNan() && ((AsUint32() & kQuietNanBit) == 0); |
368 | 0 | #else |
369 | 0 | return IsNan() && ((AsUint32() & kQuietNanBit) != 0); |
370 | 0 | #endif |
371 | 0 | } |
372 | | |
373 | 0 | bool IsSignalingNan() const { |
374 | 0 | #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) |
375 | 0 | return IsNan() && ((AsUint32() & kQuietNanBit) != 0); |
376 | 0 | #else |
377 | 0 | return IsNan() && ((AsUint32() & kQuietNanBit) == 0); |
378 | 0 | #endif |
379 | 0 | } |
380 | | |
381 | | |
382 | 0 | bool IsInfinite() const { |
383 | 0 | uint32_t d32 = AsUint32(); |
384 | 0 | return ((d32 & kExponentMask) == kExponentMask) && |
385 | 0 | ((d32 & kSignificandMask) == 0); |
386 | 0 | } |
387 | | |
388 | 0 | int Sign() const { |
389 | 0 | uint32_t d32 = AsUint32(); |
390 | 0 | return (d32 & kSignMask) == 0? 1: -1; |
391 | 0 | } |
392 | | |
393 | | // Computes the two boundaries of this. |
394 | | // The bigger boundary (m_plus) is normalized. The lower boundary has the same |
395 | | // exponent as m_plus. |
396 | | // Precondition: the value encoded by this Single must be greater than 0. |
397 | 0 | void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { |
398 | 0 | DOUBLE_CONVERSION_ASSERT(value() > 0.0); |
399 | 0 | DiyFp v = this->AsDiyFp(); |
400 | 0 | DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); |
401 | 0 | DiyFp m_minus; |
402 | 0 | if (LowerBoundaryIsCloser()) { |
403 | 0 | m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); |
404 | 0 | } else { |
405 | 0 | m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); |
406 | 0 | } |
407 | 0 | m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); |
408 | 0 | m_minus.set_e(m_plus.e()); |
409 | 0 | *out_m_plus = m_plus; |
410 | 0 | *out_m_minus = m_minus; |
411 | 0 | } |
412 | | |
413 | | // Precondition: the value encoded by this Single must be greater or equal |
414 | | // than +0.0. |
415 | 0 | DiyFp UpperBoundary() const { |
416 | 0 | DOUBLE_CONVERSION_ASSERT(Sign() > 0); |
417 | 0 | return DiyFp(Significand() * 2 + 1, Exponent() - 1); |
418 | 0 | } |
419 | | |
420 | 0 | bool LowerBoundaryIsCloser() const { |
421 | | // The boundary is closer if the significand is of the form f == 2^p-1 then |
422 | | // the lower boundary is closer. |
423 | | // Think of v = 1000e10 and v- = 9999e9. |
424 | | // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but |
425 | | // at a distance of 1e8. |
426 | | // The only exception is for the smallest normal: the largest denormal is |
427 | | // at the same distance as its successor. |
428 | | // Note: denormals have the same exponent as the smallest normals. |
429 | 0 | bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0); |
430 | 0 | return physical_significand_is_zero && (Exponent() != kDenormalExponent); |
431 | 0 | } |
432 | | |
433 | 0 | float value() const { return uint32_to_float(d32_); } |
434 | | |
435 | 0 | static float Infinity() { |
436 | 0 | return Single(kInfinity).value(); |
437 | 0 | } |
438 | | |
439 | 0 | static float NaN() { |
440 | 0 | return Single(kNaN).value(); |
441 | 0 | } |
442 | | |
443 | | private: |
444 | | static const int kExponentBias = 0x7F + kPhysicalSignificandSize; |
445 | | static const int kDenormalExponent = -kExponentBias + 1; |
446 | | static const int kMaxExponent = 0xFF - kExponentBias; |
447 | | static const uint32_t kInfinity = 0x7F800000; |
448 | | #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__) |
449 | | static const uint32_t kNaN = 0x7FBFFFFF; |
450 | | #else |
451 | | static const uint32_t kNaN = 0x7FC00000; |
452 | | #endif |
453 | | |
454 | | const uint32_t d32_; |
455 | | |
456 | | DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single); |
457 | | }; |
458 | | |
459 | | } // namespace double_conversion |
460 | | |
461 | | // ICU PATCH: Close ICU namespace |
462 | | U_NAMESPACE_END |
463 | | |
464 | | #endif // DOUBLE_CONVERSION_DOUBLE_H_ |
465 | | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING |