DoubleHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #nullable disable
  2. // Copyright 2010 the V8 project authors. All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following
  11. // disclaimer in the documentation and/or other materials provided
  12. // with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived
  15. // from this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. // Ported to Java from Mozilla's version of V8-dtoa by Hannes Wallnoefer.
  29. // The original revision was 67d1049b0bf9 from the mozilla-central tree.
  30. using System.Diagnostics;
  31. using System.Runtime.InteropServices;
  32. namespace Jint.Native.Number.Dtoa
  33. {
  34. /// <summary>
  35. /// Helper functions for doubles.
  36. /// </summary>
  37. internal sealed class DoubleHelper
  38. {
  39. internal const ulong KExponentMask = 0x7FF0000000000000L;
  40. internal const ulong KSignificandMask = 0x000FFFFFFFFFFFFFL;
  41. private const ulong KHiddenBit = 0x0010000000000000L;
  42. private static DiyFp AsDiyFp(ulong d64)
  43. {
  44. Debug.Assert(!IsSpecial(d64));
  45. return new DiyFp(Significand(d64), Exponent(d64));
  46. }
  47. // this->Significand() must not be 0.
  48. internal static DiyFp AsNormalizedDiyFp(ulong d64)
  49. {
  50. ulong f = Significand(d64);
  51. int e = Exponent(d64);
  52. Debug.Assert(f != 0);
  53. // The current double could be a denormal.
  54. while ((f & KHiddenBit) == 0)
  55. {
  56. f <<= 1;
  57. e--;
  58. }
  59. // Do the final shifts in one go. Don't forget the hidden bit (the '-1').
  60. f <<= DiyFp.KSignificandSize - KSignificandSize - 1;
  61. e -= DiyFp.KSignificandSize - KSignificandSize - 1;
  62. return new DiyFp(f, e);
  63. }
  64. internal static int Exponent(ulong d64)
  65. {
  66. if (IsDenormal(d64)) return KDenormalExponent;
  67. int biasedE = (int) ((d64 & KExponentMask).UnsignedShift(KSignificandSize) & 0xffffffffL);
  68. return biasedE - KExponentBias;
  69. }
  70. internal static int NormalizedExponent(ulong significand, int exponent)
  71. {
  72. Debug.Assert(significand != 0);
  73. while ((significand & KHiddenBit) == 0)
  74. {
  75. significand = significand << 1;
  76. exponent = exponent - 1;
  77. }
  78. return exponent;
  79. }
  80. internal static ulong Significand(ulong d64)
  81. {
  82. ulong significand = d64 & KSignificandMask;
  83. if (!IsDenormal(d64))
  84. {
  85. return significand + KHiddenBit;
  86. }
  87. return significand;
  88. }
  89. // Returns true if the double is a denormal.
  90. private static bool IsDenormal(ulong d64)
  91. {
  92. return (d64 & KExponentMask) == 0L;
  93. }
  94. // We consider denormals not to be special.
  95. // Hence only Infinity and NaN are special.
  96. private static bool IsSpecial(ulong d64)
  97. {
  98. return (d64 & KExponentMask) == KExponentMask;
  99. }
  100. [StructLayout(LayoutKind.Auto)]
  101. internal readonly struct NormalizedBoundariesResult
  102. {
  103. public NormalizedBoundariesResult(DiyFp minus, DiyFp plus)
  104. {
  105. Minus = minus;
  106. Plus = plus;
  107. }
  108. internal readonly DiyFp Minus;
  109. internal readonly DiyFp Plus;
  110. }
  111. // Returns the two boundaries of first argument.
  112. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  113. // exponent as m_plus.
  114. internal static NormalizedBoundariesResult NormalizedBoundaries(ulong d64)
  115. {
  116. DiyFp v = AsDiyFp(d64);
  117. bool significandIsZero = (v.F == KHiddenBit);
  118. var mPlus = DiyFp.Normalize((v.F << 1) + 1, v.E - 1);
  119. DiyFp mMinus;
  120. if (significandIsZero && v.E != KDenormalExponent)
  121. {
  122. // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
  123. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  124. // at a distance of 1e8.
  125. // The only exception is for the smallest normal: the largest denormal is
  126. // at the same distance as its successor.
  127. // Note: denormals have the same exponent as the smallest normals.
  128. mMinus = new DiyFp((v.F << 2) - 1, v.E - 2);
  129. }
  130. else
  131. {
  132. mMinus = new DiyFp((v.F << 1) - 1, v.E - 1);
  133. }
  134. mMinus = new DiyFp(mMinus.F << (mMinus.E - mPlus.E), mPlus.E);
  135. return new NormalizedBoundariesResult(mMinus, mPlus);
  136. }
  137. private const int KSignificandSize = 52; // Excludes the hidden bit.
  138. private const int KExponentBias = 0x3FF + KSignificandSize;
  139. private const int KDenormalExponent = -KExponentBias + 1;
  140. }
  141. }