From 3a995881268f6f06b3be8e0dacc6da9127a6fa82 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Fri, 21 Jun 2024 19:28:07 -0700 Subject: [PATCH] Ensure Hypot for double handles insignificant results --- .../src/System/Runtime/Intrinsics/VectorMath.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/VectorMath.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/VectorMath.cs index a85fe13b626b71..711093935cb10a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/VectorMath.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/VectorMath.cs @@ -336,6 +336,13 @@ public static TVectorDouble HypotDouble(TVectorDou TVectorUInt64 expDiff = xExp - yExp; + // Cover cases where x or y is insignifican compared to the other + TVectorDouble insignificanMask = Unsafe.BitCast( + 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); @@ -427,6 +434,7 @@ public static TVectorDouble HypotDouble(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);