DoubleHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /// <summary>
  34. /// Helper functions for doubles.
  35. /// </summary>
  36. internal sealed class DoubleHelper
  37. {
  38. internal const ulong KExponentMask = 0x7FF0000000000000L;
  39. internal const ulong KSignificandMask = 0x000FFFFFFFFFFFFFL;
  40. private const ulong KHiddenBit = 0x0010000000000000L;
  41. private static DiyFp AsDiyFp(ulong d64)
  42. {
  43. Debug.Assert(!IsSpecial(d64));
  44. return new DiyFp(Significand(d64), Exponent(d64));
  45. }
  46. // this->Significand() must not be 0.
  47. internal static DiyFp AsNormalizedDiyFp(ulong d64)
  48. {
  49. ulong f = Significand(d64);
  50. int e = Exponent(d64);
  51. Debug.Assert(f != 0);
  52. // The current double could be a denormal.
  53. while ((f & KHiddenBit) == 0)
  54. {
  55. f <<= 1;
  56. e--;
  57. }
  58. // Do the final shifts in one go. Don't forget the hidden bit (the '-1').
  59. f <<= DiyFp.KSignificandSize - KSignificandSize - 1;
  60. e -= DiyFp.KSignificandSize - KSignificandSize - 1;
  61. return new DiyFp(f, e);
  62. }
  63. internal static int Exponent(ulong d64)
  64. {
  65. if (IsDenormal(d64)) return KDenormalExponent;
  66. int biasedE = (int) ((d64 & KExponentMask).UnsignedShift(KSignificandSize) & 0xffffffffL);
  67. return biasedE - KExponentBias;
  68. }
  69. internal static int NormalizedExponent(ulong significand, int exponent)
  70. {
  71. Debug.Assert(significand != 0);
  72. while ((significand & KHiddenBit) == 0)
  73. {
  74. significand = significand << 1;
  75. exponent = exponent - 1;
  76. }
  77. return exponent;
  78. }
  79. internal static ulong Significand(ulong d64)
  80. {
  81. ulong significand = d64 & KSignificandMask;
  82. if (!IsDenormal(d64))
  83. {
  84. return significand + KHiddenBit;
  85. }
  86. return significand;
  87. }
  88. // Returns true if the double is a denormal.
  89. private static bool IsDenormal(ulong d64)
  90. {
  91. return (d64 & KExponentMask) == 0L;
  92. }
  93. // We consider denormals not to be special.
  94. // Hence only Infinity and NaN are special.
  95. private static bool IsSpecial(ulong d64)
  96. {
  97. return (d64 & KExponentMask) == KExponentMask;
  98. }
  99. [StructLayout(LayoutKind.Auto)]
  100. internal readonly struct NormalizedBoundariesResult
  101. {
  102. public NormalizedBoundariesResult(DiyFp minus, DiyFp plus)
  103. {
  104. Minus = minus;
  105. Plus = plus;
  106. }
  107. internal readonly DiyFp Minus;
  108. internal readonly DiyFp Plus;
  109. }
  110. // Returns the two boundaries of first argument.
  111. // The bigger boundary (m_plus) is normalized. The lower boundary has the same
  112. // exponent as m_plus.
  113. internal static NormalizedBoundariesResult NormalizedBoundaries(ulong d64)
  114. {
  115. DiyFp v = AsDiyFp(d64);
  116. bool significandIsZero = (v.F == KHiddenBit);
  117. var mPlus = DiyFp.Normalize((v.F << 1) + 1, v.E - 1);
  118. DiyFp mMinus;
  119. if (significandIsZero && v.E != KDenormalExponent)
  120. {
  121. // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
  122. // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
  123. // at a distance of 1e8.
  124. // The only exception is for the smallest normal: the largest denormal is
  125. // at the same distance as its successor.
  126. // Note: denormals have the same exponent as the smallest normals.
  127. mMinus = new DiyFp((v.F << 2) - 1, v.E - 2);
  128. }
  129. else
  130. {
  131. mMinus = new DiyFp((v.F << 1) - 1, v.E - 1);
  132. }
  133. mMinus = new DiyFp(mMinus.F << (mMinus.E - mPlus.E), mPlus.E);
  134. return new NormalizedBoundariesResult(mMinus, mPlus);
  135. }
  136. private const int KSignificandSize = 52; // Excludes the hidden bit.
  137. private const int KExponentBias = 0x3FF + KSignificandSize;
  138. private const int KDenormalExponent = -KExponentBias + 1;
  139. }