|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace Jint.Native.Math; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// https://raw.githubusercontent.com/es-shims/Math.sumPrecise/main/sum.js |
| 8 | +/// adapted from https://github.com/tc39/proposal-math-sum/blob/f4286d0a9d8525bda61be486df964bf2527c8789/polyfill/polyfill.mjs |
| 9 | +/// https://www-2.cs.cmu.edu/afs/cs/project/quake/public/papers/robust-arithmetic.ps |
| 10 | +/// Shewchuk's algorithm for exactly floating point addition |
| 11 | +/// as implemented in Python's fsum: https://github.com/python/cpython/blob/48dfd74a9db9d4aa9c6f23b4a67b461e5d977173/Modules/mathmodule.c#L1359-L1474 |
| 12 | +/// adapted to handle overflow via an additional "biased" partial, representing 2**1024 times its actual value |
| 13 | +/// </summary> |
| 14 | +internal static class SumPrecise |
| 15 | +{ |
| 16 | + // exponent 11111111110, significand all 1s |
| 17 | + private const double MaxDouble = 1.79769313486231570815e+308; // i.e. (2**1024 - 2**(1023 - 52)) |
| 18 | + |
| 19 | + // exponent 11111111110, significand all 1s except final 0 |
| 20 | + private const double PenultimateDouble = 1.79769313486231550856e+308; // i.e. (2**1024 - 2 * 2**(1023 - 52)) |
| 21 | + |
| 22 | + private const double Two1023 = 8.98846567431158e+307; // 2 ** 1023 |
| 23 | + |
| 24 | + // exponent 11111001010, significand all 0s |
| 25 | + private const double MaxUlp = MaxDouble - PenultimateDouble; // 1.99584030953471981166e+292, i.e. 2**(1023 - 52) |
| 26 | + |
| 27 | + [StructLayout(LayoutKind.Auto)] |
| 28 | + private readonly record struct TwoSumResult(double Hi, double Lo); |
| 29 | + |
| 30 | + // prerequisite: $abs(x) >= $abs(y) |
| 31 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 32 | + private static TwoSumResult TwoSum(double x, double y) |
| 33 | + { |
| 34 | + var hi = x + y; |
| 35 | + var lo = y - (hi - x); |
| 36 | + return new TwoSumResult(hi, lo); |
| 37 | + } |
| 38 | + |
| 39 | + // preconditions: |
| 40 | + // - array only contains numbers |
| 41 | + // - none of them are -0, NaN, or ±Infinity |
| 42 | + // - all of them are finite |
| 43 | + [MethodImpl(512)] |
| 44 | + internal static double Sum(List<double> array) |
| 45 | + { |
| 46 | + double hi, lo; |
| 47 | + |
| 48 | + List<double> partials = []; |
| 49 | + |
| 50 | + var overflow = 0; // conceptually 2**1024 times this value; the final partial is biased by this amount |
| 51 | + |
| 52 | + var index = -1; |
| 53 | + |
| 54 | + // main loop |
| 55 | + while (index + 1 < array.Count) |
| 56 | + { |
| 57 | + var x = +array[++index]; |
| 58 | + |
| 59 | + var actuallyUsedPartials = 0; |
| 60 | + for (var j = 0; j < partials.Count; j += 1) |
| 61 | + { |
| 62 | + var y = partials[j]; |
| 63 | + |
| 64 | + if (System.Math.Abs(x) < System.Math.Abs(y)) |
| 65 | + { |
| 66 | + var tmp = x; |
| 67 | + x = y; |
| 68 | + y = tmp; |
| 69 | + } |
| 70 | + |
| 71 | + (hi, lo) = TwoSum(x, y); |
| 72 | + |
| 73 | + if (double.IsPositiveInfinity(System.Math.Abs(hi))) |
| 74 | + { |
| 75 | + var sign = double.IsPositiveInfinity(hi) ? 1 : -1; |
| 76 | + overflow += sign; |
| 77 | + |
| 78 | + x = x - sign * Two1023 - sign * Two1023; |
| 79 | + if (System.Math.Abs(x) < System.Math.Abs(y)) |
| 80 | + { |
| 81 | + var tmp2 = x; |
| 82 | + x = y; |
| 83 | + y = tmp2; |
| 84 | + } |
| 85 | + |
| 86 | + var s2 = TwoSum(x, y); |
| 87 | + hi = s2.Hi; |
| 88 | + lo = s2.Lo; |
| 89 | + } |
| 90 | + |
| 91 | + if (lo != 0) |
| 92 | + { |
| 93 | + partials[actuallyUsedPartials] = lo; |
| 94 | + actuallyUsedPartials += 1; |
| 95 | + } |
| 96 | + |
| 97 | + x = hi; |
| 98 | + } |
| 99 | + |
| 100 | + while (partials.Count > actuallyUsedPartials) |
| 101 | + { |
| 102 | + partials.RemoveAt(partials.Count - 1); |
| 103 | + } |
| 104 | + |
| 105 | + if (x != 0) |
| 106 | + { |
| 107 | + partials.Add(x); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // compute the exact sum of partials, stopping once we lose precision |
| 112 | + var n = partials.Count - 1; |
| 113 | + hi = 0; |
| 114 | + lo = 0; |
| 115 | + |
| 116 | + if (overflow != 0) |
| 117 | + { |
| 118 | + var next = n >= 0 ? partials[n] : 0; |
| 119 | + n -= 1; |
| 120 | + if (System.Math.Abs(overflow) > 1 || (overflow > 0 && next > 0) || (overflow < 0 && next < 0)) |
| 121 | + { |
| 122 | + return overflow > 0 ? double.PositiveInfinity : double.NegativeInfinity; |
| 123 | + } |
| 124 | + |
| 125 | + // here we actually have to do the arithmetic |
| 126 | + // drop a factor of 2 so we can do it without overflow |
| 127 | + // assert($abs(overflow) === 1) |
| 128 | + var s = TwoSum(overflow * Two1023, next / 2); |
| 129 | + hi = s.Hi; |
| 130 | + lo = s.Lo; |
| 131 | + lo *= 2; |
| 132 | + if (double.IsPositiveInfinity(System.Math.Abs(2 * hi))) |
| 133 | + { |
| 134 | + // stupid edge case: rounding to the maximum value |
| 135 | + // MAX_DOUBLE has a 1 in the last place of its significand, so if we subtract exactly half a ULP from 2**1024, the result rounds away from it (i.e. to infinity) under ties-to-even |
| 136 | + // but if the next partial has the opposite sign of the current value, we need to round towards MAX_DOUBLE instead |
| 137 | + // this is the same as the "handle rounding" case below, but there's only one potentially-finite case we need to worry about, so we just hardcode that one |
| 138 | + if (hi > 0) |
| 139 | + { |
| 140 | + if (hi == Two1023 && lo == -(MaxUlp / 2) && n >= 0 && partials[n] < 0) |
| 141 | + { |
| 142 | + return MaxDouble; |
| 143 | + } |
| 144 | + |
| 145 | + return double.PositiveInfinity; |
| 146 | + } |
| 147 | + |
| 148 | + if (hi == -Two1023 && lo == (MaxUlp / 2) && n >= 0 && partials[n] > 0) |
| 149 | + { |
| 150 | + return -MaxDouble; |
| 151 | + } |
| 152 | + |
| 153 | + return double.NegativeInfinity; |
| 154 | + } |
| 155 | + |
| 156 | + if (lo != 0) |
| 157 | + { |
| 158 | + partials[n + 1] = lo; |
| 159 | + n += 1; |
| 160 | + lo = 0; |
| 161 | + } |
| 162 | + |
| 163 | + hi *= 2; |
| 164 | + } |
| 165 | + |
| 166 | + while (n >= 0) |
| 167 | + { |
| 168 | + var x1 = hi; |
| 169 | + var y1 = partials[n]; |
| 170 | + n -= 1; |
| 171 | + // assert: $abs(x1) > $abs(y1) |
| 172 | + (hi, lo) = TwoSum(x1, y1); |
| 173 | + if (lo != 0) |
| 174 | + { |
| 175 | + break; // eslint-disable-line no-restricted-syntax |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // handle rounding |
| 180 | + // when the roundoff error is exactly half of the ULP for the result, we need to check one more partial to know which way to round |
| 181 | + if (n >= 0 && ((lo < 0.0 && partials[n] < 0.0) || (lo > 0.0 && partials[n] > 0.0))) |
| 182 | + { |
| 183 | + var y2 = lo * 2.0; |
| 184 | + var x2 = hi + y2; |
| 185 | + var yr = x2 - hi; |
| 186 | + if (y2 == yr) |
| 187 | + { |
| 188 | + hi = x2; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return hi; |
| 193 | + } |
| 194 | +} |
0 commit comments