Skip to content

Commit a2c3690

Browse files
[release/9.0-staging] Fix ILogB for subnormal values (#116973)
* Fix LeadingZeroCount in ILogB * Add test --------- Co-authored-by: Huo Yaoyuan <[email protected]>
1 parent ae731f6 commit a2c3690

File tree

5 files changed

+5
-3
lines changed

5 files changed

+5
-3
lines changed

src/libraries/System.Private.CoreLib/src/System/Half.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ public static int ILogB(Half x)
15681568
}
15691569

15701570
Debug.Assert(IsSubnormal(x));
1571-
return MinExponent - (BitOperations.TrailingZeroCount(x.TrailingSignificand) - BiasedExponentLength);
1571+
return MinExponent - (BitOperations.LeadingZeroCount(x.TrailingSignificand) - BiasedExponentLength);
15721572
}
15731573

15741574
return x.Exponent;

src/libraries/System.Private.CoreLib/src/System/Math.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ public static int ILogB(double x)
880880
}
881881

882882
Debug.Assert(double.IsSubnormal(x));
883-
return double.MinExponent - (BitOperations.TrailingZeroCount(x.TrailingSignificand) - double.BiasedExponentLength);
883+
return double.MinExponent - (BitOperations.LeadingZeroCount(x.TrailingSignificand) - double.BiasedExponentLength);
884884
}
885885

886886
return x.Exponent;

src/libraries/System.Private.CoreLib/src/System/MathF.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public static int ILogB(float x)
210210
}
211211

212212
Debug.Assert(float.IsSubnormal(x));
213-
return float.MinExponent - (BitOperations.TrailingZeroCount(x.TrailingSignificand) - float.BiasedExponentLength);
213+
return float.MinExponent - (BitOperations.LeadingZeroCount(x.TrailingSignificand) - float.BiasedExponentLength);
214214
}
215215

216216
return x.Exponent;

src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,7 @@ public static void FusedMultiplyAdd(double x, double y, double z, double expecte
22872287
[InlineData( 0.561760, -1)]
22882288
[InlineData( 0.774152, -1)]
22892289
[InlineData( -0.678764, -1)]
2290+
[InlineData( 1e-308, -1024)]
22902291
public static void ILogB(double value, int expectedResult)
22912292
{
22922293
Assert.Equal(expectedResult, Math.ILogB(value));

src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/MathF.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ public static void IEEERemainder()
742742
[InlineData(0.561760f, -1)]
743743
[InlineData(0.774152f, -1)]
744744
[InlineData(-0.678764f, -1)]
745+
[InlineData(1e-44f, -147)]
745746
public static void ILogB(float value, int expectedResult)
746747
{
747748
Assert.Equal(expectedResult, MathF.ILogB(value));

0 commit comments

Comments
 (0)