Skip to content

Commit de1d9b4

Browse files
committed
Ensure Hypot for double handles insignificant results
1 parent 753df42 commit de1d9b4

File tree

1 file changed

+8
-0
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics

1 file changed

+8
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/VectorMath.cs

+8
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ public static TVectorDouble HypotDouble<TVectorDouble, TVectorUInt64>(TVectorDou
336336

337337
TVectorUInt64 expDiff = xExp - yExp;
338338

339+
// Cover cases where x or y is insignifican compared to the other
340+
TVectorDouble insignificanMask = Unsafe.BitCast<TVectorUInt64, TVectorDouble>(
341+
TVectorUInt64.GreaterThanOrEqual(expDiff, TVectorUInt64.Create(SignificandLength + 1)) &
342+
TVectorUInt64.LessThanOrEqual(expDiff, TVectorUInt64.Create(unchecked((ulong)(-SignificandLength - 1))))
343+
);
344+
TVectorDouble insignificantResult = ax + ay;
345+
339346
// To prevent overflow, scale down by 2^+600
340347
TVectorUInt64 expBiasP500 = TVectorUInt64.Create(double.ExponentBias + 500);
341348
TVectorUInt64 scaleDownMask = TVectorUInt64.GreaterThan(xExp, expBiasP500) | TVectorUInt64.GreaterThan(yExp, expBiasP500);
@@ -427,6 +434,7 @@ public static TVectorDouble HypotDouble<TVectorDouble, TVectorUInt64>(TVectorDou
427434
// the inputs is NaN. Otherwise if either input
428435
// is NaN, we return NaN
429436

437+
result = TVectorDouble.ConditionalSelect(insignificanMask, insignificantResult, result);
430438
result = TVectorDouble.ConditionalSelect(nanMask, TVectorDouble.Create(double.NaN), result);
431439
result = TVectorDouble.ConditionalSelect(infinityMask, TVectorDouble.Create(double.PositiveInfinity), result);
432440

0 commit comments

Comments
 (0)