double-conversion-ieee.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
  146. }
  147. bool IsSignalingNan() const {
  148. return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
  149. }
  150. bool IsInfinite() const {
  151. uint64_t d64 = AsUint64();
  152. return ((d64 & kExponentMask) == kExponentMask) &&
  153. ((d64 & kSignificandMask) == 0);
  154. }
  155. int Sign() const {
  156. uint64_t d64 = AsUint64();
  157. return (d64 & kSignMask) == 0? 1: -1;
  158. }
  159. // Precondition: the value encoded by this Double must be greater or equal
  160. // than +0.0.
  161. DiyFp UpperBoundary() const {
  162. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  163. return DiyFp(Significand() * 2 + 1, Exponent() - 1);
  164. }
  165. // Computes the two boundaries of this.
  166. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  167. // exponent as m_plus.
  168. // Precondition: the value encoded by this Double must be greater than 0.
  169. void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
  170. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  171. DiyFp v = this->AsDiyFp();
  172. DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
  173. DiyFp m_minus;
  174. if (LowerBoundaryIsCloser()) {
  175. m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
  176. } else {
  177. m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
  178. }
  179. m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
  180. m_minus.set_e(m_plus.e());
  181. *out_m_plus = m_plus;
  182. *out_m_minus = m_minus;
  183. }
  184. bool LowerBoundaryIsCloser() const {
  185. // The boundary is closer if the significand is of the form f == 2^p-1 then
  186. // the lower boundary is closer.
  187. // Think of v = 1000e10 and v- = 9999e9.
  188. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  189. // at a distance of 1e8.
  190. // The only exception is for the smallest normal: the largest denormal is
  191. // at the same distance as its successor.
  192. // Note: denormals have the same exponent as the smallest normals.
  193. bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
  194. return physical_significand_is_zero && (Exponent() != kDenormalExponent);
  195. }
  196. double value() const { return uint64_to_double(d64_); }
  197. // Returns the significand size for a given order of magnitude.
  198. // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
  199. // This function returns the number of significant binary digits v will have
  200. // once it's encoded into a double. In almost all cases this is equal to
  201. // kSignificandSize. The only exceptions are denormals. They start with
  202. // leading zeroes and their effective significand-size is hence smaller.
  203. static int SignificandSizeForOrderOfMagnitude(int order) {
  204. if (order >= (kDenormalExponent + kSignificandSize)) {
  205. return kSignificandSize;
  206. }
  207. if (order <= kDenormalExponent) return 0;
  208. return order - kDenormalExponent;
  209. }
  210. static double Infinity() {
  211. return Double(kInfinity).value();
  212. }
  213. static double NaN() {
  214. return Double(kNaN).value();
  215. }
  216. private:
  217. static const int kDenormalExponent = -kExponentBias + 1;
  218. static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
  219. static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
  220. const uint64_t d64_;
  221. static uint64_t DiyFpToUint64(DiyFp diy_fp) {
  222. uint64_t significand = diy_fp.f();
  223. int exponent = diy_fp.e();
  224. while (significand > kHiddenBit + kSignificandMask) {
  225. significand >>= 1;
  226. exponent++;
  227. }
  228. if (exponent >= kMaxExponent) {
  229. return kInfinity;
  230. }
  231. if (exponent < kDenormalExponent) {
  232. return 0;
  233. }
  234. while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
  235. significand <<= 1;
  236. exponent--;
  237. }
  238. uint64_t biased_exponent;
  239. if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
  240. biased_exponent = 0;
  241. } else {
  242. biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
  243. }
  244. return (significand & kSignificandMask) |
  245. (biased_exponent << kPhysicalSignificandSize);
  246. }
  247. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
  248. };
  249. class Single {
  250. public:
  251. static const uint32_t kSignMask = 0x80000000;
  252. static const uint32_t kExponentMask = 0x7F800000;
  253. static const uint32_t kSignificandMask = 0x007FFFFF;
  254. static const uint32_t kHiddenBit = 0x00800000;
  255. static const uint32_t kQuietNanBit = 0x00400000;
  256. static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit.
  257. static const int kSignificandSize = 24;
  258. Single() : d32_(0) {}
  259. explicit Single(float f) : d32_(float_to_uint32(f)) {}
  260. explicit Single(uint32_t d32) : d32_(d32) {}
  261. // The value encoded by this Single must be greater or equal to +0.0.
  262. // It must not be special (infinity, or NaN).
  263. DiyFp AsDiyFp() const {
  264. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  265. DOUBLE_CONVERSION_ASSERT(!IsSpecial());
  266. return DiyFp(Significand(), Exponent());
  267. }
  268. // Returns the single's bit as uint64.
  269. uint32_t AsUint32() const {
  270. return d32_;
  271. }
  272. int Exponent() const {
  273. if (IsDenormal()) return kDenormalExponent;
  274. uint32_t d32 = AsUint32();
  275. int biased_e =
  276. static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
  277. return biased_e - kExponentBias;
  278. }
  279. uint32_t Significand() const {
  280. uint32_t d32 = AsUint32();
  281. uint32_t significand = d32 & kSignificandMask;
  282. if (!IsDenormal()) {
  283. return significand + kHiddenBit;
  284. } else {
  285. return significand;
  286. }
  287. }
  288. // Returns true if the single is a denormal.
  289. bool IsDenormal() const {
  290. uint32_t d32 = AsUint32();
  291. return (d32 & kExponentMask) == 0;
  292. }
  293. // We consider denormals not to be special.
  294. // Hence only Infinity and NaN are special.
  295. bool IsSpecial() const {
  296. uint32_t d32 = AsUint32();
  297. return (d32 & kExponentMask) == kExponentMask;
  298. }
  299. bool IsNan() const {
  300. uint32_t d32 = AsUint32();
  301. return ((d32 & kExponentMask) == kExponentMask) &&
  302. ((d32 & kSignificandMask) != 0);
  303. }
  304. bool IsQuietNan() const {
  305. return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
  306. }
  307. bool IsSignalingNan() const {
  308. return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
  309. }
  310. bool IsInfinite() const {
  311. uint32_t d32 = AsUint32();
  312. return ((d32 & kExponentMask) == kExponentMask) &&
  313. ((d32 & kSignificandMask) == 0);
  314. }
  315. int Sign() const {
  316. uint32_t d32 = AsUint32();
  317. return (d32 & kSignMask) == 0? 1: -1;
  318. }
  319. // Computes the two boundaries of this.
  320. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  321. // exponent as m_plus.
  322. // Precondition: the value encoded by this Single must be greater than 0.
  323. void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
  324. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  325. DiyFp v = this->AsDiyFp();
  326. DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
  327. DiyFp m_minus;
  328. if (LowerBoundaryIsCloser()) {
  329. m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
  330. } else {
  331. m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
  332. }
  333. m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
  334. m_minus.set_e(m_plus.e());
  335. *out_m_plus = m_plus;
  336. *out_m_minus = m_minus;
  337. }
  338. // Precondition: the value encoded by this Single must be greater or equal
  339. // than +0.0.
  340. DiyFp UpperBoundary() const {
  341. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  342. return DiyFp(Significand() * 2 + 1, Exponent() - 1);
  343. }
  344. bool LowerBoundaryIsCloser() const {
  345. // The boundary is closer if the significand is of the form f == 2^p-1 then
  346. // the lower boundary is closer.
  347. // Think of v = 1000e10 and v- = 9999e9.
  348. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  349. // at a distance of 1e8.
  350. // The only exception is for the smallest normal: the largest denormal is
  351. // at the same distance as its successor.
  352. // Note: denormals have the same exponent as the smallest normals.
  353. bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
  354. return physical_significand_is_zero && (Exponent() != kDenormalExponent);
  355. }
  356. float value() const { return uint32_to_float(d32_); }
  357. static float Infinity() {
  358. return Single(kInfinity).value();
  359. }
  360. static float NaN() {
  361. return Single(kNaN).value();
  362. }
  363. private:
  364. static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
  365. static const int kDenormalExponent = -kExponentBias + 1;
  366. static const int kMaxExponent = 0xFF - kExponentBias;
  367. static const uint32_t kInfinity = 0x7F800000;
  368. static const uint32_t kNaN = 0x7FC00000;
  369. const uint32_t d32_;
  370. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
  371. };
  372. } // namespace double_conversion
  373. // ICU PATCH: Close ICU namespace
  374. U_NAMESPACE_END
  375. #endif // DOUBLE_CONVERSION_DOUBLE_H_
  376. #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING