Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c43fc89

Browse files
Daniel-Svenssongfoidltannergooding
authored
UInt128 improve division by 64 bit on x64 (#99747)
* Add fast path for UInt128 division on x64 when divisor is as most 64bits * Update src/libraries/System.Private.CoreLib/src/System/UInt128.cs Co-authored-by: Günther Foidl <[email protected]> * Supress warning CA2252 // This API requires opting into preview features * Check upper bits for 0 and do normal divion for x64 (faster when it is known that upper is zero such as division by constant) * Change supression to SYSLIB5004 after DivRem was changes to [Experimental] --------- Co-authored-by: Günther Foidl <[email protected]> Co-authored-by: Tanner Gooding <[email protected]>
1 parent a1d6f92 commit c43fc89

File tree

1 file changed

+15
-0
lines changed
  • src/libraries/System.Private.CoreLib/src/System

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Numerics;
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
11+
using System.Runtime.Intrinsics.X86;
1112

1213
namespace System
1314
{
@@ -1103,6 +1104,20 @@ public static UInt128 Log2(UInt128 value)
11031104
// left and right are both uint64
11041105
return left._lower / right._lower;
11051106
}
1107+
else if (X86Base.X64.IsSupported)
1108+
{
1109+
ulong highRes = 0ul;
1110+
ulong remainder = left._upper;
1111+
1112+
#pragma warning disable SYSLIB5004 // DivRem is marked as [Experimental], partly because it does not get optmized by the JIT for constant inputs
1113+
if (remainder >= right._lower)
1114+
{
1115+
(highRes, remainder) = X86Base.X64.DivRem(left._upper, 0, right._lower);
1116+
}
1117+
1118+
return new UInt128(highRes, X86Base.X64.DivRem(left._lower, remainder, right._lower).Quotient);
1119+
#pragma warning restore SYSLIB5004 // DivRem is marked as [Experimental]
1120+
}
11061121
}
11071122

11081123
if (right >= left)

0 commit comments

Comments
 (0)