ieee.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Copyright 2012 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #ifndef DOUBLE_CONVERSION_DOUBLE_H_
  28. #define DOUBLE_CONVERSION_DOUBLE_H_
  29. #include "diy-fp.h"
  30. namespace double_conversion {
  31. // We assume that doubles and uint64_t have the same endianness.
  32. static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
  33. static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
  34. static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
  35. static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
  36. // Helper functions for doubles.
  37. class Double {
  38. public:
  39. static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
  40. static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
  41. static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
  42. static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
  43. static const uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000);
  44. static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
  45. static const int kSignificandSize = 53;
  46. static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
  47. static const int kMaxExponent = 0x7FF - kExponentBias;
  48. Double() : d64_(0) {}
  49. explicit Double(double d) : d64_(double_to_uint64(d)) {}
  50. explicit Double(uint64_t d64) : d64_(d64) {}
  51. explicit Double(DiyFp diy_fp)
  52. : d64_(DiyFpToUint64(diy_fp)) {}
  53. // The value encoded by this Double must be greater or equal to +0.0.
  54. // It must not be special (infinity, or NaN).
  55. DiyFp AsDiyFp() const {
  56. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  57. DOUBLE_CONVERSION_ASSERT(!IsSpecial());
  58. return DiyFp(Significand(), Exponent());
  59. }
  60. // The value encoded by this Double must be strictly greater than 0.
  61. DiyFp AsNormalizedDiyFp() const {
  62. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  63. uint64_t f = Significand();
  64. int e = Exponent();
  65. // The current double could be a denormal.
  66. while ((f & kHiddenBit) == 0) {
  67. f <<= 1;
  68. e--;
  69. }
  70. // Do the final shifts in one go.
  71. f <<= DiyFp::kSignificandSize - kSignificandSize;
  72. e -= DiyFp::kSignificandSize - kSignificandSize;
  73. return DiyFp(f, e);
  74. }
  75. // Returns the double's bit as uint64.
  76. uint64_t AsUint64() const {
  77. return d64_;
  78. }
  79. // Returns the next greater double. Returns +infinity on input +infinity.
  80. double NextDouble() const {
  81. if (d64_ == kInfinity) return Double(kInfinity).value();
  82. if (Sign() < 0 && Significand() == 0) {
  83. // -0.0
  84. return 0.0;
  85. }
  86. if (Sign() < 0) {
  87. return Double(d64_ - 1).value();
  88. } else {
  89. return Double(d64_ + 1).value();
  90. }
  91. }
  92. double PreviousDouble() const {
  93. if (d64_ == (kInfinity | kSignMask)) return -Infinity();
  94. if (Sign() < 0) {
  95. return Double(d64_ + 1).value();
  96. } else {
  97. if (Significand() == 0) return -0.0;
  98. return Double(d64_ - 1).value();
  99. }
  100. }
  101. int Exponent() const {
  102. if (IsDenormal()) return kDenormalExponent;
  103. uint64_t d64 = AsUint64();
  104. int biased_e =
  105. static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
  106. return biased_e - kExponentBias;
  107. }
  108. uint64_t Significand() const {
  109. uint64_t d64 = AsUint64();
  110. uint64_t significand = d64 & kSignificandMask;
  111. if (!IsDenormal()) {
  112. return significand + kHiddenBit;
  113. } else {
  114. return significand;
  115. }
  116. }
  117. // Returns true if the double is a denormal.
  118. bool IsDenormal() const {
  119. uint64_t d64 = AsUint64();
  120. return (d64 & kExponentMask) == 0;
  121. }
  122. // We consider denormals not to be special.
  123. // Hence only Infinity and NaN are special.
  124. bool IsSpecial() const {
  125. uint64_t d64 = AsUint64();
  126. return (d64 & kExponentMask) == kExponentMask;
  127. }
  128. bool IsNan() const {
  129. uint64_t d64 = AsUint64();
  130. return ((d64 & kExponentMask) == kExponentMask) &&
  131. ((d64 & kSignificandMask) != 0);
  132. }
  133. bool IsQuietNan() const {
  134. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  135. return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
  136. #else
  137. return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
  138. #endif
  139. }
  140. bool IsSignalingNan() const {
  141. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  142. return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
  143. #else
  144. return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
  145. #endif
  146. }
  147. bool IsInfinite() const {
  148. uint64_t d64 = AsUint64();
  149. return ((d64 & kExponentMask) == kExponentMask) &&
  150. ((d64 & kSignificandMask) == 0);
  151. }
  152. int Sign() const {
  153. uint64_t d64 = AsUint64();
  154. return (d64 & kSignMask) == 0? 1: -1;
  155. }
  156. // Precondition: the value encoded by this Double must be greater or equal
  157. // than +0.0.
  158. DiyFp UpperBoundary() const {
  159. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  160. return DiyFp(Significand() * 2 + 1, Exponent() - 1);
  161. }
  162. // Computes the two boundaries of this.
  163. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  164. // exponent as m_plus.
  165. // Precondition: the value encoded by this Double must be greater than 0.
  166. void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
  167. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  168. DiyFp v = this->AsDiyFp();
  169. DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
  170. DiyFp m_minus;
  171. if (LowerBoundaryIsCloser()) {
  172. m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
  173. } else {
  174. m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
  175. }
  176. m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
  177. m_minus.set_e(m_plus.e());
  178. *out_m_plus = m_plus;
  179. *out_m_minus = m_minus;
  180. }
  181. bool LowerBoundaryIsCloser() const {
  182. // The boundary is closer if the significand is of the form f == 2^p-1 then
  183. // the lower boundary is closer.
  184. // Think of v = 1000e10 and v- = 9999e9.
  185. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  186. // at a distance of 1e8.
  187. // The only exception is for the smallest normal: the largest denormal is
  188. // at the same distance as its successor.
  189. // Note: denormals have the same exponent as the smallest normals.
  190. bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
  191. return physical_significand_is_zero && (Exponent() != kDenormalExponent);
  192. }
  193. double value() const { return uint64_to_double(d64_); }
  194. // Returns the significand size for a given order of magnitude.
  195. // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
  196. // This function returns the number of significant binary digits v will have
  197. // once it's encoded into a double. In almost all cases this is equal to
  198. // kSignificandSize. The only exceptions are denormals. They start with
  199. // leading zeroes and their effective significand-size is hence smaller.
  200. static int SignificandSizeForOrderOfMagnitude(int order) {
  201. if (order >= (kDenormalExponent + kSignificandSize)) {
  202. return kSignificandSize;
  203. }
  204. if (order <= kDenormalExponent) return 0;
  205. return order - kDenormalExponent;
  206. }
  207. static double Infinity() {
  208. return Double(kInfinity).value();
  209. }
  210. static double NaN() {
  211. return Double(kNaN).value();
  212. }
  213. private:
  214. static const int kDenormalExponent = -kExponentBias + 1;
  215. static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
  216. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  217. static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF7FFFF, FFFFFFFF);
  218. #else
  219. static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
  220. #endif
  221. const uint64_t d64_;
  222. static uint64_t DiyFpToUint64(DiyFp diy_fp) {
  223. uint64_t significand = diy_fp.f();
  224. int exponent = diy_fp.e();
  225. while (significand > kHiddenBit + kSignificandMask) {
  226. significand >>= 1;
  227. exponent++;
  228. }
  229. if (exponent >= kMaxExponent) {
  230. return kInfinity;
  231. }
  232. if (exponent < kDenormalExponent) {
  233. return 0;
  234. }
  235. while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
  236. significand <<= 1;
  237. exponent--;
  238. }
  239. uint64_t biased_exponent;
  240. if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
  241. biased_exponent = 0;
  242. } else {
  243. biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
  244. }
  245. return (significand & kSignificandMask) |
  246. (biased_exponent << kPhysicalSignificandSize);
  247. }
  248. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
  249. };
  250. class Single {
  251. public:
  252. static const uint32_t kSignMask = 0x80000000;
  253. static const uint32_t kExponentMask = 0x7F800000;
  254. static const uint32_t kSignificandMask = 0x007FFFFF;
  255. static const uint32_t kHiddenBit = 0x00800000;
  256. static const uint32_t kQuietNanBit = 0x00400000;
  257. static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit.
  258. static const int kSignificandSize = 24;
  259. Single() : d32_(0) {}
  260. explicit Single(float f) : d32_(float_to_uint32(f)) {}
  261. explicit Single(uint32_t d32) : d32_(d32) {}
  262. // The value encoded by this Single must be greater or equal to +0.0.
  263. // It must not be special (infinity, or NaN).
  264. DiyFp AsDiyFp() const {
  265. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  266. DOUBLE_CONVERSION_ASSERT(!IsSpecial());
  267. return DiyFp(Significand(), Exponent());
  268. }
  269. // Returns the single's bit as uint64.
  270. uint32_t AsUint32() const {
  271. return d32_;
  272. }
  273. int Exponent() const {
  274. if (IsDenormal()) return kDenormalExponent;
  275. uint32_t d32 = AsUint32();
  276. int biased_e =
  277. static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
  278. return biased_e - kExponentBias;
  279. }
  280. uint32_t Significand() const {
  281. uint32_t d32 = AsUint32();
  282. uint32_t significand = d32 & kSignificandMask;
  283. if (!IsDenormal()) {
  284. return significand + kHiddenBit;
  285. } else {
  286. return significand;
  287. }
  288. }
  289. // Returns true if the single is a denormal.
  290. bool IsDenormal() const {
  291. uint32_t d32 = AsUint32();
  292. return (d32 & kExponentMask) == 0;
  293. }
  294. // We consider denormals not to be special.
  295. // Hence only Infinity and NaN are special.
  296. bool IsSpecial() const {
  297. uint32_t d32 = AsUint32();
  298. return (d32 & kExponentMask) == kExponentMask;
  299. }
  300. bool IsNan() const {
  301. uint32_t d32 = AsUint32();
  302. return ((d32 & kExponentMask) == kExponentMask) &&
  303. ((d32 & kSignificandMask) != 0);
  304. }
  305. bool IsQuietNan() const {
  306. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  307. return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
  308. #else
  309. return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
  310. #endif
  311. }
  312. bool IsSignalingNan() const {
  313. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  314. return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
  315. #else
  316. return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
  317. #endif
  318. }
  319. bool IsInfinite() const {
  320. uint32_t d32 = AsUint32();
  321. return ((d32 & kExponentMask) == kExponentMask) &&
  322. ((d32 & kSignificandMask) == 0);
  323. }
  324. int Sign() const {
  325. uint32_t d32 = AsUint32();
  326. return (d32 & kSignMask) == 0? 1: -1;
  327. }
  328. // Computes the two boundaries of this.
  329. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  330. // exponent as m_plus.
  331. // Precondition: the value encoded by this Single must be greater than 0.
  332. void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
  333. DOUBLE_CONVERSION_ASSERT(value() > 0.0);
  334. DiyFp v = this->AsDiyFp();
  335. DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
  336. DiyFp m_minus;
  337. if (LowerBoundaryIsCloser()) {
  338. m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
  339. } else {
  340. m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
  341. }
  342. m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
  343. m_minus.set_e(m_plus.e());
  344. *out_m_plus = m_plus;
  345. *out_m_minus = m_minus;
  346. }
  347. // Precondition: the value encoded by this Single must be greater or equal
  348. // than +0.0.
  349. DiyFp UpperBoundary() const {
  350. DOUBLE_CONVERSION_ASSERT(Sign() > 0);
  351. return DiyFp(Significand() * 2 + 1, Exponent() - 1);
  352. }
  353. bool LowerBoundaryIsCloser() const {
  354. // The boundary is closer if the significand is of the form f == 2^p-1 then
  355. // the lower boundary is closer.
  356. // Think of v = 1000e10 and v- = 9999e9.
  357. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  358. // at a distance of 1e8.
  359. // The only exception is for the smallest normal: the largest denormal is
  360. // at the same distance as its successor.
  361. // Note: denormals have the same exponent as the smallest normals.
  362. bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
  363. return physical_significand_is_zero && (Exponent() != kDenormalExponent);
  364. }
  365. float value() const { return uint32_to_float(d32_); }
  366. static float Infinity() {
  367. return Single(kInfinity).value();
  368. }
  369. static float NaN() {
  370. return Single(kNaN).value();
  371. }
  372. private:
  373. static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
  374. static const int kDenormalExponent = -kExponentBias + 1;
  375. static const int kMaxExponent = 0xFF - kExponentBias;
  376. static const uint32_t kInfinity = 0x7F800000;
  377. #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
  378. static const uint32_t kNaN = 0x7FBFFFFF;
  379. #else
  380. static const uint32_t kNaN = 0x7FC00000;
  381. #endif
  382. const uint32_t d32_;
  383. DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
  384. };
  385. } // namespace double_conversion
  386. #endif // DOUBLE_CONVERSION_DOUBLE_H_