diff --git a/src/libraries/System.Private.CoreLib/src/System/Half.cs b/src/libraries/System.Private.CoreLib/src/System/Half.cs index 1f73acf698f795..f1e653e60e1fa5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Half.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Half.cs @@ -1536,7 +1536,7 @@ public static int ILogB(Half x) } Debug.Assert(IsSubnormal(x)); - return MinExponent - (BitOperations.TrailingZeroCount(x.TrailingSignificand) - BiasedExponentLength); + return MinExponent - (BitOperations.LeadingZeroCount(x.TrailingSignificand) - BiasedExponentLength); } return x.Exponent; diff --git a/src/libraries/System.Private.CoreLib/src/System/Math.cs b/src/libraries/System.Private.CoreLib/src/System/Math.cs index a6549b8807ae0c..b69ad824a68c76 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Math.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Math.cs @@ -895,7 +895,7 @@ public static int ILogB(double x) } Debug.Assert(double.IsSubnormal(x)); - return double.MinExponent - (BitOperations.TrailingZeroCount(x.TrailingSignificand) - double.BiasedExponentLength); + return double.MinExponent - (BitOperations.LeadingZeroCount(x.TrailingSignificand) - double.BiasedExponentLength); } return x.Exponent; diff --git a/src/libraries/System.Private.CoreLib/src/System/MathF.cs b/src/libraries/System.Private.CoreLib/src/System/MathF.cs index 834b4f5e9606ca..01f0d31f701694 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MathF.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MathF.cs @@ -210,7 +210,7 @@ public static int ILogB(float x) } Debug.Assert(float.IsSubnormal(x)); - return float.MinExponent - (BitOperations.TrailingZeroCount(x.TrailingSignificand) - float.BiasedExponentLength); + return float.MinExponent - (BitOperations.LeadingZeroCount(x.TrailingSignificand) - float.BiasedExponentLength); } return x.Exponent; diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs index 2550b706b41063..316650591fd64f 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs @@ -2287,6 +2287,7 @@ public static void FusedMultiplyAdd(double x, double y, double z, double expecte [InlineData( 0.561760, -1)] [InlineData( 0.774152, -1)] [InlineData( -0.678764, -1)] + [InlineData( 1e-308, -1024)] public static void ILogB(double value, int expectedResult) { Assert.Equal(expectedResult, Math.ILogB(value)); diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/MathF.cs b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/MathF.cs index 1363a2b407b016..e5d2b224734b13 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/MathF.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/MathF.cs @@ -742,6 +742,7 @@ public static void IEEERemainder() [InlineData(0.561760f, -1)] [InlineData(0.774152f, -1)] [InlineData(-0.678764f, -1)] + [InlineData(1e-44f, -147)] public static void ILogB(float value, int expectedResult) { Assert.Equal(expectedResult, MathF.ILogB(value));