Skip to content
Prev Previous commit
Next Next commit
Check upper bits for 0 and do normal divion for x64 (faster when it i…
…s known that upper is zero such as division by constant)
  • Loading branch information
Daniel-Svensson committed Jun 11, 2024
commit 19b92d90876a5cc014e8149b2dbf8356623659ba
12 changes: 6 additions & 6 deletions src/libraries/System.Private.CoreLib/src/System/UInt128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,12 @@ public static UInt128 Log2(UInt128 value)
ThrowHelper.ThrowDivideByZeroException();
}

if (X86Base.X64.IsSupported)
if (left._upper == 0)
{
// left and right are both uint64
return left._lower / right._lower;
}
else if (X86Base.X64.IsSupported)
{
ulong highRes = 0ul;
ulong remainder = left._upper;
Expand All @@ -1113,11 +1118,6 @@ public static UInt128 Log2(UInt128 value)
return new UInt128(highRes, X86Base.X64.DivRem(left._lower, remainder, right._lower).Quotient);
#pragma warning restore CA2252 // This API requires opting into preview features
}
else if (left._upper == 0)
{
// left and right are both uint64
return left._lower / right._lower;
}
}

if (right >= left)
Expand Down