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