DiyFp.cs 4.5 KB

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