DiyFp.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace Jint.Native.Number.Dtoa
  32. {
  33. // This "Do It Yourself Floating Point" class implements a floating-point number
  34. // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
  35. // have the most significant bit of the significand set.
  36. // Multiplication and Subtraction do not normalize their results.
  37. // DiyFp are not designed to contain special doubles (NaN and Infinity).
  38. internal readonly struct DiyFp
  39. {
  40. internal const int KSignificandSize = 64;
  41. private const ulong KUint64MSB = 0x8000000000000000L;
  42. internal DiyFp(ulong f, int e)
  43. {
  44. F = f;
  45. E = e;
  46. }
  47. public readonly ulong F;
  48. public readonly int E;
  49. // Returns a - b.
  50. // The exponents of both numbers must be the same and this must be bigger
  51. // than other. The result will not be normalized.
  52. internal static DiyFp Minus(in DiyFp a, in DiyFp b)
  53. {
  54. Debug.Assert(a.E == b.E);
  55. return new DiyFp(a.F - b.F, a.E);
  56. }
  57. // this = this * other.
  58. // returns a * b;
  59. internal static DiyFp Times(in DiyFp a, in DiyFp b)
  60. {
  61. // Simply "emulates" a 128 bit multiplication.
  62. // However: the resulting number only contains 64 bits. The least
  63. // significant 64 bits are only used for rounding the most significant 64
  64. // bits.
  65. const ulong kM32 = 0xFFFFFFFFL;
  66. ulong a1 = a.F >> 32;
  67. ulong b1 = a.F & kM32;
  68. ulong c = b.F >> 32;
  69. ulong d = b.F & kM32;
  70. ulong ac = a1*c;
  71. ulong bc = b1*c;
  72. ulong ad = a1*d;
  73. ulong bd = b1*d;
  74. ulong tmp = (bd >> 32) + (ad & kM32) + (bc & kM32);
  75. // By adding 1U << 31 to tmp we round the final result.
  76. // Halfway cases will be round up.
  77. tmp += 1L << 31;
  78. ulong resultF = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
  79. return new DiyFp(resultF, a.E + b.E + 64);
  80. }
  81. internal static DiyFp Normalize(ulong f, int e)
  82. {
  83. // This method is mainly called for normalizing boundaries. In general
  84. // boundaries need to be shifted by 10 bits. We thus optimize for this case.
  85. const ulong k10MsBits = (ulong) 0x3FF << 54;
  86. while ((f & k10MsBits) == 0)
  87. {
  88. f <<= 10;
  89. e -= 10;
  90. }
  91. while ((f & KUint64MSB) == 0)
  92. {
  93. f <<= 1;
  94. e--;
  95. }
  96. return new DiyFp(f, e);
  97. }
  98. public override string ToString()
  99. {
  100. return "[DiyFp f:" + F + ", e:" + E + "]";
  101. }
  102. }
  103. }