Skip to content

Commit

Permalink
Ensure Hypot for double handles insignificant results
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Jun 22, 2024
1 parent 753df42 commit 3a99588
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ public static TVectorDouble HypotDouble<TVectorDouble, TVectorUInt64>(TVectorDou

TVectorUInt64 expDiff = xExp - yExp;

// Cover cases where x or y is insignifican compared to the other
TVectorDouble insignificanMask = Unsafe.BitCast<TVectorUInt64, TVectorDouble>(
TVectorUInt64.GreaterThanOrEqual(expDiff, TVectorUInt64.Create(double.SignificandLength + 1)) &
TVectorUInt64.LessThanOrEqual(expDiff, TVectorUInt64.Create(unchecked((ulong)(-double.SignificandLength - 1))))
);
TVectorDouble insignificantResult = ax + ay;

// To prevent overflow, scale down by 2^+600
TVectorUInt64 expBiasP500 = TVectorUInt64.Create(double.ExponentBias + 500);
TVectorUInt64 scaleDownMask = TVectorUInt64.GreaterThan(xExp, expBiasP500) | TVectorUInt64.GreaterThan(yExp, expBiasP500);
Expand Down Expand Up @@ -427,6 +434,7 @@ public static TVectorDouble HypotDouble<TVectorDouble, TVectorUInt64>(TVectorDou
// the inputs is NaN. Otherwise if either input
// is NaN, we return NaN

result = TVectorDouble.ConditionalSelect(insignificanMask, insignificantResult, result);
result = TVectorDouble.ConditionalSelect(nanMask, TVectorDouble.Create(double.NaN), result);
result = TVectorDouble.ConditionalSelect(infinityMask, TVectorDouble.Create(double.PositiveInfinity), result);

Expand Down

0 comments on commit 3a99588

Please sign in to comment.