DiyFp.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2010 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. // Ported to Java from Mozilla's version of V8-dtoa by Hannes Wallnoefer.
  28. // The original revision was 67d1049b0bf9 from the mozilla-central tree.
  29. using System.Diagnostics;
  30. namespace Jint.Native.Number.Dtoa
  31. {
  32. // This "Do It Yourself Floating Point" class implements a floating-point number
  33. // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
  34. // have the most significant bit of the significand set.
  35. // Multiplication and Subtraction do not normalize their results.
  36. // DiyFp are not designed to contain special doubles (NaN and Infinity).
  37. internal class DiyFp
  38. {
  39. internal const int KSignificandSize = 64;
  40. private const ulong KUint64MSB = 0x8000000000000000L;
  41. internal DiyFp()
  42. {
  43. F = 0;
  44. E = 0;
  45. }
  46. internal DiyFp(long f, int e)
  47. {
  48. F = f;
  49. E = e;
  50. }
  51. public long F { get; set; }
  52. public int E { get; set; }
  53. private static bool Uint64Gte(long a, long b)
  54. {
  55. // greater-or-equal for unsigned int64 in java-style...
  56. return (a == b) || ((a > b) ^ (a < 0) ^ (b < 0));
  57. }
  58. // this = this - other.
  59. // The exponents of both numbers must be the same and the significand of this
  60. // must be bigger than the significand of other.
  61. // The result will not be normalized.
  62. private void Subtract(DiyFp other)
  63. {
  64. Debug.Assert(E == other.E);
  65. Debug.Assert(Uint64Gte(F, other.F));
  66. F -= other.F;
  67. }
  68. // Returns a - b.
  69. // The exponents of both numbers must be the same and this must be bigger
  70. // than other. The result will not be normalized.
  71. internal static DiyFp Minus(DiyFp a, DiyFp b)
  72. {
  73. DiyFp result = new DiyFp(a.F, a.E);
  74. result.Subtract(b);
  75. return result;
  76. }
  77. // this = this * other.
  78. private void Multiply(DiyFp other)
  79. {
  80. // Simply "emulates" a 128 bit multiplication.
  81. // However: the resulting number only contains 64 bits. The least
  82. // significant 64 bits are only used for rounding the most significant 64
  83. // bits.
  84. const long kM32 = 0xFFFFFFFFL;
  85. long a = F.UnsignedShift(32);
  86. long b = F & kM32;
  87. long c = other.F.UnsignedShift(32);
  88. long d = other.F & kM32;
  89. long ac = a*c;
  90. long bc = b*c;
  91. long ad = a*d;
  92. long bd = b*d;
  93. long tmp = bd.UnsignedShift(32) + (ad & kM32) + (bc & kM32);
  94. // By adding 1U << 31 to tmp we round the final result.
  95. // Halfway cases will be round up.
  96. tmp += 1L << 31;
  97. long resultF = ac + ad.UnsignedShift(32) + bc.UnsignedShift(32) + tmp.UnsignedShift(32);
  98. E += other.E + 64;
  99. F = resultF;
  100. }
  101. // returns a * b;
  102. internal static DiyFp Times(DiyFp a, DiyFp b)
  103. {
  104. DiyFp result = new DiyFp(a.F, a.E);
  105. result.Multiply(b);
  106. return result;
  107. }
  108. internal void Normalize()
  109. {
  110. long f = F;
  111. int e = E;
  112. // This method is mainly called for normalizing boundaries. In general
  113. // boundaries need to be shifted by 10 bits. We thus optimize for this case.
  114. const long k10MsBits = 0xFFC00000L << 32;
  115. while ((f & k10MsBits) == 0)
  116. {
  117. f <<= 10;
  118. e -= 10;
  119. }
  120. while (((ulong) f & KUint64MSB) == 0)
  121. {
  122. f <<= 1;
  123. e--;
  124. }
  125. F = f;
  126. E = e;
  127. }
  128. public override string ToString()
  129. {
  130. return "[DiyFp f:" + F + ", e:" + E + "]";
  131. }
  132. }
  133. }