Number.DiyFp.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. namespace System
  6. {
  7. internal static partial class Number
  8. {
  9. // This is a port of the `DiyFp` implementation here: https://github.com/google/double-conversion/blob/a711666ddd063eb1e4b181a6cb981d39a1fc8bac/double-conversion/diy-fp.h
  10. // The backing structure and how it is used is described in more detail here: http://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf
  11. // This "Do It Yourself Floating Point" class implements a floating-point number with a ulong significand and an int exponent.
  12. // Normalized DiyFp numbers will have the most significant bit of the significand set.
  13. // Multiplication and Subtraction do not normalize their results.
  14. // DiyFp are not designed to contain special doubles (NaN and Infinity).
  15. internal readonly ref struct DiyFp
  16. {
  17. public const int DoubleImplicitBitIndex = 52;
  18. public const int SingleImplicitBitIndex = 23;
  19. public const int SignificandSize = 64;
  20. public readonly ulong f;
  21. public readonly int e;
  22. // Computes the two boundaries of value.
  23. //
  24. // The bigger boundary (mPlus) is normalized.
  25. // The lower boundary has the same exponent as mPlus.
  26. //
  27. // Precondition:
  28. // The value encoded by value must be greater than 0.
  29. public static DiyFp CreateAndGetBoundaries(double value, out DiyFp mMinus, out DiyFp mPlus)
  30. {
  31. var result = new DiyFp(value);
  32. result.GetBoundaries(DoubleImplicitBitIndex, out mMinus, out mPlus);
  33. return result;
  34. }
  35. // Computes the two boundaries of value.
  36. //
  37. // The bigger boundary (mPlus) is normalized.
  38. // The lower boundary has the same exponent as mPlus.
  39. //
  40. // Precondition:
  41. // The value encoded by value must be greater than 0.
  42. public static DiyFp CreateAndGetBoundaries(float value, out DiyFp mMinus, out DiyFp mPlus)
  43. {
  44. var result = new DiyFp(value);
  45. result.GetBoundaries(SingleImplicitBitIndex, out mMinus, out mPlus);
  46. return result;
  47. }
  48. public DiyFp(double value)
  49. {
  50. Debug.Assert(double.IsFinite(value));
  51. Debug.Assert(value > 0.0);
  52. f = ExtractFractionAndBiasedExponent(value, out e);
  53. }
  54. public DiyFp(float value)
  55. {
  56. Debug.Assert(float.IsFinite(value));
  57. Debug.Assert(value > 0.0f);
  58. f = ExtractFractionAndBiasedExponent(value, out e);
  59. }
  60. public DiyFp(ulong f, int e)
  61. {
  62. this.f = f;
  63. this.e = e;
  64. }
  65. public DiyFp Multiply(in DiyFp other)
  66. {
  67. // Simply "emulates" a 128-bit multiplication
  68. //
  69. // However: the resulting number only contains 64-bits. The least
  70. // signficant 64-bits are only used for rounding the most significant
  71. // 64-bits.
  72. uint a = (uint)(f >> 32);
  73. uint b = (uint)(f);
  74. uint c = (uint)(other.f >> 32);
  75. uint d = (uint)(other.f);
  76. ulong ac = ((ulong)(a) * c);
  77. ulong bc = ((ulong)(b) * c);
  78. ulong ad = ((ulong)(a) * d);
  79. ulong bd = ((ulong)(b) * d);
  80. ulong tmp = (bd >> 32) + (uint)(ad) + (uint)(bc);
  81. // By adding (1UL << 31) to tmp, we round the final result.
  82. // Halfway cases will be rounded up.
  83. tmp += (1U << 31);
  84. return new DiyFp((ac + (ad >> 32) + (bc >> 32) + (tmp >> 32)), (e + other.e + SignificandSize));
  85. }
  86. public DiyFp Normalize()
  87. {
  88. // This method is mainly called for normalizing boundaries.
  89. //
  90. // We deviate from the reference implementation by just using
  91. // our LeadingZeroCount function so that we only need to shift
  92. // and subtract once.
  93. Debug.Assert(f != 0);
  94. int lzcnt = BitOps.LeadingZeroCount(f);
  95. return new DiyFp((f << lzcnt), (e - lzcnt));
  96. }
  97. // The exponents of both numbers must be the same.
  98. // The significand of 'this' must be bigger than the significand of 'other'.
  99. // The result will not be normalized.
  100. public DiyFp Subtract(in DiyFp other)
  101. {
  102. Debug.Assert(e == other.e);
  103. Debug.Assert(f >= other.f);
  104. return new DiyFp((f - other.f), e);
  105. }
  106. private void GetBoundaries(int implicitBitIndex, out DiyFp mMinus, out DiyFp mPlus)
  107. {
  108. mPlus = new DiyFp(((f << 1) + 1), (e - 1)).Normalize();
  109. // The boundary is closer if the sigificand is of the form:
  110. // f == 2^p-1
  111. //
  112. // Think of v = 1000e10 and v- = 9999e9
  113. // Then the boundary == (v - v-) / 2 is not just at a distance of 1e9 but at a distance of 1e8.
  114. // The only exception is for the smallest normal, where the largest denormal is at the same distance as its successor.
  115. //
  116. // Note: denormals have the same exponent as the smallest normals.
  117. // We deviate from the reference implementation by just checking if the significand has only the implicit bit set.
  118. // In this scenario, we know that all the explicit bits are 0 and that the unbiased exponent is non-zero.
  119. if (f == (1UL << implicitBitIndex))
  120. {
  121. mMinus = new DiyFp(((f << 2) - 1), (e - 2));
  122. }
  123. else
  124. {
  125. mMinus = new DiyFp(((f << 1) - 1), (e - 1));
  126. }
  127. mMinus = new DiyFp((mMinus.f << (mMinus.e - mPlus.e)), mPlus.e);
  128. }
  129. }
  130. }
  131. }