double-conversion-ieee.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
  33. #include "unicode/utypes.h"
  34. #if !UCONFIG_NO_FORMATTING
  35. #ifndef DOUBLE_CONVERSION_DOUBLE_H_
  36. #define DOUBLE_CONVERSION_DOUBLE_H_
  37. // ICU PATCH: Customize header file paths for ICU.
  38. #include "double-conversion-diy-fp.h"
  39. // ICU PATCH: Wrap in ICU namespace
  40. U_NAMESPACE_BEGIN
  41. namespace double_conversion {
  42. // We assume that doubles and uint64_t have the same endianness.
  43. static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
  44. static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
  45. static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
  46. static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
  47. // Helper functions for doubles.
  48. class Double {
  49. public:
  50. static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
  51. static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
  52. static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
  53. static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
  54. static const uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000);
  55. static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
  56. static const int kSignificandSize = 53;
  57. static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
  58. static const int kMaxExponent = 0x7FF - kExponentBias;
  59. Double() : d64_(0) {}
  60. explicit Double(double d) : d64_(double_to_uint64(d)) {}
  61. explicit Double(uint64_t d64) : d64_(d64) {}
  62. explicit Double(DiyFp diy_fp)
  63. : d64_(DiyFpToUint64(diy_fp)) {}
  64. // The value encoded by this Double must be greater or equal to +0.0.
  65. // It must not be special (infinity, or NaN).
  66. DiyFp AsDiyFp() const {
  67. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  68. DOUBLE_CONVERSION_ASSERT(!IsSpecial());
  69. return DiyFp(Significand(), Exponent());
  70. }
  71. // The value encoded by this Double must be strictly greater than 0.
  72. DiyFp AsNormalizedDiyFp() const {
  73. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  74. uint64_t f = Significand();
  75. int e = Exponent();
  76. // The current double could be a denormal.
  77. while ((f & kHiddenBit) == 0) {
  78. f <<= 1;
  79. e--;
  80. }
  81. // Do the final shifts in one go.
  82. f <<= DiyFp::kSignificandSize - kSignificandSize;
  83. e -= DiyFp::kSignificandSize - kSignificandSize;
  84. return DiyFp(f, e);
  85. }
  86. // Returns the double's bit as uint64.
  87. uint64_t AsUint64() const {
  88. return d64_;
  89. }
  90. // Returns the next greater double. Returns +infinity on input +infinity.
  91. double NextDouble() const {
  92. if (d64_ == kInfinity) return Double(kInfinity).value();
  93. if (Sign() < 0 && Significand() == 0) {
  94. // -0.0
  95. return 0.0;
  96. }
  97. if (Sign() < 0) {
  98. return Double(d64_ - 1).value();
  99. } else {
  100. return Double(d64_ + 1).value();
  101. }
  102. }
  103. double PreviousDouble() const {
  104. if (d64_ == (kInfinity | kSignMask)) return -Infinity();
  105. if (Sign() < 0) {
  106. return Double(d64_ + 1).value();
  107. } else {
  108. if (Significand() == 0) return -0.0;
  109. return Double(d64_ - 1).value();
  110. }
  111. }
  112. int Exponent() const {
  113. if (IsDenormal()) return kDenormalExponent;
  114. uint64_t d64 = AsUint64();
  115. int biased_e =
  116. static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
  117. return biased_e - kExponentBias;
  118. }
  119. uint64_t Significand() const {
  120. uint64_t d64 = AsUint64();
  121. uint64_t significand = d64 & kSignificandMask;
  122. if (!IsDenormal()) {
  123. return significand + kHiddenBit;
  124. } else {
  125. return significand;
  126. }
  127. }
  128. // Returns true if the double is a denormal.
  129. bool IsDenormal() const {
  130. uint64_t d64 = AsUint64();
  131. return (d64 & kExponentMask) == 0;
  132. }
  133. // We consider denormals not to be special.
  134. // Hence only Infinity and NaN are special.
  135. bool IsSpecial() const {
  136. uint64_t d64 = AsUint64();
  137. return (d64 & kExponentMask) == kExponentMask;
  138. }
  139. bool IsNan() const {
  140. uint64_t d64 = AsUint64();
  141. return ((d64 & kExponentMask) == kExponentMask) &&
  142. ((d64 & kSignificandMask) != 0);
  143. }
  144. bool IsQuietNan() const {
  145. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  146. return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
  147. #else
  148. return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
  149. #endif
  150. }
  151. bool IsSignalingNan() const {
  152. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  153. return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
  154. #else
  155. return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
  156. #endif
  157. }
  158. bool IsInfinite() const {
  159. uint64_t d64 = AsUint64();
  160. return ((d64 & kExponentMask) == kExponentMask) &&
  161. ((d64 & kSignificandMask) == 0);
  162. }
  163. int Sign() const {
  164. uint64_t d64 = AsUint64();
  165. return (d64 & kSignMask) == 0? 1: -1;
  166. }
  167. // Precondition: the value encoded by this Double must be greater or equal
  168. // than +0.0.
  169. DiyFp UpperBoundary() const {
  170. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  171. return DiyFp(Significand() * 2 + 1, Exponent() - 1);
  172. }
  173. // Computes the two boundaries of this.
  174. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  175. // exponent as m_plus.
  176. // Precondition: the value encoded by this Double must be greater than 0.
  177. void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
  178. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  179. DiyFp v = this->AsDiyFp();
  180. DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
  181. DiyFp m_minus;
  182. if (LowerBoundaryIsCloser()) {
  183. m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
  184. } else {
  185. m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
  186. }
  187. m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
  188. m_minus.set_e(m_plus.e());
  189. *out_m_plus = m_plus;
  190. *out_m_minus = m_minus;
  191. }
  192. bool LowerBoundaryIsCloser() const {
  193. // The boundary is closer if the significand is of the form f == 2^p-1 then
  194. // the lower boundary is closer.
  195. // Think of v = 1000e10 and v- = 9999e9.
  196. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  197. // at a distance of 1e8.
  198. // The only exception is for the smallest normal: the largest denormal is
  199. // at the same distance as its successor.
  200. // Note: denormals have the same exponent as the smallest normals.
  201. bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
  202. return physical_significand_is_zero && (Exponent() != kDenormalExponent);
  203. }
  204. double value() const { return uint64_to_double(d64_); }
  205. // Returns the significand size for a given order of magnitude.
  206. // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
  207. // This function returns the number of significant binary digits v will have
  208. // once it's encoded into a double. In almost all cases this is equal to
  209. // kSignificandSize. The only exceptions are denormals. They start with
  210. // leading zeroes and their effective significand-size is hence smaller.
  211. static int SignificandSizeForOrderOfMagnitude(int order) {
  212. if (order >= (kDenormalExponent + kSignificandSize)) {
  213. return kSignificandSize;
  214. }
  215. if (order <= kDenormalExponent) return 0;
  216. return order - kDenormalExponent;
  217. }
  218. static double Infinity() {
  219. return Double(kInfinity).value();
  220. }
  221. static double NaN() {
  222. return Double(kNaN).value();
  223. }
  224. private:
  225. static const int kDenormalExponent = -kExponentBias + 1;
  226. static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
  227. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  228. static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF7FFFF, FFFFFFFF);
  229. #else
  230. static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
  231. #endif
  232. const uint64_t d64_;
  233. static uint64_t DiyFpToUint64(DiyFp diy_fp) {
  234. uint64_t significand = diy_fp.f();
  235. int exponent = diy_fp.e();
  236. while (significand > kHiddenBit + kSignificandMask) {
  237. significand >>= 1;
  238. exponent++;
  239. }
  240. if (exponent >= kMaxExponent) {
  241. return kInfinity;
  242. }
  243. if (exponent < kDenormalExponent) {
  244. return 0;
  245. }
  246. while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
  247. significand <<= 1;
  248. exponent--;
  249. }
  250. uint64_t biased_exponent;
  251. if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
  252. biased_exponent = 0;
  253. } else {
  254. biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
  255. }
  256. return (significand & kSignificandMask) |
  257. (biased_exponent << kPhysicalSignificandSize);
  258. }
  259. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
  260. };
  261. class Single {
  262. public:
  263. static const uint32_t kSignMask = 0x80000000;
  264. static const uint32_t kExponentMask = 0x7F800000;
  265. static const uint32_t kSignificandMask = 0x007FFFFF;
  266. static const uint32_t kHiddenBit = 0x00800000;
  267. static const uint32_t kQuietNanBit = 0x00400000;
  268. static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit.
  269. static const int kSignificandSize = 24;
  270. Single() : d32_(0) {}
  271. explicit Single(float f) : d32_(float_to_uint32(f)) {}
  272. explicit Single(uint32_t d32) : d32_(d32) {}
  273. // The value encoded by this Single must be greater or equal to +0.0.
  274. // It must not be special (infinity, or NaN).
  275. DiyFp AsDiyFp() const {
  276. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  277. DOUBLE_CONVERSION_ASSERT(!IsSpecial());
  278. return DiyFp(Significand(), Exponent());
  279. }
  280. // Returns the single's bit as uint64.
  281. uint32_t AsUint32() const {
  282. return d32_;
  283. }
  284. int Exponent() const {
  285. if (IsDenormal()) return kDenormalExponent;
  286. uint32_t d32 = AsUint32();
  287. int biased_e =
  288. static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
  289. return biased_e - kExponentBias;
  290. }
  291. uint32_t Significand() const {
  292. uint32_t d32 = AsUint32();
  293. uint32_t significand = d32 & kSignificandMask;
  294. if (!IsDenormal()) {
  295. return significand + kHiddenBit;
  296. } else {
  297. return significand;
  298. }
  299. }
  300. // Returns true if the single is a denormal.
  301. bool IsDenormal() const {
  302. uint32_t d32 = AsUint32();
  303. return (d32 & kExponentMask) == 0;
  304. }
  305. // We consider denormals not to be special.
  306. // Hence only Infinity and NaN are special.
  307. bool IsSpecial() const {
  308. uint32_t d32 = AsUint32();
  309. return (d32 & kExponentMask) == kExponentMask;
  310. }
  311. bool IsNan() const {
  312. uint32_t d32 = AsUint32();
  313. return ((d32 & kExponentMask) == kExponentMask) &&
  314. ((d32 & kSignificandMask) != 0);
  315. }
  316. bool IsQuietNan() const {
  317. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  318. return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
  319. #else
  320. return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
  321. #endif
  322. }
  323. bool IsSignalingNan() const {
  324. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  325. return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
  326. #else
  327. return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
  328. #endif
  329. }
  330. bool IsInfinite() const {
  331. uint32_t d32 = AsUint32();
  332. return ((d32 & kExponentMask) == kExponentMask) &&
  333. ((d32 & kSignificandMask) == 0);
  334. }
  335. int Sign() const {
  336. uint32_t d32 = AsUint32();
  337. return (d32 & kSignMask) == 0? 1: -1;
  338. }
  339. // Computes the two boundaries of this.
  340. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  341. // exponent as m_plus.
  342. // Precondition: the value encoded by this Single must be greater than 0.
  343. void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
  344. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  345. DiyFp v = this->AsDiyFp();
  346. DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
  347. DiyFp m_minus;
  348. if (LowerBoundaryIsCloser()) {
  349. m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
  350. } else {
  351. m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
  352. }
  353. m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
  354. m_minus.set_e(m_plus.e());
  355. *out_m_plus = m_plus;
  356. *out_m_minus = m_minus;
  357. }
  358. // Precondition: the value encoded by this Single must be greater or equal
  359. // than +0.0.
  360. DiyFp UpperBoundary() const {
  361. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  362. return DiyFp(Significand() * 2 + 1, Exponent() - 1);
  363. }
  364. bool LowerBoundaryIsCloser() const {
  365. // The boundary is closer if the significand is of the form f == 2^p-1 then
  366. // the lower boundary is closer.
  367. // Think of v = 1000e10 and v- = 9999e9.
  368. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  369. // at a distance of 1e8.
  370. // The only exception is for the smallest normal: the largest denormal is
  371. // at the same distance as its successor.
  372. // Note: denormals have the same exponent as the smallest normals.
  373. bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
  374. return physical_significand_is_zero && (Exponent() != kDenormalExponent);
  375. }
  376. float value() const { return uint32_to_float(d32_); }
  377. static float Infinity() {
  378. return Single(kInfinity).value();
  379. }
  380. static float NaN() {
  381. return Single(kNaN).value();
  382. }
  383. private:
  384. static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
  385. static const int kDenormalExponent = -kExponentBias + 1;
  386. static const int kMaxExponent = 0xFF - kExponentBias;
  387. static const uint32_t kInfinity = 0x7F800000;
  388. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  389. static const uint32_t kNaN = 0x7FBFFFFF;
  390. #else
  391. static const uint32_t kNaN = 0x7FC00000;
  392. #endif
  393. const uint32_t d32_;
  394. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
  395. };
  396. } // namespace double_conversion
  397. // ICU PATCH: Close ICU namespace
  398. U_NAMESPACE_END
  399. #endif // DOUBLE_CONVERSION_DOUBLE_H_
  400. #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING